changeset 1633:35f8e1adb010 feature/grids/manifolds

Merge
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 24 Jun 2024 21:36:27 +0200
parents 6a898e9bce62 (diff) 84c3b9d71218 (current diff)
children e213bd857f3f
files
diffstat 1 files changed, 49 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/ext/SbplibMakieExt.jl	Fri Jun 14 13:09:00 2024 +0200
+++ b/ext/SbplibMakieExt.jl	Mon Jun 24 21:36:27 2024 +0200
@@ -2,6 +2,7 @@
 
 using Sbplib.Grids
 using Makie
+using StaticArrays
 
 
 function verticies_and_faces_and_values(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2})
@@ -33,39 +34,58 @@
 end
 
 
-function make_plot(g,gf)
-    v,f,c = verticies_and_faces_and_values(g,gf)
-    mesh(v,f,color=c,
+## 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})
+    M = collect(g)
+
+    function cat_with_NaN(a,b)
+        vcat(a,[@SVector[NaN,NaN]],b)
+    end
+
+    xlines = reduce(cat_with_NaN, eachrow(M))
+    ylines = reduce(cat_with_NaN, eachcol(M))
+
+    return (cat_with_NaN(xlines,ylines),)
+end
+
+Makie.plot!(plot::Plot(Grid{<:Any,2})) = lines!(plot, plot.attributes, plot[1])
+
+
+## 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
-
-# scatter(collect(g)[:])
+# TBD: Can we define `mesh` instead of the above function and then forward plot! to that?
 
-function Makie.surface(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}; kwargs...)
-    surface(getindex.(g,1), getindex.(g,2), gf;
-        shading = NoShading,
-        kwargs...,
-    )
+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
 
-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
-
-Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (map(Tuple,collect(g)[:]),)
-end