1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#include "Quat.h"
#include <cmath>
#include "Defs.h"
#include "Vector3.h"
#include "Basis.h"
namespace godot {
real_t Quat::length() const
{
return ::sqrt(length_squared());
}
void Quat::normalize()
{
*this /= length();
}
Quat Quat::normalized() const
{
return *this / length();
}
Quat Quat::inverse() const
{
return Quat( -x, -y, -z, w );
}
void Quat::set_euler(const Vector3& p_euler)
{
real_t half_a1 = p_euler.x * 0.5;
real_t half_a2 = p_euler.y * 0.5;
real_t half_a3 = p_euler.z * 0.5;
// R = X(a1).Y(a2).Z(a3) convention for Euler angles.
// Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-2)
// a3 is the angle of the first rotation, following the notation in this reference.
real_t cos_a1 = ::cos(half_a1);
real_t sin_a1 = ::sin(half_a1);
real_t cos_a2 = ::cos(half_a2);
real_t sin_a2 = ::sin(half_a2);
real_t cos_a3 = ::cos(half_a3);
real_t sin_a3 = ::sin(half_a3);
set(sin_a1*cos_a2*cos_a3 + sin_a2*sin_a3*cos_a1,
-sin_a1*sin_a3*cos_a2 + sin_a2*cos_a1*cos_a3,
sin_a1*sin_a2*cos_a3 + sin_a3*cos_a1*cos_a2,
-sin_a1*sin_a2*sin_a3 + cos_a1*cos_a2*cos_a3);
}
Quat Quat::slerp(const Quat& q, const real_t& t) const {
Quat to1;
real_t omega, cosom, sinom, scale0, scale1;
// calc cosine
cosom = dot(q);
// adjust signs (if necessary)
if ( cosom <0.0 ) {
cosom = -cosom;
to1.x = - q.x;
to1.y = - q.y;
to1.z = - q.z;
to1.w = - q.w;
} else {
to1.x = q.x;
to1.y = q.y;
to1.z = q.z;
to1.w = q.w;
}
// calculate coefficients
if ( (1.0 - cosom) > CMP_EPSILON ) {
// standard case (slerp)
omega = ::acos(cosom);
sinom = ::sin(omega);
scale0 = ::sin((1.0 - t) * omega) / sinom;
scale1 = ::sin(t * omega) / sinom;
} else {
// "from" and "to" quaternions are very close
// ... so we can do a linear interpolation
scale0 = 1.0 - t;
scale1 = t;
}
// calculate final values
return Quat(
scale0 * x + scale1 * to1.x,
scale0 * y + scale1 * to1.y,
scale0 * z + scale1 * to1.z,
scale0 * w + scale1 * to1.w
);
}
Quat Quat::slerpni(const Quat& q, const real_t& t) const {
const Quat &from = *this;
real_t dot = from.dot(q);
if (::fabs(dot) > 0.9999) return from;
real_t theta = ::acos(dot),
sinT = 1.0 / ::sin(theta),
newFactor = ::sin(t * theta) * sinT,
invFactor = ::sin((1.0 - t) * theta) * sinT;
return Quat(invFactor * from.x + newFactor * q.x,
invFactor * from.y + newFactor * q.y,
invFactor * from.z + newFactor * q.z,
invFactor * from.w + newFactor * q.w);
}
Quat Quat::cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const
{
//the only way to do slerp :|
real_t t2 = (1.0-t)*t*2;
Quat sp = this->slerp(q,t);
Quat sq = prep.slerpni(postq,t);
return sp.slerpni(sq,t2);
}
void Quat::get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
r_angle = 2 * ::acos(w);
r_axis.x = x / ::sqrt(1-w*w);
r_axis.y = y / ::sqrt(1-w*w);
r_axis.z = z / ::sqrt(1-w*w);
}
Quat Quat::operator*(const Vector3& v) const
{
return Quat( w * v.x + y * v.z - z * v.y,
w * v.y + z * v.x - x * v.z,
w * v.z + x * v.y - y * v.x,
-x * v.x - y * v.y - z * v.z);
}
Vector3 Quat::xform(const Vector3& v) const {
Quat q = *this * v;
q *= this->inverse();
return Vector3(q.x,q.y,q.z);
}
Quat::operator String() const
{
return String(); // @Todo
}
Quat::Quat(const Vector3& axis, const real_t& angle)
{
real_t d = axis.length();
if (d==0)
set(0,0,0,0);
else {
real_t sin_angle = ::sin(angle * 0.5);
real_t cos_angle = ::cos(angle * 0.5);
real_t s = sin_angle / d;
set(axis.x * s, axis.y * s, axis.z * s,
cos_angle);
}
}
Quat::Quat(const Vector3& v0, const Vector3& v1) // shortest arc
{
Vector3 c = v0.cross(v1);
real_t d = v0.dot(v1);
if (d < -1.0 + CMP_EPSILON) {
x=0;
y=1;
z=0;
w=0;
} else {
real_t s = ::sqrt((1.0 + d) * 2.0);
real_t rs = 1.0 / s;
x=c.x*rs;
y=c.y*rs;
z=c.z*rs;
w=s * 0.5;
}
}
real_t Quat::dot(const Quat& q) const {
return x * q.x+y * q.y+z * q.z+w * q.w;
}
real_t Quat::length_squared() const {
return dot(*this);
}
void Quat::operator+=(const Quat& q) {
x += q.x; y += q.y; z += q.z; w += q.w;
}
void Quat::operator-=(const Quat& q) {
x -= q.x; y -= q.y; z -= q.z; w -= q.w;
}
void Quat::operator*=(const Quat& q) {
x *= q.x; y *= q.y; z *= q.z; w *= q.w;
}
void Quat::operator*=(const real_t& s) {
x *= s; y *= s; z *= s; w *= s;
}
void Quat::operator/=(const real_t& s) {
*this *= 1.0 / s;
}
Quat Quat::operator+(const Quat& q2) const {
const Quat& q1 = *this;
return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
}
Quat Quat::operator-(const Quat& q2) const {
const Quat& q1 = *this;
return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
}
Quat Quat::operator*(const Quat& q2) const {
Quat q1 = *this;
q1 *= q2;
return q1;
}
Quat Quat::operator-() const {
const Quat& q2 = *this;
return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
}
Quat Quat::operator*(const real_t& s) const {
return Quat(x * s, y * s, z * s, w * s);
}
Quat Quat::operator/(const real_t& s) const {
return *this * (1.0 / s);
}
bool Quat::operator==(const Quat& p_quat) const {
return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
}
bool Quat::operator!=(const Quat& p_quat) const {
return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
}
Vector3 Quat::get_euler() const
{
Basis m(*this);
return m.get_euler();
}
}
|