Skip to content

Commit

Permalink
Switch to GeometryBasics
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 3, 2020
1 parent 8a91640 commit 4052029
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ repo = "https://github.com/JuliaPolyhedra/Polyhedra.jl.git"
version = "0.6.5"

[deps]
GeometryTypes = "4d00f742-c7ba-57c2-abde-4428a4b178cb"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
GeometryTypes = "0.7, 0.8"
GeometryBasics = "0.2"
JuMP = "0.21"
RecipesBase = "0.7, 0.8, 1.0"
StaticArrays = "0.12"
Expand Down
59 changes: 28 additions & 31 deletions src/decompose.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import GeometryTypes
import GeometryBasics

"""
struct Mesh{N, T, PT <: Polyhedron{T}} <: GeometryTypes.GeometryPrimitive{N, T}
struct Mesh{N, T, PT <: Polyhedron{T}} <: GeometryBasics.GeometryPrimitive{N, T}
polyhedron::PT
coordinates::Vector{GeometryBasics.Point{3, T}}
faces::Vector{GeometryBasics.TriangleFace{Int}}
normals::Vector{GeometryBasics.Point{3, T}}
end
Mesh wrapper type that inherits from `GeometryPrimitive` to be used for plotting
a polyhedron. Note that `Mesh(p)` is type unstable but one can use `Mesh{3}(p)`
instead if it is known that `p` is defined in a 3-dimensional space.
"""
struct Mesh{N, T, PT <: Polyhedron{T}} <: GeometryTypes.GeometryPrimitive{N, T}
mutable struct Mesh{N, T, PT <: Polyhedron{T}} <: GeometryBasics.GeometryPrimitive{N, T}
polyhedron::PT
coordinates::Union{Nothing, Vector{GeometryBasics.Point{N, T}}}
faces::Union{Nothing, Vector{GeometryBasics.TriangleFace{Int}}}
normals::Union{Nothing, Vector{GeometryBasics.Point{N, T}}}
end
function Mesh{N}(polyhedron::Polyhedron{T}) where {N, T}
return Mesh{N, T, typeof(polyhedron)}(polyhedron)
return Mesh{N, T, typeof(polyhedron)}(polyhedron, nothing, nothing, nothing)
end
function Mesh(polyhedron::Polyhedron, ::StaticArrays.Size{N}) where N
return Mesh{N[1]}(polyhedron)
Expand All @@ -27,6 +33,13 @@ function Mesh(polyhedron::Polyhedron)
return Mesh(polyhedron, FullDim(polyhedron))
end

function fulldecompose!(mesh::Mesh)
if mesh.coordinates === nothing
mesh.coordinates, mesh.faces, mesh.normals = fulldecompose(mesh)
end
return
end

# Creates a scene for the vizualisation to be used to truncate the lines and rays
function scene(vr::VRep, ::Type{T}) where T
# First compute the smallest rectangle containing the P-representation (i.e. the points).
Expand All @@ -37,10 +50,10 @@ function scene(vr::VRep, ::Type{T}) where T
if width == zero(T)
width = 2
end
scene = GeometryTypes.HyperRectangle{3, T}([(xmin + xmax) / 2 - width,
(ymin + ymax) / 2 - width,
(zmin + zmax) / 2 - width],
2 * width * ones(T, 3))
scene = GeometryBasics.HyperRectangle{3, T}([(xmin + xmax) / 2 - width,
(ymin + ymax) / 2 - width,
(zmin + zmax) / 2 - width],
2 * width * ones(T, 3))
# Intersection of rays with the limits of the scene
(start, ray) -> begin
times = max.((Vector(minimum(scene))-start) ./ ray, (Vector(maximum(scene))-start) ./ ray)
Expand Down Expand Up @@ -197,9 +210,9 @@ function fulldecompose(poly_geom::Mesh{3}, ::Type{T}) where T
end

ntri = length(triangles)
pts = Vector{GeometryTypes.Point{3, T}}(undef, 3ntri)
faces = Vector{GeometryTypes.Face{3, Int}}(undef, ntri)
ns = Vector{GeometryTypes.Normal{3, T}}(undef, 3ntri)
pts = Vector{GeometryBasics.Point{3, T}}(undef, 3ntri)
faces = Vector{GeometryBasics.TriangleFace{Int}}(undef, ntri)
ns = Vector{GeometryBasics.Point{3, T}}(undef, 3ntri)
for i in 1:ntri
tri = pop!(triangles)
normal = tri[2]
Expand Down Expand Up @@ -228,23 +241,7 @@ end

fulldecompose(poly::Mesh{N, T}) where {N, T} = fulldecompose(poly, typeof(one(T)/2))

GeometryTypes.isdecomposable(::Type{<:GeometryTypes.Point{3}}, ::Type{<:Mesh{3}}) = true
GeometryTypes.isdecomposable(::Type{<:GeometryTypes.Face{3}}, ::Type{<:Mesh{3}}) = true
GeometryTypes.isdecomposable(::Type{<:GeometryTypes.Normal{3}}, ::Type{<:Mesh{3}}) = true
function GeometryTypes.decompose(PT::Type{<:GeometryTypes.Point}, poly::Mesh)
points = fulldecompose(poly)[1]
map(PT, points)
end
function GeometryTypes.decompose(FT::Type{<:GeometryTypes.Face}, poly::Mesh)
faces = fulldecompose(poly)[2]
GeometryTypes.decompose(FT, faces)
end
function GeometryTypes.decompose(NT::Type{<:GeometryTypes.Normal}, poly::Mesh)
ns = fulldecompose(poly)[3]
map(NT, ns)
end

# In AbstractPlotting, when asking to plot an object, it calls this constructor
# which is only defined for `GeometryTypes.GeometryPrimitive` which is a
# supertype of `Polyhedra.Mesh`
GeometryTypes.GLNormalMesh(p::Polyhedron) = GeometryTypes.GLNormalMesh(Mesh(p))
GeometryBasics.coordinates(poly::Mesh) = (fulldecompose!(poly); poly.coordinates)
GeometryBasics.faces(poly::Mesh) = (fulldecompose!(poly); poly.faces)
GeometryBasics.texturecoordinates(poly::Mesh) = nothing
GeometryBasics.normals(poly::Mesh) = (fulldecompose!(poly); poly.normals)
14 changes: 7 additions & 7 deletions test/decompose.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import GeometryTypes
using Test
using Polyhedra
import GeometryBasics

struct Face
points::Vector{Vector{Float64}}
Expand All @@ -21,12 +23,10 @@ end
nfaces(d::Dict{<:Any, Face}) = length(d)
nfaces(d::Dict{<:Any, <:Vector}) = sum(map(length, values(d)))
function test_decompose(p::Polyhedra.Mesh, d::Dict)
@test GeometryTypes.isdecomposable(GeometryTypes.Point{3, Float32}, typeof(p))
points = GeometryTypes.decompose(GeometryTypes.Point{3, Float32}, p)
@test GeometryTypes.isdecomposable(GeometryTypes.Face{3, Int}, typeof(p))
faces = GeometryTypes.decompose(GeometryTypes.Face{3, Int}, p)
@test GeometryTypes.isdecomposable(GeometryTypes.Normal{3,Float64}, typeof(p))
normals = GeometryTypes.decompose(GeometryTypes.Normal{3,Float64}, p)
P = GeometryBasics.Point{3, Float64}
points = GeometryBasics.decompose(P, p)
faces = GeometryBasics.decompose(GeometryBasics.GLTriangleFace, p)
normals = GeometryBasics.decompose(GeometryBasics.Normal{P}(), p)
nf = nfaces(d)
@test length(points) == 3nf
@test length(faces) == nf
Expand Down

0 comments on commit 4052029

Please sign in to comment.