comparison ext/SbplibMakieExt.jl @ 1583:2a9ec1e2abad feature/grids/manifolds

Add package extensions for Makie and Plots to plot grids and charts
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 26 Apr 2024 22:19:30 +0200
parents
children bf5927631b21
comparison
equal deleted inserted replaced
1582:d9d767980d6f 1583:2a9ec1e2abad
1 module SbplibMakieExt
2
3 using Sbplib.Grids
4 using Makie
5
6
7 function verticies_and_faces_and_values(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2})
8 ps = map(Tuple, g)[:]
9 values = gf[:]
10
11 N = length(ps)
12
13 faces = Vector{NTuple{3,Int}}()
14
15 n,m = size(g)
16 Li = LinearIndices((1:n, 1:m))
17 for i ∈ 1:n-1, j = 1:m-1
18
19 # Add point in the middle of the patch to preserve symmetries
20 push!(ps, Tuple((g[i,j] + g[i+1,j] + g[i+1,j+1] + g[i,j+1])/4))
21 push!(values, (gf[i,j] + gf[i+1,j] + gf[i+1,j+1] + gf[i,j+1])/4)
22
23 push!(faces, (Li[i,j], Li[i+1,j], length(ps)))
24 push!(faces, (Li[i+1,j], Li[i+1,j+1], length(ps)))
25 push!(faces, (Li[i+1,j+1], Li[i,j+1], length(ps)))
26 push!(faces, (Li[i,j+1], Li[i,j], length(ps)))
27 end
28
29 verticies = permutedims(reinterpret(reshape,eltype(eltype(ps)), ps))
30 faces = permutedims(reinterpret(reshape,Int, faces))
31
32 return verticies, faces, values
33 end
34
35
36 function make_plot(g,gf)
37 v,f,c = verticies_and_faces_and_values(g,gf)
38 mesh(v,f,color=c,
39 shading = NoShading,
40 )
41 end
42
43 # scatter(collect(g)[:])
44
45 function Makie.surface(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}; kwargs...)
46 surface(getindex.(g,1), getindex.(g,2), gf;
47 shading = NoShading,
48 kwargs...,
49 )
50 end
51
52 function Makie.mesh(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}; kwargs...)
53 v,f,c = verticies_and_faces_and_values(g, gf)
54 mesh(v,f,color=c,
55 shading = NoShading,
56 kwargs...,
57 )
58 end
59
60 function Makie.plot!(plot::Plot(Grid{<:Any,2},AbstractArray{<:Any, 2}))
61 # TODO: How to handle kwargs?
62 # v,f,c = verticies_and_faces_and_values(plot[1], plot[2])
63 r = @lift verticies_and_faces_and_values($(plot[1]), $(plot[2]))
64 v,f,c = (@lift $r[1]), (@lift $r[2]), (@lift $r[3])
65 mesh!(plot, v, f, color=c,
66 shading = NoShading,
67 )
68 end
69
70 Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (map(Tuple,collect(g)[:]),)
71 end