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 <cmath>
22#include <limits>
23
24namespace superdex {
25
26/************************************************************************************
27 TransformRT Inlines
28*/
29
30inline void TransformRT::WarnIfRotationNotNormalized() const {
31 // Log a warning in debug builds if the rotation is not unit length. This does not use
32 // MOCHI_ASSERT_VERBOSE because we don't want it to be fatal, especially in external code.
33#if MOCHI_ASSERT_VERBOSE_ENABLED && MOCHI_LOG_ENABLED
34 auto norm = Norm(_rotation);
35 if (!NearEqual(norm, 1_r, kQuaternionTol)) MOCHI_UNLIKELY {
37 "TransformRT expects a unit length quaternion, but got [%g, %g, %g, %g] (magnitude %g).",
38 _rotation.data[0],
39 _rotation.data[1],
40 _rotation.data[2],
41 _rotation.data[3],
42 norm);
43 }
44#endif
45}
46
48 : _rotation(rotation), _translation(ToReal3(translation)) {
49 WarnIfRotationNotNormalized();
50}
51
52MOCHI_FORCE_INLINE TransformRT::TransformRT(Quaternion const& rotation, Real3 const& translation)
53 : _rotation(rotation), _translation(translation) {
54 WarnIfRotationNotNormalized();
55}
56
57MOCHI_FORCE_INLINE TransformRT::TransformRT(Quaternion const& rotation) : _rotation(rotation) {
58 WarnIfRotationNotNormalized();
59}
60
62 : _translation(ToReal3(translation)) {}
63
64MOCHI_FORCE_INLINE TransformRT::TransformRT(Real3 const& translation) : _translation(translation) {}
65
69
71 // Note: Implementation adapted from RTech's <graphics/vector.h>
72 // For a faster implementation, it might be worth measuring the
73 // performance compared to the raw assembly code described here:
74 // https://www.fd.cvut.cz/personal/voracsar/geometriepg/pgr020/matrix2quaternions.pdf
75 alignas(alignof(Vec4r)) real x[4];
76 alignas(alignof(Vec4r)) real y[4];
77 alignas(alignof(Vec4r)) real z[4];
78 VMatrix4x4r const& matColumns = mT;
79 Store(x, matColumns[0]);
80 Store(y, matColumns[1]);
81 Store(z, matColumns[2]);
82
83 alignas(alignof(Vec4r)) real q[4];
84 int k0 = 0, k1 = 0, k2 = 0, k3 = 0;
85 real s0 = 0_r, s1 = 0_r, s2 = 0_r;
86 if (x[0] + y[1] + z[2] > 0_r) {
87 k0 = 3;
88 k1 = 2;
89 k2 = 1;
90 k3 = 0;
91 s0 = 1.0_r;
92 s1 = 1.0_r;
93 s2 = 1.0_r;
94 } else if (x[0] > y[1] && x[0] > z[2]) {
95 k0 = 0;
96 k1 = 1;
97 k2 = 2;
98 k3 = 3;
99 s0 = 1.0_r;
100 s1 = -1.0_r;
101 s2 = -1.0_r;
102 } else if (y[1] > z[2]) {
103 k0 = 1;
104 k1 = 0;
105 k2 = 3;
106 k3 = 2;
107 s0 = -1.0_r;
108 s1 = 1.0_r;
109 s2 = -1.0_r;
110 } else {
111 k0 = 2;
112 k1 = 3;
113 k2 = 0;
114 k3 = 1;
115 s0 = -1.0_r;
116 s1 = -1.0_r;
117 s2 = 1.0_r;
118 }
119 real t = (s0 * x[0]) + (s1 * y[1]) + (s2 * z[2]) + 1.0_r;
120 real s = (1.0_r / std::sqrt(t)) * 0.5_r;
121 q[k0] = s * t;
122 q[k1] = (x[1] - s2 * y[0]) * s;
123 q[k2] = (z[0] - s1 * x[2]) * s;
124 q[k3] = (y[2] - s0 * z[1]) * s;
125
126 return TransformRT{Quaternion{q[0], q[1], q[2], q[3]}, matColumns[3]};
127}
128
132
134 return _rotation;
135}
136
138 _rotation = rotation;
139 WarnIfRotationNotNormalized();
140}
141
143 return ToSimd(_translation, 1_r);
144}
145
147 return _translation;
148}
149
151 _translation = ToReal3(translation);
152}
153
155 _translation = translation;
156}
157
159 return _rotation == rhs._rotation && _translation == rhs._translation;
160}
161
163 return !(*this == rhs);
164}
165
167 return TransformRT{
168 this->GetRotation() * rhs.GetRotation(),
169 this->VGetTranslation() + this->GetRotation() * rhs.VGetTranslation()};
170}
171
173 *this = (*this) * rhs;
174 return *this;
175}
176
178 return ToReal3(_rotation * ToSimd(pt, 0_r) + VGetTranslation());
179}
180
182 return ToReal3(_rotation * ToSimd(dir, 0_r));
183}
184
186 return ToReal3(Conjugate(_rotation) * (ToSimd(pt, 0_r) - VGetTranslation()));
187}
188
190 return ToReal3(Conjugate(_rotation) * ToSimd(dir, 0_r));
191}
192
194 return _rotation * ToSimdDirection(pt) + VGetTranslation();
195}
196
198 return _rotation * ToSimdDirection(dir);
199}
200
204
208
209/************************************************************************************
210 Utilities
211*/
212
214 Quaternion q = a.GetRotation();
215 return VMatrix4x4r{
216 q * Vec4r(1.0_r, 0.0_r, 0.0_r),
217 q * Vec4r(0.0_r, 1.0_r, 0.0_r),
218 q * Vec4r(0.0_r, 0.0_r, 1.0_r),
219 a.VGetTranslation()};
220}
221
225
226MOCHI_FORCE_INLINE bool NearEqual(TransformRT const& a, TransformRT const& b, Vec4r epsilon) {
227 return NearEqual(a.GetRotation(), b.GetRotation(), epsilon) &&
228 NearEqual(a.VGetTranslation(), b.VGetTranslation(), epsilon);
229}
230
231MOCHI_FORCE_INLINE bool NearEqual(TransformRT const& a, TransformRT const& b, real epsilon) {
232 return NearEqual(a, b, Vec4r{epsilon});
233}
234
238
240 return AllTrue(VIsFinite(a));
241}
242
244 Quaternion rotation = Conjugate(a.GetRotation());
245 Vec4r translation = rotation * ToSimdDirection(-a.VGetTranslation());
246 return TransformRT{rotation, translation};
247}
248
252
253MOCHI_FORCE_INLINE TransformRT Repivot(TransformRT const& transform, Real3 const& pivot) {
254 return TransformRT(
255 transform.GetRotation(),
256 transform.GetTranslation() - pivot + transform.GetRotation() * pivot);
257}
258
259inline TransformRT Interpolate(TransformRT const& a, TransformRT const& b, real t) {
260 auto trans = a.GetTranslation() * (1.0_r - t) + b.GetTranslation() * t;
261 auto rot = Slerp(a.GetRotation(), b.GetRotation(), t);
262 return TransformRT(rot, trans);
263}
264
265inline std::pair<Real3, TransformRT> DecomposeMatrixTransform(VMatrix4x4r const& matrixTransform) {
266 // Extract translation and scale using transpose to access columns
267 auto matrixT = Transpose4x4(matrixTransform);
268 auto translation = ToReal3(matrixT[3]);
269 auto scale = Real3{
270 Norm<3>(matrixT[0]), // Column 0
271 Norm<3>(matrixT[1]), // Column 1
272 Norm<3>(matrixT[2]) // Column 2
273 };
274
275 // Calculate 3x3 determinant to detect negative scaling (reflection)
276 auto determinant = Det3x3(matrixT);
277 if (determinant < 0_r) {
278 scale[0] = -scale[0]; // Flip the sign of any one axis.
279 }
280
281 // Normalize columns to extract rotation
282 matrixT[0] /= scale[0] + std::numeric_limits<real>::min();
283 matrixT[1] /= scale[1] + std::numeric_limits<real>::min();
284 matrixT[2] /= scale[2] + std::numeric_limits<real>::min();
285 auto rotation = QuaternionFromMatrix(Transpose3x3(matrixT));
286
287 return std::make_pair(scale, TransformRT{rotation, translation});
288}
289
290} // 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
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:834
Simd< T, 4 > ToSimd(NdArray< T, 2 > const &v, T z=T(0), T w=T(0))
Definition vmatrix.h:413
T Norm(Simd< T, N > a)
Definition simd_inl.h:941
bool AllTrue(T const &a)
Definition basic_utils.h:60
V ToSimdDirection(V a)
Definition simd_inl.h:180
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:946
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
TransformRT Repivot(TransformRT const &transform, Real3 const &pivot)
VMatrix4x4r ToVMatrix4x4Transpose(TransformRT const &a)
void Store(T *ptr, Simd< T, N > a)
Definition simd_inl.h:220
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, real eps=1e-3_r)
V ToSimdPoint(V a)
Definition simd_inl.h:175
bool IsFinite(TransformRT const &a)