Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
joint_state.cpp
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
9
12
13namespace momentum {
14
15template <typename T>
16const std::array<Vector3<T>, 3> RotationAxis = {
20
21template <typename T>
23 const JointT<T>& joint,
24 const JointVectorT<T>& parameters,
25 const JointStateT<T>* parentState,
26 bool computeDeriv) noexcept {
27 derivDirty = !computeDeriv;
28
29 TransformT<T> parent;
30 if (parentState != nullptr) {
31 parent = parentState->transform;
32 }
33
34 // Translation acts in the parent's frame, so the per-axis translation derivative is
35 // the parent's rotated unit axis. Identity when there is no parent.
36 if (computeDeriv) {
37 if (parentState != nullptr) {
38 translationAxis = parentState->transform.toLinear();
39 } else {
40 translationAxis.setIdentity();
41 }
42 }
43
44 localTransform.translation.noalias() = joint.translationOffset + parameters.template head<3>();
45
46 localTransform.rotation = joint.preRotation;
47
48 // Apply rotations in reverse order (Z, Y, X) so the resulting product is Rpre * Rx * Ry * Rz.
49 // The rotation derivative axis for index i is the parent rotation composed with the
50 // partially-accumulated local rotation up to (but not including) axis i.
51 for (int index = 2; index >= 0; --index) {
52 if (computeDeriv) {
53 rotationAxis.col(index).noalias() =
54 (parent.rotation * localTransform.rotation) * RotationAxis<T>[index];
55 }
56 localTransform.rotation *=
57 Quaternion<T>(AngleAxis<T>((T)parameters[3 + index], RotationAxis<T>[index]));
58 }
59
60 // Scale parameter is stored as a log2 value: scale = exp2(parameters[6]).
61 // Keeps scale strictly positive and makes the parameter space uniform in log-ratio.
62 localTransform.scale = std::exp2((T)parameters[6]);
63
64 transform = parent * localTransform;
65}
66
67template <typename T>
68Vector3<T> JointStateT<T>::getRotationDerivative(const size_t index, const Vector3<T>& ref) const {
69 MT_CHECK(!derivDirty, "Derivatives haven't been computed yet.");
70 return rotationAxis.col(index).cross(ref);
71}
72
73template <typename T>
75 MT_CHECK(!derivDirty, "Derivatives haven't been computed yet.");
76 return translationAxis.col(index);
77}
78
79template <typename T>
81 return ref * ln2<T>();
82}
83
84template <typename T>
85template <typename T2>
87 localTransform = rhs.localTransform.template cast<T>();
88 transform = rhs.transform.template cast<T>();
89
91 rotationAxis = rhs.rotationAxis.template cast<T>();
92
94}
95
96template struct JointStateT<float>;
97template struct JointStateT<double>;
98
99template void JointStateT<float>::set(const JointStateT<float>& rhs);
100template void JointStateT<float>::set(const JointStateT<double>& rhs);
101template void JointStateT<double>::set(const JointStateT<float>& rhs);
102template void JointStateT<double>::set(const JointStateT<double>& rhs);
103
104} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
Eigen::Matrix< T, kParametersPerJoint, 1 > JointVectorT
Definition types.h:27
Eigen::AngleAxis< T > AngleAxis
Definition types.h:374
constexpr T ln2() noexcept
Returns the natural logarithm of 2 (log_e 2).
Definition constants.h:40
Eigen::Quaternion< T > Quaternion
Unit quaternion representing a 3D rotation.
Definition types.h:385
Vector< T, 3 > Vector3
Definition types.h:236
const std::array< Vector3< T >, 3 > RotationAxis
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.
TransformT< T > localTransform
Local transformation relative to parent joint.
Definition joint_state.h:54
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.
Matrix3< T > rotationAxis
Rotation axes in global space.
Definition joint_state.h:69
Matrix3< T > translationAxis
Translation axes in global space.
Definition joint_state.h:64
TransformT< T > transform
Global transformation in world space.
Definition joint_state.h:59
void set(const JointT< T > &joint, const JointVectorT< T > &parameters, const JointStateT< T > *parentState=nullptr, bool computeDeriv=true) noexcept
Updates joint state based on joint definition and parameters.
bool derivDirty
Indicates if derivative data needs recomputation.
Definition joint_state.h:74
A simplified version of a Maya joint with pre-rotation only (no post-rotation).
Definition joint.h:18
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36
Quaternion< T > rotation
Unit quaternion encoding the rotation.
Definition transform.h:39