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

Commit 928ec96

Browse files
authored
Merge pull request #196 from JuliaGeometry/sd/fixes
a couple of fixes for decompose and friends
2 parents 8d6da2e + f96bfa9 commit 928ec96

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

src/decompose.jl

+37-25
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,26 @@ end
137137
# less strict version of above
138138
decompose(::Type{Simplex{1}}, f::Simplex{N, T}) where {N, T} = decompose(Simplex{1,T}, f)
139139

140+
141+
decompose(::Type{P}, points::AbstractVector{P}) where {P<: Point} = points
142+
function decompose(::Type{Point{N1, T1}}, points::AbstractVector{Point{N2, T2}}) where {N1, T1, N2, T2}
143+
return map(x-> to_pointn(Point{N1, T1}, x), points)
144+
end
145+
140146
"""
141147
Get decompose a `HyperRectangle` into points.
142148
"""
143149
function decompose(
144-
PT::Type{Point{N, T1}}, rect::HyperRectangle{N, T2}
145-
) where {N, T1, T2}
150+
PT::Type{Point{N1, T1}}, rect::HyperRectangle{N2, T2}
151+
) where {N1, N2, T1, T2}
146152
# The general strategy is that since there are a deterministic number of
147153
# points, we can generate all points by looking at the binary increments.
148154
w = widths(rect)
149155
o = origin(rect)
150-
points = T1[o[j]+((i>>(j-1))&1)*w[j] for j=1:N, i=0:(2^N-1)]
151-
reshape(reinterpret(PT, points), (2^N,))
156+
points = T1[o[j]+((i>>(j-1))&1)*w[j] for j=1:N2, i=0:(2^N2-1)]
157+
return decompose(PT, reshape(reinterpret(Point{N2, T1}, points), (2^N2,)))
152158
end
159+
153160
"""
154161
Get decompose a `HyperRectangle` into Texture Coordinates.
155162
"""
@@ -163,23 +170,6 @@ function decompose(
163170
points = T1[((i>>(j-1))&1) for j=1:N, i=0:(2^N-1)]
164171
reshape(reinterpret(UVWT, points), (8,))
165172
end
166-
decompose(::Type{FT}, faces::Vector{FT}) where {FT<:Face} = faces
167-
function decompose(::Type{FT1}, faces::Vector{FT2}) where {FT1<:Face, FT2<:Face}
168-
isempty(faces) && return FT1[]
169-
N1,N2 = length(FT1), length(FT2)
170-
171-
n = length(decompose(FT1, first(faces)))
172-
outfaces = Vector{FT1}(undef, length(faces)*n)
173-
i = 1
174-
for face in faces
175-
for outface in decompose(FT1, face)
176-
outfaces[i] = outface
177-
i += 1
178-
end
179-
end
180-
outfaces
181-
end
182-
183173

184174
"""
185175
Get decompose a `HyperRectangle` into faces.
@@ -198,6 +188,30 @@ function decompose(
198188
decompose(FT, faces)
199189
end
200190

191+
function decompose(
192+
FT::Type{Face{N, T}}, rect::HyperRectangle{2, T2}
193+
) where {N, T, T2}
194+
return decompose(FT, SimpleRectangle(minimum(rect), widths(rect)))
195+
end
196+
197+
decompose(::Type{FT}, faces::Vector{FT}) where {FT<:Face} = faces
198+
199+
function decompose(::Type{FT1}, faces::Vector{FT2}) where {FT1<:Face, FT2<:Face}
200+
isempty(faces) && return FT1[]
201+
N1,N2 = length(FT1), length(FT2)
202+
203+
n = length(decompose(FT1, first(faces)))
204+
outfaces = Vector{FT1}(undef, length(faces)*n)
205+
i = 1
206+
for face in faces
207+
for outface in decompose(FT1, face)
208+
outfaces[i] = outface
209+
i += 1
210+
end
211+
end
212+
outfaces
213+
end
214+
201215
function decompose(P::Type{Point{2, PT}}, r::SimpleRectangle, resolution=(2,2)) where PT
202216
w, h = resolution
203217
vec(P[(x,y) for x=range(r.x, stop=r.x+r.w, length=w), y=range(r.y, stop=r.y+r.h, length=h)])
@@ -283,10 +297,8 @@ end
283297
# Define decompose for your own meshtype, to easily convert it to Homogenous attributes
284298

285299
#Gets the normal attribute to a mesh
286-
function decompose(T::Type{Point{3, VT}}, mesh::AbstractMesh) where VT
287-
vts = mesh.vertices
288-
eltype(vts) == T && return vts
289-
eltype(vts) <: Point && return map(T, vts)
300+
function decompose(T::Type{Point{N, VT}}, mesh::AbstractMesh) where {N, VT}
301+
return decompose(T, mesh.vertices)
290302
end
291303

292304
# gets the wanted face type

src/primitives.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ function (meshtype::Type{T})(c::GeometryPrimitive, args...) where T <: AbstractM
1818
newattribs[fieldname] = decompose(eltype(typ), c, args...)
1919
end
2020
end
21-
T(newattribs)
21+
return T(newattribs)
2222
end
2323

2424
function to_pointn(::Type{Point{N, T}}, p::StaticVector{N2}, d = T(0)) where {T, N, N2}
25-
Point(ntuple(i-> i <= N2 ? p[i] : d, Val{N}))
25+
return Point{N, T}(ntuple(i-> i <= N2 ? T(p[i]) : T(d), N))
2626
end
2727

2828
function (::Type{T})(c::Circle, n = 32) where T <: AbstractMesh
2929
newattribs = Dict{Symbol, Any}()
3030
VT = vertextype(T)
31-
verts = decompose(VT, c)
31+
verts = decompose(VT, c, n)
3232
N = length(verts)
3333
push!(verts, to_pointn(VT, origin(c))) # middle point
3434
middle_idx = length(verts)
3535
FT = facetype(T)
3636
faces = map(1:N) do i
3737
FT(i, middle_idx, i + 1)
3838
end
39-
T(vertices = verts, faces = faces)
39+
return T(vertices = verts, faces = faces)
4040
end
4141

4242
function (meshtype::Type{T})(
4343
c::Union{HyperCube{3,T}, HyperRectangle{3,HT}}
44-
) where {T <: HMesh,HT}
44+
) where {T <: HMesh, HT}
4545
xdir = Vec{3, HT}(1, 0, 0)
4646
ydir = Vec{3, HT}(0, 1, 0)
4747
zdir = Vec{3, HT}(0, 0, 1)
@@ -61,5 +61,5 @@ function (meshtype::Type{T})(
6161
map!(v, v) do v
6262
(v .* w) + o
6363
end
64-
mesh
64+
return mesh
6565
end

test/decompose.jl

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ end
1010
a = HyperRectangle(Vec(0,0),Vec(1,1))
1111
pt_expa = Point{2,Int}[(0,0), (1,0), (0,1), (1,1)]
1212
@test decompose(Point{2,Int},a) == pt_expa
13+
mesh = GLNormalMesh(a)
14+
@test decompose(Point2f0, mesh) == pt_expa
15+
1316
b = HyperRectangle(Vec(1,1,1),Vec(1,1,1))
1417
pt_expb = Point{3,Int}[(1,1,1),(2,1,1),(1,2,1),(2,2,1),(1,1,2),(2,1,2),(1,2,2),(2,2,2)]
1518
@test decompose(Point{3,Int}, b) == pt_expb
19+
mesh = NormalMesh{Int, GLTriangle, Int}(b)
20+
# TODO order of NormalMesh & decompose doesn't agree -.-
1621
end
1722

1823
@testset "Faces" begin
@@ -135,7 +140,6 @@ end
135140

136141
end
137142

138-
139143
@testset "HyperSphere" begin
140144
sphere = Sphere{Float32}(Point3f0(0), 1f0)
141145

@@ -154,12 +158,12 @@ end
154158
[4, 5, 8], [4, 8, 7], [5, 6, 9], [5, 9, 8]
155159
]
156160
@test f == face_target
157-
158-
points = decompose(Point2f0, Circle(Point2f0(0), 0f0), 20)
161+
circle = Circle(Point2f0(0), 1f0)
162+
points = decompose(Point2f0, circle, 20)
159163
@test length(points) == 20
160-
164+
mesh = GLNormalMesh(circle, 32)
165+
# end-1, since we add a middle point for the mesh!
166+
@test decompose(Point2f0, mesh)[1:end-1] decompose(Point2f0, circle, 32)
161167
end
162168

163-
164-
165169
end

0 commit comments

Comments
 (0)