PolyhedronSubmergedVolumeCalculator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Geometry/Plane.h>
  5. #ifdef JPH_DEBUG_RENDERER
  6. #include <Renderer/DebugRenderer.h>
  7. #endif // JPH_DEBUG_RENDERER
  8. namespace JPH {
  9. /// This class calculates the intersection between a fluid surface and a polyhedron and returns the submerged volume and its center of buoyancy
  10. /// Construct this class and then one by one add all faces of the polyhedron using the AddFace function. After all faces have been added the result
  11. /// can be gotten through GetResult.
  12. class PolyhedronSubmergedVolumeCalculator
  13. {
  14. private:
  15. // Calculate submerged volume * 6 and center of mass * 4 for a tetrahedron with 4 vertices submerged
  16. // inV1 .. inV4 are submerged
  17. inline static void sTetrahedronVolume4(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, Vec3Arg inV4, float &outVolumeTimes6, Vec3 &outCenterTimes4)
  18. {
  19. // Calculate center of mass and mass of this tetrahedron,
  20. // see: https://en.wikipedia.org/wiki/Tetrahedron#Volume
  21. outVolumeTimes6 = max((inV1 - inV4).Dot((inV2 - inV4).Cross(inV3 - inV4)), 0.0f); // All contributions should be positive because we use a reference point that is on the surface of the hull
  22. outCenterTimes4 = inV1 + inV2 + inV3 + inV4;
  23. }
  24. // Get the intersection point with a plane.
  25. // inV1 is inD1 distance away from the plane, inV2 is inD2 distance away from the plane
  26. inline static Vec3 sGetPlaneIntersection(Vec3Arg inV1, float inD1, Vec3Arg inV2, float inD2)
  27. {
  28. JPH_ASSERT(Sign(inD1) != Sign(inD2), "Assuming both points are on opposite ends of the plane");
  29. float delta = inD1 - inD2;
  30. if (abs(delta) < 1.0e-6f)
  31. return inV1; // Parallel to plane, just pick a point
  32. else
  33. return inV1 + inD1 * (inV2 - inV1) / delta;
  34. }
  35. // Calculate submerged volume * 6 and center of mass * 4 for a tetrahedron with 1 vertex submerged
  36. // inV1 is submerged, inV2 .. inV4 are not
  37. // inD1 .. inD4 are the distances from the points to the plane
  38. inline static void sTetrahedronVolume1(Vec3Arg inV1, float inD1, Vec3Arg inV2, float inD2, Vec3Arg inV3, float inD3, Vec3Arg inV4, float inD4, float &outVolumeTimes6, Vec3 &outCenterTimes4)
  39. {
  40. // A tetrahedron with 1 point submerged is cut along 3 edges forming a new tetrahedron
  41. Vec3 v2 = sGetPlaneIntersection(inV1, inD1, inV2, inD2);
  42. Vec3 v3 = sGetPlaneIntersection(inV1, inD1, inV3, inD3);
  43. Vec3 v4 = sGetPlaneIntersection(inV1, inD1, inV4, inD4);
  44. #ifdef JPH_DEBUG_RENDERER
  45. // Draw intersection between tetrahedron and surface
  46. if (Shape::sDrawSubmergedVolumes)
  47. {
  48. DebugRenderer::sInstance->DrawTriangle(v4, v3, v2, Color::sGreen);
  49. DebugRenderer::sInstance->DrawWireTriangle(v4, v3, v2, Color::sWhite);
  50. }
  51. #endif // JPH_DEBUG_RENDERER
  52. sTetrahedronVolume4(inV1, v2, v3, v4, outVolumeTimes6, outCenterTimes4);
  53. }
  54. // Calculate submerged volume * 6 and center of mass * 4 for a tetrahedron with 2 vertices submerged
  55. // inV1, inV2 are submerged, inV3, inV4 are not
  56. // inD1 .. inD4 are the distances from the points to the plane
  57. inline static void sTetrahedronVolume2(Vec3Arg inV1, float inD1, Vec3Arg inV2, float inD2, Vec3Arg inV3, float inD3, Vec3Arg inV4, float inD4, float &outVolumeTimes6, Vec3 &outCenterTimes4)
  58. {
  59. // A tetrahedron with 2 points submerged is cut along 4 edges forming a quad
  60. Vec3 c = sGetPlaneIntersection(inV1, inD1, inV3, inD3);
  61. Vec3 d = sGetPlaneIntersection(inV1, inD1, inV4, inD4);
  62. Vec3 e = sGetPlaneIntersection(inV2, inD2, inV4, inD4);
  63. Vec3 f = sGetPlaneIntersection(inV2, inD2, inV3, inD3);
  64. #ifdef JPH_DEBUG_RENDERER
  65. // Draw intersection between tetrahedron and surface
  66. if (Shape::sDrawSubmergedVolumes)
  67. {
  68. DebugRenderer::sInstance->DrawTriangle(c, e, d, Color::sGreen);
  69. DebugRenderer::sInstance->DrawTriangle(c, f, e, Color::sGreen);
  70. DebugRenderer::sInstance->DrawWireTriangle(c, e, d, Color::sWhite);
  71. DebugRenderer::sInstance->DrawWireTriangle(c, f, e, Color::sWhite);
  72. }
  73. #endif // JPH_DEBUG_RENDERER
  74. // We pick point c as reference (which is on the cut off surface)
  75. // This leaves us with three tetrahedrons to sum up (any faces that are in the same plane as c will have zero volume)
  76. Vec3 center1, center2, center3;
  77. float volume1, volume2, volume3;
  78. sTetrahedronVolume4(e, f, inV2, c, volume1, center1);
  79. sTetrahedronVolume4(e, inV1, d, c, volume2, center2);
  80. sTetrahedronVolume4(e, inV2, inV1, c, volume3, center3);
  81. // Tally up the totals
  82. outVolumeTimes6 = volume1 + volume2 + volume3;
  83. outCenterTimes4 = outVolumeTimes6 > 0.0f? (volume1 * center1 + volume2 * center2 + volume3 * center3) / outVolumeTimes6 : Vec3::sZero();
  84. }
  85. // Calculate submerged volume * 6 and center of mass * 4 for a tetrahedron with 3 vertices submerged
  86. // inV1, inV2, inV3 are submerged, inV4 is not
  87. // inD1 .. inD4 are the distances from the points to the plane
  88. inline static void sTetrahedronVolume3(Vec3Arg inV1, float inD1, Vec3Arg inV2, float inD2, Vec3Arg inV3, float inD3, Vec3Arg inV4, float inD4, float &outVolumeTimes6, Vec3 &outCenterTimes4)
  89. {
  90. // A tetrahedron with 1 point above the surface is cut along 3 edges forming a new tetrahedron
  91. Vec3 v1 = sGetPlaneIntersection(inV1, inD1, inV4, inD4);
  92. Vec3 v2 = sGetPlaneIntersection(inV2, inD2, inV4, inD4);
  93. Vec3 v3 = sGetPlaneIntersection(inV3, inD3, inV4, inD4);
  94. #ifdef JPH_DEBUG_RENDERER
  95. // Draw intersection between tetrahedron and surface
  96. if (Shape::sDrawSubmergedVolumes)
  97. {
  98. DebugRenderer::sInstance->DrawTriangle(v3, v2, v1, Color::sGreen);
  99. DebugRenderer::sInstance->DrawWireTriangle(v3, v2, v1, Color::sWhite);
  100. }
  101. #endif // JPH_DEBUG_RENDERER
  102. Vec3 dry_center, total_center;
  103. float dry_volume, total_volume;
  104. // We first calculate the part that is above the surface
  105. sTetrahedronVolume4(v1, v2, v3, inV4, dry_volume, dry_center);
  106. // Calculate the total volume
  107. sTetrahedronVolume4(inV1, inV2, inV3, inV4, total_volume, total_center);
  108. // From this we can calculate the center and volume of the submerged part
  109. outVolumeTimes6 = max(total_volume - dry_volume, 0.0f);
  110. outCenterTimes4 = outVolumeTimes6 > 0.0f? (total_center * total_volume - dry_center * dry_volume) / outVolumeTimes6 : Vec3::sZero();
  111. }
  112. public:
  113. /// A helper class that contains cached information about a polyhedron vertex
  114. class Point
  115. {
  116. public:
  117. Vec3 mPosition; ///< World space position of vertex
  118. float mDistanceToSurface; ///< Signed distance to the surface (> 0 is above, < 0 is below)
  119. bool mAboveSurface; ///< If the point is above the surface (mDistanceToSurface > 0)
  120. };
  121. /// Constructor
  122. /// @param inTransform Transform to transform all incoming points with
  123. /// @param inPoints Array of points that are part of the polyhedron
  124. /// @param inPointStride Amount of bytes between each point (should usually be sizeof(Vec3))
  125. /// @param inNumPoints The amount of points
  126. /// @param inSurface The plane that forms the fluid surface (normal should point up)
  127. /// @param ioBuffer A temporary buffer of Point's that should have inNumPoints entries and should stay alive while this class is alive
  128. PolyhedronSubmergedVolumeCalculator(const Mat44 &inTransform, const Vec3 *inPoints, int inPointStride, int inNumPoints, const Plane &inSurface, Point *ioBuffer) :
  129. mPoints(ioBuffer)
  130. {
  131. // Convert the points to world space and determine the distance to the surface
  132. float reference_dist = FLT_MAX;
  133. for (int p = 0; p < inNumPoints; ++p)
  134. {
  135. // Calculate values
  136. Vec3 transformed_point = inTransform * *reinterpret_cast<const Vec3 *>(reinterpret_cast<const uint8 *>(inPoints) + p * inPointStride);
  137. float dist = inSurface.SignedDistance(transformed_point);
  138. bool above = dist >= 0.0f;
  139. // Keep track if all are above or below
  140. mAllAbove &= above;
  141. mAllBelow &= !above;
  142. // Calculate lowest point, we use this to create tetrahedrons out of all faces
  143. if (reference_dist > dist)
  144. {
  145. mReferencePointIdx = p;
  146. reference_dist = dist;
  147. }
  148. // Store values
  149. ioBuffer->mPosition = transformed_point;
  150. ioBuffer->mDistanceToSurface = dist;
  151. ioBuffer->mAboveSurface = above;
  152. ++ioBuffer;
  153. }
  154. }
  155. /// Check if all points are above the surface. Should be used as early out.
  156. inline bool AreAllAbove() const
  157. {
  158. return mAllAbove;
  159. }
  160. /// Check if all points are below the surface. Should be used as early out.
  161. inline bool AreAllBelow() const
  162. {
  163. return mAllBelow;
  164. }
  165. /// Get the lowest point of the polyhedron. Used to form the 4th vertex to make a tetrahedron out of a polyhedron face.
  166. inline int GetReferencePointIdx() const
  167. {
  168. return mReferencePointIdx;
  169. }
  170. /// Add a polyhedron face. Supply the indices of the points that form the face (in counter clockwise order).
  171. void AddFace(int inIdx1, int inIdx2, int inIdx3)
  172. {
  173. JPH_ASSERT(inIdx1 != mReferencePointIdx && inIdx2 != mReferencePointIdx && inIdx3 != mReferencePointIdx, "A face using the reference point will not contribute to the volume");
  174. // Find the points
  175. const Point &ref = mPoints[mReferencePointIdx];
  176. const Point &p1 = mPoints[inIdx1];
  177. const Point &p2 = mPoints[inIdx2];
  178. const Point &p3 = mPoints[inIdx3];
  179. // Determine which vertices are submerged
  180. uint code = (p1.mAboveSurface? 0 : 0b001) | (p2.mAboveSurface? 0 : 0b010) | (p3.mAboveSurface? 0 : 0b100);
  181. float volume;
  182. Vec3 center;
  183. switch (code)
  184. {
  185. case 0b000:
  186. // One point submerged
  187. sTetrahedronVolume1(ref.mPosition, ref.mDistanceToSurface, p3.mPosition, p3.mDistanceToSurface, p2.mPosition, p2.mDistanceToSurface, p1.mPosition, p1.mDistanceToSurface, volume, center);
  188. break;
  189. case 0b001:
  190. // Two points submerged
  191. sTetrahedronVolume2(ref.mPosition, ref.mDistanceToSurface, p1.mPosition, p1.mDistanceToSurface, p3.mPosition, p3.mDistanceToSurface, p2.mPosition, p2.mDistanceToSurface, volume, center);
  192. break;
  193. case 0b010:
  194. // Two points submerged
  195. sTetrahedronVolume2(ref.mPosition, ref.mDistanceToSurface, p2.mPosition, p2.mDistanceToSurface, p1.mPosition, p1.mDistanceToSurface, p3.mPosition, p3.mDistanceToSurface, volume, center);
  196. break;
  197. case 0b100:
  198. // Two points submerged
  199. sTetrahedronVolume2(ref.mPosition, ref.mDistanceToSurface, p3.mPosition, p3.mDistanceToSurface, p2.mPosition, p2.mDistanceToSurface, p1.mPosition, p1.mDistanceToSurface, volume, center);
  200. break;
  201. case 0b011:
  202. // Three points submerged
  203. sTetrahedronVolume3(ref.mPosition, ref.mDistanceToSurface, p2.mPosition, p2.mDistanceToSurface, p1.mPosition, p1.mDistanceToSurface, p3.mPosition, p3.mDistanceToSurface, volume, center);
  204. break;
  205. case 0b101:
  206. // Three points submerged
  207. sTetrahedronVolume3(ref.mPosition, ref.mDistanceToSurface, p1.mPosition, p1.mDistanceToSurface, p3.mPosition, p3.mDistanceToSurface, p2.mPosition, p2.mDistanceToSurface, volume, center);
  208. break;
  209. case 0b110:
  210. // Three points submerged
  211. sTetrahedronVolume3(ref.mPosition, ref.mDistanceToSurface, p3.mPosition, p3.mDistanceToSurface, p2.mPosition, p2.mDistanceToSurface, p1.mPosition, p1.mDistanceToSurface, volume, center);
  212. break;
  213. case 0b111:
  214. // Four points submerged
  215. sTetrahedronVolume4(ref.mPosition, p3.mPosition, p2.mPosition, p1.mPosition, volume, center);
  216. break;
  217. default:
  218. // Should not be possible
  219. JPH_ASSERT(false);
  220. volume = 0.0f;
  221. center = Vec3::sZero();
  222. break;
  223. }
  224. mSubmergedVolume += volume;
  225. mCenterOfBuoyancy += volume * center;
  226. }
  227. /// Call after all faces have been added. Returns the submerged volume and the center of buoyancy for the submerged volume.
  228. void GetResult(float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy) const
  229. {
  230. outCenterOfBuoyancy = mSubmergedVolume > 0.0f? mCenterOfBuoyancy / (4.0f * mSubmergedVolume) : Vec3::sZero(); // Do this before dividing submerged volume by 6 to get correct weight factor
  231. outSubmergedVolume = mSubmergedVolume / 6.0f;
  232. }
  233. private:
  234. // The precalculated points for this polyhedron
  235. const Point * mPoints;
  236. // If all points are above/below the surface
  237. bool mAllBelow = true;
  238. bool mAllAbove = true;
  239. // The lowest point
  240. int mReferencePointIdx = 0;
  241. // Aggregator for submerged volume and center of buoyancy
  242. float mSubmergedVolume = 0.0f;
  243. Vec3 mCenterOfBuoyancy = Vec3::sZero();
  244. };
  245. } // JPH