Mercurial > repos > public > sbplib_julia
comparison ext/DiffinitiveMakieExt.jl @ 2057:8a2a0d678d6f feature/lazy_tensors/pretty_printing
Merge default
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Tue, 10 Feb 2026 22:41:19 +0100 |
| parents | 4d55ac2892a2 |
| children |
comparison
equal
deleted
inserted
replaced
| 1110:c0bff9f6e0fb | 2057:8a2a0d678d6f |
|---|---|
| 1 module DiffinitiveMakieExt | |
| 2 | |
| 3 using Diffinitive.Grids | |
| 4 using Makie | |
| 5 using StaticArrays | |
| 6 | |
| 7 | |
| 8 function verticies_and_faces_and_values(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}) | |
| 9 ps = map(Tuple, g)[:] | |
| 10 values = gf[:] | |
| 11 faces = Vector{NTuple{3,Int}}() | |
| 12 | |
| 13 n,m = size(g) | |
| 14 Li = LinearIndices((1:n, 1:m)) | |
| 15 for i ∈ 1:n-1, j = 1:m-1 | |
| 16 | |
| 17 # Add point in the middle of the patch to preserve symmetries | |
| 18 push!(ps, Tuple((g[i,j] + g[i+1,j] + g[i+1,j+1] + g[i,j+1])/4)) | |
| 19 push!(values, (gf[i,j] + gf[i+1,j] + gf[i+1,j+1] + gf[i,j+1])/4) | |
| 20 | |
| 21 push!(faces, (Li[i,j], Li[i+1,j], length(ps))) | |
| 22 push!(faces, (Li[i+1,j], Li[i+1,j+1], length(ps))) | |
| 23 push!(faces, (Li[i+1,j+1], Li[i,j+1], length(ps))) | |
| 24 push!(faces, (Li[i,j+1], Li[i,j], length(ps))) | |
| 25 end | |
| 26 | |
| 27 verticies = permutedims(reinterpret(reshape,eltype(eltype(ps)), ps)) | |
| 28 faces = permutedims(reinterpret(reshape,Int, faces)) | |
| 29 | |
| 30 return verticies, faces, values | |
| 31 end | |
| 32 | |
| 33 | |
| 34 ## Grids | |
| 35 | |
| 36 Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (reshape(map(Point,g),:),) | |
| 37 function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:AbstractVector}) | |
| 38 M = collect(g) | |
| 39 | |
| 40 function cat_with_NaN(a,b) | |
| 41 vcat(a,[@SVector fill(NaN, coordinate_size(g))],b) | |
| 42 end | |
| 43 | |
| 44 xlines = reduce(cat_with_NaN, eachrow(M)) | |
| 45 ylines = reduce(cat_with_NaN, eachcol(M)) | |
| 46 | |
| 47 return (cat_with_NaN(xlines,ylines),) | |
| 48 end | |
| 49 | |
| 50 Makie.plot!(plot::Plot(Grid)) = lines!(plot, plot.attributes, plot[1]) | |
| 51 | |
| 52 | |
| 53 ## Grid functions | |
| 54 | |
| 55 ### 1D | |
| 56 function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,1}, gf::AbstractArray{<:Any, 1}) | |
| 57 (collect(g), gf) | |
| 58 end | |
| 59 | |
| 60 function Makie.convert_arguments(::Type{<:Scatter}, g::Grid{<:Any,1}, gf::AbstractArray{<:Any, 1}) | |
| 61 (collect(g), gf) | |
| 62 end | |
| 63 | |
| 64 Makie.plot!(plot::Plot(Grid{<:Any,1}, AbstractArray{<:Any,1})) = lines!(plot, plot.attributes, plot[1], plot[2]) | |
| 65 | |
| 66 ### 2D | |
| 67 function Makie.convert_arguments(::Type{<:Surface}, g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}) | |
| 68 (getindex.(g,1), getindex.(g,2), gf) | |
| 69 end | |
| 70 | |
| 71 function Makie.plot!(plot::Plot(Grid{<:Any,2},AbstractArray{<:Any, 2})) | |
| 72 r = @lift verticies_and_faces_and_values($(plot[1]), $(plot[2])) | |
| 73 v,f,c = (@lift $r[1]), (@lift $r[2]), (@lift $r[3]) | |
| 74 mesh!(plot, plot.attributes, v, f; | |
| 75 color=c, | |
| 76 shading = NoShading, | |
| 77 ) | |
| 78 end | |
| 79 # TBD: Can we define `mesh` instead of the above function and then forward plot! to that? | |
| 80 | |
| 81 function Makie.convert_arguments(::Type{<:Scatter}, g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}) | |
| 82 ps = map(g,gf) do (x,y), z | |
| 83 @SVector[x,y,z] | |
| 84 end | |
| 85 (reshape(ps,:),) | |
| 86 end | |
| 87 | |
| 88 end |
