SuperDex Physics C++ API
Loading...
Searching...
No Matches
transform_rt_inl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <mochi_core/utils/transform_rt.h> // for intellisense
20
21#include <limits>
22
23namespace superdex {
24
25/************************************************************************************
26 TransformRT Inlines
27*/
28
29inline void TransformRT::WarnIfRotationNotNormalized() const {
30 // Log a warning in debug builds if the rotation is not unit length. This does not use
31 // MOCHI_ASSERT_VERBOSE because we don't want it to be fatal, especially in external code.
32#if MOCHI_ASSERT_VERBOSE_ENABLED && MOCHI_LOG_ENABLED
33 auto norm = Norm(_rotation);
34 if (!NearEqual(norm, 1_r, kQuaternionTol)) MOCHI_UNLIKELY {
36 "TransformRT expects a unit length quaternion, but got [%g, %g, %g, %g] (magnitude %g).",
37 _rotation.data[0],
38 _rotation.data[1],
39 _rotation.data[2],
40 _rotation.data[3],
41 norm);
42 }
43#endif
44}
45
47 : _rotation(rotation), _translation(ToReal3(translation)) {
48 WarnIfRotationNotNormalized();
49}
50
51MOCHI_FORCE_INLINE TransformRT::TransformRT(Quaternion const& rotation, Real3 const& translation)
52 : _rotation(rotation), _translation(translation) {
53 WarnIfRotationNotNormalized();
54}
55
56MOCHI_FORCE_INLINE TransformRT::TransformRT(Quaternion const& rotation) : _rotation(rotation) {
57 WarnIfRotationNotNormalized();
58}
59
61 : _translation(ToReal3(translation)) {}
62
63MOCHI_FORCE_INLINE TransformRT::TransformRT(Real3 const& translation) : _translation(translation) {}
64
68
72
76
78 return _rotation;
79}
80
82 _rotation = rotation;
83 WarnIfRotationNotNormalized();
84}
85
87 return ToSimd(_translation, 1_r);
88}
89
91 return _translation;
92}
93
95 _translation = ToReal3(translation);
96}
97
99 _translation = translation;
100}
101
103 return _rotation == rhs._rotation && _translation == rhs._translation;
104}
105
107 return !(*this == rhs);
108}
109
111 return TransformRT{
112 this->GetRotation() * rhs.GetRotation(),
113 this->VGetTranslation() + this->GetRotation() * rhs.VGetTranslation()};
114}
115
117 *this = (*this) * rhs;
118 return *this;
119}
120
122 return ToReal3(_rotation * ToSimd(pt, 0_r) + VGetTranslation());
123}
124
126 return ToReal3(_rotation * ToSimd(dir, 0_r));
127}
128
130 return ToReal3(Conjugate(_rotation) * (ToSimd(pt, 0_r) - VGetTranslation()));
131}
132
134 return ToReal3(Conjugate(_rotation) * ToSimd(dir, 0_r));
135}
136
138 return _rotation * ToSimdDirection(pt) + VGetTranslation();
139}
140
142 return _rotation * ToSimdDirection(dir);
143}
144
148
152
153/************************************************************************************
154 Utilities
155*/
156
158 Quaternion q = a.GetRotation();
159 return VMatrix4x4r{
160 q * Vec4r(1.0_r, 0.0_r, 0.0_r),
161 q * Vec4r(0.0_r, 1.0_r, 0.0_r),
162 q * Vec4r(0.0_r, 0.0_r, 1.0_r),
163 a.VGetTranslation()};
164}
165
169
170MOCHI_FORCE_INLINE bool NearEqual(TransformRT const& a, TransformRT const& b, Vec4r epsilon) {
171 return NearEqual(a.GetRotation(), b.GetRotation(), epsilon) &&
172 NearEqual(a.VGetTranslation(), b.VGetTranslation(), epsilon);
173}
174
175MOCHI_FORCE_INLINE bool NearEqual(TransformRT const& a, TransformRT const& b, real epsilon) {
176 return NearEqual(a, b, Vec4r{epsilon});
177}
178
182
184 return AllTrue(VIsFinite(a));
185}
186
188 Quaternion rotation = Conjugate(a.GetRotation());
189 Vec4r translation = rotation * ToSimdDirection(-a.VGetTranslation());
190 return TransformRT{rotation, translation};
191}
192
196
197MOCHI_FORCE_INLINE TransformRT Repivot(TransformRT const& transform, Real3 const& pivot) {
198 return TransformRT(
199 transform.GetRotation(),
200 transform.GetTranslation() - pivot + transform.GetRotation() * pivot);
201}
202
203inline TransformRT Interpolate(TransformRT const& a, TransformRT const& b, real t) {
204 auto trans = a.GetTranslation() * (1.0_r - t) + b.GetTranslation() * t;
205 auto rot = Slerp(a.GetRotation(), b.GetRotation(), t);
206 return TransformRT(rot, trans);
207}
208
209inline std::pair<Real3, TransformRT> DecomposeMatrixTransform(VMatrix4x4r const& matrixTransform) {
210 // Extract translation and scale using transpose to access columns
211 auto matrixT = Transpose4x4(matrixTransform);
212 auto translation = ToReal3(matrixT[3]);
213 auto scale = Real3{
214 Norm<3>(matrixT[0]), // Column 0
215 Norm<3>(matrixT[1]), // Column 1
216 Norm<3>(matrixT[2]) // Column 2
217 };
218
219 // Calculate 3x3 determinant to detect negative scaling (reflection)
220 auto determinant = Det3x3(matrixT);
221 if (determinant < 0_r) {
222 scale[0] = -scale[0]; // Flip the sign of any one axis.
223 }
224
225 // Normalize columns to extract rotation
226 matrixT[0] /= scale[0] + std::numeric_limits<real>::min();
227 matrixT[1] /= scale[1] + std::numeric_limits<real>::min();
228 matrixT[2] /= scale[2] + std::numeric_limits<real>::min();
229
230 // Zero scales leave their columns zero. Complete the rotation from the remaining columns.
231 for (int i = 0; i < 3; ++i) {
232 int const j = (i + 1) % 3;
233 int const k = (i + 2) % 3;
234 if (scale[i] == 0_r && scale[j] != 0_r && scale[k] != 0_r) {
235 matrixT[i] = Cross3(matrixT[j], matrixT[k]);
236 } else if (scale[i] != 0_r && scale[j] == 0_r && scale[k] == 0_r) {
237 matrixT[j] = Normalize<3>(OrthogonalVector3(matrixT[i]));
238 matrixT[k] = Cross3(matrixT[i], matrixT[j]);
239 }
240 }
241 auto rotation = QuaternionFromMatrix(Transpose3x3(matrixT));
242
243 return std::make_pair(scale, TransformRT{rotation, translation});
244}
245
246} // namespace superdex
static TransformRT Identity()
Real3 TransformPointInverse(Real3 const &pt) const
Quaternion const & GetRotation() const
Vec4r VGetTranslation() const
Real3 TransformDirectionInverse(Real3 const &dir) const
bool operator!=(TransformRT const &rhs) const
Real3 TransformDirection(Real3 const &dir) const
TransformRT operator*(TransformRT const &rhs) const
Real3 GetTranslation() const
void SetTranslation(Real3 translation)
TransformRT & operator*=(TransformRT const &rhs)
bool operator==(TransformRT const &rhs) const
static TransformRT FromOrthoNormalTranspose(VMatrix4x4r const &mT)
Real3 TransformPoint(Real3 const &pt) const
static TransformRT FromOrthoNormal(VMatrix4x4r const &m)
void SetRotation(Quaternion const &rotation)
#define MOCHI_LOG_WARNING(...)
Definition log.h:43
#define MOCHI_UNLIKELY
#define MOCHI_FORCE_INLINE
Quaternion Slerp(Quaternion a, Quaternion b, real t)
Simd< T, N > VIsFinite(Simd< T, N > a)
Definition simd_inl.h:862
Simd< T, 4 > ToSimd(NdArray< T, 2 > const &v, T z=T(0), T w=T(0))
Definition vmatrix.h:413
Simd< T, N > Cross3(Simd< T, N > a, Simd< T, N > b)
Definition simd_inl.h:947
T Norm(Simd< T, N > a)
Definition simd_inl.h:969
bool AllTrue(T const &a)
Definition basic_utils.h:60
V ToSimdDirection(V a)
Definition simd_inl.h:181
Real3 ToReal3(Simd< T, 4 > v)
Definition vmatrix.h:354
NdArray< Simd< T, 4 >, 3 > Transpose3x3(NdArray< Simd< T, 4 >, D0 > const &m)
Simd< real, 4 > Vec4r
Definition simd.h:206
std::pair< Real3, TransformRT > DecomposeMatrixTransform(VMatrix4x4r const &matrixTransform)
NdArray< Simd< real, 4 >, 4 > VMatrix4x4r
Definition vmatrix.h:55
Simd< T, N > Normalize(Simd< T, N > a)
Definition simd_inl.h:974
TransformRT Invert(TransformRT const &a)
VMatrix4x4r ToVMatrix4x4(TransformRT const &a)
NdArray< Simd< T, 4 >, 4 > Transpose4x4(NdArray< Simd< T, 4 >, 4 > const &m)
Quaternion Conjugate(Quaternion const &a)
NdArray< real, 3 > Real3
Definition nd_array.h:115
Simd< T, 4 > OrthogonalVector3(Simd< T, 4 > a)
TransformRT Repivot(TransformRT const &transform, Real3 const &pivot)
VMatrix4x4r ToVMatrix4x4Transpose(TransformRT const &a)
TransformRT NormalizeRotation(TransformRT const &a)
TransformRT Interpolate(TransformRT const &a, TransformRT const &b, real t)
bool NearEqual(TransformRT const &a, TransformRT const &b, real epsilon=kDefaultNearEqualEpsilon< real >)
T Det3x3(NdArray< Simd< T, 4 >, N > const &A)
Quaternion QuaternionFromMatrix(VMatrix3x3r const &matrix)
V ToSimdPoint(V a)
Definition simd_inl.h:176
bool IsFinite(TransformRT const &a)