A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Quat.hh
Go to the documentation of this file.
1#pragma once
2
3#include "egg/math/Math.hh"
4#include "egg/math/Vector.hh"
5
6namespace Kinoko::EGG {
7
12struct Quatf {
13#ifdef BUILD_DEBUG
14 constexpr Quatf() : w(std::numeric_limits<f32>::signaling_NaN()) {}
15#else
16 constexpr Quatf() = default;
17#endif
18 constexpr Quatf(const Quatf &q) = default;
19
20 constexpr Quatf(f32 w_, const Vector3f &v_) : v(v_), w(w_) {}
21
22 constexpr Quatf(f32 w_, f32 x_, f32 y_, f32 z_) : v(x_, y_, z_), w(w_) {}
23
24 constexpr ~Quatf() = default;
25
26 constexpr Quatf &operator=(const Quatf &q) {
27 w = q.w;
28 v = q.v;
29
30 return *this;
31 }
32
33 [[nodiscard]] constexpr Quatf operator+(const Quatf &rhs) const {
34 return Quatf(w + rhs.w, v + rhs.v);
35 }
36
37 constexpr Quatf &operator+=(const Quatf &rhs) {
38 return *this = *this + rhs;
39 }
40
41 [[nodiscard]] constexpr Quatf operator*(const Vector3f &vec) const {
42 Vector3f cross = v.cross(vec);
43 Vector3f scale = vec * w;
44 return Quatf(-v.dot(vec), cross + scale);
45 }
46
47 [[nodiscard]] constexpr Quatf operator*(f32 scalar) const {
48 return Quatf(w * scalar, v * scalar);
49 }
50
51 constexpr Quatf &operator*=(f32 scalar) {
52 return *this = *this * scalar;
53 }
54
57 [[nodiscard]] constexpr Quatf operator*(const Quatf &rhs) const {
58 f32 _w = w * rhs.w - v.x * rhs.v.x - v.y * rhs.v.y - v.z * rhs.v.z;
59 f32 _x = v.y * rhs.v.z + (v.x * rhs.w + w * rhs.v.x) - v.z * rhs.v.y;
60 f32 _y = v.z * rhs.v.x + (v.y * rhs.w + w * rhs.v.y) - v.x * rhs.v.z;
61 f32 _z = v.x * rhs.v.y + (v.z * rhs.w + w * rhs.v.z) - v.y * rhs.v.x;
62
63 return Quatf(_w, _x, _y, _z);
64 }
65
66 constexpr Quatf &operator*=(const Quatf &q) {
67 return *this = *this * q;
68 }
69
70 [[nodiscard]] constexpr bool operator==(const Quatf &rhs) const {
71 return w == rhs.w && v == rhs.v;
72 }
73
74 [[nodiscard]] constexpr bool operator!=(const Quatf &rhs) const {
75 return !(*this == rhs);
76 }
77
79 [[nodiscard]] explicit operator std::string() const {
80 return std::format("[0x{:08X}, 0x{:08X}, 0x{:08X}, 0x{:08X}] | [{}, {}, {}, {}]", f2u(v.x),
81 f2u(v.y), f2u(v.z), f2u(w), v.x, v.y, v.z, w);
82 }
83
86 constexpr void setRPY(const Vector3f &rpy) {
87 *this = FromRPY(rpy.x, rpy.y, rpy.z);
88 }
89
91 constexpr void setRPY(f32 r, f32 p, f32 y) {
92 const f32 cy = Mathf::cos(y * 0.5f);
93 const f32 cp = Mathf::cos(p * 0.5f);
94 const f32 cr = Mathf::cos(r * 0.5f);
95 const f32 sy = Mathf::sin(y * 0.5f);
96 const f32 sp = Mathf::sin(p * 0.5f);
97 const f32 sr = Mathf::sin(r * 0.5f);
98
99 w = cy * cp * cr + sy * sp * sr;
100 v.x = cy * cp * sr - sy * sp * cr;
101 v.y = cy * sp * cr + sy * cp * sr;
102 v.z = sy * cp * cr - cy * sp * sr;
103 }
104
107 constexpr void normalise() {
108 f32 len = squaredNorm() > std::numeric_limits<f32>::epsilon() ? norm() : 0.0f;
109
110 if (len != 0.0f) {
111 f32 inv = 1.0f / len;
112 w *= inv;
113 v *= inv;
114 }
115 }
116
119 constexpr void makeVectorRotation(const Vector3f &from, const Vector3f &to) {
120 f32 t0 = std::max(0.0f, (from.dot(to) + 1) * 2.0f);
121 t0 = Mathf::sqrt(t0);
122
123 if (t0 <= std::numeric_limits<f32>::epsilon()) {
124 *this = Quatf::ident;
125 } else {
126 const f32 inv = 1.0f / t0;
127 w = t0 * 0.5f;
128 v = from.cross(to) * inv;
129 }
130 }
131
133 [[nodiscard]] constexpr Quatf conjugate() const {
134 return Quatf(w, -v);
135 }
136
139 [[nodiscard]] constexpr Vector3f rotateVector(const Vector3f &vec) const {
140 Quatf conj = conjugate();
141 Quatf res = *this * vec;
142 Quatf ret;
143
144 ret.v.x = (res.v.y * conj.v.z + (res.v.x * conj.w + res.w * conj.v.x)) - res.v.z * conj.v.y;
145 ret.v.y = (res.v.z * conj.v.x + (res.v.y * conj.w + res.w * conj.v.y)) - res.v.x * conj.v.z;
146 ret.v.z = (res.v.x * conj.v.y + (res.v.z * conj.w + res.w * conj.v.z)) - res.v.y * conj.v.x;
147
148 return ret.v;
149 }
150
153 [[nodiscard]] constexpr Vector3f rotateVectorInv(const Vector3f &vec) const {
154 Quatf conj = conjugate();
155 Quatf res = conj * vec;
156 Quatf ret;
157
158 ret.v.x = (res.v.y * v.z + (res.v.x * w + res.w * v.x)) - res.v.z * v.y;
159 ret.v.y = (res.v.z * v.x + (res.v.y * w + res.w * v.y)) - res.v.x * v.z;
160 ret.v.z = (res.v.x * v.y + (res.v.z * w + res.w * v.z)) - res.v.y * v.x;
161
162 return ret.v;
163 }
164
168 [[nodiscard]] constexpr Quatf slerpTo(const Quatf &q1, f32 t) const {
169 f32 dot_ = std::clamp(dot(q1), -1.0f, 1.0f);
170 bool bDot = dot_ < 0.0f;
171 dot_ = Mathf::abs(dot_);
172
173 f32 acos = Mathf::acos(dot_);
174 f32 sin = Mathf::sin(acos);
175
176 f32 s;
177 if (Mathf::abs(sin) < 0.00001f) {
178 s = 1.0f - t;
179 } else {
180 f32 invSin = 1.0f / sin;
181 f32 tmp0 = t * acos;
182 s = invSin * Mathf::sin(acos - tmp0);
183 t = invSin * Mathf::sin(tmp0);
184 }
185
186 if (bDot) {
187 t = -t;
188 }
189
190 return Quatf(s * w + t * q1.w, s * v + t * q1.v);
191 }
192
195 [[nodiscard]] constexpr f32 squaredNorm() const {
196 return w * w + v.squaredLength();
197 }
198
199 [[nodiscard]] constexpr f32 norm() const {
200 return Mathf::sqrt(squaredNorm());
201 }
202
205 [[nodiscard]] constexpr f32 dot(const Quatf &q) const {
206 return w * q.w + v.dot(q.v);
207 }
208
211 constexpr void setAxisRotation(f32 angle, const EGG::Vector3f &axis) {
212 const f32 half_angle = angle * 0.5f;
213 const f32 c = Mathf::cos(half_angle);
214 const f32 s = Mathf::sin(half_angle);
215
216 w = c;
217 v = axis * s;
218 }
219
220 [[nodiscard]] constexpr Quatf multSwap(const Vector3f &vec) const {
221 f32 _w = -(v.dot(vec));
222 f32 _x = (w * vec.x + v.y * vec.z) - v.z * vec.y;
223 f32 _y = (w * vec.y + v.z * vec.x) - v.x * vec.z;
224 f32 _z = (w * vec.z + v.x * vec.y) - v.y * vec.x;
225
226 return Quatf(_w, _x, _y, _z);
227 }
228
229 [[nodiscard]] constexpr Quatf multSwap(const Quatf &q) const {
230 f32 _w = ((w * q.w - v.x * q.v.x) - v.y * q.v.y) - v.z * q.v.z;
231 f32 _x = (v.y * q.v.z + (v.x * q.w + w * q.v.x)) - v.z * q.v.y;
232 f32 _y = (v.z * q.v.x + (v.y * q.w + w * q.v.y)) - v.x * q.v.z;
233 f32 _z = (v.x * q.v.y + (v.z * q.w + w * q.v.z)) - v.y * q.v.x;
234
235 return Quatf(_w, _x, _y, _z);
236 }
237
238 void read(Stream &stream) {
239 v.read(stream);
240 w = stream.read_f32();
241 }
242
243 [[nodiscard]] static constexpr Quatf FromRPY(const EGG::Vector3f &rpy) {
244 Quatf ret;
245 ret.setRPY(rpy);
246 return ret;
247 }
248
250 [[nodiscard]] static constexpr Quatf FromRPY(f32 r, f32 p, f32 y) {
251 Quatf ret;
252 ret.setRPY(r, p, y);
253 return ret;
254 }
255
258
259 static const Quatf ident;
260};
261
262inline constexpr Quatf Quatf::ident = Quatf(1.0f, Vector3f::zero);
263
264} // namespace Kinoko::EGG
A stream of data, abstracted to allow for continuous seeking.
Definition Stream.hh:8
EGG core library.
Definition Allocator.hh:5
float f32
Definition Memory.hh:21
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
constexpr Quatf & operator=(const Quatf &q)
Definition Quat.hh:26
static const Quatf ident
Definition Quat.hh:259
constexpr void setRPY(f32 r, f32 p, f32 y)
Helper function to avoid unnecessary Vector3f construction.
Definition Quat.hh:91
void read(Stream &stream)
Definition Quat.hh:238
constexpr f32 norm() const
Definition Quat.hh:199
constexpr bool operator==(const Quatf &rhs) const
Definition Quat.hh:70
constexpr f32 dot(const Quatf &q) const
Computes .
Definition Quat.hh:205
constexpr void setAxisRotation(f32 angle, const EGG::Vector3f &axis)
Set the quat given angle and axis.
Definition Quat.hh:211
constexpr void makeVectorRotation(const Vector3f &from, const Vector3f &to)
Captures rotation between two vectors.
Definition Quat.hh:119
constexpr void normalise()
Scales the quaternion to a unit length.
Definition Quat.hh:107
constexpr Quatf operator+(const Quatf &rhs) const
Definition Quat.hh:33
constexpr Quatf operator*(const Quatf &rhs) const
Definition Quat.hh:57
constexpr Quatf & operator*=(const Quatf &q)
Definition Quat.hh:66
constexpr Vector3f rotateVector(const Vector3f &vec) const
Rotates a vector based on the quat.
Definition Quat.hh:139
static constexpr Quatf FromRPY(const EGG::Vector3f &rpy)
Definition Quat.hh:243
constexpr void setRPY(const Vector3f &rpy)
Sets roll, pitch, and yaw.
Definition Quat.hh:86
constexpr ~Quatf()=default
constexpr Quatf(f32 w_, const Vector3f &v_)
Definition Quat.hh:20
constexpr f32 squaredNorm() const
Computes .
Definition Quat.hh:195
constexpr Quatf multSwap(const Vector3f &vec) const
Definition Quat.hh:220
constexpr Quatf slerpTo(const Quatf &q1, f32 t) const
Performs spherical linear interpolation.
Definition Quat.hh:168
constexpr Quatf & operator+=(const Quatf &rhs)
Definition Quat.hh:37
constexpr Quatf multSwap(const Quatf &q) const
Definition Quat.hh:229
constexpr bool operator!=(const Quatf &rhs) const
Definition Quat.hh:74
constexpr Vector3f rotateVectorInv(const Vector3f &vec) const
Rotates a vector on the inverse quat.
Definition Quat.hh:153
constexpr Quatf(f32 w_, f32 x_, f32 y_, f32 z_)
Definition Quat.hh:22
static constexpr Quatf FromRPY(f32 r, f32 p, f32 y)
Helper function to avoid unnecessary Vector3f construction.
Definition Quat.hh:250
constexpr Quatf operator*(f32 scalar) const
Definition Quat.hh:47
constexpr Quatf operator*(const Vector3f &vec) const
Definition Quat.hh:41
constexpr Quatf()=default
constexpr Quatf & operator*=(f32 scalar)
Definition Quat.hh:51
constexpr Quatf conjugate() const
Computes .
Definition Quat.hh:133
constexpr Quatf(const Quatf &q)=default
A 3D float vector.
Definition Vector.hh:108
constexpr f32 squaredLength() const
The dot product between the vector and itself.
Definition Vector.hh:202
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
Definition Vector.hh:376
static const Vector3f zero
Definition Vector.hh:386
constexpr f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:207
constexpr Vector3f cross(const Vector3f &rhs) const
Definition Vector.hh:197