b3ConvexUtility.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. Copyright (c) 2012 Advanced Micro Devices, Inc.
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 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.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. //Originally written by Erwin Coumans
  13. #include "b3ConvexUtility.h"
  14. #include "Bullet3Geometry/b3ConvexHullComputer.h"
  15. #include "Bullet3Geometry/b3GrahamScan2dConvexHull.h"
  16. #include "Bullet3Common/b3Quaternion.h"
  17. #include "Bullet3Common/b3HashMap.h"
  18. b3ConvexUtility::~b3ConvexUtility()
  19. {
  20. }
  21. bool b3ConvexUtility::initializePolyhedralFeatures(const b3Vector3* orgVertices, int numPoints, bool mergeCoplanarTriangles)
  22. {
  23. b3ConvexHullComputer conv;
  24. conv.compute(&orgVertices[0].getX(), sizeof(b3Vector3), numPoints, 0.f, 0.f);
  25. b3AlignedObjectArray<b3Vector3> faceNormals;
  26. int numFaces = conv.faces.size();
  27. faceNormals.resize(numFaces);
  28. b3ConvexHullComputer* convexUtil = &conv;
  29. b3AlignedObjectArray<b3MyFace> tmpFaces;
  30. tmpFaces.resize(numFaces);
  31. int numVertices = convexUtil->vertices.size();
  32. m_vertices.resize(numVertices);
  33. for (int p = 0; p < numVertices; p++)
  34. {
  35. m_vertices[p] = convexUtil->vertices[p];
  36. }
  37. for (int i = 0; i < numFaces; i++)
  38. {
  39. int face = convexUtil->faces[i];
  40. //printf("face=%d\n",face);
  41. const b3ConvexHullComputer::Edge* firstEdge = &convexUtil->edges[face];
  42. const b3ConvexHullComputer::Edge* edge = firstEdge;
  43. b3Vector3 edges[3];
  44. int numEdges = 0;
  45. //compute face normals
  46. do
  47. {
  48. int src = edge->getSourceVertex();
  49. tmpFaces[i].m_indices.push_back(src);
  50. int targ = edge->getTargetVertex();
  51. b3Vector3 wa = convexUtil->vertices[src];
  52. b3Vector3 wb = convexUtil->vertices[targ];
  53. b3Vector3 newEdge = wb - wa;
  54. newEdge.normalize();
  55. if (numEdges < 2)
  56. edges[numEdges++] = newEdge;
  57. edge = edge->getNextEdgeOfFace();
  58. } while (edge != firstEdge);
  59. b3Scalar planeEq = 1e30f;
  60. if (numEdges == 2)
  61. {
  62. faceNormals[i] = edges[0].cross(edges[1]);
  63. faceNormals[i].normalize();
  64. tmpFaces[i].m_plane[0] = faceNormals[i].getX();
  65. tmpFaces[i].m_plane[1] = faceNormals[i].getY();
  66. tmpFaces[i].m_plane[2] = faceNormals[i].getZ();
  67. tmpFaces[i].m_plane[3] = planeEq;
  68. }
  69. else
  70. {
  71. b3Assert(0); //degenerate?
  72. faceNormals[i].setZero();
  73. }
  74. for (int v = 0; v < tmpFaces[i].m_indices.size(); v++)
  75. {
  76. b3Scalar eq = m_vertices[tmpFaces[i].m_indices[v]].dot(faceNormals[i]);
  77. if (planeEq > eq)
  78. {
  79. planeEq = eq;
  80. }
  81. }
  82. tmpFaces[i].m_plane[3] = -planeEq;
  83. }
  84. //merge coplanar faces and copy them to m_polyhedron
  85. b3Scalar faceWeldThreshold = 0.999f;
  86. b3AlignedObjectArray<int> todoFaces;
  87. for (int i = 0; i < tmpFaces.size(); i++)
  88. todoFaces.push_back(i);
  89. while (todoFaces.size())
  90. {
  91. b3AlignedObjectArray<int> coplanarFaceGroup;
  92. int refFace = todoFaces[todoFaces.size() - 1];
  93. coplanarFaceGroup.push_back(refFace);
  94. b3MyFace& faceA = tmpFaces[refFace];
  95. todoFaces.pop_back();
  96. b3Vector3 faceNormalA = b3MakeVector3(faceA.m_plane[0], faceA.m_plane[1], faceA.m_plane[2]);
  97. for (int j = todoFaces.size() - 1; j >= 0; j--)
  98. {
  99. int i = todoFaces[j];
  100. b3MyFace& faceB = tmpFaces[i];
  101. b3Vector3 faceNormalB = b3MakeVector3(faceB.m_plane[0], faceB.m_plane[1], faceB.m_plane[2]);
  102. if (faceNormalA.dot(faceNormalB) > faceWeldThreshold)
  103. {
  104. coplanarFaceGroup.push_back(i);
  105. todoFaces.remove(i);
  106. }
  107. }
  108. bool did_merge = false;
  109. if (coplanarFaceGroup.size() > 1)
  110. {
  111. //do the merge: use Graham Scan 2d convex hull
  112. b3AlignedObjectArray<b3GrahamVector3> orgpoints;
  113. b3Vector3 averageFaceNormal = b3MakeVector3(0, 0, 0);
  114. for (int i = 0; i < coplanarFaceGroup.size(); i++)
  115. {
  116. // m_polyhedron->m_faces.push_back(tmpFaces[coplanarFaceGroup[i]]);
  117. b3MyFace& face = tmpFaces[coplanarFaceGroup[i]];
  118. b3Vector3 faceNormal = b3MakeVector3(face.m_plane[0], face.m_plane[1], face.m_plane[2]);
  119. averageFaceNormal += faceNormal;
  120. for (int f = 0; f < face.m_indices.size(); f++)
  121. {
  122. int orgIndex = face.m_indices[f];
  123. b3Vector3 pt = m_vertices[orgIndex];
  124. bool found = false;
  125. for (int i = 0; i < orgpoints.size(); i++)
  126. {
  127. //if ((orgpoints[i].m_orgIndex == orgIndex) || ((rotatedPt-orgpoints[i]).length2()<0.0001))
  128. if (orgpoints[i].m_orgIndex == orgIndex)
  129. {
  130. found = true;
  131. break;
  132. }
  133. }
  134. if (!found)
  135. orgpoints.push_back(b3GrahamVector3(pt, orgIndex));
  136. }
  137. }
  138. b3MyFace combinedFace;
  139. for (int i = 0; i < 4; i++)
  140. combinedFace.m_plane[i] = tmpFaces[coplanarFaceGroup[0]].m_plane[i];
  141. b3AlignedObjectArray<b3GrahamVector3> hull;
  142. averageFaceNormal.normalize();
  143. b3GrahamScanConvexHull2D(orgpoints, hull, averageFaceNormal);
  144. for (int i = 0; i < hull.size(); i++)
  145. {
  146. combinedFace.m_indices.push_back(hull[i].m_orgIndex);
  147. for (int k = 0; k < orgpoints.size(); k++)
  148. {
  149. if (orgpoints[k].m_orgIndex == hull[i].m_orgIndex)
  150. {
  151. orgpoints[k].m_orgIndex = -1; // invalidate...
  152. break;
  153. }
  154. }
  155. }
  156. // are there rejected vertices?
  157. bool reject_merge = false;
  158. for (int i = 0; i < orgpoints.size(); i++)
  159. {
  160. if (orgpoints[i].m_orgIndex == -1)
  161. continue; // this is in the hull...
  162. // this vertex is rejected -- is anybody else using this vertex?
  163. for (int j = 0; j < tmpFaces.size(); j++)
  164. {
  165. b3MyFace& face = tmpFaces[j];
  166. // is this a face of the current coplanar group?
  167. bool is_in_current_group = false;
  168. for (int k = 0; k < coplanarFaceGroup.size(); k++)
  169. {
  170. if (coplanarFaceGroup[k] == j)
  171. {
  172. is_in_current_group = true;
  173. break;
  174. }
  175. }
  176. if (is_in_current_group) // ignore this face...
  177. continue;
  178. // does this face use this rejected vertex?
  179. for (int v = 0; v < face.m_indices.size(); v++)
  180. {
  181. if (face.m_indices[v] == orgpoints[i].m_orgIndex)
  182. {
  183. // this rejected vertex is used in another face -- reject merge
  184. reject_merge = true;
  185. break;
  186. }
  187. }
  188. if (reject_merge)
  189. break;
  190. }
  191. if (reject_merge)
  192. break;
  193. }
  194. if (!reject_merge)
  195. {
  196. // do this merge!
  197. did_merge = true;
  198. m_faces.push_back(combinedFace);
  199. }
  200. }
  201. if (!did_merge)
  202. {
  203. for (int i = 0; i < coplanarFaceGroup.size(); i++)
  204. {
  205. b3MyFace face = tmpFaces[coplanarFaceGroup[i]];
  206. m_faces.push_back(face);
  207. }
  208. }
  209. }
  210. initialize();
  211. return true;
  212. }
  213. inline bool IsAlmostZero(const b3Vector3& v)
  214. {
  215. if (fabsf(v.getX()) > 1e-6 || fabsf(v.getY()) > 1e-6 || fabsf(v.getZ()) > 1e-6) return false;
  216. return true;
  217. }
  218. struct b3InternalVertexPair
  219. {
  220. b3InternalVertexPair(short int v0, short int v1)
  221. : m_v0(v0),
  222. m_v1(v1)
  223. {
  224. if (m_v1 > m_v0)
  225. b3Swap(m_v0, m_v1);
  226. }
  227. short int m_v0;
  228. short int m_v1;
  229. int getHash() const
  230. {
  231. return m_v0 + (m_v1 << 16);
  232. }
  233. bool equals(const b3InternalVertexPair& other) const
  234. {
  235. return m_v0 == other.m_v0 && m_v1 == other.m_v1;
  236. }
  237. };
  238. struct b3InternalEdge
  239. {
  240. b3InternalEdge()
  241. : m_face0(-1),
  242. m_face1(-1)
  243. {
  244. }
  245. short int m_face0;
  246. short int m_face1;
  247. };
  248. //
  249. #ifdef TEST_INTERNAL_OBJECTS
  250. bool b3ConvexUtility::testContainment() const
  251. {
  252. for (int p = 0; p < 8; p++)
  253. {
  254. b3Vector3 LocalPt;
  255. if (p == 0)
  256. LocalPt = m_localCenter + b3Vector3(m_extents[0], m_extents[1], m_extents[2]);
  257. else if (p == 1)
  258. LocalPt = m_localCenter + b3Vector3(m_extents[0], m_extents[1], -m_extents[2]);
  259. else if (p == 2)
  260. LocalPt = m_localCenter + b3Vector3(m_extents[0], -m_extents[1], m_extents[2]);
  261. else if (p == 3)
  262. LocalPt = m_localCenter + b3Vector3(m_extents[0], -m_extents[1], -m_extents[2]);
  263. else if (p == 4)
  264. LocalPt = m_localCenter + b3Vector3(-m_extents[0], m_extents[1], m_extents[2]);
  265. else if (p == 5)
  266. LocalPt = m_localCenter + b3Vector3(-m_extents[0], m_extents[1], -m_extents[2]);
  267. else if (p == 6)
  268. LocalPt = m_localCenter + b3Vector3(-m_extents[0], -m_extents[1], m_extents[2]);
  269. else if (p == 7)
  270. LocalPt = m_localCenter + b3Vector3(-m_extents[0], -m_extents[1], -m_extents[2]);
  271. for (int i = 0; i < m_faces.size(); i++)
  272. {
  273. const b3Vector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
  274. const b3Scalar d = LocalPt.dot(Normal) + m_faces[i].m_plane[3];
  275. if (d > 0.0f)
  276. return false;
  277. }
  278. }
  279. return true;
  280. }
  281. #endif
  282. void b3ConvexUtility::initialize()
  283. {
  284. b3HashMap<b3InternalVertexPair, b3InternalEdge> edges;
  285. b3Scalar TotalArea = 0.0f;
  286. m_localCenter.setValue(0, 0, 0);
  287. for (int i = 0; i < m_faces.size(); i++)
  288. {
  289. int numVertices = m_faces[i].m_indices.size();
  290. int NbTris = numVertices;
  291. for (int j = 0; j < NbTris; j++)
  292. {
  293. int k = (j + 1) % numVertices;
  294. b3InternalVertexPair vp(m_faces[i].m_indices[j], m_faces[i].m_indices[k]);
  295. b3InternalEdge* edptr = edges.find(vp);
  296. b3Vector3 edge = m_vertices[vp.m_v1] - m_vertices[vp.m_v0];
  297. edge.normalize();
  298. bool found = false;
  299. b3Vector3 diff, diff2;
  300. for (int p = 0; p < m_uniqueEdges.size(); p++)
  301. {
  302. diff = m_uniqueEdges[p] - edge;
  303. diff2 = m_uniqueEdges[p] + edge;
  304. // if ((diff.length2()==0.f) ||
  305. // (diff2.length2()==0.f))
  306. if (IsAlmostZero(diff) ||
  307. IsAlmostZero(diff2))
  308. {
  309. found = true;
  310. break;
  311. }
  312. }
  313. if (!found)
  314. {
  315. m_uniqueEdges.push_back(edge);
  316. }
  317. if (edptr)
  318. {
  319. //TBD: figure out why I added this assert
  320. // b3Assert(edptr->m_face0>=0);
  321. // b3Assert(edptr->m_face1<0);
  322. edptr->m_face1 = i;
  323. }
  324. else
  325. {
  326. b3InternalEdge ed;
  327. ed.m_face0 = i;
  328. edges.insert(vp, ed);
  329. }
  330. }
  331. }
  332. #ifdef USE_CONNECTED_FACES
  333. for (int i = 0; i < m_faces.size(); i++)
  334. {
  335. int numVertices = m_faces[i].m_indices.size();
  336. m_faces[i].m_connectedFaces.resize(numVertices);
  337. for (int j = 0; j < numVertices; j++)
  338. {
  339. int k = (j + 1) % numVertices;
  340. b3InternalVertexPair vp(m_faces[i].m_indices[j], m_faces[i].m_indices[k]);
  341. b3InternalEdge* edptr = edges.find(vp);
  342. b3Assert(edptr);
  343. b3Assert(edptr->m_face0 >= 0);
  344. b3Assert(edptr->m_face1 >= 0);
  345. int connectedFace = (edptr->m_face0 == i) ? edptr->m_face1 : edptr->m_face0;
  346. m_faces[i].m_connectedFaces[j] = connectedFace;
  347. }
  348. }
  349. #endif //USE_CONNECTED_FACES
  350. for (int i = 0; i < m_faces.size(); i++)
  351. {
  352. int numVertices = m_faces[i].m_indices.size();
  353. int NbTris = numVertices - 2;
  354. const b3Vector3& p0 = m_vertices[m_faces[i].m_indices[0]];
  355. for (int j = 1; j <= NbTris; j++)
  356. {
  357. int k = (j + 1) % numVertices;
  358. const b3Vector3& p1 = m_vertices[m_faces[i].m_indices[j]];
  359. const b3Vector3& p2 = m_vertices[m_faces[i].m_indices[k]];
  360. b3Scalar Area = ((p0 - p1).cross(p0 - p2)).length() * 0.5f;
  361. b3Vector3 Center = (p0 + p1 + p2) / 3.0f;
  362. m_localCenter += Area * Center;
  363. TotalArea += Area;
  364. }
  365. }
  366. m_localCenter /= TotalArea;
  367. #ifdef TEST_INTERNAL_OBJECTS
  368. if (1)
  369. {
  370. m_radius = FLT_MAX;
  371. for (int i = 0; i < m_faces.size(); i++)
  372. {
  373. const b3Vector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
  374. const b3Scalar dist = b3Fabs(m_localCenter.dot(Normal) + m_faces[i].m_plane[3]);
  375. if (dist < m_radius)
  376. m_radius = dist;
  377. }
  378. b3Scalar MinX = FLT_MAX;
  379. b3Scalar MinY = FLT_MAX;
  380. b3Scalar MinZ = FLT_MAX;
  381. b3Scalar MaxX = -FLT_MAX;
  382. b3Scalar MaxY = -FLT_MAX;
  383. b3Scalar MaxZ = -FLT_MAX;
  384. for (int i = 0; i < m_vertices.size(); i++)
  385. {
  386. const b3Vector3& pt = m_vertices[i];
  387. if (pt.getX() < MinX) MinX = pt.getX();
  388. if (pt.getX() > MaxX) MaxX = pt.getX();
  389. if (pt.getY() < MinY) MinY = pt.getY();
  390. if (pt.getY() > MaxY) MaxY = pt.getY();
  391. if (pt.getZ() < MinZ) MinZ = pt.getZ();
  392. if (pt.getZ() > MaxZ) MaxZ = pt.getZ();
  393. }
  394. mC.setValue(MaxX + MinX, MaxY + MinY, MaxZ + MinZ);
  395. mE.setValue(MaxX - MinX, MaxY - MinY, MaxZ - MinZ);
  396. // const b3Scalar r = m_radius / sqrtf(2.0f);
  397. const b3Scalar r = m_radius / sqrtf(3.0f);
  398. const int LargestExtent = mE.maxAxis();
  399. const b3Scalar Step = (mE[LargestExtent] * 0.5f - r) / 1024.0f;
  400. m_extents[0] = m_extents[1] = m_extents[2] = r;
  401. m_extents[LargestExtent] = mE[LargestExtent] * 0.5f;
  402. bool FoundBox = false;
  403. for (int j = 0; j < 1024; j++)
  404. {
  405. if (testContainment())
  406. {
  407. FoundBox = true;
  408. break;
  409. }
  410. m_extents[LargestExtent] -= Step;
  411. }
  412. if (!FoundBox)
  413. {
  414. m_extents[0] = m_extents[1] = m_extents[2] = r;
  415. }
  416. else
  417. {
  418. // Refine the box
  419. const b3Scalar Step = (m_radius - r) / 1024.0f;
  420. const int e0 = (1 << LargestExtent) & 3;
  421. const int e1 = (1 << e0) & 3;
  422. for (int j = 0; j < 1024; j++)
  423. {
  424. const b3Scalar Saved0 = m_extents[e0];
  425. const b3Scalar Saved1 = m_extents[e1];
  426. m_extents[e0] += Step;
  427. m_extents[e1] += Step;
  428. if (!testContainment())
  429. {
  430. m_extents[e0] = Saved0;
  431. m_extents[e1] = Saved1;
  432. break;
  433. }
  434. }
  435. }
  436. }
  437. #endif
  438. }