ClosestPointTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/ConvexCollision/ClosestPointTest.h>
  6. #include <Jolt/Geometry/ClosestPoint.h>
  7. #include <Renderer/DebugRendererImp.h>
  8. #include <Utils/DebugRendererSP.h>
  9. JPH_IMPLEMENT_RTTI_VIRTUAL(ClosestPointTest)
  10. {
  11. JPH_ADD_BASE_CLASS(ClosestPointTest, Test)
  12. }
  13. void ClosestPointTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  14. {
  15. Vec3 pos(inParams.mCameraState.mPos);
  16. {
  17. // Normal tetrahedron
  18. Vec3 a(2, 0, 0);
  19. Vec3 b(1, 0, 1);
  20. Vec3 c(2, 0, 1);
  21. Vec3 d(1, 1, 0);
  22. TestTetra(pos, a, b, c, d);
  23. }
  24. {
  25. // Inside out tetrahedron
  26. Vec3 a(2, -2, 0);
  27. Vec3 b(1, -2, 1);
  28. Vec3 c(2, -2, 1);
  29. Vec3 d(1, -3, 0);
  30. TestTetra(pos, a, b, c, d);
  31. }
  32. {
  33. // Degenerate tetrahedron
  34. Vec3 a(2, 3, 0);
  35. Vec3 b = a;
  36. Vec3 c(2, 3, 1);
  37. Vec3 d(1, 4, 0);
  38. TestTetra(pos, a, b, c, d);
  39. }
  40. {
  41. // Degenerate tetrahedron
  42. Vec3 a(2, 6, 0);
  43. Vec3 b(1, 6, 1);
  44. Vec3 c = a;
  45. Vec3 d(1, 7, 0);
  46. TestTetra(pos, a, b, c, d);
  47. }
  48. {
  49. // Degenerate tetrahedron
  50. Vec3 a(2, 9, 0);
  51. Vec3 b(1, 9, 1);
  52. Vec3 c(2, 9, 1);
  53. Vec3 d = a;
  54. TestTetra(pos, a, b, c, d);
  55. }
  56. {
  57. // Degenerate tetrahedron
  58. Vec3 a(2, 12, 0);
  59. Vec3 b(1, 12, 1);
  60. Vec3 c = b;
  61. Vec3 d(1, 13, 0);
  62. TestTetra(pos, a, b, c, d);
  63. }
  64. {
  65. // Degenerate tetrahedron
  66. Vec3 a(2, 15, 0);
  67. Vec3 b(1, 15, 1);
  68. Vec3 c(2, 15, 1);
  69. Vec3 d = b;
  70. TestTetra(pos, a, b, c, d);
  71. }
  72. {
  73. // Degenerate tetrahedron
  74. Vec3 a(2, 18, 0);
  75. Vec3 b(1, 18, 1);
  76. Vec3 c(2, 18, 1);
  77. Vec3 d = c;
  78. TestTetra(pos, a, b, c, d);
  79. }
  80. {
  81. // Normal tri
  82. Vec3 a(5, 0, 0);
  83. Vec3 b(4, 0, 1);
  84. Vec3 c(5, 0, 1);
  85. TestTri(pos, a, b, c);
  86. }
  87. {
  88. // Degenerate tri
  89. Vec3 a(5, 3, 0);
  90. Vec3 b = a;
  91. Vec3 c(5, 3, 1);
  92. TestTri(pos, a, b, c);
  93. }
  94. {
  95. // Degenerate tri
  96. Vec3 a(5, 6, 0);
  97. Vec3 b(4, 6, 1);
  98. Vec3 c = a;
  99. TestTri(pos, a, b, c);
  100. }
  101. {
  102. // Degenerate tri
  103. Vec3 a(5, 9, 0);
  104. Vec3 b(4, 9, 1);
  105. Vec3 c = b;
  106. TestTri(pos, a, b, c);
  107. }
  108. {
  109. // Normal line
  110. Vec3 a(10, 0, 0);
  111. Vec3 b(9, 0, 1);
  112. TestLine(pos, a, b);
  113. }
  114. {
  115. // Degenerate line
  116. Vec3 a(10, 3, 0);
  117. Vec3 b = a;
  118. TestLine(pos, a, b);
  119. }
  120. }
  121. void ClosestPointTest::TestLine(Vec3Arg inPosition, Vec3Arg inA, Vec3Arg inB)
  122. {
  123. Vec3 a = inA - inPosition;
  124. Vec3 b = inB - inPosition;
  125. uint32 set;
  126. Vec3 closest = ClosestPoint::GetClosestPointOnLine(a, b, set) + inPosition;
  127. DrawLineSP(mDebugRenderer, inA, inB, Color::sWhite);
  128. DrawMarkerSP(mDebugRenderer, closest, Color::sRed, 0.1f);
  129. if (set & 0b0001)
  130. DrawMarkerSP(mDebugRenderer, inA, Color::sYellow, 0.5f);
  131. if (set & 0b0010)
  132. DrawMarkerSP(mDebugRenderer, inB, Color::sYellow, 0.5f);
  133. Vec3 a2 = inA - closest;
  134. Vec3 b2 = inB - closest;
  135. float u, v;
  136. ClosestPoint::GetBaryCentricCoordinates(a2, b2, u, v);
  137. DrawWireSphereSP(mDebugRenderer, u * inA + v * inB, 0.05f, Color::sGreen);
  138. DrawText3DSP(mDebugRenderer, inA, "a");
  139. DrawText3DSP(mDebugRenderer, inB, "b");
  140. }
  141. void ClosestPointTest::TestTri(Vec3Arg inPosition, Vec3Arg inA, Vec3Arg inB, Vec3Arg inC)
  142. {
  143. Vec3 a = inA - inPosition;
  144. Vec3 b = inB - inPosition;
  145. Vec3 c = inC - inPosition;
  146. uint32 set;
  147. Vec3 closest = ClosestPoint::GetClosestPointOnTriangle(a, b, c, set) + inPosition;
  148. DrawLineSP(mDebugRenderer, inA, inB, Color::sWhite);
  149. DrawLineSP(mDebugRenderer, inA, inC, Color::sWhite);
  150. DrawLineSP(mDebugRenderer, inB, inC, Color::sWhite);
  151. DrawTriangleSP(mDebugRenderer, inA, inB, inC, Color::sGrey);
  152. DrawMarkerSP(mDebugRenderer, closest, Color::sRed, 0.1f);
  153. if (set & 0b0001)
  154. DrawMarkerSP(mDebugRenderer, inA, Color::sYellow, 0.5f);
  155. if (set & 0b0010)
  156. DrawMarkerSP(mDebugRenderer, inB, Color::sYellow, 0.5f);
  157. if (set & 0b0100)
  158. DrawMarkerSP(mDebugRenderer, inC, Color::sYellow, 0.5f);
  159. Vec3 a2 = inA - closest;
  160. Vec3 b2 = inB - closest;
  161. Vec3 c2 = inC - closest;
  162. float u, v, w;
  163. ClosestPoint::GetBaryCentricCoordinates(a2, b2, c2, u, v, w);
  164. DrawWireSphereSP(mDebugRenderer, u * inA + v * inB + w * inC, 0.05f, Color::sGreen);
  165. DrawText3DSP(mDebugRenderer, inA, "a");
  166. DrawText3DSP(mDebugRenderer, inB, "b");
  167. DrawText3DSP(mDebugRenderer, inC, "c");
  168. }
  169. void ClosestPointTest::TestTetra(Vec3Arg inPosition, Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
  170. {
  171. Vec3 a = inA - inPosition;
  172. Vec3 b = inB - inPosition;
  173. Vec3 c = inC - inPosition;
  174. Vec3 d = inD - inPosition;
  175. uint32 set;
  176. Vec3 closest = ClosestPoint::GetClosestPointOnTetrahedron(a, b, c, d, set) + inPosition;
  177. DrawLineSP(mDebugRenderer, inA, inB, Color::sWhite);
  178. DrawLineSP(mDebugRenderer, inA, inC, Color::sWhite);
  179. DrawLineSP(mDebugRenderer, inA, inD, Color::sWhite);
  180. DrawLineSP(mDebugRenderer, inB, inC, Color::sWhite);
  181. DrawLineSP(mDebugRenderer, inB, inD, Color::sWhite);
  182. DrawLineSP(mDebugRenderer, inC, inD, Color::sWhite);
  183. DrawTriangleSP(mDebugRenderer, inA, inC, inB, Color::sGrey);
  184. DrawTriangleSP(mDebugRenderer, inA, inD, inC, Color::sGrey);
  185. DrawTriangleSP(mDebugRenderer, inA, inB, inD, Color::sGrey);
  186. DrawTriangleSP(mDebugRenderer, inB, inC, inD, Color::sGrey);
  187. DrawMarkerSP(mDebugRenderer, closest, Color::sRed, 0.1f);
  188. if (set & 0b0001)
  189. DrawMarkerSP(mDebugRenderer, inA, Color::sYellow, 0.5f);
  190. if (set & 0b0010)
  191. DrawMarkerSP(mDebugRenderer, inB, Color::sYellow, 0.5f);
  192. if (set & 0b0100)
  193. DrawMarkerSP(mDebugRenderer, inC, Color::sYellow, 0.5f);
  194. if (set & 0b1000)
  195. DrawMarkerSP(mDebugRenderer, inD, Color::sYellow, 0.5f);
  196. DrawText3DSP(mDebugRenderer, inA, "a");
  197. DrawText3DSP(mDebugRenderer, inB, "b");
  198. DrawText3DSP(mDebugRenderer, inC, "c");
  199. DrawText3DSP(mDebugRenderer, inD, "d");
  200. }