24 T axisAlignment = std::abs(
normal.x());
25 if (
const T yAlignment = std::abs(
normal.y()); yAlignment < axisAlignment) {
27 axisAlignment = yAlignment;
29 if (std::abs(
normal.z()) < axisAlignment) {
35bool supportPointFinite(
const Vector2f& point) {
36 return std::isfinite(point.x()) && std::isfinite(point.y());
40 if (lhs.x() == rhs.x()) {
41 return lhs.y() < rhs.y();
43 return lhs.x() < rhs.x();
47 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
50void appendSupportHullPoint(std::vector<Vector2f>& hull,
const Vector2f& point) {
51 while (hull.size() >= 2) {
52 if (
cross2d(hull.at(hull.size() - 2), hull.back(), point) > 0.0f) {
57 hull.push_back(point);
60void removeLastSupportHullPoint(std::vector<Vector2f>& hull) {
73 MT_THROW_IF(!planeNormal.allFinite(),
"Support plane normal must contain only finite values");
74 MT_THROW_IF(!std::isfinite(planeOffset),
"Support plane offset must be finite");
75 const T normalNorm = planeNormal.norm();
76 MT_THROW_IF(normalNorm <=
Eps<T>(1e-8f, 1e-17),
"Support plane normal must be non-zero");
78 normal = planeNormal / normalNorm;
79 offset = planeOffset / normalNorm;
81 MT_THROW_IF(!uAxisHint.allFinite(),
"Support plane axis hint must contain only finite values");
83 const T tangentNorm = tangent.norm();
84 if (tangentNorm >
Eps<T>(1e-8f, 1e-17)) {
85 uAxis = tangent / tangentNorm;
119 const Vector2f originToA = a - origin;
120 const Vector2f originToB = b - origin;
121 return originToA.x() * originToB.y() - originToA.y() * originToB.x();
125 std::vector<Vector2f> sortedPoints;
126 sortedPoints.reserve(points.size());
127 for (
const Vector2f& point : points) {
129 !supportPointFinite(point),
"Support polygon point must contain only finite values");
130 sortedPoints.push_back(point);
133 std::ranges::sort(sortedPoints, supportPointLess);
135 std::unique(sortedPoints.begin(), sortedPoints.end(), supportPointEqual), sortedPoints.end());
136 if (sortedPoints.size() <= 2) {
140 std::vector<Vector2f> lower;
141 lower.reserve(sortedPoints.size());
142 for (
const Vector2f& point : sortedPoints) {
143 appendSupportHullPoint(lower, point);
146 std::vector<Vector2f> upper;
147 upper.reserve(sortedPoints.size());
148 for (
size_t i = sortedPoints.size(); i-- > 0;) {
149 appendSupportHullPoint(upper, sortedPoints.at(i));
152 removeLastSupportHullPoint(lower);
153 removeLastSupportHullPoint(upper);
154 lower.insert(lower.end(), upper.begin(), upper.end());
163 const std::span<const Vector3f> points,
165 std::vector<Vector2f> projectedPoints;
166 projectedPoints.reserve(points.size());
167 for (
const Vector3f& point : points) {
168 MT_THROW_IF(!point.allFinite(),
"Support point must contain only finite values");
169 projectedPoints.push_back(supportPlane.
coordinates(point));
174template struct SupportPlaneT<float>;
175template struct SupportPlaneT<double>;
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Vector2< float > Vector2f
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
constexpr T Eps(T FloatEps=T(1e-7), T DoubleEps=T(1e-16))
Returns a type-appropriate tolerance/epsilon value.
float cross2d(const Vector2f &origin, const Vector2f &a, const Vector2f &b)
Compute the signed 2D cross product of vectors from an origin.
std::vector< Vector2f > computeConvexHull2d(const std::span< const Vector2f > points)
Compute the convex hull of 2D support points.
Vector3< float > Vector3f
SupportPlaneT< float > SupportPlane
std::vector< Vector2f > computeSupportPolygonFromWorldPoints(const std::span< const Vector3f > points)
Compute the 2D support polygon from world-space support points on the Y-up ground plane.
Vector3< T > projectPoint(const Vector3< T > &point) const
Return point projected orthogonally onto the plane.
Vector3< T > pointFromCoordinates(const Vector2< T > &point) const
Return the world-space point with the given 2D support-plane coordinates.
SupportPlaneT(const Vector3< T > &planeNormal=Vector3< T >::UnitY(), T planeOffset=T(0), const Vector3< T > &uAxisHint=Vector3< T >::UnitX())
Construct an oriented support plane and in-plane coordinate basis.
Vector3< T > uAxis
First unit in-plane basis axis used for 2D support-polygon coordinates.
Vector3< T > vAxis
Second unit in-plane basis axis. Chosen to preserve the default Y-up world-XZ coordinates.
T offset
Plane offset in normal.dot(point) - offset = 0.
Vector3< T > origin() const
Return one world-space point on the plane, equal to normal * offset.
Vector2< T > coordinates(const Vector3< T > &point) const
Return 2D support-plane coordinates of point after projection.
T signedDistance(const Vector3< T > &point) const
Return normal.dot(point) - offset; negative values are below/behind the plane.
Vector3< T > normal
Unit plane normal. Points on the non-penetrating side have non-negative signed distance.