2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
15 #if defined(_WIN32) || defined(__i386__)
16 #define BT_USE_SSE_IN_API
19 #include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h"
20 #include "btConvexPolyhedron.h"
21 #include "LinearMath/btConvexHullComputer.h"
23 #include "LinearMath/btGeometryUtil.h"
24 #include "LinearMath/btGrahamScan2dConvexHull.h"
26 btPolyhedralConvexShape::btPolyhedralConvexShape() : btConvexInternalShape(),
31 btPolyhedralConvexShape::~btPolyhedralConvexShape()
35 m_polyhedron->~btConvexPolyhedron();
36 btAlignedFree(m_polyhedron);
40 void btPolyhedralConvexShape::setPolyhedralFeatures(btConvexPolyhedron& polyhedron)
44 *m_polyhedron = polyhedron;
48 void* mem = btAlignedAlloc(sizeof(btConvexPolyhedron), 16);
49 m_polyhedron = new (mem) btConvexPolyhedron(polyhedron);
53 bool btPolyhedralConvexShape::initializePolyhedralFeatures(int shiftVerticesByMargin)
57 m_polyhedron->~btConvexPolyhedron();
58 btAlignedFree(m_polyhedron);
61 void* mem = btAlignedAlloc(sizeof(btConvexPolyhedron), 16);
62 m_polyhedron = new (mem) btConvexPolyhedron;
64 btAlignedObjectArray<btVector3> orgVertices;
66 for (int i = 0; i < getNumVertices(); i++)
68 btVector3& newVertex = orgVertices.expand();
69 getVertex(i, newVertex);
72 btConvexHullComputer conv;
74 if (shiftVerticesByMargin)
76 btAlignedObjectArray<btVector3> planeEquations;
77 btGeometryUtil::getPlaneEquationsFromVertices(orgVertices, planeEquations);
79 btAlignedObjectArray<btVector3> shiftedPlaneEquations;
80 for (int p = 0; p < planeEquations.size(); p++)
82 btVector3 plane = planeEquations[p];
83 // btScalar margin = getMargin();
84 plane[3] -= getMargin();
85 shiftedPlaneEquations.push_back(plane);
88 btAlignedObjectArray<btVector3> tmpVertices;
90 btGeometryUtil::getVerticesFromPlaneEquations(shiftedPlaneEquations, tmpVertices);
92 conv.compute(&tmpVertices[0].getX(), sizeof(btVector3), tmpVertices.size(), 0.f, 0.f);
96 conv.compute(&orgVertices[0].getX(), sizeof(btVector3), orgVertices.size(), 0.f, 0.f);
99 #ifndef BT_RECONSTRUCT_FACES
101 int numVertices = conv.vertices.size();
102 m_polyhedron->m_vertices.resize(numVertices);
103 for (int p = 0; p < numVertices; p++)
105 m_polyhedron->m_vertices[p] = conv.vertices[p];
109 for (int j = 0; j < conv.faces.size(); j++)
114 const btConvexHullComputer::Edge* edge = &conv.edges[conv.faces[j]];
115 v0 = edge->getSourceVertex();
117 combinedFace.m_indices.push_back(v0);
118 v1 = edge->getTargetVertex();
121 btVector3 wa = conv.vertices[prevVertex];
122 btVector3 wb = conv.vertices[v1];
123 btVector3 newEdge = wb - wa;
126 edges[numEdges++] = newEdge;
128 //face->addIndex(v1);
129 combinedFace.m_indices.push_back(v1);
130 edge = edge->getNextEdgeOfFace();
132 int v01 = edge->getSourceVertex();
133 v1 = edge->getTargetVertex();
136 btAssert(combinedFace.m_indices.size() > 2);
138 btVector3 faceNormal = edges[0].cross(edges[1]);
139 faceNormal.normalize();
141 btScalar planeEq = 1e30f;
143 for (int v = 0; v < combinedFace.m_indices.size(); v++)
145 btScalar eq = m_polyhedron->m_vertices[combinedFace.m_indices[v]].dot(faceNormal);
151 combinedFace.m_plane[0] = faceNormal.getX();
152 combinedFace.m_plane[1] = faceNormal.getY();
153 combinedFace.m_plane[2] = faceNormal.getZ();
154 combinedFace.m_plane[3] = -planeEq;
156 m_polyhedron->m_faces.push_back(combinedFace);
159 #else //BT_RECONSTRUCT_FACES
161 btAlignedObjectArray<btVector3> faceNormals;
162 int numFaces = conv.faces.size();
163 faceNormals.resize(numFaces);
164 btConvexHullComputer* convexUtil = &conv;
166 btAlignedObjectArray<btFace> tmpFaces;
167 tmpFaces.resize(numFaces);
169 int numVertices = convexUtil->vertices.size();
170 m_polyhedron->m_vertices.resize(numVertices);
171 for (int p = 0; p < numVertices; p++)
173 m_polyhedron->m_vertices[p] = convexUtil->vertices[p];
176 for (int i = 0; i < numFaces; i++)
178 int face = convexUtil->faces[i];
179 //printf("face=%d\n",face);
180 const btConvexHullComputer::Edge* firstEdge = &convexUtil->edges[face];
181 const btConvexHullComputer::Edge* edge = firstEdge;
185 //compute face normals
189 int src = edge->getSourceVertex();
190 tmpFaces[i].m_indices.push_back(src);
191 int targ = edge->getTargetVertex();
192 btVector3 wa = convexUtil->vertices[src];
194 btVector3 wb = convexUtil->vertices[targ];
195 btVector3 newEdge = wb - wa;
198 edges[numEdges++] = newEdge;
200 edge = edge->getNextEdgeOfFace();
201 } while (edge != firstEdge);
203 btScalar planeEq = 1e30f;
207 faceNormals[i] = edges[0].cross(edges[1]);
208 faceNormals[i].normalize();
209 tmpFaces[i].m_plane[0] = faceNormals[i].getX();
210 tmpFaces[i].m_plane[1] = faceNormals[i].getY();
211 tmpFaces[i].m_plane[2] = faceNormals[i].getZ();
212 tmpFaces[i].m_plane[3] = planeEq;
216 btAssert(0); //degenerate?
217 faceNormals[i].setZero();
220 for (int v = 0; v < tmpFaces[i].m_indices.size(); v++)
222 btScalar eq = m_polyhedron->m_vertices[tmpFaces[i].m_indices[v]].dot(faceNormals[i]);
228 tmpFaces[i].m_plane[3] = -planeEq;
231 //merge coplanar faces and copy them to m_polyhedron
233 btScalar faceWeldThreshold = 0.999f;
234 btAlignedObjectArray<int> todoFaces;
235 for (int i = 0; i < tmpFaces.size(); i++)
236 todoFaces.push_back(i);
238 while (todoFaces.size())
240 btAlignedObjectArray<int> coplanarFaceGroup;
241 int refFace = todoFaces[todoFaces.size() - 1];
243 coplanarFaceGroup.push_back(refFace);
244 btFace& faceA = tmpFaces[refFace];
245 todoFaces.pop_back();
247 btVector3 faceNormalA(faceA.m_plane[0], faceA.m_plane[1], faceA.m_plane[2]);
248 for (int j = todoFaces.size() - 1; j >= 0; j--)
250 int i = todoFaces[j];
251 btFace& faceB = tmpFaces[i];
252 btVector3 faceNormalB(faceB.m_plane[0], faceB.m_plane[1], faceB.m_plane[2]);
253 if (faceNormalA.dot(faceNormalB) > faceWeldThreshold)
255 coplanarFaceGroup.push_back(i);
260 bool did_merge = false;
261 if (coplanarFaceGroup.size() > 1)
263 //do the merge: use Graham Scan 2d convex hull
265 btAlignedObjectArray<GrahamVector3> orgpoints;
266 btVector3 averageFaceNormal(0, 0, 0);
268 for (int i = 0; i < coplanarFaceGroup.size(); i++)
270 // m_polyhedron->m_faces.push_back(tmpFaces[coplanarFaceGroup[i]]);
272 btFace& face = tmpFaces[coplanarFaceGroup[i]];
273 btVector3 faceNormal(face.m_plane[0], face.m_plane[1], face.m_plane[2]);
274 averageFaceNormal += faceNormal;
275 for (int f = 0; f < face.m_indices.size(); f++)
277 int orgIndex = face.m_indices[f];
278 btVector3 pt = m_polyhedron->m_vertices[orgIndex];
282 for (int i = 0; i < orgpoints.size(); i++)
284 //if ((orgpoints[i].m_orgIndex == orgIndex) || ((rotatedPt-orgpoints[i]).length2()<0.0001))
285 if (orgpoints[i].m_orgIndex == orgIndex)
292 orgpoints.push_back(GrahamVector3(pt, orgIndex));
297 for (int i = 0; i < 4; i++)
298 combinedFace.m_plane[i] = tmpFaces[coplanarFaceGroup[0]].m_plane[i];
300 btAlignedObjectArray<GrahamVector3> hull;
302 averageFaceNormal.normalize();
303 GrahamScanConvexHull2D(orgpoints, hull, averageFaceNormal);
305 for (int i = 0; i < hull.size(); i++)
307 combinedFace.m_indices.push_back(hull[i].m_orgIndex);
308 for (int k = 0; k < orgpoints.size(); k++)
310 if (orgpoints[k].m_orgIndex == hull[i].m_orgIndex)
312 orgpoints[k].m_orgIndex = -1; // invalidate...
318 // are there rejected vertices?
319 bool reject_merge = false;
321 for (int i = 0; i < orgpoints.size(); i++)
323 if (orgpoints[i].m_orgIndex == -1)
324 continue; // this is in the hull...
325 // this vertex is rejected -- is anybody else using this vertex?
326 for (int j = 0; j < tmpFaces.size(); j++)
328 btFace& face = tmpFaces[j];
329 // is this a face of the current coplanar group?
330 bool is_in_current_group = false;
331 for (int k = 0; k < coplanarFaceGroup.size(); k++)
333 if (coplanarFaceGroup[k] == j)
335 is_in_current_group = true;
339 if (is_in_current_group) // ignore this face...
341 // does this face use this rejected vertex?
342 for (int v = 0; v < face.m_indices.size(); v++)
344 if (face.m_indices[v] == orgpoints[i].m_orgIndex)
346 // this rejected vertex is used in another face -- reject merge
362 m_polyhedron->m_faces.push_back(combinedFace);
367 for (int i = 0; i < coplanarFaceGroup.size(); i++)
369 btFace face = tmpFaces[coplanarFaceGroup[i]];
370 m_polyhedron->m_faces.push_back(face);
375 #endif //BT_RECONSTRUCT_FACES
377 m_polyhedron->initialize();
383 #define MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
386 btVector3 btPolyhedralConvexShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0) const
388 btVector3 supVec(0, 0, 0);
391 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
393 btVector3 vec = vec0;
394 btScalar lenSqr = vec.length2();
395 if (lenSqr < btScalar(0.0001))
397 vec.setValue(1, 0, 0);
401 btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
408 for (int k = 0; k < getNumVertices(); k += 128)
411 int inner_count = MIN(getNumVertices() - k, 128);
412 for (i = 0; i < inner_count; i++)
413 getVertex(i, temp[i]);
414 i = (int)vec.maxDot(temp, inner_count, newDot);
426 void btPolyhedralConvexShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
434 for (i = 0; i < numVectors; i++)
436 supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
439 for (int j = 0; j < numVectors; j++)
441 const btVector3& vec = vectors[j];
443 for (int k = 0; k < getNumVertices(); k += 128)
446 int inner_count = MIN(getNumVertices() - k, 128);
447 for (i = 0; i < inner_count; i++)
448 getVertex(i, temp[i]);
449 i = (int)vec.maxDot(temp, inner_count, newDot);
450 if (newDot > supportVerticesOut[j][3])
452 supportVerticesOut[j] = temp[i];
453 supportVerticesOut[j][3] = newDot;
461 void btPolyhedralConvexShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
464 //not yet, return box inertia
466 btScalar margin = getMargin();
470 btVector3 aabbMin, aabbMax;
471 getAabb(ident, aabbMin, aabbMax);
472 btVector3 halfExtents = (aabbMax - aabbMin) * btScalar(0.5);
474 btScalar lx = btScalar(2.) * (halfExtents.x() + margin);
475 btScalar ly = btScalar(2.) * (halfExtents.y() + margin);
476 btScalar lz = btScalar(2.) * (halfExtents.z() + margin);
477 const btScalar x2 = lx * lx;
478 const btScalar y2 = ly * ly;
479 const btScalar z2 = lz * lz;
480 const btScalar scaledmass = mass * btScalar(0.08333333);
482 inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));
486 void btPolyhedralConvexAabbCachingShape::setLocalScaling(const btVector3& scaling)
488 btConvexInternalShape::setLocalScaling(scaling);
492 btPolyhedralConvexAabbCachingShape::btPolyhedralConvexAabbCachingShape()
493 : btPolyhedralConvexShape(),
494 m_localAabbMin(1, 1, 1),
495 m_localAabbMax(-1, -1, -1),
496 m_isLocalAabbValid(false)
500 void btPolyhedralConvexAabbCachingShape::getAabb(const btTransform& trans, btVector3& aabbMin, btVector3& aabbMax) const
502 getNonvirtualAabb(trans, aabbMin, aabbMax, getMargin());
505 void btPolyhedralConvexAabbCachingShape::recalcLocalAabb()
507 m_isLocalAabbValid = true;
510 static const btVector3 _directions[] =
512 btVector3(1., 0., 0.),
513 btVector3(0., 1., 0.),
514 btVector3(0., 0., 1.),
515 btVector3(-1., 0., 0.),
516 btVector3(0., -1., 0.),
517 btVector3(0., 0., -1.)};
519 btVector3 _supporting[] =
521 btVector3(0., 0., 0.),
522 btVector3(0., 0., 0.),
523 btVector3(0., 0., 0.),
524 btVector3(0., 0., 0.),
525 btVector3(0., 0., 0.),
526 btVector3(0., 0., 0.)};
528 batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6);
530 for (int i = 0; i < 3; ++i)
532 m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin;
533 m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin;
538 for (int i = 0; i < 3; i++)
540 btVector3 vec(btScalar(0.), btScalar(0.), btScalar(0.));
541 vec[i] = btScalar(1.);
542 btVector3 tmp = localGetSupportingVertex(vec);
543 m_localAabbMax[i] = tmp[i];
544 vec[i] = btScalar(-1.);
545 tmp = localGetSupportingVertex(vec);
546 m_localAabbMin[i] = tmp[i];