Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
sdf_collision_utility.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
17#include <momentum/math/mesh.h>
19
20#include <axel/BoundingBox.h>
21
22#include <array>
23#include <span>
24
26
37
45 const SkinWeights& skinWeights,
46 const Skeleton& skeleton,
47 size_t vertexIndex,
48 size_t sdfParent) {
49 if (sdfParent == kInvalidIndex) {
50 return kInvalidIndex;
51 }
52 size_t minSkinnedJoint = kInvalidIndex;
53 for (uint32_t k = 0; k < kMaxSkinJoints; ++k) {
54 const auto w = skinWeights.weight(vertexIndex, k);
55 if (w > 0) {
56 const auto joint = skinWeights.index(vertexIndex, k);
57 if (minSkinnedJoint == kInvalidIndex || joint < minSkinnedJoint) {
58 minSkinnedJoint = joint;
59 }
60 }
61 }
62 if (minSkinnedJoint == kInvalidIndex) {
63 return kInvalidIndex;
64 }
65 return skeleton.commonAncestor(minSkinnedJoint, sdfParent);
66}
67
72template <typename T, typename SdfColliderType>
74 const SdfColliderType& collider,
75 const SkeletonStateT<T>& state) {
76 TransformT<T> parentTransform;
77 const auto parentJoint = collider.parentJoint();
78 if (parentJoint != kInvalidIndex) {
79 MT_CHECK(parentJoint < state.jointState.size(), "Invalid parent joint index: {}", parentJoint);
80 parentTransform = state.jointState[parentJoint].transform;
81 }
82 auto colliderWorldTransform =
83 parentTransform * collider.localToParentTransform().template cast<T>();
84 return colliderWorldTransform.inverse();
85}
86
91template <typename T, typename SdfColliderType>
93 const SdfColliderType& collider,
94 const SkeletonStateT<T>& state) {
95 if (!collider.isValid()) {
96 return axel::BoundingBox<T>();
97 }
98
99 TransformT<T> colliderWorldTransform;
100 const auto parentJoint = collider.parentJoint();
101 if (parentJoint == kInvalidIndex) {
102 colliderWorldTransform = collider.localToParentTransform().template cast<T>();
103 } else {
104 MT_CHECK(parentJoint < state.jointState.size(), "Invalid parent joint index: {}", parentJoint);
105 colliderWorldTransform = state.jointState[parentJoint].transform *
106 collider.localToParentTransform().template cast<T>();
107 }
108
109 const auto& localBounds = collider.bounds();
110 const auto& minCorner = localBounds.min();
111 const auto& maxCorner = localBounds.max();
112
113 std::array<Eigen::Vector3<T>, 8> corners = {
114 {{minCorner.x(), minCorner.y(), minCorner.z()},
115 {maxCorner.x(), minCorner.y(), minCorner.z()},
116 {minCorner.x(), maxCorner.y(), minCorner.z()},
117 {maxCorner.x(), maxCorner.y(), minCorner.z()},
118 {minCorner.x(), minCorner.y(), maxCorner.z()},
119 {maxCorner.x(), minCorner.y(), maxCorner.z()},
120 {minCorner.x(), maxCorner.y(), maxCorner.z()},
121 {maxCorner.x(), maxCorner.y(), maxCorner.z()}}};
122
123 const auto firstWorldCorner = colliderWorldTransform * corners[0];
124 axel::BoundingBox<T> worldBounds(firstWorldCorner, T(0));
125 for (size_t i = 1; i < corners.size(); ++i) {
126 worldBounds.extend(colliderWorldTransform * corners[i]);
127 }
128 return worldBounds;
129}
130
134template <typename T>
135axel::BoundingBox<T> computeBoneBoundingBox(
136 std::span<const size_t> vertexIndices,
137 const MeshT<T>& posedMesh) {
138 if (vertexIndices.empty()) {
139 return axel::BoundingBox<T>();
140 }
141 axel::BoundingBox<T> bbox(posedMesh.vertices[vertexIndices[0]], T(0));
142 for (size_t i = 1; i < vertexIndices.size(); ++i) {
143 bbox.extend(posedMesh.vertices[vertexIndices[i]]);
144 }
145 return bbox;
146}
147
153template <typename T>
155 const JointStateT<T>& jointState,
156 size_t paramIndex,
157 const Eigen::Vector3<T>& posd,
158 const Eigen::Vector3<T>& direction,
159 const VectorX<bool>& activeJointParams,
160 const ParameterTransform& parameterTransform,
161 Eigen::Ref<Eigen::VectorX<T>> gradient) {
162 for (size_t d = 0; d < 3; d++) {
163 if (activeJointParams[paramIndex + d]) {
165 direction.dot(jointState.getTranslationDerivative(d)),
166 paramIndex + d,
167 parameterTransform,
168 gradient);
169 }
170 if (activeJointParams[paramIndex + 3 + d]) {
172 direction.dot(jointState.getRotationDerivative(d, posd)),
173 paramIndex + 3 + d,
174 parameterTransform,
175 gradient);
176 }
177 }
178 if (activeJointParams[paramIndex + 6]) {
180 direction.dot(jointState.getScaleDerivative(posd)),
181 paramIndex + 6,
182 parameterTransform,
183 gradient);
184 }
185}
186
191template <typename T>
193 const JointStateT<T>& jointState,
194 size_t paramIndex,
195 const Eigen::Vector3<T>& posd,
196 const Eigen::Vector3<T>& direction,
197 const VectorX<bool>& activeJointParams,
198 const ParameterTransform& parameterTransform,
199 Eigen::Ref<Eigen::MatrixX<T>> jacobianRow) {
200 for (size_t d = 0; d < 3; d++) {
201 if (activeJointParams[paramIndex + d]) {
203 direction.dot(jointState.getTranslationDerivative(d)),
204 paramIndex + d,
205 parameterTransform,
206 jacobianRow);
207 }
208 if (activeJointParams[paramIndex + 3 + d]) {
210 direction.dot(jointState.getRotationDerivative(d, posd)),
211 paramIndex + 3 + d,
212 parameterTransform,
213 jacobianRow);
214 }
215 }
216 if (activeJointParams[paramIndex + 6]) {
218 direction.dot(jointState.getScaleDerivative(posd)),
219 paramIndex + 6,
220 parameterTransform,
221 jacobianRow);
222 }
223}
224
229template <typename T>
231 const SkeletonStateT<T>& state,
232 size_t colliderParent,
233 size_t commonAncestor,
234 const Eigen::Vector3<T>& sdfSurfacePoint,
235 const Eigen::Vector3<T>& direction,
236 const Skeleton& skeleton,
237 const VectorX<bool>& activeJointParams,
238 const ParameterTransform& parameterTransform,
239 Eigen::Ref<Eigen::VectorX<T>> gradient) {
240 size_t jointIndex = colliderParent;
241 while (jointIndex != kInvalidIndex && jointIndex != commonAncestor) {
242 const auto& jointState = state.jointState[jointIndex];
243 const size_t paramIndex = jointIndex * kParametersPerJoint;
244 const Eigen::Vector3<T> posd = sdfSurfacePoint - jointState.translation();
246 jointState, paramIndex, posd, direction, activeJointParams, parameterTransform, gradient);
247 jointIndex = skeleton.joints[jointIndex].parent;
248 }
249}
250
254template <typename T>
256 const SkeletonStateT<T>& state,
257 size_t colliderParent,
258 size_t commonAncestor,
259 const Eigen::Vector3<T>& sdfSurfacePoint,
260 const Eigen::Vector3<T>& direction,
261 const Skeleton& skeleton,
262 const VectorX<bool>& activeJointParams,
263 const ParameterTransform& parameterTransform,
264 Eigen::Ref<Eigen::MatrixX<T>> jacobianRow) {
265 size_t jointIndex = colliderParent;
266 while (jointIndex != kInvalidIndex && jointIndex != commonAncestor) {
267 const auto& jointState = state.jointState[jointIndex];
268 const size_t paramIndex = jointIndex * kParametersPerJoint;
269 const Eigen::Vector3<T> posd = sdfSurfacePoint - jointState.translation();
271 jointState,
272 paramIndex,
273 posd,
274 direction,
275 activeJointParams,
276 parameterTransform,
277 jacobianRow);
278 jointIndex = skeleton.joints[jointIndex].parent;
279 }
280}
281
282} // namespace momentum::detail_sdf_collision
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
TransformT< T > computeWorldToColliderTransform(const SdfColliderType &collider, const SkeletonStateT< T > &state)
Returns the world-to-collider transform for a collider given a skeleton state.
void accumulateColliderHierarchyGradient(const SkeletonStateT< T > &state, size_t colliderParent, size_t commonAncestor, const Eigen::Vector3< T > &sdfSurfacePoint, const Eigen::Vector3< T > &direction, const Skeleton &skeleton, const VectorX< bool > &activeJointParams, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
Accumulates the gradient contribution of a collider's joint hierarchy onto the gradient.
void accumulateJointJacobian(const JointStateT< T > &jointState, size_t paramIndex, const Eigen::Vector3< T > &posd, const Eigen::Vector3< T > &direction, const VectorX< bool > &activeJointParams, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobianRow)
Accumulates the Jacobian contribution of a single joint onto one residual row.
axel::BoundingBox< T > computeColliderWorldBoundingBox(const SdfColliderType &collider, const SkeletonStateT< T > &state)
Returns the world-space axis-aligned bounding box of a collider given a skeleton state.
void accumulateJointGradient(const JointStateT< T > &jointState, size_t paramIndex, const Eigen::Vector3< T > &posd, const Eigen::Vector3< T > &direction, const VectorX< bool > &activeJointParams, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
Accumulates the gradient contribution of a single joint onto the model-parameter gradient.
void accumulateColliderHierarchyJacobian(const SkeletonStateT< T > &state, size_t colliderParent, size_t commonAncestor, const Eigen::Vector3< T > &sdfSurfacePoint, const Eigen::Vector3< T > &direction, const Skeleton &skeleton, const VectorX< bool > &activeJointParams, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobianRow)
Accumulates the Jacobian contribution of a collider's joint hierarchy onto one residual row.
axel::BoundingBox< T > computeBoneBoundingBox(std::span< const size_t > vertexIndices, const MeshT< T > &posedMesh)
Returns the world-space axis-aligned bounding box enclosing the given posed-mesh vertices.
size_t findCommonAncestorForVertex(const SkinWeights &skinWeights, const Skeleton &skeleton, size_t vertexIndex, size_t sdfParent)
Shared, stateless helpers for SDF-collision error functions.
SkeletonT< float > Skeleton
Definition fwd.h:263
void jacobian_jointParams_to_modelParams(const Eigen::Ref< const Eigen::VectorX< T > > &jacobian_jointParams, const Eigen::Index iJointParam, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobian)
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
constexpr size_t kParametersPerJoint
Definition types.h:21
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
constexpr uint32_t kMaxSkinJoints
Maximum number of joints that can influence a single vertex.
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
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
Vector3< T > getTranslationDerivative(size_t index) const
Calculates the derivative with respect to translation along a specific axis.
Vector3< T > getRotationDerivative(size_t index, const Vector3< T > &ref) const
Calculates the derivative of a point with respect to rotation around a specific axis.
Vector3< T > getScaleDerivative(const Vector3< T > &ref) const noexcept
Calculates the derivative of a point with respect to uniform scaling.
A generic mesh representation with support for vertices, faces, lines, and texture coordinates.
Definition mesh.h:20
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
Definition mesh.h:22
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.
size_t commonAncestor(size_t joint1, size_t joint2) const
Returns the lowest common ancestor of two joints in the hierarchy, or kInvalidIndex if either joint i...
Definition skeleton.cpp:97
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...
Definition skeleton.h:25
Linear blend skinning weights for a character mesh: per-vertex joint indices and influence weights.
WeightMatrix weight
Joint influence weights per vertex.
IndexMatrix index
Joint indices influencing each vertex.
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36