Skip to content

Commit 85a8169

Browse files
committed
clean up some code duplication
1 parent 2253812 commit 85a8169

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

src/primitives/Cone.jl

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,11 @@ radius(c::Cone) = c.radius
2121
height(c::Cone) = norm(c.tip - c.origin)
2222
direction(c::Cone) = (c.tip .- c.origin) ./ height(c)
2323

24-
function rotation(c::Cone{T}) where {T}
25-
d3 = direction(c)
26-
u = Vec{3, T}(d3[1], d3[2], d3[3])
27-
if abs(u[1]) > 0 || abs(u[2]) > 0
28-
v = Vec{3, T}(u[2], -u[1], T(0))
29-
else
30-
v = Vec{3, T}(T(0), -u[3], u[2])
31-
end
32-
v = normalize(v)
33-
w = Vec{3, T}(u[2] * v[3] - u[3] * v[2], -u[1] * v[3] + u[3] * v[1],
34-
u[1] * v[2] - u[2] * v[1])
35-
return Mat{3, 3, T}(v..., w..., u...)
36-
end
37-
3824
function coordinates(c::Cone{T}, nvertices=30) where {T}
3925
nvertices += isodd(nvertices)
4026
nhalf = div(nvertices, 2)
4127

42-
R = rotation(c)
28+
R = cylinder_rotation_matrix(direction(c))
4329
step = 2pi / nhalf
4430

4531
ps = Vector{Point3{T}}(undef, nhalf + 2)
@@ -57,7 +43,7 @@ function normals(c::Cone, nvertices = 30)
5743
nvertices += isodd(nvertices)
5844
nhalf = div(nvertices, 2)
5945

60-
R = rotation(c)
46+
R = cylinder_rotation_matrix(direction(c))
6147
step = 2pi / nhalf
6248

6349
ns = Vector{Vec3f}(undef, nhalf + 2)

src/primitives/cylinders.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ radius(c::Cylinder) = c.r
2121
height(c::Cylinder) = norm(c.extremity - c.origin)
2222
direction(c::Cylinder) = (c.extremity .- c.origin) ./ height(c)
2323

24-
function rotation(c::Cylinder{T}) where {T}
25-
d3 = direction(c)
24+
"""
25+
cylinder_rotation_matrix(direction::VecTypes{3})
26+
27+
Creates a basis transformation matrix `R` that maps the third dimension to the
28+
given `direction` and the first and second to orthogonal directions. This allows
29+
you to encode a rotation around `direction` in the first two components and
30+
transform it with `R * rotated_point`.
31+
"""
32+
function cylinder_rotation_matrix(d3::VecTypes{3, T}) where {T}
2633
u = Vec{3, T}(d3[1], d3[2], d3[3])
2734
if abs(u[1]) > 0 || abs(u[2]) > 0
2835
v = Vec{3, T}(u[2], -u[1], T(0))
@@ -39,9 +46,9 @@ function coordinates(c::Cylinder{T}, nvertices=30) where {T}
3946
nvertices += isodd(nvertices)
4047
nhalf = div(nvertices, 2)
4148

42-
R = rotation(c)
49+
R = cylinder_rotation_matrix(direction(c))
4350
step = 2pi / nhalf
44-
51+
4552
ps = Vector{Point3{T}}(undef, nvertices + 2)
4653
for i in 1:nhalf
4754
phi = (i-1) * step
@@ -61,9 +68,9 @@ function normals(c::Cylinder, nvertices = 30)
6168
nvertices += isodd(nvertices)
6269
nhalf = div(nvertices, 2)
6370

64-
R = rotation(c)
71+
R = cylinder_rotation_matrix(direction(c))
6572
step = 2pi / nhalf
66-
73+
6774
ns = Vector{Vec3f}(undef, nhalf + 2)
6875
for i in 1:nhalf
6976
phi = (i-1) * step

0 commit comments

Comments
 (0)