Debian Codegraph @

core/math/quaternion.h

raw symbols SCIP
1 /**************************************************************************/
2 /* quaternion.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/math/math_funcs.h"
34 #include "core/math/vector3.h"
35 #include "core/string/ustring.h"
36
37 struct [[nodiscard]] Quaternion {
38 union {
39 // NOLINTBEGIN(modernize-use-default-member-init)
40 struct {
41 real_t x;
42 real_t y;
43 real_t z;
44 real_t w;
45 };
46 real_t components[4] = { 0, 0, 0, 1.0 };
47 // NOLINTEND(modernize-use-default-member-init)
48 };
49
50 _FORCE_INLINE_ real_t &operator[](int p_idx) {
51 return components[p_idx];
52 }
53 _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
54 return components[p_idx];
55 }
56 _FORCE_INLINE_ real_t length_squared() const;
57 bool is_equal_approx(const Quaternion &p_quaternion) const;
58 bool is_same(const Quaternion &p_quaternion) const;
59 bool is_finite() const;
60 real_t length() const;
61 void normalize();
62 Quaternion normalized() const;
63 bool is_normalized() const;
64 Quaternion inverse() const;
65 Quaternion log() const;
66 Quaternion exp() const;
67 _FORCE_INLINE_ real_t dot(const Quaternion &p_q) const;
68 real_t angle_to(const Quaternion &p_to) const;
69
70 Vector3 get_euler(EulerOrder p_order = EulerOrder::YXZ) const;
71 static Quaternion from_euler(const Vector3 &p_euler);
72
73 Quaternion slerp(const Quaternion &p_to, real_t p_weight) const;
74 Quaternion slerpni(const Quaternion &p_to, real_t p_weight) const;
75 Quaternion spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, real_t p_weight) const;
76 Quaternion spherical_cubic_interpolate_in_time(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
77
78 Vector3 get_axis() const;
79 real_t get_angle() const;
80
81 _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
82 r_angle = 2 * Math::acos(w);
83 real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
84 r_axis.x = x * r;
85 r_axis.y = y * r;
86 r_axis.z = z * r;
87 }
88
89 constexpr void operator*=(const Quaternion &p_q);
90 constexpr Quaternion operator*(const Quaternion &p_q) const;
91
92 _FORCE_INLINE_ Vector3 xform(const Vector3 &p_v) const {
93 #ifdef MATH_CHECKS
94 ERR_FAIL_COND_V_MSG(!is_normalized(), p_v, "The quaternion " + operator String() + " must be normalized.");
95 #endif
96 Vector3 u(x, y, z);
97 Vector3 uv = u.cross(p_v);
98 return p_v + ((uv * w) + u.cross(uv)) * ((real_t)2);
99 }
100
101 _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_v) const {
102 return inverse().xform(p_v);
103 }
104
105 constexpr void operator+=(const Quaternion &p_q);
106 constexpr void operator-=(const Quaternion &p_q);
107 constexpr void operator*=(real_t p_s);
108 constexpr void operator/=(real_t p_s);
109 constexpr Quaternion operator+(const Quaternion &p_q2) const;
110 constexpr Quaternion operator-(const Quaternion &p_q2) const;
111 constexpr Quaternion operator-() const;
112 constexpr Quaternion operator*(real_t p_s) const;
113 constexpr Quaternion operator/(real_t p_s) const;
114
115 constexpr bool operator==(const Quaternion &p_quaternion) const;
116 constexpr bool operator!=(const Quaternion &p_quaternion) const;
117
118 explicit operator String() const;
119
120 constexpr Quaternion() :
121 x(0), y(0), z(0), w(1) {}
122
123 constexpr Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
124 x(p_x), y(p_y), z(p_z), w(p_w) {}
125
126 Quaternion(const Vector3 &p_axis, real_t p_angle);
127
128 constexpr Quaternion(const Quaternion &p_q) :
129 x(p_q.x), y(p_q.y), z(p_q.z), w(p_q.w) {}
130
131 constexpr void operator=(const Quaternion &p_q) {
132 x = p_q.x;
133 y = p_q.y;
134 z = p_q.z;
135 w = p_q.w;
136 }
137
138 Quaternion(const Vector3 &p_v0, const Vector3 &p_v1) { // Shortest arc.
139 #ifdef MATH_CHECKS
140 ERR_FAIL_COND_MSG(p_v0.is_zero_approx() || p_v1.is_zero_approx(), "The vectors must not be zero.");
141 #endif
142 #ifdef REAL_T_IS_DOUBLE
143 constexpr real_t ALMOST_ONE = 0.999999999999999;
144 #else
145 constexpr real_t ALMOST_ONE = 0.99999975f;
146 #endif
147 Vector3 n0 = p_v0.normalized();
148 Vector3 n1 = p_v1.normalized();
149 real_t d = n0.dot(n1);
150 if (Math::abs(d) > ALMOST_ONE) {
151 if (d >= 0) {
152 return; // Vectors are same.
153 }
154 Vector3 axis = n0.get_any_perpendicular();
155 x = axis.x;
156 y = axis.y;
157 z = axis.z;
158 w = 0;
159 } else {
160 Vector3 c = n0.cross(n1);
161 real_t s = Math::sqrt((1.0f + d) * 2.0f);
162 real_t rs = 1.0f / s;
163
164 x = c.x * rs;
165 y = c.y * rs;
166 z = c.z * rs;
167 w = s * 0.5f;
168 }
169 normalize();
170 }
171 };
172
173 real_t Quaternion::dot(const Quaternion &p_q) const {
174 return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w;
175 }
176
177 real_t Quaternion::length_squared() const {
178 return dot(*this);
179 }
180
181 constexpr void Quaternion::operator+=(const Quaternion &p_q) {
182 x += p_q.x;
183 y += p_q.y;
184 z += p_q.z;
185 w += p_q.w;
186 }
187
188 constexpr void Quaternion::operator-=(const Quaternion &p_q) {
189 x -= p_q.x;
190 y -= p_q.y;
191 z -= p_q.z;
192 w -= p_q.w;
193 }
194
195 constexpr void Quaternion::operator*=(real_t p_s) {
196 x *= p_s;
197 y *= p_s;
198 z *= p_s;
199 w *= p_s;
200 }
201
202 constexpr void Quaternion::operator/=(real_t p_s) {
203 *this *= (1 / p_s);
204 }
205
206 constexpr Quaternion Quaternion::operator+(const Quaternion &p_q2) const {
207 const Quaternion &q1 = *this;
208 return Quaternion(q1.x + p_q2.x, q1.y + p_q2.y, q1.z + p_q2.z, q1.w + p_q2.w);
209 }
210
211 constexpr Quaternion Quaternion::operator-(const Quaternion &p_q2) const {
212 const Quaternion &q1 = *this;
213 return Quaternion(q1.x - p_q2.x, q1.y - p_q2.y, q1.z - p_q2.z, q1.w - p_q2.w);
214 }
215
216 constexpr Quaternion Quaternion::operator-() const {
217 const Quaternion &q2 = *this;
218 return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w);
219 }
220
221 constexpr Quaternion Quaternion::operator*(real_t p_s) const {
222 return Quaternion(x * p_s, y * p_s, z * p_s, w * p_s);
223 }
224
225 constexpr Quaternion Quaternion::operator/(real_t p_s) const {
226 return *this * (1 / p_s);
227 }
228
229 constexpr bool Quaternion::operator==(const Quaternion &p_quaternion) const {
230 return x == p_quaternion.x && y == p_quaternion.y && z == p_quaternion.z && w == p_quaternion.w;
231 }
232
233 constexpr bool Quaternion::operator!=(const Quaternion &p_quaternion) const {
234 return x != p_quaternion.x || y != p_quaternion.y || z != p_quaternion.z || w != p_quaternion.w;
235 }
236
237 constexpr void Quaternion::operator*=(const Quaternion &p_q) {
238 real_t xx = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y;
239 real_t yy = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z;
240 real_t zz = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x;
241 w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z;
242 x = xx;
243 y = yy;
244 z = zz;
245 }
246
247 constexpr Quaternion Quaternion::operator*(const Quaternion &p_q) const {
248 Quaternion r = *this;
249 r *= p_q;
250 return r;
251 }
252
253 constexpr Quaternion operator*(real_t p_real, const Quaternion &p_quaternion) {
254 return p_quaternion * p_real;
255 }