comparison ext/SbplibMakieExt.jl @ 1630:27d270c9cb89 feature/grids/plotting

Fix Makie recipes for plotting grids and grid functions
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 24 Jun 2024 21:36:17 +0200
parents 2fab141e4a9a
children e2e468c45ed6
comparison
equal deleted inserted replaced
1629:2fab141e4a9a 1630:27d270c9cb89
32 32
33 return verticies, faces, values 33 return verticies, faces, values
34 end 34 end
35 35
36 36
37 function make_plot(g,gf) 37 ## Grids
38 v,f,c = verticies_and_faces_and_values(g,gf)
39 mesh(v,f,color=c,
40 shading = NoShading,
41 )
42 end
43
44 # scatter(collect(g)[:])
45
46 function Makie.surface(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}; kwargs...)
47 surface(getindex.(g,1), getindex.(g,2), gf;
48 shading = NoShading,
49 kwargs...,
50 )
51 end
52
53 function Makie.mesh(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}; kwargs...)
54 v,f,c = verticies_and_faces_and_values(g, gf)
55 mesh(v,f,color=c,
56 shading = NoShading,
57 kwargs...,
58 )
59 end
60
61 function Makie.plot!(plot::Plot(Grid{<:Any,2},AbstractArray{<:Any, 2}))
62 # TODO: How to handle kwargs?
63 # v,f,c = verticies_and_faces_and_values(plot[1], plot[2])
64 r = @lift verticies_and_faces_and_values($(plot[1]), $(plot[2]))
65 v,f,c = (@lift $r[1]), (@lift $r[2]), (@lift $r[3])
66 mesh!(plot, v, f, color=c,
67 shading = NoShading,
68 )
69 end
70
71 38
72 Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (reshape(map(Point,g),:),) # (map(Point,collect(g)[:]),) 39 Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (reshape(map(Point,g),:),) # (map(Point,collect(g)[:]),)
73 function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,2}) 40 function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,2})
74 M = collect(g) 41 M = collect(g)
75 42
82 49
83 return (cat_with_NaN(xlines,ylines),) 50 return (cat_with_NaN(xlines,ylines),)
84 end 51 end
85 52
86 Makie.plot!(plot::Plot(Grid{<:Any,2})) = lines!(plot, plot.attributes, plot[1]) 53 Makie.plot!(plot::Plot(Grid{<:Any,2})) = lines!(plot, plot.attributes, plot[1])
87 # Makie.convert_arguments(::PointBased, g::Grid) = (map(Tuple,collect(g)[:]),) 54
55
56 ## Grid functions
57
58 ### 1D
59 function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,1}, gf::AbstractArray{<:Any, 1})
60 (collect(g), gf)
88 end 61 end
62
63 function Makie.convert_arguments(::Type{<:Scatter}, g::Grid{<:Any,1}, gf::AbstractArray{<:Any, 1})
64 (collect(g), gf)
65 end
66
67 Makie.plot!(plot::Plot(Grid{<:Any,1}, AbstractArray{<:Any,1})) = lines!(plot, plot.attributes, plot[1], plot[2])
68
69 ### 2D
70 function Makie.convert_arguments(::Type{<:Surface}, g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2})
71 (getindex.(g,1), getindex.(g,2), gf)
72 end
73
74 function Makie.plot!(plot::Plot(Grid{<:Any,2},AbstractArray{<:Any, 2}))
75 r = @lift verticies_and_faces_and_values($(plot[1]), $(plot[2]))
76 v,f,c = (@lift $r[1]), (@lift $r[2]), (@lift $r[3])
77 mesh!(plot, plot.attributes, v, f;
78 color=c,
79 shading = NoShading,
80 )
81 end
82 # TBD: Can we define `mesh` instead of the above function and then forward plot! to that?
83
84 function Makie.convert_arguments(::Type{<:Scatter}, g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2})
85 ps = map(g,gf) do (x,y), z
86 @SVector[x,y,z]
87 end
88 (reshape(ps,:),)
89 end
90
91 end