Skip to content

Commit 739d225

Browse files
committed
use struct instead of mutable struct for SupportPoint
1 parent 22aaf3a commit 739d225

File tree

1 file changed

+22
-13
lines changed
  • src/contactDetection/ContactDetectionMPR

1 file changed

+22
-13
lines changed

src/contactDetection/ContactDetectionMPR/mpr.jl

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Collision detection algorithm based on the MPR algorithm
66

7-
mutable struct SupportPoint{T}
7+
struct SupportPoint{T}
88
p::SVector{3,T} # support point
99
n::SVector{3,T} # support normal unit vector
1010
a::SVector{3,T} # point on shapeA
@@ -22,7 +22,7 @@ function getSupportPoint(shapeA::Modia3D.Composition.Object3D, shapeB::Compositi
2222
end
2323

2424

25-
function barycentric(r1::SupportPoint,r2::SupportPoint,r3::SupportPoint,r4::SupportPoint)
25+
function barycentric(r1::SupportPoint{T}, r2::SupportPoint{T}, r3::SupportPoint{T}, r4::SupportPoint{T}) where {T}
2626
r21 = r2.p - r1.p
2727
r31 = r3.p - r1.p
2828
r41 = r4.p - r1.p
@@ -42,8 +42,9 @@ function barycentric(r1::SupportPoint,r2::SupportPoint,r3::SupportPoint,r4::Supp
4242
b21 = dot(n31,r41) / dot(n31,r21)
4343
b31 = dot(n21,r41) / dot(n21,r31)
4444

45-
r4.a = r1.a + b21*(r2.a - r1.a) + b31*(r3.a - r1.a)
46-
r4.b = r1.b + b21*(r2.b - r1.b) + b31*(r3.b - r1.b)
45+
a = r1.a + b21*(r2.a - r1.a) + b31*(r3.a - r1.a)
46+
b = r1.b + b21*(r2.b - r1.b) + b31*(r3.b - r1.b)
47+
r4 = SupportPoint{T}(r4.p, r4.n, a, b)
4748

4849

4950
### only works if r4.n goes through zero
@@ -74,6 +75,8 @@ end
7475

7576
function checkIfShapesArePlanar(r0::SupportPoint,r1::SupportPoint,r2::SupportPoint,n2::SVector{3,T},
7677
shapeA::Composition.Object3D,shapeB::Composition.Object3D) where {T}
78+
r3 = SupportPoint{T}
79+
7780
# r3 is in the direction of plane normal that contains triangle r0-r1-r2
7881
n3 = cross(r1.p-r0.p, r2.p-r0.p)
7982
neps = Modia3D.nepsType(T)
@@ -161,6 +164,7 @@ end
161164
# construction of r4
162165
function constructR4(r0::SupportPoint,r1::SupportPoint,r2::SupportPoint,r3::SupportPoint,
163166
shapeA::Composition.Object3D,shapeB::Composition.Object3D, scale::T) where {T}
167+
r4 = SupportPoint{T}
164168
n4 = cross(r2.p-r1.p, r3.p-r1.p)
165169
neps = Modia3D.nepsType(T)
166170
if norm(n4) <= neps
@@ -210,13 +214,13 @@ end
210214
scaleVector(scale, ri) = ri.*scale
211215

212216

213-
function skalarization(r0::SupportPoint, r1::SupportPoint, r2::SupportPoint, r3::SupportPoint)
217+
function skalarization(r0::SupportPoint{T}, r1::SupportPoint{T}, r2::SupportPoint{T}, r3::SupportPoint{T}) where {T}
214218
x = maximum([abs.(r0.p) abs.(r1.p) abs.(r2.p) abs.(r3.p)])
215219
scale = 1/x
216-
r0.p = scaleVector(scale, r0.p)
217-
r1.p = scaleVector(scale, r1.p)
218-
r2.p = scaleVector(scale, r2.p)
219-
r3.p = scaleVector(scale, r3.p)
220+
r0 = SupportPoint{T}(scaleVector(scale, r0.p), r0.n, r0.a, r0.b)
221+
r1 = SupportPoint{T}(scaleVector(scale, r1.p), r1.n, r1.a, r1.b)
222+
r2 = SupportPoint{T}(scaleVector(scale, r2.p), r2.n, r2.a, r2.b)
223+
r3 = SupportPoint{T}(scaleVector(scale, r3.p), r3.n, r3.a, r3.b)
220224
return (r0, r1, r2, r3, scale)
221225
end
222226

@@ -226,12 +230,12 @@ function finalTC2(r1::SupportPoint, r2::SupportPoint, r3::SupportPoint, r4::Supp
226230
# error("shapeA = ", shapeA, " shapeB = ", shapeB)
227231
#end
228232
distance = -dot(r4.n, r4.p)
229-
barycentric(r1,r2,r3,r4)
233+
r4 = barycentric(r1, r2, r3, r4)
230234
return distance, r1, r2, r3, r4
231235
end
232236

233237

234-
function finalTC3(r0::SupportPoint, r1::SupportPoint, r2::SupportPoint, r3::SupportPoint, r4::SupportPoint)
238+
function finalTC3(r0::SupportPoint{T}, r1::SupportPoint{T}, r2::SupportPoint{T}, r3::SupportPoint{T}, r4::SupportPoint{T}) where {T}
235239
#println("TC 3")
236240

237241
#doesRayIntersectPortal(r1.p,r2.p,r3.p, r4.p)
@@ -241,8 +245,10 @@ function finalTC3(r0::SupportPoint, r1::SupportPoint, r2::SupportPoint, r3::Supp
241245
#if !analyzeFinalPortal(r1.p, r2.p, r3.p, r4.p)
242246
# error("shapeA = ", shapeA, " shapeB = ", shapeB)
243247
#end
244-
(r4.p, distance) = signedDistanceToPortal(r0.p,r1.p,r2.p,r3.p)
245-
barycentric(r1,r2,r3,r4)
248+
249+
(r4p, distance) = signedDistanceToPortal(r0.p, r1.p, r2.p, r3.p)
250+
r4 = SupportPoint{T}(r4p, r4.n, r4.a, r4.b)
251+
r4 = barycentric(r1, r2, r3, r4)
246252
return distance, r1, r2, r3, r4
247253
end
248254

@@ -361,6 +367,9 @@ function mprGeneral(ch::Composition.ContactDetectionMPR_handler{T,F}, shapeA::Co
361367
tol_rel = ch.tol_rel
362368
niter_max = ch.niter_max
363369
neps = Modia3D.nepsType(T)
370+
r0 = SupportPoint{T}
371+
r1 = SupportPoint{T}
372+
r2 = SupportPoint{T}
364373

365374
########### Phase 1, Minkowski Portal Refinement ###################
366375
# Construction of r0 and initial portal triangle points r1, r2, r3

0 commit comments

Comments
 (0)