-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Cry_Quat.h
1782 lines (1601 loc) · 51.3 KB
/
Cry_Quat.h
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
#pragma once
#include <limits>
//! Quaternion.
template<typename F> struct Quat_tpl
{
Vec3_tpl<F> v;
F w;
//constructors
#if defined(_DEBUG)
ILINE Quat_tpl()
{
if (sizeof(F) == 4)
{
uint32* p = alias_cast<uint32*>(&v.x);
p[0] = F32NAN;
p[1] = F32NAN;
p[2] = F32NAN;
p[3] = F32NAN;
}
if (sizeof(F) == 8)
{
uint64* p = alias_cast<uint64*>(&v.x);
p[0] = F64NAN;
p[1] = F64NAN;
p[2] = F64NAN;
p[3] = F64NAN;
}
}
#else
ILINE Quat_tpl() {}
#endif
//! Initialize with zeros.
ILINE Quat_tpl(type_zero)
{
w = 0, v.x = 0, v.y = 0, v.z = 0;
}
ILINE Quat_tpl(type_identity)
{
w = 1, v.x = 0, v.y = 0, v.z = 0;
}
//! ASSIGNMENT OPERATOR of identical Quat types.
//! The assignment operator has precedence over assignment constructor.
//! Quat q; q=q0;
ILINE Quat_tpl<F>& operator=(const Quat_tpl<F>& src)
{
v.x = src.v.x;
v.y = src.v.y;
v.z = src.v.z;
w = src.w;
CRY_MATH_ASSERT(IsValid());
return *this;
}
//! CONSTRUCTOR to initialize a Quat from 4 floats.
//! Quat q(1,0,0,0);
ILINE Quat_tpl<F>(F qw, F qx, F qy, F qz)
{
w = qw;
v.x = qx;
v.y = qy;
v.z = qz;
CRY_MATH_ASSERT(IsValid());
}
//! CONSTRUCTOR to initialize a Quat with a scalar and a vector.
//! Quat q(1,Vec3(0,0,0));
ILINE Quat_tpl<F>(F scalar, const Vec3_tpl<F> &vector)
{
v = vector;
w = scalar;
CRY_MATH_ASSERT(IsValid());
};
//! CONSTRUCTOR for identical types.
//! Quat q=q0;
ILINE Quat_tpl<F>(const Quat_tpl<F> &q)
{
w = q.w;
v.x = q.v.x;
v.y = q.v.y;
v.z = q.v.z;
CRY_MATH_ASSERT(IsValid());
}
//! CONSTRUCTOR for identical types which converts between double/float.
//! Quat q32=q64;
//! Quatr q64=q32;
template<class F1> ILINE Quat_tpl<F>(const Quat_tpl<F1> &q)
{
CRY_MATH_ASSERT(q.IsValid());
w = F(q.w);
v.x = F(q.v.x);
v.y = F(q.v.y);
v.z = F(q.v.z);
}
//! CONSTRUCTOR for different types. It converts a Euler Angle into a Quat.
//! Needs to be 'explicit' because we loose fp-precision in the conversion process.
//! Quat(Ang3(1,2,3));
explicit ILINE Quat_tpl<F>(const Ang3_tpl<F> &ang)
{
CRY_MATH_ASSERT(ang.IsValid());
SetRotationXYZ(ang);
}
//! CONSTRUCTOR for different types. It converts a Euler Angle into a Quat and converts between double/float.
//! Needs to be 'explicit' because we loose fp-precision in the conversion process.
//! Quat(Ang3r(1,2,3));
template<class F1> explicit ILINE Quat_tpl<F>(const Ang3_tpl<F1> &ang)
{
CRY_MATH_ASSERT(ang.IsValid());
SetRotationXYZ(Ang3_tpl<F>(F(ang.x), F(ang.y), F(ang.z)));
}
//! CONSTRUCTOR for different types. It converts a Matrix33 into a Quat.
//! Needs to be 'explicit' because we loose fp-precision in the conversion process.
//! Quat(m33);
explicit ILINE Quat_tpl<F>(const Matrix33_tpl<F> &m)
{
CRY_MATH_ASSERT(m.IsOrthonormalRH(0.1f));
F s, p, tr = m.m00 + m.m11 + m.m22;
w = 1, v.x = 0, v.y = 0, v.z = 0;
if (tr > 0)
s = sqrt_tpl(tr + 1.0f), p = 0.5f / s, w = s * 0.5f, v.x = (m.m21 - m.m12) * p, v.y = (m.m02 - m.m20) * p, v.z = (m.m10 - m.m01) * p;
else if ((m.m00 >= m.m11) && (m.m00 >= m.m22))
s = sqrt_tpl(m.m00 - m.m11 - m.m22 + 1.0f), p = 0.5f / s, w = (m.m21 - m.m12) * p, v.x = s * 0.5f, v.y = (m.m10 + m.m01) * p, v.z = (m.m20 + m.m02) * p;
else if ((m.m11 >= m.m00) && (m.m11 >= m.m22))
s = sqrt_tpl(m.m11 - m.m22 - m.m00 + 1.0f), p = 0.5f / s, w = (m.m02 - m.m20) * p, v.x = (m.m01 + m.m10) * p, v.y = s * 0.5f, v.z = (m.m21 + m.m12) * p;
else if ((m.m22 >= m.m00) && (m.m22 >= m.m11))
s = sqrt_tpl(m.m22 - m.m00 - m.m11 + 1.0f), p = 0.5f / s, w = (m.m10 - m.m01) * p, v.x = (m.m02 + m.m20) * p, v.y = (m.m12 + m.m21) * p, v.z = s * 0.5f;
}
//! CONSTRUCTOR for different types. It converts a Matrix33 into a Quat and converts between double/float.
//! Needs to be 'explicit' because we loose fp-precision the conversion process.
//! Quat(m33r);
//! Quatr(m33);
template<class F1> explicit ILINE Quat_tpl<F>(const Matrix33_tpl<F1> &m)
{
CRY_MATH_ASSERT(m.IsOrthonormalRH(0.1f));
F1 s, p, tr = m.m00 + m.m11 + m.m22;
w = 1, v.x = 0, v.y = 0, v.z = 0;
if (tr > 0)
s = sqrt_tpl(tr + 1.0f), p = 0.5f / s, w = F(s * 0.5), v.x = F((m.m21 - m.m12) * p), v.y = F((m.m02 - m.m20) * p), v.z = F((m.m10 - m.m01) * p);
else if ((m.m00 >= m.m11) && (m.m00 >= m.m22))
s = sqrt_tpl(m.m00 - m.m11 - m.m22 + 1.0f), p = 0.5f / s, w = F((m.m21 - m.m12) * p), v.x = F(s * 0.5), v.y = F((m.m10 + m.m01) * p), v.z = F((m.m20 + m.m02) * p);
else if ((m.m11 >= m.m00) && (m.m11 >= m.m22))
s = sqrt_tpl(m.m11 - m.m22 - m.m00 + 1.0f), p = 0.5f / s, w = F((m.m02 - m.m20) * p), v.x = F((m.m01 + m.m10) * p), v.y = F(s * 0.5), v.z = F((m.m21 + m.m12) * p);
else if ((m.m22 >= m.m00) && (m.m22 >= m.m11))
s = sqrt_tpl(m.m22 - m.m00 - m.m11 + 1.0f), p = 0.5f / s, w = F((m.m10 - m.m01) * p), v.x = F((m.m02 + m.m20) * p), v.y = F((m.m12 + m.m21) * p), v.z = F(s * 0.5);
}
//! CONSTRUCTOR for different types. It converts a Matrix34 into a Quat.
//! Needs to be 'explicit' because we loose fp-precision in the conversion process.
//! Quat(m34);
explicit ILINE Quat_tpl<F>(const Matrix34_tpl<F> &m)
{
*this = Quat_tpl<F>(Matrix33_tpl<F>(m));
}
//! CONSTRUCTOR for different types. It converts a Matrix34 into a Quat and converts between double/float.
//! Needs to be 'explicit' because we loose fp-precision the conversion process.
//! Quat(m34r);
//! Quatr(m34);
template<class F1> explicit ILINE Quat_tpl<F>(const Matrix34_tpl<F1> &m)
{
*this = Quat_tpl<F>(Matrix33_tpl<F1>(m));
}
//! Invert quaternion.
//! Example 1:
//! Quat q=Quat::CreateRotationXYZ(Ang3(1,2,3));
//! Quat result = !q;
//! Quat result = GetInverted(q);
//! q.Invert();
ILINE Quat_tpl<F> operator!() const { return Quat_tpl(w, -v); }
ILINE void Invert(void) { *this = !*this; }
ILINE Quat_tpl<F> GetInverted() const { return !(*this); }
//! Flip quaternion. don't confuse this with quaternion-inversion.
ILINE Quat_tpl<F> operator-() const { return Quat_tpl<F>(-w, -v); };
//! Multiplication by a scalar.
void operator*=(F op) { w *= op; v *= op; }
//! Exact compare of 2 quats.
ILINE bool operator==(const Quat_tpl<F>& q) const { return (v == q.v) && (w == q.w); }
ILINE bool operator!=(const Quat_tpl<F>& q) const { return !(*this == q); }
//! A quaternion is a compressed matrix. Thus there is no problem extracting the rows & columns.
ILINE Vec3_tpl<F> GetColumn(uint32 i)
{
if (i == 0) return GetColumn0();
if (i == 1) return GetColumn1();
if (i == 2) return GetColumn2();
CRY_MATH_ASSERT(0); //bad index
return Vec3(ZERO);
}
ILINE Vec3_tpl<F> GetColumn0() const { return Vec3_tpl<F>(2 * (v.x * v.x + w * w) - 1, 2 * (v.y * v.x + v.z * w), 2 * (v.z * v.x - v.y * w)); }
ILINE Vec3_tpl<F> GetColumn1() const { return Vec3_tpl<F>(2 * (v.x * v.y - v.z * w), 2 * (v.y * v.y + w * w) - 1, 2 * (v.z * v.y + v.x * w)); }
ILINE Vec3_tpl<F> GetColumn2() const { return Vec3_tpl<F>(2 * (v.x * v.z + v.y * w), 2 * (v.y * v.z - v.x * w), 2 * (v.z * v.z + w * w) - 1); }
ILINE Vec3_tpl<F> GetRow0() const { return Vec3_tpl<F>(2 * (v.x * v.x + w * w) - 1, 2 * (v.x * v.y - v.z * w), 2 * (v.x * v.z + v.y * w)); }
ILINE Vec3_tpl<F> GetRow1() const { return Vec3_tpl<F>(2 * (v.y * v.x + v.z * w), 2 * (v.y * v.y + w * w) - 1, 2 * (v.y * v.z - v.x * w)); }
ILINE Vec3_tpl<F> GetRow2() const { return Vec3_tpl<F>(2 * (v.z * v.x - v.y * w), 2 * (v.z * v.y + v.x * w), 2 * (v.z * v.z + w * w) - 1); }
// These are just copy & pasted components of the GetColumn1() above.
ILINE F GetFwdX() const { return (2 * (v.x * v.y - v.z * w)); }
ILINE F GetFwdY() const { return (2 * (v.y * v.y + w * w) - 1); }
ILINE F GetFwdZ() const { return (2 * (v.z * v.y + v.x * w)); }
ILINE F GetRotZ() const { return atan2_tpl(-GetFwdX(), GetFwdY()); }
//! Set identity quaternion.
//! Example:
//! Quat q=Quat::CreateIdentity();
//! or
//! q.SetIdentity();
//! or
//! Quat p=Quat(IDENTITY);
ILINE void SetIdentity(void)
{
w = 1;
v.x = 0;
v.y = 0;
v.z = 0;
}
ILINE static Quat_tpl<F> CreateIdentity(void)
{
return Quat_tpl<F>(1, 0, 0, 0);
}
//! Check if identity quaternion.
ILINE bool IsIdentity() const
{
return w == 1 && v.x == 0 && v.y == 0 && v.z == 0;
}
ILINE bool IsUnit(F e = VEC_EPSILON) const
{
return fabs_tpl(1 - (w * w + v.x * v.x + v.y * v.y + v.z * v.z)) < e;
}
ILINE bool IsValid(F e = VEC_EPSILON) const
{
if (!v.IsValid()) return false;
if (!NumberValid(w)) return false;
//if (!IsUnit(e)) return false;
return true;
}
ILINE void SetRotationAA(F rad, const Vec3_tpl<F>& axis)
{
F s, c;
sincos_tpl(rad * (F)0.5, &s, &c);
SetRotationAA(c, s, axis);
}
ILINE static Quat_tpl<F> CreateRotationAA(F rad, const Vec3_tpl<F>& axis)
{
Quat_tpl<F> q;
q.SetRotationAA(rad, axis);
return q;
}
ILINE void SetRotationAA(F cosha, F sinha, const Vec3_tpl<F>& axis)
{
CRY_MATH_ASSERT(axis.IsUnit(0.001f));
w = cosha;
v = axis * sinha;
}
ILINE static Quat_tpl<F> CreateRotationAA(F cosha, F sinha, const Vec3_tpl<F>& axis)
{
Quat_tpl<F> q;
q.SetRotationAA(cosha, sinha, axis);
return q;
}
//! Create rotation-quaternion that around the fixed coordinate axes.
//! Example:
//! Quat q=Quat::CreateRotationXYZ( Ang3(1,2,3) );
//! or
//! q.SetRotationXYZ( Ang3(1,2,3) );
ILINE void SetRotationXYZ(const Ang3_tpl<F>& a)
{
CRY_MATH_ASSERT(a.IsValid());
F sx, cx;
sincos_tpl(F(a.x * F(0.5)), &sx, &cx);
F sy, cy;
sincos_tpl(F(a.y * F(0.5)), &sy, &cy);
F sz, cz;
sincos_tpl(F(a.z * F(0.5)), &sz, &cz);
w = cx * cy * cz + sx * sy * sz;
v.x = cz * cy * sx - sz * sy * cx;
v.y = cz * sy * cx + sz * cy * sx;
v.z = sz * cy * cx - cz * sy * sx;
}
ILINE static Quat_tpl<F> CreateRotationXYZ(const Ang3_tpl<F>& a)
{
CRY_MATH_ASSERT(a.IsValid());
Quat_tpl<F> q;
q.SetRotationXYZ(a);
return q;
}
//! Create rotation-quaternion that about the x-axis.
//! Example:
//! Quat q=Quat::CreateRotationX( radiant );
//! or
//! q.SetRotationX( Ang3(1,2,3) );
ILINE void SetRotationX(f32 r)
{
F s, c;
sincos_tpl(F(r * F(0.5)), &s, &c);
w = c;
v.x = s;
v.y = 0;
v.z = 0;
}
ILINE static Quat_tpl<F> CreateRotationX(f32 r)
{
Quat_tpl<F> q;
q.SetRotationX(r);
return q;
}
//! Create rotation-quaternion that about the y-axis.
//! Example:
//! Quat q=Quat::CreateRotationY( radiant );
//! or
//! q.SetRotationY( radiant );
ILINE void SetRotationY(f32 r)
{
F s, c;
sincos_tpl(F(r * F(0.5)), &s, &c);
w = c;
v.x = 0;
v.y = s;
v.z = 0;
}
ILINE static Quat_tpl<F> CreateRotationY(f32 r)
{
Quat_tpl<F> q;
q.SetRotationY(r);
return q;
}
//! Create rotation-quaternion that about the z-axis.
//! Example:
//! Quat q=Quat::CreateRotationZ( radiant );
//! or
//! q.SetRotationZ( radiant );
ILINE void SetRotationZ(f32 r)
{
F s, c;
sincos_tpl(F(r * F(0.5)), &s, &c);
w = c;
v.x = 0;
v.y = 0;
v.z = s;
}
ILINE static Quat_tpl<F> CreateRotationZ(f32 r)
{
Quat_tpl<F> q;
q.SetRotationZ(r);
return q;
}
//! Create rotation-quaternion that rotates from one vector to another.
//! Both vectors are assumed to be normalized.
//! Example:
//! Quat q=Quat::CreateRotationV0V1( v0,v1 );
//! q.SetRotationV0V1( v0,v1 );
ILINE void SetRotationV0V1(const Vec3_tpl<F>& v0, const Vec3_tpl<F>& v1)
{
CRY_MATH_ASSERT(v0.IsUnit(0.01f));
CRY_MATH_ASSERT(v1.IsUnit(0.01f));
F dot = v0.x * v1.x + v0.y * v1.y + v0.z * v1.z + F(1.0);
if (dot > F(0.0001))
{
F vx = v0.y * v1.z - v0.z * v1.y;
F vy = v0.z * v1.x - v0.x * v1.z;
F vz = v0.x * v1.y - v0.y * v1.x;
F d = isqrt_tpl(dot * dot + vx * vx + vy * vy + vz * vz);
w = F(dot * d);
v.x = F(vx * d);
v.y = F(vy * d);
v.z = F(vz * d);
return;
}
w = 0;
v = v0.GetOrthogonal().GetNormalized();
}
ILINE static Quat_tpl<F> CreateRotationV0V1(const Vec3_tpl<F>& v0, const Vec3_tpl<F>& v1) { Quat_tpl<F> q; q.SetRotationV0V1(v0, v1); return q; }
//! \param vdir Normalized view direction.
//! \param roll Radiant to rotate about Y-axis.
//! Given a view-direction and a radiant to rotate about Y-axis, this function builds a 3x3 look-at quaternion
//! using only simple vector arithmetic. This function is always using the implicit up-vector Vec3(0,0,1).
//! The view-direction is always stored in column(1).
//! IMPORTANT: The view-vector is assumed to be normalized, because all trig-values for the orientation are being
//! extracted directly out of the vector. This function must NOT be called with a view-direction
//! that is close to Vec3(0,0,1) or Vec3(0,0,-1). If one of these rules is broken, the function returns a quaternion
//! with an undefined rotation about the Z-axis.
//! Rotation order for the look-at-quaternion is Z-X-Y. (Zaxis=YAW / Xaxis=PITCH / Yaxis=ROLL)
//! COORDINATE-SYSTEM
//! z-axis
//! ^
//! |
//! | y-axis
//! | /
//! | /
//! |/
//! +---------------> x-axis
//! Example:
//! Quat LookAtQuat=Quat::CreateRotationVDir( Vec3(0,1,0) );
//! or
//! Quat LookAtQuat=Quat::CreateRotationVDir( Vec3(0,1,0), 0.333f );
ILINE void SetRotationVDir(const Vec3_tpl<F>& vdir)
{
CRY_MATH_ASSERT(vdir.IsUnit(0.01f));
//set default initialization for up-vector
w = F(0.70710676908493042);
v.x = F(vdir.z * 0.70710676908493042);
v.y = F(0.0);
v.z = F(0.0);
F l = sqrt_tpl(vdir.x * vdir.x + vdir.y * vdir.y);
if (l > F(0.00001))
{
//calculate LookAt quaternion
Vec3_tpl<F> hv = Vec3_tpl<F>(vdir.x / l, vdir.y / l + 1.0f, l + 1.0f);
F r = sqrt_tpl(hv.x * hv.x + hv.y * hv.y);
F s = sqrt_tpl(hv.z * hv.z + vdir.z * vdir.z);
//generate the half-angle sine&cosine
F hacos0 = 0.0;
F hasin0 = -1.0;
if (r > F(0.00001)) { hacos0 = hv.y / r; hasin0 = -hv.x / r; } //yaw
F hacos1 = hv.z / s;
F hasin1 = vdir.z / s; //pitch
w = F(hacos0 * hacos1);
v.x = F(hacos0 * hasin1);
v.y = F(hasin0 * hasin1);
v.z = F(hasin0 * hacos1);
}
}
ILINE static Quat_tpl<F> CreateRotationVDir(const Vec3_tpl<F>& vdir) { Quat_tpl<F> q; q.SetRotationVDir(vdir); return q; }
ILINE void SetRotationVDir(const Vec3_tpl<F>& vdir, F r)
{
SetRotationVDir(vdir);
F sy, cy;
sincos_tpl(r * (F)0.5, &sy, &cy);
F vx = v.x, vy = v.y;
v.x = F(vx * cy - v.z * sy);
v.y = F(w * sy + vy * cy);
v.z = F(v.z * cy + vx * sy);
w = F(w * cy - vy * sy);
}
ILINE static Quat_tpl<F> CreateRotationVDir(const Vec3_tpl<F>& vdir, F roll) { Quat_tpl<F> q; q.SetRotationVDir(vdir, roll); return q; }
//! Normalize quaternion.
//! Example 1:
//! Quat q; q.Normalize();
//! Example 2:
//! Quat q=Quat(1,2,3,4);
//! Quat qn=q.GetNormalized();
ILINE void Normalize(void)
{
F d = isqrt_tpl(w * w + v.x * v.x + v.y * v.y + v.z * v.z);
w *= d;
v.x *= d;
v.y *= d;
v.z *= d;
}
ILINE Quat_tpl<F> GetNormalized() const
{
Quat_tpl<F> t = *this;
t.Normalize();
return t;
}
ILINE void NormalizeSafe(void)
{
F d = w * w + v.x * v.x + v.y * v.y + v.z * v.z;
if (d > 1e-8f)
{
d = isqrt_tpl(d);
w *= d;
v.x *= d;
v.y *= d;
v.z *= d;
}
else
{
SetIdentity();
}
}
ILINE Quat_tpl<F> GetNormalizedSafe() const
{
Quat_tpl<F> t = *this;
t.NormalizeSafe();
return t;
}
ILINE void NormalizeFast(void)
{
CRY_MATH_ASSERT(this->IsValid());
F fInvLen = isqrt_fast_tpl(v.x * v.x + v.y * v.y + v.z * v.z + w * w);
v.x *= fInvLen;
v.y *= fInvLen;
v.z *= fInvLen;
w *= fInvLen;
}
ILINE Quat_tpl<F> GetNormalizedFast() const
{
Quat_tpl<F> t = *this;
t.NormalizeFast();
return t;
}
//! Get length of quaternion.
//! Example:
//! f32 l=q.GetLength();
ILINE F GetLength() const
{
return sqrt_tpl(w * w + v.x * v.x + v.y * v.y + v.z * v.z);
}
ILINE static bool IsEquivalent(const Quat_tpl<F>& q1, const Quat_tpl<F>& q2, F qe = RAD_EPSILON)
{
Quat_tpl<f64> q1r = q1;
Quat_tpl<f64> q2r = q2;
f64 rad = acos(min(1.0, fabs_tpl(q1r.v.x * q2r.v.x + q1r.v.y * q2r.v.y + q1r.v.z * q2r.v.z + q1r.w * q2r.w)));
bool qdif = rad <= qe;
return qdif;
}
//! Exponent of a Quaternion.
ILINE static Quat_tpl<F> exp(const Vec3_tpl<F>& v)
{
F lensqr = v.len2();
if (lensqr > F(0))
{
F len = sqrt_tpl(lensqr);
F s, c;
sincos_tpl(len, &s, &c);
s /= len;
return Quat_tpl<F>(c, v.x * s, v.y * s, v.z * s);
}
return Quat_tpl<F>(IDENTITY);
}
//! Logarithm of a quaternion, imaginary part (the real part of the logarithm is always 0).
ILINE static Vec3_tpl<F> log(const Quat_tpl<F>& q)
{
CRY_MATH_ASSERT(q.IsValid());
F lensqr = q.v.len2();
if (lensqr > 0.0f)
{
F len = sqrt_tpl(lensqr);
F angle = atan2_tpl(len, q.w) / len;
return q.v * angle;
}
// logarithm of a quaternion, imaginary part (the real part of the logarithm is always 0)
return Vec3_tpl<F>(0, 0, 0);
}
//! Logarithm of Quaternion difference.
ILINE static Quat_tpl<F> LnDif(const Quat_tpl<F>& q1, const Quat_tpl<F>& q2)
{
return Quat_tpl<F>(0, log(q2 / q1));
}
//! Linear-interpolation between quaternions (lerp).
//! Example:
//! CQuaternion result,p,q;
//! result=qlerp( p, q, 0.5f );
ILINE void SetNlerp(const Quat_tpl<F>& p, const Quat_tpl<F>& tq, F t)
{
Quat_tpl<F> q = tq;
CRY_MATH_ASSERT(p.IsValid());
CRY_MATH_ASSERT(q.IsValid());
if ((p | q) < 0) { q = -q; }
v.x = p.v.x * (1.0f - t) + q.v.x * t;
v.y = p.v.y * (1.0f - t) + q.v.y * t;
v.z = p.v.z * (1.0f - t) + q.v.z * t;
w = p.w * (1.0f - t) + q.w * t;
Normalize();
}
ILINE static Quat_tpl<F> CreateNlerp(const Quat_tpl<F>& p, const Quat_tpl<F>& tq, F t)
{
Quat_tpl<F> d;
d.SetNlerp(p, tq, t);
return d;
}
//! Linear-interpolation between unit quaternions (nlerp).
//! In this case we convert the t-value into a 1d cubic spline to get closer to Slerp.
//! Example:
//! Quat result,p,q;
//! result.SetNlerpCubic( p, q, 0.5f );
ILINE void SetNlerpCubic(const Quat_tpl<F>& p, const Quat_tpl<F>& tq, F t)
{
Quat_tpl<F> q = tq;
CRY_MATH_ASSERT((fabs_tpl(1 - (p | p))) < 0.001); //check if unit-quaternion
CRY_MATH_ASSERT((fabs_tpl(1 - (q | q))) < 0.001); //check if unit-quaternion
F cosine = (p | q);
if (cosine < 0) q = -q;
F k = (1 - fabs_tpl(cosine)) * F(0.4669269);
F s = 2 * k * t * t * t - 3 * k * t * t + (1 + k) * t;
v.x = p.v.x * (1.0f - s) + q.v.x * s;
v.y = p.v.y * (1.0f - s) + q.v.y * s;
v.z = p.v.z * (1.0f - s) + q.v.z * s;
w = p.w * (1.0f - s) + q.w * s;
Normalize();
}
ILINE static Quat_tpl<F> CreateNlerpCubic(const Quat_tpl<F>& p, const Quat_tpl<F>& tq, F t)
{
Quat_tpl<F> d;
d.SetNlerpCubic(p, tq, t);
return d;
}
//! Spherical-interpolation between unit quaternions (geometrical slerp).
ILINE void SetSlerp(const Quat_tpl<F>& tp, const Quat_tpl<F>& tq, F t)
{
CRY_MATH_ASSERT(tp.IsValid());
CRY_MATH_ASSERT(tq.IsValid());
CRY_MATH_ASSERT(tp.IsUnit());
CRY_MATH_ASSERT(tq.IsUnit());
Quat_tpl<F> p = tp, q = tq;
Quat_tpl<F> q2;
F cosine = (p | q);
if (cosine < 0.0f) { cosine = -cosine; q = -q; } //take shortest arc
if (cosine > 0.9999f)
{
SetNlerp(p, q, t);
return;
}
// from now on, a division by 0 is not possible any more
q2.w = q.w - p.w * cosine;
q2.v.x = q.v.x - p.v.x * cosine;
q2.v.y = q.v.y - p.v.y * cosine;
q2.v.z = q.v.z - p.v.z * cosine;
F sine = sqrt(q2 | q2);
// If sine is 0 here (so you get a divide by zero below), probably you passed in non-normalized quaternions!
CRY_MATH_ASSERT(sine != 0.0000f);
F s, c;
sincos_tpl(atan2_tpl(sine, cosine) * t, &s, &c);
w = F(p.w * c + q2.w * s / sine);
v.x = F(p.v.x * c + q2.v.x * s / sine);
v.y = F(p.v.y * c + q2.v.y * s / sine);
v.z = F(p.v.z * c + q2.v.z * s / sine);
}
//! Create a quaternion by spherically interpolating between unit quaternions (geometrical slerp)
ILINE static Quat_tpl<F> CreateSlerp(const Quat_tpl<F>& p, const Quat_tpl<F>& tq, F t)
{
Quat_tpl<F> d;
d.SetSlerp(p, tq, t);
return d;
}
//! Spherical-interpolation between unit quaternions (algebraic slerp_a).
//! I have included this function just for the sake of completeness, because
//! it is the only useful application to check if exp & log really work.
//! Both slerp-functions return the same result.
//! Example:
//! Quat result,p,q;
//! result.SetExpSlerp( p,q,0.3345f );
ILINE void SetExpSlerp(const Quat_tpl<F>& p, const Quat_tpl<F>& tq, F t)
{
CRY_MATH_ASSERT((fabs_tpl(1 - (p | p))) < 0.001); //check if unit-quaternion
CRY_MATH_ASSERT((fabs_tpl(1 - (tq | tq))) < 0.001); //check if unit-quaternion
Quat_tpl<F> q = tq;
if ((p | q) < 0) { q = -q; }
*this = p * exp(log(!p * q) * t); //algebraic slerp (1)
//...and some more exp-slerp-functions producing all the same result
//*this = exp( log (p* !q) * (1-t)) * q; //algebraic slerp (2)
//*this = exp( log (q* !p) * t) * p; //algebraic slerp (3)
//*this = q * exp( log (!q*p) * (1-t)); //algebraic slerp (4)
}
ILINE static Quat_tpl<F> CreateExpSlerp(const Quat_tpl<F>& p, const Quat_tpl<F>& q, F t)
{
Quat_tpl<F> d;
d.SetExpSlerp(p, q, t);
return d;
}
//! squad(p,a,b,q,t) = slerp( slerp(p,q,t),slerp(a,b,t), 2(1-t)t).
ILINE void SetSquad(const Quat_tpl<F>& p, const Quat_tpl<F>& a, const Quat_tpl<F>& b, const Quat_tpl<F>& q, F t)
{
SetSlerp(CreateSlerp(p, q, t), CreateSlerp(a, b, t), 2.0f * (1.0f - t) * t);
}
ILINE static Quat_tpl<F> CreateSquad(const Quat_tpl<F>& p, const Quat_tpl<F>& a, const Quat_tpl<F>& b, const Quat_tpl<F>& q, F t)
{
Quat_tpl<F> d;
d.SetSquad(p, a, b, q, t);
return d;
}
//! Useless, please delete.
ILINE Quat_tpl<F> GetScaled(F scale) const
{
return CreateNlerp(IDENTITY, *this, scale);
}
AUTO_STRUCT_INFO;
};
// Typedefs
#ifndef MAX_API_NUM
typedef Quat_tpl<f32> Quat; //!< Always 32 bit.
typedef Quat_tpl<f64> Quatd; //!< Always 64 bit.
typedef Quat_tpl<real> Quatr; //!< Variable float precision. depending on the target system it can be between 32, 64 or 80 bit.
#endif
typedef Quat_tpl<f32> CryQuat;
typedef Quat_tpl<f32> quaternionf;
typedef Quat_tpl<real> quaternion;
// aligned versions
#ifndef MAX_API_NUM
typedef CRY_ALIGN (16) Quat QuatA;
typedef CRY_ALIGN (32) Quatd QuatrA;
#endif
//! The "inner product" or "dot product" operation.
//! Calculate the "inner product" between two Quaternion.
//! If both Quaternion are unit-quaternions, the result is the cosine: p*q=cos(angle).
//! Example:
//! Quat p(1,0,0,0),q(1,0,0,0);
//! f32 cosine = ( p | q );
template<typename F1, typename F2> ILINE F1 operator|(const Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
CRY_MATH_ASSERT(q.v.IsValid());
CRY_MATH_ASSERT(p.v.IsValid());
return (q.v.x * p.v.x + q.v.y * p.v.y + q.v.z * p.v.z + q.w * p.w);
}
//! Implements the multiplication operator: Qua=QuatA*QuatB.
//! AxB = operation B followed by operation A.
//! A multiplication takes 16 muls and 12 adds.
//! Example 1:
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat result=p*q;
//! Example 2: (self-multiplication)
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat p*=q;
template<class F1, class F2> Quat_tpl<F1> ILINE operator*(const Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
CRY_MATH_ASSERT(q.IsValid());
CRY_MATH_ASSERT(p.IsValid());
return Quat_tpl<F1>
(
q.w * p.w - (q.v.x * p.v.x + q.v.y * p.v.y + q.v.z * p.v.z),
q.v.y * p.v.z - q.v.z * p.v.y + q.w * p.v.x + q.v.x * p.w,
q.v.z * p.v.x - q.v.x * p.v.z + q.w * p.v.y + q.v.y * p.w,
q.v.x * p.v.y - q.v.y * p.v.x + q.w * p.v.z + q.v.z * p.w
);
}
template<class F1, class F2> ILINE void operator*=(Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
CRY_MATH_ASSERT(q.IsValid());
CRY_MATH_ASSERT(p.IsValid());
F1 s0 = q.w;
q.w = q.w * p.w - (q.v | p.v);
q.v = p.v * s0 + q.v * p.w + (q.v % p.v);
}
//! Implements the multiplication operator: QuatT=Quat*Quatpos.
//! AxB = operation B followed by operation A.
//! A multiplication takes 31 muls and 27 adds (=58 float operations).
//! Example:
//! Quat quat = Quat::CreateRotationX(1.94192f);
//! QuatT quatpos = QuatT::CreateRotationZ(3.14192f,Vec3(11,22,33));
//! QuatT qp = quat*quatpos;
template<class F1, class F2> ILINE QuatT_tpl<F1> operator*(const Quat_tpl<F1>& q, const QuatT_tpl<F2>& p)
{
return QuatT_tpl<F1>(q * p.q, q * p.t);
}
//! Example 1:
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat result=p/q;
//! Example 2: (self-division)
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat p/=q;
template<class F1, class F2> ILINE Quat_tpl<F1> operator/(const Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
return (!p * q);
}
template<class F1, class F2> ILINE void operator/=(Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
q = (!p * q);
}
//! Example:
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat result=p+q;
//! Example:(self-addition operator)
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat p-=q;
template<class F1, class F2> ILINE Quat_tpl<F1> operator+(const Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
return Quat_tpl<F1>(q.w + p.w, q.v + p.v);
}
template<class F1, class F2> ILINE void operator+=(Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
q.w += p.w;
q.v += p.v;
}
//! Example:
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat result=p-q;
//! Example: (self-subtraction operator)
//! Quat p(1,0,0,0),q(1,0,0,0);
//! Quat p-=q;
template<class F1, class F2> ILINE Quat_tpl<F1> operator-(const Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
return Quat_tpl<F1>(q.w - p.w, q.v - p.v);
}
template<class F1, class F2> ILINE void operator-=(Quat_tpl<F1>& q, const Quat_tpl<F2>& p)
{
q.w -= p.w;
q.v -= p.v;
}
//! Scale quaternion free function.
template<typename F> ILINE Quat_tpl<F> operator*(F t, const Quat_tpl<F>& q)
{
return Quat_tpl<F>(t * q.w, t * q.v);
};
template<typename F1, typename F2> ILINE Quat_tpl<F1> operator*(const Quat_tpl<F1>& q, F2 t)
{
return Quat_tpl<F1>(q.w * t, q.v * t);
};
template<typename F1, typename F2> ILINE Quat_tpl<F1> operator/(const Quat_tpl<F1>& q, F2 t)
{
return Quat_tpl<F1>(q.w / t, q.v / t);
};
//! Post-multiply of a quaternion and a Vec3 (3D rotations with quaternions).
//! Example:
//! Quat q(1,0,0,0);
//! Vec3 v(33,44,55);
//! Vec3 result = q*v;
template<class F, class F2> ILINE Vec3_tpl<F> operator*(const Quat_tpl<F>& q, const Vec3_tpl<F2>& v)
{
CRY_MATH_ASSERT(v.IsValid());
CRY_MATH_ASSERT(q.IsValid());
//muls=15 / adds=15
Vec3_tpl<F> out, r2;
r2.x = (q.v.y * v.z - q.v.z * v.y) + q.w * v.x;
r2.y = (q.v.z * v.x - q.v.x * v.z) + q.w * v.y;
r2.z = (q.v.x * v.y - q.v.y * v.x) + q.w * v.z;
out.x = (r2.z * q.v.y - r2.y * q.v.z);
out.x += out.x + v.x;
out.y = (r2.x * q.v.z - r2.z * q.v.x);
out.y += out.y + v.y;
out.z = (r2.y * q.v.x - r2.x * q.v.y);
out.z += out.z + v.z;
return out;
}
//! Pre-multiply of a quaternion and a Vec3 (3D rotations with quaternions).
//! Example:
//! Quat q(1,0,0,0);
//! Vec3 v(33,44,55);
//! Vec3 result = v*q;
template<class F, class F2> ILINE Vec3_tpl<F2> operator*(const Vec3_tpl<F>& v, const Quat_tpl<F2>& q)
{
CRY_MATH_ASSERT(v.IsValid());
CRY_MATH_ASSERT(q.IsValid());
//muls=15 / adds=15
Vec3_tpl<F> out, r2;
r2.x = (q.v.z * v.y - q.v.y * v.z) + q.w * v.x;
r2.y = (q.v.x * v.z - q.v.z * v.x) + q.w * v.y;
r2.z = (q.v.y * v.x - q.v.x * v.y) + q.w * v.z;
out.x = (r2.y * q.v.z - r2.z * q.v.y);
out.x += out.x + v.x;
out.y = (r2.z * q.v.x - r2.x * q.v.z);
out.y += out.y + v.y;
out.z = (r2.x * q.v.y - r2.y * q.v.x);
out.z += out.z + v.z;
return out;
}
template<class F1, class F2> ILINE Quat_tpl<F1> operator%(const Quat_tpl<F1>& q, const Quat_tpl<F2>& tp)
{
Quat_tpl<F1> p = tp;
if ((p | q) < 0) { p = -p; }
return Quat_tpl<F1>(q.w + p.w, q.v + p.v);
}
template<class F1, class F2> ILINE void operator%=(Quat_tpl<F1>& q, const Quat_tpl<F2>& tp)
{
Quat_tpl<F1> p = tp;
if ((p | q) < 0) { p = -p; }
q = Quat_tpl<F1>(q.w + p.w, q.v + p.v);
}
// Quaternion with translation vector.
template<typename F> struct QuatT_tpl
{
Quat_tpl<F> q; //this is the quaternion
Vec3_tpl<F> t; //this is the translation vector and a scalar (for uniform scaling?)
ILINE QuatT_tpl(){}
//! Initialize with zeros.
ILINE QuatT_tpl(type_zero)
{
q.w = 0, q.v.x = 0, q.v.y = 0, q.v.z = 0, t.x = 0, t.y = 0, t.z = 0;
}
ILINE QuatT_tpl(type_identity)
{
q.w = 1, q.v.x = 0, q.v.y = 0, q.v.z = 0, t.x = 0, t.y = 0, t.z = 0;
}
ILINE QuatT_tpl(const Vec3_tpl<F>& _t, const Quat_tpl<F>& _q) { q = _q; t = _t; }
//! CONSTRUCTOR: implement the copy/casting/assignment constructor.
template<typename F1> ILINE QuatT_tpl(const QuatT_tpl<F1>& qt) : q(qt.q), t(qt.t) {}
//! Convert unit DualQuat back to QuatT.
ILINE QuatT_tpl(const DualQuat_tpl<F>& qd)
{
//copy quaternion part
q = qd.nq;
//convert translation vector:
t = (qd.nq.w * qd.dq.v - qd.dq.w * qd.nq.v + qd.nq.v % qd.dq.v) * 2; //perfect for HLSL
}
explicit ILINE QuatT_tpl(const QuatTS_tpl<F>& qts) : q(qts.q), t(qts.t) {}
explicit ILINE QuatT_tpl(const Matrix34_tpl<F>& m)
{
q = Quat_tpl<F>(Matrix33(m));
t = m.GetTranslation();
}
ILINE QuatT_tpl(const Quat_tpl<F>& quat, const Vec3_tpl<F>& trans)
{
q = quat;
t = trans;
}
ILINE void SetIdentity()
{
q.w = 1;
q.v.x = 0;
q.v.y = 0;
q.v.z = 0;
t.x = 0;
t.y = 0;
t.z = 0;
}
ILINE bool IsIdentity() const
{
return (q.IsIdentity() && t.IsZero());
}
//! Convert three Euler angle to mat33 (rotation order:XYZ).
//! The Euler angles are assumed to be in radians.
//! The translation-vector is set to zero by default.
//! Example 1:
//! QuatT qp;
//! qp.SetRotationXYZ( Ang3(0.5f,0.2f,0.9f), translation );
//! Example 2:
//! QuatT qp=QuatT::CreateRotationXYZ( Ang3(0.5f,0.2f,0.9f), translation );
ILINE void SetRotationXYZ(const Ang3_tpl<F>& rad, const Vec3_tpl<F>& trans = Vec3(ZERO))
{
CRY_MATH_ASSERT(rad.IsValid());
CRY_MATH_ASSERT(trans.IsValid());
q.SetRotationXYZ(rad);
t = trans;
}
ILINE static QuatT_tpl<F> CreateRotationXYZ(const Ang3_tpl<F>& rad, const Vec3_tpl<F>& trans = Vec3(ZERO))
{
CRY_MATH_ASSERT(rad.IsValid());
CRY_MATH_ASSERT(trans.IsValid());
QuatT_tpl<F> qp;
qp.SetRotationXYZ(rad, trans);
return qp;
}
ILINE void SetRotationAA(F cosha, F sinha, const Vec3_tpl<F> axis, const Vec3_tpl<F>& trans = Vec3(ZERO))
{
q.SetRotationAA(cosha, sinha, axis);
t = trans;