20#include <axel/BoundingBox.h>
54 const auto w = skinWeights.
weight(vertexIndex, k);
56 const auto joint = skinWeights.
index(vertexIndex, k);
57 if (minSkinnedJoint ==
kInvalidIndex || joint < minSkinnedJoint) {
58 minSkinnedJoint = joint;
72template <
typename T,
typename SdfColl
iderType>
74 const SdfColliderType& collider,
77 const auto parentJoint = collider.parentJoint();
79 MT_CHECK(parentJoint < state.
jointState.size(),
"Invalid parent joint index: {}", parentJoint);
80 parentTransform = state.
jointState[parentJoint].transform;
82 auto colliderWorldTransform =
83 parentTransform * collider.localToParentTransform().template
cast<T>();
84 return colliderWorldTransform.inverse();
91template <
typename T,
typename SdfColl
iderType>
93 const SdfColliderType& collider,
95 if (!collider.isValid()) {
96 return axel::BoundingBox<T>();
100 const auto parentJoint = collider.parentJoint();
102 colliderWorldTransform = collider.localToParentTransform().template
cast<T>();
104 MT_CHECK(parentJoint < state.
jointState.size(),
"Invalid parent joint index: {}", parentJoint);
105 colliderWorldTransform = state.
jointState[parentJoint].transform *
106 collider.localToParentTransform().template
cast<T>();
109 const auto& localBounds = collider.bounds();
110 const auto& minCorner = localBounds.min();
111 const auto& maxCorner = localBounds.max();
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()}}};
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]);
136 std::span<const size_t> vertexIndices,
138 if (vertexIndices.empty()) {
139 return axel::BoundingBox<T>();
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]]);
157 const Eigen::Vector3<T>& posd,
158 const Eigen::Vector3<T>& direction,
161 Eigen::Ref<Eigen::VectorX<T>> gradient) {
162 for (
size_t d = 0; d < 3; d++) {
163 if (activeJointParams[paramIndex + d]) {
170 if (activeJointParams[paramIndex + 3 + d]) {
178 if (activeJointParams[paramIndex + 6]) {
195 const Eigen::Vector3<T>& posd,
196 const Eigen::Vector3<T>& direction,
199 Eigen::Ref<Eigen::MatrixX<T>> jacobianRow) {
200 for (
size_t d = 0; d < 3; d++) {
201 if (activeJointParams[paramIndex + d]) {
208 if (activeJointParams[paramIndex + 3 + d]) {
216 if (activeJointParams[paramIndex + 6]) {
232 size_t colliderParent,
233 size_t commonAncestor,
234 const Eigen::Vector3<T>& sdfSurfacePoint,
235 const Eigen::Vector3<T>& direction,
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];
244 const Eigen::Vector3<T> posd = sdfSurfacePoint - jointState.translation();
246 jointState, paramIndex, posd, direction, activeJointParams, parameterTransform, gradient);
247 jointIndex = skeleton.
joints[jointIndex].parent;
257 size_t colliderParent,
258 size_t commonAncestor,
259 const Eigen::Vector3<T>& sdfSurfacePoint,
260 const Eigen::Vector3<T>& direction,
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];
269 const Eigen::Vector3<T> posd = sdfSurfacePoint - jointState.translation();
278 jointIndex = skeleton.
joints[jointIndex].parent;
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
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 ¶meterTransform, 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 ¶meterTransform, 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 ¶meterTransform, 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 ¶meterTransform, 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
void jacobian_jointParams_to_modelParams(const Eigen::Ref< const Eigen::VectorX< T > > &jacobian_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobian)
ParameterTransformT< float > ParameterTransform
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
constexpr size_t kParametersPerJoint
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
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.
Represents the state of a joint with local and global transformations.
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.
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
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...
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...
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.