Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
support_polygon.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
12
13#include <algorithm>
14#include <cmath>
15#include <cstddef>
16
17namespace momentum {
18
19namespace {
20
21template <typename T>
22Vector3<T> fallbackSupportPlaneAxis(const Vector3<T>& normal) {
24 T axisAlignment = std::abs(normal.x());
25 if (const T yAlignment = std::abs(normal.y()); yAlignment < axisAlignment) {
26 axis = Vector3<T>::UnitY();
27 axisAlignment = yAlignment;
28 }
29 if (std::abs(normal.z()) < axisAlignment) {
30 axis = Vector3<T>::UnitZ();
31 }
32 return (axis - normal * axis.dot(normal)).normalized();
33}
34
35bool supportPointFinite(const Vector2f& point) {
36 return std::isfinite(point.x()) && std::isfinite(point.y());
37}
38
39bool supportPointLess(const Vector2f& lhs, const Vector2f& rhs) {
40 if (lhs.x() == rhs.x()) {
41 return lhs.y() < rhs.y();
42 }
43 return lhs.x() < rhs.x();
44}
45
46bool supportPointEqual(const Vector2f& lhs, const Vector2f& rhs) {
47 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
48}
49
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) {
53 break;
54 }
55 hull.pop_back();
56 }
57 hull.push_back(point);
58}
59
60void removeLastSupportHullPoint(std::vector<Vector2f>& hull) {
61 if (!hull.empty()) {
62 hull.pop_back();
63 }
64}
65
66} // namespace
67
68template <typename T>
70 const Vector3<T>& planeNormal,
71 const T planeOffset,
72 const Vector3<T>& uAxisHint) {
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");
77
78 normal = planeNormal / normalNorm;
79 offset = planeOffset / normalNorm;
80
81 MT_THROW_IF(!uAxisHint.allFinite(), "Support plane axis hint must contain only finite values");
82 Vector3<T> tangent = uAxisHint - normal * uAxisHint.dot(normal);
83 const T tangentNorm = tangent.norm();
84 if (tangentNorm > Eps<T>(1e-8f, 1e-17)) {
85 uAxis = tangent / tangentNorm;
86 } else {
87 uAxis = fallbackSupportPlaneAxis(normal);
88 }
89 vAxis = uAxis.cross(normal).normalized();
90}
91
92template <typename T>
96
97template <typename T>
99 return normal.dot(point) - offset;
100}
101
102template <typename T>
104 return point - signedDistance(point) * normal;
105}
106
107template <typename T>
109 const Vector3<T> fromOrigin = projectPoint(point) - origin();
110 return Vector2<T>(fromOrigin.dot(uAxis), fromOrigin.dot(vAxis));
111}
112
113template <typename T>
115 return origin() + point.x() * uAxis + point.y() * vAxis;
116}
117
118float cross2d(const Vector2f& origin, const Vector2f& a, const Vector2f& b) {
119 const Vector2f originToA = a - origin;
120 const Vector2f originToB = b - origin;
121 return originToA.x() * originToB.y() - originToA.y() * originToB.x();
122}
123
124std::vector<Vector2f> computeConvexHull2d(const std::span<const Vector2f> points) {
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);
131 }
132
133 std::ranges::sort(sortedPoints, supportPointLess);
134 sortedPoints.erase(
135 std::unique(sortedPoints.begin(), sortedPoints.end(), supportPointEqual), sortedPoints.end());
136 if (sortedPoints.size() <= 2) {
137 return sortedPoints;
138 }
139
140 std::vector<Vector2f> lower;
141 lower.reserve(sortedPoints.size());
142 for (const Vector2f& point : sortedPoints) {
143 appendSupportHullPoint(lower, point);
144 }
145
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));
150 }
151
152 removeLastSupportHullPoint(lower);
153 removeLastSupportHullPoint(upper);
154 lower.insert(lower.end(), upper.begin(), upper.end());
155 return lower;
156}
157
158std::vector<Vector2f> computeSupportPolygonFromWorldPoints(const std::span<const Vector3f> points) {
160}
161
163 const std::span<const Vector3f> points,
164 const SupportPlane& supportPlane) {
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));
170 }
171 return computeConvexHull2d(projectedPoints);
172}
173
174template struct SupportPlaneT<float>;
175template struct SupportPlaneT<double>;
176
177} // namespace momentum
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
Vector2< float > Vector2f
Definition types.h:250
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
Definition random-inl.h:382
constexpr T Eps(T FloatEps=T(1e-7), T DoubleEps=T(1e-16))
Returns a type-appropriate tolerance/epsilon value.
Definition constants.h:30
float cross2d(const Vector2f &origin, const Vector2f &a, const Vector2f &b)
Compute the signed 2D cross product of vectors from an origin.
Vector< T, 2 > Vector2
Definition types.h:234
std::vector< Vector2f > computeConvexHull2d(const std::span< const Vector2f > points)
Compute the convex hull of 2D support points.
Vector< T, 3 > Vector3
Definition types.h:236
Vector3< float > Vector3f
Definition types.h:251
SupportPlaneT< float > SupportPlane
Definition fwd.h:58
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.