Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/ConvexHullBuilder2D.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/NonCopyable.h>78//#define JPH_CONVEX_BUILDER_2D_DEBUG910JPH_NAMESPACE_BEGIN1112/// A convex hull builder that tries to create 2D hulls as accurately as possible. Used for offline processing.13class JPH_EXPORT ConvexHullBuilder2D : public NonCopyable14{15public:16using Positions = Array<Vec3>;17using Edges = Array<int>;1819/// Constructor20/// @param inPositions Positions used to make the hull. Uses X and Y component of Vec3 only!21explicit ConvexHullBuilder2D(const Positions &inPositions);2223/// Destructor24~ConvexHullBuilder2D();2526/// Result enum that indicates how the hull got created27enum class EResult28{29Success, ///< Hull building finished successfully30MaxVerticesReached, ///< Hull building finished successfully, but the desired accuracy was not reached because the max vertices limit was reached31};3233/// Takes all positions as provided by the constructor and use them to build a hull34/// Any points that are closer to the hull than inTolerance will be discarded35/// @param inIdx1 , inIdx2 , inIdx3 The indices to use as initial hull (in any order)36/// @param inMaxVertices Max vertices to allow in the hull. Specify INT_MAX if there is no limit.37/// @param inTolerance Max distance that a point is allowed to be outside of the hull38/// @param outEdges On success this will contain the list of indices that form the hull (counter clockwise)39/// @return Status code that reports if the hull was created or not40EResult Initialize(int inIdx1, int inIdx2, int inIdx3, int inMaxVertices, float inTolerance, Edges &outEdges);4142private:43#ifdef JPH_CONVEX_BUILDER_2D_DEBUG44/// Factor to scale convex hull when debug drawing the construction process45static constexpr Real cDrawScale = 10;46#endif4748class Edge;4950/// Frees all edges51void FreeEdges();5253/// Assigns a position to one of the supplied edges based on which edge is closest.54/// @param inPositionIdx Index of the position to add55/// @param inEdges List of edges to consider56void AssignPointToEdge(int inPositionIdx, const Array<Edge *> &inEdges) const;5758#ifdef JPH_CONVEX_BUILDER_2D_DEBUG59/// Draw state of algorithm60void DrawState();61#endif6263#ifdef JPH_ENABLE_ASSERTS64/// Validate that the edge structure is intact65void ValidateEdges() const;66#endif6768using ConflictList = Array<int>;6970/// Linked list of edges71class Edge72{73public:74JPH_OVERRIDE_NEW_DELETE7576/// Constructor77explicit Edge(int inStartIdx) : mStartIdx(inStartIdx) { }7879/// Calculate the center of the edge and the edge normal80void CalculateNormalAndCenter(const Vec3 *inPositions);8182/// Check if this edge is facing inPosition83inline bool IsFacing(Vec3Arg inPosition) const { return mNormal.Dot(inPosition - mCenter) > 0.0f; }8485Vec3 mNormal; ///< Normal of the edge (not normalized)86Vec3 mCenter; ///< Center of the edge87ConflictList 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.88Edge * mPrevEdge = nullptr; ///< Previous edge in circular list89Edge * mNextEdge = nullptr; ///< Next edge in circular list90int mStartIdx; ///< Position index of start of this edge91float mFurthestPointDistanceSq = 0.0f; ///< Squared distance of furthest point from the conflict list to the edge92};9394const Positions & mPositions; ///< List of positions (some of them are part of the hull)95Edge * mFirstEdge = nullptr; ///< First edge of the hull96int mNumEdges = 0; ///< Number of edges in hull9798#ifdef JPH_CONVEX_BUILDER_2D_DEBUG99RVec3 mOffset; ///< Offset to use for state drawing100Vec3 mDelta; ///< Delta offset between next states101#endif102};103104JPH_NAMESPACE_END105106107