27 set(referenceSkeleton, computeDeriv);
36 set(referenceSkeleton, computeDeriv);
59 for (
size_t iJoint = 0; iJoint < jointState.size(); ++iJoint) {
60 jointState[iJoint].set(rhs.
jointState[iJoint]);
72 set(referenceSkeleton, computeDeriv);
83 set(referenceSkeleton, computeDeriv);
90 const size_t numJoints = joints.size();
94 "Unexpected joint parameter size. Expected '{}' (# of joints '{}' X kParametersPerJoint '{}') but got '{}'.",
98 jointParameters.size());
104 for (
size_t jointID = 0; jointID < numJoints; jointID++) {
106 const JointT<T>& joint = joints[jointID];
109 jointState[jointID].set(
110 joint, jointParameters.v.template middleRows<7>(parameterOffset),
nullptr, computeDeriv);
112 jointState[jointID].set(
114 jointParameters.v.template middleRows<7>(parameterOffset),
115 &jointState[joint.
parent],
120 MT_CHECK(jointState.size() == numJoints,
"{} is not {}", jointState.size(), numJoints);
128 result.push_back(js.transform);
156 for (
size_t i = 0; i < state1.
jointState.size(); i++) {
159 const T
dot = std::min(
161 state1.
jointState[i].rotation().normalized().dot(
162 state2.
jointState[i].rotation().normalized()),
165 const T sgn =
dot < T(0) ? T(-1) : T(1);
169 const auto rootMeanSquare = [](
const VectorXf& errors) {
170 return std::sqrt(errors.squaredNorm() /
static_cast<float>(errors.size()));
191 size_t ancestorA = jointA;
192 size_t ancestorB = jointB;
202 B_to_ancestorB = skelState.
jointState[ancestorB].localTransform * B_to_ancestorB;
203 if (ancestorB < referenceSkeleton.
joints.size()) {
204 ancestorB = referenceSkeleton.
joints[ancestorB].parent;
210 A_to_ancestorA = skelState.
jointState[ancestorA].localTransform * A_to_ancestorA;
211 if (ancestorA < referenceSkeleton.
joints.size()) {
212 ancestorA = referenceSkeleton.
joints[ancestorA].parent;
222 MT_CHECK(ancestorA == ancestorB,
"{} is not {}", ancestorA, ancestorB);
223 return B_to_ancestorB.
inverse() * A_to_ancestorA;
229[[nodiscard]] Eigen::Vector<T, kParametersPerJoint> localTransformToJointParameters(
231 const TransformT<T>& localTransform) {
232 Eigen::Vector<T, kParametersPerJoint> result = Eigen::Vector<T, kParametersPerJoint>::Zero();
233 result.template segment<3>(0) =
234 localTransform.translation - joint.translationOffset.template
cast<T>();
236 const Eigen::Quaternion<T> localRotation =
237 joint.preRotation.template
cast<T>().inverse() * localTransform.rotation;
239 result(6) = std::log2(localTransform.scale);
251 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
254 localTransformToJointParameters(skeleton.
joints[i], localXF);
266 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
267 const auto parentJoint = skeleton.
joints[i].parent;
270 parentXF = state[parentJoint];
276 localTransformToJointParameters(skeleton.
joints[i], localXF);
288[[nodiscard]] Eigen::Vector<T, kParametersPerJoint>
289localTransformToJointParametersRespectingConstraints(
291 const TransformT<T>& localTransform,
296 Eigen::Vector<T, kParametersPerJoint> result = Eigen::Vector<T, kParametersPerJoint>::Zero();
298 for (
int k = 0; k < 3; ++k) {
299 if (activeJointParams[paramOffset + k]) {
300 result(k) = localTransform.translation(k) -
static_cast<T
>(joint.translationOffset(k));
305 const Eigen::Quaternion<T> localRotation =
306 joint.preRotation.template
cast<T>().inverse() * localTransform.rotation;
308 const Matrix3<T> rotationMatrix = localRotation.toRotationMatrix();
310 std::array<int, 3> activeAxes{};
311 size_t numActiveAxes = 0;
312 for (
int k = 0; k < 3; ++k) {
313 if (activeJointParams[paramOffset + 3 + k]) {
314 activeAxes[numActiveAxes++] = k;
318 if (numActiveAxes == 1) {
319 const auto axis = activeAxes[0];
321 result(3 + axis) = angle;
322 }
else if (numActiveAxes == 2) {
325 result(3 + activeAxes[0]) = angles[0];
326 result(3 + activeAxes[1]) = angles[1];
327 }
else if (numActiveAxes == 3) {
330 result.template segment<3>(3) =
336 if (activeJointParams[paramOffset + 6]) {
337 result(6) = std::log2(localTransform.scale);
351 activeJointParams.size() ==
353 "activeJointParams size {} does not match expected size {} (skeleton joints: {})",
354 activeJointParams.size(),
361 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
364 localTransformToJointParametersRespectingConstraints(
365 skeleton.
joints[i], localXF, activeJointParams, i);
377 activeJointParams.size() ==
379 "activeJointParams size {} does not match expected size {} (skeleton joints: {})",
380 activeJointParams.size(),
387 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
388 const auto parentJoint = skeleton.
joints[i].parent;
391 parentXF = state[parentJoint];
397 localTransformToJointParametersRespectingConstraints(
398 skeleton.
joints[i], localXF, activeJointParams, i);
404template struct SkeletonStateT<float>;
405template struct SkeletonStateT<double>;
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
T rotationMatrixToOneAxisEuler(const Matrix3< T > &m, int axis0)
Fits a single-axis Euler rotation to a rotation matrix.
SkeletonT< float > Skeleton
Vector3< T > rotationMatrixToEulerXYZ(const Matrix3< T > &m, EulerConvention convention)
An optimized version of rotationMatrixToEuler(m, 0, 1, 2, convention) or rotationMatrixToEuler(m,...
TransformT< T > transformAtoB(size_t jointA, size_t jointB, const Skeleton &referenceSkeleton, const SkeletonStateT< T > &skelState)
Computes the relative transform between two joints in a skeleton.
std::vector< TransformT< T > > TransformListT
Per-joint transform list describing the state of all joints in a skeleton (one entry per joint,...
Vector3< T > quaternionToEuler(const Quaternion< T > &q)
Converts a quaternion to Euler angles using the Extrinsic XYZ convention.
Eigen::Matrix< T, 3, 3 > Matrix3
@ Extrinsic
The extrinsic convention.
FloatP dot(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes dot product of Eigen::Vector3f and 3-vector of packets.
Vector2< T > rotationMatrixToTwoAxisEuler(const Matrix3< T > &m, int axis0, int axis1)
Fits a two-axis Euler rotation to a rotation matrix.
JointParametersT< T > skeletonStateToJointParametersRespectingActiveParameters(const SkeletonStateT< T > &state, const Skeleton &skeleton, const VectorX< bool > &activeJointParams)
Converts skeleton state back to joint parameters, respecting active joint parameter constraints.
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
constexpr size_t kParametersPerJoint
JointParametersT< T > skeletonStateToJointParameters(const SkeletonStateT< T > &state, const Skeleton &skeleton)
The "RespectingActiveParameters" variants use rotationMatrixToOneAxisEuler or rotationMatrixToTwoAxis...
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
VectorX< float > VectorXf
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
std::vector< JointT< T > > JointListT
A list of joints (e.g., of a skeleton).
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
static EIGEN_DEVICE_FUNC JointParametersT< Scalar > Zero(::Eigen::Index size)
A vector describing joint parameters, of size numSkeletonJoints * kParametersPerJoint.
A simplified version of a Maya joint with pre-rotation only (no post-rotation).
size_t parent
Index of the parent joint in the skeleton hierarchy, or kInvalidIndex for the root.
Represents the complete state of a skeleton.
SkeletonStateT< T2 > cast() const
Converts the skeleton state to a different scalar type.
void set(const JointParametersT< T > &jointParameters, const Skeleton &referenceSkeleton, bool computeDeriv=true)
Updates the skeleton state with new joint parameters.
JointParametersT< T > jointParameters
Joint parameters for all joints in the skeleton.
TransformListT< T > toTransforms() const
Extracts global transforms for all joints in the skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.
static StateSimilarity compare(const SkeletonStateT< T > &state1, const SkeletonStateT< T > &state2)
Compares two skeleton states and returns similarity metrics.
SkeletonStateT() noexcept=default
Creates an empty skeleton state.
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...
Measures of similarity between two skeleton states.
float orientationRMSE
Root mean square of orientation errors across all joints.
float positionRMSE
Root mean square of position errors across all joints.
float orientationMax
Maximum orientation error across all joints.
float positionMax
Maximum position error across all joints.
VectorXf orientationError
Orientation error for each joint (in radians).
VectorXf positionError
Position error for each joint (in distance units).