Skip to content

Commit

Permalink
Use Makie.automatic for colorrange (#120)
Browse files Browse the repository at this point in the history
* Use Makie.automatic for colorrange

* Add to CellPlot and include in docstrings

* Better NaN handling.

---------

Co-authored-by: termi-official <termi-official@users.noreply.github.com>
  • Loading branch information
KnutAM and termi-official committed Apr 10, 2024
1 parent 806dc6d commit 9278bd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
34 changes: 13 additions & 21 deletions src/makieplotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ keyword arguments are:
- `deformation_field::Symbol=:default` field that transforms the mesh by the given deformation, defaults to no deformation
- `process::Function=postprocess` function to construct nodal scalar values from a vector valued problem
- `colormap::Symbol=:cividis`
- `colorrange::NTuple{2,<:Number}`: Specify (min, max) of the colorscale. If not given, min and max are calculated automatically from the data.
- `deformation_scale=1.0`
- `shading=false`
- `scale_plot=false`
- `transparent=false`
- `nan_color::Union{Symbol, <:Colorant}=:red`
"""
@recipe(SolutionPlot) do scene
Attributes(
Expand All @@ -33,9 +35,10 @@ keyword arguments are:
deformation_field=:default,
process=postprocess,
colormap=:cividis,
colorrange=(0,1),
colorrange=Makie.automatic,
transparent=false,
deformation_scale = 1.0,
nan_color=:red,
)
end

Expand Down Expand Up @@ -64,20 +67,7 @@ function Makie.plot!(SP::SolutionPlot{<:Tuple{<:MakiePlotter}})
plotter.physical_coords_mesh[1:end] = plotter.physical_coords .+ ($(SP[:deformation_scale]) .* $(u_matrix))
end
end
mins = @lift(minimum(x->isnan(x) ? 1e10 : x, $solution))
maxs = @lift(maximum(x->isnan(x) ? -1e10 : x, $solution))
SP[:colorrange] = @lift begin
if isapprox($mins,$maxs)
if isapprox($mins,zero($mins)) && isapprox($maxs,zero($maxs))
(1e-18,2e-18)
else
($mins,1.01($maxs))
end
else
($mins,$maxs)
end
end
return Makie.mesh!(SP, plotter.mesh, color=solution, shading=SP[:shading], scale_plot=SP[:scale_plot], colormap=SP[:colormap],colorrange=SP[:colorrange] , transparent=SP[:transparent])
return Makie.mesh!(SP, plotter.mesh, color=solution, shading=SP[:shading], scale_plot=SP[:scale_plot], colormap=SP[:colormap],colorrange=SP[:colorrange] , transparent=SP[:transparent], nan_color=SP[:nan_color])
end

"""
Expand All @@ -91,10 +81,12 @@ keyword arguments are:
- `deformation_field::Symbol=:default` field that transforms the mesh by the given deformation, defaults to no deformation
- `process::Function=identity` function to construct cell scalar values. Defaults to `identity`, i.e. scalar values.
- `colormap::Symbol=:cividis`
- `colorrange::NTuple{2,<:Number}`: Specify (min, max) of the colorscale. If not given, min and max are calculated automatically from the data.
- `deformation_scale=1.0`
- `shading=false`
- `scale_plot=false`
- `transparent=false`
- `nan_color::Union{Symbol, <:Colorant}=:red`
"""
@recipe(CellPlot) do scene
Attributes(
Expand All @@ -103,9 +95,10 @@ keyword arguments are:
deformation_field=:default,
process=identity,
colormap=:cividis,
colorrange=(0,1),
colorrange=Makie.automatic,
transparent=false,
deformation_scale = 1.0,
nan_color=:red,
)
end

Expand All @@ -126,11 +119,8 @@ function Makie.plot!(CP::CellPlot{<:Tuple{<:MakiePlotter{dim},Vector}}) where di
plotter.physical_coords_mesh[1:end] = plotter.physical_coords .+ ($(CP[:deformation_scale]) .* $(u_matrix))
end
end
mins = minimum(qp_values)
maxs = maximum(qp_values)
CP[:colorrange] = @lift(isapprox($mins,$maxs) ? ($mins,1.01($maxs)) : ($mins,$maxs))
solution = @lift(reshape(transfer_scalar_celldata(plotter, qp_values; process=$(CP[:process])), num_vertices(plotter)))
return Makie.mesh!(CP, plotter.mesh, color=solution, shading=CP[:shading], scale_plot=CP[:scale_plot], colormap=CP[:colormap], transparent=CP[:transparent])
return Makie.mesh!(CP, plotter.mesh, color=solution, shading=CP[:shading], scale_plot=CP[:scale_plot], colormap=CP[:colormap], transparent=CP[:transparent], colorrange=CP[:colorrange], nan_color=CP[:nan_color])
end

"""
Expand Down Expand Up @@ -312,6 +302,7 @@ values are transformed to a scalar based on `process` which defaults to the magn
- `scale_plot = false`
- `shading = false`
- `colormap = :cividis`
- `nan_color::Union{Symbol, <:Colorant}=:red`
"""
@recipe(Surface) do scene
Attributes(
Expand All @@ -320,6 +311,7 @@ values are transformed to a scalar based on `process` which defaults to the magn
scale_plot = false,
shading = false,
colormap = :cividis,
nan_color=:red,
)
end

Expand All @@ -336,7 +328,7 @@ function Makie.plot!(SF::Surface{<:Tuple{<:MakiePlotter{2}}})
coords = @lift begin
Point3f[Point3f(coord[1], coord[2], $(solution)[idx]) for (idx, coord) in enumerate(plotter.physical_coords)]
end
return Makie.mesh!(SF, coords, plotter.vis_triangles, color=solution, scale_plot=SF[:scale_plot], shading=SF[:shading], colormap=SF[:colormap])
return Makie.mesh!(SF, coords, plotter.vis_triangles, color=solution, scale_plot=SF[:scale_plot], shading=SF[:shading], colormap=SF[:colormap], nan_color=SF[:nan_color])
end

"""
Expand Down
6 changes: 3 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,13 @@ function uniform_refinement(plotter::MakiePlotter{dim,DH,T1,TOP,T2,M,TRI}) where
n_visible = sum(plotter.visible[refined_triangle_cell_map])
n_notvisible = length(refined_triangles) - n_visible
refined_vis_triangles[ .! plotter.visible[refined_triangle_cell_map]] .= (GeometryBasics.GLTriangleFace(1,1,1) for i in 1:n_notvisible)
refined_vis_triangles = ShaderAbstractions.Buffer(Makie.Observable(refined_triangles))
refined_vis_triangles_m = ShaderAbstractions.Buffer(Makie.Observable(refined_vis_triangles))
refined_physical_coords_m = ShaderAbstractions.Buffer(Makie.Observable(copy(refined_physical_coords)))
refined_mesh = GeometryBasics.Mesh(refined_physical_coords_m, refined_vis_triangles)
refined_mesh = GeometryBasics.Mesh(refined_physical_coords_m, refined_vis_triangles_m)

return MakiePlotter{dim,DH,T1,TOP,T2,M,TRI}(
plotter.dh, plotter.u, plotter.topology, plotter.visible, plotter.gridnodes,
refined_physical_coords, refined_physical_coords_m, refined_triangles, refined_vis_triangles, refined_triangle_cell_map,
refined_physical_coords, refined_physical_coords_m, refined_triangles, refined_vis_triangles_m, refined_triangle_cell_map,
plotter.cell_triangle_offsets .* 4, refined_reference_coords, refined_mesh
)
end
Expand Down

0 comments on commit 9278bd1

Please sign in to comment.