TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Matrix.h
Go to the documentation of this file.
1 
9 #ifndef G3D_MATRIX_H
10 #define G3D_MATRIX_H
11 
12 #include "G3D/g3dmath.h"
13 #include "G3D/Vector3.h"
14 #include "G3D/Vector4.h"
15 #include "G3D/Matrix3.h"
16 #include "G3D/Matrix4.h"
17 #include "G3D/ReferenceCount.h"
18 
19 namespace G3D {
20 
56 class Matrix {
57 public:
65  typedef float T;
66 
69  static int debugNumCopyOps;
70 
73  static int debugNumAllocOps;
74 
75 private:
76 public:
77 
82  class Impl : public ReferenceCountedObject {
83  public:
84 
85  static void* operator new(size_t size) {
86  return System::malloc(size);
87  }
88 
89  static void operator delete(void* p) {
90  System::free(p);
91  }
92 
93  ~Impl();
94 
95  private:
96  friend class Matrix;
97 
99  T** elt;
100 
102  T* data;
103 
105  int R;
106 
108  int C;
109 
110  int dataSize;
111 
115  void setSize(int newRows, int newCols);
116 
117  inline Impl() : elt(NULL), data(NULL), R(0), C(0), dataSize(0) {}
118 
119  Impl(const Matrix3& M);
120 
121  Impl(const Matrix4& M);
122 
123  inline Impl(int r, int c) : elt(NULL), data(NULL), R(0), C(0), dataSize(0) {
124  setSize(r, c);
125  }
126 
127  Impl& operator=(const Impl& m);
128 
129  inline Impl(const Impl& B) : elt(NULL), data(NULL), R(0), C(0), dataSize(0) {
130  // Use the assignment operator
131  *this = B;
132  }
133 
134  void setZero();
135 
136  inline void set(int r, int c, T v) {
137  debugAssert(r < R);
138  debugAssert(c < C);
139  elt[r][c] = v;
140  }
141 
142  inline const T& get(int r, int c) const {
143  debugAssert(r < R);
144  debugAssert(c < C);
145  return elt[r][c];
146  }
147 
149  void mul(const Impl& B, Impl& out) const;
150 
152  void add(const Impl& B, Impl& out) const;
153 
155  void add(T B, Impl& out) const;
156 
158  void sub(const Impl& B, Impl& out) const;
159 
161  void sub(T B, Impl& out) const;
162 
164  void lsub(T B, Impl& out) const;
165 
167  void arrayMul(const Impl& B, Impl& out) const;
168 
170  void mul(T B, Impl& out) const;
171 
173  void arrayDiv(const Impl& B, Impl& out) const;
174 
176  void div(T B, Impl& out) const;
177 
178  void negate(Impl& out) const;
179 
181  void inverseViaAdjoint(Impl& out) const;
182 
185 
186  void adjoint(Impl& out) const;
187 
189  void cofactor(Impl& out) const;
190 
196  T cofactor(int r, int c) const;
197 
199  void transpose(Impl& out) const;
200 
201  T determinant() const;
202 
204  T determinant(int r, int c) const;
205 
206  void arrayLog(Impl& out) const;
207 
208  void arrayExp(Impl& out) const;
209 
210  void arraySqrt(Impl& out) const;
211 
212  void arrayCos(Impl& out) const;
213 
214  void arraySin(Impl& out) const;
215 
216  void swapRows(int r0, int r1);
217 
218  void swapAndNegateCols(int c0, int c1);
219 
220  void mulRow(int r, const T& v);
221 
222  void abs(Impl& out) const;
223 
225  void withoutRowAndCol(int excludeRow, int excludeCol, Impl& out) const;
226 
227  bool anyNonZero() const;
228 
229  bool allNonZero() const;
230 
231  void setRow(int r, const T* vals);
232 
233  void setCol(int c, const T* vals);
234  };
235 private:
236 
237  typedef shared_ptr<Impl> ImplRef;
238 
239  ImplRef impl;
240 
241  inline Matrix(ImplRef i) : impl(i) {}
242  inline Matrix(Impl* i) : impl(ImplRef(i)) {}
243 
245  class SortRank {
246  public:
247  T value;
248  int col;
249 
250  inline bool operator>(const SortRank& x) const {
251  return x.value > value;
252  }
253 
254  inline bool operator<(const SortRank& x) const {
255  return x.value < value;
256  }
257 
258  inline bool operator>=(const SortRank& x) const {
259  return x.value >= value;
260  }
261 
262  inline bool operator<=(const SortRank& x) const {
263  return x.value <= value;
264  }
265 
266  inline bool operator==(const SortRank& x) const {
267  return x.value == value;
268  }
269 
270  inline bool operator!=(const SortRank& x) const {
271  return x.value != value;
272  }
273  };
274 
275  Matrix vectorPseudoInverse() const;
279 
280  Matrix col2PseudoInverse(const Matrix& B) const;
281  Matrix col3PseudoInverse(const Matrix& B) const;
282  Matrix col4PseudoInverse(const Matrix& B) const;
283  Matrix row2PseudoInverse(const Matrix& B) const;
284  Matrix row3PseudoInverse(const Matrix& B) const;
285  Matrix row4PseudoInverse(const Matrix& B) const;
286 
287 public:
288 
289  Matrix() : impl(new Impl(0, 0)) {}
290 
291  Matrix(const Matrix3& M) : impl(new Impl(M)) {}
292 
293  Matrix(const Matrix4& M) : impl(new Impl(M)) {}
294 
295  template<class S>
296  static Matrix fromDiagonal(const Array<S>& d) {
297  Matrix D = zero(d.length(), d.length());
298  for (int i = 0; i < d.length(); ++i) {
299  D.set(i, i, d[i]);
300  }
301  return D;
302  }
303 
304  static Matrix fromDiagonal(const Matrix& d);
305 
307  Matrix(int R, int C) : impl(new Impl(R, C)) {
308  impl->setZero();
309  }
310 
312  static Matrix zero(int R, int C);
313 
315  static Matrix one(int R, int C);
316 
318  static Matrix identity(int N);
319 
321  static Matrix random(int R, int C);
322 
324  inline int rows() const {
325  return impl->R;
326  }
327 
329  inline int cols() const {
330  return impl->C;
331  }
332 
334  Matrix& operator*=(const T& B);
335 
337  Matrix& operator/=(const T& B);
338 
340  Matrix& operator+=(const T& B);
341 
343  Matrix& operator-=(const T& B);
344 
348  Matrix& operator*=(const Matrix& B);
349 
351  Matrix& operator+=(const Matrix& B);
352 
354  Matrix& operator-=(const Matrix& B);
355 
358  Matrix subMatrix(int r1, int r2, int c1, int c2) const;
359 
362  inline Matrix operator*(const Matrix& B) const {
363  Matrix C(impl->R, B.impl->C);
364  impl->mul(*B.impl, *C.impl);
365  return C;
366  }
367 
369  inline Matrix operator*(const T& B) const {
370  Matrix C(impl->R, impl->C);
371  impl->mul(B, *C.impl);
372  return C;
373  }
374 
376  inline Matrix operator+(const Matrix& B) const {
377  Matrix C(impl->R, impl->C);
378  impl->add(*B.impl, *C.impl);
379  return C;
380  }
381 
383  inline Matrix operator-(const Matrix& B) const {
384  Matrix C(impl->R, impl->C);
385  impl->sub(*B.impl, *C.impl);
386  return C;
387  }
388 
390  inline Matrix operator+(const T& v) const {
391  Matrix C(impl->R, impl->C);
392  impl->add(v, *C.impl);
393  return C;
394  }
395 
397  inline Matrix operator-(const T& v) const {
398  Matrix C(impl->R, impl->C);
399  impl->sub(v, *C.impl);
400  return C;
401  }
402 
403 
404  Matrix operator>(const T& scalar) const;
405 
406  Matrix operator<(const T& scalar) const;
407 
408  Matrix operator>=(const T& scalar) const;
409 
410  Matrix operator<=(const T& scalar) const;
411 
412  Matrix operator==(const T& scalar) const;
413 
414  Matrix operator!=(const T& scalar) const;
415 
417  inline Matrix lsub(const T& B) const {
418  Matrix C(impl->R, impl->C);
419  impl->lsub(B, *C.impl);
420  return C;
421  }
422 
423  inline Matrix arrayMul(const Matrix& B) const {
424  Matrix C(impl->R, impl->C);
425  impl->arrayMul(*B.impl, *C.impl);
426  return C;
427  }
428 
429  Matrix3 toMatrix3() const;
430 
431  Matrix4 toMatrix4() const;
432 
433  Vector2 toVector2() const;
434 
435  Vector3 toVector3() const;
436 
437  Vector4 toVector4() const;
438 
440  void arrayMulInPlace(const Matrix& B);
441 
443  void arrayDivInPlace(const Matrix& B);
444 
445  // Declares an array unary method and its explicit-argument counterpart
446 # define DECLARE_METHODS_1(method)\
447  inline Matrix method() const {\
448  Matrix C(impl->R, impl->C);\
449  impl->method(*C.impl);\
450  return C;\
451  }\
452  void method(Matrix& out) const;
453 
454 
456  DECLARE_METHODS_1(arrayLog)
457  DECLARE_METHODS_1(arrayExp)
458  DECLARE_METHODS_1(arraySqrt)
459  DECLARE_METHODS_1(arrayCos)
460  DECLARE_METHODS_1(arraySin)
462 
463 # undef DECLARE_METHODS_1
464 
465  inline Matrix operator-() const {
466  return negate();
467  }
468 
475  inline Matrix inverse() const {
476  Impl* A = new Impl(*impl);
478  return Matrix(A);
479  }
480 
481  inline T determinant() const {
482  return impl->determinant();
483  }
484 
488  inline Matrix transpose() const {
489  Impl* A = new Impl(cols(), rows());
490  impl->transpose(*A);
491  return Matrix(A);
492  }
493 
495  void transpose(Matrix& out) const;
496 
497  inline Matrix adjoint() const {
498  Impl* A = new Impl(cols(), rows());
499  impl->adjoint(*A);
500  return Matrix(A);
501  }
502 
512  Matrix pseudoInverse(float tolerance = -1) const;
513 
515  Matrix svdPseudoInverse(float tolerance = -1) const;
516 
522  Matrix trans = transpose();
523  return (trans * (*this)).inverse() * trans;
524  }
525 
536  void svd(Matrix& U, Array<T>& d, Matrix& V, bool sort = true) const;
537 
538  void set(int r, int c, T v);
539 
540  void setCol(int c, const Matrix& vec);
541 
542  void setRow(int r, const Matrix& vec);
543 
544  Matrix col(int c) const;
545 
546  Matrix row(int r) const;
547 
548  T get(int r, int c) const;
549 
550  Vector2int16 size() const {
551  return Vector2int16(rows(), cols());
552  }
553 
554  int numElements() const {
555  return rows() * cols();
556  }
557 
558  void swapRows(int r0, int r1);
559 
561  void swapAndNegateCols(int c0, int c1);
562 
563  void mulRow(int r, const T& v);
564 
566  bool anyNonZero() const;
567 
569  bool allNonZero() const;
570 
571  inline bool allZero() const {
572  return !anyNonZero();
573  }
574 
575  inline bool anyZero() const {
576  return !allNonZero();
577  }
578 
580  void serialize(TextOutput& t) const;
581 
582  std::string toString(const std::string& name) const;
583 
584  std::string toString() const {
585  static const std::string name = "";
586  return toString(name);
587  }
588 
590  double normSquared() const;
591 
593  double norm() const;
594 
614  static const char* svdCore(float** U, int rows, int cols, float* D, float** V);
615 
616 };
617 
618 }
619 
620 inline G3D::Matrix operator-(const G3D::Matrix::T& v, const G3D::Matrix& M) {
621  return M.lsub(v);
622 }
623 
624 inline G3D::Matrix operator*(const G3D::Matrix::T& v, const G3D::Matrix& M) {
625  return M * v;
626 }
627 
628 inline G3D::Matrix operator+(const G3D::Matrix::T& v, const G3D::Matrix& M) {
629  return M + v;
630 }
631 
632 inline G3D::Matrix abs(const G3D::Matrix& M) {
633  return M.abs();
634 }
635 
636 #endif
637 
Matrix operator>(const T &scalar) const
Matrix(int R, int C)
Definition: Matrix.h:307
G3D::Matrix abs(const G3D::Matrix &M)
Definition: Matrix.h:632
bool allNonZero() const
Definition: Matrix.cpp:420
Definition: Vector2.h:40
Matrix operator==(const T &scalar) const
Matrix(Impl *i)
Definition: Matrix.h:242
Matrix & operator/=(const T &B)
Definition: Matrix.cpp:107
static const char * svdCore(float **U, int rows, int cols, float *D, float **V)
Definition: Matrix.cpp:1505
Matrix vectorPseudoInverse() const
Definition: Matrix.cpp:1012
Matrix(ImplRef i)
Definition: Matrix.h:241
void setCol(int c, const T *vals)
Definition: Matrix.cpp:816
void inverseInPlaceGaussJordan()
Definition: Matrix.cpp:1353
void arrayDiv(const Impl &B, Impl &out) const
double norm() const
Definition: Matrix.cpp:523
Matrix3 toMatrix3() const
Definition: Matrix.cpp:310
Matrix col(int c) const
Definition: Matrix.cpp:217
Matrix & operator*=(const T &B)
Definition: Matrix.cpp:89
void div(T B, Impl &out) const
Matrix operator*(const T &B) const
Definition: Matrix.h:369
Matrix operator-() const
Definition: Matrix.h:465
Vector2int16
Definition: Vector2int16.h:28
Matrix row3PseudoInverse(const Matrix &B) const
Definition: Matrix.cpp:1262
int rows() const
Definition: Matrix.h:324
void sub(const Impl &B, Impl &out) const
double abs(double fValue)
Definition: g3dmath.h:617
void adjoint(Impl &out) const
Definition: Matrix.cpp:776
bool allZero() const
Definition: Matrix.h:571
int dataSize
Definition: Matrix.h:110
void setZero()
Definition: Matrix.cpp:595
void setRow(int r, const T *vals)
Definition: Matrix.cpp:810
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
int col
Definition: Matrix.h:248
G3D::Matrix operator+(const G3D::Matrix::T &v, const G3D::Matrix &M)
Definition: Matrix.h:628
arena_t NULL
Definition: jemalloc_internal.h:624
int numElements() const
Definition: Matrix.h:554
static Matrix fromDiagonal(const Array< S > &d)
Definition: Matrix.h:296
void arrayExp(Impl &out) const
static G3D::Matrix::T negate(G3D::Matrix::T x)
Definition: Matrix.cpp:8
void set(int r, int c, T v)
Definition: Matrix.cpp:159
int length() const
Definition: Array.h:438
void setRow(int r, const Matrix &vec)
Definition: Matrix.cpp:168
void mulRow(int r, const T &v)
Definition: Matrix.cpp:289
Impl & operator=(const Impl &m)
Definition: Matrix.cpp:587
void cofactor(Impl &out) const
Definition: Matrix.cpp:783
T determinant() const
Definition: Matrix.h:481
bool operator>(const SortRank &x) const
Definition: Matrix.h:250
int R
Definition: Matrix.h:105
Definition: Matrix.h:245
void swapRows(int r0, int r1)
Definition: Matrix.cpp:600
Matrix pseudoInverse(float tolerance=-1) const
Computes the Moore-Penrose pseudo inverse, equivalent to (ATA)-1AT). The SVD method is used for perfo...
Definition: Matrix.cpp:885
T * data
Definition: Matrix.h:102
void swapAndNegateCols(int c0, int c1)
Definition: Matrix.cpp:612
T determinant() const
Definition: Matrix.cpp:823
std::string toString() const
Definition: Matrix.h:584
Definition: Vector3.h:58
void inverseViaAdjoint(Impl &out) const
Definition: Matrix.cpp:734
static void * malloc(size_t bytes)
Definition: System.cpp:1441
static Matrix identity(int N)
Definition: Matrix.cpp:262
Matrix colPartPseudoInverse() const
Definition: Matrix.cpp:1073
void arraySqrt(Impl &out) const
Matrix inverse() const
Definition: Matrix.h:475
Definition: ReferenceCount.h:24
T ** elt
Definition: Matrix.h:99
Definition: Matrix4.h:36
ImplRef impl
Definition: Matrix.h:239
Matrix subMatrix(int r1, int r2, int c1, int c2) const
Definition: Matrix.cpp:395
bool allNonZero() const
Definition: Matrix.cpp:1474
bool anyNonZero() const
Definition: Matrix.cpp:1463
Impl(const Impl &B)
Definition: Matrix.h:129
Matrix lsub(const T &B) const
Definition: Matrix.h:417
void mulRow(int r, const T &v)
Definition: Matrix.cpp:623
Definition: adtfile.h:39
void arrayCos(Impl &out) const
void setSize(int newRows, int newCols)
Definition: Matrix.cpp:550
Matrix & operator-=(const T &B)
Definition: Matrix.cpp:95
void serialize(TextOutput &t) const
Definition: Matrix.cpp:17
#define debugAssert(exp)
Definition: debugAssert.h:160
Matrix4 toMatrix4() const
Definition: Matrix.cpp:320
Vector2 toVector2() const
Definition: Matrix.cpp:331
Matrix operator-(const Matrix &B) const
Definition: Matrix.h:383
Impl()
Definition: Matrix.h:117
Matrix col4PseudoInverse(const Matrix &B) const
Definition: Matrix.cpp:1188
Matrix operator-(const T &v) const
Definition: Matrix.h:397
static int debugNumCopyOps
Definition: Matrix.h:69
bool operator==(const SortRank &x) const
Definition: Matrix.h:266
Matrix row(int r) const
Definition: Matrix.cpp:208
Matrix operator<=(const T &scalar) const
static Matrix one(int R, int C)
Definition: Matrix.cpp:244
void arrayLog(Impl &out) const
void setCol(int c, const Matrix &vec)
Definition: Matrix.cpp:185
Definition: Vector4.h:39
Matrix col2PseudoInverse(const Matrix &B) const
Definition: Matrix.cpp:1111
void swapRows(int r0, int r1)
Definition: Matrix.cpp:364
Vector2int16 size() const
Definition: Matrix.h:550
Matrix operator!=(const T &scalar) const
T value
Definition: Matrix.h:247
Matrix operator*(const Matrix &B) const
Definition: Matrix.h:362
Matrix partitionPseudoInverse() const
Definition: Matrix.cpp:1334
void set(int r, int c, T v)
Definition: Matrix.h:136
Definition: TextOutput.h:60
void arrayMulInPlace(const Matrix &B)
Definition: Matrix.cpp:134
Matrix(const Matrix4 &M)
Definition: Matrix.h:293
Definition: Matrix3.h:37
Definition: Matrix.h:82
Matrix operator+(const Matrix &B) const
Definition: Matrix.h:376
void abs(Impl &out) const
Vector3 toVector3() const
Definition: Matrix.cpp:341
int cols() const
Definition: Matrix.h:329
static void free(void *p)
Definition: System.cpp:1473
#define DECLARE_METHODS_1(method)
Definition: Matrix.h:446
G3D::Matrix operator-(const G3D::Matrix::T &v, const G3D::Matrix &M)
Definition: Matrix.h:620
void mul(const Impl &B, Impl &out) const
Definition: Matrix.cpp:632
bool anyZero() const
Definition: Matrix.h:575
Matrix operator+(const T &v) const
Definition: Matrix.h:390
static int debugNumAllocOps
Definition: Matrix.h:73
float T
Definition: Matrix.h:65
Matrix adjoint() const
Definition: Matrix.h:497
void transpose(Impl &out) const
Definition: Matrix.cpp:753
bool anyNonZero() const
Definition: Matrix.cpp:415
Matrix & operator+=(const T &B)
Definition: Matrix.cpp:101
G3D::Matrix operator*(const G3D::Matrix::T &v, const G3D::Matrix &M)
Definition: Matrix.h:624
Matrix row4PseudoInverse(const Matrix &B) const
Definition: Matrix.cpp:1297
Matrix operator>=(const T &scalar) const
Impl(int r, int c)
Definition: Matrix.h:123
Matrix(const Matrix3 &M)
Definition: Matrix.h:291
bool operator<(const SortRank &x) const
Definition: Matrix.h:254
Matrix()
Definition: Matrix.h:289
double normSquared() const
Definition: Matrix.cpp:508
void arrayMul(const Impl &B, Impl &out) const
void lsub(T B, Impl &out) const
Definition: Matrix.cpp:722
Matrix operator<(const T &scalar) const
Matrix row2PseudoInverse(const Matrix &B) const
Definition: Matrix.cpp:1224
void svd(Matrix &U, Array< T > &d, Matrix &V, bool sort=true) const
Definition: Matrix.cpp:425
void add(const Impl &B, Impl &out) const
shared_ptr< Impl > ImplRef
Definition: Matrix.h:237
void negate(Impl &out) const
void swapAndNegateCols(int c0, int c1)
Definition: Matrix.cpp:380
G3D::int16 x
Definition: Vector2int16.h:37
Definition: Matrix.h:56
static Matrix zero(int R, int C)
Definition: Matrix.cpp:237
void withoutRowAndCol(int excludeRow, int excludeCol, Impl &out) const
Definition: Matrix.cpp:873
Matrix rowPartPseudoInverse() const
Definition: Matrix.cpp:1033
bool operator>=(const SortRank &x) const
Definition: Matrix.h:258
static Matrix random(int R, int C)
Definition: Matrix.cpp:253
void arrayDivInPlace(const Matrix &B)
Definition: Matrix.cpp:140
Definition: Vector2int16.h:22
Matrix arrayMul(const Matrix &B) const
Definition: Matrix.h:423
Matrix svdPseudoInverse(float tolerance=-1) const
Definition: Matrix.cpp:900
Matrix transpose() const
Definition: Matrix.h:488
Vector4 toVector4() const
Definition: Matrix.cpp:351
~Impl()
Definition: Matrix.cpp:580
int C
Definition: Matrix.h:108
bool operator!=(const SortRank &x) const
Definition: Matrix.h:270
Matrix col3PseudoInverse(const Matrix &B) const
Definition: Matrix.cpp:1152
bool operator<=(const SortRank &x) const
Definition: Matrix.h:262
Matrix gaussJordanPseudoInverse() const
Definition: Matrix.h:521
void arraySin(Impl &out) const