Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ save
default_plotter
default_plotter!
plottertype
PlotterType
PythonPlotType
PyPlotType
MakieType
AbstractPythonPlotterType
AbstractMakieType
PlotsType
PlutoVistaType
VTKViewType
Expand Down
34 changes: 17 additions & 17 deletions ext/GridVisualizeMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -84,18 +84,18 @@ 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)
end
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

Expand All @@ -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], '.')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/GridVisualize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
90 changes: 40 additions & 50 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading