1 /*************************************************************************/
2 /* quat.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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 // Circular dependency between Vector3 and Basis :/
32 #include "core/math/vector3.h"
33
34 #ifndef QUAT_H
35 #define QUAT_H
36
37 #include "core/math/math_defs.h"
38 #include "core/math/math_funcs.h"
39 #include "core/ustring.h"
40
41 class Quat {
42 public:
43 real_t x, y, z, w;
44
45 _FORCE_INLINE_ real_t length_squared() const;
46 bool is_equal_approx(const Quat &p_quat) const;
47 real_t length() const;
48 void normalize();
49 Quat normalized() const;
50 bool is_normalized() const;
51 Quat inverse() const;
52 _FORCE_INLINE_ real_t dot(const Quat &q) const;
53
54 void set_euler_xyz(const Vector3 &p_euler);
55 Vector3 get_euler_xyz() const;
56 void set_euler_yxz(const Vector3 &p_euler);
57 Vector3 get_euler_yxz() const;
58
set_euler(const Vector3 & p_euler)59 void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); };
get_euler()60 Vector3 get_euler() const { return get_euler_yxz(); };
61
62 Quat slerp(const Quat &q, const real_t &t) const;
63 Quat slerpni(const Quat &q, const real_t &t) const;
64 Quat cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const;
65
66 void set_axis_angle(const Vector3 &axis, const real_t &angle);
get_axis_angle(Vector3 & r_axis,real_t & r_angle)67 _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
68 r_angle = 2 * Math::acos(w);
69 real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
70 r_axis.x = x * r;
71 r_axis.y = y * r;
72 r_axis.z = z * r;
73 }
74
75 void operator*=(const Quat &q);
76 Quat operator*(const Quat &q) const;
77
78 Quat operator*(const Vector3 &v) const {
79 return Quat(w * v.x + y * v.z - z * v.y,
80 w * v.y + z * v.x - x * v.z,
81 w * v.z + x * v.y - y * v.x,
82 -x * v.x - y * v.y - z * v.z);
83 }
84
xform(const Vector3 & v)85 _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const {
86 #ifdef MATH_CHECKS
87 ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized.");
88 #endif
89 Vector3 u(x, y, z);
90 Vector3 uv = u.cross(v);
91 return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
92 }
93
94 _FORCE_INLINE_ void operator+=(const Quat &q);
95 _FORCE_INLINE_ void operator-=(const Quat &q);
96 _FORCE_INLINE_ void operator*=(const real_t &s);
97 _FORCE_INLINE_ void operator/=(const real_t &s);
98 _FORCE_INLINE_ Quat operator+(const Quat &q2) const;
99 _FORCE_INLINE_ Quat operator-(const Quat &q2) const;
100 _FORCE_INLINE_ Quat operator-() const;
101 _FORCE_INLINE_ Quat operator*(const real_t &s) const;
102 _FORCE_INLINE_ Quat operator/(const real_t &s) const;
103
104 _FORCE_INLINE_ bool operator==(const Quat &p_quat) const;
105 _FORCE_INLINE_ bool operator!=(const Quat &p_quat) const;
106
107 operator String() const;
108
set(real_t p_x,real_t p_y,real_t p_z,real_t p_w)109 inline void set(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
110 x = p_x;
111 y = p_y;
112 z = p_z;
113 w = p_w;
114 }
Quat(real_t p_x,real_t p_y,real_t p_z,real_t p_w)115 inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
116 x(p_x),
117 y(p_y),
118 z(p_z),
119 w(p_w) {
120 }
Quat(const Vector3 & axis,const real_t & angle)121 Quat(const Vector3 &axis, const real_t &angle) { set_axis_angle(axis, angle); }
122
Quat(const Vector3 & euler)123 Quat(const Vector3 &euler) { set_euler(euler); }
Quat(const Quat & q)124 Quat(const Quat &q) :
125 x(q.x),
126 y(q.y),
127 z(q.z),
128 w(q.w) {
129 }
130
131 Quat operator=(const Quat &q) {
132 x = q.x;
133 y = q.y;
134 z = q.z;
135 w = q.w;
136 return *this;
137 }
138
Quat(const Vector3 & v0,const Vector3 & v1)139 Quat(const Vector3 &v0, const Vector3 &v1) // shortest arc
140 {
141 Vector3 c = v0.cross(v1);
142 real_t d = v0.dot(v1);
143
144 if (d < -1.0 + CMP_EPSILON) {
145 x = 0;
146 y = 1;
147 z = 0;
148 w = 0;
149 } else {
150
151 real_t s = Math::sqrt((1.0 + d) * 2.0);
152 real_t rs = 1.0 / s;
153
154 x = c.x * rs;
155 y = c.y * rs;
156 z = c.z * rs;
157 w = s * 0.5;
158 }
159 }
160
Quat()161 inline Quat() :
162 x(0),
163 y(0),
164 z(0),
165 w(1) {
166 }
167 };
168
dot(const Quat & q)169 real_t Quat::dot(const Quat &q) const {
170 return x * q.x + y * q.y + z * q.z + w * q.w;
171 }
172
length_squared()173 real_t Quat::length_squared() const {
174 return dot(*this);
175 }
176
177 void Quat::operator+=(const Quat &q) {
178 x += q.x;
179 y += q.y;
180 z += q.z;
181 w += q.w;
182 }
183
184 void Quat::operator-=(const Quat &q) {
185 x -= q.x;
186 y -= q.y;
187 z -= q.z;
188 w -= q.w;
189 }
190
191 void Quat::operator*=(const real_t &s) {
192 x *= s;
193 y *= s;
194 z *= s;
195 w *= s;
196 }
197
198 void Quat::operator/=(const real_t &s) {
199
200 *this *= 1.0 / s;
201 }
202
203 Quat Quat::operator+(const Quat &q2) const {
204 const Quat &q1 = *this;
205 return Quat(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
206 }
207
208 Quat Quat::operator-(const Quat &q2) const {
209 const Quat &q1 = *this;
210 return Quat(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
211 }
212
213 Quat Quat::operator-() const {
214 const Quat &q2 = *this;
215 return Quat(-q2.x, -q2.y, -q2.z, -q2.w);
216 }
217
218 Quat Quat::operator*(const real_t &s) const {
219 return Quat(x * s, y * s, z * s, w * s);
220 }
221
222 Quat Quat::operator/(const real_t &s) const {
223 return *this * (1.0 / s);
224 }
225
226 bool Quat::operator==(const Quat &p_quat) const {
227 return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w;
228 }
229
230 bool Quat::operator!=(const Quat &p_quat) const {
231 return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
232 }
233
234 #endif
235