ConvexHullShrinkTest.cpp 5.9 KB

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