forked from cemyuksel/cyCodeBase
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcyMatrix.h
1975 lines (1733 loc) · 112 KB
/
cyMatrix.h
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
// cyCodeBase by Cem Yuksel
// [www.cemyuksel.com]
//-------------------------------------------------------------------------------
//! \file cyMatrix.h
//! \author Cem Yuksel
//!
//! \brief 2x2, 3x3, 3x4, and 4x4 matrix classes
//!
//-------------------------------------------------------------------------------
//
// Copyright (c) 2016, Cem Yuksel <[email protected]>
// All rights reserved.
//
// 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.
//
//-------------------------------------------------------------------------------
#ifndef _CY_MATRIX_H_INCLUDED_
#define _CY_MATRIX_H_INCLUDED_
//-------------------------------------------------------------------------------
#include "cyPoint.h"
//-------------------------------------------------------------------------------
namespace cy {
//-------------------------------------------------------------------------------
// Forward declarations
//! \cond HIDDEN_SYMBOLS
template <typename TYPE> class Matrix3;
template <typename TYPE> class Matrix34;
template <typename TYPE> class Matrix4;
//! \endcond
//-------------------------------------------------------------------------------
//! 2x2 matrix class.
//!
//! Its data stores 4-value array of column-major matrix elements.
//! You can use Matrix2 with Point2<TYPE> to transform 2D points.
template <typename TYPE>
class Matrix2
{
friend Matrix2 operator * ( const TYPE value, const Matrix2 &right ) { Matrix2 r; for (int i=0; i<4; i++) r.data[i] = value * right.data[i]; return r; } //!< multiply matrix by a value
friend Matrix2 Inverse( const Matrix2 &m ) { return m.GetInverse(); } //!< return the inverse of the matrix
public:
//! Elements of the matrix are column-major: \n
//! | 0 2 | \n
//! | 1 3 | \n
TYPE data[4];
//////////////////////////////////////////////////////////////////////////
//!@name Constructors
Matrix2() {} //!< Default constructor
Matrix2( const Matrix2 &matrix ) { CY_MEMCOPY(TYPE,data,matrix.data,4); } //!< Copy constructor
template <typename T> explicit Matrix2<TYPE>( const Matrix2<T> &matrix ) { CY_MEMCONVERT(TYPE,data,matrix.data,4); } //!< Copy constructor for different types
explicit Matrix2( const TYPE *values ) { Set(values); } //!< Initialize the matrix using an array of 4 values
explicit Matrix2( const TYPE &v ) { SetScaledIdentity(v); } //!< Initialize the matrix as identity scaled by v
explicit Matrix2( const Point2<TYPE> &x, const Point2<TYPE> &y ) { Set(x,y); } //!< Initialize the matrix using two vectors as columns
explicit Matrix2( const Matrix3<TYPE> &m );
explicit Matrix2( const Matrix34<TYPE> &m );
explicit Matrix2( const Matrix4<TYPE> &m );
//! Constructor using row-major order for initialization
Matrix2( const TYPE &row0col0, const TYPE &row0col1,
const TYPE &row1col0, const TYPE &row1col1 )
{
data[0] = row0col0; data[2] = row0col1;
data[1] = row1col0; data[3] = row1col1;
}
//////////////////////////////////////////////////////////////////////////
//!@name Set & Get Methods
//! Set all the values as zero
void Zero() { CY_MEMCLEAR(TYPE,data,4); }
//! Returns true if the matrix is exactly zero
bool IsZero() const { for ( int i=0; i<4; i++ ) if ( data[i] != 0 ) return false; return true; }
//! Copies the matrix data to the given values array of size 4
void Get( TYPE *values ) { CY_MEMCOPY(TYPE,values,data,4); }
//! Set Matrix using an array of 4 values
void Set( const TYPE *values ) { CY_MEMCOPY(TYPE,data,values,4); }
//! Set Matrix using two vectors as columns
void Set( const Point2<TYPE> &x, const Point2<TYPE> &y ) { x.Get(data); y.Get(data+2); }
//! Converts the matrix to an identity matrix
void SetIdentity() { SetScaledIdentity(TYPE(1)); }
//! Converts the matrix to an identity matrix scaled by a scalar
void SetScaledIdentity(TYPE v) { SetScale(v); }
//! Sets the matrix as the tensor product (outer product) of two vectors
void SetTensorProduct( const Point2<TYPE> &v0, const Point2<TYPE> &v1 )
{
for ( int i=0; i<2; i++ ) data[ i] = v0[i] * v1.x;
for ( int i=0; i<2; i++ ) data[2+i] = v0[i] * v1.y;
}
//////////////////////////////////////////////////////////////////////////
//!@name Affine transformations
//! Sets a uniform scale matrix
void SetScale( const TYPE &uniformScale ) { SetScale(uniformScale,uniformScale); }
//! Sets a scale matrix
void SetScale( const TYPE &scaleX, const TYPE &scaleY ) { data[0]=scaleX; data[1]=0; data[2]=0; data[3]=scaleY;}
//! Sets a scale matrix
void SetScale( const Point2<TYPE> &scale ) { SetScale(scale.x,scale.y); }
//! Removes the scale component of the matrix
void SetNoScale() { Point2<TYPE> *p = (Point2<TYPE>*)data; p[0].Normalize(); p[1].Normalize(); }
//! Set a rotation matrix by angle
void SetRotation( TYPE angle ) { SetRotation( cySin(angle), cyCos(angle) ); }
//! Set a rotation matrix by cos and sin of angle
void SetRotation( TYPE sinAngle, TYPE cosAngle ) { data[0]=cosAngle; data[1]=-sinAngle; data[2]=sinAngle; data[3]=cosAngle; }
//////////////////////////////////////////////////////////////////////////
//!@name Set Row, Column, or Diagonal
void SetRow( int row, TYPE x, TYPE y ) { data[row]=x; data[row+2]=y; } //!< Sets a row of the matrix
void SetColumn( int column, TYPE x, TYPE y ) { data[2*column]=x; data[2*column+1]=y; } //!< Sets a column of the matrix
void SetDiagonal( const TYPE &xx, const TYPE &yy ) { data[0]=xx; data[3]=yy; } //!< Sets the diagonal values of the matrix
void SetDiagonal( const Point2<TYPE> &p ) { SetDiagonal( p.x, p.y ); } //!< Sets the diagonal values of the matrix
void SetDiagonal( const TYPE *values ) { SetDiagonal(values[0],values[1]); } //!< Sets the diagonal values of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Get Row, Column, or Diagonal
Point2<TYPE> GetRow ( int row ) const { return Point2<TYPE>( data[row], data[row+2] ); } //!< Returns a row of the matrix
void GetRow ( int row, Point2<TYPE> &p ) const { p.Set( data[row], data[row+1] ); } //!< Returns a row of the matrix
void GetRow ( int row, TYPE *values ) const { values[0]=data[row]; values[1]=data[row+2]; } //!< Returns a row of the matrix
Point2<TYPE> GetColumn( int col ) const { return Point2<TYPE>( &data[col*2] ); } //!< Returns a column of the matrix
void GetColumn( int col, Point2<TYPE> &p ) const { p.Set( &data[col*2] ); } //!< Returns a column of the matrix
void GetColumn( int col, TYPE *values ) const { values[0]=data[col*2]; values[1]=data[col*2+1]; } //!< Returns a column of the matrix
Point2<TYPE> GetDiagonal() const { return Point2<TYPE>( data[0], data[3] ); } //!< Returns the diagonal of the matrix
void GetDiagonal( Point2<TYPE> &p ) const { p.Set( data[0], data[3] ); } //!< Returns the diagonal of the matrix
void GetDiagonal( TYPE *values ) const { values[0]=data[0]; values[1]=data[3]; } //!< Returns the diagonal of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Comparison Operators
bool operator == ( const Matrix2 &right ) const { for ( int i=0; i<4; i++ ) if ( data[i] != right.data[i] ) return false; return true; } //!< compare equal
bool operator != ( const Matrix2 &right ) const { for ( int i=0; i<4; i++ ) if ( data[i] != right.data[i] ) return true; return false; } //!< compare not equal
//////////////////////////////////////////////////////////////////////////
//!@name Access Operators
TYPE& operator () ( int row, int column ) { return data[ column * 2 + row ]; } //!< subscript operator
const TYPE& operator () ( int row, int column ) const { return data[ column * 2 + row ]; } //!< constant subscript operator
TYPE& operator [] ( int i ) { return data[i]; } //!< subscript operator
const TYPE& operator [] ( int i ) const { return data[i]; } //!< constant subscript operator
//////////////////////////////////////////////////////////////////////////
//!@name Unary and Binary Operators
// Unary operators
Matrix2 operator - () const { Matrix2 r; for (int i=0; i<4; i++) r.data[i]=- data[i]; return r; } //!< negative matrix
// Binary operators
Matrix2 operator * ( const TYPE &value ) const { Matrix2 r; for (int i=0; i<4; i++) r.data[i] = data[i] * value; return r; } //!< multiply matrix by a value
Matrix2 operator / ( const TYPE &value ) const { Matrix2 r; for (int i=0; i<4; i++) r.data[i] = data[i] / value; return r; } //!< divide matrix by a value;
Matrix2 operator + ( const Matrix2 &right ) const { Matrix2 r; for (int i=0; i<4; i++) r.data[i] = data[i] + right.data[i]; return r; } //!< add two Matrices
Matrix2 operator - ( const Matrix2 &right ) const { Matrix2 r; for (int i=0; i<4; i++) r.data[i] = data[i] - right.data[i]; return r; } //!< subtract one Matrix2 from another
Matrix2 operator * ( const Matrix2 &right ) const //!< multiply a matrix with another
{
Matrix2 r;
r[0] = data[0] * right.data[0] + data[2] * right.data[1];
r[1] = data[1] * right.data[0] + data[3] * right.data[1];
r[2] = data[0] * right.data[2] + data[2] * right.data[3];
r[3] = data[1] * right.data[2] + data[3] * right.data[3];
return r;
}
Point2<TYPE> operator * ( const Point2<TYPE> &p ) const { return Point2<TYPE>( p.x*data[0] + p.y*data[2], p.x*data[1] + p.y*data[3] ); }
//////////////////////////////////////////////////////////////////////////
//!@name Assignment Operators
const Matrix2& operator = ( const Matrix2 &right ) { CY_MEMCOPY(TYPE,data,right.data,4); return *this; }
const Matrix2& operator += ( const Matrix2 &right ) { for (int i=0; i<4; i++) data[i] += right.data[i]; return *this; } //!< add two Matrices modify this
const Matrix2& operator -= ( const Matrix2 &right ) { for (int i=0; i<4; i++) data[i] -= right.data[i]; return *this; } //!< subtract one Matrix2 from another matrix and modify this matrix
const Matrix2& operator *= ( const Matrix2 &right ) { *this = operator*(right); return *this; } //!< multiply a matrix with another matrix and modify this matrix
const Matrix2& operator *= ( const TYPE value ) { for (int i=0; i<4; i++) data[i] *= value; return *this; } //!< multiply a matrix with a value modify this matrix
const Matrix2& operator /= ( const TYPE value ) { for (int i=0; i<4; i++) data[i] /= value; return *this; } //!< divide the matrix by a value modify the this matrix
//////////////////////////////////////////////////////////////////////////
//!@name Other Methods
void Transpose() { TYPE tmp=data[0]; data[0]=data[3]; data[3]=tmp; } //!< Transpose this matrix
void GetTranspose( Matrix2 &m ) const //!< return Transpose of this matrix
{
m.data[0] = data[0]; m.data[1] = data[2];
m.data[2] = data[1]; m.data[3] = data[3];
}
Matrix2 GetTranspose() const { Matrix2 t; GetTranspose(t); return t; } //!< return Transpose of this matrix
//! Multiply the give vector with the transpose of the matrix
Point2<TYPE> TransposeMult( const Point2<TYPE> &p ) const { return Point2<TYPE>( p.x*data[0] + p.y*data[1], p.x*data[2] + p.y*data[3] ); }
TYPE GetDeterminant() const { return data[0]*data[3]-data[2]*data[1]; } //!< Get the determinant of this matrix
void Invert() //!< Invert this matrix
{
TYPE det = GetDeterminant();
TYPE d0 = data[0] / det;
data[0] = data[3] / det;
data[1] = -data[1] / det;
data[2] = -data[2] / det;
data[3] = d0;
}
void GetInverse( Matrix2 &inverse ) const { inverse=*this; inverse.Invert(); } //!< Get the inverse of this matrix
Matrix2 GetInverse() const { Matrix2 inv(*this); inv.Invert(); return inv; } //!< Get the inverse of this matrix
//! Orthogonalizes the matrix and removes the scale component, preserving the x direction
void OrthogonalizeX()
{
Point2<TYPE> *p = (Point2<TYPE>*)data;
p[0].Normalize();
p[1] -= p[0] * (p[1]%p[0]);
p[1].Normalize();
}
//! Orthogonalizes the matrix and removes the scale component, preserving the y direction
void OrthogonalizeY()
{
Point2<TYPE> *p = (Point2<TYPE>*)data;
p[1].Normalize();
p[0] -= p[1] * (p[0]%p[1]);
p[0].Normalize();
}
//! Returns if the matrix is identity within the given error tollerance.
bool IsIdentity( TYPE tollerance=TYPE(0.001) ) const { return cyAbs(data[0] - TYPE(1)) < tollerance && cyAbs(data[1]) < tollerance && cyAbs(data[2]) < tollerance && cyAbs(data[3] - TYPE(1)) < tollerance; }
//! Returns if the matrix is symmetric within the given error tollerance.
bool IsSymmetric( TYPE tollerance=TYPE(0.001) ) const { return cyAbs(data[0] - data[2]) < tollerance; }
//////////////////////////////////////////////////////////////////////////
//!@name Static Methods
//! Returns an identity matrix
static Matrix2 MatrixIdentity() { Matrix2 m; m.SetIdentity(); return m; }
//! Returns a rotation matrix about the given axis by angle in radians
static Matrix2 MatrixRotation( TYPE angle ) { Matrix2 m; m.SetRotation(angle); return m; }
//! Returns a uniform scale matrix
static Matrix2 MatrixScale( const TYPE &uniformScale ) { Matrix2 m; m.SetScale(uniformScale); return m; }
//! Returns a scale matrix
static Matrix2 MatrixScale( const TYPE &scaleX, const TYPE &scaleY ) { Matrix2 m; m.SetScale(scaleX,scaleY); return m; }
//! Returns a scale matrix
static Matrix2 MatrixScale( const Point2<TYPE> &scale ) { Matrix2 m; m.SetScale(scale); return m; }
//////////////////////////////////////////////////////////////////////////
};
//-------------------------------------------------------------------------------
//! 3x3 matrix class.
//!
//! Its data stores 9-value array of column-major matrix elements.
//! You can use Matrix3 with Point3<TYPE> to transform 3D points.
template <typename TYPE>
class Matrix3
{
#ifdef CY_NONVECTORIZED_MATRIX3
friend Matrix3 operator * ( const TYPE value, const Matrix3 &right ) { Matrix3 r; for (int i=0; i<9; i++) r.data[i] = value * right.data[i]; return r; } //!< multiply matrix by a value
#else
friend Matrix3 operator * ( const TYPE value, const Matrix3 &right ) { Matrix3 r; _CY_IVDEP_FOR (int i=0; i<8; i++) r.data[i] = value * right.data[i]; r.data[8] = value * right.data[8]; return r; } //!< multiply matrix by a value
#endif
friend Matrix3 Inverse( const Matrix3 &m ) { return m.GetInverse(); } //!< return the inverse of the matrix
public:
//! Elements of the matrix are column-major: \n
//! | 0 3 6 | \n
//! | 1 4 7 | \n
//! | 2 5 8 | \n
TYPE data[9];
//////////////////////////////////////////////////////////////////////////
//!@name Constructors
Matrix3() {} //!< Default constructor
Matrix3( const Matrix3 &matrix ) { CY_MEMCOPY(TYPE,data,matrix.data,9); } //!< Copy constructor
template <typename T> explicit Matrix3<TYPE>( const Matrix3<T> &matrix ) { CY_MEMCONVERT(TYPE,data,matrix.data,9); } //!< Copy constructor for different types
explicit Matrix3( const TYPE *values ) { Set(values); } //!< Initialize the matrix using an array of 9 values
explicit Matrix3( const TYPE &v ) { SetScaledIdentity(v); } //!< Initialize the matrix as identity scaled by v
explicit Matrix3( const Point3<TYPE> &x, const Point3<TYPE> &y, const Point3<TYPE> &z ) { Set(x,y,z); } //!< Initialize the matrix using x,y,z vectors as columns
explicit Matrix3( const Matrix2<TYPE> &m ) {
data[0] = m.data[0]; data[1] = m.data[1]; data[2] = TYPE(0);
data[3] = m.data[2]; data[4] = m.data[3]; data[5] = TYPE(0);
data[6] = TYPE(0); data[7] = TYPE(0); data[8] = TYPE(1);
}
explicit Matrix3( const Matrix34<TYPE> &m );
explicit Matrix3( const Matrix4<TYPE> &m );
//! Constructor using row-major order for initialization
Matrix3( const TYPE &row0col0, const TYPE &row0col1, const TYPE &row0col2,
const TYPE &row1col0, const TYPE &row1col1, const TYPE &row1col2,
const TYPE &row2col0, const TYPE &row2col1, const TYPE &row2col2 )
{
data[0] = row0col0; data[3] = row0col1; data[6] = row0col2;
data[1] = row1col0; data[4] = row1col1; data[7] = row1col2;
data[2] = row2col0; data[5] = row2col1; data[8] = row2col2;
}
//////////////////////////////////////////////////////////////////////////
//!@name Set & Get Methods
//! Set all the values as zero
void Zero() { CY_MEMCLEAR(TYPE,data,9); }
//! Returns true if the matrix is exactly zero
bool IsZero() const { for ( int i=0; i<9; i++ ) if ( data[i] != 0 ) return false; return true; }
//! Copies the matrix data to the given values array of size 9
void Get( TYPE *values ) { CY_MEMCOPY(TYPE,values,data,9); }
//! Set matrix using an array of 9 values
void Set( const TYPE *values ) { CY_MEMCOPY(TYPE,data,values,9); }
//! Set matrix using x,y,z vectors as columns
void Set( const Point3<TYPE> &x, const Point3<TYPE> &y, const Point3<TYPE> &z ) { x.Get(&data[0]); y.Get(&data[3]); z.Get(&data[6]); }
//! Converts the matrix to an identity matrix
void SetIdentity() { SetScaledIdentity(TYPE(1)); }
//! Converts the matrix to an identity matrix scaled by a scalar
void SetScaledIdentity(TYPE v) { SetScale(v); }
//! Sets the matrix as the tensor product (outer product) of two vectors
void SetTensorProduct( const Point3<TYPE> &v0, const Point3<TYPE> &v1 )
{
for ( int i=0; i<3; i++ ) data[ i] = v0[i] * v1.x;
for ( int i=0; i<3; i++ ) data[3+i] = v0[i] * v1.y;
for ( int i=0; i<3; i++ ) data[6+i] = v0[i] * v1.z;
}
//! Matrix representation of the cross product ( a x b)
void SetCrossProd( const Point3<TYPE> &p ) { data[0]=TYPE(0); data[1]=p.z; data[2]=-p.y; data[3]=-p.z; data[4]=TYPE(0); data[5]=p.x; data[6]=p.y; data[7]=-p.x; data[8]=TYPE(0); }
//////////////////////////////////////////////////////////////////////////
//!@name Affine transformations
//! Sets a uniform scale matrix
void SetScale( const TYPE &uniformScale ) { SetScale(uniformScale,uniformScale,uniformScale); }
//! Sets a scale matrix
void SetScale( const TYPE &scaleX, const TYPE &scaleY, const TYPE &scaleZ )
{
data[0] = scaleX; data[1] = 0; data[2]=0;
data[3] = 0; data[4] = scaleY; data[5]=0;
data[6] = 0; data[7] = 0; data[8]=scaleZ;
}
//! Sets a scale matrix
void SetScale( const Point3<TYPE> &scale ) { SetScale(scale.x,scale.y,scale.z); }
//! Removes the scale component of the matrix
void SetNoScale() { Point3<TYPE> *p = (Point3<TYPE>*)data; p[0].Normalize(); p[1].Normalize(); p[2].Normalize(); }
//! Set as rotation matrix around x axis
void SetRotationX( TYPE angle ) { SetRotationX( cySin(angle), cyCos(angle) ); }
//! Set as rotation matrix around x axis by cos and sin of angle
void SetRotationX( TYPE sinAngle, TYPE cosAngle )
{
data[0] = TYPE(1); data[1] = TYPE(0); data[2] = TYPE(0);
data[3] = TYPE(0); data[4] = cosAngle; data[5] = sinAngle;
data[6] = TYPE(0); data[7] = -sinAngle; data[8] = cosAngle;
}
//! Set as rotation matrix around y axis
void SetRotationY( TYPE angle ) { SetRotationY( cySin(angle), cyCos(angle) ); }
//! Set as rotation matrix around y axis by cos and sin of angle
void SetRotationY( TYPE sinAngle, TYPE cosAngle )
{
data[0] = cosAngle; data[1] = TYPE(0); data[2] = -sinAngle;
data[3] = TYPE(0); data[4] = TYPE(1); data[5] = TYPE(0);
data[6] = sinAngle; data[7] = TYPE(0); data[8] = cosAngle;
}
//! Set as rotation matrix around z axis
void SetRotationZ( TYPE angle ) { SetRotationZ( cySin(angle), cyCos(angle) ); }
//! Set as rotation matrix around z axis by cos and sin of angle
void SetRotationZ( TYPE sinAngle, TYPE cosAngle )
{
data[0] = cosAngle; data[1] = sinAngle; data[2] = TYPE(0);
data[3] = -sinAngle; data[4] = cosAngle; data[5] = TYPE(0);
data[6] = TYPE(0); data[7] = TYPE(0); data[8] = TYPE(1);
}
//! Set as rotation matrix around x, y, and then z axes ( Rz * Ry * Rx )
void SetRotationXYZ( TYPE angleX, TYPE angleY, TYPE angleZ )
{
const TYPE sx = cySin(angleX);
const TYPE cx = cyCos(angleX);
const TYPE sy = cySin(angleY);
const TYPE cy = cyCos(angleY);
const TYPE sz = cySin(angleZ);
const TYPE cz = cyCos(angleZ);
data[0] = cy*cz; data[1] = cy*sz; data[2] =-sy;
data[3] = cz*sx*sy - cx*sz; data[4] = cx*cz + sx*sy*sz; data[5] = cy*sx;
data[6] = cx*cz*sy + sx*sz; data[7] =-cz*sx + cx*sy*sz; data[8] = cx*cy;
}
//! Set as rotation matrix around z, y, and then x axes ( Rx * Ry * Rz )
void SetRotationZYX( TYPE angleX, TYPE angleY, TYPE angleZ )
{
const TYPE sx = cySin(angleX);
const TYPE cx = cyCos(angleX);
const TYPE sy = cySin(angleY);
const TYPE cy = cyCos(angleY);
const TYPE sz = cySin(angleZ);
const TYPE cz = cyCos(angleZ);
data[0] = cy*cz; data[1] = cx*sz + sx*sy*cz; data[2] = sx*sz - cx*sy*cz;
data[3] = -cy*sz; data[4] = cx*cz - sx*sy*sz; data[5] = sx*cz + cx*sy*sz;
data[6] = sy; data[7] = -sx*cy; data[8] = cx*cy;
}
//! Set a rotation matrix about the given axis by angle
void SetRotation( const Point3<TYPE> &axis, TYPE angle ) { SetRotation(axis,cySin(angle),cyCos(angle)); }
//! Set a rotation matrix about the given axis by cos and sin of angle
void SetRotation( const Point3<TYPE> &axis, TYPE sinAngle, TYPE cosAngle )
{
const TYPE t = TYPE(1) - cosAngle;
const TYPE tx = t * axis.x;
const TYPE ty = t * axis.y;
const TYPE tz = t * axis.z;
const TYPE txy = tx * axis.y;
const TYPE txz = tx * axis.z;
const TYPE tyz = ty * axis.z;
const TYPE sx = sinAngle * axis.x;
const TYPE sy = sinAngle * axis.y;
const TYPE sz = sinAngle * axis.z;
data[0] = tx * axis.x + cosAngle; data[1] = txy + sz; data[2] = txz - sy;
data[3] = txy - sz; data[4] = ty * axis.y + cosAngle; data[5] = tyz + sx;
data[6] = txz + sy; data[7] = tyz - sx; data[8] = tz * axis.z + cosAngle;
}
//! Set a rotation matrix that sets [from] unit vector to [to] unit vector
void SetRotation( const Point3<TYPE> &from, const Point3<TYPE> &to )
{
TYPE c = from.Dot(to);
if ( c > TYPE(0.9999999) ) SetIdentity();
else {
TYPE s = cySqrt(TYPE(1) - c*c);
Point3<TYPE> axis = from.Cross(to).GetNormalized();
SetRotation(axis, s, c);
}
}
//! Set view matrix using position, target and approximate up vector
void SetView( const Point3<TYPE> &target, const Point3<TYPE> &up )
{
Point3<TYPE> f = target;
f.Normalize();
Point3<TYPE> s = f.Cross(up);
s.Normalize();
Point3<TYPE> u = s.Cross(f);
data[0] = s.x; data[1] = u.x; data[2] = -f.x;
data[3] = s.y; data[4] = u.y; data[5] = -f.y;
data[6] = s.z; data[7] = u.z; data[8] = -f.z;
}
//! Set matrix using normal, and approximate x direction
void SetNormal(const Point3<TYPE> &normal, const Point3<TYPE> &dir ) { Point3<TYPE> y = normal.Cross(dir); y.Normalize(); Point3<TYPE> newdir=y.Cross(normal); Set(newdir,y,normal); }
//////////////////////////////////////////////////////////////////////////
//!@name Set Row, Column, or Diagonal
void SetRow ( int row, TYPE x, TYPE y, TYPE z ) { data[row]=x; data[row+3]=y; data[row+6]=z; } //!< Sets a row of the matrix
void SetColumn( int column, TYPE x, TYPE y, TYPE z ) { data[3*column]=x; data[3*column+1]=y; data[3*column+2]=z; } //!< Sets a column of the matrix
void SetDiagonal( const TYPE &xx, const TYPE &yy, const TYPE &zz ) { data[0]=xx; data[4]=yy; data[8]=zz; } //!< Sets the diagonal values of the matrix
void SetDiagonal( const Point3<TYPE> &p ) { SetDiagonal( p.x, p.y, p.z ); } //!< Sets the diagonal values of the matrix
void SetDiagonal( const TYPE *values ) { SetDiagonal(values[0],values[1],values[2]); } //!< Sets the diagonal values of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Get Row, Column, or Diagonal
Point3<TYPE> GetRow ( int row ) const { return Point3<TYPE>( data[row], data[row+3], data[row+6] ); } //!< Returns a row of the matrix
void GetRow ( int row, Point3<TYPE> &p ) const { p.Set( data[row], data[row+3], data[row+6] ); } //!< Returns a row of the matrix
void GetRow ( int row, TYPE *values ) const { values[0]=data[row]; values[1]=data[row+3]; values[2]=data[row+6]; } //!< Returns a row of the matrix
Point3<TYPE> GetColumn( int col ) const { return Point3<TYPE>( &data[col*3] ); } //!< Returns a column of the matrix
void GetColumn( int col, Point3<TYPE> &p ) const { p.Set( &data[col*3] ); } //!< Returns a column of the matrix
void GetColumn( int col, TYPE *values ) const { values[0]=data[col*3]; values[1]=data[col*3+1]; values[2]=data[col*3+2]; } //!< Returns a column of the matrix
Point3<TYPE> GetDiagonal() const { Point3<TYPE> r; GetDiagonal(r); return r; } //!< Returns the diagonal of the matrix
void GetDiagonal( Point3<TYPE> &p ) const { GetDiagonal(&p.x); } //!< Returns the diagonal of the matrix
void GetDiagonal( TYPE *values ) const { values[0]=data[0]; values[1]=data[4]; values[2]=data[8]; } //!< Returns the diagonal of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Get Sub-matrix data
void GetSubMatrix ( Matrix2<TYPE> &m ) const { GetSubMatrix2(m); } //!< Returns the 2x2 portion of the matrix
Matrix2<TYPE> GetSubMatrix2() const { Matrix2<TYPE> m; GetSubMatrix2(m.data); return m; } //!< Returns the 2x2 portion of the matrix
void GetSubMatrix2( Matrix2<TYPE> &m ) const { GetSubMatrix2(m.data); } //!< Returns the 2x2 portion of the matrix
void GetSubMatrix2( TYPE *mdata ) const { CY_MEMCOPY(TYPE,mdata,data,2); CY_MEMCOPY(TYPE,mdata+2,data+3,2); } //!< Returns the 2x2 portion of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Comparison Operators
bool operator == ( const Matrix3 &right ) const { for ( int i=0; i<9; i++ ) if ( data[i] != right.data[i] ) return false; return true; } //!< compare equal
bool operator != ( const Matrix3 &right ) const { for ( int i=0; i<9; i++ ) if ( data[i] != right.data[i] ) return true; return false; } //!< compare not equal
//////////////////////////////////////////////////////////////////////////
//!@name Access Operators
TYPE& operator () ( int row, int column ) { return data[ column * 3 + row ]; } //!< subscript operator
const TYPE& operator () ( int row, int column ) const { return data[ column * 3 + row ]; } //!< constant subscript operator
TYPE& operator [] ( int i ) { return data[i]; } //!< subscript operator
const TYPE& operator [] ( int i ) const { return data[i]; } //!< constant subscript operator
//////////////////////////////////////////////////////////////////////////
//!@name Unary and Binary Operators
// Unary operators
#ifdef CY_NONVECTORIZED_MATRIX3
Matrix3 operator - () const { Matrix3 r; for (int i=0; i<9; i++) r.data[i] = -data[i]; return r; } //!< negative matrix
#else
Matrix3 operator - () const { Matrix3 r; _CY_IVDEP_FOR (int i=0; i<8; i++) r.data[i] = -data[i]; r.data[8] = -data[8]; return r; } //!< negative matrix
#endif
// Binary operators
#ifdef CY_NONVECTORIZED_MATRIX3
Matrix3 operator * ( const TYPE &value ) const { Matrix3 r; for (int i=0; i<9; i++) r.data[i] = data[i] * value; return r; } //!< multiply matrix by a value
Matrix3 operator / ( const TYPE &value ) const { Matrix3 r; for (int i=0; i<9; i++) r.data[i] = data[i] / value; return r; } //!< divide matrix by a value;
Matrix3 operator + ( const Matrix3 &right ) const { Matrix3 r; for (int i=0; i<9; i++) r.data[i] = data[i] + right.data[i]; return r; } //!< add two Matrices
Matrix3 operator - ( const Matrix3 &right ) const { Matrix3 r; for (int i=0; i<9; i++) r.data[i] = data[i] - right.data[i]; return r; } //!< subtract one Matrix3 from another
#else
Matrix3 operator * ( const TYPE &value ) const { Matrix3 r; _CY_IVDEP_FOR (int i=0; i<8; i++) r.data[i] = data[i] * value; r.data[8] = data[8] * value; return r; } //!< multiply matrix by a value
Matrix3 operator / ( const TYPE &value ) const { Matrix3 r; _CY_IVDEP_FOR (int i=0; i<8; i++) r.data[i] = data[i] / value; r.data[8] = data[8] / value; return r; } //!< divide matrix by a value;
Matrix3 operator + ( const Matrix3 &right ) const { Matrix3 r; _CY_IVDEP_FOR (int i=0; i<8; i++) r.data[i] = data[i] + right.data[i]; r.data[8] = data[8] + right.data[8]; return r; } //!< add two Matrices
Matrix3 operator - ( const Matrix3 &right ) const { Matrix3 r; _CY_IVDEP_FOR (int i=0; i<8; i++) r.data[i] = data[i] - right.data[i]; r.data[8] = data[8] - right.data[8]; return r; } //!< subtract one Matrix3 from another
#endif
Matrix3 operator * ( const Matrix3 &right ) const //!< multiply a matrix with another
{
Matrix3 r;
TYPE *rd = r.data;
for ( int i=0; i<9; i+=3, rd+=3 ) {
TYPE a[3], b[3], c[3];
for ( int j=0; j<3; ++j ) a [j] = data[ j] * right.data[i ];
for ( int j=0; j<3; ++j ) b [j] = data[3+j] * right.data[i+1];
for ( int j=0; j<3; ++j ) c [j] = data[6+j] * right.data[i+2];
for ( int j=0; j<3; ++j ) rd[j] = a[j] + b[j] + c[j];
}
return r;
}
Point3<TYPE> operator * ( const Point3<TYPE> &p ) const
{
return Point3<TYPE>( p.x*data[0] + p.y*data[3] + p.z*data[6],
p.x*data[1] + p.y*data[4] + p.z*data[7],
p.x*data[2] + p.y*data[5] + p.z*data[8] );
//TYPE a[3], b[3], c[3];
//Point3<TYPE> rr;
//for ( int i=0; i<3; ++i ) a [i] = p[0] * data[ i];
//for ( int i=0; i<3; ++i ) b [i] = p[1] * data[3+i];
//for ( int i=0; i<3; ++i ) c [i] = p[2] * data[6+i];
//for ( int i=0; i<3; ++i ) rr[i] = a[i] + b[i] + c[i];
//return rr;
}
//////////////////////////////////////////////////////////////////////////
//!@name Assignment Operators
const Matrix3& operator = ( const Matrix3 &right ) { CY_MEMCOPY(TYPE,data,right.data,9); return *this; } //!< assignment operator
const Matrix3& operator *= ( const Matrix3 &right ) { *this = operator*(right); return *this; } //!< multiply a matrix with another matrix and modify this matrix
#ifdef CY_NONVECTORIZED_MATRIX3
const Matrix3& operator += ( const Matrix3 &right ) { for (int i=0; i<9; i++) data[i] += right.data[i]; return *this; } //!< add two Matrices modify this
const Matrix3& operator -= ( const Matrix3 &right ) { for (int i=0; i<9; i++) data[i] -= right.data[i]; return *this; } //!< subtract one Matrix3 from another matrix and modify this matrix
const Matrix3& operator *= ( const TYPE &value ) { for (int i=0; i<9; i++) data[i] *= value; return *this; } //!< multiply a matrix with a value modify this matrix
const Matrix3& operator /= ( const TYPE &value ) { for (int i=0; i<9; i++) data[i] /= value; return *this; } //!< divide the matrix by a value modify the this matrix
#else
const Matrix3& operator += ( const Matrix3 &right ) { _CY_IVDEP_FOR (int i=0; i<8; i++) data[i] += right.data[i]; data[8] += right.data[8]; return *this; } //!< add two Matrices modify this
const Matrix3& operator -= ( const Matrix3 &right ) { _CY_IVDEP_FOR (int i=0; i<8; i++) data[i] -= right.data[i]; data[8] -= right.data[8]; return *this; } //!< subtract one Matrix3 from another matrix and modify this matrix
const Matrix3& operator *= ( const TYPE &value ) { _CY_IVDEP_FOR (int i=0; i<8; i++) data[i] *= value; data[8] *= value; return *this; } //!< multiply a matrix with a value modify this matrix
const Matrix3& operator /= ( const TYPE &value ) { _CY_IVDEP_FOR (int i=0; i<8; i++) data[i] /= value; data[8] /= value; return *this; } //!< divide the matrix by a value modify the this matrix
#endif
//////////////////////////////////////////////////////////////////////////
//!@name Other Methods
void Transpose() //!< Transpose this matrix
{
for (int i = 1; i < 3; i++) {
for (int j = 0; j < i; j++) {
TYPE temp = data[i * 3 + j];
data[i * 3 + j] = data[j * 3 + i];
data[j * 3 + i] = temp;
}
}
}
void GetTranspose( Matrix3 &m ) const //!< return Transpose of this matrix
{
m.data[0] = data[0]; m.data[1] = data[3]; m.data[2] = data[6];
m.data[3] = data[1]; m.data[4] = data[4]; m.data[5] = data[7];
m.data[6] = data[2]; m.data[7] = data[5]; m.data[8] = data[8];
}
Matrix3 GetTranspose() const { Matrix3 t; GetTranspose(t); return t; } //!< return Transpose of this matrix
//! Multiply the give vector with the transpose of the matrix
Point3<TYPE> TransposeMult( const Point3<TYPE> &p ) const
{
return Point3<TYPE>( p.x*data[0] + p.y*data[1] + p.z*data[2],
p.x*data[3] + p.y*data[4] + p.z*data[5],
p.x*data[6] + p.y*data[7] + p.z*data[8] );
}
TYPE GetDeterminant() const { //!< Get the determinant of this matrix
// 0 (4 8 - 5 7) + 1 (5 6 - 3 8) + 2 (3 7 - 4 6)
return data[0] * ( data[4] * data[8] - data[5] * data[7] ) +
data[1] * ( data[5] * data[6] - data[3] * data[8] ) +
data[2] * ( data[3] * data[7] - data[4] * data[6] );
}
void Invert() { Matrix3 inv; GetInverse(inv); *this=inv; } //!< Invert this matrix
void GetInverse( Matrix3 &inverse ) const //!< Get the inverse of this matrix
{
// ( 4 8 - 5 7 5 6 - 3 8 3 7 - 4 6 )
// ( 2 7 - 1 8 0 8 - 2 6 1 6 - 0 7 ) / det
// ( 1 5 - 2 4 2 3 - 0 5 0 4 - 1 3 )
inverse.data[0] = (data[4]*data[8] - data[5]*data[7]);
inverse.data[1] = (data[2]*data[7] - data[1]*data[8]);
inverse.data[2] = (data[1]*data[5] - data[2]*data[4]);
inverse.data[3] = (data[5]*data[6] - data[3]*data[8]);
inverse.data[4] = (data[0]*data[8] - data[2]*data[6]);
inverse.data[5] = (data[2]*data[3] - data[0]*data[5]);
inverse.data[6] = (data[3]*data[7] - data[4]*data[6]);
inverse.data[7] = (data[1]*data[6] - data[0]*data[7]);
inverse.data[8] = (data[0]*data[4] - data[1]*data[3]);
TYPE det = data[0] * inverse.data[0] + data[1] * inverse.data[3] + data[2] * inverse.data[6];
inverse /= det;
}
Matrix3 GetInverse() const { Matrix3 inv; GetInverse(inv); return inv; } //!< Get the inverse of this matrix
//! Orthogonalizes the matrix and removes the scale component, preserving the x direction
void OrthogonalizeX()
{
Point3<TYPE> *p = (Point3<TYPE>*)data;
p[0].Normalize();
p[1] -= p[0] * (p[1]%p[0]);
p[1].Normalize();
p[2] -= p[0] * (p[2]%p[0]);
p[2] -= p[1] * (p[2]%p[1]);
p[2].Normalize();
}
//! Orthogonalizes the matrix and removes the scale component, preserving the y direction
void OrthogonalizeY()
{
Point3<TYPE> *p = (Point3<TYPE>*)data;
p[1].Normalize();
p[0] -= p[1] * (p[0]%p[1]);
p[0].Normalize();
p[2] -= p[1] * (p[2]%p[1]);
p[2] -= p[0] * (p[2]%p[0]);
p[2].Normalize();
}
//! Orthogonalizes the matrix and removes the scale component, preserving the z direction
void OrthogonalizeZ()
{
Point3<TYPE> *p = (Point3<TYPE>*)data;
p[2].Normalize();
p[0] -= p[2] * (p[0]%p[2]);
p[0].Normalize();
p[1] -= p[2] * (p[1]%p[2]);
p[1] -= p[0] * (p[1]%p[0]);
p[1].Normalize();
}
//! Returns if the matrix is identity within the given error tollerance.
bool IsIdentity( TYPE tollerance=TYPE(0.001) ) const
{
return cyAbs(data[0]-TYPE(1)) < tollerance && cyAbs(data[1]) < tollerance && cyAbs(data[2]) < tollerance &&
cyAbs(data[3]) < tollerance && cyAbs(data[4]-TYPE(1)) < tollerance && cyAbs(data[5]) < tollerance &&
cyAbs(data[6]) < tollerance && cyAbs(data[7]) < tollerance && cyAbs(data[8]-TYPE(1)) < tollerance;
}
//! Returns if the matrix is symmetric within the given error tollerance.
bool IsSymmetric( TYPE tollerance=TYPE(0.001) ) const { return cyAbs(data[1] - data[3]) < tollerance && cyAbs(data[2] - data[6]) < tollerance && cyAbs(data[5] - data[7]) < tollerance; }
//////////////////////////////////////////////////////////////////////////
//!@name Static Methods
//! Returns an identity matrix
static Matrix3 MatrixIdentity() { Matrix3 m; m.SetIdentity(); return m; }
//! Returns a view matrix using position, target and approximate up vector
static Matrix3 MatrixView( const Point3<TYPE> &target, Point3<TYPE> &up ) { Matrix3 m; m.SetView(target,up); return m; }
//! Returns a matrix using normal, and approximate x direction
static Matrix3 MatrixNormal( const Point3<TYPE> &normal, Point3<TYPE> &dir ) { Matrix3 m; m.SetNormal(normal,dir); return m; }
//! Returns a rotation matrix around x axis by angle in radians
static Matrix3 MatrixRotationX( TYPE angle ) { Matrix3 m; m.SetRotationX(angle); return m; }
//! Returns a rotation matrix around y axis by angle in radians
static Matrix3 MatrixRotationY( TYPE angle ) { Matrix3 m; m.SetRotationY(angle); return m; }
//! Returns a rotation matrix around z axis by angle in radians
static Matrix3 MatrixRotationZ( TYPE angle ) { Matrix3 m; m.SetRotationZ(angle); return m; }
//! Returns a rotation matrix around x, y, and then z axes by angle in radians (Rz * Ry * Rx)
static Matrix3 MatrixRotationXYZ( TYPE angleX, TYPE angleY, TYPE angleZ ) { Matrix3 m; m.SetRotationXYZ(angleX,angleY,angleZ); return m; }
//! Returns a rotation matrix around z, y, and then x axes by angle in radians (Rx * Ry * Rz)
static Matrix3 MatrixRotationZYX( TYPE angleX, TYPE angleY, TYPE angleZ ) { Matrix3 m; m.SetRotationZYX(angleX,angleY,angleZ); return m; }
//! Returns a rotation matrix about the given axis by angle in radians
static Matrix3 MatrixRotation( const Point3<TYPE> &axis, TYPE angle ) { Matrix3 m; m.SetRotation(axis,angle); return m; }
//! Returns a rotation matrix that sets [from] unit vector to [to] unit vector
static Matrix3 MatrixRotation( const Point3<TYPE> &from, const Point3<TYPE> &to ) { Matrix3 m; m.SetRotation(from,to); return m; }
//! Returns a uniform scale matrix
static Matrix3 MatrixScale( const TYPE &uniformScale ) { Matrix3 m; m.SetScale(uniformScale); return m; }
//! Returns a scale matrix
static Matrix3 MatrixScale( const TYPE &scaleX, const TYPE &scaleY, const TYPE &scaleZ ) { Matrix3 m; m.SetScale(scaleX,scaleY,scaleZ); return m; }
//! Returns a scale matrix
static Matrix3 MatrixScale( const Point3<TYPE> &scale ) { Matrix3 m; m.SetScale(scale); return m; }
//! Returns the matrix representation of cross product ( a x b )
static Matrix3 MatrixCrossProd( const Point3<TYPE> &a ) { Matrix3 m; m.SetCrossProd(a); return m; }
//////////////////////////////////////////////////////////////////////////
};
//-------------------------------------------------------------------------------
//! 3x4 matrix class.
//!
//! Its data stores 12-value array of column-major matrix elements.
//! I chose column-major format to be compatible with OpenGL
//! You can use Matrix34 with Point3<TYPE> and Point4<TYPE>
//! to transform 3D and 4D points.
template <typename TYPE>
class Matrix34
{
friend Matrix34 operator * ( const TYPE value, const Matrix34 &right ) { Matrix34 r; for (int i=0; i<12; i++) r.data[i] = value * right.data[i]; return r; } //!< multiply matrix by a value
friend Matrix34 Inverse( const Matrix34 &m ) { return m.GetInverse(); } //!< return the inverse of the matrix
public:
//! Elements of the matrix are column-major: \n
//! | 0 3 6 9 | \n
//! | 1 4 7 10 | \n
//! | 2 5 8 11 | \n
TYPE data[12];
//////////////////////////////////////////////////////////////////////////
//!@name Constructors
Matrix34() {} //!< Default constructor
Matrix34( const Matrix34 &matrix ) { CY_MEMCOPY(TYPE,data,matrix.data,12); } //!< Copy constructor
template <typename T> explicit Matrix34<TYPE>( const Matrix34<T> &matrix ) { CY_MEMCONVERT(TYPE,data,matrix.data,12); } //!< Copy constructor for different types
explicit Matrix34( const TYPE *values ) { Set(values); } //!< Initialize the matrix using an array of 9 values
explicit Matrix34( const TYPE &v ) { SetScaledIdentity(v); } //!< Initialize the matrix as identity scaled by v
explicit Matrix34( const Point3<TYPE> &x, const Point3<TYPE> &y, const Point3<TYPE> &z, const Point3<TYPE> &pos ) { Set(x,y,z,pos); } //!< Initialize the matrix using x,y,z vectors and coordinate center
explicit Matrix34( const Point3<TYPE> &pos, const Point3<TYPE> &normal, const Point3<TYPE> &dir ) { Set(pos,normal,dir); } //!< Initialize the matrix using position, normal, and approximate x direction
explicit Matrix34( const Matrix3<TYPE> &m ) { CY_MEMCOPY(TYPE,data,m.data,9); CY_MEMCLEAR(TYPE,data+9,3); }
explicit Matrix34( const Matrix3<TYPE> &m, const Point3<TYPE> &pos ) { CY_MEMCOPY(TYPE,data,m.data,9); data[9]=pos.x; data[10]=pos.y; data[11]=pos.z; }
explicit Matrix34( const Matrix2<TYPE> &m ) {
data[ 0] = m.data[ 0]; data[ 1] = m.data[ 1]; data[ 2] = TYPE(0);
data[ 3] = m.data[ 2]; data[ 4] = m.data[ 3]; data[ 5] = TYPE(0);
data[ 6] = TYPE(0); data[ 7] = TYPE(0); data[ 8] = TYPE(1);
CY_MEMCLEAR(TYPE,data+9,3);
}
explicit Matrix34( const Matrix4<TYPE> &m );
//! Constructor using row-major order for initialization
Matrix34( const TYPE &row0col0, const TYPE &row0col1, const TYPE &row0col2, const TYPE &row0col3,
const TYPE &row1col0, const TYPE &row1col1, const TYPE &row1col2, const TYPE &row1col3,
const TYPE &row2col0, const TYPE &row2col1, const TYPE &row2col2, const TYPE &row2col3 )
{
data[ 0] = row0col0; data[ 3] = row0col1; data[ 6] = row0col2; data[ 9] = row0col3;
data[ 1] = row1col0; data[ 4] = row1col1; data[ 7] = row1col2; data[10] = row1col3;
data[ 2] = row2col0; data[ 5] = row2col1; data[ 8] = row2col2; data[11] = row2col3;
}
//////////////////////////////////////////////////////////////////////////
//!@name Set & Get Methods
//! Set all the values as zero
void Zero() { CY_MEMCLEAR(TYPE,data,12); }
//! Returns true if the matrix is exactly zero
bool IsZero() const { for ( int i=0; i<12; i++ ) if ( data[i] != 0 ) return false; return true; }
//! Copies the matrix data to the given values array of size 12
void Get( TYPE *values ) { CY_MEMCOPY(TYPE,values,data,12); }
//! Set Matrix using an array of 12 values
void Set( const TYPE *values ) { CY_MEMCOPY(TYPE,data,values,12); }
//! Set matrix using x,y,z vectors and coordinate center
void Set( const Point3<TYPE> &x, const Point3<TYPE> &y, const Point3<TYPE> &z, const Point3<TYPE> &pos ) { x.Get(data); y.Get(data+3); z.Get(data+6); pos.Get(data+9); }
//! Set matrix using position, normal, and approximate x direction
void Set( const Point3<TYPE> &pos, const Point3<TYPE> &normal, const Point3<TYPE> &dir ) { Point3<TYPE> y=normal.Cross(dir); y.Normalize(); Point3<TYPE> newdir=y.Cross(normal); Set(newdir,y,normal,pos); }
//! Converts the matrix to an identity matrix
void SetIdentity() { SetScaledIdentity(TYPE(1)); }
//! Converts the matrix to an identity matrix scaled by a scalar
void SetScaledIdentity( TYPE v ) { SetScale(v); }
//////////////////////////////////////////////////////////////////////////
//!@name Affine transformations
//! Sets a uniform scale matrix
void SetScale( const TYPE &uniformScale ) { SetScale(uniformScale,uniformScale,uniformScale); }
//! Sets a scale matrix
void SetScale( const TYPE &scaleX, const TYPE &scaleY, const TYPE &scaleZ )
{
data[ 0] = scaleX; data[ 1] = 0; data[ 2]=0;
data[ 3] = 0; data[ 4] = scaleY; data[ 5]=0;
data[ 6] = 0; data[ 7] = 0; data[ 8]=scaleZ;
data[ 9] = 0; data[10] = 0; data[11]=0;
}
//! Sets a scale matrix
void SetScale( const Point3<TYPE> &scale ) { SetScale(scale.x,scale.y,scale.z); }
//! Removes the scale component of the matrix
void SetNoScale() { Point3<TYPE> *p = (Point3<TYPE>*)data; p[0].Normalize(); p[1].Normalize(); p[2].Normalize(); }
//! Set as rotation matrix around x axis
void SetRotationX( TYPE angle ) { SetRotationX( cySin(angle), cyCos(angle) ); }
//! Set as rotation matrix around x axis by cos and sin of angle
void SetRotationX( TYPE sinAngle, TYPE cosAngle )
{
data[0] = TYPE(1); data[1] = TYPE(0); data[2] = TYPE(0);
data[3] = TYPE(0); data[4] = cosAngle; data[5] = sinAngle;
data[6] = TYPE(0); data[7] = -sinAngle; data[8] = cosAngle;
CY_MEMCLEAR(TYPE,data+9,3);
}
//! Set as rotation matrix around y axis
void SetRotationY( TYPE angle ) { SetRotationY( cySin(angle), cyCos(angle) ); }
//! Set as rotation matrix around y axis by cos and sin of angle
void SetRotationY( TYPE sinAngle, TYPE cosAngle )
{
data[0] = cosAngle; data[1] = TYPE(0); data[2] = -sinAngle;
data[3] = TYPE(0); data[4] = TYPE(1); data[5] = TYPE(0);
data[6] = sinAngle; data[7] = TYPE(0); data[8] = cosAngle;
CY_MEMCLEAR(TYPE,data+9,3);
}
//! Set as rotation matrix around z axis
void SetRotationZ( TYPE angle ) { SetRotationZ( cySin(angle), cyCos(angle) ); }
//! Set as rotation matrix around z axis by cos and sin of angle
void SetRotationZ( TYPE sinAngle, TYPE cosAngle )
{
data[0] = cosAngle; data[1] = sinAngle; data[2] = TYPE(0);
data[3] = -sinAngle; data[4] = cosAngle; data[5] = TYPE(0);
data[6] = TYPE(0); data[7] = TYPE(0); data[8] = TYPE(1);
CY_MEMCLEAR(TYPE,data+9,3);
}
//! Set as rotation matrix around x, y, and then z axes ( Rz * Ry * Rx )
void SetRotationXYZ( TYPE angleX, TYPE angleY, TYPE angleZ )
{
const TYPE sx = cySin(angleX);
const TYPE cx = cyCos(angleX);
const TYPE sy = cySin(angleY);
const TYPE cy = cyCos(angleY);
const TYPE sz = cySin(angleZ);
const TYPE cz = cyCos(angleZ);
data[0] = cy*cz; data[1] = cy*sz; data[2] =-sy;
data[3] = cz*sx*sy - cx*sz; data[4] = cx*cz + sx*sy*sz; data[5] = cy*sx;
data[6] = cx*cz*sy + sx*sz; data[7] =-cz*sx + cx*sy*sz; data[8] = cx*cy;
CY_MEMCLEAR(TYPE,data+9,3);
}
//! Set as rotation matrix around z, y, and then x axes ( Rx * Ry * Rz )
void SetRotationZYX( TYPE angleX, TYPE angleY, TYPE angleZ )
{
const TYPE sx = cySin(angleX);
const TYPE cx = cyCos(angleX);
const TYPE sy = cySin(angleY);
const TYPE cy = cyCos(angleY);
const TYPE sz = cySin(angleZ);
const TYPE cz = cyCos(angleZ);
data[0] = cy*cz; data[1] = cx*sz + sx*sy*cz; data[2] = sx*sz - cx*sy*cz;
data[3] =-cy*sz; data[4] = cx*cz - sx*sy*sz; data[5] = sx*cz + cx*sy*sz;
data[6] = sy; data[7] =-sx*cy; data[8] = cx*cy;
CY_MEMCLEAR(TYPE,data+9,3);
}
//! Set a rotation matrix about the given axis by angle
void SetRotation( const Point3<TYPE> &axis, TYPE angle ) { SetRotation(axis,cySin(angle),cyCos(angle)); }
//! Set a rotation matrix about the given axis by cos and sin of angle
void SetRotation( const Point3<TYPE> &axis, TYPE sinAngle, TYPE cosAngle )
{
const TYPE t = TYPE(1) - cosAngle;
const TYPE tx = t * axis.x;
const TYPE ty = t * axis.y;
const TYPE tz = t * axis.z;
const TYPE txy = tx * axis.y;
const TYPE txz = tx * axis.z;
const TYPE tyz = ty * axis.z;
const TYPE sx = sinAngle * axis.x;
const TYPE sy = sinAngle * axis.y;
const TYPE sz = sinAngle * axis.z;
data[ 0] = tx * axis.x + cosAngle; data[ 1] = txy + sz; data[ 2] = txz - sy;
data[ 3] = txy - sz; data[ 4] = ty * axis.y + cosAngle; data[ 5] = tyz + sx;
data[ 6] = txz + sy; data[ 7] = tyz - sx; data[ 8] = tz * axis.z + cosAngle;
CY_MEMCLEAR(TYPE,data+9,3);
}
//! Set a rotation matrix that sets [from] unit vector to [to] unit vector
void SetRotation( const Point3<TYPE> &from, const Point3<TYPE> &to )
{
TYPE c = from.Dot(to);
if ( c > TYPE(0.9999999) ) SetIdentity();
else {
TYPE s = cySqrt(TYPE(1) - c*c);
Point3<TYPE> axis = from.Cross(to).GetNormalized();
SetRotation(axis, s, c);
}
}
//! Sets a translation matrix with no rotation or scale
void SetTrans( const Point3<TYPE> &move ) { TYPE d[12]={1,0,0, 0,1,0, 0,0,1 }; CY_MEMCOPY(TYPE,data,d,9); data[9]=move.x; data[10]=move.y; data[11]=move.z; }
//! Adds a translation to the matrix
void AddTrans( const Point3<TYPE> &move ) { data[9]+=move.x; data[10]+=move.y; data[11]+=move.z; }
//! Sets the translation component of the matrix
void SetTransComponent( const Point3<TYPE> &move ) { data[9]=move.x; data[10]=move.y; data[11]=move.z; }
//! Set view matrix using position, target and approximate up vector
void SetView( const Point3<TYPE> &pos, const Point3<TYPE> &target, const Point3<TYPE> &up )
{
Point3<TYPE> f = target - pos;
f.Normalize();
Point3<TYPE> s = f.Cross(up);
s.Normalize();
Point3<TYPE> u = s.Cross(f);
data[ 0]=s.x; data[ 1]=u.x; data[ 2]=-f.x;
data[ 3]=s.y; data[ 4]=u.y; data[ 5]=-f.y;
data[ 6]=s.z; data[ 7]=u.z; data[ 8]=-f.z;
data[ 9]= -s % pos;
data[10]= -u % pos;
data[11]= f % pos;
}
//! Set matrix using normal and approximate x direction
void SetNormal(const Point3<TYPE> &normal, const Point3<TYPE> &dir ) { Point3<TYPE> y=normal.Cross(dir); y.Normalize(); Point3<TYPE> newdir=y.Cross(normal); Set(newdir,y,normal,Point3<TYPE>(TYPE(0),TYPE(0),TYPE(0))); }
//////////////////////////////////////////////////////////////////////////
//!@name Set Row, Column, or Diagonal
void SetRow( int row, TYPE x, TYPE y, TYPE z, TYPE w ) { data[row]=x; data[row+3]=y; data[row+6]=z; data[row+9]=w; } //!< Sets a row of the matrix
void SetColumn( int column, TYPE x, TYPE y, TYPE z ) { data[3*column]=x; data[3*column+1]=y; data[3*column+2]=z; } //!< Sets a column of the matrix
void SetDiagonal( const TYPE &xx, const TYPE &yy, const TYPE &zz ) { data[0]=xx; data[4]=yy; data[8]=zz; } //!< Sets the diagonal values of the matrix
void SetDiagonal( const Point3<TYPE> &p ) { SetDiagonal( p.x, p.y, p.z ); } //!< Sets the diagonal values of the matrix
void SetDiagonal( const TYPE *values ) { SetDiagonal(values[0],values[1],values[2]); } //!< Sets the diagonal values of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Get Row, Column, or Diagonal
Point4<TYPE> GetRow ( int row ) const { return Point4<TYPE>( data[row], data[row+3], data[row+6], data[row+9] ); } //!< Returns a row of the matrix
void GetRow ( int row, Point4<TYPE> &p ) const { p.Set( data[row], data[row+3], data[row+6], data[row+9] ); } //!< Returns a row of the matrix
void GetRow ( int row, TYPE *values ) const { values[0]=data[row]; values[1]=data[row+3]; values[2]=data[row+6]; values[3]=data[row+9]; } //!< Returns a row of the matrix
Point3<TYPE> GetColumn( int col ) const { return Point3<TYPE>( &data[col*3] ); } //!< Returns a column of the matrix
void GetColumn( int col, Point3<TYPE> &p ) const { p.Set( &data[col*3] ); } //!< Returns a column of the matrix
void GetColumn( int col, TYPE *values ) const { values[0]=data[col*3]; values[1]=data[col*3+1]; values[2]=data[col*3+2]; } //!< Returns a column of the matrix
Point3<TYPE> GetDiagonal() const { Point3<TYPE> r; GetDiagonal(r); return r; } //!< Returns the diagonal of the matrix
void GetDiagonal( Point3<TYPE> &p ) const { GetDiagonal(&p.x); } //!< Returns the diagonal of the matrix
void GetDiagonal( TYPE *values ) const { values[0]=data[0]; values[1]=data[4]; values[2]=data[8]; } //!< Returns the diagonal of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Get Sub-matrix data
void GetSubMatrix ( Matrix3<TYPE> &m ) const { GetSubMatrix3(m); } //!< Returns the 3x3 portion of the matrix
void GetSubMatrix ( Matrix2<TYPE> &m ) const { GetSubMatrix2(m); } //!< Returns the 2x2 portion of the matrix
Matrix3<TYPE> GetSubMatrix3() const { Matrix3<TYPE> m; GetSubMatrix3(m.data); return m; } //!< Returns the 3x3 portion of the matrix
void GetSubMatrix3( Matrix3<TYPE> &m ) const { GetSubMatrix3(m.data); } //!< Returns the 3x3 portion of the matrix
void GetSubMatrix3( TYPE *mdata ) const { CY_MEMCOPY(TYPE,mdata,data,9); } //!< Returns the 3x3 portion of the matrix
Matrix2<TYPE> GetSubMatrix2() const { Matrix2<TYPE> m; GetSubMatrix2(m.data); return m; } //!< Returns the 2x2 portion of the matrix
void GetSubMatrix2( Matrix2<TYPE> &m ) const { GetSubMatrix2(m.data); } //!< Returns the 2x2 portion of the matrix
void GetSubMatrix2( TYPE *mdata ) const { CY_MEMCOPY(TYPE,mdata,data,2); CY_MEMCOPY(TYPE,mdata+2,data+3,2); } //!< Returns the 2x2 portion of the matrix
Point3<TYPE> GetTrans() const { Point3<TYPE> p; GetTrans(p); return p; } //! Returns the translation component of the matrix
void GetTrans( Point3<TYPE> &p ) const { p.x=data[9]; p.y=data[10]; p.z=data[11]; } //! Returns the translation component of the matrix
void GetTrans( TYPE *trans ) const { CY_MEMCOPY(TYPE,trans,data+9,3); } //! Returns the translation component of the matrix
//////////////////////////////////////////////////////////////////////////
//!@name Comparison Operators
bool operator == ( const Matrix34 &right ) const { for ( int i=0; i<12; i++ ) if ( data[i] != right.data[i] ) return false; return true; } //!< compare equal
bool operator != ( const Matrix34 &right ) const { for ( int i=0; i<12; i++ ) if ( data[i] != right.data[i] ) return true; return false; } //!< compare not equal
//////////////////////////////////////////////////////////////////////////
//!@name Access Operators
TYPE& operator () ( int row, int column ) { return data[ column * 3 + row ]; } //!< subscript operator
const TYPE& operator () ( int row, int column ) const { return data[ column * 3 + row ]; } //!< constant subscript operator
TYPE& operator [] ( int i ) { return data[i]; } //!< subscript operator
const TYPE& operator [] ( int i ) const { return data[i]; } //!< constant subscript operator
//////////////////////////////////////////////////////////////////////////
//!@name Unary and Binary Operators