2
0

GJKTests.cpp 8.9 KB

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