Skip to content

Commit ad92376

Browse files
committed
Replace plottertypes by Singleton
1 parent a97adce commit ad92376

File tree

4 files changed

+61
-69
lines changed

4 files changed

+61
-69
lines changed

docs/src/api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ save
1414
default_plotter
1515
default_plotter!
1616
plottertype
17+
PlotterType
1718
PythonPlotType
1819
PyPlotType
19-
MakieType
20+
AbstractPythonPlotterType
21+
AbstractMakieType
2022
PlotsType
2123
PlutoVistaType
2224
VTKViewType

ext/GridVisualizeMakieExt.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using Interpolations: linear_interpolation
1515
using IntervalSets
1616

1717
import GridVisualize: initialize!, save, reveal, gridplot!, scalarplot!, vectorplot!, streamplot!, customplot!, movie, plot_triangulateio!
18-
using GridVisualize: MakieType, GridVisualizer, SubVisualizer
18+
using GridVisualize: AbstractMakieType, GridVisualizer, SubVisualizer
1919
using GridVisualize: isolevels, cellcolors, num_cellcolors, vectorsample, quiverdata, regionmesh, bfacesegments
2020

2121
using ExtendableGrids
@@ -24,7 +24,7 @@ using Observables
2424

2525
include("flippablelayout.jl")
2626

27-
function initialize!(p::GridVisualizer, ::Type{MakieType})
27+
function initialize!(p::GridVisualizer, ::Type{MakieType}) where {MakieType <: AbstractMakieType}
2828
XMakie = p.context[:Plotter]
2929

3030
# Prepare flippable layout
@@ -57,7 +57,7 @@ end
5757
add_scene!(ctx, ax) = ctx[:flayout][ctx[:subplot]...] = ax
5858

5959
# Revealing the visualizer just returns the figure
60-
function reveal(p::GridVisualizer, ::Type{MakieType})
60+
function reveal(p::GridVisualizer, ::Type{MakieType}) where {MakieType <: AbstractMakieType}
6161
XMakie = p.context[:Plotter]
6262
layout = p.context[:layout]
6363
# For 1D plots the legend should be rendered only once,
@@ -84,18 +84,18 @@ function reveal(p::GridVisualizer, ::Type{MakieType})
8484
end
8585
end
8686

87-
function reveal(ctx::SubVisualizer, TP::Type{MakieType})
87+
function reveal(ctx::SubVisualizer, TP::Type{MakieType}) where {MakieType <: AbstractMakieType}
8888
FlippableLayout.yieldwait(ctx[:flayout])
8989
if ctx[:show] || ctx[:reveal]
9090
return reveal(ctx[:GridVisualizer], TP)
9191
end
9292
return nothing
9393
end
9494

95-
function save(fname, p::GridVisualizer, ::Type{MakieType})
95+
function save(fname, p::GridVisualizer, ::Type{MakieType}) where {MakieType <: AbstractMakieType}
9696
return p.context[:Plotter].save(fname, p.context[:figure])
9797
end
98-
function save(fname, scene, XMakie, ::Type{MakieType})
98+
function save(fname, scene, XMakie, ::Type{MakieType}) where {MakieType <: AbstractMakieType}
9999
return isnothing(scene) ? nothing : XMakie.save(fname, scene)
100100
end
101101

@@ -107,7 +107,7 @@ function movie(
107107
file = nothing,
108108
format = "gif",
109109
kwargs...,
110-
)
110+
) where {MakieType <: AbstractMakieType}
111111
Plotter = vis.context[:Plotter]
112112
if !isnothing(file)
113113
format = lstrip(splitext(file)[2], '.')
@@ -275,7 +275,7 @@ function scenecorners1d(grid, gridscale)
275275
return [Point2f(xmin * gridscale, -5 * h), Point2f(xmax * gridscale, 5 * h)]
276276
end
277277

278-
function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid)
278+
function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grid) where {MakieType <: AbstractMakieType}
279279
XMakie = ctx[:Plotter]
280280
nregions = num_cellregions(grid)
281281
nbregions = num_bfaceregions(grid)
@@ -352,7 +352,7 @@ end
352352
########################################################################
353353
# 1D function
354354

355-
function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid, funcs)
355+
function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid, funcs) where {MakieType <: AbstractMakieType}
356356
XMakie = ctx[:Plotter]
357357

358358
nfuncs = length(funcs)
@@ -598,7 +598,7 @@ function set_plot_data!(ctx, key, data)
598598
return haskey(ctx, key) ? ctx[key][] = data : ctx[key] = Observable(data)
599599
end
600600

601-
function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid)
601+
function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid) where {MakieType <: AbstractMakieType}
602602
XMakie = ctx[:Plotter]
603603

604604
nregions = num_cellcolors(grid, ctx[:cellcoloring])
@@ -711,7 +711,7 @@ function makescene2d(ctx, key)
711711
end
712712

713713
# 2D function
714-
function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grids, parentgrid, funcs)
714+
function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grids, parentgrid, funcs) where {MakieType <: AbstractMakieType}
715715
XMakie = ctx[:Plotter]
716716
gridscale = ctx[:gridscale]
717717

@@ -849,7 +849,7 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grids, parentgrid
849849
end
850850

851851
# 2D vector
852-
function vectorplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func)
852+
function vectorplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func) where {MakieType <: AbstractMakieType}
853853
XMakie = ctx[:Plotter]
854854

855855
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)
894894
return reveal(ctx, TP)
895895
end
896896

897-
function streamplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func)
897+
function streamplot!(ctx, TP::Type{MakieType}, ::Type{Val{2}}, grid, func) where {MakieType <: AbstractMakieType}
898898
XMakie = ctx[:Plotter]
899899

900900
rc, rv = vectorsample(
@@ -1060,7 +1060,7 @@ pgup/pgdown: coarse control control value
10601060
h: print this message
10611061
"""
10621062

1063-
function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grid)
1063+
function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grid) where {MakieType <: AbstractMakieType}
10641064
function make_mesh(pts, fcs)
10651065
if pkgversion(GeometryBasics) < v"0.5"
10661066
return GeometryBasics.Mesh(meta(pts; normals = normals(pts, fcs)), fcs)
@@ -1253,7 +1253,7 @@ function gridplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grid)
12531253
end
12541254

12551255
# 3d function
1256-
function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid, funcs)
1256+
function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid, funcs) where {MakieType <: AbstractMakieType}
12571257
levels, crange, colorbarticks = isolevels(ctx, funcs)
12581258
ctx[:crange] = crange
12591259
ctx[:colorbarticks] = colorbarticks
@@ -1494,7 +1494,7 @@ end
14941494
# Thanks! lines(x, y, axis = (targetlimits = lims,)) indeed makes the limits update.^
14951495
# I found that autolimits!(axis) gave good results, even better than me manually computing limits!
14961496

1497-
function customplot!(ctx, TP::Type{MakieType}, func)
1497+
function customplot!(ctx, TP::Type{MakieType}, func) where {MakieType <: AbstractMakieType}
14981498
XMakie = ctx[:Plotter]
14991499
if !haskey(ctx, :scene)
15001500
ctx[:scene] = XMakie.Axis(
@@ -1519,7 +1519,7 @@ function plot_triangulateio!(
15191519
voronoi = nothing,
15201520
circumcircles = false,
15211521
kwargs...
1522-
)
1522+
) where {MakieType <: AbstractMakieType}
15231523
XMakie = ctx[:Plotter]
15241524

15251525
function frgb(Plotter, i, max; pastel = false)

src/GridVisualize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export isplots, isvtkview, ispyplot, ispythonplot, ismakie, isplutovista
3535
export GridVisualizer, SubVisualizer
3636
export plottertype, available_kwargs
3737
export default_plotter!, default_plotter
38-
export PyPlotType, PythonPlotType, MakieType, PlotsType, VTKViewType, PlutoVistaType, MeshCatType
38+
export PlotterType, PyPlotType, PythonPlotType, AbstractPythonPlotterType, AbstractMakieType, PlotsType, VTKViewType, PlutoVistaType, MeshCatType
3939
export movie
4040

4141
end

src/dispatch.jl

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -77,89 +77,79 @@ Abstract type for dispatching on plotter
7777
"""
7878
abstract type AbstractPlotterType end
7979

80+
"""
81+
$(TYPEDEF)
8082
81-
abstract type AbstractPythonPlotterType <: AbstractPlotterType end
83+
Singleton type for dispatching on plotter
84+
"""
85+
struct PlotterType{T} <: AbstractPlotterType end
8286

8387
"""
84-
$(TYPEDEF)
88+
const PyPlotType = PlotterType{:PyPlot}
8589
86-
Abstract type for dispatching on plotter
90+
Shorthand for PyPlot PlotterType Singleton.
8791
"""
88-
abstract type PyPlotType <: AbstractPythonPlotterType end
92+
const PyPlotType = PlotterType{:PyPlot}
8993

9094
"""
91-
$(TYPEDEF)
95+
const PythonPlotType = PlotterType{:PythonPlot}
9296
93-
Abstract type for dispatching on plotter
97+
Shorthand for PythonPlot PlotterType Singleton.
9498
"""
95-
abstract type PythonPlotType <: AbstractPythonPlotterType end
99+
const PythonPlotType = PlotterType{:PythonPlot}
96100

97101
"""
98-
$(TYPEDEF)
102+
const PlotsType = PlotterType{:Plots}
99103
100-
Abstract type for dispatching on plotter
104+
Shorthand for Plots PlotterType Singleton.
101105
"""
102-
abstract type MakieType <: AbstractPlotterType end
106+
const PlotsType = PlotterType{:Plots}
103107

104108
"""
105-
$(TYPEDEF)
109+
const PlutoVistaType = PlotterType{:PlutoVista}
106110
107-
Abstract type for dispatching on plotter
111+
Shorthand for PlutoVista PlotterType Singleton.
108112
"""
109-
abstract type PlotsType <: AbstractPlotterType end
113+
const PlutoVistaType = PlotterType{:PlutoVista}
110114

111115
"""
112-
$(TYPEDEF)
116+
const VTKViewType = PlotterType{:VTKView}
113117
114-
Abstract type for dispatching on plotter. Experimental.
118+
Shorthand for VTKView PlotterType Singleton. Experimental
115119
"""
116-
abstract type VTKViewType <: AbstractPlotterType end
120+
const VTKViewType = PlotterType{:VTKView}
117121

118122
"""
119-
$(TYPEDEF)
123+
const MeshCatType = PlotterType{:MeshCat}
120124
121-
Abstract type for dispatching on plotter. Experimental.
125+
Shorthand for MeshCat PlotterType Singleton. Experimental
122126
"""
123-
abstract type MeshCatType <: AbstractPlotterType end
127+
const MeshCatType = PlotterType{:MeshCat}
124128

125129
"""
126-
$(TYPEDEF)
130+
const AbstractPythonPlotterType = Union{PyPlotType,PythonPlotType}
127131
128-
Abstract type for dispatching on plotter
132+
Parent type for dispatch on Python plotters.
133+
"""
134+
const AbstractPythonPlotterType = Union{PyPlotType, PythonPlotType}
135+
136+
"""
137+
const AbstractMakieType = Union{PlotterType{:CairoMakie},PlotterType{:WGLMakie},PlotterType{:RPRMakie}}
138+
139+
Parent type for dispatch on Makie plotters.
129140
"""
130-
abstract type PlutoVistaType <: AbstractPlotterType end
141+
const AbstractMakieType = Union{PlotterType{:CairoMakie}, PlotterType{:WGLMakie}, PlotterType{:RPRMakie}}
131142

132143
"""
133144
$(SIGNATURES)
134-
135-
Heuristically detect type of plotter, returns the corresponding abstract type for plotting.
136-
"""
137-
function plottertype(Plotter::Union{Module, Nothing})
138-
if ismakie(Plotter)
139-
return MakieType
140-
elseif isplots(Plotter)
141-
return PlotsType
142-
elseif ispyplot(Plotter)
143-
return PyPlotType
144-
elseif ispythonplot(Plotter)
145-
return PythonPlotType
146-
elseif isvtkview(Plotter)
147-
return VTKViewType
148-
elseif ismeshcat(Plotter)
149-
return MeshCatType
150-
elseif isplutovista(Plotter)
151-
return PlutoVistaType
152-
end
153-
return Nothing
154-
end
155145
156-
plottername(::Type{MakieType}) = "Makie"
157-
plottername(::Type{PlotsType}) = "Plots"
158-
plottername(::Type{PyPlotType}) = "PyPlot"
159-
plottername(::Type{PythonPlotType}) = "PythonPlot"
160-
plottername(::Type{PlutoVistaType}) = "PlutoVista"
161-
plottername(::Type{VTKViewType}) = "VTKView"
162-
plottername(::Type{MeshCatType}) = "MeshCat"
146+
Obtain Singleton type of given Plotter.
147+
"""
148+
plottertype(Plotter::Module) = PlotterType{nameof(Plotter)}
149+
plottertype(::Nothing) = Type{Nothing}
150+
151+
152+
plottername(::PlotterType{T}) where {T} = String(T)
163153
plottername(::Type{Nothing}) = "nothing"
164154
plottername(p::Union{Module, Nothing}) = plottertype(p) |> plottername
165155

0 commit comments

Comments
 (0)