Skip to content

Commit 885cdb3

Browse files
committed
deal with conditions for 2d mpr
1 parent d4a49d8 commit 885cdb3

File tree

1 file changed

+21
-9
lines changed
  • src/contactDetection/ContactDetectionMPR

1 file changed

+21
-9
lines changed

src/contactDetection/ContactDetectionMPR/mpr.jl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ function checkIfShapesArePlanar(r0::SupportPoint,r1::SupportPoint,r2::SupportPoi
8181
n3 = cross(r1.p-r0.p, r2.p-r0.p)
8282
neps = Modia3D.nepsType(T)
8383
# the triangle r0-r1-r2 has degenerated into a line segment
84+
# println("norm(n3) = ", norm(n3))
8485
if norm(n3) <= neps
8586
# change search direction for r2
8687
# because we are still interested in distances if shapes are not intersecting
8788
n2 = -n2
8889
r2 = getSupportPoint(shapeA, shapeB, n2)
89-
if abs(dot((r2.p-r1.p),n2)) <= neps
90+
n3 = cross(r1.p-r0.p, r2.p-r0.p) # new normal to the triangle plane (r0-r1-r2_new)
91+
if norm(n3) <= neps
9092
# Shape is purely planar. Computing the shortest distance for a planar shape
9193
# requires an MPR 2D algorithm (using lines instead of triangles as portals).
9294
# However, this is not implemented and therefore the shortest distance cannot be computed
93-
error("MPR: Shapes are planar and MPR2D is not supported. abs(dot((r2.p-r1.p),n2)). Look at $(Modia3D.fullName(shapeA)) and $(Modia3D.fullName(shapeB)).")
95+
error("MPR: Shapes are planar and MPR2D is not supported. norm(cross(r1.p-r0.p, r2.p-r0.p)). Look at $(Modia3D.fullName(shapeA)) and $(Modia3D.fullName(shapeB)).")
9496
end
95-
# new normal to the triangle plane (r0-r1-r2_new)
96-
n3 = cross(r1.p-r0.p, r2.p-r0.p) # |n3| > 0 guaranteed, due to construction
9797
end
9898

9999
if dot(n3,r0.p) >= 0.0
@@ -103,14 +103,15 @@ function checkIfShapesArePlanar(r0::SupportPoint,r1::SupportPoint,r2::SupportPoi
103103
# check if portal triangle r1-r2-r3 has degenerated into a line segment <--> points r1,r2,r3 are on the same line
104104
r3 = getSupportPoint(shapeA, shapeB, Basics.normalizeVector(n3))
105105
n3b = cross(r2.p-r1.p, r3.p-r1.p)
106+
# println("norm(n3b) = ", norm(n3b))
106107
if norm(n3b) <= neps
107108
# change search direction for r3
108109
r3 = getSupportPoint(shapeA, shapeB, -r3.n)
109-
if abs(dot((r3.p-r1.p),r3.n)) <= neps
110+
if norm(cross(r2.p-r1.p, r3.p-r1.p)) <= neps
110111
# Shape is purely planar. Computing the shortest distance for a planar shape
111112
# requires an MPR 2D algorithm (using lines instead of triangles as portals).
112113
# However, this is not implemented and therefore the shortest distance cannot be computed
113-
error("MPR: Shapes are planar and MPR2D is not supported. r1, r2, r3 are on the same ray. abs(dot((r3.p-r1.p),r3.n)) <= neps. Look at $(Modia3D.fullName(shapeA)) and $(Modia3D.fullName(shapeB)).")
114+
error("MPR: Shapes are planar and MPR2D is not supported. r1, r2, r3 are on the same ray. norm(cross(r2.p-r1.p, r3.p-r1.p)) <= neps. Look at $(Modia3D.fullName(shapeA)) and $(Modia3D.fullName(shapeB)).")
114115
end
115116
end
116117

@@ -162,15 +163,16 @@ function constructR4(r0::SupportPoint,r1::SupportPoint,r2::SupportPoint,r3::Supp
162163
r4 = SupportPoint{T}
163164
n4 = cross(r2.p-r1.p, r3.p-r1.p)
164165
neps = Modia3D.nepsType(T)
166+
# println("norm(n4) = ", norm(n4))
165167
if norm(n4) <= neps
166168
r3 = getSupportPoint(shapeA, shapeB, -r3.n, scale=scale) # change search direction
167-
if abs(dot((r3.p-r1.p),r3.n)) <= neps
169+
n4 = cross(r2.p-r1.p, r3.p-r1.p)
170+
if norm(n4) <= neps
168171
# Shape is purely planar. Computing the shortest distance for a planar shape
169172
# requires an MPR 2D algorithm (using lines instead of triangles as portals).
170173
# However, this is not implemented and therefore the shortest distance cannot be computed
171-
error("MPR: Shapes are planar and MPR2D is not supported. abs(dot((r3.p-r1.p),r3.n)). Look at $(Modia3D.fullName(shapeA)) and $(Modia3D.fullName(shapeB)).")
174+
error("MPR: Shapes are planar and MPR2D is not supported. norm(n4). Look at $(Modia3D.fullName(shapeA)) and $(Modia3D.fullName(shapeB)).")
172175
end
173-
n4 = cross(r2.p-r1.p, r3.p-r1.p) # |n4| > 0 guaranteed, due to construction
174176
end
175177
if dot(n4,r0.p) >= 0.0
176178
n4 = -n4
@@ -244,6 +246,7 @@ function finalTC3(r0::SupportPoint{T}, r1::SupportPoint{T}, r2::SupportPoint{T},
244246
(r4p, distance) = signedDistanceToPortal(r0.p, r1.p, r2.p, r3.p)
245247
r4 = SupportPoint{T}(r4p, r4.n, r4.a, r4.b)
246248
r4 = barycentric(r1, r2, r3, r4)
249+
#println("distance ", distance)
247250
return distance, r1, r2, r3, r4
248251
end
249252

@@ -368,6 +371,13 @@ function mprGeneral(ch::Composition.ContactDetectionMPR_handler{T,F}, shapeA::Co
368371
# the direction of the origin ray r0 is -r0.p
369372
centroidA = getCentroid(shapeA)
370373
centroidB = getCentroid(shapeB)
374+
if isnan(centroidA[1]) || isnan(centroidA[2]) || isnan(centroidA[3]) || isnan(centroidB[1]) || isnan(centroidB[2]) || isnan(centroidB[3])
375+
println("shapeA.r_abs ", shapeA.r_abs)
376+
println("shapeA.R_abs ", shapeA.R_abs)
377+
println("shapeB.r_abs ", shapeB.r_abs)
378+
println("shapeB.R_abs ", shapeB.R_abs)
379+
error("MPR: One of the absolute position or translation is NaN.")
380+
end
371381
r0 = SupportPoint{T}(centroidA-centroidB, -(centroidA-centroidB), SVector{3,T}(0.0,0.0,0.0), SVector{3,T}(0.0,0.0,0.0))
372382
# check if centers of shapes are overlapping
373383
checkCentersOfShapesOverlapp(r0, shapeA, shapeB)
@@ -381,6 +391,7 @@ function mprGeneral(ch::Composition.ContactDetectionMPR_handler{T,F}, shapeA::Co
381391
### Phase 1.3: construction of initial r2 ###
382392
n2 = cross(r0.n, r1.p)
383393
n2abs = norm(n2)
394+
# println("norm(n2) = ", norm(n2))
384395
## TERMINATION CONDITION 1 ##
385396
if n2abs <= neps
386397
# r0 || r1, r0 ist parallel zu r1
@@ -389,6 +400,7 @@ function mprGeneral(ch::Composition.ContactDetectionMPR_handler{T,F}, shapeA::Co
389400
# e.g. any collision/or distance between two spheres
390401
#println("TC 1")
391402
distance = dot(r1.p,normalize(r0.p))
403+
#println("distance ", distance)
392404
return (distance, r1.a, r1.b, r1.n, false, Modia3D.ZeroVector3D(T), Modia3D.ZeroVector3D(T), Modia3D.ZeroVector3D(T), Modia3D.ZeroVector3D(T), Modia3D.ZeroVector3D(T), Modia3D.ZeroVector3D(T) )
393405
else
394406
# normalize n2

0 commit comments

Comments
 (0)