Skip to content

Commit 87f250e

Browse files
committed
better typealias
1 parent 76f6aad commit 87f250e

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

src/GeometryBasics.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ include("basic_types.jl")
88
include("metadata.jl")
99
include("viewtypes.jl")
1010

11-
12-
1311
end # module

src/basic_types.jl

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
"""
2+
Abstract Geometry in R{Dim} with Number type T
3+
"""
4+
abstract type AbstractGeometry{Dim, T <: Real} end
5+
16
"""
27
Geometry made of N connected points. Connected as one flat geometry, it makes a Ngon / Polygon.
38
Connected as volume it will be a Simplex / Tri / Cube.
49
Note That `Polytype{N} where N == 3` denotes a Triangle both as a Simplex or Ngon.
510
"""
6-
abstract type Polytope{Dim, T <: Real} end
11+
abstract type Polytope{Dim, T} <: AbstractGeometry{Dim, T} end
712
abstract type AbstractPolygon{Dim, T} <: Polytope{Dim, T} end
813

914
abstract type AbstractPoint{Dim, T} <: StaticVector{Dim, T} end
@@ -38,10 +43,9 @@ const TriangleFace{T} = NgonFace{3, T}
3843
Base.show(io::IO, x::Type{<: TriangleFace{T}}) where T = print(io, "TriangleFace{", T, "}")
3944
Face(::Type{<: NgonFace{N}}, ::Type{T}) where {N, T} = NgonFace{N, T}
4045

41-
4246
@propagate_inbounds Base.getindex(x::Polytope, i::Integer) = coordinates(x)[i]
4347
@propagate_inbounds Base.iterate(x::Polytope) = iterate(coordinates(x))
44-
@propagate_inbounds Base.iterate(x::Polytope, i::Integer) = iterate(coordinates(x), i)
48+
@propagate_inbounds Base.iterate(x::Polytope, i) = iterate(coordinates(x), i)
4549

4650
"""
4751
Fixed Size Polygon, e.g.
@@ -88,7 +92,17 @@ end
8892
# Simplex{D, T, 3} & Ngon{D, T, 3} are both representing a triangle.
8993
# Since Ngon is supposed to be flat and a triangle is flat, lets prefer Ngon
9094
# for triangle:
91-
const Triangle{Dim, T} = Ngon{Dim, T, 3, P} where P <: AbstractPoint{Dim, T}
95+
const TriangleP{Dim, T, P <: AbstractPoint{Dim, T}} = Ngon{Dim, T, 3, P}
96+
97+
const Triangle{Dim, T} = TriangleP{Dim, T, Point{Dim, T}}
98+
const Triangle3d{T} = Triangle{3, T}
99+
100+
function Base.show(io::IO, ::Type{TriangleP{D, T, P}}) where {D, T, P}
101+
print(io, "Tetrahedron{", D, ", ", T, "}")
102+
end
103+
Base.show(io::IO, ::Type{Triangle}) = print(io, "Triangle")
104+
Base.show(io::IO, x::TriangleP) = print(io, "Triangle(", join(x, ", "), ")")
105+
92106
const Quadrilateral{Dim, T} = Ngon{Dim, T, 4, P} where P <: AbstractPoint{Dim, T}
93107

94108
"""
@@ -115,9 +129,19 @@ struct Simplex{
115129

116130
points::SVector{N, Point}
117131
end
132+
118133
const NSimplex{N} = Simplex{Dim, T, N, P} where {Dim, T, P}
119-
const Line{Dim, T, P <: AbstractPoint{Dim, T}} = Simplex{Dim, T, 2, P}
120-
const Tetrahedron{Dim, T, P <: AbstractPoint{Dim, T}} = Simplex{Dim, T, 3, P}
134+
const LineP{Dim, T, P <: AbstractPoint{Dim, T}} = Simplex{Dim, T, 2, P}
135+
const Line{Dim, T} = LineP{Dim, T, Point{Dim, T}}
136+
const TetrahedronP{T, P <: AbstractPoint{3, T}} = Simplex{3, T, 4, P}
137+
const Tetrahedron{T} = TetrahedronP{T, Point{3, T}}
138+
139+
function Base.show(io::IO, ::Type{TetrahedronP{T, P}}) where {T, P}
140+
print(io, "Tetrahedron{", T, "}")
141+
end
142+
Base.show(io::IO, ::Type{Tetrahedron}) = print(io, "Tetrahedron")
143+
Base.show(io::IO, x::TetrahedronP) = print(io, "Tetrahedron(", join(x, ", "), ")")
144+
121145

122146
coordinates(x::Simplex) = x.points
123147
function (::Type{<: NSimplex{N}})(points::Vararg{P, N}) where {P <: AbstractPoint{Dim, T}, N} where {Dim, T}
@@ -142,26 +166,20 @@ function Polytope(::Type{<: NSimplex{N}}, P::Type{<: AbstractPoint{NDim, T}}) wh
142166
Simplex{NDim, T, N, P}
143167
end
144168

145-
function Base.show(io::IO, ::Type{Line{D, T, P}}) where {D, T, P}
169+
function Base.show(io::IO, ::Type{LineP{D, T, P}}) where {D, T, P}
146170
print(io, "Line{", D, ", ", T, "}")
147171
end
148-
149-
function Base.show(io::IO, ::Type{Line})
150-
print(io, "Line")
151-
end
152-
153-
function Base.show(io::IO, x::Line{D, T}) where {D, T}
154-
print(io, "Line: ", x[1], " => ", x[2])
155-
end
172+
Base.show(io::IO, ::Type{Line}) = print(io, "Line")
173+
Base.show(io::IO, x::LineP) = print(io, "Line(", x[1], " => ", x[2], ")")
156174

157175
"""
158176
A LineString is a geometry of connected line segments
159177
"""
160178
struct LineString{
161179
Dim, T <: Real,
162180
P <: AbstractPoint,
163-
V <: AbstractVector{<: Line{Dim, T, P}}
164-
} <: AbstractVector{Line{Dim, T}}
181+
V <: AbstractVector{<: LineP{Dim, T, P}}
182+
} <: AbstractVector{LineP{Dim, T}}
165183
points::V
166184
end
167185
coordinates(x::LineString) = x.points
@@ -170,7 +188,7 @@ Base.size(x::LineString) = size(coordinates(x))
170188
Base.getindex(x::LineString, i) = getindex(coordinates(x), i)
171189

172190

173-
function LineString(points::AbstractVector{Line{Dim, T, P}}) where {Dim, T, P}
191+
function LineString(points::AbstractVector{LineP{Dim, T, P}}) where {Dim, T, P}
174192
LineString{Dim, T, P, typeof(points)}(points)
175193
end
176194

@@ -186,11 +204,11 @@ linestring = LineString(points)
186204
```
187205
"""
188206
function LineString(points::AbstractVector{<: AbstractPoint}, skip = 1)
189-
LineString(connect(points, Line, skip))
207+
LineString(connect(points, LineP, skip))
190208
end
191209

192210
function LineString(points::AbstractVector{<: Pair{P, P}}) where P <: AbstractPoint{N, T} where {N, T}
193-
LineString(reinterpret(Line{N, T, P}, points))
211+
LineString(reinterpret(LineP{N, T, P}, points))
194212
end
195213

196214
function LineString(points::AbstractVector{<: AbstractPoint}, faces::AbstractVector{<: LineFace})
@@ -223,7 +241,8 @@ end
223241

224242
struct Polygon{
225243
Dim, T <: Real,
226-
L <: AbstractVector{<: Line{Dim, T}},
244+
P <: AbstractPoint{Dim, T},
245+
L <: AbstractVector{<: LineP{Dim, T, P}},
227246
V <: AbstractVector{L}
228247
} <: AbstractPolygon{Dim, T}
229248
exterior::L
@@ -232,11 +251,11 @@ end
232251

233252
Base.:(==)(a::Polygon, b::Polygon) = (a.exterior == b.exterior) && (a.interiors == b.interiors)
234253

235-
function Polygon(exterior::AbstractVector{Line{Dim, T, P}}, interiors::V) where {Dim, T, P, V <: AbstractVector{<: AbstractVector{Line{Dim, T, P}}}}
254+
function Polygon(exterior::AbstractVector{LineP{Dim, T, P}}, interiors::V) where {Dim, T, P, V <: AbstractVector{<: AbstractVector{LineP{Dim, T, P}}}}
236255
Polygon{Dim, T, typeof(exterior), V}(exterior, interiors)
237256
end
238257

239-
Polygon(exterior::L) where L <: AbstractVector{<: Line} = Polygon(exterior, L[])
258+
Polygon(exterior::L) where L <: AbstractVector{<: LineP} = Polygon(exterior, L[])
240259

241260
function Polygon(exterior::AbstractVector{P}, skip::Int = 1) where P <: AbstractPoint{Dim, T} where {Dim, T}
242261
Polygon(LineString(exterior, skip))

0 commit comments

Comments
 (0)