-
Notifications
You must be signed in to change notification settings - Fork 17
/
Abacus.Double.cs
1800 lines (1539 loc) · 126 KB
/
Abacus.Double.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
// ┌────────────────────────────────────────────────────────────────────────┐ \\
// │ _____ ___. │ \\
// │ / _ \\_ |__ _____ ____ __ __ ______ │ \\
// │ / /_\ \| __ \\__ \ _/ ___\| | \/ ___/ │ \\
// │ / | \ \_\ \/ __ \\ \___| | /\___ \ │ \\
// │ \____|__ /___ (____ /\___ >____//____ > │ \\
// │ \/ \/ \/ \/ \/ v1.1.0 │ \\
// │ │ \\
// │ Fast, efficient, cross platform, cross precision, maths library. │ \\
// │ │ \\
// │ ________________________________________________________ │ \\
// │ / ____________________________________________________ \ │ \\
// │ | |<_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> | | │ \\
// │ | |<_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> | | │ \\
// │ | |_|___|___|___|___|___|___|___|___|___|___|___|___|__| | │ \\
// │ | ____________________________________________________ | │ \\
// │ | | | | | | | | | | | | | | | | | │ \\
// │ | |<_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> | | │ \\
// │ | |<_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> | | │ \\
// │ | |<_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> | | │ \\
// │ | |<_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> <_> | | │ \\
// │ | |<_>_<_>_<_>_<_>_<_>_<_>_<_>_<_>_<_>_<_>_<_>_<_>_<_>_| | │ \\
// │ \________________________________________________________/ │ \\
// │ │ \\
// ├────────────────────────────────────────────────────────────────────────┤ \\
// │ Copyright © 2012 - 2020 Ash Pook │ \\
// ├────────────────────────────────────────────────────────────────────────┤ \\
// │ Permission is hereby granted, free of charge, to any person obtaining │ \\
// │ a copy of this software and associated documentation files (the │ \\
// │ "Software"), to deal in the Software without restriction, including │ \\
// │ without limitation the rights to use, copy, modify, merge, publish, │ \\
// │ distribute, sublicense, and/or sellcopies of the Software, and to │ \\
// │ permit persons to whom the Software is furnished to do so, subject to │ \\
// │ the following conditions: │ \\
// │ │ \\
// │ The above copyright notice and this permission notice shall be │ \\
// │ included in all copies or substantial portions of the Software. │ \\
// │ │ \\
// │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ \\
// │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ \\
// │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ \\
// │ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ \\
// │ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ \\
// │ TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE │ \\
// │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ \\
// └────────────────────────────────────────────────────────────────────────┘ \\
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using MI = System.Runtime.CompilerServices.MethodImplAttribute;
using O = System.Runtime.CompilerServices.MethodImplOptions;
namespace Abacus.DoublePrecision
{
/// <summary>
/// Double precision Quaternion.
/// </summary>
[StructLayout (LayoutKind.Sequential), Serializable]
public struct Quaternion : IEquatable<Quaternion> {
public Double I, J, K, U;
[MI(O.AggressiveInlining)] public Quaternion (Double i, Double j, Double k, Double u) { I = i; J = j; K = k; U = u; }
[MI(O.AggressiveInlining)] public Quaternion (Vector3 vectorPart, Double scalarPart) { I = vectorPart.X; J = vectorPart.Y; K = vectorPart.Z; U = scalarPart; }
public override String ToString () { return String.Format ("(I:{0}, J:{1}, K:{2}, U:{3})", I, J, K, U); }
[MI(O.AggressiveInlining)] public override Int32 GetHashCode () {
return U.GetHashCode ().ShiftAndWrap (6) ^ K.GetHashCode ().ShiftAndWrap (4)
^ J.GetHashCode ().ShiftAndWrap (2) ^ I.GetHashCode ();
}
[MI(O.AggressiveInlining)] public override Boolean Equals (Object obj) { return (obj is Quaternion) ? this.Equals ((Quaternion) obj) : false; }
[MI(O.AggressiveInlining)] public Boolean Equals (Quaternion other) { Boolean r; Equals (ref this, ref other, out r); return r; }
[MI(O.AggressiveInlining)] public Boolean ApproximateEquals (Quaternion other) { Boolean r; ApproximateEquals (ref this, ref other, out r); return r; }
// Constants //-------------------------------------------------------//
static Quaternion identity, zero;
static Quaternion () {
identity = new Quaternion (0, 0, 0, 1);
zero = new Quaternion (0, 0, 0, 0);
}
public static Quaternion Identity { get { return identity; } }
public static Quaternion Zero { get { return zero; } }
// Operators //-------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void Equals (ref Quaternion q1, ref Quaternion q2, out Boolean r) {
r = (q1.I == q2.I) && (q1.J == q2.J) && (q1.K == q2.K) && (q1.U == q2.U);
}
[MI(O.AggressiveInlining)] public static void ApproximateEquals (ref Quaternion q1, ref Quaternion q2, out Boolean r) {
r = Maths.ApproximateEquals (q1.I, q2.I) && Maths.ApproximateEquals (q1.J, q2.J)
&& Maths.ApproximateEquals (q1.K, q2.K) && Maths.ApproximateEquals (q1.U, q2.U);
}
[MI(O.AggressiveInlining)] public static void Add (ref Quaternion q1, ref Quaternion q2, out Quaternion r) {
r.I = q1.I + q2.I; r.J = q1.J + q2.J; r.K = q1.K + q2.K; r.U = q1.U + q2.U;
}
[MI(O.AggressiveInlining)] public static void Subtract (ref Quaternion q1, ref Quaternion q2, out Quaternion r) {
r.I = q1.I - q2.I; r.J = q1.J - q2.J; r.K = q1.K - q2.K; r.U = q1.U - q2.U;
}
[MI(O.AggressiveInlining)] public static void Negate (ref Quaternion quaternion, out Quaternion r) {
r.I = -quaternion.I; r.J = -quaternion.J; r.K = -quaternion.K; r.U = -quaternion.U;
}
// http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/arithmetic/index.htm
[MI(O.AggressiveInlining)] public static void Multiply (ref Quaternion q1, ref Quaternion q2, out Quaternion r) {
r.I = q1.I * q2.U + q1.U * q2.I + q1.J * q2.K - q1.K * q2.J;
r.J = q1.U * q2.J - q1.I * q2.K + q1.J * q2.U + q1.K * q2.I;
r.K = q1.U * q2.K + q1.I * q2.J - q1.J * q2.I + q1.K * q2.U;
r.U = q1.U * q2.U - q1.I * q2.I - q1.J * q2.J - q1.K * q2.K;
}
[MI(O.AggressiveInlining)] public static Boolean operator == (Quaternion a, Quaternion b) { Boolean r; Equals (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Boolean operator != (Quaternion a, Quaternion b) { Boolean r; Equals (ref a, ref b, out r); return !r; }
[MI(O.AggressiveInlining)] public static Quaternion operator + (Quaternion a, Quaternion b) { Quaternion r; Add (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion operator - (Quaternion a, Quaternion b) { Quaternion r; Subtract (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion operator - (Quaternion v) { Quaternion r; Negate (ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion operator * (Quaternion a, Quaternion b) { Quaternion r; Multiply (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector3 operator * (Vector3 v, Quaternion q) { Vector3 r; Transform (ref q, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector4 operator * (Vector4 v, Quaternion q) { Vector4 r; Transform (ref q, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector3 operator * (Quaternion q, Vector3 v) { Vector3 r; Transform (ref q, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector4 operator * (Quaternion q, Vector4 v) { Vector4 r; Transform (ref q, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion operator ~ (Quaternion v) { Quaternion r; Normalise (ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Boolean Equals (Quaternion a, Quaternion b) { Boolean r; Equals (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Boolean ApproximateEquals (Quaternion a, Quaternion b) { Boolean r; ApproximateEquals (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Add (Quaternion a, Quaternion b) { Quaternion r; Add (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Subtract (Quaternion a, Quaternion b) { Quaternion r; Subtract (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Negate (Quaternion v) { Quaternion r; Negate (ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Multiply (Quaternion a, Quaternion b) { Quaternion r; Multiply (ref a, ref b, out r); return r; }
// Utilities //-------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void Lerp (ref Quaternion q1, ref Quaternion q2, ref Double amount, out Quaternion r) {
Debug.Assert (amount >= 0 && amount <= 1);
Double remaining = 1 - amount;
Double f = remaining;
Double a = amount;
r.U = (f * q1.U) + (a * q2.U);
r.I = (f * q1.I) + (a * q2.I);
r.J = (f * q1.J) + (a * q2.J);
r.K = (f * q1.K) + (a * q2.K);
}
// http://en.wikipedia.org/wiki/Slerp
[MI(O.AggressiveInlining)] public static void Slerp (ref Quaternion q1, ref Quaternion q2, ref Double amount,out Quaternion r) {
Debug.Assert (amount >= 0 && amount <= 1);
Double remaining = 1 - amount;
Double angle;
Dot (ref q1, ref q2, out angle);
if (angle < 0) {
Negate (ref q1, out q1);
angle = -angle;
}
Double theta = Maths.ArcCos (angle);
Double f = remaining;
Double a = amount;
if (theta > Maths.Epsilon) {
Double x = Maths.Sin (remaining * theta);
Double y = Maths.Sin (amount * theta);
Double z = Maths.Sin (theta);
f = x / z;
a = y / z;
}
r.U = (f * q1.U) + (a * q2.U);
r.I = (f * q1.I) + (a * q2.I);
r.J = (f * q1.J) + (a * q2.J);
r.K = (f * q1.K) + (a * q2.K);
}
[MI(O.AggressiveInlining)] public static void IsUnit (ref Quaternion q, out Boolean r) {
r = Maths.IsApproximatelyZero((Double) 1 - q.U * q.U - q.I * q.I - q.J * q.J - q.K * q.K);
}
[MI(O.AggressiveInlining)] public bool IsUnit () { Boolean r; IsUnit (ref this, out r); return r; }
[MI(O.AggressiveInlining)] public static Boolean IsUnit (Quaternion q) { Boolean r; IsUnit (ref q, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Lerp (Quaternion a, Quaternion b, Double amount) { Quaternion r; Lerp (ref a, ref b, ref amount, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Slerp (Quaternion a, Quaternion b, Double amount) { Quaternion r; Slerp (ref a, ref b, ref amount, out r); return r; }
// Maths //-----------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void LengthSquared (ref Quaternion q, out Double r) {
r = (q.I * q.I) + (q.J * q.J) + (q.K * q.K) + (q.U * q.U);
}
[MI(O.AggressiveInlining)] public static void Length (ref Quaternion q, out Double r) {
Double lengthSquared = (q.I * q.I) + (q.J * q.J) + (q.K * q.K) + (q.U * q.U);
r = Maths.Sqrt (lengthSquared);
}
[MI(O.AggressiveInlining)] public static void Conjugate (ref Quaternion value, out Quaternion r) {
r.I = -value.I; r.J = -value.J;
r.K = -value.K; r.U = value.U;
}
[MI(O.AggressiveInlining)] public static void Inverse (ref Quaternion q, out Quaternion r) {
Double a = (q.I * q.I) + (q.J * q.J) + (q.K * q.K) + (q.U * q.U);
Double b = 1 / a;
r.I = -q.I * b; r.J = -q.J * b;
r.K = -q.K * b; r.U = q.U * b;
}
[MI(O.AggressiveInlining)] public static void Dot (ref Quaternion q1, ref Quaternion q2, out Double r) {
r = (q1.I * q2.I) + (q1.J * q2.J) + (q1.K * q2.K) + (q1.U * q2.U);
}
[MI(O.AggressiveInlining)] public static void Concatenate (ref Quaternion q1, ref Quaternion q2, out Quaternion r) {
Double a = (q1.K * q2.J) - (q1.J * q2.K);
Double b = (q1.I * q2.K) - (q1.K * q2.I);
Double c = (q1.J * q2.I) - (q1.I * q2.J);
Double d = (q1.I * q2.I) - (q1.J * q2.J);
Double i = (q1.U * q2.I) + (q1.I * q2.U) + a;
Double j = (q1.U * q2.J) + (q1.J * q2.U) + b;
Double k = (q1.U * q2.K) + (q1.K * q2.U) + c;
Double u = (q1.U * q2.U) - (q1.K * q2.K) - d;
r.I = i; r.J = j; r.K = k; r.U = u;
}
[MI(O.AggressiveInlining)] public static void Normalise (ref Quaternion q, out Quaternion r) {
Double a = (q.I * q.I) + (q.J * q.J)
+ (q.K * q.K) + (q.U * q.U);
Double b = 1 / Maths.Sqrt (a);
r.I = q.I * b; r.J = q.J * b;
r.K = q.K * b; r.U = q.U * b;
}
[MI(O.AggressiveInlining)] public static void Transform (ref Quaternion rotation, ref Vector3 vector, out Vector3 r) {
Double i = rotation.I, j = rotation.J, k = rotation.K, u = rotation.U;
Double ii = i * i, jj = j * j, kk = k * k;
Double ui = u * i, uj = u * j, uk = u * k;
Double ij = i * j, ik = i * k, jk = j * k;
Double x = vector.X - (2 * vector.X * (jj + kk)) + (2 * vector.Y * (ij - uk)) + (2 * vector.Z * (ik + uj));
Double y = vector.Y + (2 * vector.X * (ij + uk)) - (2 * vector.Y * (ii + kk)) + (2 * vector.Z * (jk - ui));
Double z = vector.Z + (2 * vector.X * (ik - uj)) + (2 * vector.Y * (jk + ui)) - (2 * vector.Z * (ii + jj));
r.X = x; r.Y = y; r.Z = z;
}
[MI(O.AggressiveInlining)] public static void Transform (ref Quaternion rotation, ref Vector4 vector, out Vector4 r) {
Double i = rotation.I, j = rotation.J, k = rotation.K, u = rotation.U;
Double ii = i * i, jj = j * j, kk = k * k;
Double ui = u * i, uj = u * j, uk = u * k;
Double ij = i * j, ik = i * k, jk = j * k;
Double x = vector.X - (vector.X * 2 * (jj + kk)) + (vector.Y * 2 * (ij - uk)) + (vector.Z * 2 * (ik + uj));
Double y = vector.Y + (vector.X * 2 * (ij + uk)) - (vector.Y * 2 * (ii + kk)) + (vector.Z * 2 * (jk - ui));
Double z = vector.Z + (vector.X * 2 * (ik - uj)) + (vector.Y * 2 * (jk + ui)) - (vector.Z * 2 * (ii + jj));
Double w = vector.W;
r.X = x; r.Y = y; r.Z = z; r.W = w;
}
[MI(O.AggressiveInlining)] public static void ToYawPitchRoll (ref Quaternion q, out Vector3 r) { // Angle of rotation, in radians. Angles are measured anti-clockwise when viewed from the rotation axis (positive side) toward the origin.
// roll (x-axis rotation)
Double sinr_cosp = 2 * (q.U * q.K + q.I * q.J);
Double cosr_cosp = ((Double) 1) - 2 * (q.K * q.K + q.I * q.I);
r.Z = Maths.ArcTan2 (sinr_cosp, cosr_cosp);
// pitch (y-axis rotation)
Double sinp = 2 * (q.U * q.I - q.J * q.K);
if (Maths.Abs (sinp) >= 1f)
r.Y = Maths.CopySign (Maths.HalfPi, sinp);
else
r.Y = Maths.ArcSin (sinp);
// yaw (z-axis rotation)
Double siny_cosp = 2 * (q.U * q.J + q.K * q.I);
Double cosy_cosp = ((Double) 1) - 2 * (q.I * q.I + q.J * q.J);
r.X = Maths.ArcTan2 (siny_cosp, cosy_cosp);
}
[MI(O.AggressiveInlining)] public Double LengthSquared () { Double r; LengthSquared (ref this, out r); return r; }
[MI(O.AggressiveInlining)] public Double Length () { Double r; Length (ref this, out r); return r; }
[MI(O.AggressiveInlining)] public void Normalise () { Normalise (ref this, out this); }
[MI(O.AggressiveInlining)] public Quaternion Conjugate () { Conjugate (ref this, out this); return this; }
[MI(O.AggressiveInlining)] public Quaternion Inverse () { Inverse (ref this, out this); return this; }
[MI(O.AggressiveInlining)] public Double Dot (Quaternion q) { Double r; Dot (ref this, ref q, out r); return r; }
[MI(O.AggressiveInlining)] public Quaternion Concatenate (Quaternion q) { Concatenate (ref this, ref q, out this); return this; }
[MI(O.AggressiveInlining)] public Vector3 Transform (Vector3 v) { Vector3 r; Transform (ref this, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public Vector4 Transform (Vector4 v) { Vector4 r; Transform (ref this, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public Vector3 ToYawPitchRoll () { Vector3 r; ToYawPitchRoll (ref this, out r); return r; }
[MI(O.AggressiveInlining)] public static Double LengthSquared (Quaternion q) { Double r; LengthSquared (ref q, out r); return r; }
[MI(O.AggressiveInlining)] public static Double Length (Quaternion q) { Double r; Length (ref q, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Normalise (Quaternion q) { Quaternion r; Normalise (ref q, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Conjugate (Quaternion q) { Quaternion r; Conjugate (ref q, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Inverse (Quaternion q) { Quaternion r; Inverse (ref q, out r); return r; }
[MI(O.AggressiveInlining)] public static Double Dot (Quaternion a, Quaternion b) { Double r; Dot (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion Concatenate (Quaternion a, Quaternion b) { Quaternion r; Concatenate (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector3 Transform (Quaternion rotation, Vector3 v) { Vector3 r; Transform (ref rotation, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector4 Transform (Quaternion rotation, Vector4 v) { Vector4 r; Transform (ref rotation, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector3 ToYawPitchRoll (Quaternion q) { Vector3 r; ToYawPitchRoll (ref q, out r); return r; }
// Creation //--------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void CreateFromAxisAngle (ref Vector3 axis, ref Double angle, out Quaternion r) {
Double theta = angle * Maths.Half;
Double sin = Maths.Sin (theta), cos = Maths.Cos (theta);
r.I = axis.X * sin;
r.J = axis.Y * sin;
r.K = axis.Z * sin;
r.U = cos;
}
[MI(O.AggressiveInlining)] public static void CreateFromYawPitchRoll (ref Double yaw, ref Double pitch, ref Double roll, out Quaternion r) {
Double hr = roll * Maths.Half, hp = pitch * Maths.Half, hy = yaw * Maths.Half;
Double shr = Maths.Sin (hr), chr = Maths.Cos (hr);
Double shp = Maths.Sin (hp), chp = Maths.Cos (hp);
Double shy = Maths.Sin (hy), chy = Maths.Cos (hy);
r.I = (chy * shp * chr) + (shy * chp * shr);
r.J = (shy * chp * chr) - (chy * shp * shr);
r.K = (chy * chp * shr) - (shy * shp * chr);
r.U = (chy * chp * chr) + (shy * shp * shr);
}
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
[MI(O.AggressiveInlining)] public static void CreateFromRotationMatrix (ref Matrix44 m, out Quaternion r) {
Double tr = m.R0C0 + m.R1C1 + m.R2C2;
if (tr > 0) {
Double s = Maths.Sqrt (tr + 1) * 2;
r.U = Maths.Quarter * s;
r.I = (m.R1C2 - m.R2C1) / s;
r.J = (m.R2C0 - m.R0C2) / s;
r.K = (m.R0C1 - m.R1C0) / s;
}
else if ((m.R0C0 >= m.R1C1) && (m.R0C0 >= m.R2C2)) {
Double s = Maths.Sqrt (1 + m.R0C0 - m.R1C1 - m.R2C2) * 2;
r.U = (m.R1C2 - m.R2C1) / s;
r.I = Maths.Quarter * s;
r.J = (m.R0C1 + m.R1C0) / s;
r.K = (m.R0C2 + m.R2C0) / s;
}
else if (m.R1C1 > m.R2C2) {
Double s = Maths.Sqrt (1 + m.R1C1 - m.R0C0 - m.R2C2) * 2;
r.U = (m.R2C0 - m.R0C2) / s;
r.I = (m.R1C0 + m.R0C1) / s;
r.J = Maths.Quarter * s;
r.K = (m.R2C1 + m.R1C2) / s;
}
else {
Double s = Maths.Sqrt (1 + m.R2C2 - m.R0C0 - m.R1C1) * 2;
r.U = (m.R0C1 - m.R1C0) / s;
r.I = (m.R2C0 + m.R0C2) / s;
r.J = (m.R2C1 + m.R1C2) / s;
r.K = Maths.Quarter * s;
}
}
[MI(O.AggressiveInlining)] public static Quaternion CreateFromAxisAngle (Vector3 axis, Double angle) { Quaternion r; CreateFromAxisAngle (ref axis, ref angle, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion CreateFromYawPitchRoll (Double yaw, Double pitch, Double roll) { Quaternion r; CreateFromYawPitchRoll (ref yaw, ref pitch, ref roll, out r); return r; }
[MI(O.AggressiveInlining)] public static Quaternion CreateFromRotationMatrix (Matrix44 matrix) { Quaternion r; CreateFromRotationMatrix (ref matrix, out r); return r; }
}
/// <summary>
/// Double precision Matrix44.
/// </summary>
[StructLayout (LayoutKind.Sequential), Serializable]
public struct Matrix44 : IEquatable<Matrix44> {
public Double R0C0, R0C1, R0C2, R0C3;
public Double R1C0, R1C1, R1C2, R1C3;
public Double R2C0, R2C1, R2C2, R2C3;
public Double R3C0, R3C1, R3C2, R3C3;
[MI(O.AggressiveInlining)] public Matrix44 (
Double m00, Double m01, Double m02, Double m03, Double m10, Double m11, Double m12, Double m13,
Double m20, Double m21, Double m22, Double m23, Double m30, Double m31, Double m32, Double m33) {
this.R0C0 = m00; this.R0C1 = m01; this.R0C2 = m02; this.R0C3 = m03;
this.R1C0 = m10; this.R1C1 = m11; this.R1C2 = m12; this.R1C3 = m13;
this.R2C0 = m20; this.R2C1 = m21; this.R2C2 = m22; this.R2C3 = m23;
this.R3C0 = m30; this.R3C1 = m31; this.R3C2 = m32; this.R3C3 = m33;
}
public override String ToString () {
return String.Format ("((R0C0:{0}, R0C1:{1}, R0C2:{2}, R0C3:{3}), ", this.R0C0, this.R0C1, this.R0C2, this.R0C3)
+ String.Format ("(R1C0:{0}, R1C1:{1}, R1C2:{2}, R1C3:{3}), ", this.R1C0, this.R1C1, this.R1C2, this.R1C3)
+ String.Format ("(R2C0:{0}, R2C1:{1}, R2C2:{2}, R2C3:{3}), ", this.R2C0, this.R2C1, this.R2C2, this.R2C3)
+ String.Format ("(R3C0:{0}, R3C1:{1}, R3C2:{2}, R3C3:{3}))", this.R3C0, this.R3C1, this.R3C2, this.R3C3);
}
[MI(O.AggressiveInlining)] public override Int32 GetHashCode () {
return R0C0.GetHashCode () ^ R0C1.GetHashCode ().ShiftAndWrap (2)
^ R0C2.GetHashCode ().ShiftAndWrap (4) ^ R0C3.GetHashCode ().ShiftAndWrap (6)
^ R1C0.GetHashCode ().ShiftAndWrap (8) ^ R1C1.GetHashCode ().ShiftAndWrap (10)
^ R1C2.GetHashCode ().ShiftAndWrap (12) ^ R1C3.GetHashCode ().ShiftAndWrap (14)
^ R2C0.GetHashCode ().ShiftAndWrap (16) ^ R2C1.GetHashCode ().ShiftAndWrap (18)
^ R2C2.GetHashCode ().ShiftAndWrap (20) ^ R2C3.GetHashCode ().ShiftAndWrap (22)
^ R3C0.GetHashCode ().ShiftAndWrap (24) ^ R3C1.GetHashCode ().ShiftAndWrap (26)
^ R3C2.GetHashCode ().ShiftAndWrap (28) ^ R3C3.GetHashCode ().ShiftAndWrap (30);
}
[MI(O.AggressiveInlining)] public override Boolean Equals (Object obj) { return (obj is Matrix44) ? this.Equals ((Matrix44)obj) : false; }
[MI(O.AggressiveInlining)] public Boolean Equals (Matrix44 other) { Boolean r; Equals (ref this, ref other, out r); return r; }
[MI(O.AggressiveInlining)] public Boolean ApproximateEquals (Matrix44 other) { Boolean r; ApproximateEquals (ref this, ref other, out r); return r; }
[MI(O.AggressiveInlining)] public Boolean IsSymmetric () {
Matrix44 transpose = this;
Transpose (ref transpose, out transpose);
return transpose.Equals (this);
}
[MI(O.AggressiveInlining)] public Boolean IsSkewSymmetric () {
Matrix44 transpose = this;
Transpose (ref transpose, out transpose);
Negate (ref transpose, out transpose);
return transpose.Equals (this);
}
// Accessors //-------------------------------------------------------//
public Vector3 Up { get { return new Vector3 ( R1C0, R1C1, R1C2); } set { R1C0 = value.X; R1C1 = value.Y; R1C2 = value.Z; } }
public Vector3 Down { get { return new Vector3 (-R1C0, -R1C1, -R1C2); } set { R1C0 = -value.X; R1C1 = -value.Y; R1C2 = -value.Z; } }
public Vector3 Right { get { return new Vector3 ( R0C0, R0C1, R0C2); } set { R0C0 = value.X; R0C1 = value.Y; R0C2 = value.Z; } }
public Vector3 Left { get { return new Vector3 (-R0C0, -R0C1, -R0C2); } set { R0C0 = -value.X; R0C1 = -value.Y; R0C2 = -value.Z; } }
public Vector3 Forward { get { return new Vector3 (-R2C0, -R2C1, -R2C2); } set { R2C0 = -value.X; R2C1 = -value.Y; R2C2 = -value.Z; } }
public Vector3 Backward { get { return new Vector3 ( R2C0, R2C1, R2C2); } set { R2C0 = value.X; R2C1 = value.Y; R2C2 = value.Z; } }
public Vector3 Translation { get { return new Vector3 ( R3C0, R3C1, R3C2); } set { R3C0 = value.X; R3C1 = value.Y; R3C2 = value.Z; } }
// Constants //-------------------------------------------------------//
static Matrix44 identity, zero;
static Matrix44 () {
identity = new Matrix44 (1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
zero = new Matrix44 (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
public static Matrix44 Identity { get { return identity; } }
public static Matrix44 Zero { get { return zero; } }
// Operators //-------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void Equals (ref Matrix44 a, ref Matrix44 b, out Boolean r) {
r = (a.R0C0 == b.R0C0) && (a.R1C1 == b.R1C1) &&
(a.R2C2 == b.R2C2) && (a.R3C3 == b.R3C3) &&
(a.R0C1 == b.R0C1) && (a.R0C2 == b.R0C2) &&
(a.R0C3 == b.R0C3) && (a.R1C0 == b.R1C0) &&
(a.R1C2 == b.R1C2) && (a.R1C3 == b.R1C3) &&
(a.R2C0 == b.R2C0) && (a.R2C1 == b.R2C1) &&
(a.R2C3 == b.R2C3) && (a.R3C0 == b.R3C0) &&
(a.R3C1 == b.R3C1) && (a.R3C2 == b.R3C2);
}
[MI(O.AggressiveInlining)] public static void ApproximateEquals (ref Matrix44 a, ref Matrix44 b, out Boolean r) {
r = Maths.ApproximateEquals (a.R0C0, b.R0C0) && Maths.ApproximateEquals (a.R1C1, b.R1C1) &&
Maths.ApproximateEquals (a.R2C2, b.R2C2) && Maths.ApproximateEquals (a.R3C3, b.R3C3) &&
Maths.ApproximateEquals (a.R0C1, b.R0C1) && Maths.ApproximateEquals (a.R0C2, b.R0C2) &&
Maths.ApproximateEquals (a.R0C3, b.R0C3) && Maths.ApproximateEquals (a.R1C0, b.R1C0) &&
Maths.ApproximateEquals (a.R1C2, b.R1C2) && Maths.ApproximateEquals (a.R1C3, b.R1C3) &&
Maths.ApproximateEquals (a.R2C0, b.R2C0) && Maths.ApproximateEquals (a.R2C1, b.R2C1) &&
Maths.ApproximateEquals (a.R2C3, b.R2C3) && Maths.ApproximateEquals (a.R3C0, b.R3C0) &&
Maths.ApproximateEquals (a.R3C1, b.R3C1) && Maths.ApproximateEquals (a.R3C2, b.R3C2);
}
[MI(O.AggressiveInlining)] public static void Add (ref Matrix44 a, ref Matrix44 b, out Matrix44 r) {
r.R0C0 = a.R0C0 + b.R0C0; r.R0C1 = a.R0C1 + b.R0C1;
r.R0C2 = a.R0C2 + b.R0C2; r.R0C3 = a.R0C3 + b.R0C3;
r.R1C0 = a.R1C0 + b.R1C0; r.R1C1 = a.R1C1 + b.R1C1;
r.R1C2 = a.R1C2 + b.R1C2; r.R1C3 = a.R1C3 + b.R1C3;
r.R2C0 = a.R2C0 + b.R2C0; r.R2C1 = a.R2C1 + b.R2C1;
r.R2C2 = a.R2C2 + b.R2C2; r.R2C3 = a.R2C3 + b.R2C3;
r.R3C0 = a.R3C0 + b.R3C0; r.R3C1 = a.R3C1 + b.R3C1;
r.R3C2 = a.R3C2 + b.R3C2; r.R3C3 = a.R3C3 + b.R3C3;
}
[MI(O.AggressiveInlining)] public static void Subtract (ref Matrix44 a, ref Matrix44 b, out Matrix44 r) {
r.R0C0 = a.R0C0 - b.R0C0; r.R0C1 = a.R0C1 - b.R0C1;
r.R0C2 = a.R0C2 - b.R0C2; r.R0C3 = a.R0C3 - b.R0C3;
r.R1C0 = a.R1C0 - b.R1C0; r.R1C1 = a.R1C1 - b.R1C1;
r.R1C2 = a.R1C2 - b.R1C2; r.R1C3 = a.R1C3 - b.R1C3;
r.R2C0 = a.R2C0 - b.R2C0; r.R2C1 = a.R2C1 - b.R2C1;
r.R2C2 = a.R2C2 - b.R2C2; r.R2C3 = a.R2C3 - b.R2C3;
r.R3C0 = a.R3C0 - b.R3C0; r.R3C1 = a.R3C1 - b.R3C1;
r.R3C2 = a.R3C2 - b.R3C2; r.R3C3 = a.R3C3 - b.R3C3;
}
[MI(O.AggressiveInlining)] public static void Negate (ref Matrix44 m, out Matrix44 r) {
r.R0C0 = -m.R0C0; r.R0C1 = -m.R0C1;
r.R0C2 = -m.R0C2; r.R0C3 = -m.R0C3;
r.R1C0 = -m.R1C0; r.R1C1 = -m.R1C1;
r.R1C2 = -m.R1C2; r.R1C3 = -m.R1C3;
r.R2C0 = -m.R2C0; r.R2C1 = -m.R2C1;
r.R2C2 = -m.R2C2; r.R2C3 = -m.R2C3;
r.R3C0 = -m.R3C0; r.R3C1 = -m.R3C1;
r.R3C2 = -m.R3C2; r.R3C3 = -m.R3C3;
}
[MI(O.AggressiveInlining)] public static void Product (ref Matrix44 a, ref Matrix44 b, out Matrix44 r) {
Double r0c0 = (a.R0C0 * b.R0C0) + (a.R0C1 * b.R1C0) + (a.R0C2 * b.R2C0) + (a.R0C3 * b.R3C0);
Double r0c1 = (a.R0C0 * b.R0C1) + (a.R0C1 * b.R1C1) + (a.R0C2 * b.R2C1) + (a.R0C3 * b.R3C1);
Double r0c2 = (a.R0C0 * b.R0C2) + (a.R0C1 * b.R1C2) + (a.R0C2 * b.R2C2) + (a.R0C3 * b.R3C2);
Double r0c3 = (a.R0C0 * b.R0C3) + (a.R0C1 * b.R1C3) + (a.R0C2 * b.R2C3) + (a.R0C3 * b.R3C3);
Double r1c0 = (a.R1C0 * b.R0C0) + (a.R1C1 * b.R1C0) + (a.R1C2 * b.R2C0) + (a.R1C3 * b.R3C0);
Double r1c1 = (a.R1C0 * b.R0C1) + (a.R1C1 * b.R1C1) + (a.R1C2 * b.R2C1) + (a.R1C3 * b.R3C1);
Double r1c2 = (a.R1C0 * b.R0C2) + (a.R1C1 * b.R1C2) + (a.R1C2 * b.R2C2) + (a.R1C3 * b.R3C2);
Double r1c3 = (a.R1C0 * b.R0C3) + (a.R1C1 * b.R1C3) + (a.R1C2 * b.R2C3) + (a.R1C3 * b.R3C3);
Double r2c0 = (a.R2C0 * b.R0C0) + (a.R2C1 * b.R1C0) + (a.R2C2 * b.R2C0) + (a.R2C3 * b.R3C0);
Double r2c1 = (a.R2C0 * b.R0C1) + (a.R2C1 * b.R1C1) + (a.R2C2 * b.R2C1) + (a.R2C3 * b.R3C1);
Double r2c2 = (a.R2C0 * b.R0C2) + (a.R2C1 * b.R1C2) + (a.R2C2 * b.R2C2) + (a.R2C3 * b.R3C2);
Double r2c3 = (a.R2C0 * b.R0C3) + (a.R2C1 * b.R1C3) + (a.R2C2 * b.R2C3) + (a.R2C3 * b.R3C3);
Double r3c0 = (a.R3C0 * b.R0C0) + (a.R3C1 * b.R1C0) + (a.R3C2 * b.R2C0) + (a.R3C3 * b.R3C0);
Double r3c1 = (a.R3C0 * b.R0C1) + (a.R3C1 * b.R1C1) + (a.R3C2 * b.R2C1) + (a.R3C3 * b.R3C1);
Double r3c2 = (a.R3C0 * b.R0C2) + (a.R3C1 * b.R1C2) + (a.R3C2 * b.R2C2) + (a.R3C3 * b.R3C2);
Double r3c3 = (a.R3C0 * b.R0C3) + (a.R3C1 * b.R1C3) + (a.R3C2 * b.R2C3) + (a.R3C3 * b.R3C3);
r.R0C0 = r0c0; r.R0C1 = r0c1; r.R0C2 = r0c2; r.R0C3 = r0c3;
r.R1C0 = r1c0; r.R1C1 = r1c1; r.R1C2 = r1c2; r.R1C3 = r1c3;
r.R2C0 = r2c0; r.R2C1 = r2c1; r.R2C2 = r2c2; r.R2C3 = r2c3;
r.R3C0 = r3c0; r.R3C1 = r3c1; r.R3C2 = r3c2; r.R3C3 = r3c3;
}
[MI(O.AggressiveInlining)] public static void Multiply (ref Matrix44 m, ref Double f, out Matrix44 r) {
r.R0C0 = m.R0C0 * f; r.R0C1 = m.R0C1 * f;
r.R0C2 = m.R0C2 * f; r.R0C3 = m.R0C3 * f;
r.R1C0 = m.R1C0 * f; r.R1C1 = m.R1C1 * f;
r.R1C2 = m.R1C2 * f; r.R1C3 = m.R1C3 * f;
r.R2C0 = m.R2C0 * f; r.R2C1 = m.R2C1 * f;
r.R2C2 = m.R2C2 * f; r.R2C3 = m.R2C3 * f;
r.R3C0 = m.R3C0 * f; r.R3C1 = m.R3C1 * f;
r.R3C2 = m.R3C2 * f; r.R3C3 = m.R3C3 * f;
}
[MI(O.AggressiveInlining)] public static Boolean operator == (Matrix44 a, Matrix44 b) { Boolean r; Equals (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Boolean operator != (Matrix44 a, Matrix44 b) { Boolean r; Equals (ref a, ref b, out r); return !r; }
[MI(O.AggressiveInlining)] public static Matrix44 operator + (Matrix44 a, Matrix44 b) { Matrix44 r; Add (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 operator - (Matrix44 a, Matrix44 b) { Matrix44 r; Subtract (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 operator - (Matrix44 m) { Matrix44 r; Negate (ref m, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 operator * (Matrix44 a, Matrix44 b) { Matrix44 r; Product (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 operator * (Matrix44 m, Double f) { Matrix44 r; Multiply (ref m, ref f, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 operator * (Double f, Matrix44 m) { Matrix44 r; Multiply (ref m, ref f, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector3 operator * (Vector3 v, Matrix44 m) { Vector3 r; Transform (ref m, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector4 operator * (Vector4 v, Matrix44 m) { Vector4 r; Transform (ref m, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector3 operator * (Matrix44 m, Vector3 v) { Vector3 r; Transform (ref m, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector4 operator * (Matrix44 m, Vector4 v) { Vector4 r; Transform (ref m, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Boolean Equals (Matrix44 a, Matrix44 b) { Boolean r; Equals (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Boolean ApproximateEquals (Matrix44 a, Matrix44 b) { Boolean r; ApproximateEquals (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Add (Matrix44 a, Matrix44 b) { Matrix44 r; Add (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Subtract (Matrix44 a, Matrix44 b) { Matrix44 r; Subtract (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Negate (Matrix44 m) { Matrix44 r; Negate (ref m, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Product (Matrix44 a, Matrix44 b) { Matrix44 r; Product (ref a, ref b, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Multiply (Matrix44 m, Double f) { Matrix44 r; Multiply (ref m, ref f, out r); return r; }
// Utilities //-------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void Lerp (ref Matrix44 a, ref Matrix44 b, ref Double amount, out Matrix44 r) {
Debug.Assert (amount > 0 && amount <= 1);
r.R0C0 = a.R0C0 + ((b.R0C0 - a.R0C0) * amount);
r.R0C1 = a.R0C1 + ((b.R0C1 - a.R0C1) * amount);
r.R0C2 = a.R0C2 + ((b.R0C2 - a.R0C2) * amount);
r.R0C3 = a.R0C3 + ((b.R0C3 - a.R0C3) * amount);
r.R1C0 = a.R1C0 + ((b.R1C0 - a.R1C0) * amount);
r.R1C1 = a.R1C1 + ((b.R1C1 - a.R1C1) * amount);
r.R1C2 = a.R1C2 + ((b.R1C2 - a.R1C2) * amount);
r.R1C3 = a.R1C3 + ((b.R1C3 - a.R1C3) * amount);
r.R2C0 = a.R2C0 + ((b.R2C0 - a.R2C0) * amount);
r.R2C1 = a.R2C1 + ((b.R2C1 - a.R2C1) * amount);
r.R2C2 = a.R2C2 + ((b.R2C2 - a.R2C2) * amount);
r.R2C3 = a.R2C3 + ((b.R2C3 - a.R2C3) * amount);
r.R3C0 = a.R3C0 + ((b.R3C0 - a.R3C0) * amount);
r.R3C1 = a.R3C1 + ((b.R3C1 - a.R3C1) * amount);
r.R3C2 = a.R3C2 + ((b.R3C2 - a.R3C2) * amount);
r.R3C3 = a.R3C3 + ((b.R3C3 - a.R3C3) * amount);
}
[MI(O.AggressiveInlining)] public static Matrix44 Lerp (Matrix44 a, Matrix44 b, Double amount) { Matrix44 r; Lerp (ref a, ref b, ref amount, out r); return r; }
// Maths //-----------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void Transpose (ref Matrix44 m, out Matrix44 r) {
r.R0C0 = m.R0C0; r.R1C1 = m.R1C1;
r.R2C2 = m.R2C2; r.R3C3 = m.R3C3;
Double t = m.R0C1; r.R0C1 = m.R1C0; r.R1C0 = t;
t = m.R0C2; r.R0C2 = m.R2C0; r.R2C0 = t;
t = m.R0C3; r.R0C3 = m.R3C0; r.R3C0 = t;
t = m.R1C2; r.R1C2 = m.R2C1; r.R2C1 = t;
t = m.R1C3; r.R1C3 = m.R3C1; r.R3C1 = t;
t = m.R2C3; r.R2C3 = m.R3C2; r.R3C2 = t;
}
[MI(O.AggressiveInlining)] public static void Decompose (ref Matrix44 matrix, out Vector3 scale, out Quaternion rotation, out Vector3 translation, out Boolean r) {
translation.X = matrix.R3C0; translation.Y = matrix.R3C1; translation.Z = matrix.R3C2;
Vector3 a = new Vector3 (matrix.R0C0, matrix.R1C0, matrix.R2C0);
Vector3 b = new Vector3 (matrix.R0C1, matrix.R1C1, matrix.R2C1);
Vector3 c = new Vector3 (matrix.R0C2, matrix.R1C2, matrix.R2C2);
Double aLen; Vector3.Length (ref a, out aLen); scale.X = aLen;
Double bLen; Vector3.Length (ref b, out bLen); scale.Y = bLen;
Double cLen; Vector3.Length (ref c, out cLen); scale.Z = cLen;
if (Maths.IsApproximatelyZero (scale.X) || Maths.IsApproximatelyZero (scale.Y) || Maths.IsApproximatelyZero (scale.Z)) {
rotation = Quaternion.Identity;
r = false;
}
if (aLen < Maths.Epsilon) a = Vector3.Zero;
else Vector3.Normalise (ref a, out a);
if (bLen < Maths.Epsilon) b = Vector3.Zero;
else Vector3.Normalise (ref b, out b);
if (cLen < Maths.Epsilon) c = Vector3.Zero;
else Vector3.Normalise (ref c, out c);
Vector3 right = new Vector3 (a.X, b.X, c.X);
Vector3 up = new Vector3 (a.Y, b.Y, c.Y);
Vector3 backward = new Vector3 (a.Z, b.Z, c.Z);
if (right.Equals (Vector3.Zero)) right = Vector3.Right;
if (up.Equals (Vector3.Zero)) up = Vector3.Up;
if (backward.Equals (Vector3.Zero)) backward = Vector3.Backward;
Vector3.Normalise (ref right, out right);
Vector3.Normalise (ref up, out up);
Vector3.Normalise (ref backward, out backward);
Matrix44 rotMat;
Matrix44.CreateFromCartesianAxes (ref right, ref up, ref backward, out rotMat);
Quaternion.CreateFromRotationMatrix (ref rotMat, out rotation);
r = true;
}
[MI(O.AggressiveInlining)] public static void Determinant (ref Matrix44 m, out Double r) {
r = + m.R0C3 * m.R1C2 * m.R2C1 * m.R3C0 - m.R0C2 * m.R1C3 * m.R2C1 * m.R3C0
- m.R0C3 * m.R1C1 * m.R2C2 * m.R3C0 + m.R0C1 * m.R1C3 * m.R2C2 * m.R3C0
+ m.R0C2 * m.R1C1 * m.R2C3 * m.R3C0 - m.R0C1 * m.R1C2 * m.R2C3 * m.R3C0
- m.R0C3 * m.R1C2 * m.R2C0 * m.R3C1 + m.R0C2 * m.R1C3 * m.R2C0 * m.R3C1
+ m.R0C3 * m.R1C0 * m.R2C2 * m.R3C1 - m.R0C0 * m.R1C3 * m.R2C2 * m.R3C1
- m.R0C2 * m.R1C0 * m.R2C3 * m.R3C1 + m.R0C0 * m.R1C2 * m.R2C3 * m.R3C1
+ m.R0C3 * m.R1C1 * m.R2C0 * m.R3C2 - m.R0C1 * m.R1C3 * m.R2C0 * m.R3C2
- m.R0C3 * m.R1C0 * m.R2C1 * m.R3C2 + m.R0C0 * m.R1C3 * m.R2C1 * m.R3C2
+ m.R0C1 * m.R1C0 * m.R2C3 * m.R3C2 - m.R0C0 * m.R1C1 * m.R2C3 * m.R3C2
- m.R0C2 * m.R1C1 * m.R2C0 * m.R3C3 + m.R0C1 * m.R1C2 * m.R2C0 * m.R3C3
+ m.R0C2 * m.R1C0 * m.R2C1 * m.R3C3 - m.R0C0 * m.R1C2 * m.R2C1 * m.R3C3
- m.R0C1 * m.R1C0 * m.R2C2 * m.R3C3 + m.R0C0 * m.R1C1 * m.R2C2 * m.R3C3;
}
[MI(O.AggressiveInlining)] public static void Invert (ref Matrix44 m, out Matrix44 r) {
Double d; Determinant (ref m, out d); Double s = 1 / d;
Double r0c0 = m.R1C2 * m.R2C3 * m.R3C1 - m.R1C3 * m.R2C2 * m.R3C1 + m.R1C3 * m.R2C1 * m.R3C2 - m.R1C1 * m.R2C3 * m.R3C2 - m.R1C2 * m.R2C1 * m.R3C3 + m.R1C1 * m.R2C2 * m.R3C3;
Double r0c1 = m.R0C3 * m.R2C2 * m.R3C1 - m.R0C2 * m.R2C3 * m.R3C1 - m.R0C3 * m.R2C1 * m.R3C2 + m.R0C1 * m.R2C3 * m.R3C2 + m.R0C2 * m.R2C1 * m.R3C3 - m.R0C1 * m.R2C2 * m.R3C3;
Double r0c2 = m.R0C2 * m.R1C3 * m.R3C1 - m.R0C3 * m.R1C2 * m.R3C1 + m.R0C3 * m.R1C1 * m.R3C2 - m.R0C1 * m.R1C3 * m.R3C2 - m.R0C2 * m.R1C1 * m.R3C3 + m.R0C1 * m.R1C2 * m.R3C3;
Double r0c3 = m.R0C3 * m.R1C2 * m.R2C1 - m.R0C2 * m.R1C3 * m.R2C1 - m.R0C3 * m.R1C1 * m.R2C2 + m.R0C1 * m.R1C3 * m.R2C2 + m.R0C2 * m.R1C1 * m.R2C3 - m.R0C1 * m.R1C2 * m.R2C3;
Double r1c0 = m.R1C3 * m.R2C2 * m.R3C0 - m.R1C2 * m.R2C3 * m.R3C0 - m.R1C3 * m.R2C0 * m.R3C2 + m.R1C0 * m.R2C3 * m.R3C2 + m.R1C2 * m.R2C0 * m.R3C3 - m.R1C0 * m.R2C2 * m.R3C3;
Double r1c1 = m.R0C2 * m.R2C3 * m.R3C0 - m.R0C3 * m.R2C2 * m.R3C0 + m.R0C3 * m.R2C0 * m.R3C2 - m.R0C0 * m.R2C3 * m.R3C2 - m.R0C2 * m.R2C0 * m.R3C3 + m.R0C0 * m.R2C2 * m.R3C3;
Double r1c2 = m.R0C3 * m.R1C2 * m.R3C0 - m.R0C2 * m.R1C3 * m.R3C0 - m.R0C3 * m.R1C0 * m.R3C2 + m.R0C0 * m.R1C3 * m.R3C2 + m.R0C2 * m.R1C0 * m.R3C3 - m.R0C0 * m.R1C2 * m.R3C3;
Double r1c3 = m.R0C2 * m.R1C3 * m.R2C0 - m.R0C3 * m.R1C2 * m.R2C0 + m.R0C3 * m.R1C0 * m.R2C2 - m.R0C0 * m.R1C3 * m.R2C2 - m.R0C2 * m.R1C0 * m.R2C3 + m.R0C0 * m.R1C2 * m.R2C3;
Double r2c0 = m.R1C1 * m.R2C3 * m.R3C0 - m.R1C3 * m.R2C1 * m.R3C0 + m.R1C3 * m.R2C0 * m.R3C1 - m.R1C0 * m.R2C3 * m.R3C1 - m.R1C1 * m.R2C0 * m.R3C3 + m.R1C0 * m.R2C1 * m.R3C3;
Double r2c1 = m.R0C3 * m.R2C1 * m.R3C0 - m.R0C1 * m.R2C3 * m.R3C0 - m.R0C3 * m.R2C0 * m.R3C1 + m.R0C0 * m.R2C3 * m.R3C1 + m.R0C1 * m.R2C0 * m.R3C3 - m.R0C0 * m.R2C1 * m.R3C3;
Double r2c2 = m.R0C1 * m.R1C3 * m.R3C0 - m.R0C3 * m.R1C1 * m.R3C0 + m.R0C3 * m.R1C0 * m.R3C1 - m.R0C0 * m.R1C3 * m.R3C1 - m.R0C1 * m.R1C0 * m.R3C3 + m.R0C0 * m.R1C1 * m.R3C3;
Double r2c3 = m.R0C3 * m.R1C1 * m.R2C0 - m.R0C1 * m.R1C3 * m.R2C0 - m.R0C3 * m.R1C0 * m.R2C1 + m.R0C0 * m.R1C3 * m.R2C1 + m.R0C1 * m.R1C0 * m.R2C3 - m.R0C0 * m.R1C1 * m.R2C3;
Double r3c0 = m.R1C2 * m.R2C1 * m.R3C0 - m.R1C1 * m.R2C2 * m.R3C0 - m.R1C2 * m.R2C0 * m.R3C1 + m.R1C0 * m.R2C2 * m.R3C1 + m.R1C1 * m.R2C0 * m.R3C2 - m.R1C0 * m.R2C1 * m.R3C2;
Double r3c1 = m.R0C1 * m.R2C2 * m.R3C0 - m.R0C2 * m.R2C1 * m.R3C0 + m.R0C2 * m.R2C0 * m.R3C1 - m.R0C0 * m.R2C2 * m.R3C1 - m.R0C1 * m.R2C0 * m.R3C2 + m.R0C0 * m.R2C1 * m.R3C2;
Double r3c2 = m.R0C2 * m.R1C1 * m.R3C0 - m.R0C1 * m.R1C2 * m.R3C0 - m.R0C2 * m.R1C0 * m.R3C1 + m.R0C0 * m.R1C2 * m.R3C1 + m.R0C1 * m.R1C0 * m.R3C2 - m.R0C0 * m.R1C1 * m.R3C2;
Double r3c3 = m.R0C1 * m.R1C2 * m.R2C0 - m.R0C2 * m.R1C1 * m.R2C0 + m.R0C2 * m.R1C0 * m.R2C1 - m.R0C0 * m.R1C2 * m.R2C1 - m.R0C1 * m.R1C0 * m.R2C2 + m.R0C0 * m.R1C1 * m.R2C2;
r.R0C0 = r0c0; r.R0C1 = r0c1; r.R0C2 = r0c2; r.R0C3 = r0c3;
r.R1C0 = r1c0; r.R1C1 = r1c1; r.R1C2 = r1c2; r.R1C3 = r1c3;
r.R2C0 = r2c0; r.R2C1 = r2c1; r.R2C2 = r2c2; r.R2C3 = r2c3;
r.R3C0 = r3c0; r.R3C1 = r3c1; r.R3C2 = r3c2; r.R3C3 = r3c3;
Multiply (ref r, ref s, out r);
}
[MI(O.AggressiveInlining)] public static void Transform (ref Matrix44 m, ref Quaternion q, out Matrix44 r) {
Boolean qIsUnit; Quaternion.IsUnit (ref q, out qIsUnit);
Debug.Assert (qIsUnit);
Double twoI = q.I + q.I, twoJ = q.J + q.J, twoK = q.K + q.K;
Double twoUI = q.U * twoI, twoUJ = q.U * twoJ, twoUK = q.U * twoK;
Double twoII = q.I * twoI, twoIJ = q.I * twoJ, twoIK = q.I * twoK;
Double twoJJ = q.J * twoJ, twoJK = q.J * twoK, twoKK = q.K * twoK;
Double tR0C0 = 1 - twoJJ - twoKK;
Double tR1C0 = twoIJ - twoUK;
Double tR2C0 = twoIK + twoUJ;
Double tR0C1 = twoIJ + twoUK;
Double tR1C1 = 1 - twoII - twoKK;
Double tR2C1 = twoJK - twoUI;
Double tR0C2 = twoIK - twoUJ;
Double tR1C2 = twoJK + twoUI;
Double tR2C2 = 1 - twoII - twoJJ;
Double r0c0 = m.R0C0 * tR0C0 + m.R0C1 * tR1C0 + m.R0C2 * tR2C0;
Double r0c1 = m.R0C0 * tR0C1 + m.R0C1 * tR1C1 + m.R0C2 * tR2C1;
Double r0c2 = m.R0C0 * tR0C2 + m.R0C1 * tR1C2 + m.R0C2 * tR2C2;
Double r1c0 = m.R1C0 * tR0C0 + m.R1C1 * tR1C0 + m.R1C2 * tR2C0;
Double r1c1 = m.R1C0 * tR0C1 + m.R1C1 * tR1C1 + m.R1C2 * tR2C1;
Double r1c2 = m.R1C0 * tR0C2 + m.R1C1 * tR1C2 + m.R1C2 * tR2C2;
Double r2c0 = m.R2C0 * tR0C0 + m.R2C1 * tR1C0 + m.R2C2 * tR2C0;
Double r2c1 = m.R2C0 * tR0C1 + m.R2C1 * tR1C1 + m.R2C2 * tR2C1;
Double r2c2 = m.R2C0 * tR0C2 + m.R2C1 * tR1C2 + m.R2C2 * tR2C2;
Double r3c0 = m.R3C0 * tR0C0 + m.R3C1 * tR1C0 + m.R3C2 * tR2C0;
Double r3c1 = m.R3C0 * tR0C1 + m.R3C1 * tR1C1 + m.R3C2 * tR2C1;
Double r3c2 = m.R3C0 * tR0C2 + m.R3C1 * tR1C2 + m.R3C2 * tR2C2;
r.R0C0 = r0c0; r.R0C1 = r0c1; r.R0C2 = r0c2; r.R0C3 = m.R0C3;
r.R1C0 = r1c0; r.R1C1 = r1c1; r.R1C2 = r1c2; r.R1C3 = m.R1C3;
r.R2C0 = r2c0; r.R2C1 = r2c1; r.R2C2 = r2c2; r.R2C3 = m.R2C3;
r.R3C0 = r3c0; r.R3C1 = r3c1; r.R3C2 = r3c2; r.R3C3 = m.R3C3;
}
[MI(O.AggressiveInlining)] public static void Transform (ref Matrix44 m, ref Vector3 v, out Vector3 r) {
Double x = (v.X * m.R0C0) + (v.Y * m.R1C0) + (v.Z * m.R2C0) + m.R3C0;
Double y = (v.X * m.R0C1) + (v.Y * m.R1C1) + (v.Z * m.R2C1) + m.R3C1;
Double z = (v.X * m.R0C2) + (v.Y * m.R1C2) + (v.Z * m.R2C2) + m.R3C2;
Double w = (v.X * m.R0C3) + (v.Y * m.R1C3) + (v.Z * m.R2C3) + m.R3C3;
r.X = x / w; r.Y = y / w; r.Z = z / w;
}
[MI(O.AggressiveInlining)] public static void Transform (ref Matrix44 m, ref Vector4 v, out Vector4 r) {
Double x = (v.X * m.R0C0) + (v.Y * m.R1C0) + (v.Z * m.R2C0) + (v.W * m.R3C0);
Double y = (v.X * m.R0C1) + (v.Y * m.R1C1) + (v.Z * m.R2C1) + (v.W * m.R3C1);
Double z = (v.X * m.R0C2) + (v.Y * m.R1C2) + (v.Z * m.R2C2) + (v.W * m.R3C2);
Double w = (v.X * m.R0C3) + (v.Y * m.R1C3) + (v.Z * m.R2C3) + (v.W * m.R3C3);
r.X = x; r.Y = y; r.Z = z; r.W = w;
}
[MI(O.AggressiveInlining)] public Double Determinant () { Double r; Determinant (ref this, out r); return r; }
[MI(O.AggressiveInlining)] public Matrix44 Transpose () { Transpose (ref this, out this); return this; }
[MI(O.AggressiveInlining)] public Matrix44 Invert () { Invert (ref this, out this); return this; }
[MI(O.AggressiveInlining)] public Matrix44 Transform (Quaternion rotation) { Matrix44 r; Transform (ref this, ref rotation, out r); return r; }
[MI(O.AggressiveInlining)] public Vector3 Transform (Vector3 v) { Vector3 r; Transform (ref this, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public Vector4 Transform (Vector4 v) { Vector4 r; Transform (ref this, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Double Determinant (Matrix44 matrix) { Double r; Determinant (ref matrix, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Transpose (Matrix44 input) { Matrix44 r; Transpose (ref input, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Invert (Matrix44 matrix) { Matrix44 r; Invert (ref matrix, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 Transform (Matrix44 matrix, Quaternion rotation) { Matrix44 r; Transform (ref matrix, ref rotation, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector3 Transform (Matrix44 matrix, Vector3 v) { Vector3 r; Transform (ref matrix, ref v, out r); return r; }
[MI(O.AggressiveInlining)] public static Vector4 Transform (Matrix44 matrix, Vector4 v) { Vector4 r; Transform (ref matrix, ref v, out r); return r; }
// Creation //--------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void CreateTranslation (ref Vector3 position, out Matrix44 r) {
r.R0C0 = 1; r.R0C1 = 0; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = 1; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = 0; r.R2C2 = 1; r.R2C3 = 0;
r.R3C0 = position.X; r.R3C1 = position.Y; r.R3C2 = position.Z; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateTranslation (ref Double x, ref Double y, ref Double z, out Matrix44 r) {
r.R0C0 = 1; r.R0C1 = 0; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = 1; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = 0; r.R2C2 = 1; r.R2C3 = 0;
r.R3C0 = x; r.R3C1 = y; r.R3C2 = z; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateScale (ref Vector3 scale, out Matrix44 r) {
r.R0C0 = scale.X; r.R0C1 = 0; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = scale.Y; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = 0; r.R2C2 = scale.Z; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateScale (ref Double x, ref Double y, ref Double z, out Matrix44 r) {
r.R0C0 = x; r.R0C1 = 0; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = y; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = 0; r.R2C2 = z; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateScale (ref Double scale, out Matrix44 r) {
r.R0C0 = scale; r.R0C1 = 0; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = scale; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = 0; r.R2C2 = scale; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateRotationX (ref Double radians, out Matrix44 r) {
Double cos = Maths.Cos (radians), sin = Maths.Sin (radians);
r.R0C0 = 1; r.R0C1 = 0; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = cos; r.R1C2 = sin; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = -sin; r.R2C2 = cos; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateRotationY (ref Double radians, out Matrix44 r) {
Double cos = Maths.Cos (radians), sin = Maths.Sin (radians);
r.R0C0 = cos; r.R0C1 = 0; r.R0C2 = -sin; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = 1; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = sin; r.R2C1 = 0; r.R2C2 = cos; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateRotationZ (ref Double radians, out Matrix44 r) {
Double cos = Maths.Cos (radians), sin = Maths.Sin (radians);
r.R0C0 = cos; r.R0C1 = sin; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = -sin; r.R1C1 = cos; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = 0; r.R2C2 = 1; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateFromAxisAngle (ref Vector3 axis, ref Double angle, out Matrix44 r) {
Double x = axis.X, y = axis.Y, z = axis.Z;
Double sin = Maths.Sin (angle), cos = Maths.Cos (angle);
Double xx = x * x, yy = y * y, zz = z * z;
Double xy = x * y, xz = x * z, yz = y * z;
r.R0C0 = xx + (cos * (1 - xx)); r.R0C1 = xy - (cos * xy) + (sin * z); r.R0C2 = xz - (cos * xz) - (sin * y); r.R0C3 = 0;
r.R1C0 = xy - (cos * xy) - (sin * z); r.R1C1 = yy + (cos * (1 - yy)); r.R1C2 = yz - (cos * yz) + (sin * x); r.R1C3 = 0;
r.R2C0 = xz - (cos * xz) + (sin * y); r.R2C1 = yz - (cos * yz) - (sin * x); r.R2C2 = zz + (cos * (1 - zz)); r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
// Axes must be pair-wise perpendicular and have unit length.
[MI(O.AggressiveInlining)] public static void CreateFromCartesianAxes (ref Vector3 right, ref Vector3 up, ref Vector3 backward, out Matrix44 r) {
r.R0C0 = right.X; r.R0C1 = right.Y; r.R0C2 = right.Z; r.R0C3 = 0;
r.R1C0 = up.X; r.R1C1 = up.Y; r.R1C2 = up.Z; r.R1C3 = 0;
r.R2C0 = backward.X; r.R2C1 = backward.Y; r.R2C2 = backward.Z; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static void CreateWorld (ref Vector3 position, ref Vector3 forward, ref Vector3 up, out Matrix44 r) {
Vector3 backward; Vector3.Negate (ref forward, out backward); Vector3.Normalise (ref backward, out backward);
Vector3 right; Vector3.Cross (ref up, ref backward, out right); Vector3.Normalise (ref right, out right);
Vector3 finalUp; Vector3.Cross (ref right, ref backward, out finalUp); Vector3.Normalise (ref finalUp, out finalUp);
r.R0C0 = right.X; r.R0C1 = right.Y; r.R0C2 = right.Z; r.R0C3 = 0;
r.R1C0 = finalUp.X; r.R1C1 = finalUp.Y; r.R1C2 = finalUp.Z; r.R1C3 = 0;
r.R2C0 = backward.X; r.R2C1 = backward.Y; r.R2C2 = backward.Z; r.R2C3 = 0;
r.R3C0 = position.X; r.R3C1 = position.Y; r.R3C2 = position.Z; r.R3C3 = 1;
}
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/
[MI(O.AggressiveInlining)] public static void CreateFromQuaternion (ref Quaternion q, out Matrix44 r) {
Boolean qIsUnit; Quaternion.IsUnit (ref q, out qIsUnit); Debug.Assert (qIsUnit);
Double twoI = q.I + q.I, twoJ = q.J + q.J, twoK = q.K + q.K;
Double twoUI = q.U * twoI, twoUJ = q.U * twoJ, twoUK = q.U * twoK;
Double twoII = q.I * twoI, twoIJ = q.I * twoJ, twoIK = q.I * twoK;
Double twoJJ = q.J * twoJ, twoJK = q.J * twoK, twoKK = q.K * twoK;
r.R0C0 = 1 - twoJJ - twoKK; r.R1C0 = twoIJ - twoUK; r.R2C0 = twoIK + twoUJ; r.R3C0 = 0;
r.R0C1 = twoIJ + twoUK; r.R1C1 = 1 - twoII - twoKK; r.R2C1 = twoJK - twoUI; r.R3C1 = 0;
r.R0C2 = twoIK - twoUJ; r.R1C2 = twoJK + twoUI; r.R2C2 = 1 - twoII - twoJJ; r.R3C2 = 0;
r.R0C3 = 0; r.R1C3 = 0; r.R2C3 = 0; r.R3C3 = 1;
}
// Angle of rotation, in radians. Angles are measured anti-clockwise when viewed from the rotation axis (positive side) toward the origin.
[MI(O.AggressiveInlining)] public static void CreateFromYawPitchRoll (ref Double yaw, ref Double pitch, ref Double roll, out Matrix44 r) {
Double cy = Maths.Cos (yaw), sy = Maths.Sin (yaw);
Double cx = Maths.Cos (pitch), sx = Maths.Sin (pitch);
Double cz = Maths.Cos (roll), sz = Maths.Sin (roll);
r.R0C0 = cz*cy+sz*sx*sy; r.R0C1 = sz*cx; r.R0C2 = -cz*sy+sz*sx*cy; r.R0C3 = 0;
r.R1C0 = -sz*cy+cz*sx*sy; r.R1C1 = cz*cx; r.R1C2 = -cz*sy+sz*sx*cy; r.R1C3 = 0;
r.R2C0 = cx*sy; r.R2C1 = -sx; r.R2C2 = cx*cy; r.R2C3 = 0;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = 0; r.R3C3 = 1;
}
// http://msdn.microsoft.com/en-us/library/bb205351(v=vs.85).aspx
[MI(O.AggressiveInlining)] public static void CreatePerspectiveFieldOfView (ref Double fieldOfView, ref Double aspectRatio, ref Double nearPlaneDistance, ref Double farPlaneDistance, out Matrix44 r) {
Debug.Assert (fieldOfView > 0 && fieldOfView < Maths.Pi);
Debug.Assert (nearPlaneDistance > 0);
Debug.Assert (farPlaneDistance > 0);
Debug.Assert (nearPlaneDistance < farPlaneDistance);
Double yScale = (Double) 1 / (Maths.Tan (fieldOfView * Maths.Half));
Double xScale = yScale / aspectRatio;
Double f1 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
Double f2 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
r.R0C0 = xScale; r.R0C1 = 0; r.R0C2 = 0; r.R0C3 = 0;
r.R1C0 = 0; r.R1C1 = yScale; r.R1C2 = 0; r.R1C3 = 0;
r.R2C0 = 0; r.R2C1 = 0; r.R2C2 = f1; r.R2C3 = -1;
r.R3C0 = 0; r.R3C1 = 0; r.R3C2 = f2; r.R3C3 = 0;
}
// http://msdn.microsoft.com/en-us/library/bb205355(v=vs.85).aspx
[MI(O.AggressiveInlining)] public static void CreatePerspective (ref Double width, ref Double height, ref Double nearPlaneDistance, ref Double farPlaneDistance, out Matrix44 r) {
Debug.Assert (nearPlaneDistance > 0);
Debug.Assert (farPlaneDistance > 0);
Debug.Assert (nearPlaneDistance < farPlaneDistance);
r.R0C0 = (nearPlaneDistance * 2) / width;
r.R0C1 = r.R0C2 = r.R0C3 = 0;
r.R1C1 = (nearPlaneDistance * 2) / height;
r.R1C0 = r.R1C2 = r.R1C3 = 0;
r.R2C2 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
r.R2C0 = r.R2C1 = 0;
r.R2C3 = -1;
r.R3C0 = r.R3C1 = r.R3C3 = 0;
r.R3C2 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
}
// http://msdn.microsoft.com/en-us/library/bb205354(v=vs.85).aspx
[MI(O.AggressiveInlining)] public static void CreatePerspectiveOffCenter (ref Double left, ref Double right, ref Double bottom, ref Double top, ref Double nearPlaneDistance, ref Double farPlaneDistance, out Matrix44 r) {
Debug.Assert (nearPlaneDistance > 0);
Debug.Assert (farPlaneDistance > 0);
Debug.Assert (nearPlaneDistance < farPlaneDistance);
r.R0C0 = (nearPlaneDistance * 2) / (right - left);
r.R0C1 = r.R0C2 = r.R0C3 = 0;
r.R1C1 = (nearPlaneDistance * 2) / (top - bottom);
r.R1C0 = r.R1C2 = r.R1C3 = 0;
r.R2C0 = (left + right) / (right - left);
r.R2C1 = (top + bottom) / (top - bottom);
r.R2C2 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
r.R2C3 = -1;
r.R3C2 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
r.R3C0 = r.R3C1 = r.R3C3 = 0;
}
// http://msdn.microsoft.com/en-us/library/bb205349(v=vs.85).aspx
[MI(O.AggressiveInlining)] public static void CreateOrthographic (ref Double width, ref Double height, ref Double zNearPlane, ref Double zFarPlane, out Matrix44 r) {
r.R0C0 = 2 / width;
r.R0C1 = r.R0C2 = r.R0C3 = 0;
r.R1C1 = 2 / height;
r.R1C0 = r.R1C2 = r.R1C3 = 0;
r.R2C2 = 1 / (zNearPlane - zFarPlane);
r.R2C0 = r.R2C1 = r.R2C3 = 0;
r.R3C0 = r.R3C1 = 0;
r.R3C2 = zNearPlane / (zNearPlane - zFarPlane);
r.R3C3 = 1;
}
// http://msdn.microsoft.com/en-us/library/bb205348(v=vs.85).aspx
[MI(O.AggressiveInlining)] public static void CreateOrthographicOffCenter (ref Double left, ref Double right, ref Double bottom, ref Double top, ref Double zNearPlane, ref Double zFarPlane, out Matrix44 r) {
r.R0C0 = 2 / (right - left);
r.R0C1 = r.R0C2 = r.R0C3 = 0;
r.R1C1 = 2 / (top - bottom);
r.R1C0 = r.R1C2 = r.R1C3 = 0;
r.R2C2 = 1 / (zNearPlane - zFarPlane);
r.R2C0 = r.R2C1 = r.R2C3 = 0;
r.R3C0 = (left + right) / (left - right);
r.R3C1 = (top + bottom) / (bottom - top);
r.R3C2 = zNearPlane / (zNearPlane - zFarPlane);
r.R3C3 = 1;
}
// http://msdn.microsoft.com/en-us/library/bb205343(v=VS.85).aspx
[MI(O.AggressiveInlining)] public static void CreateLookAt (ref Vector3 cameraPosition, ref Vector3 cameraTarget, ref Vector3 cameraUpVector, out Matrix44 r) {
Vector3 forward; Vector3.Subtract (ref cameraPosition, ref cameraTarget, out forward); Vector3.Normalise (ref forward, out forward);
Vector3 right; Vector3.Cross (ref cameraUpVector, ref forward, out right); Vector3.Normalise (ref right, out right);
Vector3 up; Vector3.Cross (ref forward, ref right, out up); Vector3.Normalise (ref up, out up);
Double a; Vector3.Dot (ref right, ref cameraPosition, out a);
Double b; Vector3.Dot (ref up, ref cameraPosition, out b);
Double c; Vector3.Dot (ref forward, ref cameraPosition, out c);
r.R0C0 = right.X; r.R0C1 = up.X; r.R0C2 = forward.X; r.R0C3 = 0;
r.R1C0 = right.Y; r.R1C1 = up.Y; r.R1C2 = forward.Y; r.R1C3 = 0;
r.R2C0 = right.Z; r.R2C1 = up.Z; r.R2C2 = forward.Z; r.R2C3 = 0;
r.R3C0 = -a; r.R3C1 = -b; r.R3C2 = -c; r.R3C3 = 1;
}
[MI(O.AggressiveInlining)] public static Matrix44 CreateTranslation (Double xPosition, Double yPosition, Double zPosition) { Matrix44 r; CreateTranslation (ref xPosition, ref yPosition, ref zPosition, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateTranslation (Vector3 position) { Matrix44 r; CreateTranslation (ref position, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateScale (Double xScale, Double yScale, Double zScale) { Matrix44 r; CreateScale (ref xScale, ref yScale, ref zScale, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateScale (Vector3 scales) { Matrix44 r; CreateScale (ref scales, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateScale (Double scale) { Matrix44 r; CreateScale (ref scale, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateRotationX (Double radians) { Matrix44 r; CreateRotationX (ref radians, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateRotationY (Double radians) { Matrix44 r; CreateRotationY (ref radians, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateRotationZ (Double radians) { Matrix44 r; CreateRotationZ (ref radians, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateFromAxisAngle (Vector3 axis, Double angle) { Matrix44 r; CreateFromAxisAngle (ref axis, ref angle, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateFromCartesianAxes (Vector3 right, Vector3 up, Vector3 backward) { Matrix44 r; CreateFromCartesianAxes (ref right, ref up, ref backward, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateWorld (Vector3 position, Vector3 forward, Vector3 up) { Matrix44 r; CreateWorld (ref position, ref forward, ref up, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateFromQuaternion (Quaternion quaternion) { Matrix44 r; CreateFromQuaternion (ref quaternion, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateFromYawPitchRoll (Double yaw, Double pitch, Double roll) { Matrix44 r; CreateFromYawPitchRoll (ref yaw, ref pitch, ref roll, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreatePerspectiveFieldOfView (Double fieldOfView, Double aspectRatio, Double nearPlane, Double farPlane) { Matrix44 r; CreatePerspectiveFieldOfView (ref fieldOfView, ref aspectRatio, ref nearPlane, ref farPlane, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreatePerspective (Double width, Double height, Double nearPlane, Double farPlane) { Matrix44 r; CreatePerspective (ref width, ref height, ref nearPlane, ref farPlane, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreatePerspectiveOffCenter (Double left, Double right, Double bottom, Double top, Double nearPlane, Double farPlane) { Matrix44 r; CreatePerspectiveOffCenter (ref left, ref right, ref bottom, ref top, ref nearPlane, ref farPlane, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateOrthographic (Double width, Double height, Double nearPlane, Double farPlane) { Matrix44 r; CreateOrthographic (ref width, ref height, ref nearPlane, ref farPlane, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateOrthographicOffCenter (Double left, Double right, Double bottom, Double top, Double nearPlane, Double farPlane) { Matrix44 r; CreateOrthographicOffCenter (ref left, ref right, ref bottom, ref top, ref nearPlane, ref farPlane, out r); return r; }
[MI(O.AggressiveInlining)] public static Matrix44 CreateLookAt (Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector) { Matrix44 r; CreateLookAt (ref cameraPosition, ref cameraTarget, ref cameraUpVector, out r); return r; }
}
/// <summary>
/// Double precision Vector2.
/// </summary>
[StructLayout (LayoutKind.Sequential), Serializable]
public struct Vector2 : IEquatable<Vector2> {
public Double X, Y;
[MI(O.AggressiveInlining)] public Vector2 (Double x, Double y) { X = x; Y = y; }
public override String ToString () { return String.Format ("(X:{0}, Y:{1})", X, Y); }
[MI(O.AggressiveInlining)] public override Int32 GetHashCode () { return X.GetHashCode () ^ Y.GetHashCode ().ShiftAndWrap (2); }
[MI(O.AggressiveInlining)] public override Boolean Equals (Object obj) { return (obj is Vector2) ? this.Equals ((Vector2) obj) : false; }
[MI(O.AggressiveInlining)] public Boolean Equals (Vector2 other) { Boolean r; Equals (ref this, ref other, out r); return r; }
[MI(O.AggressiveInlining)] public Boolean ApproximateEquals (Vector2 other) { Boolean r; ApproximateEquals (ref this, ref other, out r); return r; }
// Constants //-------------------------------------------------------//
readonly static Vector2 zero, one;
readonly static Vector2 unitX, unitY;
static Vector2 () {
zero = new Vector2 ();
one = new Vector2 (1, 1);
unitX = new Vector2 (1, 0);
unitY = new Vector2 (0, 1);
}
public static Vector2 Zero { get { return zero; } }
public static Vector2 One { get { return one; } }
public static Vector2 UnitX { get { return unitX; } }
public static Vector2 UnitY { get { return unitY; } }
// Operators //-------------------------------------------------------//
[MI(O.AggressiveInlining)] public static void Equals (ref Vector2 a, ref Vector2 b, out Boolean r) {
r = (a.X == b.X) && (a.Y == b.Y);
}
[MI(O.AggressiveInlining)] public static void ApproximateEquals (ref Vector2 v1, ref Vector2 v2, out Boolean r) {
r = Maths.ApproximateEquals (v1.X, v2.X) && Maths.ApproximateEquals (v1.Y, v2.Y);
}
[MI(O.AggressiveInlining)] public static void Add (ref Vector2 a, ref Vector2 b, out Vector2 r) {
r.X = a.X + b.X; r.Y = a.Y + b.Y;
}
[MI(O.AggressiveInlining)] public static void Subtract (ref Vector2 a, ref Vector2 b, out Vector2 r) {
r.X = a.X - b.X; r.Y = a.Y - b.Y;