forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix3x3.cs
2163 lines (1938 loc) · 93.3 KB
/
Matrix3x3.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) 2010-2014 SharpDX - Alexandre Mutel
//
// 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.
// -----------------------------------------------------------------------------
// Original code from SlimMath project. http://code.google.com/p/slimmath/
// Greetings to SlimDX Group. Original code published with the following license:
// -----------------------------------------------------------------------------
/*
* Copyright (c) 2007-2011 SlimDX Group
*
* 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.
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace TheMaths
{
/// <summary>
/// Represents a 3x3 Matrix ( contains only Scale and Rotation ).
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Matrix3x3 : IEquatable<Matrix3x3>, IFormattable
{
/// <summary>
/// The size of the <see cref="Matrix3x3"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<Matrix3x3>();
/// <summary>
/// A <see cref="Matrix3x3"/> with all of its components set to zero.
/// </summary>
public static readonly Matrix3x3 Zero = new Matrix3x3();
/// <summary>
/// The identity <see cref="Matrix3x3"/>.
/// </summary>
public static readonly Matrix3x3 Identity = new Matrix3x3() { M11 = 1.0f, M22 = 1.0f, M33 = 1.0f };
/// <summary>
/// Value at row 1 column 1 of the Matrix3x3.
/// </summary>
public float M11;
/// <summary>
/// Value at row 1 column 2 of the Matrix3x3.
/// </summary>
public float M12;
/// <summary>
/// Value at row 1 column 3 of the Matrix3x3.
/// </summary>
public float M13;
/// <summary>
/// Value at row 2 column 1 of the Matrix3x3.
/// </summary>
public float M21;
/// <summary>
/// Value at row 2 column 2 of the Matrix3x3.
/// </summary>
public float M22;
/// <summary>
/// Value at row 2 column 3 of the Matrix3x3.
/// </summary>
public float M23;
/// <summary>
/// Value at row 3 column 1 of the Matrix3x3.
/// </summary>
public float M31;
/// <summary>
/// Value at row 3 column 2 of the Matrix3x3.
/// </summary>
public float M32;
/// <summary>
/// Value at row 3 column 3 of the Matrix3x3.
/// </summary>
public float M33;
/// <summary>
/// Initializes a new instance of the <see cref="Matrix3x3"/> struct.
/// </summary>
/// <param name="value">The value that will be assigned to all components.</param>
public Matrix3x3(float value)
{
M11 = M12 = M13 =
M21 = M22 = M23 =
M31 = M32 = M33 = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="Matrix3x3"/> struct.
/// </summary>
/// <param name="M11">The value to assign at row 1 column 1 of the Matrix3x3.</param>
/// <param name="M12">The value to assign at row 1 column 2 of the Matrix3x3.</param>
/// <param name="M13">The value to assign at row 1 column 3 of the Matrix3x3.</param>
/// <param name="M21">The value to assign at row 2 column 1 of the Matrix3x3.</param>
/// <param name="M22">The value to assign at row 2 column 2 of the Matrix3x3.</param>
/// <param name="M23">The value to assign at row 2 column 3 of the Matrix3x3.</param>
/// <param name="M31">The value to assign at row 3 column 1 of the Matrix3x3.</param>
/// <param name="M32">The value to assign at row 3 column 2 of the Matrix3x3.</param>
/// <param name="M33">The value to assign at row 3 column 3 of the Matrix3x3.</param>
public Matrix3x3(float M11, float M12, float M13,
float M21, float M22, float M23,
float M31, float M32, float M33)
{
this.M11 = M11; this.M12 = M12; this.M13 = M13;
this.M21 = M21; this.M22 = M22; this.M23 = M23;
this.M31 = M31; this.M32 = M32; this.M33 = M33;
}
/// <summary>
/// Initializes a new instance of the <see cref="Matrix3x3"/> struct.
/// </summary>
/// <param name="values">The values to assign to the components of the Matrix3x3. This must be an array with sixteen elements.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than sixteen elements.</exception>
public Matrix3x3(float[] values)
{
if (values == null)
throw new ArgumentNullException("values");
if (values.Length != 9)
throw new ArgumentOutOfRangeException("values", "There must be sixteen and only sixteen input values for Matrix3x3.");
M11 = values[0];
M12 = values[1];
M13 = values[2];
M21 = values[3];
M22 = values[4];
M23 = values[5];
M31 = values[6];
M32 = values[7];
M33 = values[8];
}
/// <summary>
/// Gets or sets the first row in the Matrix3x3; that is M11, M12, M13
/// </summary>
public Vector3 Row1
{
get { return new Vector3(M11, M12, M13); }
set { M11 = value.X; M12 = value.Y; M13 = value.Z; }
}
/// <summary>
/// Gets or sets the second row in the Matrix3x3; that is M21, M22, M23
/// </summary>
public Vector3 Row2
{
get { return new Vector3(M21, M22, M23); }
set { M21 = value.X; M22 = value.Y; M23 = value.Z; }
}
/// <summary>
/// Gets or sets the third row in the Matrix3x3; that is M31, M32, M33
/// </summary>
public Vector3 Row3
{
get { return new Vector3(M31, M32, M33); }
set { M31 = value.X; M32 = value.Y; M33 = value.Z; }
}
/// <summary>
/// Gets or sets the first column in the Matrix3x3; that is M11, M21, M31
/// </summary>
public Vector3 Column1
{
get { return new Vector3(M11, M21, M31); }
set { M11 = value.X; M21 = value.Y; M31 = value.Z; }
}
/// <summary>
/// Gets or sets the second column in the Matrix3x3; that is M12, M22, M32
/// </summary>
public Vector3 Column2
{
get { return new Vector3(M12, M22, M32); }
set { M12 = value.X; M22 = value.Y; M32 = value.Z; }
}
/// <summary>
/// Gets or sets the third column in the Matrix3x3; that is M13, M23, M33
/// </summary>
public Vector3 Column3
{
get { return new Vector3(M13, M23, M33); }
set { M13 = value.X; M23 = value.Y; M33 = value.Z; }
}
/// <summary>
/// Gets or sets the scale of the Matrix3x3; that is M11, M22, and M33.
/// </summary>
public Vector3 ScaleVector
{
get { return new Vector3(M11, M22, M33); }
set { M11 = value.X; M22 = value.Y; M33 = value.Z; }
}
/// <summary>
/// Gets a value indicating whether this instance is an identity Matrix3x3.
/// </summary>
/// <value>
/// <c>true</c> if this instance is an identity Matrix3x3; otherwise, <c>false</c>.
/// </value>
public bool IsIdentity
{
get { return this.Equals(Identity); }
}
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the Matrix3x3 component, depending on the index.</value>
/// <param name="index">The zero-based index of the component to access.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 15].</exception>
public float this[int index]
{
get
{
switch (index)
{
case 0: return M11;
case 1: return M12;
case 2: return M13;
case 3: return M21;
case 4: return M22;
case 5: return M23;
case 6: return M31;
case 7: return M32;
case 8: return M33;
}
throw new ArgumentOutOfRangeException("index", "Indices for Matrix3x3 run from 0 to 8, inclusive.");
}
set
{
switch (index)
{
case 0: M11 = value; break;
case 1: M12 = value; break;
case 2: M13 = value; break;
case 3: M21 = value; break;
case 4: M22 = value; break;
case 5: M23 = value; break;
case 6: M31 = value; break;
case 7: M32 = value; break;
case 8: M33 = value; break;
default: throw new ArgumentOutOfRangeException("index", "Indices for Matrix3x3 run from 0 to 8, inclusive.");
}
}
}
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the Matrix3x3 component, depending on the index.</value>
/// <param name="row">The row of the Matrix3x3 to access.</param>
/// <param name="column">The column of the Matrix3x3 to access.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="row"/> or <paramref name="column"/>is out of the range [0, 3].</exception>
public float this[int row, int column]
{
get
{
if (row < 0 || row > 2)
throw new ArgumentOutOfRangeException("row", "Rows and columns for matrices run from 0 to 2, inclusive.");
if (column < 0 || column > 2)
throw new ArgumentOutOfRangeException("column", "Rows and columns for matrices run from 0 to 2, inclusive.");
return this[(row * 3) + column];
}
set
{
if (row < 0 || row > 2)
throw new ArgumentOutOfRangeException("row", "Rows and columns for matrices run from 0 to 2, inclusive.");
if (column < 0 || column > 2)
throw new ArgumentOutOfRangeException("column", "Rows and columns for matrices run from 0 to 2, inclusive.");
this[(row * 3) + column] = value;
}
}
/// <summary>
/// Calculates the determinant of the Matrix3x3.
/// </summary>
/// <returns>The determinant of the Matrix3x3.</returns>
public float Determinant()
{
return M11 * M22 * M33 + M12 * M23 * M31 + M13 * M21 * M32 - M13 * M22 * M31 - M12 * M21 * M33 - M11 * M23 * M32;
}
/// <summary>
/// Inverts the Matrix3x3.
/// </summary>
public void Invert()
{
Invert(ref this, out this);
}
/// <summary>
/// Transposes the Matrix3x3.
/// </summary>
public void Transpose()
{
Transpose(ref this, out this);
}
/// <summary>
/// Orthogonalizes the specified Matrix3x3.
/// </summary>
/// <remarks>
/// <para>Orthogonalization is the process of making all rows orthogonal to each other. This
/// means that any given row in the Matrix3x3 will be orthogonal to any other given row in the
/// Matrix3x3.</para>
/// <para>Because this method uses the modified Gram-Schmidt process, the resulting Matrix3x3
/// tends to be numerically unstable. The numeric stability decreases according to the rows
/// so that the first row is the most stable and the last row is the least stable.</para>
/// <para>This operation is performed on the rows of the Matrix3x3 rather than the columns.
/// If you wish for this operation to be performed on the columns, first transpose the
/// input and than transpose the output.</para>
/// </remarks>
public void Orthogonalize()
{
Orthogonalize(ref this, out this);
}
/// <summary>
/// Orthonormalizes the specified Matrix3x3.
/// </summary>
/// <remarks>
/// <para>Orthonormalization is the process of making all rows and columns orthogonal to each
/// other and making all rows and columns of unit length. This means that any given row will
/// be orthogonal to any other given row and any given column will be orthogonal to any other
/// given column. Any given row will not be orthogonal to any given column. Every row and every
/// column will be of unit length.</para>
/// <para>Because this method uses the modified Gram-Schmidt process, the resulting Matrix3x3
/// tends to be numerically unstable. The numeric stability decreases according to the rows
/// so that the first row is the most stable and the last row is the least stable.</para>
/// <para>This operation is performed on the rows of the Matrix3x3 rather than the columns.
/// If you wish for this operation to be performed on the columns, first transpose the
/// input and than transpose the output.</para>
/// </remarks>
public void Orthonormalize()
{
Orthonormalize(ref this, out this);
}
/// <summary>
/// Decomposes a Matrix3x3 into an orthonormalized Matrix3x3 Q and a right triangular Matrix3x3 R.
/// </summary>
/// <param name="Q">When the method completes, contains the orthonormalized Matrix3x3 of the decomposition.</param>
/// <param name="R">When the method completes, contains the right triangular Matrix3x3 of the decomposition.</param>
public void DecomposeQR(out Matrix3x3 Q, out Matrix3x3 R)
{
Matrix3x3 temp = this;
temp.Transpose();
Orthonormalize(ref temp, out Q);
Q.Transpose();
R = new Matrix3x3();
R.M11 = Vector3.Dot(Q.Column1, Column1);
R.M12 = Vector3.Dot(Q.Column1, Column2);
R.M13 = Vector3.Dot(Q.Column1, Column3);
R.M22 = Vector3.Dot(Q.Column2, Column2);
R.M23 = Vector3.Dot(Q.Column2, Column3);
R.M33 = Vector3.Dot(Q.Column3, Column3);
}
/// <summary>
/// Decomposes a Matrix3x3 into a lower triangular Matrix3x3 L and an orthonormalized Matrix3x3 Q.
/// </summary>
/// <param name="L">When the method completes, contains the lower triangular Matrix3x3 of the decomposition.</param>
/// <param name="Q">When the method completes, contains the orthonormalized Matrix3x3 of the decomposition.</param>
public void DecomposeLQ(out Matrix3x3 L, out Matrix3x3 Q)
{
Orthonormalize(ref this, out Q);
L = new Matrix3x3();
L.M11 = Vector3.Dot(Q.Row1, Row1);
L.M21 = Vector3.Dot(Q.Row1, Row2);
L.M22 = Vector3.Dot(Q.Row2, Row2);
L.M31 = Vector3.Dot(Q.Row1, Row3);
L.M32 = Vector3.Dot(Q.Row2, Row3);
L.M33 = Vector3.Dot(Q.Row3, Row3);
}
/// <summary>
/// Decomposes a Matrix3x3 into a scale, rotation, and translation.
/// </summary>
/// <param name="scale">When the method completes, contains the scaling component of the decomposed Matrix3x3.</param>
/// <param name="rotation">When the method completes, contains the rotation component of the decomposed Matrix3x3.</param>
/// <remarks>
/// This method is designed to decompose an SRT transformation Matrix3x3 only.
/// </remarks>
public bool Decompose(out Vector3 scale, out Quaternion rotation)
{
//Source: Unknown
//References: http://www.gamedev.net/community/forums/topic.asp?topic_id=441695
//Scaling is the length of the rows.
scale.X = (float)Math.Sqrt((M11 * M11) + (M12 * M12) + (M13 * M13));
scale.Y = (float)Math.Sqrt((M21 * M21) + (M22 * M22) + (M23 * M23));
scale.Z = (float)Math.Sqrt((M31 * M31) + (M32 * M32) + (M33 * M33));
//If any of the scaling factors are zero, than the rotation Matrix3x3 can not exist.
if (MathUtil.IsZero(scale.X) ||
MathUtil.IsZero(scale.Y) ||
MathUtil.IsZero(scale.Z))
{
rotation = Quaternion.Identity;
return false;
}
//The rotation is the left over Matrix3x3 after dividing out the scaling.
Matrix3x3 rotationMatrix3x3 = new Matrix3x3();
rotationMatrix3x3.M11 = M11 / scale.X;
rotationMatrix3x3.M12 = M12 / scale.X;
rotationMatrix3x3.M13 = M13 / scale.X;
rotationMatrix3x3.M21 = M21 / scale.Y;
rotationMatrix3x3.M22 = M22 / scale.Y;
rotationMatrix3x3.M23 = M23 / scale.Y;
rotationMatrix3x3.M31 = M31 / scale.Z;
rotationMatrix3x3.M32 = M32 / scale.Z;
rotationMatrix3x3.M33 = M33 / scale.Z;
Utilities.RotationMatrix(ref rotationMatrix3x3, out rotation);
return true;
}
/// <summary>
/// Decomposes a uniform scale matrix into a scale, rotation, and translation.
/// A uniform scale matrix has the same scale in every axis.
/// </summary>
/// <param name="scale">When the method completes, contains the scaling component of the decomposed matrix.</param>
/// <param name="rotation">When the method completes, contains the rotation component of the decomposed matrix.</param>
/// <remarks>
/// This method is designed to decompose only an SRT transformation matrix that has the same scale in every axis.
/// </remarks>
public bool DecomposeUniformScale(out float scale, out Quaternion rotation)
{
//Scaling is the length of the rows. ( just take one row since this is a uniform matrix)
scale = (float)Math.Sqrt((M11 * M11) + (M12 * M12) + (M13 * M13));
var inv_scale = 1f / scale;
//If any of the scaling factors are zero, then the rotation matrix can not exist.
if (Math.Abs(scale) < MathUtil.ZeroTolerance)
{
rotation = Quaternion.Identity;
return false;
}
//The rotation is the left over matrix after dividing out the scaling.
Matrix3x3 rotationmatrix = new Matrix3x3();
rotationmatrix.M11 = M11 * inv_scale;
rotationmatrix.M12 = M12 * inv_scale;
rotationmatrix.M13 = M13 * inv_scale;
rotationmatrix.M21 = M21 * inv_scale;
rotationmatrix.M22 = M22 * inv_scale;
rotationmatrix.M23 = M23 * inv_scale;
rotationmatrix.M31 = M31 * inv_scale;
rotationmatrix.M32 = M32 * inv_scale;
rotationmatrix.M33 = M33 * inv_scale;
Utilities.RotationMatrix(ref rotationmatrix, out rotation);
return true;
}
/// <summary>
/// Exchanges two rows in the Matrix3x3.
/// </summary>
/// <param name="firstRow">The first row to exchange. This is an index of the row starting at zero.</param>
/// <param name="secondRow">The second row to exchange. This is an index of the row starting at zero.</param>
public void ExchangeRows(int firstRow, int secondRow)
{
if (firstRow < 0)
throw new ArgumentOutOfRangeException("firstRow", "The parameter firstRow must be greater than or equal to zero.");
if (firstRow > 2)
throw new ArgumentOutOfRangeException("firstRow", "The parameter firstRow must be less than or equal to two.");
if (secondRow < 0)
throw new ArgumentOutOfRangeException("secondRow", "The parameter secondRow must be greater than or equal to zero.");
if (secondRow > 2)
throw new ArgumentOutOfRangeException("secondRow", "The parameter secondRow must be less than or equal to two.");
if (firstRow == secondRow)
return;
float temp0 = this[secondRow, 0];
float temp1 = this[secondRow, 1];
float temp2 = this[secondRow, 2];
this[secondRow, 0] = this[firstRow, 0];
this[secondRow, 1] = this[firstRow, 1];
this[secondRow, 2] = this[firstRow, 2];
this[firstRow, 0] = temp0;
this[firstRow, 1] = temp1;
this[firstRow, 2] = temp2;
}
/// <summary>
/// Exchanges two columns in the Matrix3x3.
/// </summary>
/// <param name="firstColumn">The first column to exchange. This is an index of the column starting at zero.</param>
/// <param name="secondColumn">The second column to exchange. This is an index of the column starting at zero.</param>
public void ExchangeColumns(int firstColumn, int secondColumn)
{
if (firstColumn < 0)
throw new ArgumentOutOfRangeException("firstColumn", "The parameter firstColumn must be greater than or equal to zero.");
if (firstColumn > 2)
throw new ArgumentOutOfRangeException("firstColumn", "The parameter firstColumn must be less than or equal to two.");
if (secondColumn < 0)
throw new ArgumentOutOfRangeException("secondColumn", "The parameter secondColumn must be greater than or equal to zero.");
if (secondColumn > 2)
throw new ArgumentOutOfRangeException("secondColumn", "The parameter secondColumn must be less than or equal to two.");
if (firstColumn == secondColumn)
return;
float temp0 = this[0, secondColumn];
float temp1 = this[1, secondColumn];
float temp2 = this[2, secondColumn];
this[0, secondColumn] = this[0, firstColumn];
this[1, secondColumn] = this[1, firstColumn];
this[2, secondColumn] = this[2, firstColumn];
this[0, firstColumn] = temp0;
this[1, firstColumn] = temp1;
this[2, firstColumn] = temp2;
}
/// <summary>
/// Creates an array containing the elements of the Matrix3x3.
/// </summary>
/// <returns>A 9-element array containing the components of the Matrix3x3.</returns>
public float[] ToArray()
{
return new[] { M11, M12, M13, M21, M22, M23, M31, M32, M33 };
}
/// <summary>
/// Determines the sum of two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to add.</param>
/// <param name="right">The second Matrix3x3 to add.</param>
/// <param name="result">When the method completes, contains the sum of the two matrices.</param>
public static void Add(ref Matrix3x3 left, ref Matrix3x3 right, out Matrix3x3 result)
{
result.M11 = left.M11 + right.M11;
result.M12 = left.M12 + right.M12;
result.M13 = left.M13 + right.M13;
result.M21 = left.M21 + right.M21;
result.M22 = left.M22 + right.M22;
result.M23 = left.M23 + right.M23;
result.M31 = left.M31 + right.M31;
result.M32 = left.M32 + right.M32;
result.M33 = left.M33 + right.M33;
}
/// <summary>
/// Determines the sum of two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to add.</param>
/// <param name="right">The second Matrix3x3 to add.</param>
/// <returns>The sum of the two matrices.</returns>
public static Matrix3x3 Add(Matrix3x3 left, Matrix3x3 right)
{
Matrix3x3 result;
Add(ref left, ref right, out result);
return result;
}
/// <summary>
/// Determines the difference between two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to subtract.</param>
/// <param name="right">The second Matrix3x3 to subtract.</param>
/// <param name="result">When the method completes, contains the difference between the two matrices.</param>
public static void Subtract(ref Matrix3x3 left, ref Matrix3x3 right, out Matrix3x3 result)
{
result.M11 = left.M11 - right.M11;
result.M12 = left.M12 - right.M12;
result.M13 = left.M13 - right.M13;
result.M21 = left.M21 - right.M21;
result.M22 = left.M22 - right.M22;
result.M23 = left.M23 - right.M23;
result.M31 = left.M31 - right.M31;
result.M32 = left.M32 - right.M32;
result.M33 = left.M33 - right.M33;
}
/// <summary>
/// Determines the difference between two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to subtract.</param>
/// <param name="right">The second Matrix3x3 to subtract.</param>
/// <returns>The difference between the two matrices.</returns>
public static Matrix3x3 Subtract(Matrix3x3 left, Matrix3x3 right)
{
Matrix3x3 result;
Subtract(ref left, ref right, out result);
return result;
}
/// <summary>
/// Scales a Matrix3x3 by the given value.
/// </summary>
/// <param name="left">The Matrix3x3 to scale.</param>
/// <param name="right">The amount by which to scale.</param>
/// <param name="result">When the method completes, contains the scaled Matrix3x3.</param>
public static void Multiply(ref Matrix3x3 left, float right, out Matrix3x3 result)
{
result.M11 = left.M11 * right;
result.M12 = left.M12 * right;
result.M13 = left.M13 * right;
result.M21 = left.M21 * right;
result.M22 = left.M22 * right;
result.M23 = left.M23 * right;
result.M31 = left.M31 * right;
result.M32 = left.M32 * right;
result.M33 = left.M33 * right;
}
/// <summary>
/// Scales a Matrix3x3 by the given value.
/// </summary>
/// <param name="left">The Matrix3x3 to scale.</param>
/// <param name="right">The amount by which to scale.</param>
/// <returns>The scaled Matrix3x3.</returns>
public static Matrix3x3 Multiply(Matrix3x3 left, float right)
{
Matrix3x3 result;
Multiply(ref left, right, out result);
return result;
}
/// <summary>
/// Determines the product of two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to multiply.</param>
/// <param name="right">The second Matrix3x3 to multiply.</param>
/// <param name="result">The product of the two matrices.</param>
public static void Multiply(ref Matrix3x3 left, ref Matrix3x3 right, out Matrix3x3 result)
{
Matrix3x3 temp = new Matrix3x3();
temp.M11 = (left.M11 * right.M11) + (left.M12 * right.M21) + (left.M13 * right.M31);
temp.M12 = (left.M11 * right.M12) + (left.M12 * right.M22) + (left.M13 * right.M32);
temp.M13 = (left.M11 * right.M13) + (left.M12 * right.M23) + (left.M13 * right.M33);
temp.M21 = (left.M21 * right.M11) + (left.M22 * right.M21) + (left.M23 * right.M31);
temp.M22 = (left.M21 * right.M12) + (left.M22 * right.M22) + (left.M23 * right.M32);
temp.M23 = (left.M21 * right.M13) + (left.M22 * right.M23) + (left.M23 * right.M33);
temp.M31 = (left.M31 * right.M11) + (left.M32 * right.M21) + (left.M33 * right.M31);
temp.M32 = (left.M31 * right.M12) + (left.M32 * right.M22) + (left.M33 * right.M32);
temp.M33 = (left.M31 * right.M13) + (left.M32 * right.M23) + (left.M33 * right.M33);
result = temp;
}
/// <summary>
/// Determines the product of two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to multiply.</param>
/// <param name="right">The second Matrix3x3 to multiply.</param>
/// <returns>The product of the two matrices.</returns>
public static Matrix3x3 Multiply(Matrix3x3 left, Matrix3x3 right)
{
Matrix3x3 result;
Multiply(ref left, ref right, out result);
return result;
}
/// <summary>
/// Scales a Matrix3x3 by the given value.
/// </summary>
/// <param name="left">The Matrix3x3 to scale.</param>
/// <param name="right">The amount by which to scale.</param>
/// <param name="result">When the method completes, contains the scaled Matrix3x3.</param>
public static void Divide(ref Matrix3x3 left, float right, out Matrix3x3 result)
{
float inv = 1.0f / right;
result.M11 = left.M11 * inv;
result.M12 = left.M12 * inv;
result.M13 = left.M13 * inv;
result.M21 = left.M21 * inv;
result.M22 = left.M22 * inv;
result.M23 = left.M23 * inv;
result.M31 = left.M31 * inv;
result.M32 = left.M32 * inv;
result.M33 = left.M33 * inv;
}
/// <summary>
/// Scales a Matrix3x3 by the given value.
/// </summary>
/// <param name="left">The Matrix3x3 to scale.</param>
/// <param name="right">The amount by which to scale.</param>
/// <returns>The scaled Matrix3x3.</returns>
public static Matrix3x3 Divide(Matrix3x3 left, float right)
{
Matrix3x3 result;
Divide(ref left, right, out result);
return result;
}
/// <summary>
/// Determines the quotient of two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to divide.</param>
/// <param name="right">The second Matrix3x3 to divide.</param>
/// <param name="result">When the method completes, contains the quotient of the two matrices.</param>
public static void Divide(ref Matrix3x3 left, ref Matrix3x3 right, out Matrix3x3 result)
{
result.M11 = left.M11 / right.M11;
result.M12 = left.M12 / right.M12;
result.M13 = left.M13 / right.M13;
result.M21 = left.M21 / right.M21;
result.M22 = left.M22 / right.M22;
result.M23 = left.M23 / right.M23;
result.M31 = left.M31 / right.M31;
result.M32 = left.M32 / right.M32;
result.M33 = left.M33 / right.M33;
}
/// <summary>
/// Determines the quotient of two matrices.
/// </summary>
/// <param name="left">The first Matrix3x3 to divide.</param>
/// <param name="right">The second Matrix3x3 to divide.</param>
/// <returns>The quotient of the two matrices.</returns>
public static Matrix3x3 Divide(Matrix3x3 left, Matrix3x3 right)
{
Matrix3x3 result;
Divide(ref left, ref right, out result);
return result;
}
/// <summary>
/// Performs the exponential operation on a Matrix3x3.
/// </summary>
/// <param name="value">The Matrix3x3 to perform the operation on.</param>
/// <param name="exponent">The exponent to raise the Matrix3x3 to.</param>
/// <param name="result">When the method completes, contains the exponential Matrix3x3.</param>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="exponent"/> is negative.</exception>
public static void Exponent(ref Matrix3x3 value, int exponent, out Matrix3x3 result)
{
//Source: http://rosettacode.org
//Reference: http://rosettacode.org/wiki/Matrix3x3-exponentiation_operator
if (exponent < 0)
throw new ArgumentOutOfRangeException("exponent", "The exponent can not be negative.");
if (exponent == 0)
{
result = Matrix3x3.Identity;
return;
}
if (exponent == 1)
{
result = value;
return;
}
Matrix3x3 identity = Matrix3x3.Identity;
Matrix3x3 temp = value;
while (true)
{
if ((exponent & 1) != 0)
identity = identity * temp;
exponent /= 2;
if (exponent > 0)
temp *= temp;
else
break;
}
result = identity;
}
/// <summary>
/// Performs the exponential operation on a Matrix3x3.
/// </summary>
/// <param name="value">The Matrix3x3 to perform the operation on.</param>
/// <param name="exponent">The exponent to raise the Matrix3x3 to.</param>
/// <returns>The exponential Matrix3x3.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="exponent"/> is negative.</exception>
public static Matrix3x3 Exponent(Matrix3x3 value, int exponent)
{
Matrix3x3 result;
Exponent(ref value, exponent, out result);
return result;
}
/// <summary>
/// Negates a Matrix3x3.
/// </summary>
/// <param name="value">The Matrix3x3 to be negated.</param>
/// <param name="result">When the method completes, contains the negated Matrix3x3.</param>
public static void Negate(ref Matrix3x3 value, out Matrix3x3 result)
{
result.M11 = -value.M11;
result.M12 = -value.M12;
result.M13 = -value.M13;
result.M21 = -value.M21;
result.M22 = -value.M22;
result.M23 = -value.M23;
result.M31 = -value.M31;
result.M32 = -value.M32;
result.M33 = -value.M33;
}
/// <summary>
/// Negates a Matrix3x3.
/// </summary>
/// <param name="value">The Matrix3x3 to be negated.</param>
/// <returns>The negated Matrix3x3.</returns>
public static Matrix3x3 Negate(Matrix3x3 value)
{
Matrix3x3 result;
Negate(ref value, out result);
return result;
}
/// <summary>
/// Performs a linear interpolation between two matrices.
/// </summary>
/// <param name="start">Start Matrix3x3.</param>
/// <param name="end">End Matrix3x3.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <param name="result">When the method completes, contains the linear interpolation of the two matrices.</param>
/// <remarks>
/// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
/// </remarks>
public static void Lerp(ref Matrix3x3 start, ref Matrix3x3 end, float amount, out Matrix3x3 result)
{
result.M11 = MathUtil.Lerp(start.M11, end.M11, amount);
result.M12 = MathUtil.Lerp(start.M12, end.M12, amount);
result.M13 = MathUtil.Lerp(start.M13, end.M13, amount);
result.M21 = MathUtil.Lerp(start.M21, end.M21, amount);
result.M22 = MathUtil.Lerp(start.M22, end.M22, amount);
result.M23 = MathUtil.Lerp(start.M23, end.M23, amount);
result.M31 = MathUtil.Lerp(start.M31, end.M31, amount);
result.M32 = MathUtil.Lerp(start.M32, end.M32, amount);
result.M33 = MathUtil.Lerp(start.M33, end.M33, amount);
}
/// <summary>
/// Performs a linear interpolation between two matrices.
/// </summary>
/// <param name="start">Start Matrix3x3.</param>
/// <param name="end">End Matrix3x3.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <returns>The linear interpolation of the two matrices.</returns>
/// <remarks>
/// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
/// </remarks>
public static Matrix3x3 Lerp(Matrix3x3 start, Matrix3x3 end, float amount)
{
Matrix3x3 result;
Lerp(ref start, ref end, amount, out result);
return result;
}
/// <summary>
/// Performs a cubic interpolation between two matrices.
/// </summary>
/// <param name="start">Start Matrix3x3.</param>
/// <param name="end">End Matrix3x3.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <param name="result">When the method completes, contains the cubic interpolation of the two matrices.</param>
public static void SmoothStep(ref Matrix3x3 start, ref Matrix3x3 end, float amount, out Matrix3x3 result)
{
amount = MathUtil.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
/// <summary>
/// Performs a cubic interpolation between two matrices.
/// </summary>
/// <param name="start">Start Matrix3x3.</param>
/// <param name="end">End Matrix3x3.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <returns>The cubic interpolation of the two matrices.</returns>
public static Matrix3x3 SmoothStep(Matrix3x3 start, Matrix3x3 end, float amount)
{
Matrix3x3 result;
SmoothStep(ref start, ref end, amount, out result);
return result;
}
/// <summary>
/// Calculates the transpose of the specified Matrix3x3.
/// </summary>
/// <param name="value">The Matrix3x3 whose transpose is to be calculated.</param>
/// <param name="result">When the method completes, contains the transpose of the specified Matrix3x3.</param>
public static void Transpose(ref Matrix3x3 value, out Matrix3x3 result)
{
Matrix3x3 temp = new Matrix3x3();
temp.M11 = value.M11;
temp.M12 = value.M21;
temp.M13 = value.M31;
temp.M21 = value.M12;
temp.M22 = value.M22;
temp.M23 = value.M32;
temp.M31 = value.M13;
temp.M32 = value.M23;
temp.M33 = value.M33;
result = temp;
}
/// <summary>
/// Calculates the transpose of the specified Matrix3x3.
/// </summary>
/// <param name="value">The Matrix3x3 whose transpose is to be calculated.</param>
/// <param name="result">When the method completes, contains the transpose of the specified Matrix3x3.</param>
public static void TransposeByRef(ref Matrix3x3 value, ref Matrix3x3 result)
{
result.M11 = value.M11;
result.M12 = value.M21;
result.M13 = value.M31;
result.M21 = value.M12;
result.M22 = value.M22;
result.M23 = value.M32;
result.M31 = value.M13;
result.M32 = value.M23;
result.M33 = value.M33;
}
/// <summary>
/// Calculates the transpose of the specified Matrix3x3.
/// </summary>
/// <param name="value">The Matrix3x3 whose transpose is to be calculated.</param>
/// <returns>The transpose of the specified Matrix3x3.</returns>
public static Matrix3x3 Transpose(Matrix3x3 value)
{
Matrix3x3 result;
Transpose(ref value, out result);
return result;
}
/// <summary>
/// Calculates the inverse of the specified Matrix3x3.
/// </summary>