Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
coordinate_system.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
10namespace momentum {
11
12namespace {
13
14// Returns the scale factor in meters for a given LengthUnit.
15template <typename T>
16constexpr T unitInMeters(LengthUnit u) {
17 switch (u) {
19 return T(1);
21 return T(0.1);
23 return T(0.01);
25 return T(0.001);
26 }
27 // Defensive fallback for ill-formed LengthUnit values (e.g. cast from int).
28 // All declared enumerators are handled above, so this path is unreachable for valid inputs.
29 return T(1);
30}
31
32// Computes the signed permutation matrix P that converts coordinates from
33// system 'from' to system 'to': v_to = P * v_from.
34//
35// Convention: each coordinate system maps the semantic axes
36// (right, forward, up) to world axes (x, y, z) as follows:
37// Y-up right-hand: right=+X, forward=-Z, up=+Y (OpenGL-style)
38// Z-up right-hand: right=+X, forward=+Y, up=+Z (Blender / robotics-style)
39// X-up right-hand: right=+Y, forward=+Z, up=+X
40// For left-handed systems, only the forward axis is negated; right and up
41// keep the same world directions. This is the minimal flip that preserves
42// up alignment while reversing chirality.
43//
44// Note: only handedness changes the sign of det(axes); axis permutation
45// alone is always a proper rotation. The forward-only flip means
46// det(axes) = -1 iff Handedness::Left, so P keeps right vectors right
47// and up vectors up — only forward-pointing components flip when the
48// handedness differs between 'from' and 'to'.
49//
50// The change-of-basis matrix is: P = toAxes * fromAxes^T
51// where each column of 'axes' is the world-space direction of a semantic
52// axis: col(0)=right, col(1)=forward, col(2)=up. So 'axes' maps a vector
53// expressed in (right, forward, up) coordinates into world (x, y, z),
54// and 'axes^T' is its inverse (the matrix is orthogonal by construction).
55template <typename T>
56Matrix3<T> permutationMatrix(const CoordinateSystem& from, const CoordinateSystem& to) {
57 // Returns the 3x3 matrix whose columns are the world-axis directions of
58 // the (right, forward, up) semantic axes for the given (up, handedness).
59 auto getAxes = [](UpAxis up, Handedness hand) -> Matrix3<T> {
60 Matrix3<T> m;
61 switch (up) {
62 case UpAxis::Y:
63 m.col(0) = Vector3<T>(1, 0, 0); // right = +X
64 // forward = -Z (right-handed) or +Z (left-handed)
65 m.col(1) = (hand == Handedness::Right) ? Vector3<T>(0, 0, -1) : Vector3<T>(0, 0, 1);
66 m.col(2) = Vector3<T>(0, 1, 0); // up = +Y
67 break;
68 case UpAxis::Z:
69 m.col(0) = Vector3<T>(1, 0, 0); // right = +X
70 // forward = +Y (right-handed) or -Y (left-handed)
71 m.col(1) = (hand == Handedness::Right) ? Vector3<T>(0, 1, 0) : Vector3<T>(0, -1, 0);
72 m.col(2) = Vector3<T>(0, 0, 1); // up = +Z
73 break;
74 case UpAxis::X:
75 m.col(0) = Vector3<T>(0, 1, 0); // right = +Y
76 // forward = +Z (right-handed) or -Z (left-handed)
77 m.col(1) = (hand == Handedness::Right) ? Vector3<T>(0, 0, 1) : Vector3<T>(0, 0, -1);
78 m.col(2) = Vector3<T>(1, 0, 0); // up = +X
79 break;
80 }
81 return m;
82 };
83
84 const Matrix3<T> fromAxes = getAxes(from.up, from.hand);
85 const Matrix3<T> toAxes = getAxes(to.up, to.hand);
86 // 'axes' is orthogonal (columns are unit basis vectors with at most a sign
87 // flip), so transpose() == inverse() — no need for a numeric inverse.
88 return toAxes * fromAxes.transpose();
89}
90
91} // namespace
92
93template <typename T>
95 return unitInMeters<T>(from.unit) / unitInMeters<T>(to.unit);
96}
97
98template <typename T>
100changeVector(const Vector3<T>& v, const CoordinateSystem& from, const CoordinateSystem& to) {
101 const T s = scaleFactor<T>(from, to);
102 const Matrix3<T> P = permutationMatrix<T>(from, to);
103 return s * (P * v);
104}
105
106template <typename T>
109 // Round-trip via rotation matrix: this transparently handles handedness
110 // flips. P*R*P^T has det = det(P)^2 * det(R) = +1 regardless of whether
111 // 'from' and 'to' share handedness, so the result is always a proper
112 // rotation that Eigen's Quaternion(Matrix3) constructor can normalize.
113 const Matrix3<T> R = q.toRotationMatrix();
114 const Matrix3<T> newR = changeRotationMatrix(R, from, to);
115 return Quaternion<T>(newR);
116}
117
118template <typename T>
120 const Matrix3<T>& m,
121 const CoordinateSystem& from,
122 const CoordinateSystem& to) {
123 const Matrix3<T> P = permutationMatrix<T>(from, to);
124 return P * m * P.transpose();
125}
126
127// --- Convenience wrappers ---
128
129template <typename T>
133
134template <typename T>
138
139template <typename T>
143
144template <typename T>
148
149template <typename T>
153
154template <typename T>
158
159// --- Explicit template instantiations ---
160
163
164template Vector3<float>
166template Vector3<double>
168
169template Quaternion<float>
171template Quaternion<double>
173
174template Matrix3<float>
176template Matrix3<double>
178
183
186 const Quaternion<double>&,
187 const CoordinateSystem&);
189 const Quaternion<float>&,
190 const CoordinateSystem&);
192 const Quaternion<double>&,
193 const CoordinateSystem&);
194
199 const Matrix3<double>&,
200 const CoordinateSystem&);
201
202} // namespace momentum
Quaternion< T > changeQuaternion(const Quaternion< T > &q, const CoordinateSystem &from, const CoordinateSystem &to)
Converts a quaternion rotation between coordinate systems.
template float scaleFactor< float >(const CoordinateSystem &, const CoordinateSystem &)
Matrix3< T > fromMomentumRotationMatrix(const Matrix3< T > &m, const CoordinateSystem &to)
Converts a 3x3 rotation matrix from Momentum's canonical coordinate system into to.
Eigen::Matrix< T, 3, 3 > Matrix3
Definition types.h:96
Vector3< T > toMomentumVector(const Vector3< T > &v, const CoordinateSystem &from)
Converts a position vector from from into Momentum's canonical coordinate system.
LengthUnit
Specifies the unit of length measurement.
Vector3< T > fromMomentumVector(const Vector3< T > &v, const CoordinateSystem &to)
Converts a position vector from Momentum's canonical coordinate system into to.
Quaternion< T > toMomentumQuaternion(const Quaternion< T > &q, const CoordinateSystem &from)
Converts a quaternion rotation from from into Momentum's canonical coordinate system.
Quaternion< T > fromMomentumQuaternion(const Quaternion< T > &q, const CoordinateSystem &to)
Converts a quaternion rotation from Momentum's canonical coordinate system into to.
Eigen::Quaternion< T > Quaternion
Unit quaternion representing a 3D rotation.
Definition types.h:385
Handedness
Specifies the handedness (chirality) of a coordinate system.
template double scaleFactor< double >(const CoordinateSystem &, const CoordinateSystem &)
Matrix3< T > changeRotationMatrix(const Matrix3< T > &m, const CoordinateSystem &from, const CoordinateSystem &to)
Converts a 3x3 rotation matrix between coordinate systems.
UpAxis
Specifies which axis points "up" in a 3D coordinate system.
constexpr CoordinateSystem kMomentumCoordinateSystem
Momentum's canonical coordinate system: Y-up, right-handed, centimeters.
Vector< T, 3 > Vector3
Definition types.h:236
Vector3< T > changeVector(const Vector3< T > &v, const CoordinateSystem &from, const CoordinateSystem &to)
Converts a 3D position vector between coordinate systems.
T scaleFactor(const CoordinateSystem &from, const CoordinateSystem &to)
Returns the length scale factor when converting a length from from's unit to to's unit.
Matrix3< T > toMomentumRotationMatrix(const Matrix3< T > &m, const CoordinateSystem &from)
Converts a 3x3 rotation matrix from from into Momentum's canonical coordinate system.
A 3D coordinate system defined by its up axis, handedness, and length unit.