-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Matrix4x4.cs
2208 lines (1929 loc) · 83.7 KB
/
Matrix4x4.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Globalization;
namespace System.Numerics
{
/// <summary>
/// A structure encapsulating a 4x4 matrix.
/// </summary>
public struct Matrix4x4
{
#region Public Fields
/// <summary>
/// Value at row 1, column 1 of the matrix.
/// </summary>
public double M11;
/// <summary>
/// Value at row 1, column 2 of the matrix.
/// </summary>
public double M12;
/// <summary>
/// Value at row 1, column 3 of the matrix.
/// </summary>
public double M13;
/// <summary>
/// Value at row 1, column 4 of the matrix.
/// </summary>
public double M14;
/// <summary>
/// Value at row 2, column 1 of the matrix.
/// </summary>
public double M21;
/// <summary>
/// Value at row 2, column 2 of the matrix.
/// </summary>
public double M22;
/// <summary>
/// Value at row 2, column 3 of the matrix.
/// </summary>
public double M23;
/// <summary>
/// Value at row 2, column 4 of the matrix.
/// </summary>
public double M24;
/// <summary>
/// Value at row 3, column 1 of the matrix.
/// </summary>
public double M31;
/// <summary>
/// Value at row 3, column 2 of the matrix.
/// </summary>
public double M32;
/// <summary>
/// Value at row 3, column 3 of the matrix.
/// </summary>
public double M33;
/// <summary>
/// Value at row 3, column 4 of the matrix.
/// </summary>
public double M34;
/// <summary>
/// Value at row 4, column 1 of the matrix.
/// </summary>
public double M41;
/// <summary>
/// Value at row 4, column 2 of the matrix.
/// </summary>
public double M42;
/// <summary>
/// Value at row 4, column 3 of the matrix.
/// </summary>
public double M43;
/// <summary>
/// Value at row 4, column 4 of the matrix.
/// </summary>
public double M44;
#endregion Public Fields
private static readonly Matrix4x4 _identity = new Matrix4x4
(
1f, 0, 0, 0,
0, 1f, 0, 0,
0, 0, 1f, 0,
0, 0, 0, 1f
);
/// <summary>
/// Returns the multiplicative identity matrix.
/// </summary>
public static Matrix4x4 Identity
{
get { return _identity; }
}
/// <summary>
/// Returns whether the matrix is the identity matrix.
/// </summary>
public bool IsIdentity
{
get
{
return M11 == 1f && M22 == 1f && M33 == 1f && M44 == 1f && // Check diagonal element first for early out.
M12 == 0 && M13 == 0 && M14 == 0 &&
M21 == 0 && M23 == 0 && M24 == 0 &&
M31 == 0 && M32 == 0 && M34 == 0 &&
M41 == 0 && M42 == 0 && M43 == 0;
}
}
/// <summary>
/// Gets or sets the translation component of this matrix.
/// </summary>
public Vector3 Translation
{
get
{
return new Vector3(M41, M42, M43);
}
set
{
M41 = value.X;
M42 = value.Y;
M43 = value.Z;
}
}
/// <summary>
/// Constructs a Matrix4x4 from the given components.
/// </summary>
public Matrix4x4(double m11, double m12, double m13, double m14,
double m21, double m22, double m23, double m24,
double m31, double m32, double m33, double m34,
double m41, double m42, double m43, double m44)
{
this.M11 = m11;
this.M12 = m12;
this.M13 = m13;
this.M14 = m14;
this.M21 = m21;
this.M22 = m22;
this.M23 = m23;
this.M24 = m24;
this.M31 = m31;
this.M32 = m32;
this.M33 = m33;
this.M34 = m34;
this.M41 = m41;
this.M42 = m42;
this.M43 = m43;
this.M44 = m44;
}
/// <summary>
/// Constructs a Matrix4x4 from the given Matrix3x2.
/// </summary>
/// <param name="value">The source Matrix3x2.</param>
public Matrix4x4(Matrix3x2 value)
{
M11 = value.M11;
M12 = value.M12;
M13 = 0;
M14 = 0;
M21 = value.M21;
M22 = value.M22;
M23 = 0;
M24 = 0;
M31 = 0;
M32 = 0;
M33 = 1f;
M34 = 0;
M41 = value.M31;
M42 = value.M32;
M43 = 0;
M44 = 1f;
}
/// <summary>
/// Creates a spherical billboard that rotates around a specified object position.
/// </summary>
/// <param name="objectPosition">Position of the object the billboard will rotate around.</param>
/// <param name="cameraPosition">Position of the camera.</param>
/// <param name="cameraUpVector">The up vector of the camera.</param>
/// <param name="cameraForwardVector">The forward vector of the camera.</param>
/// <returns>The created billboard matrix</returns>
public static Matrix4x4 CreateBillboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 cameraUpVector, Vector3 cameraForwardVector)
{
const double epsilon = 1e-4f;
Vector3 zaxis = new Vector3(
objectPosition.X - cameraPosition.X,
objectPosition.Y - cameraPosition.Y,
objectPosition.Z - cameraPosition.Z);
double norm = zaxis.LengthSquared();
if (norm < epsilon)
{
zaxis = -cameraForwardVector;
}
else
{
zaxis = Vector3.Multiply(zaxis, 1.0 / Math.Sqrt(norm));
}
Vector3 xaxis = Vector3.Normalize(Vector3.Cross(cameraUpVector, zaxis));
Vector3 yaxis = Vector3.Cross(zaxis, xaxis);
Matrix4x4 result;
result.M11 = xaxis.X;
result.M12 = xaxis.Y;
result.M13 = xaxis.Z;
result.M14 = 0.0;
result.M21 = yaxis.X;
result.M22 = yaxis.Y;
result.M23 = yaxis.Z;
result.M24 = 0.0;
result.M31 = zaxis.X;
result.M32 = zaxis.Y;
result.M33 = zaxis.Z;
result.M34 = 0.0;
result.M41 = objectPosition.X;
result.M42 = objectPosition.Y;
result.M43 = objectPosition.Z;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a cylindrical billboard that rotates around a specified axis.
/// </summary>
/// <param name="objectPosition">Position of the object the billboard will rotate around.</param>
/// <param name="cameraPosition">Position of the camera.</param>
/// <param name="rotateAxis">Axis to rotate the billboard around.</param>
/// <param name="cameraForwardVector">Forward vector of the camera.</param>
/// <param name="objectForwardVector">Forward vector of the object.</param>
/// <returns>The created billboard matrix.</returns>
public static Matrix4x4 CreateConstrainedBillboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 rotateAxis, Vector3 cameraForwardVector, Vector3 objectForwardVector)
{
const double epsilon = 1e-4;
const double minAngle = 1.0 - (0.1 * (Math.PI / 180.0)); // 0.1 degrees
// Treat the case when object and camera positions are too close.
Vector3 faceDir = new Vector3(
objectPosition.X - cameraPosition.X,
objectPosition.Y - cameraPosition.Y,
objectPosition.Z - cameraPosition.Z);
double norm = faceDir.LengthSquared();
if (norm < epsilon)
{
faceDir = -cameraForwardVector;
}
else
{
faceDir = Vector3.Multiply(faceDir, (1.0 / Math.Sqrt(norm)));
}
Vector3 yaxis = rotateAxis;
Vector3 xaxis;
Vector3 zaxis;
// Treat the case when angle between faceDir and rotateAxis is too close to 0.
double dot = Vector3.Dot(rotateAxis, faceDir);
if (Math.Abs(dot) > minAngle)
{
zaxis = objectForwardVector;
// Make sure passed values are useful for compute.
dot = Vector3.Dot(rotateAxis, zaxis);
if (Math.Abs(dot) > minAngle)
{
zaxis = (Math.Abs(rotateAxis.Z) > minAngle) ? new Vector3(1, 0, 0) : new Vector3(0, 0, -1);
}
xaxis = Vector3.Normalize(Vector3.Cross(rotateAxis, zaxis));
zaxis = Vector3.Normalize(Vector3.Cross(xaxis, rotateAxis));
}
else
{
xaxis = Vector3.Normalize(Vector3.Cross(rotateAxis, faceDir));
zaxis = Vector3.Normalize(Vector3.Cross(xaxis, yaxis));
}
Matrix4x4 result;
result.M11 = xaxis.X;
result.M12 = xaxis.Y;
result.M13 = xaxis.Z;
result.M14 = 0.0;
result.M21 = yaxis.X;
result.M22 = yaxis.Y;
result.M23 = yaxis.Z;
result.M24 = 0.0;
result.M31 = zaxis.X;
result.M32 = zaxis.Y;
result.M33 = zaxis.Z;
result.M34 = 0.0;
result.M41 = objectPosition.X;
result.M42 = objectPosition.Y;
result.M43 = objectPosition.Z;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a translation matrix.
/// </summary>
/// <param name="position">The amount to translate in each axis.</param>
/// <returns>The translation matrix.</returns>
public static Matrix4x4 CreateTranslation(Vector3 position)
{
Matrix4x4 result;
result.M11 = 1.0;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = 1.0;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = 1.0;
result.M34 = 0.0;
result.M41 = position.X;
result.M42 = position.Y;
result.M43 = position.Z;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a translation matrix.
/// </summary>
/// <param name="xPosition">The amount to translate on the X-axis.</param>
/// <param name="yPosition">The amount to translate on the Y-axis.</param>
/// <param name="zPosition">The amount to translate on the Z-axis.</param>
/// <returns>The translation matrix.</returns>
public static Matrix4x4 CreateTranslation(double xPosition, double yPosition, double zPosition)
{
Matrix4x4 result;
result.M11 = 1.0;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = 1.0;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = 1.0;
result.M34 = 0.0;
result.M41 = xPosition;
result.M42 = yPosition;
result.M43 = zPosition;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a scaling matrix.
/// </summary>
/// <param name="xScale">Value to scale by on the X-axis.</param>
/// <param name="yScale">Value to scale by on the Y-axis.</param>
/// <param name="zScale">Value to scale by on the Z-axis.</param>
/// <returns>The scaling matrix.</returns>
public static Matrix4x4 CreateScale(double xScale, double yScale, double zScale)
{
Matrix4x4 result;
result.M11 = xScale;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = yScale;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = zScale;
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = 0.0;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a scaling matrix with a center point.
/// </summary>
/// <param name="xScale">Value to scale by on the X-axis.</param>
/// <param name="yScale">Value to scale by on the Y-axis.</param>
/// <param name="zScale">Value to scale by on the Z-axis.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>The scaling matrix.</returns>
public static Matrix4x4 CreateScale(double xScale, double yScale, double zScale, Vector3 centerPoint)
{
Matrix4x4 result;
double tx = centerPoint.X * (1 - xScale);
double ty = centerPoint.Y * (1 - yScale);
double tz = centerPoint.Z * (1 - zScale);
result.M11 = xScale;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = yScale;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = zScale;
result.M34 = 0.0;
result.M41 = tx;
result.M42 = ty;
result.M43 = tz;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a scaling matrix.
/// </summary>
/// <param name="scales">The vector containing the amount to scale by on each axis.</param>
/// <returns>The scaling matrix.</returns>
public static Matrix4x4 CreateScale(Vector3 scales)
{
Matrix4x4 result;
result.M11 = scales.X;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = scales.Y;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = scales.Z;
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = 0.0;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a scaling matrix with a center point.
/// </summary>
/// <param name="scales">The vector containing the amount to scale by on each axis.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>The scaling matrix.</returns>
public static Matrix4x4 CreateScale(Vector3 scales, Vector3 centerPoint)
{
Matrix4x4 result;
double tx = centerPoint.X * (1 - scales.X);
double ty = centerPoint.Y * (1 - scales.Y);
double tz = centerPoint.Z * (1 - scales.Z);
result.M11 = scales.X;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = scales.Y;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = scales.Z;
result.M34 = 0.0;
result.M41 = tx;
result.M42 = ty;
result.M43 = tz;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a uniform scaling matrix that scales equally on each axis.
/// </summary>
/// <param name="scale">The uniform scaling factor.</param>
/// <returns>The scaling matrix.</returns>
public static Matrix4x4 CreateScale(double scale)
{
Matrix4x4 result;
result.M11 = scale;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = scale;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = scale;
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = 0.0;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a uniform scaling matrix that scales equally on each axis with a center point.
/// </summary>
/// <param name="scale">The uniform scaling factor.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>The scaling matrix.</returns>
public static Matrix4x4 CreateScale(double scale, Vector3 centerPoint)
{
Matrix4x4 result;
double tx = centerPoint.X * (1 - scale);
double ty = centerPoint.Y * (1 - scale);
double tz = centerPoint.Z * (1 - scale);
result.M11 = scale;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = scale;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = scale;
result.M34 = 0.0;
result.M41 = tx;
result.M42 = ty;
result.M43 = tz;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a matrix for rotating points around the X-axis.
/// </summary>
/// <param name="radians">The amount, in radians, by which to rotate around the X-axis.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix4x4 CreateRotationX(double radians)
{
Matrix4x4 result;
double c = (double)Math.Cos(radians);
double s = (double)Math.Sin(radians);
// [ 1 0 0 0 ]
// [ 0 c s 0 ]
// [ 0 -s c 0 ]
// [ 0 0 0 1 ]
result.M11 = 1.0;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = c;
result.M23 = s;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = -s;
result.M33 = c;
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = 0.0;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a matrix for rotating points around the X-axis, from a center point.
/// </summary>
/// <param name="radians">The amount, in radians, by which to rotate around the X-axis.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix4x4 CreateRotationX(double radians, Vector3 centerPoint)
{
Matrix4x4 result;
double c = (double)Math.Cos(radians);
double s = (double)Math.Sin(radians);
double y = centerPoint.Y * (1 - c) + centerPoint.Z * s;
double z = centerPoint.Z * (1 - c) - centerPoint.Y * s;
// [ 1 0 0 0 ]
// [ 0 c s 0 ]
// [ 0 -s c 0 ]
// [ 0 y z 1 ]
result.M11 = 1.0;
result.M12 = 0.0;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = c;
result.M23 = s;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = -s;
result.M33 = c;
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = y;
result.M43 = z;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a matrix for rotating points around the Y-axis.
/// </summary>
/// <param name="radians">The amount, in radians, by which to rotate around the Y-axis.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix4x4 CreateRotationY(double radians)
{
Matrix4x4 result;
double c = (double)Math.Cos(radians);
double s = (double)Math.Sin(radians);
// [ c 0 -s 0 ]
// [ 0 1 0 0 ]
// [ s 0 c 0 ]
// [ 0 0 0 1 ]
result.M11 = c;
result.M12 = 0.0;
result.M13 = -s;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = 1.0;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = s;
result.M32 = 0.0;
result.M33 = c;
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = 0.0;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a matrix for rotating points around the Y-axis, from a center point.
/// </summary>
/// <param name="radians">The amount, in radians, by which to rotate around the Y-axis.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix4x4 CreateRotationY(double radians, Vector3 centerPoint)
{
Matrix4x4 result;
double c = (double)Math.Cos(radians);
double s = (double)Math.Sin(radians);
double x = centerPoint.X * (1 - c) - centerPoint.Z * s;
double z = centerPoint.Z * (1 - c) + centerPoint.X * s;
// [ c 0 -s 0 ]
// [ 0 1 0 0 ]
// [ s 0 c 0 ]
// [ x 0 z 1 ]
result.M11 = c;
result.M12 = 0.0;
result.M13 = -s;
result.M14 = 0.0;
result.M21 = 0.0;
result.M22 = 1.0;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = s;
result.M32 = 0.0;
result.M33 = c;
result.M34 = 0.0;
result.M41 = x;
result.M42 = 0.0;
result.M43 = z;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a matrix for rotating points around the Z-axis.
/// </summary>
/// <param name="radians">The amount, in radians, by which to rotate around the Z-axis.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix4x4 CreateRotationZ(double radians)
{
Matrix4x4 result;
double c = (double)Math.Cos(radians);
double s = (double)Math.Sin(radians);
// [ c s 0 0 ]
// [ -s c 0 0 ]
// [ 0 0 1 0 ]
// [ 0 0 0 1 ]
result.M11 = c;
result.M12 = s;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = -s;
result.M22 = c;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = 1.0;
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = 0.0;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a matrix for rotating points around the Z-axis, from a center point.
/// </summary>
/// <param name="radians">The amount, in radians, by which to rotate around the Z-axis.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix4x4 CreateRotationZ(double radians, Vector3 centerPoint)
{
Matrix4x4 result;
double c = (double)Math.Cos(radians);
double s = (double)Math.Sin(radians);
double x = centerPoint.X * (1 - c) + centerPoint.Y * s;
double y = centerPoint.Y * (1 - c) - centerPoint.X * s;
// [ c s 0 0 ]
// [ -s c 0 0 ]
// [ 0 0 1 0 ]
// [ x y 0 1 ]
result.M11 = c;
result.M12 = s;
result.M13 = 0.0;
result.M14 = 0.0;
result.M21 = -s;
result.M22 = c;
result.M23 = 0.0;
result.M24 = 0.0;
result.M31 = 0.0;
result.M32 = 0.0;
result.M33 = 1.0;
result.M34 = 0.0;
result.M41 = x;
result.M42 = y;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a matrix that rotates around an arbitrary vector.
/// </summary>
/// <param name="axis">The axis to rotate around.</param>
/// <param name="angle">The angle to rotate around the given axis, in radians.</param>
/// <returns>The rotation matrix.</returns>
public static Matrix4x4 CreateFromAxisAngle(Vector3 axis, double angle)
{
// a: angle
// x, y, z: unit vector for axis.
//
// Rotation matrix M can compute by using below equation.
//
// T T
// M = uu + (cos a)( I-uu ) + (sin a)S
//
// Where:
//
// u = ( x, y, z )
//
// [ 0 -z y ]
// S = [ z 0 -x ]
// [ -y x 0 ]
//
// [ 1 0 0 ]
// I = [ 0 1 0 ]
// [ 0 0 1 ]
//
//
// [ xx+cosa*(1-xx) yx-cosa*yx-sina*z zx-cosa*xz+sina*y ]
// M = [ xy-cosa*yx+sina*z yy+cosa(1-yy) yz-cosa*yz-sina*x ]
// [ zx-cosa*zx-sina*y zy-cosa*zy+sina*x zz+cosa*(1-zz) ]
//
double x = axis.X, y = axis.Y, z = axis.Z;
double sa = (double)Math.Sin(angle), ca = (double)Math.Cos(angle);
double xx = x * x, yy = y * y, zz = z * z;
double xy = x * y, xz = x * z, yz = y * z;
Matrix4x4 result;
result.M11 = xx + ca * (1.0 - xx);
result.M12 = xy - ca * xy + sa * z;
result.M13 = xz - ca * xz - sa * y;
result.M14 = 0.0;
result.M21 = xy - ca * xy - sa * z;
result.M22 = yy + ca * (1.0 - yy);
result.M23 = yz - ca * yz + sa * x;
result.M24 = 0.0;
result.M31 = xz - ca * xz + sa * y;
result.M32 = yz - ca * yz - sa * x;
result.M33 = zz + ca * (1.0 - zz);
result.M34 = 0.0;
result.M41 = 0.0;
result.M42 = 0.0;
result.M43 = 0.0;
result.M44 = 1.0;
return result;
}
/// <summary>
/// Creates a perspective projection matrix based on a field of view, aspect ratio, and near and far view plane distances.
/// </summary>
/// <param name="fieldOfView">Field of view in the y direction, in radians.</param>
/// <param name="aspectRatio">Aspect ratio, defined as view space width divided by height.</param>
/// <param name="nearPlaneDistance">Distance to the near view plane.</param>
/// <param name="farPlaneDistance">Distance to the far view plane.</param>
/// <returns>The perspective projection matrix.</returns>
public static Matrix4x4 CreatePerspectiveFieldOfView(double fieldOfView, double aspectRatio, double nearPlaneDistance, double farPlaneDistance)
{
if (fieldOfView <= 0.0 || fieldOfView >= Math.PI)
throw new ArgumentOutOfRangeException("fieldOfView");
if (nearPlaneDistance <= 0.0)
throw new ArgumentOutOfRangeException("nearPlaneDistance");
if (farPlaneDistance <= 0.0)
throw new ArgumentOutOfRangeException("farPlaneDistance");
if (nearPlaneDistance >= farPlaneDistance)
throw new ArgumentOutOfRangeException("nearPlaneDistance");
double yScale = 1.0 / (double)Math.Tan(fieldOfView * 0.5f);
double xScale = yScale / aspectRatio;
Matrix4x4 result;
result.M11 = xScale;
result.M12 = result.M13 = result.M14 = 0.0;
result.M22 = yScale;
result.M21 = result.M23 = result.M24 = 0.0;
result.M31 = result.M32 = 0.0;
result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M34 = -1.0;
result.M41 = result.M42 = result.M44 = 0.0;
result.M43 = nearPlaneDistance * farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
return result;
}
/// <summary>
/// Creates a perspective projection matrix from the given view volume dimensions.
/// </summary>
/// <param name="width">Width of the view volume at the near view plane.</param>
/// <param name="height">Height of the view volume at the near view plane.</param>
/// <param name="nearPlaneDistance">Distance to the near view plane.</param>
/// <param name="farPlaneDistance">Distance to the far view plane.</param>
/// <returns>The perspective projection matrix.</returns>
public static Matrix4x4 CreatePerspective(double width, double height, double nearPlaneDistance, double farPlaneDistance)
{
if (nearPlaneDistance <= 0.0)
throw new ArgumentOutOfRangeException("nearPlaneDistance");
if (farPlaneDistance <= 0.0)
throw new ArgumentOutOfRangeException("farPlaneDistance");
if (nearPlaneDistance >= farPlaneDistance)
throw new ArgumentOutOfRangeException("nearPlaneDistance");
Matrix4x4 result;
result.M11 = 2.0 * nearPlaneDistance / width;
result.M12 = result.M13 = result.M14 = 0.0;
result.M22 = 2.0 * nearPlaneDistance / height;
result.M21 = result.M23 = result.M24 = 0.0;
result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M31 = result.M32 = 0.0;
result.M34 = -1.0;
result.M41 = result.M42 = result.M44 = 0.0;
result.M43 = nearPlaneDistance * farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
return result;
}
/// <summary>
/// Creates a customized, perspective projection matrix.
/// </summary>
/// <param name="left">Minimum x-value of the view volume at the near view plane.</param>
/// <param name="right">Maximum x-value of the view volume at the near view plane.</param>
/// <param name="bottom">Minimum y-value of the view volume at the near view plane.</param>
/// <param name="top">Maximum y-value of the view volume at the near view plane.</param>
/// <param name="nearPlaneDistance">Distance to the near view plane.</param>
/// <param name="farPlaneDistance">Distance to of the far view plane.</param>
/// <returns>The perspective projection matrix.</returns>
public static Matrix4x4 CreatePerspectiveOffCenter(double left, double right, double bottom, double top, double nearPlaneDistance, double farPlaneDistance)
{
if (nearPlaneDistance <= 0.0)
throw new ArgumentOutOfRangeException("nearPlaneDistance");
if (farPlaneDistance <= 0.0)
throw new ArgumentOutOfRangeException("farPlaneDistance");
if (nearPlaneDistance >= farPlaneDistance)
throw new ArgumentOutOfRangeException("nearPlaneDistance");
Matrix4x4 result;
result.M11 = 2.0 * nearPlaneDistance / (right - left);
result.M12 = result.M13 = result.M14 = 0.0;
result.M22 = 2.0 * nearPlaneDistance / (top - bottom);
result.M21 = result.M23 = result.M24 = 0.0;
result.M31 = (left + right) / (right - left);
result.M32 = (top + bottom) / (top - bottom);
result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M34 = -1.0;
result.M43 = nearPlaneDistance * farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M41 = result.M42 = result.M44 = 0.0;
return result;
}
/// <summary>
/// Creates an orthographic perspective matrix from the given view volume dimensions.
/// </summary>
/// <param name="width">Width of the view volume.</param>
/// <param name="height">Height of the view volume.</param>
/// <param name="zNearPlane">Minimum Z-value of the view volume.</param>
/// <param name="zFarPlane">Maximum Z-value of the view volume.</param>
/// <returns>The orthographic projection matrix.</returns>
public static Matrix4x4 CreateOrthographic(double width, double height, double zNearPlane, double zFarPlane)
{
Matrix4x4 result;
result.M11 = 2.0 / width;
result.M12 = result.M13 = result.M14 = 0.0;
result.M22 = 2.0 / height;
result.M21 = result.M23 = result.M24 = 0.0;