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