Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
initial sketch of Polytope/Polygon/Polyhedron type
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkelly committed Jan 7, 2016
1 parent bfa8790 commit 23fdfae
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/GeometryTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FixedSizeArrays: eltype_or, ndims_or

import Base: ==,
*,
angle,
call,
contains,
convert,
Expand Down Expand Up @@ -46,6 +47,8 @@ include("distancefields.jl")
include("setops.jl")
include("display.jl")
include("slice.jl")
include("polytopes.jl")
include("angles.jl")
include("decompose.jl")
include("deprecated.jl")
include("checkbounds.jl")
Expand Down Expand Up @@ -92,6 +95,9 @@ export AABB,
Particle,
PlainMesh,
Point,
Polygon,
Polytope,
Polyhedron,
Pyramid,
Quad,
Rectangle,
Expand Down
25 changes: 25 additions & 0 deletions src/angles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Compute the angle between two vectors
"""
function angle(a,b)
acos(dot(a,b)/sqrt(dot(a,a))*sqrt(dot(b,b)))
end

"""
Compute the angle at element `i` of the `Simplex`.
"""
function angle(s::Simplex{3}, i)
if i == 1
v1 = s[2] - s[i]
v2 = s[3] - s[i]
elseif i == 2
v1 = s[3] - s[i]
v2 = s[1] - s[i]
elseif i == 3
v1 = s[2] - s[i]
v2 = s[1] - s[i]
end
a = angle(v1,v2)
a < pi/2 ? a : pi - a
end

12 changes: 12 additions & 0 deletions src/decompose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,15 @@ function decompose{T <: Color}(::Type{T}, mesh::AbstractMesh)
c == nothing && return DefaultColor
convert(T, c)
end

function decompose{T1,T2}(::Type{Simplex{2,T1}}, p::Polyhedron{Simplex{3,T2}})
edges = Array(Simplex{2,T1}, length(p.elements)*3)
@inbounds for i = eachindex(p.elements)
elt = p.elements[i]
se = decompose(Simplex{2,T1}, elt)
edges[3*(i-1)+1] = se[1]
edges[3*(i-1)+2] = se[2]
edges[3*(i-1)+3] = se[3]
end
edges
end
Empty file added src/normals.jl
Empty file.
11 changes: 11 additions & 0 deletions src/polytopes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Base.call{N,T}(::Type{Polytope{N,T}}, elts...)
Polytope{N,T}(T[elts...])
end

function Base.call{T}(::Type{Polygon}, elts::T...)
Polygon{T}(T[elts...])
end

function Base.call{T}(::Type{Polyhedron}, elts::T...)
Polyhedron{T}(T[elts...])
end
12 changes: 12 additions & 0 deletions src/typealias.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ typealias AABB{T} HyperRectangle{3, T}
call(::Type{AABB}, m...) =
HyperRectangle(m...)

"""
A `Polygon` is a `Polytope` realizable with only two dimensions.
Generally this will be composed of `Points` or `LineSegment`s.
"""
typealias Polygon{T} Polytope{2,T}

"""
A `Polyhedron` is a `Polytope` realizable with only three dimensions.
Generally this will be composed of `Face`s or two-simplices (`Simplex{3}`).
"""
typealias Polyhedron{T} Polytope{2,T}

typealias HMesh HomogenousMesh

typealias UV{T} TextureCoordinate{2, T}
Expand Down
23 changes: 22 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ abstract AbstractSignedDistanceField <: AbstractDistanceField
Abstract to categorize geometry primitives of dimensionality `N`.
"""
abstract GeometryPrimitive{N, T}
"""
An `AbstractMesh` helps classify [Polygonal Meshes](https://en.wikipedia.org/wiki/Polygon_mesh)
These are distinct from polytopes, since these try to be as congruent
as possible with data strucutres and file formats for 3D visualisation.
"""
abstract AbstractMesh{VertT, FaceT} <: GeometryPrimitive

"""
Abstract to classify Simplices. The convention for N starts at 1, which means
`AbstractSimplex` helps classify Simplices on dimension and vertex type.
The convention for N starts at 1, which means
a Simplex has 1 point. A 2-simplex has 2 points, and so forth. This convention
is not the same as most mathematical texts.
"""
abstract AbstractSimplex{N,T} <: FixedVector{N,T}

"""
An `AbstractPolytope` categorizes geometric objects with flat sides.
For example, a `Polygon` is a polytope realizable in 2 dimensions, so `N` = 2.
"""
abstract AbstractPolytope{N,T} <: GeometryPrimitive{N,T}

"""
A `Simplex` is a generalization of an N-dimensional tetrahedra and can be thought
Expand Down Expand Up @@ -149,3 +160,13 @@ immutable HomogenousMesh{VertT, FaceT, NormalT, TexCoordT, ColorT, AttribT, Attr
attributes ::AttribT
attribute_id ::Vector{AttribIDT}
end


"""
A `Polytope` is an `N` dimensional object with elements `T` of the same type.
For example typealias `Polygon` and `Polyhedron` exist for dimensions 2 and
3 respectively.
"""
type Polytope{N,T} <: AbstractPolytope{N,T}
elements::Vector{T}
end
7 changes: 7 additions & 0 deletions test/angles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
context("angle") do


context("Simplex") do

end
end
5 changes: 5 additions & 0 deletions test/polytopes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
context("Polytopes") do
p1 = Polygon(Point(0,0), Point(1,0), Point(1,1), Point(0,1))
@fact typeof(p1) --> Polytope{2,Point{2,Int}}
@fact length(p1.elements) --> 4
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ facts("GeometryTypes") do
include("meshes.jl")
include("distancefields.jl")
include("primitives.jl")
include("polytopes.jl")
include("decompose.jl")
include("simplerectangle.jl")
include("hypersphere.jl")
Expand Down

0 comments on commit 23fdfae

Please sign in to comment.