ConvexHullShrinkTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/ConvexHullShrinkTest.h>
  6. #include <Utils/Log.h>
  7. #include <Utils/DebugRendererSP.h>
  8. #include <Jolt/Geometry/ConvexSupport.h>
  9. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  10. #include <Renderer/DebugRendererImp.h>
  11. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  12. #include <fstream>
  13. JPH_SUPPRESS_WARNINGS_STD_END
  14. JPH_IMPLEMENT_RTTI_VIRTUAL(ConvexHullShrinkTest)
  15. {
  16. JPH_ADD_BASE_CLASS(ConvexHullShrinkTest, Test)
  17. }
  18. void ConvexHullShrinkTest::Initialize()
  19. {
  20. // First add a list of shapes that were problematic before
  21. mPoints = {
  22. {
  23. Vec3(1, 1, 1),
  24. Vec3(1, 1, -1),
  25. Vec3(1, -1, 1),
  26. Vec3(1, -1, -1),
  27. },
  28. {
  29. Vec3(1, 1, 1),
  30. Vec3(1, 1, -1),
  31. Vec3(1, -1, 1),
  32. Vec3(1, -1, -1),
  33. Vec3(-1, 1, 1),
  34. Vec3(-1, 1, -1),
  35. Vec3(-1, -1, 1),
  36. Vec3(-1, -1, -1),
  37. },
  38. {
  39. Vec3(0.24055352f, 0.42262089f, 0.20811508f),
  40. Vec3(0.23034751f, 0.42984104f, -0.21389426f),
  41. Vec3(0.21995061f, 0.43724900f, 0.20929135f),
  42. Vec3(0.18619442f, 0.44122630f, 0.10257969f),
  43. Vec3(-0.22997921f, 0.43706810f, 0.21128670f),
  44. Vec3(0.18488347f, -0.44135576f, 0.10415942f),
  45. Vec3(-0.20950880f, -0.43603044f, 0.20873074f),
  46. Vec3(-0.21230474f, -0.43691945f, -0.20506332f),
  47. Vec3(0.23440370f, -0.43392032f, 0.20985059f),
  48. Vec3(0.22406587f, -0.43578571f, -0.21132792f),
  49. Vec3(0.24845430f, -0.41821426f, -0.21033705f),
  50. Vec3(0.24780219f, -0.42262548f, 0.21058462f),
  51. Vec3(-0.24866026f, 0.41188520f, 0.20908103f),
  52. Vec3(-0.25144735f, 0.41933101f, -0.20718251f),
  53. Vec3(-0.24799588f, -0.20490804f, 0.21178717f),
  54. Vec3(0.01075744f, -0.41775572f, -0.22181017f),
  55. Vec3(-0.18624404f, -0.18736419f, -0.21975047f),
  56. Vec3(0.22080457f, 0.01773871f, -0.22080121f),
  57. Vec3(-0.17988407f, 0.40095943f, -0.21670545f),
  58. Vec3(-0.23094913f, 0.42154532f, 0.21846796f),
  59. Vec3(0.23783659f, 0.41114848f, -0.20812420f),
  60. Vec3(0.25242796f, 0.00087111f, 0.04875314f),
  61. Vec3(0.20976084f, 0.43694448f, -0.20819492f),
  62. Vec3(0.21914389f, -0.42215359f, -0.21839635f),
  63. Vec3(0.22120973f, 0.42172050f, 0.21581716f),
  64. Vec3(0.07287904f, 0.40937370f, 0.21898652f),
  65. Vec3(-0.23638439f, 0.42299985f, -0.21391643f),
  66. Vec3(0.25210538f, -0.20603905f, 0.20603551f),
  67. Vec3(-0.22867783f, -0.43080616f, -0.21309699f),
  68. Vec3(-0.22365719f, 0.43650645f, -0.20515810f),
  69. Vec3(-0.23701435f, 0.43320888f, -0.20985882f),
  70. Vec3(-0.24509817f, 0.42541492f, 0.21352110f),
  71. Vec3(0.22803798f, -0.41877448f, 0.21590335f),
  72. Vec3(-0.21627685f, -0.41884291f, 0.21908275f),
  73. Vec3(-0.24125161f, -0.13299965f, -0.21386964f),
  74. Vec3(-0.22310710f, -0.43280768f, 0.21368177f),
  75. Vec3(-0.23707944f, -0.41916745f, 0.21170078f),
  76. Vec3(-0.23729360f, -0.42400050f, -0.20905880f),
  77. Vec3(-0.23056241f, 0.44033193f, -0.00191451f),
  78. Vec3(-0.24118152f, -0.41101628f, -0.20855166f),
  79. Vec3(0.21646300f, 0.42087674f, -0.21763385f),
  80. Vec3(0.25090047f, -0.41023433f, 0.10248772f),
  81. Vec3(0.03950108f, -0.43627834f, -0.21231101f),
  82. Vec3(-0.22727611f, -0.24993966f, 0.21899925f),
  83. Vec3(0.24388977f, -0.07015021f, -0.21204789f)
  84. }
  85. };
  86. // Open the external file with hulls
  87. // A stream containing predefined convex hulls
  88. ifstream points_stream("Assets/convex_hulls.bin", std::ios::binary);
  89. if (points_stream.is_open())
  90. {
  91. for (;;)
  92. {
  93. // Read the length of the next point cloud
  94. uint32 len = 0;
  95. points_stream.read((char *)&len, sizeof(len));
  96. if (points_stream.eof())
  97. break;
  98. // Read the points
  99. if (len > 0)
  100. {
  101. Points p;
  102. for (uint32 i = 0; i < len; ++i)
  103. {
  104. Float3 v;
  105. points_stream.read((char *)&v, sizeof(v));
  106. p.push_back(Vec3(v));
  107. }
  108. mPoints.push_back(std::move(p));
  109. }
  110. }
  111. }
  112. }
  113. void ConvexHullShrinkTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  114. {
  115. // Take one of the predefined shapes
  116. const Points &points = mIteration < mPoints.size()? mPoints[mIteration] : mPoints.back();
  117. mIteration++;
  118. // Create shape
  119. ConvexHullShapeSettings settings(points, cDefaultConvexRadius);
  120. Shape::ShapeResult result = settings.Create();
  121. if (!result.IsValid())
  122. {
  123. Trace("%d: %s", mIteration - 1, result.GetError().c_str());
  124. return;
  125. }
  126. RefConst<ConvexHullShape> shape = static_cast<const ConvexHullShape *>(result.Get().GetPtr());
  127. // Shape creation may have reduced the convex radius, fetch the result
  128. const float convex_radius = shape->GetConvexRadius();
  129. if (convex_radius > 0.0f)
  130. {
  131. // Get the support function of the shape excluding convex radius and add the convex radius
  132. ConvexShape::SupportBuffer buffer;
  133. const ConvexShape::Support *support = shape->GetSupportFunction(ConvexShape::ESupportMode::ExcludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  134. AddConvexRadius<ConvexShape::Support> add_cvx(*support, convex_radius);
  135. // Calculate the error w.r.t. the original hull
  136. float max_error = -FLT_MAX;
  137. int max_error_plane = 0;
  138. Vec3 max_error_support_point = Vec3::sZero();
  139. const Array<Plane> &planes = shape->GetPlanes();
  140. for (int i = 0; i < (int)planes.size(); ++i)
  141. {
  142. const Plane &plane = planes[i];
  143. Vec3 support_point = add_cvx.GetSupport(plane.GetNormal());
  144. float distance = plane.SignedDistance(support_point);
  145. if (distance > max_error)
  146. {
  147. max_error = distance;
  148. max_error_support_point = support_point;
  149. max_error_plane = i;
  150. }
  151. }
  152. if (max_error > settings.mMaxErrorConvexRadius)
  153. {
  154. Trace("%d, %f, %f", mIteration - 1, (double)convex_radius, (double)max_error);
  155. DrawMarkerSP(mDebugRenderer, max_error_support_point, Color::sPurple, 0.1f);
  156. DrawArrowSP(mDebugRenderer, max_error_support_point, max_error_support_point - max_error * planes[max_error_plane].GetNormal(), Color::sPurple, 0.01f);
  157. }
  158. }
  159. #ifdef JPH_DEBUG_RENDERER
  160. // Draw the hulls
  161. shape->Draw(DebugRenderer::sInstance, RMat44::sIdentity(), Vec3::sReplicate(1.0f), Color::sRed, false, false);
  162. shape->DrawGetSupportFunction(DebugRenderer::sInstance, RMat44::sIdentity(), Vec3::sReplicate(1.0f), Color::sLightGrey, false);
  163. shape->DrawShrunkShape(DebugRenderer::sInstance, RMat44::sIdentity(), Vec3::sReplicate(1.0f));
  164. #endif // JPH_DEBUG_RENDERER
  165. }