ConvexHullShrinkTest.cpp 6.0 KB

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