-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quat.cs
1325 lines (1200 loc) · 39.6 KB
/
Quat.cs
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
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// A four-dimensional complex number. The x, y and z components are
/// coefficients of the imaginary i, j, and k. Discovered by William R. Hamilton
/// with the formula i i = j j = k k = i j k = -1.0 . Quaternions with a
/// magnitude of 1.0 are commonly used to rotate 3D objects from one orientation
/// to another without suffering gimbal lock.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit)]
public readonly struct Quat : IEquatable<Quat>, IEnumerable
{
/// <summary>
/// The coefficients of the imaginary components i, j and k.
/// </summary>
[FieldOffset(4)] private readonly Vec3 imag;
/// <summary>
/// The real component.
/// </summary>
[FieldOffset(0)] private readonly float real;
/// <summary>
/// The forward axis.
/// </summary>
/// <value>forward</value>
public Vec3 Forward
{
get
{
float w = this.real;
float x = this.imag.X;
float y = this.imag.Y;
float z = this.imag.Z;
float xy = x * y;
float xw = x * w;
float yz = y * z;
float zw = z * w;
return new Vec3(
xy + xy - (zw + zw),
w * w + y * y - x * x - z * z,
xw + xw + yz + yz);
}
}
/// <summary>
/// The coefficients of the imaginary components i, j and k.
/// </summary>
/// <value>imaginary vector</value>
public Vec3 Imag { get { return this.imag; } }
/// <summary>
/// Returns the number of elements in this quaternion.
/// </summary>
/// <value>length</value>
public int Length { get { return 1 + this.imag.Length; } }
/// <summary>
/// The real component.
/// </summary>
/// <value>real scalar</value>
public float Real { get { return this.real; } }
/// <summary>
/// The right axis.
/// </summary>
/// <value>right</value>
public Vec3 Right
{
get
{
float w = this.real;
float x = this.imag.X;
float y = this.imag.Y;
float z = this.imag.Z;
float xy = x * y;
float xz = x * z;
float yw = y * w;
float zw = z * w;
return new Vec3(
w * w + x * x - y * y - z * z,
zw + zw + xy + xy,
xz + xz - (yw + yw));
}
}
/// <summary>
/// The up axis.
/// </summary>
/// <value>up</value>
public Vec3 Up
{
get
{
float w = this.real;
float x = this.imag.X;
float y = this.imag.Y;
float z = this.imag.Z;
float xz = x * z;
float xw = x * w;
float yz = y * z;
float yw = y * w;
return new Vec3(
yw + yw + xz + xz,
yz + yz - (xw + xw),
w * w + z * z - x * x - y * y);
}
}
/// <summary>
/// The real component.
/// </summary>
/// <value>w</value>
public float W { get { return this.real; } }
/// <summary>
/// The coefficient of the imaginary i.
/// </summary>
/// <value>x</value>
public float X { get { return this.imag.X; } }
/// <summary>
/// The coefficient of the imaginary j.
/// </summary>
/// <value>y</value>
public float Y { get { return this.imag.Y; } }
/// <summary>
/// The coefficient of the imaginary k.
/// </summary>
/// <value>z</value>
public float Z { get { return this.imag.Z; } }
/// <summary>
/// Retrieves an element by index. The real component is assumed to be the
/// first element.
/// </summary>
/// <value>element</value>
public float this[int i]
{
get
{
return i switch
{
0 or -4 => this.real,
1 or -3 => this.imag.X,
2 or -2 => this.imag.Y,
3 or -1 => this.imag.Z,
_ => 0.0f,
};
}
}
/// <summary>
/// Constructs a quaternion from a real number and an imaginary vector.
/// </summary>
/// <param name="real">real number</param>
/// <param name="imag">imaginary vector</param>
public Quat(in float real = 1.0f, in Vec3 imag = new Vec3())
{
this.real = real;
this.imag = imag;
}
/// <summary>
/// Constructs a quaternion from a real number, w, and three imaginary
/// numbers: x, y and z.
/// </summary>
/// <param name="w">real number</param>
/// <param name="x">x imaginary</param>
/// <param name="y">y imaginary</param>
/// <param name="z">z imaginary</param>
public Quat(in float w = 1.0f, in float x = 0.0f, in float y = 0.0f, in float z = 0.0f)
{
this.real = w;
this.imag = new Vec3(x, y, z);
}
/// <summary>
/// Tests this quaternion for equivalence with an object.
/// </summary>
/// <param name="value">object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is Quat quat) { return this.Equals(quat); }
return false;
}
/// <summary>
/// Returns a hash code representing this quaternion.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
unchecked
{
int hash = Utils.HashBase;
hash = hash * Utils.HashMul ^ this.real.GetHashCode();
hash = hash * Utils.HashMul ^ this.imag.GetHashCode();
return hash;
}
}
/// <summary>
/// Returns a string representation of this quaternion.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Quat.ToString(this);
}
/// <summary>
/// Tests this quaternion for equivalence with another in compliance with
/// the IEquatable interface.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>equivalence</returns>
public bool Equals(Quat q)
{
if (this.real.GetHashCode() != q.real.GetHashCode()) { return false; }
if (this.imag.GetHashCode() != q.imag.GetHashCode()) { return false; }
return true;
}
/// <summary>
/// Returns an enumerator (or iterator) for this quaternion, allowing its
/// components to be accessed in a foreach loop. The real component, w, is
/// treated as the first element.
/// </summary>
/// <returns>enumerator</returns>
public IEnumerator GetEnumerator()
{
yield return this.real;
yield return this.imag.X;
yield return this.imag.Y;
yield return this.imag.Z;
}
/// <summary>
/// Promotes a real number to a quaternion.
/// </summary>
/// <param name="real">real number</param>
public static implicit operator Quat(in float real)
{
return new(real, Vec3.Zero);
}
/// <summary>
/// Promotes an imaginary vector to a pure quaternion.
/// </summary>
/// <param name="imag">vector</param>
public static implicit operator Quat(in Vec3 imag)
{
return new(0.0f, new Vec3(imag.X, imag.Y, imag.Z));
}
/// <summary>
/// Converts a four dimensional vector to a quaternion. The vector's w
/// component is interpreted to be the real component.
/// </summary>
/// <param name="v">vector</param>
public static explicit operator Quat(in Vec4 v)
{
return new(v.W, new Vec3(v.X, v.Y, v.Z));
}
/// <summary>
/// Converts a quaternion to a four dimensional vector. The quaternion's
/// real component is interpreted to be the w component.
/// </summary>
/// <param name="q">quaternion</param>
public static explicit operator Vec4(in Quat q)
{
Vec3 i = q.imag;
return new Vec4(i.X, i.Y, i.Z, q.real);
}
/// <summary>
/// Converts a quaternion to a boolean by finding whether any of its
/// components are non-zero.
/// </summary>
/// <param name="q">quaternion</param>
public static explicit operator bool(in Quat q)
{
return Quat.Any(q);
}
/// <summary>
/// A quaternion evaluates to true when any of its components are not equal
/// to zero.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool operator true(in Quat q)
{
return Quat.Any(q);
}
/// <summary>
/// A quaternion evaluates to false when all of its components are zero.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool operator false(in Quat q)
{
return Quat.None(q);
}
/// <summary>
/// Negates the real and imaginary components of a quaternion.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>negation</returns>
public static Quat operator -(in Quat q)
{
return new(-q.real, -q.imag);
}
/// <summary>
/// Multiplies two quaternions. Uses the formula:
///
/// a b := { a.real b.real - dot ( a.imag, b.imag ), cross ( a.imag, b.imag
/// ) + a.real b.imag + b.real a.imag }
///
/// Quaternion multiplication is not commutative.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Quat operator *(in Quat a, in Quat b)
{
return new(
(a.real * b.real) -
Vec3.Dot(a.imag, b.imag),
Vec3.Cross(a.imag, b.imag) +
(a.real * b.imag) +
(b.real * a.imag));
}
/// <summary>
/// Multiplies a quaternion and a scalar.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Quat operator *(in Quat a, in float b)
{
return new(a.real * b, a.imag * b);
}
/// <summary>
/// Multiplies a scalar and a quaternion.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Quat operator *(in float a, in Quat b)
{
return new(a * b.real, a * b.imag);
}
/// <summary>
/// Multiplies a quaternion and a vector. The latter is treated as a pure
/// quaternion, not as a point. Either see MulVector method or use RPR'.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Quat operator *(in Quat a, in Vec3 b)
{
return new(-Vec3.Dot(a.imag, b),
Vec3.Cross(a.imag, b) + (a.real * b));
}
/// <summary>
/// Multiplies a vector and a quaternion. The former is treated as a pure
/// quaternion, not as a point. Either see MulVector method or use RPR'.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Quat operator *(in Vec3 a, in Quat b)
{
return new(-Vec3.Dot(a, b.imag),
Vec3.Cross(a, b.imag) + (b.real * a));
}
/// <summary>
/// Divides two quaternions. Equivalent to multiplying the numerator and the
/// inverse of the denominator.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Quat operator /(in Quat a, in Quat b)
{
if (Quat.Any(b))
{
return a * Quat.Inverse(b);
}
return Quat.Identity;
}
/// <summary>
/// Divides a quaternion by a real number.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Quat operator /(in Quat a, in float b)
{
if (b != 0.0f)
{
float bInv = 1.0f / b;
return new(a.real * bInv, a.imag * bInv);
}
return Quat.Identity;
}
/// <summary>
/// Divides a real number by a quaternion.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Quat operator /(in float a, in Quat b)
{
if (Quat.Any(b))
{
return a * Quat.Inverse(b);
}
return Quat.Identity;
}
/// <summary>
/// Divides a quaternion by a vector. the denominator is treated as a pure
/// quaternion.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Quat operator /(in Quat a, in Vec3 b)
{
if (Vec3.Any(b))
{
return a * (-b / Vec3.MagSq(b));
}
return Quat.Identity;
}
/// <summary>
/// Divides a vector by a quaternion; the numerator is treated as a pure
/// quaternion.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Quat operator /(in Vec3 a, in Quat b)
{
if (Quat.Any(b))
{
return a * Quat.Inverse(b);
}
return Quat.Identity;
}
/// <summary>
/// Adds two quaternions.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Quat operator +(in Quat a, in Quat b)
{
return new(a.real + b.real, a.imag + b.imag);
}
/// <summary>
/// Adds a real number and a quaternion.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Quat operator +(in Quat a, in float b)
{
return new(a.real + b, a.imag);
}
/// <summary>
/// Adds a real number and a quaternion.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Quat operator +(in float a, in Quat b)
{
return new(a + b.real, b.imag);
}
/// <summary>
/// Adds an imaginary vector and a quaternion.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Quat operator +(in Quat a, in Vec3 b)
{
return new(a.real, a.imag + b);
}
/// <summary>
/// Adds an imaginary vector and a quaternion.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Quat operator +(in Vec3 a, in Quat b)
{
return new(b.real, a + b.imag);
}
/// <summary>
/// Subtracts the right quaternion from the left.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Quat operator -(in Quat a, in Quat b)
{
return new(a.real - b.real, a.imag - b.imag);
}
/// <summary>
/// Subtracts a real number from a quaternion.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Quat operator -(in Quat a, in float b)
{
return new(a.real - b, a.imag);
}
/// <summary>
/// Subtracts a quaternion from a real number.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Quat operator -(in float a, in Quat b)
{
return new(a - b.real, -b.imag);
}
/// <summary>
/// Subtracts an imaginary vector from a quaternion.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Quat operator -(in Quat a, in Vec3 b)
{
return new(a.real, a.imag - b);
}
/// <summary>
/// Subtracts a quaternion from an imaginary vector.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Quat operator -(in Vec3 a, in Quat b)
{
return new(-b.real, a - b.imag);
}
/// <summary>
/// Tests to see if all the quaternion's components are non-zero.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool All(in Quat q)
{
return (q.real != 0.0f) && Vec3.All(q.imag);
}
/// <summary>
/// Tests to see if any of the quaternion's components are non-zero.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool Any(in Quat q)
{
return (q.real != 0.0f) || Vec3.Any(q.imag);
}
/// <summary>
/// Evaluates whether or not two quaternions approximate each other.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <param name="tol">tolerance</param>
/// <returns>evaluation</returns>
public static bool Approx(
in Quat a, in Quat b,
in float tol = Utils.Epsilon)
{
return Utils.Approx(a.real, b.real, tol) &&
Vec3.Approx(a.imag, b.imag, tol);
}
/// <summary>
/// Returns the conjugate of the quaternion, where the imaginary component
/// is negated.
///
/// a* := { a.real, -a.imag }
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>conjugate</returns>
public static Quat Conj(in Quat q)
{
return new(q.real, -q.imag);
}
/// <summary>
/// Finds the dot product of two quaternions by summing the products of
/// their corresponding components.
///
/// dot ( a, b ) := a.real b.real + dot ( a.imag, b.imag )
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>dot product</returns>
public static float Dot(in Quat a, in Quat b)
{
return a.real * b.real + Vec3.Dot(a.imag, b.imag);
}
/// <summary>
/// Sets a quaternion from an angle. The axis is assumed to be (0.0, 0.0,
/// 1.0) . Sets the real component of the quaternion to cosine of the angle;
/// the imaginary z component is set to the sine. Useful when working in
/// 2.5D, where a two-dimensional angle may need to be transferred to a
/// three-dimensional transform.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>quaternion</returns>
public static Quat FromAngle(in float radians)
{
float rHalf = radians % Utils.Tau * 0.5f;
float cosa = MathF.Cos(rHalf);
float sina = MathF.Sin(rHalf);
return new(cosa, 0.0f, 0.0f, sina);
}
/// <summary>
/// Creates a quaternion from an axis and angle. Normalizes the axis.
/// If the axis has no magnitude, returns the identity.
/// </summary>
/// <param name="radians">angle</param>
/// <param name="axis">axis</param>
/// <returns>quaternion</returns>
public static Quat FromAxisAngle(in float radians, in Vec3 axis)
{
float ax = axis.X;
float ay = axis.Y;
float az = axis.Z;
float amSq = ax * ax + ay * ay + az * az;
if (amSq > 0.0f)
{
float rHalf = radians % Utils.Tau * 0.5f;
float amInv = MathF.Sin(rHalf) / MathF.Sqrt(amSq);
return new(MathF.Cos(rHalf), new Vec3(
ax * amInv,
ay * amInv,
az * amInv));
}
return Quat.Identity;
}
/// <summary>
/// Creates a quaternion from spherical coordinates. The quaternion's right
/// axis corresponds to the point on the sphere, i.e., what would be
/// returned from Vec3.FromSpherical.
/// </summary>
/// <param name="azimuth">azimuth</param>
/// <param name="inclination">axis</param>
/// <returns>quaternion</returns>
public static Quat FromSpherical(in float azimuth, in float inclination)
{
float azHalf = 0.5f * (azimuth % Utils.Tau);
float cosAzim = MathF.Cos(azHalf);
float sinAzim = MathF.Sin(azHalf);
float inHalf = Utils.Tau - inclination * 0.5f;
float cosIncl = MathF.Cos(inHalf);
float sinIncl = MathF.Sin(inHalf);
return new(
cosAzim * cosIncl,
sinAzim * -sinIncl,
sinIncl * cosAzim,
sinAzim * cosIncl);
}
/// <summary>
/// Creates a quaternion with reference to two vectors. This function
/// creates normalized copies of the vectors. Uses the formula:
///
/// fromTo (a, b) := { a . b, a x b }
/// </summary>
/// <param name="origin">origin</param>
/// <param name="dest">destination</param>
/// <returns>quaternion</returns>
public static Quat FromTo(in Vec3 origin, in Vec3 dest)
{
Vec3 o = Vec3.Normalize(origin);
Vec3 d = Vec3.Normalize(dest);
return new(Vec3.Dot(o, d), Vec3.Cross(o, d));
}
/// <summary>
/// Finds the inverse, or reciprocal, of a quaternion, which is the its
/// conjugate divided by its magnitude squared.
///
/// 1 / a := a* / ( a a* )
///
/// If a quaternion is of unit length, its inverse is equal to its
/// conjugate.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>inverse</returns>
public static Quat Inverse(in Quat q)
{
// This should be inlined to avoid circularity with division
// operator definitions above, where divide by zero must return
// consistent results for floats, vectors, quaternions.
Vec3 i = q.imag;
float mSq = q.real * q.real + Vec3.MagSq(i);
if (mSq != 0.0f)
{
float msi = 1.0f / mSq;
return new(q.real * msi, -i.X * msi, -i.Y * msi, -i.Z * msi);
}
return Quat.Identity;
}
/// <summary>
/// Multiplies a vector by a quaternion's inverse, allowing a prior
/// rotation to be undone.
/// </summary>
/// <param name="q">quaternion</param>
/// <param name="v">input vector</param>
/// <returns>the unrotated vector</returns>
public static Vec3 InvMulVector(in Quat q, in Vec3 v)
{
float mSq = Quat.MagSq(q);
if (mSq > 0.0f)
{
float mSqInv = 1.0f / mSq;
float w = q.real * mSqInv;
Vec3 i = q.imag;
float qx = -i.X * mSqInv;
float qy = -i.Y * mSqInv;
float qz = -i.Z * mSqInv;
float iw = -qx * v.X - qy * v.Y - qz * v.Z;
float ix = w * v.X + qy * v.Z - qz * v.Y;
float iy = w * v.Y + qz * v.X - qx * v.Z;
float iz = w * v.Z + qx * v.Y - qy * v.X;
return new Vec3(
ix * w + iz * qy - iw * qx - iy * qz,
iy * w + ix * qz - iw * qy - iz * qx,
iz * w + iy * qx - iw * qz - ix * qy);
}
return Vec3.Zero;
}
/// <summary>
/// Tests if the quaternion is the identity, where its real component is 1.0
/// and its imaginary components are all zero.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool IsIdentity(in Quat q)
{
return q.real == 1.0f && Vec3.None(q.imag);
}
/// <summary>
/// Tests to see if a quaternion is pure, i.e. if its real component is
/// zero.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool IsPure(in Quat q)
{
return q.real == 0.0f;
}
/// <summary>
/// Tests if the quaternion is of unit magnitude.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool IsVersor(in Quat q)
{
return Utils.Approx(Quat.MagSq(q), 1.0f);
}
/// <summary>
/// Finds the length, or magnitude, of a quaternion.
///
/// |a| := sqrt ( dot ( a, a ) )
///
/// |a| := sqrt ( a a* )
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>magnitude</returns>
public static float Mag(in Quat q)
{
return MathF.Sqrt(Quat.MagSq(q));
}
/// <summary>
/// Finds the magnitude squared of a quaternion. Equivalent to the dot
/// product of a quaternion with itself and to the product of a quaternion
/// with its conjugate.
///
/// |a|<sup>2</sup> := dot ( a, a )
///
/// |a|<sup>2</sup> := a a*
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>magnitude squared</returns>
public static float MagSq(in Quat q)
{
return q.real * q.real + Vec3.MagSq(q.imag);
}
/// <summary>
/// Eases between two quaternions by spherical linear interpolation (slerp).
/// Chooses the shortest path between two orientations and maintains
/// constant speed for a step in [0.0, 1.0] .
/// </summary>
/// <param name="o">origin</param>
/// <param name="d">destination</param>
/// <param name="step">step</param>
/// <returns>quaternion</returns>
public static Quat Mix(in Quat o, in Quat d, in float step = 0.5f)
{
// Decompose origin quaternion.
Vec3 oi = o.imag;
float ow = o.real;
float ox = oi.X;
float oy = oi.Y;
float oz = oi.Z;
// Decompose destination quaternion.
Vec3 di = d.imag;
float dw = d.real;
float dx = di.X;
float dy = di.Y;
float dz = di.Z;
// Clamped dot product.
float dotp = Utils.Clamp(
ow * dw +
ox * dx +
oy * dy +
oz * dz, -1.0f, 1.0f);
// Flip values if the orientation is negative.
if (dotp < 0.0f)
{
dw = -dw;
dx = -dx;
dy = -dy;
dz = -dz;
dotp = -dotp;
}
float theta = MathF.Acos(dotp);
float sinTheta = MathF.Sqrt(1.0f - dotp * dotp);
// The complementary step, i.e., 1.0 - step.
float u;
// The step.
float v;
if (sinTheta > Utils.Epsilon)
{
float sInv = 1.0f / sinTheta;
float ang = step * theta;
u = MathF.Sin(theta - ang) * sInv;
v = MathF.Sin(ang) * sInv;
}
else
{
u = 1.0f - step;
v = step;
}
// Unclamped linear interpolation.
float cw = u * ow + v * dw;
float cx = u * ox + v * dx;
float cy = u * oy + v * dy;
float cz = u * oz + v * dz;
// Find magnitude squared.
float mSq = cw * cw + cx * cx + cy * cy + cz * cz;
// If 0, then invalid, reset to identity.
if (MathF.Abs(mSq) < Utils.Epsilon)
{
return Quat.Identity;
}
// If 1, no need to normalize.
if (MathF.Abs(1.0f - mSq) < Utils.Epsilon)
{
return new(cw, cx, cy, cz);
}
// Normalize.
float mInv = 1.0f / MathF.Sqrt(mSq);
return new(
cw * mInv,
cx * mInv,
cy * mInv,
cz * mInv);
}
/// <summary>
/// Rotates a vector by a quaternion. Equivalent to promoting the vector to
/// a pure quaternion, multiplying the rotation quaternion and promoted
/// vector, then multiplying the product by the rotation's inverse.
///
/// a b := ( a { 0.0, b } ) / a
///
/// The result is then demoted to a vector, as the real component should be
/// 0.0 . This is often denoted as P' = RPR'.
/// </summary>
/// <param name="q">quaternion</param>
/// <param name="v">input vector</param>
/// <returns>rotated vector</returns>
public static Vec3 MulVector(in Quat q, in Vec3 v)
{
// Quat product = (q * source) / q; return product.imag;
float w = q.real;
Vec3 i = q.imag;
float qx = i.X;
float qy = i.Y;
float qz = i.Z;
float iw = -qx * v.X - qy * v.Y - qz * v.Z;
float ix = w * v.X + qy * v.Z - qz * v.Y;
float iy = w * v.Y + qz * v.X - qx * v.Z;
float iz = w * v.Z + qx * v.Y - qy * v.X;
return new Vec3(
ix * w + iz * qy - iw * qx - iy * qz,
iy * w + ix * qz - iw * qy - iz * qx,
iz * w + iy * qx - iw * qz - ix * qy);
}
/// <summary>
/// Tests if all components of the quaternion are zero.
/// </summary>
/// <param name="q">quaternion</param>
/// <returns>evaluation</returns>
public static bool None(in Quat q)
{
return q.real == 0.0f && Vec3.None(q.imag);
}
/// <summary>
/// Divides a quaternion by its magnitude, such that its new magnitude is
/// one and it lies on a 4D hyper-sphere. Uses the formula:
///
/// a^ = a / |a|