diff --git a/docs/src/api.md b/docs/src/api.md index fd3fbca..d5a20ec 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -14,9 +14,11 @@ save default_plotter default_plotter! plottertype +PlotterType PythonPlotType PyPlotType -MakieType +AbstractPythonPlotterType +AbstractMakieType PlotsType PlutoVistaType VTKViewType diff --git a/ext/GridVisualizeMakieExt.jl b/ext/GridVisualizeMakieExt.jl index 9ff52a7..5c75263 100644 --- a/ext/GridVisualizeMakieExt.jl +++ b/ext/GridVisualizeMakieExt.jl @@ -15,7 +15,7 @@ using Interpolations: linear_interpolation using IntervalSets import GridVisualize: initialize!, save, reveal, gridplot!, scalarplot!, vectorplot!, streamplot!, customplot!, movie, plot_triangulateio! -using GridVisualize: MakieType, GridVisualizer, SubVisualizer +using GridVisualize: AbstractMakieType, GridVisualizer, SubVisualizer using GridVisualize: isolevels, cellcolors, num_cellcolors, vectorsample, quiverdata, regionmesh, bfacesegments using ExtendableGrids @@ -24,7 +24,7 @@ using Observables include("flippablelayout.jl") -function initialize!(p::GridVisualizer, ::Type{MakieType}) +function initialize!(p::GridVisualizer, ::Type{MakieType}) where {MakieType <: AbstractMakieType} XMakie = p.context[:Plotter] # Prepare flippable layout @@ -57,7 +57,7 @@ end add_scene!(ctx, ax) = ctx[:flayout][ctx[:subplot]...] = ax # Revealing the visualizer just returns the figure -function reveal(p::GridVisualizer, ::Type{MakieType}) +function reveal(p::GridVisualizer, ::Type{MakieType}) where {MakieType <: AbstractMakieType} XMakie = p.context[:Plotter] layout = p.context[:layout] # For 1D plots the legend should be rendered only once, @@ -84,7 +84,7 @@ function reveal(p::GridVisualizer, ::Type{MakieType}) end end -function reveal(ctx::SubVisualizer, TP::Type{MakieType}) +function reveal(ctx::SubVisualizer, TP::Type{MakieType}) where {MakieType <: AbstractMakieType} FlippableLayout.yieldwait(ctx[:flayout]) if ctx[:show] || ctx[:reveal] return reveal(ctx[:GridVisualizer], TP) @@ -92,10 +92,10 @@ function reveal(ctx::SubVisualizer, TP::Type{MakieType}) return nothing end -function save(fname, p::GridVisualizer, ::Type{MakieType}) +function save(fname, p::GridVisualizer, ::Type{MakieType}) where {MakieType <: AbstractMakieType} return p.context[:Plotter].save(fname, p.context[:figure]) end -function save(fname, scene, XMakie, ::Type{MakieType}) +function save(fname, scene, XMakie, ::Type{MakieType}) where {MakieType <: AbstractMakieType} return isnothing(scene) ? nothing : XMakie.save(fname, scene) end @@ -107,7 +107,7 @@ function movie( file = nothing, format = "gif", kwargs..., - ) + ) where {MakieType <: AbstractMakieType} Plotter = vis.context[:Plotter] if !isnothing(file) format = lstrip(splitext(file)[2], '.') @@ -275,7 +275,7 @@ function scenecorners1d(grid, gridscale) return [Point2f(xmin * gridscale, -5 * h), Point2f(xmax * gridscale, 5 * h)] end -function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid) +function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] nregions = num_cellregions(grid) nbregions = num_bfaceregions(grid) @@ -352,7 +352,7 @@ end ######################################################################## # 1D function -function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid, funcs) +function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid, funcs) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] nfuncs = length(funcs) @@ -598,7 +598,7 @@ function set_plot_data!(ctx, key, data) return haskey(ctx, key) ? ctx[key][] = data : ctx[key] = Observable(data) end -function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid) +function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] nregions = num_cellcolors(grid, ctx[:cellcoloring]) @@ -711,7 +711,7 @@ function makescene2d(ctx, key) end # 2D function -function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grids, parentgrid, funcs) +function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grids, parentgrid, funcs) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] gridscale = ctx[:gridscale] @@ -849,7 +849,7 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grids, parentgrid end # 2D vector -function vectorplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func) +function vectorplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] rc, rv = vectorsample(grid, func; gridscale = ctx[:gridscale], rasterpoints = ctx[:rasterpoints], offset = ctx[:offset]) @@ -894,7 +894,7 @@ function vectorplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func) return reveal(ctx, TP) end -function streamplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func) +function streamplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] rc, rv = vectorsample( @@ -1060,7 +1060,7 @@ pgup/pgdown: coarse control control value h: print this message """ -function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grid) +function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grid) where {MakieType <: AbstractMakieType} function make_mesh(pts, fcs) if pkgversion(GeometryBasics) < v"0.5" return GeometryBasics.Mesh(meta(pts; normals = normals(pts, fcs)), fcs) @@ -1253,7 +1253,7 @@ function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grid) end # 3d function -function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid, funcs) +function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid, funcs) where {MakieType <: AbstractMakieType} levels, crange, colorbarticks = isolevels(ctx, funcs) ctx[:crange] = crange ctx[:colorbarticks] = colorbarticks @@ -1494,7 +1494,7 @@ end # Thanks! lines(x, y, axis = (targetlimits = lims,)) indeed makes the limits update.^ # I found that autolimits!(axis) gave good results, even better than me manually computing limits! -function customplot!(ctx, TP::Type{MakieType}, func) +function customplot!(ctx, TP::Type{MakieType}, func) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] if !haskey(ctx, :scene) ctx[:scene] = XMakie.Axis( @@ -1519,7 +1519,7 @@ function plot_triangulateio!( voronoi = nothing, circumcircles = false, kwargs... - ) + ) where {MakieType <: AbstractMakieType} XMakie = ctx[:Plotter] function frgb(Plotter, i, max; pastel = false) diff --git a/src/GridVisualize.jl b/src/GridVisualize.jl index d63776f..9bfca69 100644 --- a/src/GridVisualize.jl +++ b/src/GridVisualize.jl @@ -35,7 +35,7 @@ export isplots, isvtkview, ispyplot, ispythonplot, ismakie, isplutovista export GridVisualizer, SubVisualizer export plottertype, available_kwargs export default_plotter!, default_plotter -export PyPlotType, PythonPlotType, MakieType, PlotsType, VTKViewType, PlutoVistaType, MeshCatType +export PlotterType, PyPlotType, PythonPlotType, AbstractPythonPlotterType, AbstractMakieType, PlotsType, VTKViewType, PlutoVistaType, MeshCatType export movie end diff --git a/src/dispatch.jl b/src/dispatch.jl index 2d50129..c3bb399 100644 --- a/src/dispatch.jl +++ b/src/dispatch.jl @@ -77,89 +77,79 @@ Abstract type for dispatching on plotter """ abstract type AbstractPlotterType end +""" +$(TYPEDEF) -abstract type AbstractPythonPlotterType <: AbstractPlotterType end +Singleton type for dispatching on plotter +""" +struct PlotterType{T} <: AbstractPlotterType end """ -$(TYPEDEF) + const PyPlotType = PlotterType{:PyPlot} -Abstract type for dispatching on plotter +Shorthand for PyPlot PlotterType Singleton. """ -abstract type PyPlotType <: AbstractPythonPlotterType end +const PyPlotType = PlotterType{:PyPlot} """ -$(TYPEDEF) + const PythonPlotType = PlotterType{:PythonPlot} -Abstract type for dispatching on plotter +Shorthand for PythonPlot PlotterType Singleton. """ -abstract type PythonPlotType <: AbstractPythonPlotterType end +const PythonPlotType = PlotterType{:PythonPlot} """ -$(TYPEDEF) + const PlotsType = PlotterType{:Plots} -Abstract type for dispatching on plotter +Shorthand for Plots PlotterType Singleton. """ -abstract type MakieType <: AbstractPlotterType end +const PlotsType = PlotterType{:Plots} """ -$(TYPEDEF) + const PlutoVistaType = PlotterType{:PlutoVista} -Abstract type for dispatching on plotter +Shorthand for PlutoVista PlotterType Singleton. """ -abstract type PlotsType <: AbstractPlotterType end +const PlutoVistaType = PlotterType{:PlutoVista} """ -$(TYPEDEF) + const VTKViewType = PlotterType{:VTKView} -Abstract type for dispatching on plotter. Experimental. +Shorthand for VTKView PlotterType Singleton. Experimental """ -abstract type VTKViewType <: AbstractPlotterType end +const VTKViewType = PlotterType{:VTKView} """ -$(TYPEDEF) + const MeshCatType = PlotterType{:MeshCat} -Abstract type for dispatching on plotter. Experimental. +Shorthand for MeshCat PlotterType Singleton. Experimental """ -abstract type MeshCatType <: AbstractPlotterType end +const MeshCatType = PlotterType{:MeshCat} """ -$(TYPEDEF) + const AbstractPythonPlotterType = Union{PyPlotType,PythonPlotType} -Abstract type for dispatching on plotter +Parent type for dispatch on Python plotters. +""" +const AbstractPythonPlotterType = Union{PyPlotType, PythonPlotType} + +""" + const AbstractMakieType = Union{PlotterType{:CairoMakie},PlotterType{:WGLMakie},PlotterType{:RPRMakie}} + +Parent type for dispatch on Makie plotters. """ -abstract type PlutoVistaType <: AbstractPlotterType end +const AbstractMakieType = Union{PlotterType{:CairoMakie}, PlotterType{:WGLMakie}, PlotterType{:RPRMakie}} """ $(SIGNATURES) - -Heuristically detect type of plotter, returns the corresponding abstract type for plotting. -""" -function plottertype(Plotter::Union{Module, Nothing}) - if ismakie(Plotter) - return MakieType - elseif isplots(Plotter) - return PlotsType - elseif ispyplot(Plotter) - return PyPlotType - elseif ispythonplot(Plotter) - return PythonPlotType - elseif isvtkview(Plotter) - return VTKViewType - elseif ismeshcat(Plotter) - return MeshCatType - elseif isplutovista(Plotter) - return PlutoVistaType - end - return Nothing -end -plottername(::Type{MakieType}) = "Makie" -plottername(::Type{PlotsType}) = "Plots" -plottername(::Type{PyPlotType}) = "PyPlot" -plottername(::Type{PythonPlotType}) = "PythonPlot" -plottername(::Type{PlutoVistaType}) = "PlutoVista" -plottername(::Type{VTKViewType}) = "VTKView" -plottername(::Type{MeshCatType}) = "MeshCat" +Obtain Singleton type of given Plotter. +""" +plottertype(Plotter::Module) = PlotterType{nameof(Plotter)} +plottertype(::Nothing) = Type{Nothing} + + +plottername(::PlotterType{T}) where {T} = String(T) plottername(::Type{Nothing}) = "nothing" plottername(p::Union{Module, Nothing}) = plottertype(p) |> plottername