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