2
0

GJKTests.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include <Jolt/Geometry/ConvexSupport.h>
  6. #include <Jolt/Geometry/GJKClosestPoint.h>
  7. #include <Jolt/Geometry/AABox.h>
  8. #include <Jolt/Geometry/Sphere.h>
  9. #include <Jolt/Geometry/RayTriangle.h>
  10. #include <Jolt/Geometry/RaySphere.h>
  11. #include <Jolt/Geometry/RayAABox.h>
  12. #include <Jolt/Geometry/RayCapsule.h>
  13. #include <Jolt/Geometry/RayCylinder.h>
  14. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  15. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  16. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  17. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  18. #include <random>
  19. TEST_SUITE("GJKTests")
  20. {
  21. TEST_CASE("TestGJKIntersectSphere")
  22. {
  23. GJKClosestPoint gjk;
  24. // Sphere 1 is centered around the origin
  25. Sphere s1(Vec3::sZero(), 1.0f);
  26. // Shere 2 is far away from s1
  27. Vec3 c2(10.0f, 10.0f, 10.0f);
  28. Sphere s2(c2, 1.0f);
  29. // Sphere 3 is exactly 2 away from s1
  30. float l = 2.0f / sqrt(3.0f);
  31. Vec3 c3(l, l, l);
  32. Sphere s3(c3, 1.0f);
  33. {
  34. // Test sphere s1 and s2, they should not collide
  35. Vec3 v = Vec3::sZero();
  36. CHECK_FALSE(gjk.Intersects(s1, s2, 1.0e-4f, v));
  37. }
  38. {
  39. // Test sphere s1 and s3, they should touch exactly
  40. Vec3 v = Vec3::sZero();
  41. CHECK(gjk.Intersects(s1, s3, 1.0e-4f, v));
  42. }
  43. {
  44. // Test sphere s1 and s2, they should not collide, verify their closest points
  45. Vec3 pa, pb, v = Vec3::sZero();
  46. float d = sqrt(gjk.GetClosestPoints(s1, s2, 1.0e-4f, FLT_MAX, v, pa, pb));
  47. CHECK_APPROX_EQUAL(c2.Length() - 2.0f, d, 1.0e-4f);
  48. CHECK_APPROX_EQUAL(c2.Normalized(), pa, 1.0e-4f);
  49. CHECK_APPROX_EQUAL(c2 - c2.Normalized(), pb, 1.0e-4f);
  50. }
  51. {
  52. // Test sphere s1 and s3, they should touch exactly, verify their closest points
  53. Vec3 pa, pb, v = Vec3::sZero();
  54. float d = sqrt(gjk.GetClosestPoints(s1, s3, 1.0e-4f, FLT_MAX, v, pa, pb));
  55. CHECK_APPROX_EQUAL(0.0f, d, 1.0e-4f);
  56. CHECK_APPROX_EQUAL(c2.Normalized(), pa, 1.0e-4f);
  57. CHECK_APPROX_EQUAL(c2.Normalized(), pb, 1.0e-4f);
  58. }
  59. }
  60. template <typename A, typename B>
  61. static void TestIntersect(
  62. A (*inCreateFuncA)(UnitTestRandom &),
  63. B (*inCreateFuncB)(UnitTestRandom &),
  64. bool (*inCompareFunc)(const A &inA, const B &inB, bool inIsIntersecting, float inTolerance))
  65. {
  66. UnitTestRandom random(12345);
  67. const int count = 10000;
  68. int hits = 0;
  69. GJKClosestPoint gjk;
  70. for (int i = 0; i < count; ++i)
  71. {
  72. A shape1 = inCreateFuncA(random);
  73. B shape2 = inCreateFuncB(random);
  74. // Use GJK to test for intersection
  75. Vec3 v = Vec3::sZero();
  76. const float cTolerance = 1.0e-4f;
  77. bool result_gjk = gjk.Intersects(shape1, shape2, cTolerance, v);
  78. // Compare with reference function and increase tolerance a bit to account for floating point imprecision
  79. CHECK(inCompareFunc(shape1, shape2, result_gjk, 2.0f * cTolerance));
  80. if (result_gjk)
  81. ++hits;
  82. }
  83. // Check that there were enough hits so that the test is representative
  84. float hit_rate = 100.0f * hits / count;
  85. CHECK(hit_rate > 30.0f);
  86. CHECK(hit_rate < 70.0f);
  87. }
  88. TEST_CASE("TestGJKSphereVsSphereIntersect")
  89. {
  90. auto sphere_creator = [](UnitTestRandom &inRandom) {
  91. uniform_real_distribution<float> pos(-2.0f, 2.0f);
  92. uniform_real_distribution<float> rad(0.5f, 2.0f);
  93. return Sphere(Vec3(pos(inRandom), pos(inRandom), pos(inRandom)), rad(inRandom));
  94. };
  95. TestIntersect<Sphere, Sphere>(
  96. sphere_creator,
  97. sphere_creator,
  98. [](const Sphere &inSphereA, const Sphere &inSphereB, bool inIsIntersecting, float inTolerance) {
  99. // Test without and with tolerance if the results are equal
  100. return inSphereA.Overlaps(inSphereB) == inIsIntersecting
  101. || Sphere(inSphereA.GetCenter(), inSphereA.GetRadius() + inTolerance).Overlaps(inSphereB) == inIsIntersecting;
  102. });
  103. }
  104. TEST_CASE("TestGJKSphereVsBoxIntersect")
  105. {
  106. auto sphere_creator = [](UnitTestRandom &inRandom) {
  107. uniform_real_distribution<float> pos(-2.0f, 2.0f);
  108. uniform_real_distribution<float> rad(0.5f, 2.0f);
  109. return Sphere(Vec3(pos(inRandom), pos(inRandom), pos(inRandom)), rad(inRandom));
  110. };
  111. auto box_creator = [](UnitTestRandom &inRandom) {
  112. uniform_real_distribution<float> pos(-2.0f, 2.0f);
  113. Vec3 p1 = Vec3(pos(inRandom), pos(inRandom), pos(inRandom));
  114. Vec3 p2 = Vec3(pos(inRandom), pos(inRandom), pos(inRandom));
  115. return AABox::sFromTwoPoints(p1, p2);
  116. };
  117. TestIntersect<Sphere, AABox>(
  118. sphere_creator,
  119. box_creator,
  120. [](const Sphere &inSphereA, const AABox &inBoxB, bool inIsIntersecting, float inTolerance) {
  121. // Test without and with tolerance if the results are equal
  122. return inSphereA.Overlaps(inBoxB) == inIsIntersecting
  123. || Sphere(inSphereA.GetCenter(), inSphereA.GetRadius() + inTolerance).Overlaps(inBoxB) == inIsIntersecting;
  124. });
  125. }
  126. template <typename A, typename Context>
  127. static void TestRay(const A &inA, const Context &inContext, float (*inCompareFunc)(const Context &inContext, Vec3Arg inRayOrigin, Vec3Arg inRayDirection))
  128. {
  129. UnitTestRandom random(12345);
  130. uniform_real_distribution<float> random_scale(-2.0f, 2.0f);
  131. const int count = 1000;
  132. for (int i = 0; i < count; ++i)
  133. {
  134. Vec3 from(random_scale(random), random_scale(random), random_scale(random));
  135. Vec3 to(random_scale(random), random_scale(random), random_scale(random));
  136. Vec3 direction = to - from;
  137. // Use GJK to cast a ray
  138. float fraction1 = 1.0f + FLT_EPSILON;
  139. GJKClosestPoint gjk;
  140. if (!gjk.CastRay(from, direction, 1.0e-4f, inA, fraction1))
  141. fraction1 = FLT_MAX;
  142. // Use the comparison function
  143. float fraction2 = inCompareFunc(inContext, from, direction);
  144. // The comparison functions work with infinite rays, so a fraction > 1 means a miss
  145. if (fraction2 > 1.0f)
  146. fraction2 = FLT_MAX;
  147. CHECK_APPROX_EQUAL(fraction1, fraction2, 0.01f);
  148. }
  149. }
  150. TEST_CASE("TestGJKRaySphere")
  151. {
  152. Sphere sphere(Vec3(0.1f, 0.2f, 0.3f), 1.1f);
  153. TestRay<Sphere, Sphere>(sphere, sphere, [](const Sphere &inSphere, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  154. return RaySphere(inRayOrigin, inRayDirection, inSphere.GetCenter(), inSphere.GetRadius());
  155. });
  156. }
  157. TEST_CASE("TestGJKRaySphereShape")
  158. {
  159. SphereShape sphere_shape(1.1f);
  160. ConvexShape::SupportBuffer buffer;
  161. const ConvexShape::Support *support = sphere_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  162. TestRay<ConvexShape::Support, SphereShape>(*support, sphere_shape, [](const SphereShape &inSphere, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  163. return RaySphere(inRayOrigin, inRayDirection, Vec3::sZero(), inSphere.GetRadius());
  164. });
  165. }
  166. TEST_CASE("TestGJKRayBox")
  167. {
  168. AABox box(Vec3(-0.9f, -1.0f, -1.1f), Vec3(0.8f, 0.9f, 1.0f));
  169. TestRay<AABox, AABox>(box, box, [](const AABox &inBox, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  170. float fraction = RayAABox(inRayOrigin, RayInvDirection(inRayDirection), inBox.mMin, inBox.mMax);
  171. return max(fraction, 0.0f);
  172. });
  173. }
  174. TEST_CASE("TestGJKRayBoxShape")
  175. {
  176. BoxShape box_shape(Vec3(0.9f, 1.0f, 1.1f), 0.0f);
  177. ConvexShape::SupportBuffer buffer;
  178. const ConvexShape::Support *support = box_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  179. TestRay<ConvexShape::Support, BoxShape>(*support, box_shape, [](const BoxShape &inBox, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  180. float fraction = RayAABox(inRayOrigin, RayInvDirection(inRayDirection), -inBox.GetHalfExtent(), inBox.GetHalfExtent());
  181. return max(fraction, 0.0f);
  182. });
  183. }
  184. TEST_CASE("TestGJKRayCapsuleShape")
  185. {
  186. CapsuleShape capsule_shape(1.1f, 0.6f);
  187. ConvexShape::SupportBuffer buffer;
  188. const ConvexShape::Support *support = capsule_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  189. TestRay<ConvexShape::Support, CapsuleShape>(*support, capsule_shape, [](const CapsuleShape &inCapsule, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  190. return RayCapsule(inRayOrigin, inRayDirection, inCapsule.GetHalfHeightOfCylinder(), inCapsule.GetRadius());
  191. });
  192. }
  193. TEST_CASE("TestGJKRayCylinderShape")
  194. {
  195. CylinderShape cylinder_shape(1.5f, 0.6f, 0.0f);
  196. ConvexShape::SupportBuffer buffer;
  197. const ConvexShape::Support *support = cylinder_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  198. TestRay<ConvexShape::Support, CylinderShape>(*support, cylinder_shape, [](const CylinderShape &inCylinder, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  199. return RayCylinder(inRayOrigin, inRayDirection, inCylinder.GetHalfHeight(), inCylinder.GetRadius());
  200. });
  201. }
  202. TEST_CASE("TestGJKRayTriangle")
  203. {
  204. TriangleConvexSupport triangle(Vec3(0.1f, 0.9f, 0.3f), Vec3(-0.9f, -0.5f, 0.2f), Vec3(0.7f, -0.3f, -0.1f));
  205. TestRay<TriangleConvexSupport, TriangleConvexSupport>(triangle, triangle, [](const TriangleConvexSupport &inTriangle, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  206. return RayTriangle(inRayOrigin, inRayDirection, inTriangle.mV1, inTriangle.mV2, inTriangle.mV3);
  207. });
  208. }
  209. }