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#ifndef QUATERNION_H
32#define QUATERNION_H
33
34#include "core/math/math_funcs.h"
35#include "core/math/vector3.h"
36
37class String;
38
39struct _NO_DISCARD_ Quaternion {
40 union {
41 struct {
42 real_t x;
43 real_t y;
44 real_t z;
45 real_t w;
46 };
47 real_t components[4] = { 0, 0, 0, 1.0 };
48 };
49
50 _FORCE_INLINE_ real_t &operator[](int idx) {
51 return components[idx];
52 }
53 _FORCE_INLINE_ const real_t &operator[](int idx) const {
54 return components[idx];
55 }
56 _FORCE_INLINE_ real_t length_squared() const;
57 bool is_equal_approx(const Quaternion &p_quaternion) const;
58 bool is_finite() const;
59 real_t length() const;
60 void normalize();
61 Quaternion normalized() const;
62 bool is_normalized() const;
63 Quaternion inverse() const;
64 Quaternion log() const;
65 Quaternion exp() const;
66 _FORCE_INLINE_ real_t dot(const Quaternion &p_q) const;
67 real_t angle_to(const Quaternion &p_to) const;
68
69 Vector3 get_euler(EulerOrder p_order = EulerOrder::YXZ) const;
70 static Quaternion from_euler(const Vector3 &p_euler);
71
72 Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const;
73 Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const;
74 Quaternion spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const;
75 Quaternion spherical_cubic_interpolate_in_time(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
76
77 Vector3 get_axis() const;
78 real_t get_angle() const;
79
80 _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
81 r_angle = 2 * Math::acos(w);
82 real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
83 r_axis.x = x * r;
84 r_axis.y = y * r;
85 r_axis.z = z * r;
86 }
87
88 void operator*=(const Quaternion &p_q);
89 Quaternion operator*(const Quaternion &p_q) const;
90
91 _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const {
92#ifdef MATH_CHECKS
93 ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized.");
94#endif
95 Vector3 u(x, y, z);
96 Vector3 uv = u.cross(v);
97 return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
98 }
99
100 _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &v) const {
101 return inverse().xform(v);
102 }
103
104 _FORCE_INLINE_ void operator+=(const Quaternion &p_q);
105 _FORCE_INLINE_ void operator-=(const Quaternion &p_q);
106 _FORCE_INLINE_ void operator*=(const real_t &s);
107 _FORCE_INLINE_ void operator/=(const real_t &s);
108 _FORCE_INLINE_ Quaternion operator+(const Quaternion &q2) const;
109 _FORCE_INLINE_ Quaternion operator-(const Quaternion &q2) const;
110 _FORCE_INLINE_ Quaternion operator-() const;
111 _FORCE_INLINE_ Quaternion operator*(const real_t &s) const;
112 _FORCE_INLINE_ Quaternion operator/(const real_t &s) const;
113
114 _FORCE_INLINE_ bool operator==(const Quaternion &p_quaternion) const;
115 _FORCE_INLINE_ bool operator!=(const Quaternion &p_quaternion) const;
116
117 operator String() const;
118
119 _FORCE_INLINE_ Quaternion() {}
120
121 _FORCE_INLINE_ Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
122 x(p_x),
123 y(p_y),
124 z(p_z),
125 w(p_w) {
126 }
127
128 Quaternion(const Vector3 &p_axis, real_t p_angle);
129
130 Quaternion(const Quaternion &p_q) :
131 x(p_q.x),
132 y(p_q.y),
133 z(p_q.z),
134 w(p_q.w) {
135 }
136
137 void operator=(const Quaternion &p_q) {
138 x = p_q.x;
139 y = p_q.y;
140 z = p_q.z;
141 w = p_q.w;
142 }
143
144 Quaternion(const Vector3 &v0, const Vector3 &v1) { // Shortest arc.
145 Vector3 c = v0.cross(v1);
146 real_t d = v0.dot(v1);
147
148 if (d < -1.0f + (real_t)CMP_EPSILON) {
149 x = 0;
150 y = 1;
151 z = 0;
152 w = 0;
153 } else {
154 real_t s = Math::sqrt((1.0f + d) * 2.0f);
155 real_t rs = 1.0f / s;
156
157 x = c.x * rs;
158 y = c.y * rs;
159 z = c.z * rs;
160 w = s * 0.5f;
161 }
162 }
163};
164
165real_t Quaternion::dot(const Quaternion &p_q) const {
166 return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w;
167}
168
169real_t Quaternion::length_squared() const {
170 return dot(*this);
171}
172
173void Quaternion::operator+=(const Quaternion &p_q) {
174 x += p_q.x;
175 y += p_q.y;
176 z += p_q.z;
177 w += p_q.w;
178}
179
180void Quaternion::operator-=(const Quaternion &p_q) {
181 x -= p_q.x;
182 y -= p_q.y;
183 z -= p_q.z;
184 w -= p_q.w;
185}
186
187void Quaternion::operator*=(const real_t &s) {
188 x *= s;
189 y *= s;
190 z *= s;
191 w *= s;
192}
193
194void Quaternion::operator/=(const real_t &s) {
195 *this *= 1.0f / s;
196}
197
198Quaternion Quaternion::operator+(const Quaternion &q2) const {
199 const Quaternion &q1 = *this;
200 return Quaternion(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
201}
202
203Quaternion Quaternion::operator-(const Quaternion &q2) const {
204 const Quaternion &q1 = *this;
205 return Quaternion(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
206}
207
208Quaternion Quaternion::operator-() const {
209 const Quaternion &q2 = *this;
210 return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w);
211}
212
213Quaternion Quaternion::operator*(const real_t &s) const {
214 return Quaternion(x * s, y * s, z * s, w * s);
215}
216
217Quaternion Quaternion::operator/(const real_t &s) const {
218 return *this * (1.0f / s);
219}
220
221bool Quaternion::operator==(const Quaternion &p_quaternion) const {
222 return x == p_quaternion.x && y == p_quaternion.y && z == p_quaternion.z && w == p_quaternion.w;
223}
224
225bool Quaternion::operator!=(const Quaternion &p_quaternion) const {
226 return x != p_quaternion.x || y != p_quaternion.y || z != p_quaternion.z || w != p_quaternion.w;
227}
228
229_FORCE_INLINE_ Quaternion operator*(const real_t &p_real, const Quaternion &p_quaternion) {
230 return p_quaternion * p_real;
231}
232
233#endif // QUATERNION_H
234