Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
sdf_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 <axel/BoundingBox.h>
18#include <axel/SignedDistanceField.h>
19
20#include <memory>
21
22namespace momentum {
23
27template <typename S>
29 using Scalar = S;
30 using SDFType = axel::SignedDistanceField<S>;
31
35
39
42 std::shared_ptr<const SDFType> sdf;
43
45
47 const TransformT<S>& transform,
48 size_t parentJoint,
49 std::shared_ptr<const SDFType> sdfPtr)
50 : transformation(transform), parent(parentJoint), sdf(std::move(sdfPtr)) {}
51
57 [[nodiscard]] bool isApprox(const SDFColliderT& other, const S& tol = Eps<S>(1e-4f, 1e-10))
58 const {
59 if (!transformation.isApprox(other.transformation, tol)) {
60 return false;
61 }
62
63 if (parent != other.parent) {
64 return false;
65 }
66
67 if (sdf != other.sdf) {
68 return false;
69 }
70
71 return true;
72 }
73
76 [[nodiscard]] bool isValid() const {
77 return sdf != nullptr;
78 }
79
86
88 template <typename T>
89 [[nodiscard]] T evaluate(const Eigen::Vector3<T>& localPos) const {
90 return static_cast<T>(sdf->sample(localPos));
91 }
92
94 template <typename T>
95 [[nodiscard]] std::pair<T, Eigen::Vector3<T>> evaluateWithGradient(
96 const Eigen::Vector3<T>& localPos) const {
97 const auto [d, g] = sdf->sampleWithGradient(localPos);
98 return {static_cast<T>(d), g.template cast<T>()};
99 }
100
102 [[nodiscard]] axel::BoundingBox<S> bounds() const {
103 return sdf->bounds();
104 }
105
107 [[nodiscard]] size_t parentJoint() const {
108 return parent;
109 }
110
112 [[nodiscard]] TransformT<S> localToParentTransform() const {
113 return transformation;
114 }
115
117 template <typename T>
118 void update(const Eigen::VectorX<T>&) {}
119
121};
122
124template <typename S>
125using SDFCollisionGeometryT = std::vector<SDFColliderT<S>>;
126
129
132
133} // namespace momentum
#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
SDFCollisionGeometryT< double > SDFCollisionGeometryd
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
SDFCollisionGeometryT< float > SDFCollisionGeometry
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Definition types.h:466
std::vector< SDFColliderT< S > > SDFCollisionGeometryT
Collection of SDF colliders representing a character's collision geometry.
bool isApprox(const SDFColliderT &other, const S &tol=Eps< S >(1e-4f, 1e-10)) const
Checks if the current SDF collider is approximately equal to another.
std::pair< T, Eigen::Vector3< T > > evaluateWithGradient(const Eigen::Vector3< T > &localPos) const
Evaluate the SDF distance and gradient at a local-frame position.
axel::SignedDistanceField< float > SDFType
axel::BoundingBox< S > bounds() const
Bounding box of the SDF in local frame.
TransformT< S > localToParentTransform() const
Transform from SDF local frame to parent joint frame.
void update(const Eigen::VectorX< T > &)
Per-iteration update (no-op for voxel SDFs).
bool isValid() const
Returns true when the collider holds a non-null SDF and is therefore usable for collision queries.
std::shared_ptr< const SDFType > sdf
SDFColliderT(const TransformT< S > &transform, size_t parentJoint, std::shared_ptr< const SDFType > sdfPtr)
T evaluate(const Eigen::Vector3< T > &localPos) const
Evaluate the SDF distance at a local-frame position.
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36