Skip to content

Commit c227571

Browse files
committed
Increase precision in intersection of TorusMantle
1 parent 965f96d commit c227571

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/ConstructiveSolidGeometry/SurfacePrimitives/TorusMantle.jl

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,33 +214,36 @@ Calculates the intersections of a `Line` with a `TorusMantle`.
214214
function intersection(tm::TorusMantle{T}, l::Line{T}) where {T}
215215
obj_l = _transform_into_object_coordinate_system(l, tm) # direction is not normalized
216216

217-
L1 = obj_l.origin.x
218-
L2 = obj_l.origin.y
219-
L3 = obj_l.origin.z
220-
D1 = obj_l.direction.x
221-
D2 = obj_l.direction.y
222-
D3 = obj_l.direction.z
217+
let T2 = Float64 # will this work on 32bit-machines?
223218

224-
R = tm.r_torus
225-
r = tm.r_tube
219+
L1::T2 = obj_l.origin.x |> T2
220+
L2::T2 = obj_l.origin.y |> T2
221+
L3::T2 = obj_l.origin.z |> T2
222+
D1::T2 = obj_l.direction.x |> T2
223+
D2::T2 = obj_l.direction.y |> T2
224+
D3::T2 = obj_l.direction.z |> T2
226225

227-
A = L1^2 + L2^2 + L3^2 + R^2 - r^2
228-
B = 2*(L1*D1 + L2*D2 + L3*D3)
229-
C = D1^2 + D2^2 + D3^2
226+
R::T2 = tm.r_torus |> T2
227+
r::T2 = tm.r_tube |> T2
230228

231-
a = (2*B*C) / C^2
232-
b = (2*A*C + B^2 - 4*R^2*(D1^2 + D2^2)) / C^2
233-
c = (2*A*B - 8*R^2*(L1*D1 + L2*D2)) / C^2
234-
d = (A^2 - 4*R^2*(L1^2 + L2^2)) / C^2
229+
A::T2 = L1^2 + L2^2 + L3^2 + R^2 - r^2
230+
B::T2 = 2*(L1*D1 + L2*D2 + L3*D3)
231+
C::T2 = D1^2 + D2^2 + D3^2
232+
233+
a::T2 = (2*B*C)
234+
b::T2 = (2*A*C + B^2 - 4*R^2*(D1^2 + D2^2))
235+
c::T2 = (2*A*B - 8*R^2*(L1*D1 + L2*D2))
236+
d::T2 = (A^2 - 4*R^2*(L1^2 + L2^2))
235237

236238
# Solve: `solve (sqrt((L1 + λ*D1)^2 + (L2 + λ*D2)^2)-R)^2 + (L3 + λ*D3)^2 = r^2 for λ`
237239

238240
# λ1, λ2, λ3, λ4 = roots_of_4th_order_polynomial(a, b, c, d) # That does not work for all combinations of a, b, c, d...
239241
# fallback to Polynomials.jl, which is slower... We should improve `roots_of_4th_order_polynomial`...
240242
return broadcast-> _transform_into_global_coordinate_system(obj_l.origin + λ * obj_l.direction, tm),
241-
real.(Polynomials.roots(Polynomial((d, c, b, a, one(T))))))
243+
T.(real.(Polynomials.roots(Polynomial((d, c, b, a, C^2))))))
242244
# pts[.!in.(pts, Ref(tm))] .= Ref(CartesianPoint{T}(NaN,NaN,NaN)) # for Partial Tori: discard all intersection points that are not part of the Torus
243245
# return pts
246+
end
244247
end
245248

246249
# These function compose cross sections of Tori at constant θ and ensure that the height is always positive

0 commit comments

Comments
 (0)