Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::JointStateT< T > Struct Template Reference

Represents the state of a joint with local and global transformations. More...

#include <joint_state.h>

Public Member Functions

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.
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 > getTranslationDerivative (size_t index) const
 Calculates the derivative with respect to translation along a specific axis.
Vector3< T > getScaleDerivative (const Vector3< T > &ref) const noexcept
 Calculates the derivative of a point with respect to uniform scaling.
template<typename T2>
void set (const JointStateT< T2 > &rhs)
 Copies state from another JointState with potentially different scalar type.
const Quaternion< T > & localRotation () const
 Access the joint's local rotation quaternion.
Quaternion< T > & localRotation ()
 Access the joint's local rotation quaternion (mutable).
const Vector3< T > & localTranslation () const
 Access the joint's local translation vector.
Vector3< T > & localTranslation ()
 Access the joint's local translation vector (mutable).
const T & localScale () const
 Access the joint's local scale factor.
T & localScale ()
 Access the joint's local scale factor (mutable).
const Quaternion< T > & rotation () const
 Access the joint's global rotation quaternion.
Quaternion< T > & rotation ()
 Access the joint's global rotation quaternion (mutable).
const Vector3< T > & translation () const
 Access the joint's global position vector.
Vector3< T > & translation ()
 Access the joint's global position vector (mutable).
const T & x () const
 Access the X component of the global position.
T & x ()
 Access the X component of the global position (mutable).
const T & y () const
 Access the Y component of the global position.
T & y ()
 Access the Y component of the global position (mutable).
const T & z () const
 Access the Z component of the global position.
T & z ()
 Access the Z component of the global position (mutable).
const T & quatW () const
 Access the W component of the global rotation quaternion.
T & quatW ()
 Access the W component of the global rotation quaternion (mutable).
const T & quatX () const
 Access the X component of the global rotation quaternion.
T & quatX ()
 Access the X component of the global rotation quaternion (mutable).
const T & quatY () const
 Access the Y component of the global rotation quaternion.
T & quatY ()
 Access the Y component of the global rotation quaternion (mutable).
const T & quatZ () const
 Access the Z component of the global rotation quaternion.
T & quatZ ()
 Access the Z component of the global rotation quaternion (mutable).
const T & scale () const
 Access the joint's global scale factor.
T & scale ()
 Access the joint's global scale factor (mutable).

Public Attributes

TransformT< T > localTransform
 Local transformation relative to parent joint.
TransformT< T > transform
 Global transformation in world space.
Matrix3< T > translationAxis
 Translation axes in global space.
Matrix3< T > rotationAxis
 Rotation axes in global space.
bool derivDirty = true
 Indicates if derivative data needs recomputation.

Detailed Description

template<typename T>
struct momentum::JointStateT< T >

Represents the state of a joint with local and global transformations.

Joint transform hierarchy is defined as: WorldTransform = ParentWorldTransform * Tz * Ty * Tx * Rpre * R * S

Where:

  • Tx, Ty, Tz are translations along each axis
  • Rpre is the pre-rotation from the joint definition
  • R is the joint rotation (applied in order: rz * ry * rx)
  • S is the uniform scale factor

Each joint has 7 parameters: 3 translation, 3 rotation, and 1 scale.

The ordering has direct implications for derivative computation:

  • Translation Jacobian: Because translation is applied in the parent's frame (before local rotation), the translation derivative for axis i is simply translationAxis.col(i) — the parent's rotated axis direction. It does NOT depend on the joint's own rotation.
  • Rotation Jacobian: Because rotation is applied after translation and pre-rotation, the rotation derivative for a point p is rotationAxis.col(i).cross(p - jointPosition). The pre-rotation (Rpre) is baked into rotationAxis and is not a free parameter.
  • Scale Jacobian: Because scale is applied last (innermost), it scales the offset from the joint position: (p - jointPosition) * ln(2) (since scale is stored as exp2(parameter[6])).

Swapping the order (e.g., applying rotation before translation) would change which frame the translation operates in, producing incorrect Jacobians in all error functions.

Definition at line 50 of file joint_state.h.

Member Function Documentation

◆ getRotationDerivative()

template<typename T>
Vector3< T > momentum::JointStateT< T >::getRotationDerivative ( size_t index,
const Vector3< T > & ref ) const
nodiscard

Calculates the derivative of a point with respect to rotation around a specific axis.

Parameters
indexThe rotation axis index (0=X, 1=Y, 2=Z)
refThe reference point in global space
Returns
The derivative vector

Definition at line 68 of file joint_state.cpp.

References derivDirty, MT_CHECK, and rotationAxis.

Referenced by momentum::detail_sdf_collision::accumulateJointGradient(), and momentum::detail_sdf_collision::accumulateJointJacobian().

◆ getScaleDerivative()

template<typename T>
Vector3< T > momentum::JointStateT< T >::getScaleDerivative ( const Vector3< T > & ref) const
nodiscardnoexcept

Calculates the derivative of a point with respect to uniform scaling.

Parameters
refThe reference point in global space
Returns
The derivative vector

Definition at line 80 of file joint_state.cpp.

References momentum::ln2().

Referenced by momentum::detail_sdf_collision::accumulateJointGradient(), and momentum::detail_sdf_collision::accumulateJointJacobian().

◆ getTranslationDerivative()

template<typename T>
Vector3< T > momentum::JointStateT< T >::getTranslationDerivative ( size_t index) const
nodiscard

Calculates the derivative with respect to translation along a specific axis.

Parameters
indexThe translation axis index (0=X, 1=Y, 2=Z)
Returns
The derivative vector

Definition at line 74 of file joint_state.cpp.

References derivDirty, MT_CHECK, and translationAxis.

Referenced by momentum::detail_sdf_collision::accumulateJointGradient(), and momentum::detail_sdf_collision::accumulateJointJacobian().

◆ localRotation() [1/2]

template<typename T>
Quaternion< T > & momentum::JointStateT< T >::localRotation ( )
inline

Access the joint's local rotation quaternion (mutable).

Definition at line 130 of file joint_state.h.

◆ localRotation() [2/2]

template<typename T>
const Quaternion< T > & momentum::JointStateT< T >::localRotation ( ) const
inlinenodiscard

Access the joint's local rotation quaternion.

Definition at line 125 of file joint_state.h.

◆ localScale() [1/2]

template<typename T>
T & momentum::JointStateT< T >::localScale ( )
inline

Access the joint's local scale factor (mutable).

Definition at line 162 of file joint_state.h.

◆ localScale() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::localScale ( ) const
inlinenodiscard

Access the joint's local scale factor.

This scale propagates to all descendant joints

The scale parameter is stored as a log2 value and applied as exp2(parameter[6]). This means:

  • Parameter value 0.0 → scale = 1.0 (no scaling)
  • Parameter value 1.0 → scale = 2.0 (double size)
  • Parameter value -1.0 → scale = 0.5 (half size)
  • The Jacobian of scale involves exp2(param) * ln(2), not 1.0

This encoding keeps scale always positive and makes the parameter space more linear for optimization (uniform steps in log-space = uniform ratios).

Definition at line 157 of file joint_state.h.

◆ localTranslation() [1/2]

template<typename T>
Vector3< T > & momentum::JointStateT< T >::localTranslation ( )
inline

Access the joint's local translation vector (mutable).

Definition at line 140 of file joint_state.h.

◆ localTranslation() [2/2]

template<typename T>
const Vector3< T > & momentum::JointStateT< T >::localTranslation ( ) const
inlinenodiscard

Access the joint's local translation vector.

Definition at line 135 of file joint_state.h.

◆ quatW() [1/2]

template<typename T>
T & momentum::JointStateT< T >::quatW ( )
inline

Access the W component of the global rotation quaternion (mutable).

Definition at line 222 of file joint_state.h.

◆ quatW() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::quatW ( ) const
inlinenodiscard

Access the W component of the global rotation quaternion.

Definition at line 217 of file joint_state.h.

◆ quatX() [1/2]

template<typename T>
T & momentum::JointStateT< T >::quatX ( )
inline

Access the X component of the global rotation quaternion (mutable).

Definition at line 232 of file joint_state.h.

◆ quatX() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::quatX ( ) const
inlinenodiscard

Access the X component of the global rotation quaternion.

Definition at line 227 of file joint_state.h.

◆ quatY() [1/2]

template<typename T>
T & momentum::JointStateT< T >::quatY ( )
inline

Access the Y component of the global rotation quaternion (mutable).

Definition at line 242 of file joint_state.h.

◆ quatY() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::quatY ( ) const
inlinenodiscard

Access the Y component of the global rotation quaternion.

Definition at line 237 of file joint_state.h.

◆ quatZ() [1/2]

template<typename T>
T & momentum::JointStateT< T >::quatZ ( )
inline

Access the Z component of the global rotation quaternion (mutable).

Definition at line 252 of file joint_state.h.

◆ quatZ() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::quatZ ( ) const
inlinenodiscard

Access the Z component of the global rotation quaternion.

Definition at line 247 of file joint_state.h.

◆ rotation() [1/2]

template<typename T>
Quaternion< T > & momentum::JointStateT< T >::rotation ( )
inline

Access the joint's global rotation quaternion (mutable).

Definition at line 172 of file joint_state.h.

◆ rotation() [2/2]

◆ scale() [1/2]

template<typename T>
T & momentum::JointStateT< T >::scale ( )
inline

Access the joint's global scale factor (mutable).

Definition at line 264 of file joint_state.h.

◆ scale() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::scale ( ) const
inlinenodiscard

Access the joint's global scale factor.

This is the cumulative scale from all parent joints

Definition at line 259 of file joint_state.h.

◆ set() [1/2]

template<typename T>
template<typename T2>
void momentum::JointStateT< T >::set ( const JointStateT< T2 > & rhs)

Copies state from another JointState with potentially different scalar type.

Template Parameters
T2Source scalar type
Parameters
rhsSource joint state to copy from

Definition at line 86 of file joint_state.cpp.

References momentum::cast(), derivDirty, localTransform, rotationAxis, transform, and translationAxis.

◆ set() [2/2]

template<typename T>
void momentum::JointStateT< T >::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.

Recursively computes all transformations from parent to child, including local and global transforms, and optionally derivative information.

Parameters
jointThe joint definition containing offset and pre-rotation
parametersThe 7 joint parameters [tx, ty, tz, rx, ry, rz, scale]
parentStateOptional parent joint state for hierarchical transformations
computeDerivIf true, computes translationAxis and rotationAxis matrices needed for Jacobian computation in error functions. Set to false ONLY when the state is used for visualization, FK output inspection, or IO — never when the state will be passed to a solver or error function. Skipping derivatives saves ~30% of per-joint FK cost. When false, derivDirty is set to true and derivative accessors (getRotationDerivative, getTranslationDerivative, getScaleDerivative) return invalid results.

Definition at line 22 of file joint_state.cpp.

References derivDirty, localTransform, momentum::TransformT< T >::rotation, momentum::RotationAxis, rotationAxis, transform, and translationAxis.

◆ translation() [1/2]

template<typename T>
Vector3< T > & momentum::JointStateT< T >::translation ( )
inline

Access the joint's global position vector (mutable).

Definition at line 182 of file joint_state.h.

◆ translation() [2/2]

template<typename T>
const Vector3< T > & momentum::JointStateT< T >::translation ( ) const
inlinenodiscard

Access the joint's global position vector.

Definition at line 177 of file joint_state.h.

◆ x() [1/2]

template<typename T>
T & momentum::JointStateT< T >::x ( )
inline

Access the X component of the global position (mutable).

Definition at line 192 of file joint_state.h.

◆ x() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::x ( ) const
inlinenodiscard

Access the X component of the global position.

Definition at line 187 of file joint_state.h.

◆ y() [1/2]

template<typename T>
T & momentum::JointStateT< T >::y ( )
inline

Access the Y component of the global position (mutable).

Definition at line 202 of file joint_state.h.

◆ y() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::y ( ) const
inlinenodiscard

Access the Y component of the global position.

Definition at line 197 of file joint_state.h.

◆ z() [1/2]

template<typename T>
T & momentum::JointStateT< T >::z ( )
inline

Access the Z component of the global position (mutable).

Definition at line 212 of file joint_state.h.

◆ z() [2/2]

template<typename T>
const T & momentum::JointStateT< T >::z ( ) const
inlinenodiscard

Access the Z component of the global position.

Definition at line 207 of file joint_state.h.

Member Data Documentation

◆ derivDirty

template<typename T>
bool momentum::JointStateT< T >::derivDirty = true

Indicates if derivative data needs recomputation.

When true, derivative methods should not be called as they may return invalid results

Definition at line 74 of file joint_state.h.

Referenced by getRotationDerivative(), getTranslationDerivative(), set(), and set().

◆ localTransform

template<typename T>
TransformT<T> momentum::JointStateT< T >::localTransform

Local transformation relative to parent joint.

Defined by the joint parameters (translation, rotation, scale)

Definition at line 54 of file joint_state.h.

Referenced by set(), and set().

◆ rotationAxis

template<typename T>
Matrix3<T> momentum::JointStateT< T >::rotationAxis

Rotation axes in global space.

Each column represents one rotation axis (X, Y, Z) in global coordinates

Definition at line 69 of file joint_state.h.

Referenced by momentum::getRotationDerivative(), getRotationDerivative(), set(), and set().

◆ transform

◆ translationAxis

template<typename T>
Matrix3<T> momentum::JointStateT< T >::translationAxis

Translation axes in global space.

Each column represents one translation axis (X, Y, Z) in global coordinates

Definition at line 64 of file joint_state.h.

Referenced by getTranslationDerivative(), set(), and set().


The documentation for this struct was generated from the following files: