SuperDex Physics C++ API
Loading...
Searching...
No Matches
quaternion_utils.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
22
23#include <utility>
24
25namespace superdex {
26
27/************************************************************************************
28 Quaternion Utilities
29*/
30
31// (abs(a-b) <= epsilon)
32[[nodiscard]] MOCHI_FORCE_INLINE bool
33NearEqual(Quaternion const& a, Quaternion const& b, Vec4r epsilon);
34[[nodiscard]] MOCHI_FORCE_INLINE bool
36
37// Similar to NearEqual but EquivalentRotation(q, -q) is also true
38[[nodiscard]] MOCHI_FORCE_INLINE bool
39EquivalentRotation(Quaternion const& a, Quaternion const& b, Vec4r epsilon);
40[[nodiscard]] MOCHI_FORCE_INLINE bool EquivalentRotation(
41 Quaternion const& a,
42 Quaternion const& b,
44
45// Scalar multiplication
49
50// Linear interpolation. The faction t is not clamped.
52
53// Spherical linear interpolation of two Quaternions.
54// Assumes a and b are normalized. The fraction t is not clamped
55[[nodiscard]] inline Quaternion Slerp(Quaternion a, Quaternion b, real t);
56
57// Squared magnitude
59
60// Vector magnitude
61[[nodiscard]] MOCHI_FORCE_INLINE real Norm(Quaternion a);
62
63// Make unit length
65
66// Conjugate Quaternion (opposite rotation)
68
69// Construct a quaternion from a rotation matrix. Must be orthonormal.
70// eps is used to determine whether the angle is close to +/- 180deg
71[[nodiscard]] inline Quaternion QuaternionFromMatrix(VMatrix3x3r const& matrix, real eps = 1e-3_r);
72[[nodiscard]] inline Quaternion QuaternionFromMatrix(Matrix3x3r const& matrix, real eps = 1e-3_r);
73
74// Compute a Matrix3x3r from Quaternion. The resulting matrix can be used to rotate vectors by
75// calling DotMatVec3x3(VMatrix3x3r, Vec4r), which is equivalent to (Quaternion * Real3). However,
76// if you have to transform multiple points, consider using ToVMatrix3x3Transpose to get the
77// transpose, so that you can then use DotVecMat3x3 instead (faster).
79
80// Equivalent to Transpose3x3(ToVMatrix3x3(q)) only faster.
82
83// Compute a Matrix3x3r from Quaternion
85
86// One stop shopping in case you need both the matrix and its transpose.
87// Example:
88// auto [R, RT] = ToVMatrix3x3_WithTranspose(q);
89[[nodiscard]] MOCHI_FORCE_INLINE std::pair<VMatrix3x3r, VMatrix3x3r> ToVMatrix3x3_WithTranspose(
90 Quaternion const& q);
91
92// Return true if all values are finite
93[[nodiscard]] MOCHI_FORCE_INLINE Vec4r VIsFinite(Quaternion const& q);
94[[nodiscard]] MOCHI_FORCE_INLINE bool IsFinite(Quaternion const& q);
95
96} // namespace superdex
97
#define MOCHI_FORCE_INLINE
Quaternion Slerp(Quaternion a, Quaternion b, real t)
Simd< T, N > VIsFinite(Simd< T, N > a)
Definition simd_inl.h:755
T NormSqr(Simd< T, N > a)
Definition simd_inl.h:849
Matrix3x3r ToMatrix3x3(Quaternion const &q)
constexpr T kDefaultNearEqualEpsilon
T Norm(Simd< T, N > a)
Definition simd_inl.h:854
NdArray< Simd< real, 4 >, 3 > VMatrix3x3r
Definition vmatrix.h:52
Simd< real, 4 > Vec4r
Definition simd.h:206
Simd< T, N > Normalize(Simd< T, N > a)
Definition simd_inl.h:859
constexpr NdArray< T, D0, DIMS... > operator/(NdArray< T, D0, DIMS... > const &lhs, NdArray< T, D0, DIMS... > const &rhs)
Definition nd_array.h:287
Quaternion Conjugate(Quaternion const &a)
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:286
VMatrix3x3r ToVMatrix3x3(Quaternion const &q)
constexpr T Lerp(T a, T b, Frac t)
NdArray< real, 3, 3 > Matrix3x3r
Definition nd_array.h:125
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, real eps=1e-3_r)
bool IsFinite(TransformRT const &a)
bool EquivalentRotation(Quaternion const &a, Quaternion const &b, Vec4r epsilon)