changeset 1632:6a898e9bce62 feature/grids/manifolds

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 bf5927631b21
children 35f8e1adb010
files ext/SbplibMakieExt.jl
diffstat 1 files changed, 38 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
diff -r bf5927631b21 -r 6a898e9bce62 ext/SbplibMakieExt.jl
--- a/ext/SbplibMakieExt.jl	Mon Jun 24 08:40:57 2024 +0200
+++ b/ext/SbplibMakieExt.jl	Mon Jun 24 21:36:17 2024 +0200
@@ -34,40 +34,7 @@
 end
 
 
-function make_plot(g,gf)
-    v,f,c = verticies_and_faces_and_values(g,gf)
-    mesh(v,f,color=c,
-        shading = NoShading,
-    )
-end
-
-# scatter(collect(g)[:])
-
-function Makie.surface(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}; kwargs...)
-    surface(getindex.(g,1), getindex.(g,2), gf;
-        shading = NoShading,
-        kwargs...,
-    )
-end
-
-function Makie.mesh(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}; kwargs...)
-    v,f,c = verticies_and_faces_and_values(g, gf)
-    mesh(v,f,color=c,
-        shading = NoShading,
-        kwargs...,
-    )
-end
-
-function Makie.plot!(plot::Plot(Grid{<:Any,2},AbstractArray{<:Any, 2}))
-    # TODO: How to handle kwargs?
-    # v,f,c = verticies_and_faces_and_values(plot[1], plot[2])
-    r = @lift verticies_and_faces_and_values($(plot[1]), $(plot[2]))
-    v,f,c = (@lift $r[1]), (@lift $r[2]), (@lift $r[3])
-    mesh!(plot, v, f, color=c,
-        shading = NoShading,
-    )
-end
-
+## Grids
 
 Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (reshape(map(Point,g),:),) # (map(Point,collect(g)[:]),)
 function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,2})
@@ -84,5 +51,41 @@
 end
 
 Makie.plot!(plot::Plot(Grid{<:Any,2})) = lines!(plot, plot.attributes, plot[1])
-# Makie.convert_arguments(::PointBased, g::Grid) = (map(Tuple,collect(g)[:]),)
+
+
+## Grid functions
+
+### 1D
+function Makie.convert_arguments(::Type{<:Lines}, g::Grid{<:Any,1}, gf::AbstractArray{<:Any, 1})
+    (collect(g), gf)
+end
+
+function Makie.convert_arguments(::Type{<:Scatter}, g::Grid{<:Any,1}, gf::AbstractArray{<:Any, 1})
+    (collect(g), gf)
+end
+
+Makie.plot!(plot::Plot(Grid{<:Any,1}, AbstractArray{<:Any,1})) = lines!(plot, plot.attributes, plot[1], plot[2])
+
+### 2D
+function Makie.convert_arguments(::Type{<:Surface}, g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2})
+    (getindex.(g,1), getindex.(g,2), gf)
 end
+
+function Makie.plot!(plot::Plot(Grid{<:Any,2},AbstractArray{<:Any, 2}))
+    r = @lift verticies_and_faces_and_values($(plot[1]), $(plot[2]))
+    v,f,c = (@lift $r[1]), (@lift $r[2]), (@lift $r[3])
+    mesh!(plot, plot.attributes, v, f;
+        color=c,
+        shading = NoShading,
+    )
+end
+# TBD: Can we define `mesh` instead of the above function and then forward plot! to that?
+
+function Makie.convert_arguments(::Type{<:Scatter}, g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2})
+    ps = map(g,gf) do (x,y), z
+        @SVector[x,y,z]
+    end
+    (reshape(ps,:),)
+end
+
+end