EPAConvexHullBuilder.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. // Define to validate the integrity of the hull structure
  5. //#define JPH_EPA_CONVEX_BUILDER_VALIDATE
  6. // Define to draw the building of the hull for debugging purposes
  7. //#define JPH_EPA_CONVEX_BUILDER_DRAW
  8. #include <Jolt/Core/NonCopyable.h>
  9. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  10. #include <Jolt/Renderer/DebugRenderer.h>
  11. #include <Jolt/Core/StringTools.h>
  12. #endif
  13. JPH_NAMESPACE_BEGIN
  14. /// A convex hull builder specifically made for the EPA penetration depth calculation. It trades accuracy for speed and will simply abort of the hull forms defects due to numerical precision problems.
  15. class EPAConvexHullBuilder : public NonCopyable
  16. {
  17. private:
  18. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  19. /// Factor to scale convex hull when debug drawing the construction process
  20. static constexpr float cDrawScale = 10.0f;
  21. #endif
  22. public:
  23. // Due to the Euler characteristic (https://en.wikipedia.org/wiki/Euler_characteristic) we know that Vertices - Edges + Faces = 2
  24. // In our case we only have triangles and they are always fully connected, so each edge is shared exactly between 2 faces: Edges = Faces * 3 / 2
  25. // Substituting: Vertices = Faces / 2 + 2 which is approximately Faces / 2.
  26. static constexpr int cMaxTriangles = 256; ///< Max triangles in hull
  27. static constexpr int cMaxPoints = cMaxTriangles / 2; ///< Max number of points in hull
  28. // Constants
  29. static constexpr int cMaxEdgeLength = 128; ///< Max number of edges in FindEdge
  30. static constexpr float cMinTriangleArea = 1.0e-10f; ///< Minimum area of a triangle before, if smaller than this it will not be added to the priority queue
  31. static constexpr float cBarycentricEpsilon = 1.0e-3f; ///< Epsilon value used to determine if a point is in the interior of a triangle
  32. // Forward declare
  33. class Triangle;
  34. /// Class that holds the information of an edge
  35. class Edge
  36. {
  37. public:
  38. /// Information about neighbouring triangle
  39. Triangle * mNeighbourTriangle; ///< Triangle that neighbours this triangle
  40. int mNeighbourEdge; ///< Index in mEdge that specifies edge that this Edge is connected to
  41. int mStartIdx; ///< Vertex index in mPositions that indicates the start vertex of this edge
  42. };
  43. using Edges = StaticArray<Edge, cMaxEdgeLength>;
  44. using NewTriangles = StaticArray<Triangle *, cMaxEdgeLength>;
  45. /// Class that holds the information of one triangle
  46. class Triangle : public NonCopyable
  47. {
  48. public:
  49. /// Constructor
  50. inline Triangle(int inIdx0, int inIdx1, int inIdx2, const Vec3 *inPositions);
  51. /// Check if triangle is facing inPosition
  52. inline bool IsFacing(Vec3Arg inPosition) const
  53. {
  54. JPH_ASSERT(!mRemoved);
  55. return mNormal.Dot(inPosition - mCentroid) > 0.0f;
  56. }
  57. /// Check if triangle is facing the origin
  58. inline bool IsFacingOrigin() const
  59. {
  60. JPH_ASSERT(!mRemoved);
  61. return mNormal.Dot(mCentroid) < 0.0f;
  62. }
  63. /// Get the next edge of edge inIndex
  64. inline const Edge & GetNextEdge(int inIndex) const
  65. {
  66. return mEdge[(inIndex + 1) % 3];
  67. }
  68. Edge mEdge[3]; ///< 3 edges of this triangle
  69. Vec3 mNormal; ///< Normal of this triangle, length is 2 times area of triangle
  70. Vec3 mCentroid; ///< Center of the triangle
  71. float mClosestLenSq = FLT_MAX; ///< Closest distance^2 from origin to triangle
  72. float mLambda[2]; ///< Barycentric coordinates of closest point to origin on triangle
  73. bool mLambdaRelativeTo0; ///< How to calculate the closest point, true: y0 + l0 * (y1 - y0) + l1 * (y2 - y0), false: y1 + l0 * (y0 - y1) + l1 * (y2 - y1)
  74. bool mClosestPointInterior = false; ///< Flag that indicates that the closest point from this triangle to the origin is an interior point
  75. bool mRemoved = false; ///< Flag that indicates that triangle has been removed
  76. bool mInQueue = false; ///< Flag that indicates that this triangle was placed in the sorted heap (stays true after it is popped because the triangle is freed by the main EPA algorithm loop)
  77. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  78. int mIteration; ///< Iteration that this triangle was created
  79. #endif
  80. };
  81. /// Factory that creates triangles in a fixed size buffer
  82. class TriangleFactory : public NonCopyable
  83. {
  84. private:
  85. /// Struct that stores both a triangle or a next pointer in case the triangle is unused
  86. union alignas(Triangle) Block
  87. {
  88. uint8 mTriangle[sizeof(Triangle)];
  89. Block * mNextFree;
  90. };
  91. /// Storage for triangle data
  92. Block mTriangles[cMaxTriangles]; ///< Storage for triangles
  93. Block * mNextFree = nullptr; ///< List of free triangles
  94. int mHighWatermark = 0; ///< High water mark for used triangles (if mNextFree == nullptr we can take one from here)
  95. public:
  96. /// Return all triangles to the free pool
  97. void Clear()
  98. {
  99. mNextFree = nullptr;
  100. mHighWatermark = 0;
  101. }
  102. /// Allocate a new triangle with 3 indexes
  103. Triangle * CreateTriangle(int inIdx0, int inIdx1, int inIdx2, const Vec3 *inPositions)
  104. {
  105. Triangle *t;
  106. if (mNextFree != nullptr)
  107. {
  108. // Entry available from the free list
  109. t = reinterpret_cast<Triangle *>(&mNextFree->mTriangle);
  110. mNextFree = mNextFree->mNextFree;
  111. }
  112. else
  113. {
  114. // Allocate from never used before triangle store
  115. if (mHighWatermark >= cMaxTriangles)
  116. return nullptr; // Buffer full
  117. t = reinterpret_cast<Triangle *>(&mTriangles[mHighWatermark].mTriangle);
  118. ++mHighWatermark;
  119. }
  120. // Call constructor
  121. new (t) Triangle(inIdx0, inIdx1, inIdx2, inPositions);
  122. return t;
  123. }
  124. /// Free a triangle
  125. void FreeTriangle(Triangle *inT)
  126. {
  127. // Destruct triangle
  128. inT->~Triangle();
  129. #ifdef _DEBUG
  130. memset(inT, 0xcd, sizeof(Triangle));
  131. #endif
  132. // Add triangle to the free list
  133. Block *tu = reinterpret_cast<Block *>(inT);
  134. tu->mNextFree = mNextFree;
  135. mNextFree = tu;
  136. }
  137. };
  138. // Typedefs
  139. using PointsBase = StaticArray<Vec3, cMaxPoints>;
  140. using Triangles = StaticArray<Triangle *, cMaxTriangles>;
  141. /// Specialized points list that allows direct access to the size
  142. class Points : public PointsBase
  143. {
  144. public:
  145. size_type & GetSizeRef()
  146. {
  147. return mSize;
  148. }
  149. };
  150. /// Specialized triangles list that keeps them sorted on closest distance to origin
  151. class TriangleQueue : public Triangles
  152. {
  153. public:
  154. /// Function to sort triangles on closest distance to origin
  155. static bool sTriangleSorter(const Triangle *inT1, const Triangle *inT2)
  156. {
  157. return inT1->mClosestLenSq > inT2->mClosestLenSq;
  158. }
  159. /// Add triangle to the list
  160. void push_back(Triangle *inT)
  161. {
  162. // Add to base
  163. Triangles::push_back(inT);
  164. // Mark in queue
  165. inT->mInQueue = true;
  166. // Resort heap
  167. std::push_heap(begin(), end(), sTriangleSorter);
  168. }
  169. /// Peek the next closest triangle without removing it
  170. Triangle * PeekClosest()
  171. {
  172. return front();
  173. }
  174. /// Get next closest triangle
  175. Triangle * PopClosest()
  176. {
  177. // Move largest to end
  178. std::pop_heap(begin(), end(), sTriangleSorter);
  179. // Remove last triangle
  180. Triangle *t = back();
  181. pop_back();
  182. return t;
  183. }
  184. };
  185. /// Constructor
  186. explicit EPAConvexHullBuilder(const Points &inPositions) :
  187. mPositions(inPositions)
  188. {
  189. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  190. mIteration = 0;
  191. mOffset = Vec3::sZero();
  192. #endif
  193. }
  194. /// Initialize the hull with 3 points
  195. void Initialize(int inIdx1, int inIdx2, int inIdx3)
  196. {
  197. // Release triangles
  198. mFactory.Clear();
  199. // Create triangles (back to back)
  200. Triangle *t1 = CreateTriangle(inIdx1, inIdx2, inIdx3);
  201. Triangle *t2 = CreateTriangle(inIdx1, inIdx3, inIdx2);
  202. // Link triangles edges
  203. sLinkTriangle(t1, 0, t2, 2);
  204. sLinkTriangle(t1, 1, t2, 1);
  205. sLinkTriangle(t1, 2, t2, 0);
  206. // Always add both triangles to the priority queue
  207. mTriangleQueue.push_back(t1);
  208. mTriangleQueue.push_back(t2);
  209. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  210. // Draw current state
  211. DrawState();
  212. // Increment iteration counter
  213. ++mIteration;
  214. #endif
  215. }
  216. /// Check if there's another triangle to process from the queue
  217. bool HasNextTriangle() const
  218. {
  219. return !mTriangleQueue.empty();
  220. }
  221. /// Access to the next closest triangle to the origin (won't remove it from the queue).
  222. Triangle * PeekClosestTriangleInQueue()
  223. {
  224. return mTriangleQueue.PeekClosest();
  225. }
  226. /// Access to the next closest triangle to the origin and remove it from the queue.
  227. Triangle * PopClosestTriangleFromQueue()
  228. {
  229. return mTriangleQueue.PopClosest();
  230. }
  231. /// Find the triangle on which inPosition is the furthest to the front
  232. /// Note this function works as long as all points added have been added with AddPoint(..., FLT_MAX).
  233. Triangle * FindFacingTriangle(Vec3Arg inPosition, float &outBestDistSq)
  234. {
  235. Triangle *best = nullptr;
  236. float best_dist_sq = 0.0f;
  237. for (Triangle *t : mTriangleQueue)
  238. if (!t->mRemoved)
  239. {
  240. float dot = t->mNormal.Dot(inPosition - t->mCentroid);
  241. if (dot > 0.0f)
  242. {
  243. float dist_sq = dot * dot / t->mNormal.LengthSq();
  244. if (dist_sq > best_dist_sq)
  245. {
  246. best = t;
  247. best_dist_sq = dist_sq;
  248. }
  249. }
  250. }
  251. outBestDistSq = best_dist_sq;
  252. return best;
  253. }
  254. /// Add a new point to the convex hull
  255. bool AddPoint(Triangle *inFacingTriangle, int inIdx, float inClosestDistSq, NewTriangles &outTriangles)
  256. {
  257. // Get position
  258. Vec3 pos = mPositions[inIdx];
  259. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  260. // Draw new support point
  261. DrawMarker(pos, Color::sYellow, 1.0f);
  262. #endif
  263. #ifdef JPH_EPA_CONVEX_BUILDER_VALIDATE
  264. // Check if structure is intact
  265. ValidateTriangles();
  266. #endif
  267. // Find edge of convex hull of triangles that are not facing the new vertex w
  268. Edges edges;
  269. if (!FindEdge(inFacingTriangle, pos, edges))
  270. return false;
  271. // Create new triangles
  272. int num_edges = edges.size();
  273. for (int i = 0; i < num_edges; ++i)
  274. {
  275. // Create new triangle
  276. Triangle *nt = CreateTriangle(edges[i].mStartIdx, edges[(i + 1) % num_edges].mStartIdx, inIdx);
  277. if (nt == nullptr)
  278. return false;
  279. outTriangles.push_back(nt);
  280. // Check if we need to put this triangle in the priority queue
  281. if ((nt->mClosestPointInterior && nt->mClosestLenSq < inClosestDistSq) // For the main algorithm
  282. || nt->mClosestLenSq < 0.0f) // For when the origin is not inside the hull yet
  283. mTriangleQueue.push_back(nt);
  284. }
  285. // Link edges
  286. for (int i = 0; i < num_edges; ++i)
  287. {
  288. sLinkTriangle(outTriangles[i], 0, edges[i].mNeighbourTriangle, edges[i].mNeighbourEdge);
  289. sLinkTriangle(outTriangles[i], 1, outTriangles[(i + 1) % num_edges], 2);
  290. }
  291. #ifdef JPH_EPA_CONVEX_BUILDER_VALIDATE
  292. // Check if structure is intact
  293. ValidateTriangles();
  294. #endif
  295. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  296. // Draw state of the hull
  297. DrawState();
  298. // Increment iteration counter
  299. ++mIteration;
  300. #endif
  301. return true;
  302. }
  303. /// Free a triangle
  304. void FreeTriangle(Triangle *inT)
  305. {
  306. #ifdef JPH_ENABLE_ASSERTS
  307. // Make sure that this triangle is not connected
  308. JPH_ASSERT(inT->mRemoved);
  309. for (const Edge &e : inT->mEdge)
  310. JPH_ASSERT(e.mNeighbourTriangle == nullptr);
  311. #endif
  312. #if defined(JPH_EPA_CONVEX_BUILDER_VALIDATE) || defined(JPH_EPA_CONVEX_BUILDER_DRAW)
  313. // Remove from list of all triangles
  314. Triangles::iterator i = std::find(mTriangles.begin(), mTriangles.end(), inT);
  315. JPH_ASSERT(i != mTriangles.end());
  316. mTriangles.erase(i);
  317. #endif
  318. mFactory.FreeTriangle(inT);
  319. }
  320. private:
  321. /// Create a new triangle
  322. Triangle * CreateTriangle(int inIdx1, int inIdx2, int inIdx3)
  323. {
  324. // Call provider to create triangle
  325. Triangle *t = mFactory.CreateTriangle(inIdx1, inIdx2, inIdx3, mPositions.data());
  326. if (t == nullptr)
  327. return nullptr;
  328. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  329. // Remember iteration counter
  330. t->mIteration = mIteration;
  331. #endif
  332. #if defined(JPH_EPA_CONVEX_BUILDER_VALIDATE) || defined(JPH_EPA_CONVEX_BUILDER_DRAW)
  333. // Add to list of triangles for debugging purposes
  334. mTriangles.push_back(t);
  335. #endif
  336. return t;
  337. }
  338. /// Link triangle edge to other triangle edge
  339. static void sLinkTriangle(Triangle *inT1, int inEdge1, Triangle *inT2, int inEdge2)
  340. {
  341. JPH_ASSERT(inEdge1 >= 0 && inEdge1 < 3);
  342. JPH_ASSERT(inEdge2 >= 0 && inEdge2 < 3);
  343. Edge &e1 = inT1->mEdge[inEdge1];
  344. Edge &e2 = inT2->mEdge[inEdge2];
  345. // Check not connected yet
  346. JPH_ASSERT(e1.mNeighbourTriangle == nullptr);
  347. JPH_ASSERT(e2.mNeighbourTriangle == nullptr);
  348. // Check vertices match
  349. JPH_ASSERT(e1.mStartIdx == inT2->GetNextEdge(inEdge2).mStartIdx);
  350. JPH_ASSERT(e2.mStartIdx == inT1->GetNextEdge(inEdge1).mStartIdx);
  351. // Link up
  352. e1.mNeighbourTriangle = inT2;
  353. e1.mNeighbourEdge = inEdge2;
  354. e2.mNeighbourTriangle = inT1;
  355. e2.mNeighbourEdge = inEdge1;
  356. }
  357. /// Unlink this triangle
  358. void UnlinkTriangle(Triangle *inT)
  359. {
  360. // Unlink from neighbours
  361. for (int i = 0; i < 3; ++i)
  362. {
  363. Edge &edge = inT->mEdge[i];
  364. if (edge.mNeighbourTriangle != nullptr)
  365. {
  366. Edge &neighbour_edge = edge.mNeighbourTriangle->mEdge[edge.mNeighbourEdge];
  367. // Validate that neighbour points to us
  368. JPH_ASSERT(neighbour_edge.mNeighbourTriangle == inT);
  369. JPH_ASSERT(neighbour_edge.mNeighbourEdge == i);
  370. // Unlink
  371. neighbour_edge.mNeighbourTriangle = nullptr;
  372. edge.mNeighbourTriangle = nullptr;
  373. }
  374. }
  375. // If this triangle is not in the priority queue, we can delete it now
  376. if (!inT->mInQueue)
  377. FreeTriangle(inT);
  378. }
  379. /// Given one triangle that faces inVertex, find the edges of the triangles that are not facing inVertex.
  380. /// Will flag all those triangles for removal.
  381. bool FindEdge(Triangle *inFacingTriangle, Vec3Arg inVertex, Edges &outEdges)
  382. {
  383. // Assert that we were given an empty array
  384. JPH_ASSERT(outEdges.empty());
  385. // Should start with a facing triangle
  386. JPH_ASSERT(inFacingTriangle->IsFacing(inVertex));
  387. // Flag as removed
  388. inFacingTriangle->mRemoved = true;
  389. // Instead of recursing, we build our own stack with the information we need
  390. struct StackEntry
  391. {
  392. Triangle * mTriangle;
  393. int mEdge;
  394. int mIter;
  395. };
  396. StackEntry stack[cMaxEdgeLength];
  397. int cur_stack_pos = 0;
  398. // Start with the triangle / edge provided
  399. stack[0].mTriangle = inFacingTriangle;
  400. stack[0].mEdge = 0;
  401. stack[0].mIter = -1; // Start with edge 0 (is incremented below before use)
  402. // Next index that we expect to find, if we don't then there are 'islands'
  403. int next_expected_start_idx = -1;
  404. for (;;)
  405. {
  406. StackEntry &cur_entry = stack[cur_stack_pos];
  407. // Next iteration
  408. if (++cur_entry.mIter >= 3)
  409. {
  410. // This triangle needs to be removed, unlink it now
  411. UnlinkTriangle(cur_entry.mTriangle);
  412. // Pop from stack
  413. if (--cur_stack_pos < 0)
  414. break;
  415. }
  416. else
  417. {
  418. // Visit neighbour
  419. Edge &e = cur_entry.mTriangle->mEdge[(cur_entry.mEdge + cur_entry.mIter) % 3];
  420. Triangle *n = e.mNeighbourTriangle;
  421. if (n != nullptr && !n->mRemoved)
  422. {
  423. // Check if vertex is on the front side of this triangle
  424. if (n->IsFacing(inVertex))
  425. {
  426. // Vertex on front, this triangle needs to be removed
  427. n->mRemoved = true;
  428. // Add element to the stack of elements to visit
  429. cur_stack_pos++;
  430. JPH_ASSERT(cur_stack_pos < cMaxEdgeLength);
  431. StackEntry &new_entry = stack[cur_stack_pos];
  432. new_entry.mTriangle = n;
  433. new_entry.mEdge = e.mNeighbourEdge;
  434. new_entry.mIter = 0; // Is incremented before use, we don't need to test this edge again since we came from it
  435. }
  436. else
  437. {
  438. // Detect if edge doesn't connect to previous edge, if this happens we have found and 'island' which means
  439. // the newly added point is so close to the triangles of the hull that we classified some (nearly) coplanar
  440. // triangles as before and some behind the point. At this point we just abort adding the point because
  441. // we've reached numerical precision.
  442. // Note that we do not need to test if the first and last edge connect, since when there are islands
  443. // there should be at least 2 disconnects.
  444. if (e.mStartIdx != next_expected_start_idx && next_expected_start_idx != -1)
  445. return false;
  446. // Next expected index is the start index of our neighbour's edge
  447. next_expected_start_idx = n->mEdge[e.mNeighbourEdge].mStartIdx;
  448. // Vertex behind, keep edge
  449. outEdges.push_back(e);
  450. }
  451. }
  452. }
  453. }
  454. // Assert that we have a fully connected loop
  455. JPH_ASSERT(outEdges.empty() || outEdges[0].mStartIdx == next_expected_start_idx);
  456. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  457. // Draw edge of facing triangles
  458. for (int i = 0; i < (int)outEdges.size(); ++i)
  459. {
  460. Vec3 edge_start = cDrawScale * (mPositions[outEdges[i].mStartIdx] + mOffset);
  461. DebugRenderer::sInstance->DrawArrow(edge_start, cDrawScale * (mPositions[outEdges[(i + 1) % outEdges.size()].mStartIdx] + mOffset), Color::sYellow, 0.01f);
  462. DebugRenderer::sInstance->DrawText3D(edge_start, ConvertToString(outEdges[i].mStartIdx), Color::sWhite);
  463. }
  464. // Draw the state with the facing triangles removed
  465. DrawState();
  466. #endif
  467. // When we start with two triangles facing away from each other and adding a point that is on the plane,
  468. // sometimes we consider the point in front of both causing both triangles to be removed resulting in an empty edge list.
  469. // In this case we fail to add the point which will result in no collision reported (the shapes are contacting in 1 point so there's 0 penetration)
  470. return outEdges.size() >= 3;
  471. }
  472. #ifdef JPH_EPA_CONVEX_BUILDER_VALIDATE
  473. /// Check consistency of 1 triangle
  474. void ValidateTriangle(const Triangle *inT) const
  475. {
  476. if (inT->mRemoved)
  477. {
  478. // Valdiate that removed triangles are not connected to anything
  479. for (const Edge &my_edge : inT->mEdge)
  480. JPH_ASSERT(my_edge.mNeighbourTriangle == nullptr);
  481. }
  482. else
  483. {
  484. for (int i = 0; i < 3; ++i)
  485. {
  486. const Edge &my_edge = inT->mEdge[i];
  487. // Assert that we have a neighbour
  488. const Triangle *nb = my_edge.mNeighbourTriangle;
  489. JPH_ASSERT(nb != nullptr);
  490. if (nb != nullptr)
  491. {
  492. // Assert that our neighbours edge points to us
  493. const Edge &nb_edge = nb->mEdge[my_edge.mNeighbourEdge];
  494. JPH_ASSERT(nb_edge.mNeighbourTriangle == inT);
  495. JPH_ASSERT(nb_edge.mNeighbourEdge == i);
  496. // Assert that the next edge of the neighbour points to the same vertex as this edge's vertex
  497. const Edge &nb_next_edge = nb->GetNextEdge(my_edge.mNeighbourEdge);
  498. JPH_ASSERT(nb_next_edge.mStartIdx == my_edge.mStartIdx);
  499. // Assert that my next edge points to the same vertex as my neighbours vertex
  500. const Edge &my_next_edge = inT->GetNextEdge(i);
  501. JPH_ASSERT(my_next_edge.mStartIdx == nb_edge.mStartIdx);
  502. }
  503. }
  504. }
  505. }
  506. /// Check consistency of all triangles
  507. void ValidateTriangles() const
  508. {
  509. for (const Triangle *t : mTriangles)
  510. ValidateTriangle(t);
  511. }
  512. #endif
  513. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  514. public:
  515. /// Draw state of algorithm
  516. void DrawState()
  517. {
  518. // Draw origin
  519. DebugRenderer::sInstance->DrawCoordinateSystem(Mat44::sTranslation(cDrawScale * mOffset), 1.0f);
  520. // Draw triangles
  521. for (const Triangle *t : mTriangles)
  522. if (!t->mRemoved)
  523. {
  524. // Calculate the triangle vertices
  525. Vec3 p1 = cDrawScale * (mPositions[t->mEdge[0].mStartIdx] + mOffset);
  526. Vec3 p2 = cDrawScale * (mPositions[t->mEdge[1].mStartIdx] + mOffset);
  527. Vec3 p3 = cDrawScale * (mPositions[t->mEdge[2].mStartIdx] + mOffset);
  528. // Draw triangle
  529. DebugRenderer::sInstance->DrawTriangle(p1, p2, p3, Color::sGetDistinctColor(t->mIteration));
  530. DebugRenderer::sInstance->DrawWireTriangle(p1, p2, p3, Color::sGrey);
  531. // Draw normal
  532. Vec3 centroid = cDrawScale * (t->mCentroid + mOffset);
  533. float len = t->mNormal.Length();
  534. if (len > 0.0f)
  535. DebugRenderer::sInstance->DrawArrow(centroid, centroid + t->mNormal / len, Color::sDarkGreen, 0.01f);
  536. }
  537. // Determine max position
  538. float min_x = FLT_MAX;
  539. float max_x = -FLT_MAX;
  540. for (Vec3 p : mPositions)
  541. {
  542. min_x = min(min_x, p.GetX());
  543. max_x = max(max_x, p.GetX());
  544. }
  545. // Offset to the right
  546. mOffset += Vec3(max_x - min_x + 0.5f, 0.0f, 0.0f);
  547. }
  548. /// Draw a triangle for debugging purposes
  549. void DrawWireTriangle(const Triangle &inTriangle, ColorArg inColor)
  550. {
  551. Vec3 prev = cDrawScale * (mPositions[inTriangle.mEdge[2].mStartIdx] + mOffset);
  552. for (const Edge &edge : inTriangle.mEdge)
  553. {
  554. Vec3 cur = cDrawScale * (mPositions[edge.mStartIdx] + mOffset);
  555. DebugRenderer::sInstance->DrawArrow(prev, cur, inColor, 0.01f);
  556. prev = cur;
  557. }
  558. }
  559. /// Draw a marker for debugging purposes
  560. void DrawMarker(Vec3Arg inPosition, ColorArg inColor, float inSize)
  561. {
  562. DebugRenderer::sInstance->DrawMarker(cDrawScale * (mOffset + inPosition), inColor, inSize);
  563. }
  564. /// Draw an arrow for debugging purposes
  565. void DrawArrow(Vec3Arg inFrom, Vec3Arg inTo, ColorArg inColor, float inArrowSize)
  566. {
  567. DebugRenderer::sInstance->DrawArrow(cDrawScale * (mOffset + inFrom), cDrawScale * (mOffset + inTo), inColor, inArrowSize);
  568. }
  569. #endif
  570. private:
  571. TriangleFactory mFactory; ///< Factory to create new triangles and remove old ones
  572. const Points & mPositions; ///< List of positions (some of them are part of the hull)
  573. TriangleQueue mTriangleQueue; ///< List of triangles that are part of the hull that still need to be checked (if !mRemoved)
  574. #if defined(JPH_EPA_CONVEX_BUILDER_VALIDATE) || defined(JPH_EPA_CONVEX_BUILDER_DRAW)
  575. Triangles mTriangles; ///< The list of all triangles in this hull (for debug purposes)
  576. #endif
  577. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  578. int mIteration; ///< Number of iterations we've had so far (for debug purposes)
  579. Vec3 mOffset; ///< Offset to use for state drawing
  580. #endif
  581. };
  582. // The determinant that is calculated in the Triangle constructor is really sensitive
  583. // to numerical round off, disable the fmadd instructions to maintain precision.
  584. JPH_PRECISE_MATH_ON
  585. EPAConvexHullBuilder::Triangle::Triangle(int inIdx0, int inIdx1, int inIdx2, const Vec3 *inPositions)
  586. {
  587. // Fill in indexes
  588. JPH_ASSERT(inIdx0 != inIdx1 && inIdx0 != inIdx2 && inIdx1 != inIdx2);
  589. mEdge[0].mStartIdx = inIdx0;
  590. mEdge[1].mStartIdx = inIdx1;
  591. mEdge[2].mStartIdx = inIdx2;
  592. // Clear links
  593. mEdge[0].mNeighbourTriangle = nullptr;
  594. mEdge[1].mNeighbourTriangle = nullptr;
  595. mEdge[2].mNeighbourTriangle = nullptr;
  596. // Get vertex positions
  597. Vec3 y0 = inPositions[inIdx0];
  598. Vec3 y1 = inPositions[inIdx1];
  599. Vec3 y2 = inPositions[inIdx2];
  600. // Calculate centroid
  601. mCentroid = (y0 + y1 + y2) / 3.0f;
  602. // Calculate edges
  603. Vec3 y10 = y1 - y0;
  604. Vec3 y20 = y2 - y0;
  605. Vec3 y21 = y2 - y1;
  606. // The most accurate normal is calculated by using the two shortest edges
  607. // See: https://box2d.org/posts/2014/01/troublesome-triangle/
  608. // The difference in normals is most pronounced when one edge is much smaller than the others (in which case the other 2 must have roughly the same length).
  609. // Therefore we can suffice by just picking the shortest from 2 edges and use that with the 3rd edge to calculate the normal.
  610. // We first check which of the edges is shorter.
  611. float y20_dot_y20 = y20.Dot(y20);
  612. float y21_dot_y21 = y21.Dot(y21);
  613. if (y20_dot_y20 < y21_dot_y21)
  614. {
  615. // We select the edges y10 and y20
  616. mNormal = y10.Cross(y20);
  617. // Check if triangle is degenerate
  618. float normal_len_sq = mNormal.LengthSq();
  619. if (normal_len_sq > cMinTriangleArea)
  620. {
  621. // Determine distance between triangle and origin: distance = (centroid - origin) . normal / |normal|
  622. // Note that this way of calculating the closest point is much more accurate than first calculating barycentric coordinates and then calculating the closest
  623. // point based on those coordinates. Note that we preserve the sign of the distance to check on which side the origin is.
  624. float c_dot_n = mCentroid.Dot(mNormal);
  625. mClosestLenSq = abs(c_dot_n) * c_dot_n / normal_len_sq;
  626. // Calculate closest point to origin using barycentric coordinates:
  627. //
  628. // v = y0 + l0 * (y1 - y0) + l1 * (y2 - y0)
  629. // v . (y1 - y0) = 0
  630. // v . (y2 - y0) = 0
  631. //
  632. // Written in matrix form:
  633. //
  634. // | y10.y10 y20.y10 | | l0 | = | -y0.y10 |
  635. // | y10.y20 y20.y20 | | l1 | | -y0.y20 |
  636. //
  637. // (y10 = y1 - y0 etc.)
  638. //
  639. // Cramers rule to invert matrix:
  640. float y10_dot_y10 = y10.LengthSq();
  641. float y10_dot_y20 = y10.Dot(y20);
  642. float determinant = y10_dot_y10 * y20_dot_y20 - y10_dot_y20 * y10_dot_y20;
  643. if (determinant > 0.0f) // If determinant == 0 then the system is linearly dependent and the triangle is degenerate, since y10.10 * y20.y20 > y10.y20^2 it should also be > 0
  644. {
  645. float y0_dot_y10 = y0.Dot(y10);
  646. float y0_dot_y20 = y0.Dot(y20);
  647. float l0 = (y10_dot_y20 * y0_dot_y20 - y20_dot_y20 * y0_dot_y10) / determinant;
  648. float l1 = (y10_dot_y20 * y0_dot_y10 - y10_dot_y10 * y0_dot_y20) / determinant;
  649. mLambda[0] = l0;
  650. mLambda[1] = l1;
  651. mLambdaRelativeTo0 = true;
  652. // Check if closest point is interior to the triangle. For a convex hull which contains the origin each face must contain the origin, but because
  653. // our faces are triangles, we can have multiple coplanar triangles and only 1 will have the origin as an interior point. We want to use this triangle
  654. // to calculate the contact points because it gives the most accurate results, so we will only add these triangles to the priority queue.
  655. if (l0 > -cBarycentricEpsilon && l1 > -cBarycentricEpsilon && l0 + l1 < 1.0f + cBarycentricEpsilon)
  656. mClosestPointInterior = true;
  657. }
  658. }
  659. }
  660. else
  661. {
  662. // We select the edges y10 and y21
  663. mNormal = y10.Cross(y21);
  664. // Check if triangle is degenerate
  665. float normal_len_sq = mNormal.LengthSq();
  666. if (normal_len_sq > cMinTriangleArea)
  667. {
  668. // Again calculate distance between triangle and origin
  669. float c_dot_n = mCentroid.Dot(mNormal);
  670. mClosestLenSq = abs(c_dot_n) * c_dot_n / normal_len_sq;
  671. // Calculate closest point to origin using barycentric coordinates but this time using y1 as the reference vertex
  672. //
  673. // v = y1 + l0 * (y0 - y1) + l1 * (y2 - y1)
  674. // v . (y0 - y1) = 0
  675. // v . (y2 - y1) = 0
  676. //
  677. // Written in matrix form:
  678. //
  679. // | y10.y10 -y21.y10 | | l0 | = | y1.y10 |
  680. // | -y10.y21 y21.y21 | | l1 | | -y1.y21 |
  681. //
  682. // Cramers rule to invert matrix:
  683. float y10_dot_y10 = y10.LengthSq();
  684. float y10_dot_y21 = y10.Dot(y21);
  685. float determinant = y10_dot_y10 * y21_dot_y21 - y10_dot_y21 * y10_dot_y21;
  686. if (determinant > 0.0f)
  687. {
  688. float y1_dot_y10 = y1.Dot(y10);
  689. float y1_dot_y21 = y1.Dot(y21);
  690. float l0 = (y21_dot_y21 * y1_dot_y10 - y10_dot_y21 * y1_dot_y21) / determinant;
  691. float l1 = (y10_dot_y21 * y1_dot_y10 - y10_dot_y10 * y1_dot_y21) / determinant;
  692. mLambda[0] = l0;
  693. mLambda[1] = l1;
  694. mLambdaRelativeTo0 = false;
  695. // Again check if the closest point is inside the triangle
  696. if (l0 > -cBarycentricEpsilon && l1 > -cBarycentricEpsilon && l0 + l1 < 1.0f + cBarycentricEpsilon)
  697. mClosestPointInterior = true;
  698. }
  699. }
  700. }
  701. }
  702. JPH_PRECISE_MATH_OFF
  703. JPH_NAMESPACE_END