Bullet Collision Detection & Physics Library
btPolyhedralContactClipping.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16
20
21
24
25#include <float.h> //for FLT_MAX
26
30
31// Clips a face to the back of a plane
33{
34
35 int ve;
36 btScalar ds, de;
37 int numVerts = pVtxIn.size();
38 if (numVerts < 2)
39 return;
40
43
45
46 for (ve = 0; ve < numVerts; ve++)
47 {
49
51
52 if (ds<0)
53 {
54 if (de<0)
55 {
56 // Start < 0, end < 0, so output endVertex
57 ppVtxOut.push_back(endVertex);
58 }
59 else
60 {
61 // Start < 0, end >= 0, so output intersection
62 ppVtxOut.push_back( firstVertex.lerp(endVertex,btScalar(ds * 1.f/(ds - de))));
63 }
64 }
65 else
66 {
67 if (de<0)
68 {
69 // Start >= 0, end < 0 so output intersection and end
70 ppVtxOut.push_back(firstVertex.lerp(endVertex,btScalar(ds * 1.f/(ds - de))));
71 ppVtxOut.push_back(endVertex);
72 }
73 }
75 ds = de;
76 }
77}
78
79
81{
86
89
90 if(Max0<Min1 || Max1<Min0)
91 return false;
92
93 btScalar d0 = Max0 - Min1;
94 btAssert(d0>=0.0f);
95 btScalar d1 = Max1 - Min0;
96 btAssert(d1>=0.0f);
97 if (d0<d1)
98 {
99 depth = d0;
102
103 } else
104 {
105 depth = d1;
108 }
109
110 return true;
111}
112
113
114
116
117inline bool IsAlmostZero(const btVector3& v)
118{
119 if(btFabs(v.x())>1e-6 || btFabs(v.y())>1e-6 || btFabs(v.z())>1e-6) return false;
120 return true;
121}
122
123#ifdef TEST_INTERNAL_OBJECTS
124
125inline void BoxSupport(const btScalar extents[3], const btScalar sv[3], btScalar p[3])
126{
127 // This version is ~11.000 cycles (4%) faster overall in one of the tests.
128// IR(p[0]) = IR(extents[0])|(IR(sv[0])&SIGN_BITMASK);
129// IR(p[1]) = IR(extents[1])|(IR(sv[1])&SIGN_BITMASK);
130// IR(p[2]) = IR(extents[2])|(IR(sv[2])&SIGN_BITMASK);
131 p[0] = sv[0] < 0.0f ? -extents[0] : extents[0];
132 p[1] = sv[1] < 0.0f ? -extents[1] : extents[1];
133 p[2] = sv[2] < 0.0f ? -extents[2] : extents[2];
134}
135
137{
138 const btMatrix3x3& rot = tr.getBasis();
139 const btVector3& r0 = rot[0];
140 const btVector3& r1 = rot[1];
141 const btVector3& r2 = rot[2];
142
143 const btScalar x = r0.x()*in.x() + r1.x()*in.y() + r2.x()*in.z();
144 const btScalar y = r0.y()*in.x() + r1.y()*in.y() + r2.y()*in.z();
145 const btScalar z = r0.z()*in.x() + r1.z()*in.y() + r2.z()*in.z();
146
147 out.setValue(x, y, z);
148}
149
151{
152 const btScalar dp = delta_c.dot(axis);
153
158
159 btScalar p0[3];
160 BoxSupport(convex0.m_extents, localAxis0, p0);
161 btScalar p1[3];
162 BoxSupport(convex1.m_extents, localAxis1, p1);
163
164 const btScalar Radius0 = p0[0]*localAxis0.x() + p0[1]*localAxis0.y() + p0[2]*localAxis0.z();
165 const btScalar Radius1 = p1[0]*localAxis1.x() + p1[1]*localAxis1.y() + p1[2]*localAxis1.z();
166
167 const btScalar MinRadius = Radius0>convex0.m_radius ? Radius0 : convex0.m_radius;
168 const btScalar MaxRadius = Radius1>convex1.m_radius ? Radius1 : convex1.m_radius;
169
171 const btScalar d0 = MinMaxRadius + dp;
172 const btScalar d1 = MinMaxRadius - dp;
173
174 const btScalar depth = d0<d1 ? d0:d1;
175 if(depth>dmin)
176 return false;
177 return true;
178}
179#endif //TEST_INTERNAL_OBJECTS
180
181
182
188 const btVector3& translation,
190 const btVector3& dirB, btScalar hlenB )
191{
192 // compute the parameters of the closest points on each line segment
193
197
199
200 if ( denom == 0.0f ) {
201 tA = 0.0f;
202 } else {
204 if ( tA < -hlenA )
205 tA = -hlenA;
206 else if ( tA > hlenA )
207 tA = hlenA;
208 }
209
211
212 if ( tB < -hlenB ) {
213 tB = -hlenB;
215
216 if ( tA < -hlenA )
217 tA = -hlenA;
218 else if ( tA > hlenA )
219 tA = hlenA;
220 } else if ( tB > hlenB ) {
221 tB = hlenB;
223
224 if ( tA < -hlenA )
225 tA = -hlenA;
226 else if ( tA > hlenA )
227 tA = hlenA;
228 }
229
230 // compute the closest points relative to segment centers.
231
232 offsetA = dirA * tA;
233 offsetB = dirB * tB;
234
236}
237
238
239
241{
243
244//#ifdef TEST_INTERNAL_OBJECTS
245 const btVector3 c0 = transA * hullA.m_localCenter;
246 const btVector3 c1 = transB * hullB.m_localCenter;
247 const btVector3 DeltaC2 = c0 - c1;
248//#endif
249
251 int curPlaneTests=0;
252
253 int numFacesA = hullA.m_faces.size();
254 // Test normals from hullA
255 for(int i=0;i<numFacesA;i++)
256 {
257 const btVector3 Normal(hullA.m_faces[i].m_plane[0], hullA.m_faces[i].m_plane[1], hullA.m_faces[i].m_plane[2]);
258 btVector3 faceANormalWS = transA.getBasis() * Normal;
259 if (DeltaC2.dot(faceANormalWS)<0)
260 faceANormalWS*=-1.f;
261
263#ifdef TEST_INTERNAL_OBJECTS
266 continue;
268#endif
269
270 btScalar d;
273 return false;
274
275 if(d<dmin)
276 {
277 dmin = d;
279 }
280 }
281
282 int numFacesB = hullB.m_faces.size();
283 // Test normals from hullB
284 for(int i=0;i<numFacesB;i++)
285 {
286 const btVector3 Normal(hullB.m_faces[i].m_plane[0], hullB.m_faces[i].m_plane[1], hullB.m_faces[i].m_plane[2]);
287 btVector3 WorldNormal = transB.getBasis() * Normal;
288 if (DeltaC2.dot(WorldNormal)<0)
289 WorldNormal *=-1.f;
290
292#ifdef TEST_INTERNAL_OBJECTS
295 continue;
297#endif
298
299 btScalar d;
302 return false;
303
304 if(d<dmin)
305 {
306 dmin = d;
308 }
309 }
310
312 int edgeA=-1;
313 int edgeB=-1;
317
318
319 int curEdgeEdge = 0;
320 // Test edges
321 for(int e0=0;e0<hullA.m_uniqueEdges.size();e0++)
322 {
323 const btVector3 edge0 = hullA.m_uniqueEdges[e0];
324 const btVector3 WorldEdge0 = transA.getBasis() * edge0;
325 for(int e1=0;e1<hullB.m_uniqueEdges.size();e1++)
326 {
327 const btVector3 edge1 = hullB.m_uniqueEdges[e1];
328 const btVector3 WorldEdge1 = transB.getBasis() * edge1;
329
331 curEdgeEdge++;
332 if(!IsAlmostZero(Cross))
333 {
334 Cross = Cross.normalize();
335 if (DeltaC2.dot(Cross)<0)
336 Cross *= -1.f;
337
338
339#ifdef TEST_INTERNAL_OBJECTS
342 continue;
344#endif
345
346 btScalar dist;
348 if(!TestSepAxis( hullA, hullB, transA,transB, Cross, dist,wA,wB))
349 return false;
350
351 if(dist<dmin)
352 {
353 dmin = dist;
354 sep = Cross;
355 edgeA=e0;
356 edgeB=e1;
361 }
362 }
363 }
364
365 }
366
367 if (edgeA>=0&&edgeB>=0)
368 {
369// printf("edge-edge\n");
370 //add an edge-edge contact
371
375 btScalar tA;
376 btScalar tB;
377
379
382
383 btScalar hlenB = 1e30f;
384 btScalar hlenA = 1e30f;
385
388 dirA, hlenA,
389 dirB,hlenB);
390
391 btScalar nlSqrt = ptsVector.length2();
393 {
395 ptsVector *= 1.f/nl;
396 if (ptsVector.dot(DeltaC2)<0.f)
397 {
398 ptsVector*=-1.f;
399 }
401 btScalar distance = nl;
402 resultOut.addContactPoint(ptsVector, ptOnB,-distance);
403 }
404
405 }
406
407
408 if((DeltaC2.dot(sep))<0.0f)
409 sep = -sep;
410
411 return true;
412}
413
415{
416 worldVertsB2.resize(0);
417 btVertexArray* pVtxIn = &worldVertsB1;
418 btVertexArray* pVtxOut = &worldVertsB2;
419 pVtxOut->reserve(pVtxIn->size());
420
421 int closestFaceA=-1;
422 {
424 for(int face=0;face<hullA.m_faces.size();face++)
425 {
426 const btVector3 Normal(hullA.m_faces[face].m_plane[0], hullA.m_faces[face].m_plane[1], hullA.m_faces[face].m_plane[2]);
427 const btVector3 faceANormalWS = transA.getBasis() * Normal;
428
430 if (d < dmin)
431 {
432 dmin = d;
433 closestFaceA = face;
434 }
435 }
436 }
437 if (closestFaceA<0)
438 return;
439
440 const btFace& polyA = hullA.m_faces[closestFaceA];
441
442 // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
444 for(int e0=0;e0<numVerticesA;e0++)
445 {
446 const btVector3& a = hullA.m_vertices[polyA.m_indices[e0]];
447 const btVector3& b = hullA.m_vertices[polyA.m_indices[(e0+1)%numVerticesA]];
448 const btVector3 edge0 = a - b;
449 const btVector3 WorldEdge0 = transA.getBasis() * edge0;
450 btVector3 worldPlaneAnormal1 = transA.getBasis()* btVector3(polyA.m_plane[0],polyA.m_plane[1],polyA.m_plane[2]);
451
455
456//int otherFace=0;
457#ifdef BLA1
458 int otherFace = polyA.m_connectedFaces[e0];
459 btVector3 localPlaneNormal (hullA.m_faces[otherFace].m_plane[0],hullA.m_faces[otherFace].m_plane[1],hullA.m_faces[otherFace].m_plane[2]);
460 btScalar localPlaneEq = hullA.m_faces[otherFace].m_plane[3];
461
464#else
467
468#endif
469 //clip face
470
473 pVtxOut->resize(0);
474 }
475
476
477
478//#define ONLY_REPORT_DEEPEST_POINT
479
480 btVector3 point;
481
482
483 // only keep points that are behind the witness face
484 {
485 btVector3 localPlaneNormal (polyA.m_plane[0],polyA.m_plane[1],polyA.m_plane[2]);
486 btScalar localPlaneEq = polyA.m_plane[3];
489 for (int i=0;i<pVtxIn->size();i++)
490 {
491 btVector3 vtx = pVtxIn->at(i);
493 if (depth <=minDist)
494 {
495// printf("clamped: depth=%f to minDist=%f\n",depth,minDist);
496 depth = minDist;
497 }
498
499 if (depth <=maxDist)
500 {
501 btVector3 point = pVtxIn->at(i);
502#ifdef ONLY_REPORT_DEEPEST_POINT
504#else
505#if 0
506 if (depth<-3)
507 {
508 printf("error in btPolyhedralContactClipping depth = %f\n", depth);
509 printf("likely wrong separatingNormal passed in\n");
510 }
511#endif
512 resultOut.addContactPoint(separatingNormal,point,depth);
513#endif
514 }
515 }
516 }
517#ifdef ONLY_REPORT_DEEPEST_POINT
519 {
520 resultOut.addContactPoint(separatingNormal,point,curMaxDist);
521 }
522#endif //ONLY_REPORT_DEEPEST_POINT
523
524}
525
526
527
528
529
531{
532
534// const btVector3 c0 = transA * hullA.m_localCenter;
535// const btVector3 c1 = transB * hullB.m_localCenter;
536 //const btVector3 DeltaC2 = c0 - c1;
537
538
539
540 int closestFaceB=-1;
542 {
543 for(int face=0;face<hullB.m_faces.size();face++)
544 {
545 const btVector3 Normal(hullB.m_faces[face].m_plane[0], hullB.m_faces[face].m_plane[1], hullB.m_faces[face].m_plane[2]);
546 const btVector3 WorldNormal = transB.getBasis() * Normal;
548 if (d > dmax)
549 {
550 dmax = d;
551 closestFaceB = face;
552 }
553 }
554 }
555 worldVertsB1.resize(0);
556 {
557 const btFace& polyB = hullB.m_faces[closestFaceB];
558 const int numVertices = polyB.m_indices.size();
559 for(int e0=0;e0<numVertices;e0++)
560 {
561 const btVector3& b = hullB.m_vertices[polyB.m_indices[e0]];
562 worldVertsB1.push_back(transB*b);
563 }
564 }
565
566
567 if (closestFaceB>=0)
569
570}
bool IsAlmostZero(const btVector3 &v)
const T & btMax(const T &a, const T &b)
Definition btMinMax.h:29
static bool TestSepAxis(const btConvexPolyhedron &hullA, const btConvexPolyhedron &hullB, const btTransform &transA, const btTransform &transB, const btVector3 &sep_axis, btScalar &depth, btVector3 &witnessPointA, btVector3 &witnessPointB)
int gExpectedNbTests
This file was written by Erwin Coumans Separating axis rest based on work from Pierre Terdiman,...
bool IsAlmostZero(const btVector3 &v)
void btSegmentsClosestPoints(btVector3 &ptsVector, btVector3 &offsetA, btVector3 &offsetB, btScalar &tA, btScalar &tB, const btVector3 &translation, const btVector3 &dirA, btScalar hlenA, const btVector3 &dirB, btScalar hlenB)
static int gActualSATPairTests
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:292
btScalar btSqrt(btScalar y)
Definition btScalar.h:444
btScalar btFabs(btScalar x)
Definition btScalar.h:475
#define SIMD_FORCE_INLINE
Definition btScalar.h:81
#define SIMD_EPSILON
Definition btScalar.h:521
void btSwap(T &a, T &b)
Definition btScalar.h:621
#define btAssert(x)
Definition btScalar.h:131
static btMatrix3x3 Cross(const btVector3 &v)
btScalar btDot(const btVector3 &v1, const btVector3 &v2)
Return the dot product between two vectors.
Definition btVector3.h:901
int size() const
return the number of elements in the array
void resize(int newsize, const T &fillData=T())
void push_back(const T &_Val)
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition btMatrix3x3.h:48
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition btTransform.h:34
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:84
const btScalar & z() const
Return the z value.
Definition btVector3.h:591
btVector3 cross(const btVector3 &v) const
Return the cross product between this and another vector.
Definition btVector3.h:389
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition btVector3.h:235
btVector3 normalized() const
Return a normalized version of this vector.
Definition btVector3.h:964
const btScalar & x() const
Return the x value.
Definition btVector3.h:587
const btScalar & y() const
Return the y value.
Definition btVector3.h:589
btAlignedObjectArray< int > m_indices
static void clipFace(const btVertexArray &pVtxIn, btVertexArray &ppVtxOut, const btVector3 &planeNormalWS, btScalar planeEqWS)
the clipFace method is used internally
static void clipHullAgainstHull(const btVector3 &separatingNormal1, const btConvexPolyhedron &hullA, const btConvexPolyhedron &hullB, const btTransform &transA, const btTransform &transB, const btScalar minDist, btScalar maxDist, btVertexArray &worldVertsB1, btVertexArray &worldVertsB2, btDiscreteCollisionDetectorInterface::Result &resultOut)
static void clipFaceAgainstHull(const btVector3 &separatingNormal, const btConvexPolyhedron &hullA, const btTransform &transA, btVertexArray &worldVertsB1, btVertexArray &worldVertsB2, const btScalar minDist, btScalar maxDist, btDiscreteCollisionDetectorInterface::Result &resultOut)
static bool findSeparatingAxis(const btConvexPolyhedron &hullA, const btConvexPolyhedron &hullB, const btTransform &transA, const btTransform &transB, btVector3 &sep, btDiscreteCollisionDetectorInterface::Result &resultOut)