SuperDex Physics C++ API
Loading...
Searching...
No Matches
quaternion_utils_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 "quaternion_utils.h" // For IntelliSense
20
21namespace superdex {
22
23/************************************************************************************
24 Quaternion Utilities
25*/
26
27MOCHI_FORCE_INLINE bool NearEqual(Quaternion const& a, Quaternion const& b, Vec4r epsilon) {
28 return NearEqual(a.data, b.data, epsilon);
29}
30
31MOCHI_FORCE_INLINE bool NearEqual(Quaternion const& a, Quaternion const& b, real epsilon) {
32 return NearEqual(a, b, Vec4r{epsilon});
33}
34
36EquivalentRotation(Quaternion const& a, Quaternion const& b, Vec4r epsilon) {
37 return NearEqual(a, b, epsilon) || NearEqual(a, -b, epsilon);
38}
39
41 return EquivalentRotation(a, b, Vec4r{epsilon});
42}
43
47
49 real dot = Dot<4>(a.data, b.data);
50
51 // If the dot product is negative, then they have opposite handedness
52 // Invert it so it gives the shortest rotation direction
53 if (dot < 0_r) {
54 dot = -dot;
55 b = -b;
56 }
57
58 // For b = a, b - a·dot below is rounding residue, which a large t would amplify.
59 if (a == b) {
60 return a;
61 }
62
63 // b = a·cos(θ) + u·sin(θ) with u a unit quaternion orthogonal to a. ACos(dot) is inaccurate for
64 // small θ and ASin(sinTheta) near π/2, so θ comes from whichever argument is smaller.
65 Vec4r const bOrtho = b.data - a.data * dot;
66 real const sinTheta = Norm<4>(bOrtho);
67 // If bOrtho is zero or its norm underflows, return the θ → 0 limit of the result below.
68 if (sinTheta == 0_r) MOCHI_UNLIKELY {
69 return Quaternion{a.data + bOrtho * t};
70 }
71 real const theta = dot < sinTheta ? ACos(dot) : ASin(sinTheta);
72 real const angle = theta * t;
73 return Quaternion{a.data * Cos(angle) + bOrtho * (Sin(angle) / sinTheta)};
74}
75
82
86
90
92 return QuaternionFromMatrix(ToNdArray3x3(matrix));
93}
94
96 // Shepperd's branches below agree only for an orthonormal matrix, so a matrix that is slightly
97 // off, e.g. from rounding, would make the result jump where the branch changes. Gram-Schmidt on
98 // the columns, keeping the first column's direction, removes that.
99 Real3 const col0 = Normalize(Real3{matrix[0][0], matrix[1][0], matrix[2][0]});
100 Real3 col1{matrix[0][1], matrix[1][1], matrix[2][1]};
101 col1 = Normalize(col1 - Dot(col1, col0) * col0);
102 Real3 const col2 = Cross(col0, col1);
103
104 // Shepperd's method on the rotation with columns col0, col1 and col2, so m_ij = colj[i].
105 // 4 w^2 = 1 + trace and 4 x^2 = 1 + 2 m00 - trace (likewise y and z), and the off-diagonal pairs
106 // give the other products, e.g. m21 - m12 = 4 x w and m01 + m10 = 4 x y. Each branch builds 4 c q
107 // for the component c with the largest 4 c^2, which is at least 1, and the normalization below
108 // divides out 4 c.
109 real const trace = col0[0] + col1[1] + col2[2];
110 real const diagMax = Max(col0[0], col1[1], col2[2]);
111 Real4 xyzw MOCHI_NO_INIT;
112 if (trace >= diagMax) { // 4 w q
113 xyzw = {col1[2] - col2[1], col2[0] - col0[2], col0[1] - col1[0], 1_r + trace};
114 } else if (col0[0] == diagMax) { // 4 x q
115 xyzw = {1_r + 2_r * col0[0] - trace, col1[0] + col0[1], col2[0] + col0[2], col1[2] - col2[1]};
116 } else if (col1[1] == diagMax) { // 4 y q
117 xyzw = {col1[0] + col0[1], 1_r + 2_r * col1[1] - trace, col2[1] + col1[2], col2[0] - col0[2]};
118 } else { // 4 z q
119 xyzw = {col2[0] + col0[2], col2[1] + col1[2], 1_r + 2_r * col2[2] - trace, col0[1] - col1[0]};
120 }
121
122 // w >= 0 as for an angle in [0, pi]
123 Quaternion const q = Normalize(Quaternion{xyzw});
124 return xyzw[3] < 0_r ? -q : q;
125}
126
128 return VMatrix3x3r{q * Vec4r(1_r, 0_r, 0_r), q * Vec4r(0_r, 1_r, 0_r), q * Vec4r(0_r, 0_r, 1_r)};
129}
130
134
138
139MOCHI_FORCE_INLINE std::pair<VMatrix3x3r, VMatrix3x3r> ToVMatrix3x3_WithTranspose(
140 Quaternion const& q) {
141 auto matT = ToVMatrix3x3Transpose(q);
142 return std::make_pair(Transpose3x3(matT), matT);
143}
144
148
152
156
160
162 return AllTrue(VIsFinite(q));
163}
164
165} // namespace superdex
Quaternion GetConjugate() const
#define MOCHI_UNLIKELY
#define MOCHI_FORCE_INLINE
#define MOCHI_NO_INIT
T Dot(Simd< T, N > a, Simd< T, N > b)
Definition simd.h:710
Quaternion Slerp(Quaternion a, Quaternion b, real t)
constexpr T ACos(T a)
Simd< T, N > VIsFinite(Simd< T, N > a)
Definition simd_inl.h:862
T NormSqr(Simd< T, N > a)
Definition simd_inl.h:964
constexpr T Sin(T a)
Matrix3x3r ToMatrix3x3(Quaternion const &q)
T Norm(Simd< T, N > a)
Definition simd_inl.h:969
bool AllTrue(T const &a)
Definition basic_utils.h:60
NdArray< Simd< real, 4 >, 3 > VMatrix3x3r
Definition vmatrix.h:52
NdArray< T, 3, 3 > ToNdArray3x3(NdArray< Simd< T, 4 >, 3 > m)
Definition vmatrix.h:375
NdArray< Simd< T, 4 >, 3 > Transpose3x3(NdArray< Simd< T, 4 >, D0 > const &m)
Simd< real, 4 > Vec4r
Definition simd.h:206
constexpr T Cos(T a)
Simd< T, N > Normalize(Simd< T, N > a)
Definition simd_inl.h:974
constexpr NdArray< T, D0, DIMS... > operator/(NdArray< T, D0, DIMS... > const &lhs, NdArray< T, D0, DIMS... > const &rhs)
Definition nd_array.h:285
Quaternion Conjugate(Quaternion const &a)
NdArray< real, 3 > Real3
Definition nd_array.h:115
VMatrix3x3r ToVMatrix3x3Transpose(Quaternion const &q)
constexpr NdArray< T, D0, DIMS... > operator*(NdArray< T, D0, DIMS... > const &lhs, NdArray< T, D0, DIMS... > const &rhs)
Definition nd_array.h:284
constexpr T ASin(T a)
NdArray< real, 4 > Real4
Definition nd_array.h:116
VMatrix3x3r ToVMatrix3x3(Quaternion const &q)
constexpr T Lerp(T a, T b, Frac t)
constexpr T const & Max(T const &a, T const &b)
NdArray< real, 3, 3 > Matrix3x3r
Definition nd_array.h:134
bool NearEqual(TransformRT const &a, TransformRT const &b, real epsilon=kDefaultNearEqualEpsilon< real >)
std::pair< VMatrix3x3r, VMatrix3x3r > ToVMatrix3x3_WithTranspose(Quaternion const &q)
Quaternion QuaternionFromMatrix(VMatrix3x3r const &matrix)
bool IsFinite(TransformRT const &a)
bool EquivalentRotation(Quaternion const &a, Quaternion const &b, Vec4r epsilon)
constexpr NdArray< T, 3 > Cross(NdArray< T, 3 > const &a, NdArray< T, 3 > const &b)