SuperDex Physics C++ API
Loading...
Searching...
No Matches
quaternion.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
25
26namespace superdex {
27
28class Quaternion;
29
30/**************************************************************************************************
31 Quaternion - Used for 3D rotations
32*/
34 public:
35 // Public data. Stored in (X,Y,Z,W) order.
37
38 // Default constructor is identity quaternion
39 Quaternion() : data{0.0_r, 0.0_r, 0.0_r, 1.0_r} {}
40 explicit Quaternion(Vec4r v) : data(v) {}
41 explicit Quaternion(Real4 const& v) : data(v[0], v[1], v[2], v[3]) {}
42 explicit Quaternion(real i, real j, real k, real r) : data(i, j, k, r) {}
43
44 static Quaternion Identity();
45 static Quaternion Zero();
46 static Quaternion RotationX(real a);
47 static Quaternion RotationY(real a);
48 static Quaternion RotationZ(real a);
49
50 // Construct a quaternion from an axis and angle.
51 [[nodiscard]] static Quaternion FromUnitAxisAngle(
52 Vec4r normalizedAxis,
53 real angleRadians); // Axis must be unit length.
54 [[nodiscard]] static Quaternion FromAxisAngle(Vec4r axis, real angleRadians);
55 [[nodiscard]] static Quaternion FromAxisAngle(Real3 const& axis, real angleRadians);
56
57 // Construct a quaternion from a rotation vector (Euler vector), r = axis * angle
58 [[nodiscard]] static Quaternion FromRotationVector(Vec4r rotVector);
59 [[nodiscard]] static Quaternion FromRotationVector(Real3 const& rotVector);
60
61 // Get the rotation axis and angle for this Quaternion. The axis is unit length, and the angle is
62 // in the range [-pi, pi]. Assumes unit length quaternion.
63 void ToAxisAngle(Real3* outAxis, real* outAngleRad) const;
64 void ToAxisAngle(Vec4r* outAxis, real* outAngleRad) const;
65
66 // Gets the angle of this Quaternion. The angle is in the range [-pi, pi]. Assumes unit length
67 // quaternion.
68 [[nodiscard]] real GetAngle() const; // NOTE: Assumes quaternion is normalized
69
70 // Get the rotation vector of this Quaternion. Assumes unit length quaternion.
71 // The norm of the rotation vector is bounded by pi, which corresponds to an axis-angle with angle
72 // in the range [-pi, pi]
73 [[nodiscard]] Vec4r VToRotationVector() const;
74 [[nodiscard]] Real3 ToRotationVector() const;
75
76 // Get the data as a Real4
77 [[nodiscard]] Real4 ToReal4() const;
78
79 // Exact Equality
80 [[nodiscard]] bool operator==(Quaternion const& rhs) const;
81 [[nodiscard]] bool operator!=(Quaternion const& rhs) const;
82
83 // Addition and subtraction
84 [[nodiscard]] Quaternion operator+(Quaternion const& a) const;
85 [[nodiscard]] Quaternion operator-(Quaternion const& a) const;
86
87 // Unary negation (negates all 4 components)
88 [[nodiscard]] Quaternion operator-() const;
89
90 // Concatenate rotations in right-to-left order.
91 // Quaternion q3 = q1 * q2; // is equivalent to "rotate by q2, then by q1"
92 [[nodiscard]] Quaternion operator*(Quaternion const& rhs) const;
93
94 // Rotate a vector by this quaternion. Requires that this is a unit quaternion.
95 [[nodiscard]] Vec4r operator*(
96 Vec4r v) const; // v[3] can be 0 (like a direction vector) or 1 (like a point)
97 [[nodiscard]] Real3 operator*(Real3 const& v) const;
98
99 // Get the quaternion conjugate
100 [[nodiscard]] Quaternion GetConjugate() const;
101
102 private:
103 // Private helper functions:
104 [[nodiscard]] real GetAngleImpl(Quaternion& qPositiveNormalized, real& vectorMag) const;
105};
106
107} // namespace superdex
108
109#include "quaternion_inl.h"
static Quaternion RotationY(real a)
Quaternion operator*(Quaternion const &rhs) const
static Quaternion FromUnitAxisAngle(Vec4r normalizedAxis, real angleRadians)
Quaternion GetConjugate() const
Real3 ToRotationVector() const
void ToAxisAngle(Real3 *outAxis, real *outAngleRad) const
Quaternion(real i, real j, real k, real r)
Definition quaternion.h:42
bool operator!=(Quaternion const &rhs) const
static Quaternion Identity()
Quaternion(Real4 const &v)
Definition quaternion.h:41
static Quaternion RotationX(real a)
bool operator==(Quaternion const &rhs) const
static Quaternion RotationZ(real a)
static Quaternion FromAxisAngle(Vec4r axis, real angleRadians)
static Quaternion Zero()
Quaternion operator-() const
Vec4r VToRotationVector() const
static Quaternion FromRotationVector(Vec4r rotVector)
Quaternion operator+(Quaternion const &a) const
Real4 ToReal4() const
Simd< real, 4 > Vec4r
Definition simd.h:206
NdArray< real, 3 > Real3
Definition nd_array.h:106
NdArray< real, 4 > Real4
Definition nd_array.h:107