RandomRayTest.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/ConvexCollision/RandomRayTest.h>
  5. #include <Jolt/Geometry/Sphere.h>
  6. #include <Jolt/Geometry/AABox.h>
  7. #include <Jolt/Geometry/GJKClosestPoint.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/Geometry/ConvexSupport.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 <Renderer/DebugRendererImp.h>
  19. JPH_IMPLEMENT_RTTI_VIRTUAL(RandomRayTest)
  20. {
  21. JPH_ADD_BASE_CLASS(RandomRayTest, Test)
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Tests the CastRay function
  25. //-----------------------------------------------------------------------------
  26. template <typename A, typename Context>
  27. void RandomRayTest::TestRay(const char *inTestName, Vec3Arg inRenderOffset, const A &inA, const Context &inContext, float (*inCompareFunc)(const Context &inContext, Vec3Arg inRayOrigin, Vec3Arg inRayDirection))
  28. {
  29. default_random_engine random(12345);
  30. uniform_real_distribution<float> random_scale(-2.0f, 2.0f);
  31. #ifdef _DEBUG
  32. const int count = 1000;
  33. #else
  34. const int count = 10000;
  35. #endif
  36. int mismatches = 0;
  37. int nonzero_hits = 0;
  38. int zero_hits = 0;
  39. float total_error = 0;
  40. int total_error_count = 0;
  41. float min_error = FLT_MAX;
  42. float max_error = 0;
  43. GJKClosestPoint gjk;
  44. Trace("Starting: %s", inTestName);
  45. for (int i = 0; i < count; ++i)
  46. {
  47. Vec3 from(random_scale(random), random_scale(random), random_scale(random));
  48. Vec3 to(random_scale(random), random_scale(random), random_scale(random));
  49. Vec3 direction = to - from;
  50. // Use GJK to cast a ray
  51. float fraction1 = 1.0f + FLT_EPSILON;
  52. if (!gjk.CastRay(from, direction, 1.0e-4f, inA, fraction1))
  53. fraction1 = FLT_MAX;
  54. // Use the comparison function
  55. float fraction2 = inCompareFunc(inContext, from, direction);
  56. // The comparison functions work with infinite rays, so a fraction > 1 means a miss
  57. if (fraction2 > 1.0f)
  58. fraction2 = FLT_MAX;
  59. float error = abs(fraction1 - fraction2);
  60. if (error > 0.005f)
  61. {
  62. Trace("Mismatch iteration: %d (%f vs %f, diff: %f)", i, (double)fraction1, (double)fraction2, (double)abs(fraction2 - fraction1));
  63. ++mismatches;
  64. Color c;
  65. if (fraction2 == FLT_MAX)
  66. {
  67. c = Color::sRed;
  68. mDebugRenderer->DrawMarker(inRenderOffset + from + fraction1 * direction, Color::sRed, 0.1f);
  69. }
  70. else if (fraction1 == FLT_MAX)
  71. {
  72. c = Color::sBlue;
  73. mDebugRenderer->DrawMarker(inRenderOffset + from + fraction2 * direction, Color::sBlue, 0.1f);
  74. }
  75. else
  76. {
  77. total_error += abs(fraction2 - fraction1);
  78. total_error_count++;
  79. c = Color::sGreen;
  80. mDebugRenderer->DrawMarker(inRenderOffset + from + fraction1 * direction, Color::sCyan, 0.1f);
  81. mDebugRenderer->DrawMarker(inRenderOffset + from + fraction2 * direction, Color::sGreen, 0.1f);
  82. }
  83. mDebugRenderer->DrawArrow(inRenderOffset + from, inRenderOffset + to, c, 0.1f);
  84. }
  85. else if (fraction1 != FLT_MAX)
  86. {
  87. mDebugRenderer->DrawMarker(inRenderOffset + from + fraction1 * direction, Color::sYellow, 0.02f);
  88. }
  89. if (fraction1 != FLT_MAX && fraction2 != FLT_MAX)
  90. {
  91. total_error += error;
  92. total_error_count++;
  93. min_error = min(min_error, error);
  94. max_error = max(max_error, error);
  95. }
  96. if (fraction2 == 0.0f)
  97. ++zero_hits;
  98. else if (fraction2 > 0 && fraction2 <= 1.0f)
  99. ++nonzero_hits;
  100. }
  101. Trace("Report for: %s", inTestName);
  102. Trace("Mismatches: %d (%.1f%%)", mismatches, 100.0 * mismatches / count);
  103. Trace("Hits (fraction = 0): %d (%.1f%%)", zero_hits, 100.0 * zero_hits / count);
  104. Trace("Hits (fraction > 0 and fraction <= 1): %d (%.1f%%)", nonzero_hits, 100.0 * nonzero_hits / count);
  105. Trace("Fraction error: Avg %f, Min %f, Max %f", total_error_count > 0? double(total_error / total_error_count) : 0.0, (double)min_error, (double)max_error);
  106. }
  107. void RandomRayTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  108. {
  109. {
  110. Vec3 render_offset(0, 0, 0);
  111. Sphere sphere(Vec3(0.1f, 0.2f, 0.3f), 1.1f);
  112. mDebugRenderer->DrawSphere(render_offset + sphere.GetCenter(), sphere.GetRadius(), Color::sYellow);
  113. TestRay<Sphere, Sphere>("Sphere", render_offset, sphere, sphere, [](const Sphere &inSphere, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  114. return RaySphere(inRayOrigin, inRayDirection, inSphere.GetCenter(), inSphere.GetRadius());
  115. });
  116. }
  117. {
  118. Vec3 render_offset(5, 0, 0);
  119. SphereShape sphere_shape(1.1f);
  120. #ifdef JPH_DEBUG_RENDERER
  121. sphere_shape.Draw(mDebugRenderer, Mat44::sTranslation(render_offset), Vec3::sReplicate(1.0f), Color::sYellow, false, false);
  122. #endif // JPH_DEBUG_RENDERER
  123. ConvexShape::SupportBuffer buffer;
  124. const ConvexShape::Support *support = sphere_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  125. TestRay<ConvexShape::Support, SphereShape>("Sphere Shape", render_offset, *support, sphere_shape, [](const SphereShape &inSphere, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  126. return RaySphere(inRayOrigin, inRayDirection, Vec3::sZero(), inSphere.GetRadius());
  127. });
  128. }
  129. {
  130. Vec3 render_offset(10, 0, 0);
  131. AABox box(Vec3(-0.9f, -1.0f, -1.1f), Vec3(0.8f, 0.9f, 1.0f));
  132. mDebugRenderer->DrawBox(box.Transformed(Mat44::sTranslation(render_offset)), Color::sYellow);
  133. TestRay<AABox, AABox>("Box", render_offset, box, box, [](const AABox &inBox, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  134. float fraction = RayAABox(inRayOrigin, RayInvDirection(inRayDirection), inBox.mMin, inBox.mMax);
  135. return max(fraction, 0.0f);
  136. });
  137. }
  138. {
  139. Vec3 render_offset(15, 0, 0);
  140. BoxShape box_shape(Vec3(0.9f, 1.0f, 1.1f), 0.0f);
  141. #ifdef JPH_DEBUG_RENDERER
  142. box_shape.Draw(mDebugRenderer, Mat44::sTranslation(render_offset), Vec3::sReplicate(1.0f), Color::sYellow, false, false);
  143. #endif // JPH_DEBUG_RENDERER
  144. ConvexShape::SupportBuffer buffer;
  145. const ConvexShape::Support *support = box_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  146. TestRay<ConvexShape::Support, BoxShape>("Box Shape", render_offset, *support, box_shape, [](const BoxShape &inBox, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  147. float fraction = RayAABox(inRayOrigin, RayInvDirection(inRayDirection), -inBox.GetHalfExtent(), inBox.GetHalfExtent());
  148. return max(fraction, 0.0f);
  149. });
  150. }
  151. {
  152. Vec3 render_offset(20, 0, 0);
  153. CapsuleShape capsule_shape(1.1f, 0.6f);
  154. #ifdef JPH_DEBUG_RENDERER
  155. capsule_shape.Draw(mDebugRenderer, Mat44::sTranslation(render_offset), Vec3::sReplicate(1.0f), Color::sYellow, false, false);
  156. #endif // JPH_DEBUG_RENDERER
  157. ConvexShape::SupportBuffer buffer;
  158. const ConvexShape::Support *support = capsule_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  159. TestRay<ConvexShape::Support, CapsuleShape>("Capsule Shape", render_offset, *support, capsule_shape, [](const CapsuleShape &inCapsule, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  160. return RayCapsule(inRayOrigin, inRayDirection, inCapsule.GetHalfHeightOfCylinder(), inCapsule.GetRadius());
  161. });
  162. }
  163. {
  164. Vec3 render_offset(25, 0, 0);
  165. CylinderShape cylinder_shape(1.5f, 0.6f, 0.0f);
  166. #ifdef JPH_DEBUG_RENDERER
  167. cylinder_shape.Draw(mDebugRenderer, Mat44::sTranslation(render_offset), Vec3::sReplicate(1.0f), Color::sYellow, false, false);
  168. #endif // JPH_DEBUG_RENDERER
  169. ConvexShape::SupportBuffer buffer;
  170. const ConvexShape::Support *support = cylinder_shape.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  171. TestRay<ConvexShape::Support, CylinderShape>("Cylinder Shape", render_offset, *support, cylinder_shape, [](const CylinderShape &inCylinder, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  172. return RayCylinder(inRayOrigin, inRayDirection, inCylinder.GetHalfHeight(), inCylinder.GetRadius());
  173. });
  174. }
  175. {
  176. Vec3 render_offset(30, 0, 0);
  177. TriangleConvexSupport triangle(Vec3(0.1f, 0.9f, 0.3f), Vec3(-0.9f, -0.5f, 0.2f), Vec3(0.7f, -0.3f, -0.1f));
  178. mDebugRenderer->DrawTriangle(render_offset + triangle.mV1, render_offset + triangle.mV2, render_offset + triangle.mV3, Color::sYellow);
  179. TestRay<TriangleConvexSupport, TriangleConvexSupport>("Triangle", render_offset, triangle, triangle, [](const TriangleConvexSupport &inTriangle, Vec3Arg inRayOrigin, Vec3Arg inRayDirection) {
  180. return RayTriangle(inRayOrigin, inRayDirection, inTriangle.mV1, inTriangle.mV2, inTriangle.mV3);
  181. });
  182. }
  183. }