This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
forked from mono/opentk
/
Matrix4d.cs
1305 lines (1122 loc) · 51.6 KB
/
Matrix4d.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
#region --- License ---
/*
Copyright (c) 2006 - 2008 The Open Toolkit library.
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 sell copies
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.
*/
#endregion
using System;
using System.Runtime.InteropServices;
namespace OpenTK
{
/// <summary>
/// Represents a 4x4 Matrix with double-precision components.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Matrix4d : IEquatable<Matrix4d>
{
#region Fields
/// <summary>
/// Top row of the matrix
/// </summary>
public Vector4d Row0;
/// <summary>
/// 2nd row of the matrix
/// </summary>
public Vector4d Row1;
/// <summary>
/// 3rd row of the matrix
/// </summary>
public Vector4d Row2;
/// <summary>
/// Bottom row of the matrix
/// </summary>
public Vector4d Row3;
/// <summary>
/// The identity matrix
/// </summary>
public static Matrix4d Identity = new Matrix4d(Vector4d .UnitX, Vector4d .UnitY, Vector4d .UnitZ, Vector4d .UnitW);
#endregion
#region Constructors
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="row0">Top row of the matrix</param>
/// <param name="row1">Second row of the matrix</param>
/// <param name="row2">Third row of the matrix</param>
/// <param name="row3">Bottom row of the matrix</param>
public Matrix4d(Vector4d row0, Vector4d row1, Vector4d row2, Vector4d row3)
{
Row0 = row0;
Row1 = row1;
Row2 = row2;
Row3 = row3;
}
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="m00">First item of the first row.</param>
/// <param name="m01">Second item of the first row.</param>
/// <param name="m02">Third item of the first row.</param>
/// <param name="m03">Fourth item of the first row.</param>
/// <param name="m10">First item of the second row.</param>
/// <param name="m11">Second item of the second row.</param>
/// <param name="m12">Third item of the second row.</param>
/// <param name="m13">Fourth item of the second row.</param>
/// <param name="m20">First item of the third row.</param>
/// <param name="m21">Second item of the third row.</param>
/// <param name="m22">Third item of the third row.</param>
/// <param name="m23">First item of the third row.</param>
/// <param name="m30">Fourth item of the fourth row.</param>
/// <param name="m31">Second item of the fourth row.</param>
/// <param name="m32">Third item of the fourth row.</param>
/// <param name="m33">Fourth item of the fourth row.</param>
public Matrix4d(
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)
{
Row0 = new Vector4d(m00, m01, m02, m03);
Row1 = new Vector4d(m10, m11, m12, m13);
Row2 = new Vector4d(m20, m21, m22, m23);
Row3 = new Vector4d(m30, m31, m32, m33);
}
#endregion
#region Public Members
#region Properties
/// <summary>
/// The determinant of this matrix
/// </summary>
public double Determinant
{
get
{
return
Row0.X * Row1.Y * Row2.Z * Row3.W - Row0.X * Row1.Y * Row2.W * Row3.Z + Row0.X * Row1.Z * Row2.W * Row3.Y - Row0.X * Row1.Z * Row2.Y * Row3.W
+ Row0.X * Row1.W * Row2.Y * Row3.Z - Row0.X * Row1.W * Row2.Z * Row3.Y - Row0.Y * Row1.Z * Row2.W * Row3.X + Row0.Y * Row1.Z * Row2.X * Row3.W
- Row0.Y * Row1.W * Row2.X * Row3.Z + Row0.Y * Row1.W * Row2.Z * Row3.X - Row0.Y * Row1.X * Row2.Z * Row3.W + Row0.Y * Row1.X * Row2.W * Row3.Z
+ Row0.Z * Row1.W * Row2.X * Row3.Y - Row0.Z * Row1.W * Row2.Y * Row3.X + Row0.Z * Row1.X * Row2.Y * Row3.W - Row0.Z * Row1.X * Row2.W * Row3.Y
+ Row0.Z * Row1.Y * Row2.W * Row3.X - Row0.Z * Row1.Y * Row2.X * Row3.W - Row0.W * Row1.X * Row2.Y * Row3.Z + Row0.W * Row1.X * Row2.Z * Row3.Y
- Row0.W * Row1.Y * Row2.Z * Row3.X + Row0.W * Row1.Y * Row2.X * Row3.Z - Row0.W * Row1.Z * Row2.X * Row3.Y + Row0.W * Row1.Z * Row2.Y * Row3.X;
}
}
/// <summary>
/// The first column of this matrix
/// </summary>
public Vector4d Column0
{
get { return new Vector4d(Row0.X, Row1.X, Row2.X, Row3.X); }
}
/// <summary>
/// The second column of this matrix
/// </summary>
public Vector4d Column1
{
get { return new Vector4d(Row0.Y, Row1.Y, Row2.Y, Row3.Y); }
}
/// <summary>
/// The third column of this matrix
/// </summary>
public Vector4d Column2
{
get { return new Vector4d(Row0.Z, Row1.Z, Row2.Z, Row3.Z); }
}
/// <summary>
/// The fourth column of this matrix
/// </summary>
public Vector4d Column3
{
get { return new Vector4d(Row0.W, Row1.W, Row2.W, Row3.W); }
}
/// <summary>
/// Gets or sets the value at row 1, column 1 of this instance.
/// </summary>
public double M11 { get { return Row0.X; } set { Row0.X = value; } }
/// <summary>
/// Gets or sets the value at row 1, column 2 of this instance.
/// </summary>
public double M12 { get { return Row0.Y; } set { Row0.Y = value; } }
/// <summary>
/// Gets or sets the value at row 1, column 3 of this instance.
/// </summary>
public double M13 { get { return Row0.Z; } set { Row0.Z = value; } }
/// <summary>
/// Gets or sets the value at row 1, column 4 of this instance.
/// </summary>
public double M14 { get { return Row0.W; } set { Row0.W = value; } }
/// <summary>
/// Gets or sets the value at row 2, column 1 of this instance.
/// </summary>
public double M21 { get { return Row1.X; } set { Row1.X = value; } }
/// <summary>
/// Gets or sets the value at row 2, column 2 of this instance.
/// </summary>
public double M22 { get { return Row1.Y; } set { Row1.Y = value; } }
/// <summary>
/// Gets or sets the value at row 2, column 3 of this instance.
/// </summary>
public double M23 { get { return Row1.Z; } set { Row1.Z = value; } }
/// <summary>
/// Gets or sets the value at row 2, column 4 of this instance.
/// </summary>
public double M24 { get { return Row1.W; } set { Row1.W = value; } }
/// <summary>
/// Gets or sets the value at row 3, column 1 of this instance.
/// </summary>
public double M31 { get { return Row2.X; } set { Row2.X = value; } }
/// <summary>
/// Gets or sets the value at row 3, column 2 of this instance.
/// </summary>
public double M32 { get { return Row2.Y; } set { Row2.Y = value; } }
/// <summary>
/// Gets or sets the value at row 3, column 3 of this instance.
/// </summary>
public double M33 { get { return Row2.Z; } set { Row2.Z = value; } }
/// <summary>
/// Gets or sets the value at row 3, column 4 of this instance.
/// </summary>
public double M34 { get { return Row2.W; } set { Row2.W = value; } }
/// <summary>
/// Gets or sets the value at row 4, column 1 of this instance.
/// </summary>
public double M41 { get { return Row3.X; } set { Row3.X = value; } }
/// <summary>
/// Gets or sets the value at row 4, column 2 of this instance.
/// </summary>
public double M42 { get { return Row3.Y; } set { Row3.Y = value; } }
/// <summary>
/// Gets or sets the value at row 4, column 3 of this instance.
/// </summary>
public double M43 { get { return Row3.Z; } set { Row3.Z = value; } }
/// <summary>
/// Gets or sets the value at row 4, column 4 of this instance.
/// </summary>
public double M44 { get { return Row3.W; } set { Row3.W = value; } }
#endregion
#region Instance
#region public void Invert()
/// <summary>
/// Converts this instance into its inverse.
/// </summary>
public void Invert()
{
this = Matrix4d.Invert(this);
}
#endregion
#region public void Transpose()
/// <summary>
/// Converts this instance into its transpose.
/// </summary>
public void Transpose()
{
this = Matrix4d.Transpose(this);
}
#endregion
#endregion
#region Static
#region CreateFromQuaternion
/// <summary>
/// Build a rotation matrix from the specified quaternion.
/// </summary>
/// <param name="q">Quaternion to translate.</param>
/// <param name="m">Matrix result.</param>
public static void CreateFromQuaternion(ref Quaterniond q, ref Matrix4d m)
{
double X = q.X;
double Y = q.Y;
double Z = q.Z;
double W = q.W;
double xx = X * X;
double xy = X * Y;
double xz = X * Z;
double xw = X * W;
double yy = Y * Y;
double yz = Y * Z;
double yw = Y * W;
double zz = Z * Z;
double zw = Z * W;
m = new Matrix4d();
m.Row0.X = 1 - 2 * (yy + zz);
m.Row1.X = 2 * (xy - zw);
m.Row2.X = 2 * (xz + yw);
m.Row0.Y = 2 * (xy + zw);
m.Row1.Y = 1 - 2 * (xx + zz);
m.Row2.Y = 2 * (yz - xw);
m.Row0.Z = 2 * (xz - yw);
m.Row1.Z = 2 * (yz + xw);
m.Row2.Z = 1 - 2 * (xx + yy);
m.Row3.W = 1;
}
/// <summary>
/// Build a rotation matrix from the specified quaternion.
/// </summary>
/// <param name="q">Quaternion to translate.</param>
/// <returns>A matrix instance.</returns>
public static Matrix4d CreateFromQuaternion(ref Quaterniond q)
{
double X = q.X;
double Y = q.Y;
double Z = q.Z;
double W = q.W;
double xx = X * X;
double xy = X * Y;
double xz = X * Z;
double xw = X * W;
double yy = Y * Y;
double yz = Y * Z;
double yw = Y * W;
double zz = Z * Z;
double zw = Z * W;
Matrix4d m = new Matrix4d();
m.Row0.X = 1 - 2 * (yy + zz);
m.Row1.X = 2 * (xy - zw);
m.Row2.X = 2 * (xz + yw);
m.Row0.Y = 2 * (xy + zw);
m.Row1.Y = 1 - 2 * (xx + zz);
m.Row2.Y = 2 * (yz - xw);
m.Row0.Z = 2 * (xz - yw);
m.Row1.Z = 2 * (yz + xw);
m.Row2.Z = 1 - 2 * (xx + yy);
m.Row3.W = 1;
return m;
}
#endregion
#region CreateFromAxisAngle
/// <summary>
/// Build a rotation matrix from the specified axis/angle rotation.
/// </summary>
/// <param name="axis">The axis to rotate about.</param>
/// <param name="angle">Angle in radians to rotate counter-clockwise (looking in the direction of the given axis).</param>
/// <param name="result">A matrix instance.</param>
public static void CreateFromAxisAngle(Vector3d axis, double angle, out Matrix4d result)
{
double cos = System.Math.Cos(-angle);
double sin = System.Math.Sin(-angle);
double t = 1.0 - cos;
axis.Normalize();
result = new Matrix4d(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0,
t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0,
t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0,
0, 0, 0, 1);
}
/// <summary>
/// Build a rotation matrix from the specified axis/angle rotation.
/// </summary>
/// <param name="axis">The axis to rotate about.</param>
/// <param name="angle">Angle in radians to rotate counter-clockwise (looking in the direction of the given axis).</param>
/// <returns>A matrix instance.</returns>
public static Matrix4d CreateFromAxisAngle(Vector3d axis, double angle)
{
Matrix4d result;
CreateFromAxisAngle(axis, angle, out result);
return result;
}
#endregion
#region CreateRotation[XYZ]
/// <summary>
/// Builds a rotation matrix for a rotation around the x-axis.
/// </summary>
/// <param name="angle">The counter-clockwise angle in radians.</param>
/// <param name="result">The resulting Matrix4 instance.</param>
public static void CreateRotationX(double angle, out Matrix4d result)
{
double cos = System.Math.Cos(angle);
double sin = System.Math.Sin(angle);
result.Row0 = Vector4d.UnitX;
result.Row1 = new Vector4d(0, cos, sin, 0);
result.Row2 = new Vector4d(0, -sin, cos, 0);
result.Row3 = Vector4d.UnitW;
}
/// <summary>
/// Builds a rotation matrix for a rotation around the x-axis.
/// </summary>
/// <param name="angle">The counter-clockwise angle in radians.</param>
/// <returns>The resulting Matrix4 instance.</returns>
public static Matrix4d CreateRotationX(double angle)
{
Matrix4d result;
CreateRotationX(angle, out result);
return result;
}
/// <summary>
/// Builds a rotation matrix for a rotation around the y-axis.
/// </summary>
/// <param name="angle">The counter-clockwise angle in radians.</param>
/// <param name="result">The resulting Matrix4 instance.</param>
public static void CreateRotationY(double angle, out Matrix4d result)
{
double cos = System.Math.Cos(angle);
double sin = System.Math.Sin(angle);
result.Row0 = new Vector4d(cos, 0, -sin, 0);
result.Row1 = Vector4d.UnitY;
result.Row2 = new Vector4d(sin, 0, cos, 0);
result.Row3 = Vector4d.UnitW;
}
/// <summary>
/// Builds a rotation matrix for a rotation around the y-axis.
/// </summary>
/// <param name="angle">The counter-clockwise angle in radians.</param>
/// <returns>The resulting Matrix4 instance.</returns>
public static Matrix4d CreateRotationY(double angle)
{
Matrix4d result;
CreateRotationY(angle, out result);
return result;
}
/// <summary>
/// Builds a rotation matrix for a rotation around the z-axis.
/// </summary>
/// <param name="angle">The counter-clockwise angle in radians.</param>
/// <param name="result">The resulting Matrix4 instance.</param>
public static void CreateRotationZ(double angle, out Matrix4d result)
{
double cos = System.Math.Cos(angle);
double sin = System.Math.Sin(angle);
result.Row0 = new Vector4d(cos, sin, 0, 0);
result.Row1 = new Vector4d(-sin, cos, 0, 0);
result.Row2 = Vector4d.UnitZ;
result.Row3 = Vector4d.UnitW;
}
/// <summary>
/// Builds a rotation matrix for a rotation around the z-axis.
/// </summary>
/// <param name="angle">The counter-clockwise angle in radians.</param>
/// <returns>The resulting Matrix4 instance.</returns>
public static Matrix4d CreateRotationZ(double angle)
{
Matrix4d result;
CreateRotationZ(angle, out result);
return result;
}
#endregion
#region CreateTranslation
/// <summary>
/// Creates a translation matrix.
/// </summary>
/// <param name="x">X translation.</param>
/// <param name="y">Y translation.</param>
/// <param name="z">Z translation.</param>
/// <param name="result">The resulting Matrix4d instance.</param>
public static void CreateTranslation(double x, double y, double z, out Matrix4d result)
{
result = Identity;
result.Row3 = new Vector4d(x, y, z, 1);
}
/// <summary>
/// Creates a translation matrix.
/// </summary>
/// <param name="vector">The translation vector.</param>
/// <param name="result">The resulting Matrix4d instance.</param>
public static void CreateTranslation(ref Vector3d vector, out Matrix4d result)
{
result = Identity;
result.Row3 = new Vector4d(vector.X, vector.Y, vector.Z, 1);
}
/// <summary>
/// Creates a translation matrix.
/// </summary>
/// <param name="x">X translation.</param>
/// <param name="y">Y translation.</param>
/// <param name="z">Z translation.</param>
/// <returns>The resulting Matrix4d instance.</returns>
public static Matrix4d CreateTranslation(double x, double y, double z)
{
Matrix4d result;
CreateTranslation(x, y, z, out result);
return result;
}
/// <summary>
/// Creates a translation matrix.
/// </summary>
/// <param name="vector">The translation vector.</param>
/// <returns>The resulting Matrix4d instance.</returns>
public static Matrix4d CreateTranslation(Vector3d vector)
{
Matrix4d result;
CreateTranslation(vector.X, vector.Y, vector.Z, out result);
return result;
}
#endregion
#region CreateOrthographic
/// <summary>
/// Creates an orthographic projection matrix.
/// </summary>
/// <param name="width">The width of the projection volume.</param>
/// <param name="height">The height of the projection volume.</param>
/// <param name="zNear">The near edge of the projection volume.</param>
/// <param name="zFar">The far edge of the projection volume.</param>
/// <param name="result">The resulting Matrix4d instance.</param>
public static void CreateOrthographic(double width, double height, double zNear, double zFar, out Matrix4d result)
{
CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result);
}
/// <summary>
/// Creates an orthographic projection matrix.
/// </summary>
/// <param name="width">The width of the projection volume.</param>
/// <param name="height">The height of the projection volume.</param>
/// <param name="zNear">The near edge of the projection volume.</param>
/// <param name="zFar">The far edge of the projection volume.</param>
/// <rereturns>The resulting Matrix4d instance.</rereturns>
public static Matrix4d CreateOrthographic(double width, double height, double zNear, double zFar)
{
Matrix4d result;
CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result);
return result;
}
#endregion
#region CreateOrthographicOffCenter
/// <summary>
/// Creates an orthographic projection matrix.
/// </summary>
/// <param name="left">The left edge of the projection volume.</param>
/// <param name="right">The right edge of the projection volume.</param>
/// <param name="bottom">The bottom edge of the projection volume.</param>
/// <param name="top">The top edge of the projection volume.</param>
/// <param name="zNear">The near edge of the projection volume.</param>
/// <param name="zFar">The far edge of the projection volume.</param>
/// <param name="result">The resulting Matrix4d instance.</param>
public static void CreateOrthographicOffCenter(double left, double right, double bottom, double top, double zNear, double zFar, out Matrix4d result)
{
result = new Matrix4d();
double invRL = 1 / (right - left);
double invTB = 1 / (top - bottom);
double invFN = 1 / (zFar - zNear);
result.M11 = 2 * invRL;
result.M22 = 2 * invTB;
result.M33 = -2 * invFN;
result.M41 = -(right + left) * invRL;
result.M42 = -(top + bottom) * invTB;
result.M43 = -(zFar + zNear) * invFN;
result.M44 = 1;
}
/// <summary>
/// Creates an orthographic projection matrix.
/// </summary>
/// <param name="left">The left edge of the projection volume.</param>
/// <param name="right">The right edge of the projection volume.</param>
/// <param name="bottom">The bottom edge of the projection volume.</param>
/// <param name="top">The top edge of the projection volume.</param>
/// <param name="zNear">The near edge of the projection volume.</param>
/// <param name="zFar">The far edge of the projection volume.</param>
/// <returns>The resulting Matrix4d instance.</returns>
public static Matrix4d CreateOrthographicOffCenter(double left, double right, double bottom, double top, double zNear, double zFar)
{
Matrix4d result;
CreateOrthographicOffCenter(left, right, bottom, top, zNear, zFar, out result);
return result;
}
#endregion
#region CreatePerspectiveFieldOfView
/// <summary>
/// Creates a perspective projection matrix.
/// </summary>
/// <param name="fovy">Angle of the field of view in the y direction (in radians)</param>
/// <param name="aspect">Aspect ratio of the view (width / height)</param>
/// <param name="zNear">Distance to the near clip plane</param>
/// <param name="zFar">Distance to the far clip plane</param>
/// <param name="result">A projection matrix that transforms camera space to raster space</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown under the following conditions:
/// <list type="bullet">
/// <item>fovy is zero, less than zero or larger than Math.PI</item>
/// <item>aspect is negative or zero</item>
/// <item>zNear is negative or zero</item>
/// <item>zFar is negative or zero</item>
/// <item>zNear is larger than zFar</item>
/// </list>
/// </exception>
public static void CreatePerspectiveFieldOfView(double fovy, double aspect, double zNear, double zFar, out Matrix4d result)
{
if (fovy <= 0 || fovy > Math.PI)
throw new ArgumentOutOfRangeException("fovy");
if (aspect <= 0)
throw new ArgumentOutOfRangeException("aspect");
if (zNear <= 0)
throw new ArgumentOutOfRangeException("zNear");
if (zFar <= 0)
throw new ArgumentOutOfRangeException("zFar");
double yMax = zNear * System.Math.Tan(0.5 * fovy);
double yMin = -yMax;
double xMin = yMin * aspect;
double xMax = yMax * aspect;
CreatePerspectiveOffCenter(xMin, xMax, yMin, yMax, zNear, zFar, out result);
}
/// <summary>
/// Creates a perspective projection matrix.
/// </summary>
/// <param name="fovy">Angle of the field of view in the y direction (in radians)</param>
/// <param name="aspect">Aspect ratio of the view (width / height)</param>
/// <param name="zNear">Distance to the near clip plane</param>
/// <param name="zFar">Distance to the far clip plane</param>
/// <returns>A projection matrix that transforms camera space to raster space</returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown under the following conditions:
/// <list type="bullet">
/// <item>fovy is zero, less than zero or larger than Math.PI</item>
/// <item>aspect is negative or zero</item>
/// <item>zNear is negative or zero</item>
/// <item>zFar is negative or zero</item>
/// <item>zNear is larger than zFar</item>
/// </list>
/// </exception>
public static Matrix4d CreatePerspectiveFieldOfView(double fovy, double aspect, double zNear, double zFar)
{
Matrix4d result;
CreatePerspectiveFieldOfView(fovy, aspect, zNear, zFar, out result);
return result;
}
#endregion
#region CreatePerspectiveOffCenter
/// <summary>
/// Creates an perspective projection matrix.
/// </summary>
/// <param name="left">Left edge of the view frustum</param>
/// <param name="right">Right edge of the view frustum</param>
/// <param name="bottom">Bottom edge of the view frustum</param>
/// <param name="top">Top edge of the view frustum</param>
/// <param name="zNear">Distance to the near clip plane</param>
/// <param name="zFar">Distance to the far clip plane</param>
/// <param name="result">A projection matrix that transforms camera space to raster space</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown under the following conditions:
/// <list type="bullet">
/// <item>zNear is negative or zero</item>
/// <item>zFar is negative or zero</item>
/// <item>zNear is larger than zFar</item>
/// </list>
/// </exception>
public static void CreatePerspectiveOffCenter(double left, double right, double bottom, double top, double zNear, double zFar, out Matrix4d result)
{
if (zNear <= 0)
throw new ArgumentOutOfRangeException("zNear");
if (zFar <= 0)
throw new ArgumentOutOfRangeException("zFar");
if (zNear >= zFar)
throw new ArgumentOutOfRangeException("zNear");
double x = (2.0 * zNear) / (right - left);
double y = (2.0 * zNear) / (top - bottom);
double a = (right + left) / (right - left);
double b = (top + bottom) / (top - bottom);
double c = -(zFar + zNear) / (zFar - zNear);
double d = -(2.0 * zFar * zNear) / (zFar - zNear);
result = new Matrix4d(x, 0, 0, 0,
0, y, 0, 0,
a, b, c, -1,
0, 0, d, 0);
}
/// <summary>
/// Creates an perspective projection matrix.
/// </summary>
/// <param name="left">Left edge of the view frustum</param>
/// <param name="right">Right edge of the view frustum</param>
/// <param name="bottom">Bottom edge of the view frustum</param>
/// <param name="top">Top edge of the view frustum</param>
/// <param name="zNear">Distance to the near clip plane</param>
/// <param name="zFar">Distance to the far clip plane</param>
/// <returns>A projection matrix that transforms camera space to raster space</returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown under the following conditions:
/// <list type="bullet">
/// <item>zNear is negative or zero</item>
/// <item>zFar is negative or zero</item>
/// <item>zNear is larger than zFar</item>
/// </list>
/// </exception>
public static Matrix4d CreatePerspectiveOffCenter(double left, double right, double bottom, double top, double zNear, double zFar)
{
Matrix4d result;
CreatePerspectiveOffCenter(left, right, bottom, top, zNear, zFar, out result);
return result;
}
#endregion
#region Obsolete Functions
#region Translation Functions
/// <summary>
/// Build a translation matrix with the given translation
/// </summary>
/// <param name="trans">The vector to translate along</param>
/// <returns>A Translation matrix</returns>
[Obsolete("Use CreateTranslation instead.")]
public static Matrix4d Translation(Vector3d trans)
{
return Translation(trans.X, trans.Y, trans.Z);
}
/// <summary>
/// Build a translation matrix with the given translation
/// </summary>
/// <param name="x">X translation</param>
/// <param name="y">Y translation</param>
/// <param name="z">Z translation</param>
/// <returns>A Translation matrix</returns>
[Obsolete("Use CreateTranslation instead.")]
public static Matrix4d Translation(double x, double y, double z)
{
Matrix4d result = Identity;
result.Row3 = new Vector4d(x, y, z, 1.0);
return result;
}
#endregion
#endregion
#region Scale Functions
/// <summary>
/// Build a scaling matrix
/// </summary>
/// <param name="scale">Single scale factor for x,y and z axes</param>
/// <returns>A scaling matrix</returns>
public static Matrix4d Scale(double scale)
{
return Scale(scale, scale, scale);
}
/// <summary>
/// Build a scaling matrix
/// </summary>
/// <param name="scale">Scale factors for x,y and z axes</param>
/// <returns>A scaling matrix</returns>
public static Matrix4d Scale(Vector3d scale)
{
return Scale(scale.X, scale.Y, scale.Z);
}
/// <summary>
/// Build a scaling matrix
/// </summary>
/// <param name="x">Scale factor for x-axis</param>
/// <param name="y">Scale factor for y-axis</param>
/// <param name="z">Scale factor for z-axis</param>
/// <returns>A scaling matrix</returns>
public static Matrix4d Scale(double x, double y, double z)
{
Matrix4d result;
result.Row0 = Vector4d .UnitX * x;
result.Row1 = Vector4d .UnitY * y;
result.Row2 = Vector4d .UnitZ * z;
result.Row3 = Vector4d .UnitW;
return result;
}
#endregion
#region Rotation Functions
/// <summary>
/// Build a rotation matrix that rotates about the x-axis
/// </summary>
/// <param name="angle">angle in radians to rotate counter-clockwise around the x-axis</param>
/// <returns>A rotation matrix</returns>
public static Matrix4d RotateX(double angle)
{
double cos = System.Math.Cos(angle);
double sin = System.Math.Sin(angle);
Matrix4d result;
result.Row0 = Vector4d .UnitX;
result.Row1 = new Vector4d(0.0, cos, sin, 0.0);
result.Row2 = new Vector4d(0.0, -sin, cos, 0.0);
result.Row3 = Vector4d .UnitW;
return result;
}
/// <summary>
/// Build a rotation matrix that rotates about the y-axis
/// </summary>
/// <param name="angle">angle in radians to rotate counter-clockwise around the y-axis</param>
/// <returns>A rotation matrix</returns>
public static Matrix4d RotateY(double angle)
{
double cos = System.Math.Cos(angle);
double sin = System.Math.Sin(angle);
Matrix4d result;
result.Row0 = new Vector4d(cos, 0.0, -sin, 0.0);
result.Row1 = Vector4d .UnitY;
result.Row2 = new Vector4d(sin, 0.0, cos, 0.0);
result.Row3 = Vector4d .UnitW;
return result;
}
/// <summary>
/// Build a rotation matrix that rotates about the z-axis
/// </summary>
/// <param name="angle">angle in radians to rotate counter-clockwise around the z-axis</param>
/// <returns>A rotation matrix</returns>
public static Matrix4d RotateZ(double angle)
{
double cos = System.Math.Cos(angle);
double sin = System.Math.Sin(angle);
Matrix4d result;
result.Row0 = new Vector4d(cos, sin, 0.0, 0.0);
result.Row1 = new Vector4d(-sin, cos, 0.0, 0.0);
result.Row2 = Vector4d .UnitZ;
result.Row3 = Vector4d .UnitW;
return result;
}
/// <summary>
/// Build a rotation matrix to rotate about the given axis
/// </summary>
/// <param name="axis">the axis to rotate about</param>
/// <param name="angle">angle in radians to rotate counter-clockwise (looking in the direction of the given axis)</param>
/// <returns>A rotation matrix</returns>
public static Matrix4d Rotate(Vector3d axis, double angle)
{
double cos = System.Math.Cos(-angle);
double sin = System.Math.Sin(-angle);
double t = 1.0 - cos;
axis.Normalize();
Matrix4d result;
result.Row0 = new Vector4d(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0);
result.Row1 = new Vector4d(t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0);
result.Row2 = new Vector4d(t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0);
result.Row3 = Vector4d .UnitW;
return result;
}
/// <summary>
/// Build a rotation matrix from a quaternion
/// </summary>
/// <param name="q">the quaternion</param>
/// <returns>A rotation matrix</returns>
public static Matrix4d Rotate(Quaterniond q)
{
Vector3d axis;
double angle;
q.ToAxisAngle(out axis, out angle);
return Rotate(axis, angle);
}
#endregion
#region Camera Helper Functions
/// <summary>
/// Build a world space to camera space matrix
/// </summary>
/// <param name="eye">Eye (camera) position in world space</param>
/// <param name="target">Target position in world space</param>
/// <param name="up">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
/// <returns>A Matrix that transforms world space to camera space</returns>
public static Matrix4d LookAt(Vector3d eye, Vector3d target, Vector3d up)
{
Vector3d z = Vector3d.Normalize(eye - target);
Vector3d x = Vector3d.Normalize(Vector3d.Cross(up, z));
Vector3d y = Vector3d.Normalize(Vector3d.Cross(z, x));
Matrix4d rot = new Matrix4d(new Vector4d(x.X, y.X, z.X, 0.0),
new Vector4d(x.Y, y.Y, z.Y, 0.0),
new Vector4d(x.Z, y.Z, z.Z, 0.0),
Vector4d .UnitW);
Matrix4d trans = Matrix4d.CreateTranslation(-eye);
return trans * rot;
}
/// <summary>
/// Build a world space to camera space matrix
/// </summary>
/// <param name="eyeX">Eye (camera) position in world space</param>
/// <param name="eyeY">Eye (camera) position in world space</param>
/// <param name="eyeZ">Eye (camera) position in world space</param>
/// <param name="targetX">Target position in world space</param>
/// <param name="targetY">Target position in world space</param>
/// <param name="targetZ">Target position in world space</param>
/// <param name="upX">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
/// <param name="upY">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
/// <param name="upZ">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
/// <returns>A Matrix4 that transforms world space to camera space</returns>
public static Matrix4d LookAt(double eyeX, double eyeY, double eyeZ, double targetX, double targetY, double targetZ, double upX, double upY, double upZ)
{
return LookAt(new Vector3d(eyeX, eyeY, eyeZ), new Vector3d(targetX, targetY, targetZ), new Vector3d(upX, upY, upZ));
}
/// <summary>
/// Build a projection matrix
/// </summary>
/// <param name="left">Left edge of the view frustum</param>
/// <param name="right">Right edge of the view frustum</param>
/// <param name="bottom">Bottom edge of the view frustum</param>
/// <param name="top">Top edge of the view frustum</param>
/// <param name="near">Distance to the near clip plane</param>
/// <param name="far">Distance to the far clip plane</param>
/// <returns>A projection matrix that transforms camera space to raster space</returns>
public static Matrix4d Frustum(double left, double right, double bottom, double top, double near, double far)
{
double invRL = 1.0 / (right - left);
double invTB = 1.0 / (top - bottom);
double invFN = 1.0 / (far - near);
return new Matrix4d(new Vector4d(2.0 * near * invRL, 0.0, 0.0, 0.0),
new Vector4d(0.0, 2.0 * near * invTB, 0.0, 0.0),
new Vector4d((right + left) * invRL, (top + bottom) * invTB, -(far + near) * invFN, -1.0),
new Vector4d(0.0, 0.0, -2.0 * far * near * invFN, 0.0));
}
/// <summary>
/// Build a projection matrix
/// </summary>
/// <param name="fovy">Angle of the field of view in the y direction (in radians)</param>
/// <param name="aspect">Aspect ratio of the view (width / height)</param>
/// <param name="near">Distance to the near clip plane</param>
/// <param name="far">Distance to the far clip plane</param>
/// <returns>A projection matrix that transforms camera space to raster space</returns>
public static Matrix4d Perspective(double fovy, double aspect, double near, double far)
{
double yMax = near * System.Math.Tan(0.5f * fovy);
double yMin = -yMax;
double xMin = yMin * aspect;
double xMax = yMax * aspect;