ConvexHullBuilder2D.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/NonCopyable.h>
  6. //#define JPH_CONVEX_BUILDER_2D_DEBUG
  7. JPH_NAMESPACE_BEGIN
  8. /// A convex hull builder that tries to create 2D hulls as accurately as possible. Used for offline processing.
  9. class JPH_EXPORT ConvexHullBuilder2D : public NonCopyable
  10. {
  11. public:
  12. using Positions = Array<Vec3>;
  13. using Edges = Array<int>;
  14. /// Constructor
  15. /// @param inPositions Positions used to make the hull. Uses X and Y component of Vec3 only!
  16. explicit ConvexHullBuilder2D(const Positions &inPositions);
  17. /// Destructor
  18. ~ConvexHullBuilder2D();
  19. /// Result enum that indicates how the hull got created
  20. enum class EResult
  21. {
  22. Success, ///< Hull building finished successfully
  23. MaxVerticesReached, ///< Hull building finished successfully, but the desired accuracy was not reached because the max vertices limit was reached
  24. };
  25. /// Takes all positions as provided by the constructor and use them to build a hull
  26. /// Any points that are closer to the hull than inTolerance will be discarded
  27. /// @param inIdx1 , inIdx2 , inIdx3 The indices to use as initial hull (in any order)
  28. /// @param inMaxVertices Max vertices to allow in the hull. Specify INT_MAX if there is no limit.
  29. /// @param inTolerance Max distance that a point is allowed to be outside of the hull
  30. /// @param outEdges On success this will contain the list of indices that form the hull (counter clockwise)
  31. /// @return Status code that reports if the hull was created or not
  32. EResult Initialize(int inIdx1, int inIdx2, int inIdx3, int inMaxVertices, float inTolerance, Edges &outEdges);
  33. private:
  34. #ifdef JPH_CONVEX_BUILDER_2D_DEBUG
  35. /// Factor to scale convex hull when debug drawing the construction process
  36. static constexpr Real cDrawScale = 10;
  37. #endif
  38. class Edge;
  39. /// Frees all edges
  40. void FreeEdges();
  41. /// Assigns a position to one of the supplied edges based on which edge is closest.
  42. /// @param inPositionIdx Index of the position to add
  43. /// @param inEdges List of edges to consider
  44. void AssignPointToEdge(int inPositionIdx, const Array<Edge *> &inEdges) const;
  45. #ifdef JPH_CONVEX_BUILDER_2D_DEBUG
  46. /// Draw state of algorithm
  47. void DrawState();
  48. #endif
  49. #ifdef JPH_ENABLE_ASSERTS
  50. /// Validate that the edge structure is intact
  51. void ValidateEdges() const;
  52. #endif
  53. using ConflictList = Array<int>;
  54. /// Linked list of edges
  55. class Edge
  56. {
  57. public:
  58. JPH_OVERRIDE_NEW_DELETE
  59. /// Constructor
  60. explicit Edge(int inStartIdx) : mStartIdx(inStartIdx) { }
  61. /// Calculate the center of the edge and the edge normal
  62. void CalculateNormalAndCenter(const Vec3 *inPositions);
  63. /// Check if this edge is facing inPosition
  64. inline bool IsFacing(Vec3Arg inPosition) const { return mNormal.Dot(inPosition - mCenter) > 0.0f; }
  65. Vec3 mNormal; ///< Normal of the edge (not normalized)
  66. Vec3 mCenter; ///< Center of the edge
  67. ConflictList mConflictList; ///< Positions associated with this edge (that are closest to this edge). Last entry is the one furthest away from the edge, remainder is unsorted.
  68. Edge * mPrevEdge = nullptr; ///< Previous edge in cicular list
  69. Edge * mNextEdge = nullptr; ///< Next edge in circular list
  70. int mStartIdx; ///< Position index of start of this edge
  71. float mFurthestPointDistanceSq = 0.0f; ///< Squared distance of furtest point from the conflict list to the edge
  72. };
  73. const Positions & mPositions; ///< List of positions (some of them are part of the hull)
  74. Edge * mFirstEdge = nullptr; ///< First edge of the hull
  75. int mNumEdges = 0; ///< Number of edges in hull
  76. #ifdef JPH_CONVEX_BUILDER_2D_DEBUG
  77. RVec3 mOffset; ///< Offset to use for state drawing
  78. Vec3 mDelta; ///< Delta offset between next states
  79. #endif
  80. };
  81. JPH_NAMESPACE_END