Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
transform.h
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
8#pragma once
9
10#include <momentum/math/types.h>
11
12#include <Eigen/Core>
13#include <Eigen/Geometry>
14
15namespace momentum {
16
35template <typename T>
36struct TransformT {
43
45 [[nodiscard]] static TransformT<T> makeRotation(const Quaternion<T>& rotation_in);
46
48 [[nodiscard]] static TransformT<T> makeTranslation(const Vector3<T>& translation_in);
49
51 [[nodiscard]] static TransformT<T> makeScale(const T& scale_in);
52
60 [[nodiscard]] static TransformT<T>
61 makeRandom(bool translation = true, bool rotation = true, bool scale = true);
62
66 [[nodiscard]] static TransformT<T> fromAffine3(const Affine3<T>& other);
67
71 [[nodiscard]] static TransformT<T> fromMatrix(const Matrix4<T>& other);
72
74 TransformT() : rotation(Quaternion<T>::Identity()), translation(Vector3<T>::Zero()), scale(1) {}
75
77 template <typename T2>
78 explicit TransformT(const TransformT<T2>& other)
79 : rotation(other.rotation.template cast<T>()),
80 translation(other.translation.template cast<T>()),
81 scale(other.scale) {}
82
83 explicit TransformT(
84 const Vector3<T>& translation_in,
85 const Quaternion<T>& rotation_in = Quaternion<T>::Identity(),
86 const T& scale_in = T(1))
87 : rotation(rotation_in), translation(translation_in), scale(scale_in) {}
88
89 explicit TransformT(
90 Vector3<T>&& translation_in,
91 Quaternion<T>&& rotation_in = Quaternion<T>::Identity(),
92 T&& scale_in = T(1))
93 : rotation(std::move(rotation_in)),
94 translation(std::move(translation_in)),
95 scale(std::move(scale_in)) {}
96
98 explicit TransformT(const Affine3<T>& other) {
99 *this = fromAffine3(other);
100 }
101
103 explicit TransformT(const Matrix4<T>& other) {
104 *this = fromMatrix(other);
105 }
106
109 *this = fromAffine3(other);
110 return *this;
111 }
112
115 *this = fromMatrix(other);
116 return *this;
117 }
118
124 [[nodiscard]] TransformT<T> operator*(const TransformT<T>& other) const {
125 const Vector3<T> trans = translation + rotation * (scale * other.translation);
126 const Quaternion<T> rot = rotation * other.rotation;
127 const T newScale = scale * other.scale;
128 return TransformT<T>(std::move(trans), std::move(rot), std::move(newScale));
129 }
130
133 [[nodiscard]] Affine3<T> operator*(const Affine3<T>& other) const {
135 out.linear().noalias() = toLinear() * other.linear();
136 out.translation().noalias() = rotation * (scale * other.translation()) + translation;
137 return out;
138 }
139
140 [[nodiscard]] Vector3<T> operator*(const Vector3<T>& other) const {
141 return transformPoint(other);
142 }
143
144 explicit operator Affine3<T>() const {
145 return toAffine3();
146 }
147
148 [[nodiscard]] Affine3<T> toAffine3() const;
149
151 [[nodiscard]] Matrix4<T> toMatrix() const {
153 out.template topLeftCorner<3, 3>().noalias() = rotation.toRotationMatrix() * scale;
154 out.template topRightCorner<3, 1>() = translation;
155 out(3, 3) = T(1);
156 return out;
157 }
158
160 [[nodiscard]] Matrix3<T> toRotationMatrix() const {
161 return rotation.toRotationMatrix();
162 }
163
165 [[nodiscard]] Matrix3<T> toLinear() const {
166 return toRotationMatrix() * scale;
167 }
168
173 [[nodiscard]] Vector3<T> getAxisX() const {
174 Vector3<T> axis;
175
176 const T ty = T(2) * rotation.y();
177 const T tz = T(2) * rotation.z();
178 const T twy = ty * rotation.w();
179 const T twz = tz * rotation.w();
180 const T txy = ty * rotation.x();
181 const T txz = tz * rotation.x();
182 const T tyy = ty * rotation.y();
183 const T tzz = tz * rotation.z();
184
185 axis[0] = T(1) - (tyy + tzz);
186 axis[1] = txy + twz;
187 axis[2] = txz - twy;
188
189 return axis;
190 }
191
193 [[nodiscard]] Vector3<T> transformPoint(const Vector3<T>& pt) const {
194 return translation + rotation * (scale * pt).eval();
195 }
196
198 [[nodiscard]] Vector3<T> rotate(const Vector3<T>& vec) const;
199
202 [[nodiscard]] TransformT<T> inverse() const;
203
205 template <typename T2>
206 [[nodiscard]] TransformT<T2> cast() const {
207 return TransformT<T2>(
208 this->translation.template cast<T2>(),
209 this->rotation.template cast<T2>(),
210 static_cast<T2>(this->scale));
211 }
212
216 // TODO: Compare components directly so the tolerance has well-defined meaning per
217 // component (and so two quaternions that differ by sign compare equal).
218 [[nodiscard]] bool isApprox(
219 const TransformT<T>& other,
220 T tol = Eigen::NumTraits<T>::dummy_precision()) const {
221 return toAffine3().isApprox(other.toAffine3(), tol);
222 }
223};
224
227template <typename T>
228using TransformListT = std::vector<TransformT<T>>;
229
232template <typename T>
234 momentum::span<const TransformT<T>> transforms,
236
239template <typename T>
240TransformT<T> slerp(const TransformT<T>& t1, const TransformT<T>& t2, T weight);
241
244
245} // namespace momentum
TransformListT< float > TransformList
Definition transform.h:243
TransformT< float > Transform
Definition fwd.h:77
std::vector< TransformT< T > > TransformListT
Per-joint transform list describing the state of all joints in a skeleton (one entry per joint,...
Definition transform.h:228
Eigen::Matrix< T, 3, 3 > Matrix3
Definition types.h:96
TransformT< T > slerp(const TransformT< T > &t1, const TransformT< T > &t2, T weight)
Spherical linear interpolation between two transforms.
Eigen::Matrix< T, 4, 4 > Matrix4
Definition types.h:98
Eigen::Quaternion< T > Quaternion
Unit quaternion representing a 3D rotation.
Definition types.h:385
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Definition span_compat.h:54
Vector< T, 3 > Vector3
Definition types.h:236
TransformT< T > blendTransforms(momentum::span< const TransformT< T > > transforms, momentum::span< const T > weights)
Weighted blend of transforms: rotations are blended via averaged quaternions, translations and scales...
Eigen::Transform< T, 3, Eigen::Affine > Affine3
3D affine transform (linear part + translation).
Definition types.h:404
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36
Vector3< T > rotate(const Vector3< T > &vec) const
Applies only the rotation to a direction vector (translation and scale are ignored).
Definition transform.cpp:88
TransformT(Vector3< T > &&translation_in, Quaternion< T > &&rotation_in=Quaternion< T >::Identity(), T &&scale_in=T(1))
Definition transform.h:89
TransformT(const Matrix4< T > &other)
Definition transform.h:103
TransformT(const Affine3< T > &other)
Definition transform.h:98
static TransformT< T > makeRotation(const Quaternion< T > &rotation_in)
Creates a transform with the given rotation, identity translation, and unit scale.
Definition transform.cpp:21
Affine3< T > toAffine3() const
Definition transform.cpp:79
TransformT(const TransformT< T2 > &other)
Definition transform.h:78
TransformT< T > operator*(const TransformT< T > &other) const
Composes transforms: (*this) * other applies other first, then *this.
Definition transform.h:124
static TransformT< T > makeRandom(bool translation=true, bool rotation=true, bool scale=true)
Creates a random transform.
Definition transform.cpp:36
static TransformT< T > fromMatrix(const Matrix4< T > &other)
Decomposes a 4x4 homogeneous matrix into rotation, translation, and uniform scale.
Definition transform.cpp:60
static TransformT< T > fromAffine3(const Affine3< T > &other)
Decomposes an Eigen::Affine3 into rotation, translation, and uniform scale.
Definition transform.cpp:55
TransformT< T > & operator=(const Matrix4< T > &other)
Definition transform.h:114
Vector3< float > translation
Definition transform.h:40
TransformT()
Creates the identity transform (zero translation, identity rotation, unit scale).
Definition transform.h:74
Vector3< T > transformPoint(const Vector3< T > &pt) const
Applies the full transform to a point: translation + rotation * (scale * pt).
Definition transform.h:193
static TransformT< T > makeTranslation(const Vector3< T > &translation_in)
Creates a transform with the given translation, identity rotation, and unit scale.
Definition transform.cpp:26
Matrix3< T > toRotationMatrix() const
Returns the rotation as a 3x3 orthogonal matrix (scale not included).
Definition transform.h:160
TransformT(const Vector3< T > &translation_in, const Quaternion< T > &rotation_in=Quaternion< T >::Identity(), const T &scale_in=T(1))
Definition transform.h:83
Vector3< T > getAxisX() const
Returns the rotated unit-X axis (i.e.
Definition transform.h:173
bool isApprox(const TransformT< T > &other, T tol=Eigen::NumTraits< T >::dummy_precision()) const
Approximate equality test using tol as a relative precision.
Definition transform.h:218
Quaternion< float > rotation
Definition transform.h:39
Matrix4< T > toMatrix() const
Returns the equivalent 4x4 homogeneous matrix [s*R | t; 0 0 0 1].
Definition transform.h:151
Vector3< T > operator*(const Vector3< T > &other) const
Definition transform.h:140
TransformT< T2 > cast() const
Definition transform.h:206
Affine3< T > operator*(const Affine3< T > &other) const
Composes with an Affine3.
Definition transform.h:133
Matrix3< T > toLinear() const
Returns scale * R, the linear part of the transform.
Definition transform.h:165
static TransformT< T > makeScale(const T &scale_in)
Creates a transform with the given scale, identity rotation, and zero translation.
Definition transform.cpp:31
TransformT< T > & operator=(const Affine3< T > &other)
Definition transform.h:108
TransformT< T > inverse() const
Returns the inverse transform such that (*this) * inverse() == identity.
Definition transform.cpp:93