Mercurial > repos > public > sbplib_julia
changeset 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 | d9d767980d6f |
children | d4a6f9effcdd |
files | Project.toml ext/SbplibMakieExt.jl ext/SbplibPlotsExt.jl |
diffstat | 3 files changed, 151 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/Project.toml Fri Apr 26 22:19:02 2024 +0200 +++ b/Project.toml Fri Apr 26 22:19:30 2024 +0200 @@ -8,5 +8,13 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" TiledIteration = "06e1c1a7-607b-532d-9fad-de7d9aa2abac" +[weakdeps] +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" +Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" + +[extensions] +SbplibPlotsExt = "Plots" +SbplibMakieExt = "Makie" + [compat] julia = "1.5"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ext/SbplibMakieExt.jl Fri Apr 26 22:19:30 2024 +0200 @@ -0,0 +1,71 @@ +module SbplibMakieExt + +using Sbplib.Grids +using Makie + + +function verticies_and_faces_and_values(g::Grid{<:Any,2}, gf::AbstractArray{<:Any, 2}) + ps = map(Tuple, g)[:] + values = gf[:] + + N = length(ps) + + faces = Vector{NTuple{3,Int}}() + + n,m = size(g) + Li = LinearIndices((1:n, 1:m)) + for i ∈ 1:n-1, j = 1:m-1 + + # Add point in the middle of the patch to preserve symmetries + push!(ps, Tuple((g[i,j] + g[i+1,j] + g[i+1,j+1] + g[i,j+1])/4)) + push!(values, (gf[i,j] + gf[i+1,j] + gf[i+1,j+1] + gf[i,j+1])/4) + + push!(faces, (Li[i,j], Li[i+1,j], length(ps))) + push!(faces, (Li[i+1,j], Li[i+1,j+1], length(ps))) + push!(faces, (Li[i+1,j+1], Li[i,j+1], length(ps))) + push!(faces, (Li[i,j+1], Li[i,j], length(ps))) + end + + verticies = permutedims(reinterpret(reshape,eltype(eltype(ps)), ps)) + faces = permutedims(reinterpret(reshape,Int, faces)) + + return verticies, faces, values +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 + +Makie.convert_arguments(::Type{<:Scatter}, g::Grid) = (map(Tuple,collect(g)[:]),) +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ext/SbplibPlotsExt.jl Fri Apr 26 22:19:30 2024 +0200 @@ -0,0 +1,72 @@ +module SbplibPlotsExt + +using Sbplib.Grids +using Plots + +@recipe f(::Type{<:Grid}, g::Grid) = map(Tuple,g)[:] + +@recipe function f(c::Chart{2,<:Rectangle}, n=5, m=n; draw_border=true, bordercolor=1) + Ξ = parameterspace(c) + ξs = range(limits(Ξ,1)..., n) + ηs = range(limits(Ξ,2)..., m) + + label := false + seriescolor --> 2 + for ξ ∈ ξs + @series adapted_curve_grid(η->c((ξ,η)),limits(Ξ,1)) + end + + for η ∈ ηs + @series adapted_curve_grid(ξ->c((ξ,η)),limits(Ξ,2)) + end + + if ~draw_border + return + end + + for ξ ∈ limits(Ξ,1) + @series begin + linewidth --> 3 + seriescolor := bordercolor + adapted_curve_grid(η->c((ξ,η)),limits(Ξ,1)) + end + end + + for η ∈ limits(Ξ,2) + @series begin + linewidth --> 3 + seriescolor := bordercolor + adapted_curve_grid(ξ->c((ξ,η)),limits(Ξ,2)) + end + end +end + +function adapted_curve_grid(g, minmax) + t1, _ = PlotUtils.adapted_grid(t->g(t)[1], minmax) + t2, _ = PlotUtils.adapted_grid(t->g(t)[2], minmax) + + ts = sort(vcat(t1,t2)) + + x = map(ts) do t + g(t)[1] + end + y = map(ts) do t + g(t)[2] + end + + return x, y +end + +# get_axis_limits(plt, :x) + + +# ReicpesPipline/src/user_recipe.jl +# @recipe function f(f::FuncOrFuncs{F}) where {F<:Function} + +# @recipe function f(f::Function, xmin::Number, xmax::Number) + +# _scaled_adapted_grid(f, xscale, yscale, xmin, xmax) + +end + +