Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Matrix4.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Matrix4_INCLUDE_ONCE
33 #define Matrix4_INCLUDE_ONCE
34 
35 #include <vlCore/Vector4.hpp>
36 #include <vlCore/Matrix3.hpp>
37 
38 namespace vl
39 {
40  //-----------------------------------------------------------------------------
41  // Matrix4
42  //-----------------------------------------------------------------------------
47  template<typename T_Scalar>
48  class Matrix4
49  {
50  public:
51  typedef T_Scalar scalar_type;
52  //-----------------------------------------------------------------------------
53  template<typename T>
54  explicit Matrix4(const Matrix4<T>& m)
55  {
56  e(0,0) = (T_Scalar)m.e(0,0); e(1,0) = (T_Scalar)m.e(1,0); e(2,0) = (T_Scalar)m.e(2,0); e(3,0) = (T_Scalar)m.e(3,0);
57  e(0,1) = (T_Scalar)m.e(0,1); e(1,1) = (T_Scalar)m.e(1,1); e(2,1) = (T_Scalar)m.e(2,1); e(3,1) = (T_Scalar)m.e(3,1);
58  e(0,2) = (T_Scalar)m.e(0,2); e(1,2) = (T_Scalar)m.e(1,2); e(2,2) = (T_Scalar)m.e(2,2); e(3,2) = (T_Scalar)m.e(3,2);
59  e(0,3) = (T_Scalar)m.e(0,3); e(1,3) = (T_Scalar)m.e(1,3); e(2,3) = (T_Scalar)m.e(2,3); e(3,3) = (T_Scalar)m.e(3,3);
60  }
61  //-----------------------------------------------------------------------------
63  {
64  setIdentity();
65  }
66  //-----------------------------------------------------------------------------
67  explicit Matrix4(T_Scalar n)
68  {
69  setIdentity();
70  e(0,0) = e(1,1) = e(2,2) = e(3,3) = n;
71  }
72  //-----------------------------------------------------------------------------
73  explicit Matrix4(T_Scalar* val)
74  {
75  fillPtr(val);
76  }
77  //-----------------------------------------------------------------------------
78  explicit Matrix4( T_Scalar e00, T_Scalar e01, T_Scalar e02, T_Scalar e03,
79  T_Scalar e10, T_Scalar e11, T_Scalar e12, T_Scalar e13,
80  T_Scalar e20, T_Scalar e21, T_Scalar e22, T_Scalar e23,
81  T_Scalar e30, T_Scalar e31, T_Scalar e32, T_Scalar e33)
82  {
83  e(0,0) = e00; e(0,1) = e01; e(0,2) = e02; e(0,3) = e03;
84  e(1,0) = e10; e(1,1) = e11; e(1,2) = e12; e(1,3) = e13;
85  e(2,0) = e20; e(2,1) = e21; e(2,2) = e22; e(2,3) = e23;
86  e(3,0) = e30; e(3,1) = e31; e(3,2) = e32; e(3,3) = e33;
87  }
88  //-----------------------------------------------------------------------------
89  Matrix4& fill(T_Scalar val)
90  {
91  e(0,0) = e(1,0) = e(2,0) = e(3,0) =
92  e(0,1) = e(1,1) = e(2,1) = e(3,1) =
93  e(0,2) = e(1,2) = e(2,2) = e(3,2) =
94  e(0,3) = e(1,3) = e(2,3) = e(3,3) = val;
95  return *this;
96  }
97  //-----------------------------------------------------------------------------
98  Matrix4& fillPtr(T_Scalar* val)
99  {
100  memcpy(ptr(), val, sizeof(T_Scalar)*16);
101  return *this;
102  }
103  //-----------------------------------------------------------------------------
104  T_Scalar diff(const Matrix4& other) const
105  {
106  T_Scalar err = 0;
107  for(int i=0; i<4; ++i)
108  for(int j=0; j<4; ++j)
109  if (e(j,i) > other.e(j,i)) // avoid fabs/abs
110  err += e(j,i) - other.e(j,i);
111  else
112  err += other.e(j,i) - e(j,i);
113  return err;
114  }
115  //-----------------------------------------------------------------------------
117  {
118  return Vector3<T_Scalar>(mVec[0].x(), mVec[0].y(), mVec[0].z());
119  }
120  //-----------------------------------------------------------------------------
122  {
123  return Vector3<T_Scalar>(mVec[1].x(), mVec[1].y(), mVec[1].z());
124  }
125  //-----------------------------------------------------------------------------
127  {
128  return Vector3<T_Scalar>(mVec[2].x(), mVec[2].y(), mVec[2].z());
129  }
130  //-----------------------------------------------------------------------------
132  {
133  return Vector3<T_Scalar>(mVec[3].x(), mVec[3].y(), mVec[3].z());
134  }
135  //-----------------------------------------------------------------------------
137  {
138  mVec[0].x() = v.x();
139  mVec[0].y() = v.y();
140  mVec[0].z() = v.z();
141  return *this;
142  }
143  //-----------------------------------------------------------------------------
145  {
146  mVec[1].x() = v.x();
147  mVec[1].y() = v.y();
148  mVec[1].z() = v.z();
149  return *this;
150  }
151  //-----------------------------------------------------------------------------
153  {
154  mVec[2].x() = v.x();
155  mVec[2].y() = v.y();
156  mVec[2].z() = v.z();
157  return *this;
158  }
159  //-----------------------------------------------------------------------------
161  {
162  mVec[3].x() = v.x();
163  mVec[3].y() = v.y();
164  mVec[3].z() = v.z();
165  return *this;
166  }
167  //-----------------------------------------------------------------------------
168  bool operator==(const Matrix4& m) const
169  {
170  return memcmp(m.mVec, mVec, sizeof(T_Scalar)*4*4) == 0;
171  }
172  //-----------------------------------------------------------------------------
173  bool operator!=(const Matrix4& m) const
174  {
175  return !operator==(m);
176  }
177  //-----------------------------------------------------------------------------
179  {
180  memcpy(mVec, m.mVec, sizeof(T_Scalar)*16);
181  return *this;
182  }
183  //-----------------------------------------------------------------------------
184  Matrix4 operator+(const Matrix4& m) const
185  {
186  Matrix4 t;
187  for(int i=0; i<4; ++i)
188  for(int j=0; j<4; ++j)
189  t.e(j,i) = e(j,i) + m.e(j,i);
190 
191  return t;
192  }
193  //-----------------------------------------------------------------------------
195  {
196  for(int i=0; i<4; ++i)
197  for(int j=0; j<4; ++j)
198  e(j,i) += m.e(i,j);
199  return *this;
200  }
201  //-----------------------------------------------------------------------------
202  Matrix4 operator-(const Matrix4& m) const
203  {
204  Matrix4 t;
205  for(int i=0; i<4; ++i)
206  for(int j=0; j<4; ++j)
207  t.e(j,i) = e(j,i) - m.e(j,i);
208 
209  return t;
210  }
211  //-----------------------------------------------------------------------------
213  {
214  for(int i=0; i<4; ++i)
215  for(int j=0; j<4; ++j)
216  e(j,i) -= m.e(i,j);
217  return *this;
218  }
219  //-----------------------------------------------------------------------------
221  {
222  return postMultiply(m);
223  }
224  //-----------------------------------------------------------------------------
226  {
227  Matrix4 t;
228  for(int i=0; i<4; ++i)
229  for(int j=0; j<4; ++j)
230  t.e(j,i) = -e(j,i);
231  return t;
232  }
233  //-----------------------------------------------------------------------------
234  Matrix4 operator+(T_Scalar d) const
235  {
236  Matrix4 t;
237  for(int i=0; i<4; ++i)
238  for(int j=0; j<4; ++j)
239  t.e(j,i) = e(j,i) + d;
240  return t;
241  }
242  //-----------------------------------------------------------------------------
243  Matrix4& operator+=(T_Scalar d)
244  {
245  for(int i=0; i<4; ++i)
246  for(int j=0; j<4; ++j)
247  e(j,i) += d;
248  return *this;
249  }
250  //-----------------------------------------------------------------------------
251  Matrix4 operator-(T_Scalar d) const
252  {
253  Matrix4 t;
254  for(int i=0; i<4; ++i)
255  for(int j=0; j<4; ++j)
256  t.e(j,i) = e(j,i) - d;
257  return t;
258  }
259  //-----------------------------------------------------------------------------
260  Matrix4& operator-=(T_Scalar d)
261  {
262  for(int i=0; i<4; ++i)
263  for(int j=0; j<4; ++j)
264  e(j,i) -= d;
265  return *this;
266  }
267  //-----------------------------------------------------------------------------
268  Matrix4 operator*(T_Scalar d) const
269  {
270  Matrix4 t;
271  for(int i=0; i<4; ++i)
272  for(int j=0; j<4; ++j)
273  t.e(j,i) = e(j,i) * d;
274  return t;
275  }
276  //-----------------------------------------------------------------------------
277  Matrix4& operator*=(T_Scalar d)
278  {
279  for(int i=0; i<4; ++i)
280  for(int j=0; j<4; ++j)
281  e(j,i) *= d;
282  return *this;
283  }
284  //-----------------------------------------------------------------------------
285  Matrix4 operator/(T_Scalar d) const
286  {
287  d = (T_Scalar)1 / d;
288  Matrix4 t;
289  for(int i=0; i<4; ++i)
290  for(int j=0; j<4; ++j)
291  t.e(j,i) = e(j,i) * d;
292  return t;
293  }
294  //-----------------------------------------------------------------------------
295  Matrix4& operator/=(T_Scalar d)
296  {
297  d = (T_Scalar)1 / d;
298  for(int i=0; i<4; ++i)
299  for(int j=0; j<4; ++j)
300  e(j,i) *= d;
301  return *this;
302  }
303  //-----------------------------------------------------------------------------
304  bool isIdentity() const
305  {
306  Matrix4 i;
307  return memcmp(ptr(), i.ptr(), sizeof(T_Scalar)*16) == 0;
308  }
309  //-----------------------------------------------------------------------------
310  Matrix4 as3x3() const
311  {
312  Matrix4 t = *this;
313  t[0][3] = 0;
314  t[1][3] = 0;
315  t[2][3] = 0;
316  t[3][3] = 1;
317  t[3][0] = 0;
318  t[3][1] = 0;
319  t[3][2] = 0;
320  return t;
321  }
322  //-----------------------------------------------------------------------------
324  {
326  t.e(0,0) = e(0,0); t.e(1,0) = e(1,0); t.e(2,0) = e(2,0);
327  t.e(0,1) = e(0,1); t.e(1,1) = e(1,1); t.e(2,1) = e(2,1);
328  t.e(0,2) = e(0,2); t.e(1,2) = e(1,2); t.e(2,2) = e(2,2);
329  return t;
330  }
331  //-----------------------------------------------------------------------------
333  void set3x3(const Matrix3<T_Scalar>& m)
334  {
335  e(0,0) = m.e(0,0); e(1,0) = m.e(1,0); e(2,0) = m.e(2,0);
336  e(0,1) = m.e(0,1); e(1,1) = m.e(1,1); e(2,1) = m.e(2,1);
337  e(0,2) = m.e(0,2); e(1,2) = m.e(1,2); e(2,2) = m.e(2,2);
338  }
339  //-----------------------------------------------------------------------------
340  T_Scalar* ptr()
341  {
342  return &e(0,0);
343  }
344  //-----------------------------------------------------------------------------
345  const T_Scalar* ptr() const
346  {
347  return &e(0,0);
348  }
349  //-----------------------------------------------------------------------------
351  {
352  T_Scalar tmp;
353  for(int i=0; i<4; ++i)
354  for(int j=i; j<4; ++j)
355  {
356  tmp = e(j,i);
357  e(j,i) = e(i,j);
358  e(i,j) = tmp;
359  }
360  return *this;
361  }
362  //-----------------------------------------------------------------------------
364  {
365  Matrix4 m;
366  for(int i=0; i<4; ++i)
367  for(int j=0; j<4; ++j)
368  m.e(j,i) = e(i,j);
369  return m;
370  }
371  //-----------------------------------------------------------------------------
373  {
374  for(int i=0; i<4; ++i)
375  for(int j=0; j<4; ++j)
376  dest.e(j,i) = e(i,j);
377  return dest;
378  }
379  //-----------------------------------------------------------------------------
380  bool isNull() const
381  {
382  for(int i=0; i<4; ++i)
383  for(int j=0; j<4; ++j)
384  if(e(i,j) != 0)
385  return false;
386  return true;
387  }
388  //-----------------------------------------------------------------------------
390  {
391  fill(0);
392  return *this;
393  }
394  //-----------------------------------------------------------------------------
395  static Matrix4& getNull(Matrix4& out)
396  {
397  out.fill(0);
398  return out;
399  }
400  //-----------------------------------------------------------------------------
401  static Matrix4 getNull()
402  {
403  return Matrix4().fill(0);
404  }
405  //-----------------------------------------------------------------------------
407  {
408  static const T_Scalar I4d[] =
409  {
410  (T_Scalar)1, (T_Scalar)0, (T_Scalar)0, (T_Scalar)0,
411  (T_Scalar)0, (T_Scalar)1, (T_Scalar)0, (T_Scalar)0,
412  (T_Scalar)0, (T_Scalar)0, (T_Scalar)1, (T_Scalar)0,
413  (T_Scalar)0, (T_Scalar)0, (T_Scalar)0, (T_Scalar)1
414  };
415  memcpy(mVec, I4d, sizeof(T_Scalar)*16);
416  return *this;
417  }
418  //-----------------------------------------------------------------------------
420  {
421  return Matrix4();
422  }
423  //-----------------------------------------------------------------------------
425  {
426  out.setIdentity();
427  return out;
428  }
429  //-----------------------------------------------------------------------------
430  T_Scalar getInverse(Matrix4& dest) const;
431  //-----------------------------------------------------------------------------
432  Matrix4 getInverse(T_Scalar *determinant=NULL) const
433  {
434  Matrix4 tmp;
435  T_Scalar det = getInverse(tmp);
436  if (determinant)
437  *determinant = det;
438  return tmp;
439  }
440  //-----------------------------------------------------------------------------
441  Matrix4& invert(T_Scalar *determinant=NULL)
442  {
443  T_Scalar det = getInverse(*this);
444  if (determinant)
445  *determinant = det;
446  return *this;
447  }
448  //-----------------------------------------------------------------------------
449  static Matrix4 getPerspective(T_Scalar fovy, T_Scalar aspect_ratio, T_Scalar znear, T_Scalar zfar);
450  //-----------------------------------------------------------------------------
451  static Matrix4 getFrustum(T_Scalar pleft, T_Scalar pright, T_Scalar pbottom, T_Scalar ptop, T_Scalar pnear, T_Scalar pfar);
452  //-----------------------------------------------------------------------------
453  static Matrix4 getOrtho(T_Scalar pleft, T_Scalar pright, T_Scalar pbottom, T_Scalar ptop, T_Scalar pnear, T_Scalar pfar);
454  //-----------------------------------------------------------------------------
455  static Matrix4 getOrtho2D(T_Scalar pleft, T_Scalar pright, T_Scalar pbottom, T_Scalar ptop);
456  //-----------------------------------------------------------------------------
457  static Matrix4 getLookAtModeling(const Vector3<T_Scalar>& eye, const Vector3<T_Scalar>& at, const Vector3<T_Scalar>& up);
458  //-----------------------------------------------------------------------------
459  static Matrix4 getLookAt(const Vector3<T_Scalar>& eye, const Vector3<T_Scalar>& at, const Vector3<T_Scalar>& up);
460  //-----------------------------------------------------------------------------
462  //-----------------------------------------------------------------------------
464  //-----------------------------------------------------------------------------
465  void getYXRotationAngles(T_Scalar& degrees_y, T_Scalar& degrees_x) const;
466  //-----------------------------------------------------------------------------
467  static Matrix4& getRotation(Matrix4& out, T_Scalar degrees, T_Scalar x, T_Scalar y, T_Scalar z);
468  //-----------------------------------------------------------------------------
469  static Matrix4 getRotation(T_Scalar degrees, T_Scalar x, T_Scalar y, T_Scalar z)
470  {
471  Matrix4 m;
472  return getRotation(m, degrees, x, y, z);
473  }
474  //-----------------------------------------------------------------------------
475  static Matrix4 getRotation(T_Scalar degrees, const Vector3<T_Scalar>& v)
476  {
477  return getRotation(degrees, v.x(), v.y(), v.z());
478  }
479  //-----------------------------------------------------------------------------
480  static Matrix4 getRotation(T_Scalar degrees1, const Vector3<T_Scalar>& v1, T_Scalar degrees2, const Vector3<T_Scalar>& v2)
481  {
482  return getRotation(degrees1, v1.x(), v1.y(), v1.z()) * getRotation(degrees2, v2.x(), v2.y(), v2.z());
483  }
484  //-----------------------------------------------------------------------------
485  static Matrix4 getRotation(T_Scalar degrees1, const Vector3<T_Scalar>& v1, T_Scalar degrees2, const Vector3<T_Scalar>& v2, T_Scalar degrees3, const Vector3<T_Scalar>& v3)
486  {
487  return getRotation(degrees1, v1.x(), v1.y(), v1.z()) * getRotation(degrees2, v2.x(), v2.y(), v2.z()) * getRotation(degrees3, v3.x(), v3.y(), v3.z());
488  }
489  //-----------------------------------------------------------------------------
490  Matrix4& rotate(T_Scalar degrees, const Vector3<T_Scalar>& v)
491  {
492  return rotate(degrees, v.x(), v.y(), v.z());
493  }
494  //-----------------------------------------------------------------------------
495  Matrix4& rotate(T_Scalar degrees, T_Scalar x, T_Scalar y, T_Scalar z)
496  {
497  return preMultiply(getRotation(degrees, x, y, z));
498  }
499  //-----------------------------------------------------------------------------
500  Matrix4& rotate(T_Scalar degrees1, const Vector3<T_Scalar>& v1, T_Scalar degrees2, const Vector3<T_Scalar>& v2)
501  {
502  return preMultiply(getRotation(degrees1, v1, degrees2, v2));
503  }
504  //-----------------------------------------------------------------------------
505  Matrix4& rotate(T_Scalar degrees1, const Vector3<T_Scalar>& v1, T_Scalar degrees2, const Vector3<T_Scalar>& v2, T_Scalar degrees3, const Vector3<T_Scalar>& v3)
506  {
507  return preMultiply(getRotation(degrees1, v1, degrees2, v2, degrees3, v3));
508  }
509  //-----------------------------------------------------------------------------
510  static Matrix4 getRotationXYZ(T_Scalar degX, T_Scalar degY, T_Scalar degZ)
511  {
512  return getRotation(degX, 1,0,0) * getRotation(degY, 0,1,0) * getRotation(degZ, 0,0,1);
513  }
514  //-----------------------------------------------------------------------------
515  Matrix4& rotateXYZ(T_Scalar degX, T_Scalar degY, T_Scalar degZ)
516  {
517  return preMultiply(getRotationXYZ(degX, degY, degZ));
518  }
519  //-----------------------------------------------------------------------------
520  static Matrix4 getRotationZYX(T_Scalar degZ, T_Scalar degY, T_Scalar degX)
521  {
522  return getRotation(degZ, 0,0,1) * getRotation(degY, 0,1,0) * getRotation(degX, 1,0,0);
523  }
524  //-----------------------------------------------------------------------------
525  Matrix4& rotateZYX(T_Scalar degZ, T_Scalar degY, T_Scalar degX)
526  {
527  return preMultiply(getRotationZYX(degZ, degY, degX));
528  }
529  //-----------------------------------------------------------------------------
530  static Matrix4& getRotation(Matrix4& out, const Vector3<T_Scalar>& from, const Vector3<T_Scalar>& to);
531  //-----------------------------------------------------------------------------
533  {
534  Matrix4 m;
535  return getRotation(m, from, to);
536  }
537  //-----------------------------------------------------------------------------
539  {
540  return preMultiply(getRotation(from, to));
541  }
542  //-----------------------------------------------------------------------------
544  {
545  return preMultiply(getRotation(from, to));
546  }
547  //-----------------------------------------------------------------------------
549  {
550  return getTranslation(out, v.x(), v.y(), v.z());
551  }
552  //-----------------------------------------------------------------------------
554  {
555  Matrix4 m;
556  return getTranslation(m, v.x(), v.y(), v.z());
557  }
558  //-----------------------------------------------------------------------------
559  static Matrix4 getTranslation(T_Scalar x, T_Scalar y, T_Scalar z)
560  {
561  Matrix4 m;
562  return getTranslation(m, x, y, z);
563  }
564  //-----------------------------------------------------------------------------
565  static Matrix4& getTranslation(Matrix4& out, T_Scalar x, T_Scalar y, T_Scalar z)
566  {
567  out.setIdentity();
568  out.e(0,3) = x;
569  out.e(1,3) = y;
570  out.e(2,3) = z;
571  return out;
572  }
573  //-----------------------------------------------------------------------------
574  Matrix4& translate(T_Scalar x, T_Scalar y, T_Scalar z)
575  {
576  return preMultiply(getTranslation(x,y,z));
577  }
578  //-----------------------------------------------------------------------------
580  {
581  return preMultiply(getTranslation(v));
582  }
583  //-----------------------------------------------------------------------------
584  static Matrix4& getScaling(Matrix4& out, const Vector3<T_Scalar>& v)
585  {
586  return getScaling(out, v.x(), v.y(), v.z());
587  }
588  //-----------------------------------------------------------------------------
590  {
591  Matrix4 m;
592  return getScaling(m, v.x(), v.y(), v.z());
593  }
594  //-----------------------------------------------------------------------------
595  static Matrix4 getScaling(T_Scalar x, T_Scalar y, T_Scalar z)
596  {
597  Matrix4 m;
598  return getScaling(m, x, y, z);
599  }
600  //-----------------------------------------------------------------------------
601  static Matrix4& getScaling(Matrix4& out, T_Scalar x, T_Scalar y, T_Scalar z)
602  {
603  out.setIdentity();
604  out.e(0,0) = x;
605  out.e(1,1) = y;
606  out.e(2,2) = z;
607  return out;
608  }
609  //-----------------------------------------------------------------------------
610  Matrix4& scale(T_Scalar x, T_Scalar y, T_Scalar z)
611  {
612  return preMultiply(getScaling(x,y,z));
613  }
614  //-----------------------------------------------------------------------------
616  {
617  return preMultiply(getScaling(v.x(), v.y(), v.z()));
618  }
619  //-----------------------------------------------------------------------------
620  static Matrix4& multiply(Matrix4& out, const Matrix4& p, const Matrix4& q)
621  {
622  VL_CHECK(out.ptr() != p.ptr() && out.ptr() != q.ptr());
623 
624  out.e(0,0) = q.e(0,0)*p.e(0,0) + q.e(1,0)*p.e(0,1) + q.e(2,0)*p.e(0,2) + q.e(3,0)*p.e(0,3);
625  out.e(0,1) = q.e(0,1)*p.e(0,0) + q.e(1,1)*p.e(0,1) + q.e(2,1)*p.e(0,2) + q.e(3,1)*p.e(0,3);
626  out.e(0,2) = q.e(0,2)*p.e(0,0) + q.e(1,2)*p.e(0,1) + q.e(2,2)*p.e(0,2) + q.e(3,2)*p.e(0,3);
627  out.e(0,3) = q.e(0,3)*p.e(0,0) + q.e(1,3)*p.e(0,1) + q.e(2,3)*p.e(0,2) + q.e(3,3)*p.e(0,3);
628 
629  out.e(1,0) = q.e(0,0)*p.e(1,0) + q.e(1,0)*p.e(1,1) + q.e(2,0)*p.e(1,2) + q.e(3,0)*p.e(1,3);
630  out.e(1,1) = q.e(0,1)*p.e(1,0) + q.e(1,1)*p.e(1,1) + q.e(2,1)*p.e(1,2) + q.e(3,1)*p.e(1,3);
631  out.e(1,2) = q.e(0,2)*p.e(1,0) + q.e(1,2)*p.e(1,1) + q.e(2,2)*p.e(1,2) + q.e(3,2)*p.e(1,3);
632  out.e(1,3) = q.e(0,3)*p.e(1,0) + q.e(1,3)*p.e(1,1) + q.e(2,3)*p.e(1,2) + q.e(3,3)*p.e(1,3);
633 
634  out.e(2,0) = q.e(0,0)*p.e(2,0) + q.e(1,0)*p.e(2,1) + q.e(2,0)*p.e(2,2) + q.e(3,0)*p.e(2,3);
635  out.e(2,1) = q.e(0,1)*p.e(2,0) + q.e(1,1)*p.e(2,1) + q.e(2,1)*p.e(2,2) + q.e(3,1)*p.e(2,3);
636  out.e(2,2) = q.e(0,2)*p.e(2,0) + q.e(1,2)*p.e(2,1) + q.e(2,2)*p.e(2,2) + q.e(3,2)*p.e(2,3);
637  out.e(2,3) = q.e(0,3)*p.e(2,0) + q.e(1,3)*p.e(2,1) + q.e(2,3)*p.e(2,2) + q.e(3,3)*p.e(2,3);
638 
639  out.e(3,0) = q.e(0,0)*p.e(3,0) + q.e(1,0)*p.e(3,1) + q.e(2,0)*p.e(3,2) + q.e(3,0)*p.e(3,3);
640  out.e(3,1) = q.e(0,1)*p.e(3,0) + q.e(1,1)*p.e(3,1) + q.e(2,1)*p.e(3,2) + q.e(3,1)*p.e(3,3);
641  out.e(3,2) = q.e(0,2)*p.e(3,0) + q.e(1,2)*p.e(3,1) + q.e(2,2)*p.e(3,2) + q.e(3,2)*p.e(3,3);
642  out.e(3,3) = q.e(0,3)*p.e(3,0) + q.e(1,3)*p.e(3,1) + q.e(2,3)*p.e(3,2) + q.e(3,3)*p.e(3,3);
643 
644  return out;
645  }
646  //-----------------------------------------------------------------------------
648  {
650  return *this = multiply(t, *this, m);
651  }
652  //-----------------------------------------------------------------------------
654  {
656  return *this = multiply(t, m, *this);
657  }
658  //-----------------------------------------------------------------------------
659 
660  const T_Scalar& e(int i, int j) const { return mVec[j][i]; }
661  T_Scalar& e(int i, int j) { return mVec[j][i]; }
662 
663  private:
664  const Vector4<T_Scalar>& operator[](unsigned int i) const { VL_CHECK(i<4); return mVec[i]; }
665  Vector4<T_Scalar>& operator[](unsigned int i) { VL_CHECK(i<4); return mVec[i]; }
666 
667  protected:
669  };
670  //-----------------------------------------------------------------------------
671  // OPERATORS
672  //-----------------------------------------------------------------------------
673  template<typename T_Scalar>
675  {
678  return t;
679  }
680  //-----------------------------------------------------------------------------
681  template<typename T_Scalar>
682  inline Matrix4<T_Scalar> operator+(T_Scalar d, const Matrix4<T_Scalar>& m)
683  {
684  return m + d;
685  }
686  //-----------------------------------------------------------------------------
687  template<typename T_Scalar>
688  inline Matrix4<T_Scalar> operator*(T_Scalar d, const Matrix4<T_Scalar>& m)
689  {
690  return m * d;
691  }
692  //-----------------------------------------------------------------------------
694  template<typename T_Scalar>
696  {
697  return Vector4<T_Scalar>(
698  v.x()*m.e(0,0) + v.y()*m.e(0,1) + v.z()*m.e(0,2) + v.w()*m.e(0,3),
699  v.x()*m.e(1,0) + v.y()*m.e(1,1) + v.z()*m.e(1,2) + v.w()*m.e(1,3),
700  v.x()*m.e(2,0) + v.y()*m.e(2,1) + v.z()*m.e(2,2) + v.w()*m.e(2,3),
701  v.x()*m.e(3,0) + v.y()*m.e(3,1) + v.z()*m.e(3,2) + v.w()*m.e(3,3)
702  );
703  }
704  //-----------------------------------------------------------------------------
707  template<typename T_Scalar>
709  {
710  return Vector3<T_Scalar>(
711  v.x()*m.e(0,0) + v.y()*m.e(0,1) + v.z()*m.e(0,2) + /*1**/m.e(0,3),
712  v.x()*m.e(1,0) + v.y()*m.e(1,1) + v.z()*m.e(1,2) + /*1**/m.e(1,3),
713  v.x()*m.e(2,0) + v.y()*m.e(2,1) + v.z()*m.e(2,2) + /*1**/m.e(2,3)
714  );
715  }
716  //-----------------------------------------------------------------------------
719  template<typename T_Scalar>
721  {
722  return Vector2<T_Scalar>(
723  v.x()*m.e(0,0) + v.y()*m.e(0,1) + /*0*m.e(0,2) +*/ /*1**/m.e(0,3),
724  v.x()*m.e(1,0) + v.y()*m.e(1,1) + /*0*m.e(1,2) +*/ /*1**/m.e(1,3)
725  );
726  }
727  //-----------------------------------------------------------------------------
729  template<typename T_Scalar>
731  {
732  return Vector4<T_Scalar>(
733  v.x()*m.e(0,0) + v.y()*m.e(1,0) + v.z()*m.e(2,0) + v.w()*m.e(3,0),
734  v.x()*m.e(0,1) + v.y()*m.e(1,1) + v.z()*m.e(2,1) + v.w()*m.e(3,1),
735  v.x()*m.e(0,2) + v.y()*m.e(1,2) + v.z()*m.e(2,2) + v.w()*m.e(3,2),
736  v.x()*m.e(0,3) + v.y()*m.e(1,3) + v.z()*m.e(2,3) + v.w()*m.e(3,3)
737  );
738  }
739  //-----------------------------------------------------------------------------
742  template<typename T_Scalar>
744  {
745  return Vector3<T_Scalar>(
746  v.x()*m.e(0,0) + v.y()*m.e(1,0) + v.z()*m.e(2,0) + /*1**/m.e(3,0),
747  v.x()*m.e(0,1) + v.y()*m.e(1,1) + v.z()*m.e(2,1) + /*1**/m.e(3,1),
748  v.x()*m.e(0,2) + v.y()*m.e(1,2) + v.z()*m.e(2,2) + /*1**/m.e(3,2)
749  );
750  }
751  //-----------------------------------------------------------------------------
754  template<typename T_Scalar>
756  {
757  return Vector2<T_Scalar>(
758  v.x()*m.e(0,0) + v.y()*m.e(1,0) + /*0*m.e(2,0) +*/ /*1**/m.e(3,0),
759  v.x()*m.e(0,1) + v.y()*m.e(1,1) + /*0*m.e(2,1) +*/ /*1**/m.e(3,1)
760  );
761  }
762  //-----------------------------------------------------------------------------
763  template<typename T_Scalar>
765  {
766  Vector3<T_Scalar> zaxis = (eye-at).normalize();
767  Vector3<T_Scalar> xaxis = cross(up, zaxis).normalize();
768  Vector3<T_Scalar> yaxis = cross(zaxis, xaxis);
769 
770  // look at modeling
771  T_Scalar la_modeling[] =
772  {
773  xaxis.x() , xaxis.y() , xaxis.z() , 0,
774  yaxis.x() , yaxis.y() , yaxis.z() , 0,
775  zaxis.x() , zaxis.y() , zaxis.z() , 0,
776  eye.x() , eye.y() , eye.z() , 1
777  };
778 
779  return Matrix4<T_Scalar>(la_modeling);
780  }
781  //-----------------------------------------------------------------------------
782  template<typename T_Scalar>
784  {
785  Vector3<T_Scalar> zaxis = (eye-at).normalize();
786  Vector3<T_Scalar> xaxis = cross(up, zaxis).normalize();
787  Vector3<T_Scalar> yaxis = cross(zaxis, xaxis);
788 
789  // look at view
790  T_Scalar la_view[] =
791  {
792  xaxis.x() , yaxis.x() , zaxis.x() , 0,
793  xaxis.y() , yaxis.y() , zaxis.y() , 0,
794  xaxis.z() , yaxis.z() , zaxis.z() , 0,
795  -dot(xaxis,eye), -dot(yaxis,eye), -dot(zaxis,eye), 1
796  };
797 
798  return Matrix4<T_Scalar>(la_view);
799  }
800  //-----------------------------------------------------------------------------
801  template<typename T_Scalar>
803  {
804  eye = getT();
805 
806  at = -getZ();
807  // look.normalize();
808 
809  up = getY();
810  // up.normalize();
811 
812  right = getX();
813  // right.normalize();
814  }
815  //-----------------------------------------------------------------------------
816  template<typename T_Scalar>
818  {
819  Matrix4<T_Scalar> m = *this;
820  m.invert();
821  m.getAsLookAtModeling(eye, at, up, right);
822  }
823  //-----------------------------------------------------------------------------
824  template<typename T_Scalar>
825  Matrix4<T_Scalar> Matrix4<T_Scalar>::getPerspective(T_Scalar fovy, T_Scalar aspect_ratio, T_Scalar znear, T_Scalar zfar)
826  {
828 
829  T_Scalar rads = (fovy / ((T_Scalar)2)) * (T_Scalar)dDEG_TO_RAD;
830  T_Scalar dz = zfar - znear;
831  T_Scalar sa = sin(rads);
832  if ((dz == 0) || (sa == 0) || (aspect_ratio == 0))
833  return m * 0;
834  T_Scalar ctan = cos(rads) / sa;
835 
836  m.e(0,0) = (T_Scalar)(ctan / aspect_ratio);
837  m.e(1,1) = (T_Scalar)(ctan);
838  m.e(2,2) = (T_Scalar)(-(zfar + znear) / dz);
839  m.e(3,2) = -((T_Scalar)1);
840  m.e(2,3) = (T_Scalar)(-((T_Scalar)2) * znear * zfar / dz);
841  m.e(3,3) = 0;
842 
843  return m;
844  }
845  //-----------------------------------------------------------------------------
846  template<typename T_Scalar>
847  Matrix4<T_Scalar> Matrix4<T_Scalar>::getFrustum(T_Scalar left, T_Scalar right, T_Scalar bottom, T_Scalar top, T_Scalar pnear, T_Scalar pfar)
848  {
850 
851  if (pnear <= 0 || pfar <= 0 || pnear == pfar || left == right || top == bottom)
852  return m * 0;
853 
854  T_Scalar x = (((T_Scalar)2)*pnear) / (right-left);
855  T_Scalar y = (((T_Scalar)2)*pnear) / (top-bottom);
856  T_Scalar a = (right+left) / (right-left);
857  T_Scalar b = (top+bottom) / (top-bottom);
858  T_Scalar c = -(pfar+pnear) / (pfar-pnear);
859  T_Scalar d = -(((T_Scalar)2)*pfar*pnear) / (pfar-pnear);
860 
861  m.e(0,0) = x; m.e(0,1) = 0; m.e(0,2) = a; m.e(0,3) = 0;
862  m.e(1,0) = 0; m.e(1,1) = y; m.e(1,2) = b; m.e(1,3) = 0;
863  m.e(2,0) = 0; m.e(2,1) = 0; m.e(2,2) = c; m.e(2,3) = d;
864  m.e(3,0) = 0; m.e(3,1) = 0; m.e(3,2) = -((T_Scalar)1); m.e(3,3) = 0;
865 
866  return m;
867  }
868  //-----------------------------------------------------------------------------
869  template<typename T_Scalar>
870  Matrix4<T_Scalar> Matrix4<T_Scalar>::getOrtho(T_Scalar left, T_Scalar right, T_Scalar bottom, T_Scalar top, T_Scalar pnear, T_Scalar pfar)
871  {
873 
874  m.e(0,0) = ((T_Scalar)2) / (right-left);
875  m.e(0,1) = 0;
876  m.e(0,2) = 0;
877  m.e(0,3) = -(right+left) / (right-left);
878 
879  m.e(1,0) = 0;
880  m.e(1,1) = ((T_Scalar)2) / (top-bottom);
881  m.e(1,2) = 0;
882  m.e(1,3) = -(top+bottom) / (top-bottom);
883 
884  m.e(2,0) = 0;
885  m.e(2,1) = 0;
886  m.e(2,2) = -((T_Scalar)2) / (pfar-pnear);
887  m.e(2,3) = -(pfar+pnear) / (pfar-pnear);
888 
889  m.e(3,0) = 0;
890  m.e(3,1) = 0;
891  m.e(3,2) = 0;
892  m.e(3,3) = ((T_Scalar)1);
893 
894  return m;
895  }
896  //-----------------------------------------------------------------------------
897  template<typename T_Scalar>
898  Matrix4<T_Scalar> Matrix4<T_Scalar>::getOrtho2D(T_Scalar left, T_Scalar right, T_Scalar bottom, T_Scalar top)
899  {
900  return getOrtho(left, right, bottom, top, -1, +1);
901  }
902  //-----------------------------------------------------------------------------
903  template<typename T_Scalar>
904  Matrix4<T_Scalar>& Matrix4<T_Scalar>::getRotation(Matrix4<T_Scalar>& out, T_Scalar degrees, T_Scalar x, T_Scalar y, T_Scalar z)
905  {
906  out.setIdentity();
907 
908  if (degrees == 0 || (x == 0 && y ==0 && z == 0))
909  return out;
910 
911  degrees = T_Scalar(degrees * dDEG_TO_RAD);
912 
913  T_Scalar xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
914 
915  s = (T_Scalar) sin(degrees);
916  c = (T_Scalar) cos(degrees);
917 
918  // simple cases
919  if (x == 0)
920  {
921  if (y == 0)
922  {
923  if (z != 0)
924  {
925  // rotate only around z-axis
926  out.e(0,0) = (T_Scalar)c;
927  out.e(1,1) = (T_Scalar)c;
928  if (z < 0)
929  {
930  out.e(1,0) = -(T_Scalar)s;
931  out.e(0,1) = (T_Scalar)s;
932  }
933  else
934  {
935  out.e(1,0) = (T_Scalar)s;
936  out.e(0,1) = -(T_Scalar)s;
937  }
938  return out;
939  }
940  }
941  else if (z == 0)
942  {
943  // rotate only around y-axis
944  out.e(0,0) = (T_Scalar)c;
945  out.e(2,2) = (T_Scalar)c;
946  if (y < 0)
947  {
948  out.e(2,0) = (T_Scalar)s;
949  out.e(0,2) = -(T_Scalar)s;
950  }
951  else
952  {
953  out.e(2,0) = -(T_Scalar)s;
954  out.e(0,2) = (T_Scalar)s;
955  }
956  return out;
957  }
958  }
959  else if (y == 0)
960  {
961  if (z == 0)
962  {
963  // rotate only around x-axis
964  out.e(1,1) = (T_Scalar)c;
965  out.e(2,2) = (T_Scalar)c;
966  if (x < 0)
967  {
968  out.e(2,1) = -(T_Scalar)s;
969  out.e(1,2) = (T_Scalar)s;
970  }
971  else
972  {
973  out.e(2,1) = (T_Scalar)s;
974  out.e(1,2) = -(T_Scalar)s;
975  }
976  return out;
977  }
978  }
979 
980  // Beginning of general axisa to matrix conversion
981  T_Scalar dot = x*x + y*y + z*z;
982 
983  if (dot > (T_Scalar)((T_Scalar)1.0001) || dot < (T_Scalar)0.99999)
984  {
985  T_Scalar mag = (T_Scalar) sqrt(dot);
986  x /= mag;
987  y /= mag;
988  z /= mag;
989  }
990 
991  xx = x *x;
992  yy = y * y;
993  zz = z * z;
994  xy = x * y;
995  yz = y * z;
996  zx = z * x;
997  xs = x * s;
998  ys = y * s;
999  zs = z * s;
1000  one_c = ((T_Scalar)1) - c;
1001 
1002  out.e(0,0) = (T_Scalar)((one_c * xx) + c); out.e(1,0) = (T_Scalar)((one_c * xy) + zs); out.e(2,0) = (T_Scalar)((one_c * zx) - ys);
1003  out.e(0,1) = (T_Scalar)((one_c * xy) - zs); out.e(1,1) = (T_Scalar)((one_c * yy) + c); out.e(2,1) = (T_Scalar)((one_c * yz) + xs);
1004  out.e(0,2) = (T_Scalar)((one_c * zx) + ys); out.e(1,2) = (T_Scalar)((one_c * yz) - xs); out.e(2,2) = (T_Scalar)((one_c * zz) + c);
1005  return out;
1006  }
1007  //-----------------------------------------------------------------------------
1008  template<typename T_Scalar>
1010  {
1011  const T_Scalar* in = ptr();
1012  T_Scalar* out = dest.ptr();
1013 
1014  // | 0 | 4 | 8 | 12 |
1015  // | 1 | 5 | 9 | 13 |
1016  // | 2 | 6 | 10 | 14 |
1017  // | 3 | 7 | 11 | 15 |
1018 
1019  // | a | b | c | d |
1020  // | e | f | g | h |
1021  // | i | l | m | n |
1022  // | o | p | q | r |
1023 
1024  const T_Scalar a = in[0]; const T_Scalar b = in[4]; const T_Scalar c = in[ 8]; const T_Scalar d = in[12];
1025  const T_Scalar e = in[1]; const T_Scalar f = in[5]; const T_Scalar g = in[ 9]; const T_Scalar h = in[13];
1026  const T_Scalar i = in[2]; const T_Scalar l = in[6]; const T_Scalar m = in[10]; const T_Scalar n = in[14];
1027  const T_Scalar o = in[3]; const T_Scalar p = in[7]; const T_Scalar q = in[11]; const T_Scalar r = in[15];
1028 
1029  // 3x3 determinant:
1030  //
1031  // [ a b c ]
1032  // [ d e f ] = aei - ahf + dhc - dbi + gbf - gec = (aei + dhc + gbf) - (ahf + dbi + gec)
1033  // [ g h i ]
1034 
1035  const T_Scalar mr = m*r;
1036  const T_Scalar gn = g*n;
1037  const T_Scalar el = e*l;
1038  const T_Scalar ip = i*p;
1039  const T_Scalar mo = m*o;
1040  const T_Scalar hl = h*l;
1041  const T_Scalar mp = m*p;
1042  const T_Scalar nq = n*q;
1043  const T_Scalar gl = g*l;
1044  const T_Scalar no = n*o;
1045  const T_Scalar gi = g*i;
1046  const T_Scalar np = n*p;
1047  const T_Scalar fi = f*i;
1048  const T_Scalar rc = r*c;
1049  const T_Scalar be = b*e;
1050  const T_Scalar af = a*f;
1051  const T_Scalar de = d*e;
1052  const T_Scalar df = d*f;
1053  const T_Scalar ch = c*h;
1054  const T_Scalar qh = q*h;
1055 
1056  // | f | g | h |
1057  // | l | m | n |
1058  // | p | q | r |
1059  T_Scalar Ca = +(( f*mr + gn*p + hl*q ) - ( h*mp + nq*f + r*gl ));
1060 
1061  // | e | g | h |
1062  // | i | m | n |
1063  // | o | q | r |
1064  T_Scalar Cb = -(( e*mr + gn*o + i*qh ) - ( h*mo + gi*r + nq*e ));
1065 
1066  // | e | f | h |
1067  // | i | l | n |
1068  // | o | p | r |
1069  T_Scalar Cc = +(( el*r + ip*h + f*no ) - ( hl*o + np*e + fi*r ));
1070 
1071  // | e | f | g |
1072  // | i | l | m |
1073  // | o | p | q |
1074  T_Scalar Cd = -(( el*q + f*mo + g*ip ) - ( gl*o + mp*e + q*fi ));
1075 
1076  T_Scalar det = a*Ca + b*Cb + c*Cc + d*Cd;
1077 
1078  // singular matrix
1079  if (det == 0)
1080  return det;
1081 
1082  // | b | c | d |
1083  // | l | m | n |
1084  // | p | q | r |
1085  T_Scalar Ce = -(( b*mr + c*np + d*l*q ) - ( d*mp + nq*b + rc*l ));
1086 
1087  // | a | c | d |
1088  // | i | m | n |
1089  // | o | q | r |
1090  T_Scalar Cf = +(( a*mr + c*no + d*i*q ) - ( d*mo + nq*a + rc*i ));
1091 
1092  // | a | b | d |
1093  // | i | l | n |
1094  // | o | p | r |
1095  T_Scalar Cg = -(( a*l*r + b*no + d*ip ) - ( d*l*o + np*a + r*b*i ));
1096 
1097  // | a | b | c |
1098  // | i | l | m |
1099  // | o | p | q |
1100  T_Scalar Ch = +(( a*l*q + b*mo + c*ip ) - ( c*l*o + mp*a + q*b*i ));
1101 
1102 
1103  // | b | c | d |
1104  // | f | g | h |
1105  // | p | q | r |
1106  T_Scalar Ci = +(( b*g*r + ch*p + df*q ) - ( d*g*p + q*h*b + rc*f ));
1107 
1108  // | a | c | d |
1109  // | e | g | h |
1110  // | o | q | r |
1111  T_Scalar Cl = -(( a*g*r + ch*o + de*q ) - ( d*g*o + qh*a + rc*e ));
1112 
1113  // | a | b | d |
1114  // | e | f | h |
1115  // | o | p | r |
1116  T_Scalar Cm = +(( af*r + b*h*o + de*p ) - ( df*o + h*p*a + r*be ));
1117 
1118  // | a | b | c |
1119  // | e | f | g |
1120  // | o | p | q |
1121  T_Scalar Cn = -(( af*q + b*g*o + c*e*p ) - ( c*f*o + g*p*a + q*be ));
1122 
1123 
1124  // | b | c | d |
1125  // | f | g | h |
1126  // | l | m | n |
1127  T_Scalar Co = -(( b*gn + c*hl + df*m ) - ( d*gl + h*m*b + n*c*f ));
1128 
1129  // | a | c | d |
1130  // | e | g | h |
1131  // | i | m | n |
1132  T_Scalar Cp = +(( a*gn + ch*i + de*m ) - ( d*gi + h*m*a + n*c*e ));
1133 
1134  // | a | b | d |
1135  // | e | f | h |
1136  // | i | l | n |
1137  T_Scalar Cq = -(( af*n + b*h*i + d*el ) - ( d*fi + hl*a + n*be ));
1138 
1139  // | a | b | c |
1140  // | e | f | g |
1141  // | i | l | m |
1142  T_Scalar Cr = +(( af*m + b*gi + c*el ) - ( c*fi + gl*a + m*be ));
1143 
1144 #if 0
1145  T_Scalar det2 = e*Ce + f*Cf + g*Cg + h*Ch;
1146  T_Scalar det3 = i*Ci + l*Cl + m*Cm + n*Cn;
1147  T_Scalar det4 = o*Co + p*Cp + q*Cq + r*Cr;
1148  VL_CHECK( fabs(det - det1) < 0.0001 );
1149  VL_CHECK( fabs(det - det3) < 0.0001 );
1150  VL_CHECK( fabs(det - det4) < 0.0001 );
1151 #endif
1152 
1153  T_Scalar inv_det = 1 / det;
1154 
1155  out[0] = inv_det * Ca;
1156  out[1] = inv_det * Cb;
1157  out[2] = inv_det * Cc;
1158  out[3] = inv_det * Cd;
1159  out[4] = inv_det * Ce;
1160  out[5] = inv_det * Cf;
1161  out[6] = inv_det * Cg;
1162  out[7] = inv_det * Ch;
1163  out[8] = inv_det * Ci;
1164  out[9] = inv_det * Cl;
1165  out[10] = inv_det * Cm;
1166  out[11] = inv_det * Cn;
1167  out[12] = inv_det * Co;
1168  out[13] = inv_det * Cp;
1169  out[14] = inv_det * Cq;
1170  out[15] = inv_det * Cr;
1171 
1172  return det;
1173  }
1174  //-----------------------------------------------------------------------------
1175  template<typename T_Scalar>
1177  {
1178  Vector3<T_Scalar> a,b;
1179  a = from;
1180  b = to;
1181  a.normalize();
1182  b.normalize();
1183  T_Scalar cosa = dot(a,b);
1184  cosa = clamp(cosa,-((T_Scalar)1),+((T_Scalar)1));
1185  Vector3<T_Scalar> axis,n2;
1186  axis = cross(a,b);
1187  axis.normalize();
1188  T_Scalar alpha = acos(cosa);
1189  return getRotation(out, alpha*(T_Scalar)dRAD_TO_DEG, axis.x(), axis.y(), axis.z());
1190  }
1191  //-----------------------------------------------------------------------------
1198  template<typename T_Scalar>
1199  void Matrix4<T_Scalar>::getYXRotationAngles(T_Scalar& degrees_y, T_Scalar& degrees_x) const
1200  {
1201  Vector3<T_Scalar> vx = getX();
1202  Vector3<T_Scalar> vy = getY();
1203  Vector3<T_Scalar> vz = getZ();
1204 
1205  vx.normalize();
1206  vy.normalize();
1207  vz.normalize();
1208 
1209  T_Scalar kx = dot(vy,Vector3<T_Scalar>(0,1,0));
1210  kx = clamp(kx,-((T_Scalar)1),+((T_Scalar)1));
1211  degrees_x = acos(kx) * (T_Scalar)dRAD_TO_DEG;
1212  if(dot(vz, Vector3<T_Scalar>(0,1,0)) > 0)
1213  degrees_x = -degrees_x;
1214 
1215  T_Scalar ky = dot(vx, Vector3<T_Scalar>(1,0,0));
1216  ky = clamp(ky,-((T_Scalar)1),+((T_Scalar)1));
1217  degrees_y = acos(ky) * (T_Scalar)dRAD_TO_DEG;
1218  if(dot(vz, Vector3<T_Scalar>(1,0,0)) < 0)
1219  degrees_y = -degrees_y;
1220  if (fabs(degrees_x) > (T_Scalar)90)
1221  degrees_y = -degrees_y;
1222  }
1223 
1224  //-----------------------------------------------------------------------------
1225 
1234 
1235  #if VL_PIPELINE_PRECISION == 2
1236  typedef dmat4 mat4;
1238  #else
1239  typedef fmat4 mat4;
1241  #endif
1242 }
1243 
1244 #endif
const T_Scalar & z() const
Definition: Vector4.hpp:103
float clamp(float x, float minval, float maxval)
Definition: Vector2.hpp:315
Matrix4 & operator-=(const Matrix4 &m)
Definition: Matrix4.hpp:212
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:227
Matrix4 & getTransposed(Matrix4 &dest) const
Definition: Matrix4.hpp:372
Matrix4 & rotate(T_Scalar degrees, const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:490
const T_Scalar & e(int i, int j) const
Definition: Matrix4.hpp:660
const T_Scalar & x() const
Definition: Vector4.hpp:101
Matrix4 getTransposed() const
Definition: Matrix4.hpp:363
T sqrt(T a)
Definition: glsl_math.hpp:592
Matrix4< unsigned int > umat4
A 4x4 matrix using unsigned int precision.
Definition: Matrix4.hpp:1233
Vector3< T_Scalar > getZ() const
Definition: Matrix4.hpp:126
static Matrix4 & getTranslation(Matrix4 &out, T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:565
Matrix4 & rotateZYX(T_Scalar degZ, T_Scalar degY, T_Scalar degX)
Definition: Matrix4.hpp:525
Matrix4(T_Scalar e00, T_Scalar e01, T_Scalar e02, T_Scalar e03, T_Scalar e10, T_Scalar e11, T_Scalar e12, T_Scalar e13, T_Scalar e20, T_Scalar e21, T_Scalar e22, T_Scalar e23, T_Scalar e30, T_Scalar e31, T_Scalar e32, T_Scalar e33)
Definition: Matrix4.hpp:78
static Matrix4 getRotation(T_Scalar degrees, T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:469
Matrix4 & setT(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:160
Matrix4 & setX(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:136
static Matrix4 getPerspective(T_Scalar fovy, T_Scalar aspect_ratio, T_Scalar znear, T_Scalar zfar)
Definition: Matrix4.hpp:825
Matrix4 & rotate(const Vector4< T_Scalar > &from, const Vector4< T_Scalar > &to)
Definition: Matrix4.hpp:538
Matrix4< double > dmat4
A 4x4 matrix using double precision.
Definition: Matrix4.hpp:1227
Matrix4 & transpose()
Definition: Matrix4.hpp:350
T sin(T a)
Definition: glsl_math.hpp:199
T degrees(T radians)
Definition: glsl_math.hpp:173
const T_Scalar & z() const
Definition: Vector3.hpp:91
Matrix4 & translate(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:579
Matrix4(const Matrix4< T > &m)
Definition: Matrix4.hpp:54
Matrix4 & translate(T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:574
Matrix4(T_Scalar *val)
Definition: Matrix4.hpp:73
bool isNull() const
Definition: Matrix4.hpp:380
T_Scalar scalar_type
Definition: Matrix4.hpp:51
Matrix4 & scale(const Vector3< T_Scalar > v)
Definition: Matrix4.hpp:615
Matrix4 operator-() const
Definition: Matrix4.hpp:225
Vector3< T_Scalar > getY() const
Definition: Matrix4.hpp:121
static Matrix4 getTranslation(T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:559
const T_Scalar & e(int i, int j) const
Definition: Matrix3.hpp:517
static Matrix4 getOrtho(T_Scalar pleft, T_Scalar pright, T_Scalar pbottom, T_Scalar ptop, T_Scalar pnear, T_Scalar pfar)
Definition: Matrix4.hpp:870
const double dRAD_TO_DEG
Constant to convert radian into degree using double precision.
Definition: std_types.hpp:68
The Vector4 class is a template class that implements a generic 4 components vector, see also vl::fvec4, vl::dvec4, vl::uvec4, vl::ivec4, vl::svec4, vl::usvec4, vl::bvec4, vl::ubvec4.
Definition: Vector4.hpp:44
Matrix4 & postMultiply(const Matrix4 &m)
Definition: Matrix4.hpp:647
Matrix4 & invert(T_Scalar *determinant=NULL)
Definition: Matrix4.hpp:441
fvec3 cross(const fvec3 &v1, const fvec3 &v2)
Definition: Vector3.hpp:277
static Matrix4 getIdentity()
Definition: Matrix4.hpp:419
Matrix4 & operator+=(T_Scalar d)
Definition: Matrix4.hpp:243
Matrix4< int > imat4
A 4x4 matrix using int precision.
Definition: Matrix4.hpp:1231
Matrix4 getInverse(T_Scalar *determinant=NULL) const
Definition: Matrix4.hpp:432
Visualization Library main namespace.
Vector3< T_Scalar > getX() const
Definition: Matrix4.hpp:116
Matrix4 operator/(T_Scalar d) const
Definition: Matrix4.hpp:285
float dot(float a, float b)
Definition: glsl_math.hpp:1111
const double dDEG_TO_RAD
Constant to convert degree into radian using double precision.
Definition: std_types.hpp:66
static Matrix4 getTranslation(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:553
Matrix4(T_Scalar n)
Definition: Matrix4.hpp:67
The Matrix3 class is a template class that implements a generic 3x3 matrix, see also vl::dmat3...
Definition: Matrix3.hpp:48
T_Scalar getInverse(Matrix4 &dest) const
Definition: Matrix4.hpp:1009
Matrix3< T_Scalar > get3x3() const
Definition: Matrix4.hpp:323
Matrix4 operator*(T_Scalar d) const
Definition: Matrix4.hpp:268
static Matrix4 & getIdentity(Matrix4 &out)
Definition: Matrix4.hpp:424
Matrix4 & setZ(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:152
The Vector3 class is a template class that implements a generic 3 components vector, see also vl::fvec3, vl::dvec3, vl::uvec3, vl::ivec3, vl::svec3, vl::usvec3, vl::bvec3, vl::ubvec3.
Definition: Vector3.hpp:44
Matrix4 & operator=(const Matrix4 &m)
Definition: Matrix4.hpp:178
Matrix4 operator-(T_Scalar d) const
Definition: Matrix4.hpp:251
static Matrix4 getLookAtModeling(const Vector3< T_Scalar > &eye, const Vector3< T_Scalar > &at, const Vector3< T_Scalar > &up)
Definition: Matrix4.hpp:764
static Matrix4 getRotation(T_Scalar degrees1, const Vector3< T_Scalar > &v1, T_Scalar degrees2, const Vector3< T_Scalar > &v2, T_Scalar degrees3, const Vector3< T_Scalar > &v3)
Definition: Matrix4.hpp:485
Matrix4 & operator+=(const Matrix4 &m)
Definition: Matrix4.hpp:194
void set3x3(const Matrix3< T_Scalar > &m)
This writes only on the upper 3x3 part of the matrix without touching the last row and column...
Definition: Matrix4.hpp:333
static Matrix4 getRotation(T_Scalar degrees, const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:475
Matrix4 & fill(T_Scalar val)
Definition: Matrix4.hpp:89
static Matrix4 getRotationXYZ(T_Scalar degX, T_Scalar degY, T_Scalar degZ)
Definition: Matrix4.hpp:510
const T_Scalar & y() const
Definition: Vector3.hpp:90
Matrix4 operator+(const Matrix4 &m) const
Definition: Matrix4.hpp:184
static Matrix4 getNull()
Definition: Matrix4.hpp:401
Matrix4 & rotate(T_Scalar degrees1, const Vector3< T_Scalar > &v1, T_Scalar degrees2, const Vector3< T_Scalar > &v2)
Definition: Matrix4.hpp:500
const T_Scalar & y() const
Definition: Vector4.hpp:102
static Matrix4 & multiply(Matrix4 &out, const Matrix4 &p, const Matrix4 &q)
Definition: Matrix4.hpp:620
Matrix4 operator-(const Matrix4 &m) const
Definition: Matrix4.hpp:202
Matrix4 & operator/=(T_Scalar d)
Definition: Matrix4.hpp:295
Matrix4 & scale(T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:610
bool isIdentity() const
Definition: Matrix4.hpp:304
The Matrix4 class is a template class that implements a generic 4x4 matrix, see also vl::dmat4...
Definition: Matrix4.hpp:48
Matrix4 & fillPtr(T_Scalar *val)
Definition: Matrix4.hpp:98
#define NULL
Definition: OpenGLDefs.hpp:81
const T_Scalar * ptr() const
Definition: Matrix4.hpp:345
Matrix4 & setY(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:144
Vector4< T_Scalar > mVec[4]
Definition: Matrix4.hpp:668
void getYXRotationAngles(T_Scalar &degrees_y, T_Scalar &degrees_x) const
If this matrix can be represented as RY(degrees_y) * RX(degrees_x), where RX and RY are getRotation m...
Definition: Matrix4.hpp:1199
static Matrix4 & getNull(Matrix4 &out)
Definition: Matrix4.hpp:395
static Matrix4 & getRotation(Matrix4 &out, T_Scalar degrees, T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:904
Matrix4 & rotate(T_Scalar degrees, T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:495
The Vector2 class is a template class that implements a generic 2 components vector, see also vl::fvec2, vl::dvec2, vl::uvec2, vl::ivec2, vl::svec2, vl::usvec2, vl::bvec2, vl::ubvec2.
Definition: Vector2.hpp:97
Vector3< T_Scalar > getT() const
Definition: Matrix4.hpp:131
static Matrix4 getFrustum(T_Scalar pleft, T_Scalar pright, T_Scalar pbottom, T_Scalar ptop, T_Scalar pnear, T_Scalar pfar)
Definition: Matrix4.hpp:847
fmat4 mat4
Defined as: &#39;typedef fmat4 mat4&#39;. See also VL_PIPELINE_PRECISION.
Definition: Matrix4.hpp:1240
Matrix4 & setIdentity()
Definition: Matrix4.hpp:406
T_Scalar diff(const Matrix4 &other) const
Definition: Matrix4.hpp:104
Matrix4 & rotate(const Vector3< T_Scalar > &from, const Vector3< T_Scalar > &to)
Definition: Matrix4.hpp:543
void getAsLookAtModeling(Vector3< T_Scalar > &eye, Vector3< T_Scalar > &at, Vector3< T_Scalar > &up, Vector3< T_Scalar > &right) const
Definition: Matrix4.hpp:802
static Matrix4 & getTranslation(Matrix4 &out, const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:548
T_Scalar * ptr()
Definition: Matrix4.hpp:340
static Matrix4 getScaling(T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:595
static Matrix4 & getScaling(Matrix4 &out, T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Matrix4.hpp:601
Matrix4 & operator-=(T_Scalar d)
Definition: Matrix4.hpp:260
static Matrix4 getScaling(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:589
static Matrix4 getOrtho2D(T_Scalar pleft, T_Scalar pright, T_Scalar pbottom, T_Scalar ptop)
Definition: Matrix4.hpp:898
Matrix4 & preMultiply(const Matrix4 &m)
Definition: Matrix4.hpp:653
T cos(T a)
Definition: glsl_math.hpp:225
Matrix4 & setNull()
Definition: Matrix4.hpp:389
Matrix4 & operator*=(T_Scalar d)
Definition: Matrix4.hpp:277
static Matrix4 & getScaling(Matrix4 &out, const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:584
const T_Scalar & x() const
Definition: Vector3.hpp:89
const T_Scalar & x() const
Definition: Vector2.hpp:132
Matrix4 & rotateXYZ(T_Scalar degX, T_Scalar degY, T_Scalar degZ)
Definition: Matrix4.hpp:515
Matrix4< float > fmat4
A 4x4 matrix using float precision.
Definition: Matrix4.hpp:1229
Matrix4 & operator*=(const Matrix4 &m)
Definition: Matrix4.hpp:220
T acos(T a)
Definition: glsl_math.hpp:329
static Matrix4 getRotation(const Vector3< T_Scalar > &from, const Vector3< T_Scalar > &to)
Definition: Matrix4.hpp:532
void getAsLookAt(Vector3< T_Scalar > &eye, Vector3< T_Scalar > &at, Vector3< T_Scalar > &up, Vector3< T_Scalar > &right) const
Definition: Matrix4.hpp:817
Matrix4 operator+(T_Scalar d) const
Definition: Matrix4.hpp:234
Matrix4 as3x3() const
Definition: Matrix4.hpp:310
Matrix4 & rotate(T_Scalar degrees1, const Vector3< T_Scalar > &v1, T_Scalar degrees2, const Vector3< T_Scalar > &v2, T_Scalar degrees3, const Vector3< T_Scalar > &v3)
Definition: Matrix4.hpp:505
const T_Scalar & y() const
Definition: Vector2.hpp:133
T_Scalar & e(int i, int j)
Definition: Matrix4.hpp:661
static Matrix4 getLookAt(const Vector3< T_Scalar > &eye, const Vector3< T_Scalar > &at, const Vector3< T_Scalar > &up)
Definition: Matrix4.hpp:783
#define VL_CHECK(expr)
Definition: checks.hpp:73
static Matrix4 getRotation(T_Scalar degrees1, const Vector3< T_Scalar > &v1, T_Scalar degrees2, const Vector3< T_Scalar > &v2)
Definition: Matrix4.hpp:480
bool operator!=(const Matrix4 &m) const
Definition: Matrix4.hpp:173
bool operator==(const Matrix4 &m) const
Definition: Matrix4.hpp:168
static Matrix4 getRotationZYX(T_Scalar degZ, T_Scalar degY, T_Scalar degX)
Definition: Matrix4.hpp:520
T normalize(T)
Definition: glsl_math.hpp:1128
const T_Scalar & w() const
Definition: Vector4.hpp:104