Polyhedra contains utilities to visualize either a 2-dimensional or a 3-dimensional polyhedron, see Polyhedron for how to construct a polyhedron, e.g. from its H- or V-representation.
A 2-dimensional polytope, i.e. bounded polyhedron, can be visualized with Plots. Suppose for instance that we want to visualize the polyhedron having the following H-representation:
julia> using Polyhedra
julia> h = HalfSpace([1, 1], 1) ∩ HalfSpace([-1, 0], 0) ∩ HalfSpace([0, -1], 0)
H-representation Polyhedra.Intersection{Int64,Array{Int64,1},Int64}:
3-element iterator of HalfSpace{Int64,Array{Int64,1}}:
HalfSpace([1, 1], 1)
HalfSpace([-1, 0], 0)
HalfSpace([0, -1], 0)
The H-representation cannot be given to Plots directly, it first need to be transformed into a polyhedron:
julia> p = polyhedron(h)
Polyhedron DefaultPolyhedron{Rational{BigInt},Polyhedra.Intersection{Rational{BigInt},Array{Rational{BigInt},1},Int64},Polyhedra.Hull{Rational{BigInt},Array{Rational{BigInt},1},Int64}}:
3-element iterator of HalfSpace{Rational{BigInt},Array{Rational{BigInt},1}}:
HalfSpace(Rational{BigInt}[1//1, 1//1], 1//1)
HalfSpace(Rational{BigInt}[-1//1, 0//1], 0//1)
HalfSpace(Rational{BigInt}[0//1, -1//1], 0//1)
The polyhedron can be given to Plots as follows
julia> using Plots
julia> plot(p)
See Polyhedral Function and 3D Plotting a projection of the 4D permutahedron for example notebooks.
A 3-dimensional polyhedron can be visualized with either MeshCat or Makie. Unbounded polyhedron are supported by truncating the polyhedron into a polytope and not triangularizing the faces in the directions of unbounded rays.
Suppose for instance that we want to visualize the polyhedron having the following H-representation:
julia> using Polyhedra
julia> v = convexhull([0, 0, 0]) + conichull([1, 0, 0], [0, 1, 0], [0, 0, 1])
V-representation Polyhedra.Hull{Int64,Array{Int64,1},Int64}:
1-element iterator of Array{Int64,1}:
[0, 0, 0],
3-element iterator of Ray{Int64,Array{Int64,1}}:
Ray([1, 0, 0])
Ray([0, 1, 0])
Ray([0, 0, 1])
The V-representation cannot be given to MeshCat or Makie directly, it first need to be transformed into a polyhedron:
julia> p = polyhedron(v)
Polyhedron DefaultPolyhedron{Rational{BigInt},Polyhedra.Intersection{Rational{BigInt},Array{Rational{BigInt},1},Int64},Polyhedra.Hull{Rational{BigInt},Array{Rational{BigInt},1},Int64}}:
1-element iterator of Array{Rational{BigInt},1}:
Rational{BigInt}[0//1, 0//1, 0//1],
3-element iterator of Ray{Rational{BigInt},Array{Rational{BigInt},1}}:
Ray(Rational{BigInt}[1//1, 0//1, 0//1])
Ray(Rational{BigInt}[0//1, 1//1, 0//1])
Ray(Rational{BigInt}[0//1, 0//1, 1//1])
Then, we need to create a mess from the polyhedron:
julia> m = Polyhedra.Mesh(p)
Polyhedra.Mesh{3,Rational{BigInt},DefaultPolyhedron{Rational{BigInt},Polyhedra.Intersection{Rational{BigInt},Array{Rational{BigInt},1},Int64},Polyhedra.Hull{Rational{BigInt},Array{Rational{BigInt},1},Int64}}}(convexhull([0//1, 0//1, 0//1]) + convexhull(Ray(Rational{BigInt}[1//1, 0//1, 0//1]), Ray(Rational{BigInt}[0//1, 1//1, 0//1]), Ray(Rational{BigInt}[0//1, 0//1, 1//1])))
Polyhedra.Mesh
The polyhedron can be plotted with MeshCat as follows
julia> using MeshCat
julia> vis = Visualizer()
julia> setobject!(vis, m)
julia> open(vis)
To plot it in a notebook, replace open(vis)
with IJuliaCell(vis)
.
To plot it with Makie instead, you can use for instance mesh
or wireframe
.
julia> import Makie
julia> Makie.mesh(m, color=:blue)
julia> Makie.wireframe(m)
See 3D Plotting a projection of the 4D permutahedron for an example notebook.