-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix3_.h
More file actions
778 lines (676 loc) · 22.3 KB
/
Matrix3_.h
File metadata and controls
778 lines (676 loc) · 22.3 KB
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
#ifndef MATRIX3__H
#define MATRIX3__H
#include "VmUtil.h"
#include "Point3.h"
#include "Vector3.h"
VM_BEGIN_NS
template<class T> class Quat4;
template<class T> class AxisAngle4;
/**
* A 3 x 3 matrix.
* Primarily to support rotations
* @version specification 1.1, implementation $Revision: 1.3 $, $Date: 1999/10/06 02:52:46 $
* @author Kenji hiranabe
*/
template<class T>
class Matrix3 {
protected:
static T abs(T t) { return VmUtil<T>::abs(t); }
static T sin(T t) { return VmUtil<T>::sin(t); }
static T cos(T t) { return VmUtil<T>::cos(t); }
/*
* $Log: Matrix3_.h,v $
* Revision 1.3 1999/10/06 02:52:46 hiranabe
* Java3D 1.2 and namespace
*
* Revision 1.2 1999/05/26 00:59:37 hiranabe
* support Visual C++
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
*/
public:
/**
* the type for values
*/
typedef T value_type;
/**
* the type for index
*/
typedef size_t size_type;
/**
* dimension
*/
enum { DIMENSION = 3 };
/**
* the type for tuple
*/
typedef Tuple3<T> tuple_type;
/**
* the type for vector
*/
typedef Vector3<T> vector_type;
/**
* the type for point
*/
typedef Point3<T> point_type;
/**
* The first element of the first row.
*/
T m00;
/**
* The second element of the first row.
*/
T m01;
/**
* third element of the first row.
*/
T m02;
/**
* The first element of the second row.
*/
T m10;
/**
* The second element of the second row.
*/
T m11;
/**
* The third element of the second row.
*/
T m12;
/**
* The first element of the third row.
*/
T m20;
/**
* The second element of the third row.
*/
T m21;
/**
* The third element of the third row.
*/
T m22;
/**
* Constrcts and initializes a Matrix3 from the specified nine values.
* @param m00 the [0][0] element
* @param m01 the [0][1] element
* @param m02 the [0][2] element
* @param m10 the [1][0] element
* @param m11 the [1][1] element
* @param m12 the [1][2] element
* @param m20 the [2][0] element
* @param m21 the [2][1] element
* @param m22 the [2][2] element
*/
Matrix3(T m00, T m01, T m02,
T m10, T m11, T m12,
T m20, T m21, T m22);
/**
* Constructs and initializes a Matrix3 from the specified 9
* element array. this.m00 =v[0], this.m01=v[1], etc.
* @param v the array of length 9 containing in order
*/
Matrix3(const T v[]);
/**
* Constructs and initializes a Matrix3 from the specified 3x3
* element array. this.m00 =m[0][0], this.m01=m[0][1], etc.
* @param m the array of 3 x 3 containing in order
*/
#ifdef VM_INCLUDE_CONVERSION_FROM_2DARRAY
Matrix3(const T m[][3]);
#endif
#if 0
/**
* Constructs a new matrix with the same values as the Matrix3f parameter.
* @param m1 The source matrix.
*/
Matrix3(Matrix3f m1):
m00(m1.m00), m01(m1.m01), m02(m1.m02),
m10(m1.m10), m11(m1.m11), m12(m1.m12),
m20(m1.m20), m21(m1.m21), m22(m1.m22) { }
#endif
/**
* Constructs and initializes a Matrix3 to all zeros.
*/
Matrix3();
/**
* Sets 9 values
* @param m00 the [0][0] element
* @param m01 the [0][1] element
* @param m02 the [0][2] element
* @param m10 the [1][0] element
* @param m11 the [1][1] element
* @param m12 the [1][2] element
* @param m20 the [2][0] element
* @param m21 the [2][1] element
* @param m22 the [2][2] element
*/
void set(T m00, T m01, T m02,
T m10, T m11, T m12,
T m20, T m21, T m22);
/**
* Sets the value of this matrix to the value of the Matrix3
* argument.
* @param m1 The source matrix.
*/
void set(const Matrix3& m1);
/**
* Sets the values in this Matrix3 equal to the row-major array parameter
* (ie, the first four elements of the array will be copied into the first
* row of this matrix, etc.).
* @param m the array of length 9 containing in order
*/
void set(const T m[]);
/**
* Sets the values in this Matrix3 equal to the row-major array parameter
* (ie, the first four elements of the array will be copied into the first
* row of this matrix, etc.).
* @param m the array of 3x3 containing in order (T m[3][3])
*/
#ifdef VM_INCLUDE_CONVERSION_FROM_2DARRAY
void set(const T m[][3]);
#endif
/**
* Sets this Matrix3 to identity.
*/
void setIdentity();
/**
* Sets the scale component of the current matrix by factoring out the
* current scale (by doing an SVD) from the rotational component and
* multiplying by the new scale.
* @param scale the new scale amount
*/
void setScale(T scale);
/**
* Sets the specified element of this matrix3d to the value provided.
* @param row the row number to be modified (zero indexed)
* @param column the column number to be modified (zero indexed)
* @param value the new value
*/
void setElement(size_type row, size_type column, T value);
/**
* Retrieves the value at the specified row and column of this matrix.
* @param row the row number to be retrieved (zero indexed)
* @param column the column number to be retrieved (zero indexed)
* @return the value at the indexed element
*/
T getElement(size_type row, size_type column) const;
/**
* Retrieves the lvalue at the specified row and column of this matrix.
* @param row the row number to be retrieved (zero indexed)
* @param column the column number to be retrieved (zero indexed)
* @return the lvalue at the indexed element
*/
T& getElementReference(size_type row, size_type column);
/**
* Sets the specified row of this matrix3d to the three values provided.
* @param row the row number to be modified (zero indexed)
* @param x the first column element
* @param y the second column element
* @param z the third column element
*/
void setRow(size_type row, T x, T y, T z);
/**
* Sets the specified row of this matrix3d to the Vector provided.
* @param row the row number to be modified (zero indexed)
* @param v the replacement row
*/
void setRow(size_type row, const Vector3<T>& v);
/**
* Sets the specified row of this matrix3 to the four values provided.
* @param row the row number to be modified (zero indexed)
* @param v the replacement row
*/
void setRow(size_type row, const T v[]);
/**
* Copies the matrix values in the specified row into the
* array parameter.
* @param row the matrix row
* @param v The array into which the matrix row values will be copied
*/
void getRow(size_type row, T v[]) const;
/**
* Copies the matrix values in the specified row into the
* vector parameter.
* @param row the matrix row
* @param v The vector into which the matrix row values will be copied
*/
void getRow(size_type row, Vector3<T>* v) const;
/**
* Sets the specified column of this matrix3 to the three values provided.
* @param column the column number to be modified (zero indexed)
* @param x the first row element
* @param y the second row element
* @param z the third row element
*/
void setColumn(size_type column, T x, T y, T z);
/**
* Sets the specified column of this matrix3d to the vector provided.
* @param column the column number to be modified (zero indexed)
* @param v the replacement column
*/
void setColumn(size_type column, const Vector3<T>& v);
/**
* Sets the specified column of this matrix3d to the four values provided.
* @param column the column number to be modified (zero indexed)
* @param v the replacement column
*/
void setColumn(size_type column, const T v[]);
/**
* Copies the matrix values in the specified column into the vector
* parameter.
* @param column the matrix column
* @param v The vector into which the matrix row values will be copied
*/
void getColumn(size_type column, Vector3<T>* v) const;
/**
* Copies the matrix values in the specified column into the array
* parameter.
* @param column the matrix column
* @param v The array into which the matrix row values will be copied
*/
void getColumn(size_type column, T v[]) const;
/**
* Performs an SVD normalization of this matrix to calculate and return the
* uniform scale factor. This matrix is not modified.
* @return the scale factor of this matrix
*/
T getScale() const;
/**
* Adds a scalar to each component of this matrix.
* @param scalar The scalar adder.
*/
void add(T scalar);
/**
* Substracts a scalar from each component of this matrix.
* @param scalar The scalar adder.
*/
void sub(T scalar);
/**
* Adds a scalar to each component of the matrix m1 and places
* the result into this. Matrix m1 is not modified.
* note this method is alias-safe.
* @param scalar The scalar adder.
* @parm m1 The original matrix values.
*/
void add(T scalar, const Matrix3& m1);
/**
* Sets the value of this matrix to the matrix sum of matrices m1 and m2.
* note this method is alias-safe.
* @param m1 the first matrix
* @param m2 the second matrix
*/
void add(const Matrix3& m1, const Matrix3& m2);
/**
* Sets the value of this matrix to sum of itself and matrix m1.
* @param m1 the other matrix
*/
void add(const Matrix3& m1);
/**
* Sets the value of this matrix to the matrix difference
* of matrices m1 and m2.
* note this method is alias-safe.
* @param m1 the first matrix
* @param m2 the second matrix
*/
void sub(const Matrix3& m1, const Matrix3& m2);
/**
* Sets the value of this matrix to the matrix difference of itself
* and matrix m1 (this = this - m1).
* @param m1 the other matrix
*/
void sub(const Matrix3& m1);
/**
* Sets the value of this matrix to its transpose.
*/
void transpose();
/**
* Sets the value of this matrix to the transpose of the argument matrix.
* note this method is alias-safe
* @param m1 the matrix to be transposed
*/
void transpose(const Matrix3& m1);
/**
* Sets the value of this matrix to the matrix conversion of the
* quaternion argument.
* @param q1 the quaternion to be converted
*/
void set(const Quat4<T>& q1); // moved to the implementation file
/**
* Sets the value of this matrix to the matrix conversion of the
* axis and angle argument.
* @param a1 the axis and angle to be converted
*/
void set(const AxisAngle4<T>& a1); // moved to the implementation file
#if 0
/**
* Sets the value of this matrix to the matrix conversion of the
* single precision quaternion argument.
* @param q1 the quaternion to be converted
*/
void set(Quat4f q1) {
setFromQuat(q1.x, q1.y, q1.z, q1.w);
}
/**
* Sets the value of this matrix to the matrix conversion of the
* single precision axis and angle argument.
* @param a1 the axis and angle to be converted
*/
void set(AxisAngle4f a1) {
setFromAxisAngle(a1.x, a1.y, a1.z, a1.angle);
}
/**
* Sets the value of this matrix to the double value of the Matrix3f
* argument.
* @param m1 the matrix3f to be converted to double
*/
void set(Matrix3f m1) {
m00 = m1.m00; m01 = m1.m01; m02 = m1.m02;
m10 = m1.m10; m11 = m1.m11; m12 = m1.m12;
m20 = m1.m20; m21 = m1.m21; m22 = m1.m22;
}
#endif
/**
* Sets the value of this matrix to the matrix inverse
* of the passed matrix m1.
* @param m1 the matrix to be inverted
*/
void invert(const Matrix3& m1);
/**
* Sets the value of this matrix to its inverse.
*/
void invert();
/**
* Computes the determinant of this matrix.
* @return the determinant of the matrix
*/
T determinant() const;
/**
* Sets the value of this matrix to a scale matrix with the
* passed scale amount.
* @param scale the scale factor for the matrix
*/
void set(T scale);
/**
* Sets the value of this matrix to a rotation matrix about the x axis
* by the passed angle.
* @param angle the angle to rotate about the X axis in radians
*/
void rotX(T angle);
/**
* Sets the value of this matrix to a rotation matrix about the y axis
* by the passed angle.
* @param angle the angle to rotate about the Y axis in radians
*/
void rotY(T angle);
/**
* Sets the value of this matrix to a rotation matrix about the z axis
* by the passed angle.
* @param angle the angle to rotate about the Z axis in radians
*/
void rotZ(T angle);
/**
* Multiplies each element of this matrix by a scalar.
* @param scalar The scalar multiplier.
*/
void mul(T scalar);
/**
* Multiplies each element of matrix m1 by a scalar and places the result
* into this. Matrix m1 is not modified.
* @param scalar The scalar multiplier.
* @param m1 The original matrix.
*/
void mul(T scalar, const Matrix3& m1);
/**
* Sets the value of this matrix to the result of multiplying itself
* with matrix m1.
* @param m1 the other matrix
*/
void mul(const Matrix3& m1);
/**
* Sets the value of this matrix to the result of multiplying
* the two argument matrices together.
* note this method is alias-safe.
* @param m1 the first matrix
* @param m2 the second matrix
*/
void mul(const Matrix3& m1, const Matrix3& m2);
/**
* Multiplies this matrix by matrix m1, does an SVD normalization of the
* result, and places the result back into this matrix this =
* SVDnorm(this*m1).
* @param m1 the matrix on the right hand side of the multiplication
*/
void mulNormalize(const Matrix3& m1) {
mul(m1);
SVD(this);
}
/**
* Multiplies matrix m1 by matrix m2, does an SVD normalization of the
* result, and places the result into this matrix this = SVDnorm(m1*m2).
* @param m1 the matrix on the left hand side of the multiplication
* @param m2 the matrix on the right hand side of the multiplication
*/
void mulNormalize(const Matrix3& m1, const Matrix3& m2) {
mul(m1, m2);
SVD(this);
}
/**
* Multiplies the transpose of matrix m1 times the transpose of matrix m2,
* and places the result into this.
* @param m1 The matrix on the left hand side of the multiplication
* @param m2 The matrix on the right hand side of the multiplication
*/
void mulTransposeBoth(const Matrix3& m1, const Matrix3& m2) {
mul(m2, m1);
transpose();
}
/**
* Multiplies matrix m1 times the transpose of matrix m2, and places the
* result into this.
* @param m1 The matrix on the left hand side of the multiplication
* @param m2 The matrix on the right hand side of the multiplication
*/
void mulTransposeRight(const Matrix3& m1, const Matrix3& m2);
/**
* Multiplies the transpose of matrix m1 times matrix m2, and places the
* result into this.
* @param m1 The matrix on the left hand side of the multiplication
* @param m2 The matrix on the right hand side of the multiplication
*/
void mulTransposeLeft(const Matrix3& m1, const Matrix3& m2);
/**
* Performs singular value decomposition normalization of this matrix.
*/
void normalize() {
SVD(this);
}
/**
* Perform singular value decomposition normalization of matrix m1 and
* place the normalized values into this.
* @param m1 Provides the matrix values to be normalized
*/
void normalize(const Matrix3& m1) {
set(m1);
SVD(this);
}
/**
* Perform cross product normalization of this matrix.
*/
void normalizeCP() {
T s = VmUtil<T>::pow(VmUtil<T>::abs(determinant()), T(-1.0/3.0));
mul(s);
}
/**
* Perform cross product normalization of matrix m1 and place the
* normalized values into this.
* @param m1 Provides the matrix values to be normalized
*/
void normalizeCP(const Matrix3& m1) {
set(m1);
normalizeCP();
}
/**
* Returns true if all of the data members of Matrix3 m1 are
* equal to the corresponding data members in this Matrix3.
* @param m1 The matrix with which the comparison is made.
* @return true or false
*/
bool equals(const Matrix3& m1) const;
/**
* Returns true if the L-infinite distance between this matrix and matrix
* m1 is less than or equal to the epsilon parameter, otherwise returns
* false. The L-infinite distance is equal to MAX[i=0,1,2,3 ; j=0,1,2,3 ;
* abs(this.m(i,j) - m1.m(i,j)]
* @param m1 The matrix to be compared to this matrix
* @param epsilon the threshold value
*/
bool epsilonEquals(const Matrix3& m1, T epsilon) const;
/**
* Sets this matrix to all zeros.
*/
void setZero();
/**
* Negates the value of this matrix: this = -this.
*/
void negate();
/**
* Sets the value of this matrix equal to the negation of of the Matrix3
* parameter.
* @param m1 The source matrix
*/
void negate(const Matrix3& m1) {
set(m1);
negate();
}
/**
* Transform the vector vec using this Matrix3 and place the
* result back into vec.
* @param t the vector to be transformed
*/
void transform(Tuple3<T>* t) const {
transform(*t, t);
}
/**
* Transform the vector vec using this Matrix3 and place the
* result into vecOut.
* note this method is alias-safe
* @paramt the double precision vector to be transformed
* @param result the vector into which the transformed values are placed
*/
void transform(const Tuple3<T>& t, Tuple3<T>* result) const;
/**
* Returns a hash number based on the data values in this
* object. Two different Matrix3 objects with identical data values
* (ie, returns true for equals(Matrix3) ) will return the same hash
* number. Two objects with different data members may return the
* same hash value, although this is not likely.
* @return the integer hash value
*/
size_t hashCode() const {
return VmUtil<T>::hashCode(sizeof *this, this);
}
/**
* Returns a string that contains the values of this Matrix3.
* @return the String representation
*/
#ifdef VM_INCLUDE_TOSTRING
std::string toString() const;
#endif
protected:
/**
* Performs SVD on this matrix and gets scale and rotation.
* Rotation is placed into rot.
* @param rot the rotation factor. if null, ignored
* @return scale factor
*/
T SVD(Matrix3* rot) const;
/**
* Sets this from a Quat4 elements
*/
void setFromQuat(T x, T y, T z, T w);
/**
* Sets this from a Quat4 elements
*/
void setFromAxisAngle(T x, T y, T z, T angle);
public:
// copy constructor and operator = is made by complier
bool operator==(const Matrix3& m1) const {
return equals(m1);
}
#ifdef VM_INCLUDE_SUBSCRIPTION_OPERATOR
T operator()(size_t row, size_t col) const {
return getElement(row, col);
}
T& operator()(size_t row, size_t col) {
return getElementReference(row, col);
}
#endif
Matrix3& operator+=(const Matrix3& m1) {
add(m1);
return *this;
}
Matrix3& operator-=(const Matrix3& m1) {
sub(m1);
return *this;
}
Matrix3& operator*=(const Matrix3& m1) {
mul(m1);
return *this;
}
Matrix3& operator*=(T s) {
mul(s);
return *this;
}
Matrix3 operator+(const Matrix3& m1) const {
return (Matrix3(*this)).operator+=(m1);
}
Matrix3 operator-(const Matrix3& m1) const {
return (Matrix3(*this)).operator-=(m1);
}
Matrix3 operator*(const Matrix3& m1) const {
return (Matrix3(*this)).operator*=(m1);
}
Matrix3 operator*(T s) const {
return (Matrix3(*this)).operator*=(s);
}
};
template <class T>
inline
Matrix3<T> operator*(T s, const Matrix3<T>& m) {
return (Matrix3<T>(m)).operator*=(s);
}
template <class T>
inline
Matrix3<T> operator*(const Matrix3<T>& m1, const Matrix3<T>& m2) {
return (Matrix3<T>(m1)).operator*=(m2);
}
template <class T>
inline
Tuple3<T> operator*(const Matrix3<T>& m, const Tuple3<T>& t) {
Tuple3<T> out;
m.transform(t,&out);
return out;
}
template <class T>
inline
Vector3<T> operator*(const Matrix3<T>& m, const Vector3<T>& t) {
return operator*(m, (const Tuple3<T>&)t);
}
template <class T>
inline
Point3<T> operator*(const Matrix3<T>& m, const Point3<T>& t) {
return operator*(m, (const Tuple3<T>&)t);
}
#ifdef VM_INCLUDE_IO
template <class T>
std::ostream& operator<<(std::ostream& o, const VM_VECMATH_NS::Matrix3<T>& t1);
#endif
typedef Matrix3<double> Matrix3d;
typedef Matrix3<float> Matrix3f;
VM_END_NS
#endif /* MATRIX3__H */