forked from ydm/mit-vecmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quat4f.cpp
501 lines (433 loc) · 11.7 KB
/
Quat4f.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include "Quat4f.h"
#include "Vector3f.h"
#include "Vector4f.h"
//////////////////////////////////////////////////////////////////////////
// Public
//////////////////////////////////////////////////////////////////////////
// static
const Quat4f Quat4f::ZERO = Quat4f( 0, 0, 0, 0 );
// static
const Quat4f Quat4f::IDENTITY = Quat4f( 1, 0, 0, 0 );
Quat4f::Quat4f()
{
m_elements[ 0 ] = 0;
m_elements[ 1 ] = 0;
m_elements[ 2 ] = 0;
m_elements[ 3 ] = 0;
}
Quat4f::Quat4f( float w, float x, float y, float z )
{
m_elements[ 0 ] = w;
m_elements[ 1 ] = x;
m_elements[ 2 ] = y;
m_elements[ 3 ] = z;
}
Quat4f::Quat4f( const Quat4f& rq )
{
m_elements[ 0 ] = rq.m_elements[ 0 ];
m_elements[ 1 ] = rq.m_elements[ 1 ];
m_elements[ 2 ] = rq.m_elements[ 2 ];
m_elements[ 3 ] = rq.m_elements[ 3 ];
}
Quat4f& Quat4f::operator = ( const Quat4f& rq )
{
if( this != ( &rq ) )
{
m_elements[ 0 ] = rq.m_elements[ 0 ];
m_elements[ 1 ] = rq.m_elements[ 1 ];
m_elements[ 2 ] = rq.m_elements[ 2 ];
m_elements[ 3 ] = rq.m_elements[ 3 ];
}
return( *this );
}
Quat4f::Quat4f( const Vector3f& v )
{
m_elements[ 0 ] = 0;
m_elements[ 1 ] = v[ 0 ];
m_elements[ 2 ] = v[ 1 ];
m_elements[ 3 ] = v[ 2 ];
}
Quat4f::Quat4f( const Vector4f& v )
{
m_elements[ 0 ] = v[ 0 ];
m_elements[ 1 ] = v[ 1 ];
m_elements[ 2 ] = v[ 2 ];
m_elements[ 3 ] = v[ 3 ];
}
const float& Quat4f::operator [] ( int i ) const
{
return m_elements[ i ];
}
float& Quat4f::operator [] ( int i )
{
return m_elements[ i ];
}
float Quat4f::w() const
{
return m_elements[ 0 ];
}
float Quat4f::x() const
{
return m_elements[ 1 ];
}
float Quat4f::y() const
{
return m_elements[ 2 ];
}
float Quat4f::z() const
{
return m_elements[ 3 ];
}
Vector3f Quat4f::xyz() const
{
return Vector3f
(
m_elements[ 1 ],
m_elements[ 2 ],
m_elements[ 3 ]
);
}
Vector4f Quat4f::wxyz() const
{
return Vector4f
(
m_elements[ 0 ],
m_elements[ 1 ],
m_elements[ 2 ],
m_elements[ 3 ]
);
}
float Quat4f::abs() const
{
return sqrt( absSquared() );
}
float Quat4f::absSquared() const
{
return
(
m_elements[ 0 ] * m_elements[ 0 ] +
m_elements[ 1 ] * m_elements[ 1 ] +
m_elements[ 2 ] * m_elements[ 2 ] +
m_elements[ 3 ] * m_elements[ 3 ]
);
}
void Quat4f::normalize()
{
float reciprocalAbs = 1.f / abs();
m_elements[ 0 ] *= reciprocalAbs;
m_elements[ 1 ] *= reciprocalAbs;
m_elements[ 2 ] *= reciprocalAbs;
m_elements[ 3 ] *= reciprocalAbs;
}
Quat4f Quat4f::normalized() const
{
Quat4f q( *this );
q.normalize();
return q;
}
void Quat4f::conjugate()
{
m_elements[ 1 ] = -m_elements[ 1 ];
m_elements[ 2 ] = -m_elements[ 2 ];
m_elements[ 3 ] = -m_elements[ 3 ];
}
Quat4f Quat4f::conjugated() const
{
return Quat4f
(
m_elements[ 0 ],
-m_elements[ 1 ],
-m_elements[ 2 ],
-m_elements[ 3 ]
);
}
void Quat4f::invert()
{
Quat4f inverse = conjugated() * ( 1.0f / absSquared() );
m_elements[ 0 ] = inverse.m_elements[ 0 ];
m_elements[ 1 ] = inverse.m_elements[ 1 ];
m_elements[ 2 ] = inverse.m_elements[ 2 ];
m_elements[ 3 ] = inverse.m_elements[ 3 ];
}
Quat4f Quat4f::inverse() const
{
return conjugated() * ( 1.0f / absSquared() );
}
Quat4f Quat4f::log() const
{
float len =
sqrt
(
m_elements[ 1 ] * m_elements[ 1 ] +
m_elements[ 2 ] * m_elements[ 2 ] +
m_elements[ 3 ] * m_elements[ 3 ]
);
if( len < 1e-6 )
{
return Quat4f( 0, m_elements[ 1 ], m_elements[ 2 ], m_elements[ 3 ] );
}
else
{
float coeff = acos( m_elements[ 0 ] ) / len;
return Quat4f( 0, m_elements[ 1 ] * coeff, m_elements[ 2 ] * coeff, m_elements[ 3 ] * coeff );
}
}
Quat4f Quat4f::exp() const
{
float theta =
sqrt
(
m_elements[ 1 ] * m_elements[ 1 ] +
m_elements[ 2 ] * m_elements[ 2 ] +
m_elements[ 3 ] * m_elements[ 3 ]
);
if( theta < 1e-6 )
{
return Quat4f( cos( theta ), m_elements[ 1 ], m_elements[ 2 ], m_elements[ 3 ] );
}
else
{
float coeff = sin( theta ) / theta;
return Quat4f( cos( theta ), m_elements[ 1 ] * coeff, m_elements[ 2 ] * coeff, m_elements[ 3 ] * coeff );
}
}
Vector3f Quat4f::getAxisAngle( float* radiansOut )
{
float theta = acos( w() ) * 2;
float vectorNorm = sqrt( x() * x() + y() * y() + z() * z() );
float reciprocalVectorNorm = 1.f / vectorNorm;
*radiansOut = theta;
return Vector3f
(
x() * reciprocalVectorNorm,
y() * reciprocalVectorNorm,
z() * reciprocalVectorNorm
);
}
void Quat4f::setAxisAngle( float radians, const Vector3f& axis )
{
m_elements[ 0 ] = cos( radians / 2 );
float sinHalfTheta = sin( radians / 2 );
float vectorNorm = axis.abs();
float reciprocalVectorNorm = 1.f / vectorNorm;
m_elements[ 1 ] = axis.x() * sinHalfTheta * reciprocalVectorNorm;
m_elements[ 2 ] = axis.y() * sinHalfTheta * reciprocalVectorNorm;
m_elements[ 3 ] = axis.z() * sinHalfTheta * reciprocalVectorNorm;
}
void Quat4f::print() const
{
printf( "< %.4f + %.4f i + %.4f j + %.4f k >\n",
m_elements[ 0 ], m_elements[ 1 ], m_elements[ 2 ], m_elements[ 3 ] );
}
// static
float Quat4f::dot( const Quat4f& q0, const Quat4f& q1 )
{
return
(
q0.w() * q1.w() +
q0.x() * q1.x() +
q0.y() * q1.y() +
q0.z() * q1.z()
);
}
// static
Quat4f Quat4f::lerp( const Quat4f& q0, const Quat4f& q1, float alpha )
{
return( ( q0 + alpha * ( q1 - q0 ) ).normalized() );
}
// static
Quat4f Quat4f::slerp( const Quat4f& a, const Quat4f& b, float t, bool allowFlip )
{
float cosAngle = Quat4f::dot( a, b );
float c1;
float c2;
// Linear interpolation for close orientations
if( ( 1.0f - fabs( cosAngle ) ) < 0.01f )
{
c1 = 1.0f - t;
c2 = t;
}
else
{
// Spherical interpolation
float angle = acos( fabs( cosAngle ) );
float sinAngle = sin( angle );
c1 = sin( angle * ( 1.0f - t ) ) / sinAngle;
c2 = sin( angle * t ) / sinAngle;
}
// Use the shortest path
if( allowFlip && ( cosAngle < 0.0f ) )
{
c1 = -c1;
}
return Quat4f( c1 * a[ 0 ] + c2 * b[ 0 ], c1 * a[ 1 ] + c2 * b[ 1 ], c1 * a[ 2 ] + c2 * b[ 2 ], c1 * a[ 3 ] + c2 * b[ 3 ] );
}
// static
Quat4f Quat4f::squad( const Quat4f& a, const Quat4f& tanA, const Quat4f& tanB, const Quat4f& b, float t )
{
Quat4f ab = Quat4f::slerp( a, b, t );
Quat4f tangent = Quat4f::slerp( tanA, tanB, t, false );
return Quat4f::slerp( ab, tangent, 2.0f * t * ( 1.0f - t ), false );
}
// static
Quat4f Quat4f::cubicInterpolate( const Quat4f& q0, const Quat4f& q1, const Quat4f& q2, const Quat4f& q3, float t )
{
// geometric construction:
// t
// (t+1)/2 t/2
// t+1 t t-1
// bottom level
Quat4f q0q1 = Quat4f::slerp( q0, q1, t + 1 );
Quat4f q1q2 = Quat4f::slerp( q1, q2, t );
Quat4f q2q3 = Quat4f::slerp( q2, q3, t - 1 );
// middle level
Quat4f q0q1_q1q2 = Quat4f::slerp( q0q1, q1q2, 0.5f * ( t + 1 ) );
Quat4f q1q2_q2q3 = Quat4f::slerp( q1q2, q2q3, 0.5f * t );
// top level
return Quat4f::slerp( q0q1_q1q2, q1q2_q2q3, t );
}
// static
Quat4f Quat4f::logDifference( const Quat4f& a, const Quat4f& b )
{
Quat4f diff = a.inverse() * b;
diff.normalize();
return diff.log();
}
// static
Quat4f Quat4f::squadTangent( const Quat4f& before, const Quat4f& center, const Quat4f& after )
{
Quat4f l1 = Quat4f::logDifference( center, before );
Quat4f l2 = Quat4f::logDifference( center, after );
Quat4f e;
for( int i = 0; i < 4; ++i )
{
e[ i ] = -0.25f * ( l1[ i ] + l2[ i ] );
}
e = center * ( e.exp() );
return e;
}
// static
Quat4f Quat4f::fromRotationMatrix( const Matrix3f& m )
{
float x;
float y;
float z;
float w;
// Compute one plus the trace of the matrix
float onePlusTrace = 1.0f + m( 0, 0 ) + m( 1, 1 ) + m( 2, 2 );
if( onePlusTrace > 1e-5 )
{
// Direct computation
float s = sqrt( onePlusTrace ) * 2.0f;
x = ( m( 2, 1 ) - m( 1, 2 ) ) / s;
y = ( m( 0, 2 ) - m( 2, 0 ) ) / s;
z = ( m( 1, 0 ) - m( 0, 1 ) ) / s;
w = 0.25f * s;
}
else
{
// Computation depends on major diagonal term
if( ( m( 0, 0 ) > m( 1, 1 ) ) & ( m( 0, 0 ) > m( 2, 2 ) ) )
{
float s = sqrt( 1.0f + m( 0, 0 ) - m( 1, 1 ) - m( 2, 2 ) ) * 2.0f;
x = 0.25f * s;
y = ( m( 0, 1 ) + m( 1, 0 ) ) / s;
z = ( m( 0, 2 ) + m( 2, 0 ) ) / s;
w = ( m( 1, 2 ) - m( 2, 1 ) ) / s;
}
else if( m( 1, 1 ) > m( 2, 2 ) )
{
float s = sqrt( 1.0f + m( 1, 1 ) - m( 0, 0 ) - m( 2, 2 ) ) * 2.0f;
x = ( m( 0, 1 ) + m( 1, 0 ) ) / s;
y = 0.25f * s;
z = ( m( 1, 2 ) + m( 2, 1 ) ) / s;
w = ( m( 0, 2 ) - m( 2, 0 ) ) / s;
}
else
{
float s = sqrt( 1.0f + m( 2, 2 ) - m( 0, 0 ) - m( 1, 1 ) ) * 2.0f;
x = ( m( 0, 2 ) + m( 2, 0 ) ) / s;
y = ( m( 1, 2 ) + m( 2, 1 ) ) / s;
z = 0.25f * s;
w = ( m( 0, 1 ) - m( 1, 0 ) ) / s;
}
}
Quat4f q( w, x, y, z );
return q.normalized();
}
// static
Quat4f Quat4f::fromRotatedBasis( const Vector3f& x, const Vector3f& y, const Vector3f& z )
{
return fromRotationMatrix( Matrix3f( x, y, z ) );
}
// static
Quat4f Quat4f::randomRotation( float u0, float u1, float u2 )
{
float z = u0;
float theta = static_cast< float >( 2.f * M_PI * u1 );
float r = sqrt( 1.f - z * z );
float w = static_cast< float >( M_PI * u2 );
return Quat4f
(
cos( w ),
sin( w ) * cos( theta ) * r,
sin( w ) * sin( theta ) * r,
sin( w ) * z
);
}
//////////////////////////////////////////////////////////////////////////
// Operators
//////////////////////////////////////////////////////////////////////////
Quat4f operator + ( const Quat4f& q0, const Quat4f& q1 )
{
return Quat4f
(
q0.w() + q1.w(),
q0.x() + q1.x(),
q0.y() + q1.y(),
q0.z() + q1.z()
);
}
Quat4f operator - ( const Quat4f& q0, const Quat4f& q1 )
{
return Quat4f
(
q0.w() - q1.w(),
q0.x() - q1.x(),
q0.y() - q1.y(),
q0.z() - q1.z()
);
}
Quat4f operator * ( const Quat4f& q0, const Quat4f& q1 )
{
return Quat4f
(
q0.w() * q1.w() - q0.x() * q1.x() - q0.y() * q1.y() - q0.z() * q1.z(),
q0.w() * q1.x() + q0.x() * q1.w() + q0.y() * q1.z() - q0.z() * q1.y(),
q0.w() * q1.y() - q0.x() * q1.z() + q0.y() * q1.w() + q0.z() * q1.x(),
q0.w() * q1.z() + q0.x() * q1.y() - q0.y() * q1.x() + q0.z() * q1.w()
);
}
Quat4f operator * ( float f, const Quat4f& q )
{
return Quat4f
(
f * q.w(),
f * q.x(),
f * q.y(),
f * q.z()
);
}
Quat4f operator * ( const Quat4f& q, float f )
{
return Quat4f
(
f * q.w(),
f * q.x(),
f * q.y(),
f * q.z()
);
}