Skip to content

Commit e08a101

Browse files
committed
improve meshing interface
1 parent d07662b commit e08a101

File tree

5 files changed

+59
-44
lines changed

5 files changed

+59
-44
lines changed

src/basic_types.jl

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,6 @@ abstract type AbstractNgonFace{N, T} <: AbstractFace{N, T} end
2020

2121
abstract type AbstractSimplex{Dim, N, T} <: StaticVector{Dim, T} end
2222

23-
"""
24-
coordinates(geometry)
25-
Returns the edges/vertices/coordinates of a geometry. Is allowed to return lazy iterators!
26-
Use `decompose(ConcretePointType, geometry)` to get `Vector{ConcretePointType}` with
27-
`ConcretePointType` to be something like `Point{3, Float32}`.
28-
"""
29-
function coordinates(points::AbstractVector{<:AbstractPoint})
30-
return points
31-
end
32-
33-
"""
34-
faces(geometry)
35-
Returns the face connections of a geometry. Is allowed to return lazy iterators!
36-
Use `decompose(ConcreteFaceType, geometry)` to get `Vector{ConcreteFaceType}` with
37-
`ConcreteFaceType` to be something like `TriangleFace{Int}`.
38-
"""
39-
function faces(f::AbstractVector{<:AbstractFace})
40-
return f
41-
end
42-
4323
"""
4424
Face index, connecting points to form a simplex
4525
"""
@@ -57,7 +37,9 @@ const LineFace{T} = NgonFace{2, T}
5737
const TriangleFace{T} = NgonFace{3, T}
5838
const QuadFace{T} = NgonFace{4, T}
5939

60-
Base.show(io::IO, x::TriangleFace{T}) where T = print(io, "TriangleFace(", join(x, ", "), ")")
40+
function Base.show(io::IO, x::TriangleFace{T}) where T
41+
print(io, "TriangleFace(", join(x, ", "), ")")
42+
end
6143

6244
Face(::Type{<: NgonFace{N}}, ::Type{T}) where {N, T} = NgonFace{N, T}
6345
Face(F::Type{NgonFace{N, FT}}, ::Type{T}) where {FT, N, T} = F
@@ -342,7 +324,7 @@ const AbstractMesh{Element} = AbstractVector{Element}
342324
The conrecte AbstractMesh implementation
343325
"""
344326
struct Mesh{
345-
Dim, T <: Real,
327+
Dim, T <: Number,
346328
Element <: Polytope{Dim, T},
347329
V <: AbstractVector{Element}
348330
} <: AbstractMesh{Element}

src/geometry_primitives.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ function collect_with_eltype(::Type{T}, iter) where T
6969
return result
7070
end
7171

72-
7372
function faces(primitive, nvertices=30)
7473
# doesn't have any specific algorithm to generate faces
7574
# so will try to triangulate the coordinates!
@@ -94,6 +93,14 @@ function decompose(::Type{T}, primitive::AbstractVector{T}) where {T<:AbstractPo
9493
return primitive
9594
end
9695

96+
function decompose(::Type{T}, primitive::AbstractVector{T2}) where {T, T2 <: Union{StaticVector, AbstractPoint}}
97+
return convert(Vector{T}, primitive)
98+
end
99+
100+
function decompose(::Type{T}, primitive::AbstractVector, args...) where {T<:AbstractPoint}
101+
return collect_with_eltype(P, coordinates(primitive, args...))
102+
end
103+
97104
function decompose(::Type{P}, primitive, args...) where {P<:AbstractPoint}
98105
return collect_with_eltype(P, coordinates(primitive, args...))
99106
end
@@ -365,6 +372,9 @@ function texturecoordinates(s::Circle, nvertices=64)
365372
return coordinates(Circle(Point2f0(0.5), 0.5f0), nvertices)
366373
end
367374

375+
best_nvertices(x::Circle) = 64
376+
best_nvertices(x::Sphere) = 24
377+
368378
function coordinates(s::Sphere, nvertices=24)
369379
θ = LinRange(0, pi, nvertices); φ = LinRange(0, 2pi, nvertices)
370380
inner(θ, φ) = Point(cos(φ)*sin(θ), sin(φ)*sin(θ), cos(θ)) .* s.r .+ s.center

src/interfaces.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
coordinates(geometry)
3+
Returns the edges/vertices/coordinates of a geometry. Is allowed to return lazy iterators!
4+
Use `decompose(ConcretePointType, geometry)` to get `Vector{ConcretePointType}` with
5+
`ConcretePointType` to be something like `Point{3, Float32}`.
6+
"""
7+
function coordinates(points::AbstractVector{<:AbstractPoint})
8+
return points
9+
end
10+
11+
"""
12+
faces(geometry)
13+
Returns the face connections of a geometry. Is allowed to return lazy iterators!
14+
Use `decompose(ConcreteFaceType, geometry)` to get `Vector{ConcreteFaceType}` with
15+
`ConcreteFaceType` to be something like `TriangleFace{Int}`.
16+
"""
17+
function faces(f::AbstractVector{<:AbstractFace})
18+
return f
19+
end
20+
21+
coordinates(x, ::Nothing) = coordinates(x)
22+
faces(x, ::Nothing) = faces(x)
23+
decompose(x, ::Nothing) = decompose(x)
24+
decompose(t::Type{<:Point}, x, ::Nothing) = decompose(t, x)
25+
decompose(t::Type{<:NgonFace}, x, ::Nothing) = decompose(t, x)
26+
decompose(t::Vec, x, ::Nothing) = decompose(t, x)
27+
decompose(t::UV, x, ::Nothing) = decompose(t, x)
28+
decompose(t::UVW, x, ::Nothing) = decompose(t, x)
29+
decompose(t::Normal, x, ::Nothing) = decompose(t, x)

src/meshes.jl

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const FaceMesh{Dim, T, Element} = Mesh{Dim, T, Element, <: FaceView{Element}}
22

33
coordinates(mesh::FaceMesh) = coordinates(getfield(mesh, :simplices))
44
faces(mesh::FaceMesh) = faces(getfield(mesh, :simplices))
5+
best_nvertices(mesh::Mesh) = nothing
56

67
const GLTriangleElement = Triangle{3, Float32}
78
const GLTriangleFace = TriangleFace{GLIndex}
@@ -73,9 +74,11 @@ const GLNormalUVWMesh{Dim} = NormalUVWMesh{Dim, Float32}
7374
const GLNormalUVWMesh2D = GLNormalUVWMesh{2}
7475
const GLNormalUVWMesh3D = GLNormalUVWMesh{3}
7576

76-
best_nvertices(any_primitive) = 24
77-
best_pointtype(::GeometryPrimitive{N, T}) where {N, T} = Point{N, T}
77+
# Types that can be converted to a mesh via the functions below
78+
const MeshLike{N, T} = Union{Mesh{N, T}, GeometryPrimitive{N, T}, AbstractVector{<: AbstractPoint{N, T}}}
7879

80+
best_nvertices(any_primitive) = nothing
81+
best_pointtype(::GeometryPrimitive{N, T}) where {N, T} = Point{N, T}
7982

8083
"""
8184
mesh(primitive::GeometryPrimitive;
@@ -90,7 +93,7 @@ Note, that this can be an `Int` or `Tuple{Int, Int}``, when the primitive is gri
9093
It also only losely correlates to the number of vertices, depending on the algorithm used.
9194
#TODO: find a better number here!
9295
"""
93-
function mesh(primitive::GeometryPrimitive;
96+
function mesh(primitive::MeshLike;
9497
pointtype=best_pointtype(primitive), facetype=GLTriangleFace,
9598
uv=nothing, normaltype=nothing, nvertices=nothing)
9699

@@ -149,40 +152,33 @@ function mesh(polygon::AbstractVector{P}; pointtype=P, facetype=GLTriangleFace,
149152
return Mesh(positions, faces)
150153
end
151154

152-
mesh(m::Mesh) = m
153-
triangle_mesh(m::Mesh) = m
154-
155-
function triangle_mesh(primitive::GeometryPrimitive{N}; nvertices=nothing) where {N}
155+
function triangle_mesh(primitive::MeshLike{N}; nvertices=nothing) where {N}
156156
return mesh(primitive; pointtype=Point{N, Float32}, facetype=GLTriangleFace, nvertices=nvertices)
157157
end
158158

159159

160-
function triangle_mesh(polygon::AbstractVector{<: AbstractPoint{2}})
161-
return mesh(polygon; pointtype=Point{2, Float32}, facetype=GLTriangleFace)
162-
end
163-
164-
function uv_mesh(primitive::GeometryPrimitive{N, T}; nvertices=nothing) where {N, T}
160+
function uv_mesh(primitive::MeshLike{N, T}; nvertices=nothing) where {N, T}
165161
return mesh(primitive; pointtype=Point{N, Float32}, uv=Vec2f0,
166162
facetype=GLTriangleFace, nvertices=nvertices)
167163
end
168164

169-
function uv_normal_mesh(primitive::GeometryPrimitive{N}; nvertices=nothing) where {N}
165+
function uv_normal_mesh(primitive::MeshLike{N}; nvertices=nothing) where {N}
170166
return mesh(primitive; pointtype=Point{N, Float32}, uv=Vec2f0, normaltype=Vec3f0,
171167
facetype=GLTriangleFace, nvertices=nvertices)
172168
end
173169

174-
function normal_mesh(primitive::GeometryPrimitive{N}; nvertices=nothing) where {N}
175-
return mesh(primitive; pointtype=Point{N, Float32}, normaltype=Vec3f0,
176-
facetype=GLTriangleFace, nvertices=nvertices)
177-
end
178-
179170
function normal_mesh(points::AbstractVector{<:AbstractPoint},
180171
faces::AbstractVector{<:AbstractFace})
181172
_points = convert(Vector{Point3f0}, points)
182173
_faces = decompose(GLTriangleFace, faces)
183174
return Mesh(meta(_points; normals=normals(_points, _faces)), _faces)
184175
end
185176

177+
function normal_mesh(primitive::MeshLike{N}; nvertices=nothing) where {N}
178+
return mesh(primitive; pointtype=Point{N, Float32}, normaltype=Vec3f0,
179+
facetype=GLTriangleFace, nvertices=nvertices)
180+
end
181+
186182
"""
187183
volume(triangle)
188184

src/viewtypes.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,9 @@ Tables.schema(faceview::FaceView) = Tables.schema(getfield(faceview, :elements))
138138

139139
Base.size(faceview::FaceView) = size(getfield(faceview, :faces))
140140

141-
Base.show(io::IO, ::Type{<: FaceView{Element}}) where Element = print(io, "FaceView{", Element, "}")
142-
143141
function Base.show(io::IO, ::Type{<: FaceView{Element}}) where Element
144142
if @isdefined Element
145-
print(io, "FaceView{", T, "}")
143+
print(io, "FaceView{", Element, "}")
146144
else
147145
print(io, "FaceView{Element}")
148146
end

0 commit comments

Comments
 (0)