ClosestPointTest.cpp 5.4 KB

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