/* Copyright (c) 2012 Advanced Micro Devices, Inc. This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 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. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ //Originally written by Erwin Coumans #include "b3ConvexUtility.h" #include "Bullet3Geometry/b3ConvexHullComputer.h" #include "Bullet3Geometry/b3GrahamScan2dConvexHull.h" #include "Bullet3Common/b3Quaternion.h" #include "Bullet3Common/b3HashMap.h" b3ConvexUtility::~b3ConvexUtility() { } bool b3ConvexUtility::initializePolyhedralFeatures(const b3Vector3* orgVertices, int numPoints, bool mergeCoplanarTriangles) { b3ConvexHullComputer conv; conv.compute(&orgVertices[0].getX(), sizeof(b3Vector3),numPoints,0.f,0.f); b3AlignedObjectArray faceNormals; int numFaces = conv.faces.size(); faceNormals.resize(numFaces); b3ConvexHullComputer* convexUtil = &conv; b3AlignedObjectArray tmpFaces; tmpFaces.resize(numFaces); int numVertices = convexUtil->vertices.size(); m_vertices.resize(numVertices); for (int p=0;pvertices[p]; } for (int i=0;ifaces[i]; //printf("face=%d\n",face); const b3ConvexHullComputer::Edge* firstEdge = &convexUtil->edges[face]; const b3ConvexHullComputer::Edge* edge = firstEdge; b3Vector3 edges[3]; int numEdges = 0; //compute face normals do { int src = edge->getSourceVertex(); tmpFaces[i].m_indices.push_back(src); int targ = edge->getTargetVertex(); b3Vector3 wa = convexUtil->vertices[src]; b3Vector3 wb = convexUtil->vertices[targ]; b3Vector3 newEdge = wb-wa; newEdge.normalize(); if (numEdges<2) edges[numEdges++] = newEdge; edge = edge->getNextEdgeOfFace(); } while (edge!=firstEdge); b3Scalar planeEq = 1e30f; if (numEdges==2) { faceNormals[i] = edges[0].cross(edges[1]); faceNormals[i].normalize(); tmpFaces[i].m_plane[0] = faceNormals[i].getX(); tmpFaces[i].m_plane[1] = faceNormals[i].getY(); tmpFaces[i].m_plane[2] = faceNormals[i].getZ(); tmpFaces[i].m_plane[3] = planeEq; } else { b3Assert(0);//degenerate? faceNormals[i].setZero(); } for (int v=0;veq) { planeEq=eq; } } tmpFaces[i].m_plane[3] = -planeEq; } //merge coplanar faces and copy them to m_polyhedron b3Scalar faceWeldThreshold= 0.999f; b3AlignedObjectArray todoFaces; for (int i=0;i coplanarFaceGroup; int refFace = todoFaces[todoFaces.size()-1]; coplanarFaceGroup.push_back(refFace); b3MyFace& faceA = tmpFaces[refFace]; todoFaces.pop_back(); b3Vector3 faceNormalA = b3MakeVector3(faceA.m_plane[0],faceA.m_plane[1],faceA.m_plane[2]); for (int j=todoFaces.size()-1;j>=0;j--) { int i = todoFaces[j]; b3MyFace& faceB = tmpFaces[i]; b3Vector3 faceNormalB = b3MakeVector3(faceB.m_plane[0],faceB.m_plane[1],faceB.m_plane[2]); if (faceNormalA.dot(faceNormalB)>faceWeldThreshold) { coplanarFaceGroup.push_back(i); todoFaces.remove(i); } } bool did_merge = false; if (coplanarFaceGroup.size()>1) { //do the merge: use Graham Scan 2d convex hull b3AlignedObjectArray orgpoints; b3Vector3 averageFaceNormal = b3MakeVector3(0,0,0); for (int i=0;im_faces.push_back(tmpFaces[coplanarFaceGroup[i]]); b3MyFace& face = tmpFaces[coplanarFaceGroup[i]]; b3Vector3 faceNormal = b3MakeVector3(face.m_plane[0],face.m_plane[1],face.m_plane[2]); averageFaceNormal+=faceNormal; for (int f=0;f hull; averageFaceNormal.normalize(); b3GrahamScanConvexHull2D(orgpoints,hull,averageFaceNormal); for (int i=0;i1e-6 || fabsf(v.getY())>1e-6 || fabsf(v.getZ())>1e-6) return false; return true; } struct b3InternalVertexPair { b3InternalVertexPair(short int v0,short int v1) :m_v0(v0), m_v1(v1) { if (m_v1>m_v0) b3Swap(m_v0,m_v1); } short int m_v0; short int m_v1; int getHash() const { return m_v0+(m_v1<<16); } bool equals(const b3InternalVertexPair& other) const { return m_v0==other.m_v0 && m_v1==other.m_v1; } }; struct b3InternalEdge { b3InternalEdge() :m_face0(-1), m_face1(-1) { } short int m_face0; short int m_face1; }; // #ifdef TEST_INTERNAL_OBJECTS bool b3ConvexUtility::testContainment() const { for(int p=0;p<8;p++) { b3Vector3 LocalPt; if(p==0) LocalPt = m_localCenter + b3Vector3(m_extents[0], m_extents[1], m_extents[2]); else if(p==1) LocalPt = m_localCenter + b3Vector3(m_extents[0], m_extents[1], -m_extents[2]); else if(p==2) LocalPt = m_localCenter + b3Vector3(m_extents[0], -m_extents[1], m_extents[2]); else if(p==3) LocalPt = m_localCenter + b3Vector3(m_extents[0], -m_extents[1], -m_extents[2]); else if(p==4) LocalPt = m_localCenter + b3Vector3(-m_extents[0], m_extents[1], m_extents[2]); else if(p==5) LocalPt = m_localCenter + b3Vector3(-m_extents[0], m_extents[1], -m_extents[2]); else if(p==6) LocalPt = m_localCenter + b3Vector3(-m_extents[0], -m_extents[1], m_extents[2]); else if(p==7) LocalPt = m_localCenter + b3Vector3(-m_extents[0], -m_extents[1], -m_extents[2]); for(int i=0;i0.0f) return false; } } return true; } #endif void b3ConvexUtility::initialize() { b3HashMap edges; b3Scalar TotalArea = 0.0f; m_localCenter.setValue(0, 0, 0); for(int i=0;im_face0>=0); // b3Assert(edptr->m_face1<0); edptr->m_face1 = i; } else { b3InternalEdge ed; ed.m_face0 = i; edges.insert(vp,ed); } } } #ifdef USE_CONNECTED_FACES for(int i=0;im_face0>=0); b3Assert(edptr->m_face1>=0); int connectedFace = (edptr->m_face0==i)?edptr->m_face1:edptr->m_face0; m_faces[i].m_connectedFaces[j] = connectedFace; } } #endif//USE_CONNECTED_FACES for(int i=0;iMaxX) MaxX = pt.getX(); if(pt.getY()MaxY) MaxY = pt.getY(); if(pt.getZ()MaxZ) MaxZ = pt.getZ(); } mC.setValue(MaxX+MinX, MaxY+MinY, MaxZ+MinZ); mE.setValue(MaxX-MinX, MaxY-MinY, MaxZ-MinZ); // const b3Scalar r = m_radius / sqrtf(2.0f); const b3Scalar r = m_radius / sqrtf(3.0f); const int LargestExtent = mE.maxAxis(); const b3Scalar Step = (mE[LargestExtent]*0.5f - r)/1024.0f; m_extents[0] = m_extents[1] = m_extents[2] = r; m_extents[LargestExtent] = mE[LargestExtent]*0.5f; bool FoundBox = false; for(int j=0;j<1024;j++) { if(testContainment()) { FoundBox = true; break; } m_extents[LargestExtent] -= Step; } if(!FoundBox) { m_extents[0] = m_extents[1] = m_extents[2] = r; } else { // Refine the box const b3Scalar Step = (m_radius - r)/1024.0f; const int e0 = (1<