From 405202968dc0a3693d0f5807ce3da7014d1837ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 3 Jun 2020 14:03:42 +0200 Subject: [PATCH] Switch to GeometryBasics --- Project.toml | 4 ++-- src/decompose.jl | 59 ++++++++++++++++++++++------------------------- test/decompose.jl | 14 +++++------ 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/Project.toml b/Project.toml index f62a9908..e10cbddb 100644 --- a/Project.toml +++ b/Project.toml @@ -4,7 +4,7 @@ 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" @@ -12,7 +12,7 @@ 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" diff --git a/src/decompose.jl b/src/decompose.jl index 451ea180..9ed7c13d 100644 --- a/src/decompose.jl +++ b/src/decompose.jl @@ -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) @@ -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). @@ -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) @@ -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] @@ -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) diff --git a/test/decompose.jl b/test/decompose.jl index 9ba2600f..9f6f0da7 100644 --- a/test/decompose.jl +++ b/test/decompose.jl @@ -1,4 +1,6 @@ -import GeometryTypes +using Test +using Polyhedra +import GeometryBasics struct Face points::Vector{Vector{Float64}} @@ -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