Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
collision_geometry.h
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
8#pragma once
9
16
17#include <cstdint>
18
19namespace momentum {
20
27
32template <typename S>
34 using Scalar = S;
35
39
42
44 size_t parent;
45
48
51 radius(Vector2<S>::Zero()),
53 length(S(0)) {
54 // Empty
55 }
56
58 [[nodiscard]] bool isApprox(const TaperedCapsuleT& other, const S& tol = Eps<S>(1e-4f, 1e-10))
59 const {
60 if (!transformation.isApprox(other.transformation, tol)) {
61 return false;
62 }
63
64 if (!radius.isApprox(other.radius)) {
65 return false;
66 }
67
68 if (parent != other.parent) {
69 return false;
70 }
71
72 if (!::momentum::isApprox(length, other.length)) {
73 return false;
74 }
75
76 return true;
77 }
78};
79
83template <typename S>
85 using Scalar = S;
86
89
92
94 size_t parent;
95
98 // Empty
99 }
100
102 [[nodiscard]] bool isApprox(const CollisionEllipsoidT& other, const S& tol = Eps<S>(1e-4f, 1e-10))
103 const {
104 if (!transformation.isApprox(other.transformation, tol)) {
105 return false;
106 }
107
108 if (!radii.isApprox(other.radii, tol)) {
109 return false;
110 }
111
112 return parent == other.parent;
113 }
114};
115
119template <typename S>
121 using Scalar = S;
122
125
128
130 size_t parent;
131
134 // Empty
135 }
136
138 [[nodiscard]] bool isApprox(const CollisionBoxT& other, const S& tol = Eps<S>(1e-4f, 1e-10))
139 const {
140 if (!transformation.isApprox(other.transformation, tol)) {
141 return false;
142 }
143
144 if (!halfExtents.isApprox(other.halfExtents, tol)) {
145 return false;
146 }
147
148 return parent == other.parent;
149 }
150};
151
157template <typename S>
159 using Scalar = S;
160
163
166
169
172
175
177 size_t parent;
178
181
185 radius(Vector2<S>::Zero()),
186 ellipsoidRadii(Vector3<S>::Zero()),
187 boxHalfExtents(Vector3<S>::Zero()),
189 length(S(0)) {
190 // Empty
191 }
192
194 /* implicit */ CollisionPrimitiveT(const TaperedCapsuleT<S>& capsule)
197 radius(capsule.radius),
198 ellipsoidRadii(Vector3<S>::Zero()),
199 boxHalfExtents(Vector3<S>::Zero()),
200 parent(capsule.parent),
201 length(capsule.length) {}
202
204 /* implicit */ CollisionPrimitiveT(const CollisionEllipsoidT<S>& ellipsoid)
207 radius(Vector2<S>::Zero()),
208 ellipsoidRadii(ellipsoid.radii),
209 boxHalfExtents(Vector3<S>::Zero()),
210 parent(ellipsoid.parent),
211 length(S(0)) {}
212
214 /* implicit */ CollisionPrimitiveT(const CollisionBoxT<S>& box)
217 radius(Vector2<S>::Zero()),
218 ellipsoidRadii(Vector3<S>::Zero()),
219 boxHalfExtents(box.halfExtents),
220 parent(box.parent),
221 length(S(0)) {}
222
227 radius = capsule.radius;
228 ellipsoidRadii.setZero();
229 boxHalfExtents.setZero();
230 parent = capsule.parent;
231 length = capsule.length;
232 return *this;
233 }
234
238 transformation = ellipsoid.transformation;
239 radius.setZero();
240 ellipsoidRadii = ellipsoid.radii;
241 boxHalfExtents.setZero();
242 parent = ellipsoid.parent;
243 length = S(0);
244 return *this;
245 }
246
251 radius.setZero();
252 ellipsoidRadii.setZero();
254 parent = box.parent;
255 length = S(0);
256 return *this;
257 }
258
260 [[nodiscard]] bool isTaperedCapsule() const {
262 }
263
265 [[nodiscard]] bool isEllipsoid() const {
267 }
268
270 [[nodiscard]] bool isBox() const {
272 }
273
278 [[nodiscard]] TaperedCapsuleT<S> toTaperedCapsule() const {
279 MT_CHECK(
281 "Collision primitive type {} does not store tapered capsule data",
282 static_cast<int>(type));
283
284 TaperedCapsuleT<S> capsule;
286 capsule.radius = radius;
287 capsule.parent = parent;
288 capsule.length = length;
289 return capsule;
290 }
291
296 [[nodiscard]] CollisionEllipsoidT<S> toEllipsoid() const {
297 MT_CHECK(
298 isEllipsoid(),
299 "Collision primitive type {} does not store ellipsoid data",
300 static_cast<int>(type));
301
302 CollisionEllipsoidT<S> ellipsoid;
303 ellipsoid.transformation = transformation;
304 ellipsoid.radii = ellipsoidRadii;
305 ellipsoid.parent = parent;
306 return ellipsoid;
307 }
308
313 [[nodiscard]] CollisionBoxT<S> toBox() const {
314 MT_CHECK(
315 isBox(), "Collision primitive type {} does not store box data", static_cast<int>(type));
319 box.parent = parent;
320 return box;
321 }
322
324 [[nodiscard]] bool isApprox(const CollisionPrimitiveT& other, const S& tol = Eps<S>(1e-4f, 1e-10))
325 const {
326 if (type != other.type || parent != other.parent) {
327 return false;
328 }
329
330 if (!transformation.isApprox(other.transformation, tol)) {
331 return false;
332 }
333
335 return radius.isApprox(other.radius, tol) && ::momentum::isApprox(length, other.length, tol);
336 }
337
339 return ellipsoidRadii.isApprox(other.ellipsoidRadii, tol);
340 }
341
343 return boxHalfExtents.isApprox(other.boxHalfExtents, tol);
344 }
345
346 return false;
347 }
348};
349
351template <typename S>
352using CollisionGeometryT = std::vector<CollisionPrimitiveT<S>>;
353
356
359
360} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MOMENTUM_DEFINE_POINTERS(x)
Define the standard set of smart-pointer type aliases for type x.
Definition memory.h:24
constexpr T Eps(T FloatEps=T(1e-7), T DoubleEps=T(1e-16))
Returns a type-appropriate tolerance/epsilon value.
Definition constants.h:30
CollisionGeometryT< float > CollisionGeometry
CollisionGeometryT< double > CollisionGeometryd
std::vector< CollisionPrimitiveT< S > > CollisionGeometryT
Collection of primitives representing a character's collision geometry.
Eigen::AlignedBox< Scalar, Dim > Box
Definition types.h:52
Vector< T, 2 > Vector2
Definition types.h:234
CollisionPrimitiveType
Collision primitive kinds supported by character collision geometry.
bool isApprox(T l, T r, T eps=Eps< T >(1e-4, 1e-6)) noexcept
Determines if two values are approximately equal within a given tolerance.
Definition utility.h:53
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
Vector< T, 3 > Vector3
Definition types.h:236
TaperedCapsuleT< float > TaperedCapsule
Definition fwd.h:320
Box collision geometry for character collision detection.
bool isApprox(const CollisionBoxT &other, const S &tol=Eps< S >(1e-4f, 1e-10)) const
Checks if the current box is approximately equal to another.
Ellipsoid collision geometry for character collision detection.
bool isApprox(const CollisionEllipsoidT &other, const S &tol=Eps< S >(1e-4f, 1e-10)) const
Checks if the current ellipsoid is approximately equal to another.
bool isTaperedCapsule() const
Returns true when this primitive stores tapered capsule data.
CollisionPrimitiveT & operator=(const TaperedCapsuleT< S > &capsule)
Assigns tapered capsule data to this primitive.
bool isBox() const
Returns true when this primitive stores box data.
CollisionPrimitiveT & operator=(const CollisionEllipsoidT< S > &ellipsoid)
Assigns ellipsoid data to this primitive.
CollisionPrimitiveT(const TaperedCapsuleT< S > &capsule)
Creates a collision primitive from a tapered capsule.
bool isEllipsoid() const
Returns true when this primitive stores ellipsoid data.
CollisionPrimitiveT(const CollisionBoxT< S > &box)
Creates a collision primitive from a box.
CollisionPrimitiveT(const CollisionEllipsoidT< S > &ellipsoid)
Creates a collision primitive from an ellipsoid.
CollisionBoxT< S > toBox() const
Converts this primitive to a box.
CollisionPrimitiveT & operator=(const CollisionBoxT< S > &box)
Assigns box data to this primitive.
CollisionEllipsoidT< S > toEllipsoid() const
Converts this primitive to an ellipsoid.
TaperedCapsuleT< S > toTaperedCapsule() const
Converts this primitive to a tapered capsule.
bool isApprox(const CollisionPrimitiveT &other, const S &tol=Eps< S >(1e-4f, 1e-10)) const
Checks if the current primitive is approximately equal to another.
Tapered capsule collision geometry for character collision detection.
bool isApprox(const TaperedCapsuleT &other, const S &tol=Eps< S >(1e-4f, 1e-10)) const
Checks if the current capsule is approximately equal to another.
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36