Skip to content

Commit

Permalink
Move planar_contour out of extension (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Jan 17, 2025
1 parent 3f8c920 commit 80cfe13
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
44 changes: 4 additions & 40 deletions ext/PolyhedraRecipesBaseExt.jl
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
module PolyhedraRecipesBaseExt

using LinearAlgebra
using RecipesBase
using Polyhedra
using Polyhedra: basis, _semi_hull
import RecipesBase
import Polyhedra

function planar_contour(p::Polyhedron)
if fulldim(p) != 2
if fulldim(p) == 3
error("Plotting 3-dimensional polyhedron with Plots is not supported, use Makie or MeshCat.")
else
error("Plotting $(fulldim(p))-dimensional polyhedron with Plots is not supported.")
end
end
removevredundancy!(p)
if hasallrays(p)
error("Rays not supported yet in the 2D plotting recipe.")
end
ps = collect(points(p))
if isempty(ps)
error("Plotting empty polyhedron is not supported.")
end
sort!(ps, by = x -> x[1])
counterclockwise = (p1, p2) -> dot(cross([p1; 0], [p2; 0]), [0, 0, 1])
sweep_norm = basis(eltype(ps), fulldim(p), 1)
top = _semi_hull(ps, 1, counterclockwise, sweep_norm)
bot = _semi_hull(ps, -1, counterclockwise, sweep_norm)
if !isempty(top) && !isempty(bot)
@assert top[end] == bot[1]
pop!(top)
end
if !isempty(bot) && !isempty(top)
@assert bot[end] == top[1]
pop!(bot)
end
hull = [top; bot]
push!(hull, hull[1]) # ensure shape is closed
return [p[1] for p in hull], [p[2] for p in hull]
end

@recipe function f(p::Polyhedron)
RecipesBase.@recipe function f(p::Polyhedra.Polyhedron)
seriestype --> :shape
legend --> false
planar_contour(p)
Polyhedra.planar_contour(p)
end

end
34 changes: 34 additions & 0 deletions src/planar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,37 @@ function planar_hull(vr::VRepresentation{T}; tol = _default_tol(T)) where {T}
d = FullDim(vr)
vrep(_planar_hull(FullDim(vr), collect(points(vr)), lines(vr), rays(vr), counterclockwise, rotate; tol)...; d = d)
end

function planar_contour(p::Polyhedron)
if fulldim(p) != 2
if fulldim(p) == 3
error("Plotting 3-dimensional polyhedron with Plots is not supported, use Makie or MeshCat.")
else
error("Plotting $(fulldim(p))-dimensional polyhedron with Plots is not supported.")
end
end
removevredundancy!(p)
if hasallrays(p)
error("Rays not supported yet in the 2D plotting recipe.")
end
ps = collect(points(p))
if isempty(ps)
error("Plotting empty polyhedron is not supported.")
end
sort!(ps, by = x -> x[1])
counterclockwise = (p1, p2) -> dot(cross([p1; 0], [p2; 0]), [0, 0, 1])
sweep_norm = basis(eltype(ps), fulldim(p), 1)
top = _semi_hull(ps, 1, counterclockwise, sweep_norm)
bot = _semi_hull(ps, -1, counterclockwise, sweep_norm)
if !isempty(top) && !isempty(bot)
@assert top[end] == bot[1]
pop!(top)
end
if !isempty(bot) && !isempty(top)
@assert bot[end] == top[1]
pop!(bot)
end
hull = [top; bot]
push!(hull, hull[1]) # ensure shape is closed
return [p[1] for p in hull], [p[2] for p in hull]
end

0 comments on commit 80cfe13

Please sign in to comment.