Skip to content

Commit

Permalink
better error messages for plot3d/plot3d! when missing packages
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Dec 12, 2019
1 parent c36e587 commit e46fc1a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
7 changes: 7 additions & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ RecipesBase.apply_recipe(::Dict{Symbol,Any}, ::LazySet{N}, ::N=N(1e-3)) where {N
RecipesBase.apply_recipe(::Dict{Symbol,Any}, ::AbstractVector{VN}, ::N=N(1e-3), ::Int=40, ::Bool=false) where {N<:Real, VN<:LazySet{N}}
```

For three-dimensional sets, we support `Makie`:

```@docs
plot3d
plot3d!
```

### Set functions that override Base functions

```@docs
Expand Down
2 changes: 1 addition & 1 deletion src/Initialization/init_Makie.jl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
eval(initialize_mesh())
eval(load_makie())
3 changes: 1 addition & 2 deletions src/Initialization/init_Polyhedra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ end)
eval(load_polyhedra_hpolytope())
eval(load_polyhedra_hpolyhedron())
eval(load_polyhedra_vpolytope())

eval(initialize_mesh())
eval(load_polyhedra_mesh())
53 changes: 36 additions & 17 deletions src/Plotting/mesh.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
function load_mesh()
export plot3d, plot3d!


function load_polyhedra_mesh()
return quote

using .Polyhedra: Mesh

end end # quote / function load_polyhedra_mesh()


function load_makie()
return quote

using .Makie: mesh, mesh!
import .Makie.AbstractPlotting: Automatic
using .Makie.AbstractPlotting: Automatic

end end # quote / function load_makie()

export plot3d, plot3d!

# helper function for 3D plotting; converts S to a polytope in H-representation
function plot3d_helper(S::LazySet{N}, backend) where {N}
function _plot3d_helper(S::LazySet{N}, backend) where {N}
@assert dim(S) <= 3 "plot3d can only be used to plot sets of dimension three (or lower); " *
"but the given set is $(dim(S))-dimensional"

Expand All @@ -24,11 +35,11 @@ end

"""
plot3d(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing,
interpolate=false, linewidth=1, overdraw=false, shading=true,
transparency=true, visible=true) where {N}
Plot a three-dimensional convex set using Makie.
Plot a three-dimensional convex set using `Makie`.
### Input
Expand All @@ -43,9 +54,10 @@ Plot a three-dimensional convex set using Makie.
- `colormap` -- (optional, default: `:viridis`) the color map of the main plot;
call `available_gradients()` to see what gradients are available,
and it can also be used as `[:red, :black]`
- `colorrange` -- (optional, default: `Automatic()`) a tuple `(min, max)` where
`min` and `max` specify the data range to be used for indexing
the colormap
- `colorrange` -- (optional, default: `nothing`, which falls back to
`Makie.AbstractPlotting.Automatic()`) a tuple `(min, max)`
where `min` and `max` specify the data range to be used for
indexing the colormap
- `interpolate` -- (optional, default: `false`) a bool for heatmap and images,
it toggles color interpolation between nearby pixels
- `linewidth` -- (optional, default: `1`) a number that specifies the width of
Expand Down Expand Up @@ -103,17 +115,22 @@ julia> plot3d!(10. * rand(Hyperrectangle, dim=3), color=:red)
```
"""
function plot3d(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(), interpolate=false,
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing, interpolate=false,
linewidth=1, overdraw=false, shading=true, transparency=true, visible=true) where {N}
require(:Makie; fun_name="plot3d")
require(:Polyhedra; fun_name="plot3d")

P_poly_mesh = plot3d_helper(S, backend)
if colorrange == nothing
colorrange = Automatic()
end
P_poly_mesh = _plot3d_helper(S, backend)
return mesh(P_poly_mesh, alpha=alpha, color=color, colormap=colormap, colorrange=colorrange,
interpolate=interpolate, linewidth=linewidth, transparency=transparency, visible=visible)
end

"""
plot3d!(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(), interpolate=false,
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing, interpolate=false,
linewidth=1, overdraw=false, shading=true, transparency=true, visible=true) where {N}
Plot a three-dimensional convex set using Makie.
Expand All @@ -129,13 +146,15 @@ documentation](http://makie.juliaplots.org/stable/plot-attributes).
See the documentation of `plot3d` for examples.
"""
function plot3d!(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(), interpolate=false,
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing, interpolate=false,
linewidth=1, overdraw=false, shading=true, transparency=true, visible=true) where {N}
require(:Makie; fun_name="plot3d!")
require(:Polyhedra; fun_name="plot3d!")

P_poly_mesh = plot3d_helper(S, backend)
if colorrange == nothing
colorrange = Automatic()
end
P_poly_mesh = _plot3d_helper(S, backend)
return mesh!(P_poly_mesh, alpha=alpha, color=color, colormap=colormap, colorrange=colorrange,
interpolate=interpolate, linewidth=linewidth, transparency=transparency, visible=visible)
end

end # quote
end # function load_mesh()
6 changes: 0 additions & 6 deletions src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ function __init__()
@require Polyhedra = "67491407-f73d-577b-9b50-8179a7c68029" include("Initialization/init_Polyhedra.jl")
end

function initialize_mesh()
if isdefined(@__MODULE__, :Polyhedra) && isdefined(@__MODULE__, :Makie)
eval(load_mesh())
end
end

"""
require(package::Symbol; fun_name::String="", explanation::String="")
Expand Down

0 comments on commit e46fc1a

Please sign in to comment.