RandomRayTest.cpp 8.6 KB

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