Mercurial > repos > public > sbplib_julia
comparison ext/SbplibMakieExt.jl @ 1668:bcc2be934326
Merge feature/grids/plotting
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 29 Jun 2024 17:05:37 +0200 |
parents | e2e468c45ed6 |
children |
comparison
equal
deleted
inserted
replaced
1666:5938f713e0d6 | 1668:bcc2be934326 |
---|---|
1 module SbplibMakieExt | |
2 | |
3 using Sbplib.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),:),) # (map(Point,collect(g)[:]),) | |
37 function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,2}) | |
38 M = collect(g) | |
39 | |
40 function cat_with_NaN(a,b) | |
41 vcat(a,[@SVector[NaN,NaN]],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{<:Any,2})) = 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 |