26 alignas(16)
double w,
x,
y,
z;
29 constexpr Quaternion(
double w_,
double x_,
double y_,
double z_) noexcept
30 :
w(w_),
x(x_),
y(y_),
z(z_) {}
32 :
w(w_),
x(v.x),
y(v.y),
z(v.z) {}
35 constexpr double norm2() const noexcept {
36 return w *
w +
x *
x +
y *
y +
z *
z;
39 double norm() const noexcept {
40 return std::sqrt(
norm2());
45 return std::abs(
w - 1.0) < eps && std::abs(
x) < eps && std::abs(
y) < eps &&
50 return std::isnan(
w) || std::isnan(
x) || std::isnan(
y) || std::isnan(
z);
64 if (std::abs(n2 - 1.0) > eps)
70 double angleRad)
noexcept {
72 double half = 0.5 * angleRad;
73 double s = std::sin(half);
74 return Quaternion(std::cos(half), nAxis.
x * s, nAxis.
y * s, nAxis.
z * s);
80 double angle = omega.norm() * dt;
87 q.
w = std::clamp(q.
w, -1.0, 1.0);
88 angleRad = 2.0 * std::acos(q.
w);
89 double s = std::sqrt(std::max(0.0, 1.0 - q.
w * q.
w));
99 double dot = a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z;
107 return (a * (1.0 - t) + b2 * t).normalized();
109 double theta = std::acos(dot);
110 double s1 = std::sin((1.0 - t) * theta);
111 double s2 = std::sin(t * theta);
112 double s = std::sin(theta);
113 return (a * s1 + b2 * s2) * (1.0 / s);
117 double xx =
x *
x, yy =
y *
y, zz =
z *
z;
118 double xy =
x *
y, xz =
x *
z, yz =
y *
z;
119 double wx =
w *
x, wy =
w *
y, wz =
w *
z;
120 m[0][0] = 1.0 - 2.0 * (yy + zz);
121 m[0][1] = 2.0 * (xy - wz);
122 m[0][2] = 2.0 * (xz + wy);
123 m[1][0] = 2.0 * (xy + wz);
124 m[1][1] = 1.0 - 2.0 * (xx + zz);
125 m[1][2] = 2.0 * (yz - wx);
126 m[2][0] = 2.0 * (xz - wy);
127 m[2][1] = 2.0 * (yz + wx);
128 m[2][2] = 1.0 - 2.0 * (xx + yy);
161 w * rhs.x +
x * rhs.w +
y * rhs.z -
z * rhs.y,
162 w * rhs.y -
x * rhs.z +
y * rhs.w +
z * rhs.x,
163 w * rhs.z +
x * rhs.y -
y * rhs.x +
z * rhs.w);
169 return Quaternion(
w * scalar,
x * scalar,
y * scalar,
z * scalar);
174 return Quaternion(q.w * scalar, q.x * scalar, q.y * scalar, q.z * scalar);
210 return w == rhs.w &&
x == rhs.x &&
y == rhs.y &&
z == rhs.z;
214 return !(*
this == rhs);
218 return os <<
"[" << q.
w <<
", (" << q.
x <<
", " << q.
y <<
", " << q.
z
void toAxisAngle(Vector3 &axis, double &angleRad) const noexcept
Definition quaternion.hpp:85
constexpr Quaternion conjugate() const noexcept
Definition quaternion.hpp:145
friend std::ostream & operator<<(std::ostream &os, const Quaternion &q)
Definition quaternion.hpp:217
double x
Definition quaternion.hpp:26
double w
Definition quaternion.hpp:26
Vector3 right() const noexcept
Definition quaternion.hpp:194
constexpr bool operator!=(const Quaternion &rhs) const noexcept
Definition quaternion.hpp:213
constexpr double norm2() const noexcept
Definition quaternion.hpp:35
static Quaternion fromAngularVelocity(const Vector3 &omega, double dt) noexcept
Definition quaternion.hpp:78
Quaternion normalized() const noexcept
Definition quaternion.hpp:54
friend constexpr Quaternion operator*(double scalar, const Quaternion &q) noexcept
Definition quaternion.hpp:172
double norm() const noexcept
Definition quaternion.hpp:39
constexpr Quaternion operator-() const noexcept
Definition quaternion.hpp:205
Quaternion inverse() const noexcept
Definition quaternion.hpp:150
void normalize() noexcept
Definition quaternion.hpp:131
double z
Definition quaternion.hpp:26
constexpr bool operator==(const Quaternion &rhs) const noexcept
Definition quaternion.hpp:209
Vector3 up() const noexcept
Definition quaternion.hpp:190
bool hasNaN() const noexcept
Definition quaternion.hpp:49
constexpr Quaternion operator+(const Quaternion &rhs) const noexcept
Definition quaternion.hpp:201
bool isIdentity(double eps=1e-12) const noexcept
Definition quaternion.hpp:44
void toMatrix(double m[3][3]) const noexcept
Definition quaternion.hpp:116
constexpr Quaternion(double w_, const Vector3 &v) noexcept
Definition quaternion.hpp:31
void normalizeIfNeeded(double eps=1e-12) noexcept
Definition quaternion.hpp:62
Vector3 rotate(const Vector3 &v) const noexcept
Definition quaternion.hpp:179
static Quaternion fromAxisAngle(const Vector3 &axis, double angleRad) noexcept
Definition quaternion.hpp:69
constexpr Quaternion(double w_, double x_, double y_, double z_) noexcept
Definition quaternion.hpp:29
Vector3 forward() const noexcept
Definition quaternion.hpp:186
static Quaternion slerp(const Quaternion &a, const Quaternion &b, double t) noexcept
Definition quaternion.hpp:97
constexpr Quaternion operator*(double scalar) const noexcept
Definition quaternion.hpp:168
constexpr Quaternion() noexcept
Definition quaternion.hpp:28
constexpr Quaternion operator*(const Quaternion &rhs) const noexcept
Definition quaternion.hpp:159
double y
Definition quaternion.hpp:26
3D vector utility used throughout JSim physics.
Definition vector.hpp:22
Vector3 normalized() const noexcept
Returns the normalized direction vector.
Definition vector.hpp:119
double z
Z component.
Definition vector.hpp:28
double x
X component.
Definition vector.hpp:24
double y
Y component.
Definition vector.hpp:26