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]
Matrix2.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 Matrix2_INCLUDE_ONCE
33 #define Matrix2_INCLUDE_ONCE
34 
35 #include <vlCore/checks.hpp>
36 #include <vlCore/Vector2.hpp>
37 #include <cstring> // memcpy
38 
39 namespace vl
40 {
41  //-----------------------------------------------------------------------------
42  // Matrix2
43  //-----------------------------------------------------------------------------
48  template<typename T_Scalar>
49  class Matrix2
50  {
51  public:
52  typedef T_Scalar scalar_type;
53  //-----------------------------------------------------------------------------
54  template<typename T>
55  explicit Matrix2(const Matrix2<T>& m)
56  {
57  e(0,0) = (T_Scalar)m.e(0,0); e(1,0) = (T_Scalar)m.e(1,0);
58  e(0,1) = (T_Scalar)m.e(0,1); e(1,1) = (T_Scalar)m.e(1,1);
59  }
60  //-----------------------------------------------------------------------------
62  {
63  setIdentity();
64  }
65  //-----------------------------------------------------------------------------
66  explicit Matrix2(T_Scalar n)
67  {
68  setIdentity();
69  e(0,0) = e(1,1) = n;
70  }
71  //-----------------------------------------------------------------------------
72  explicit Matrix2(T_Scalar e00, T_Scalar e01,
73  T_Scalar e10, T_Scalar e11 )
74  {
75  e(0,0) = e00; e(0,1) = e01;
76  e(1,0) = e10; e(1,1) = e11;
77  }
78  //-----------------------------------------------------------------------------
79  Matrix2& fill(T_Scalar val)
80  {
81  e(0,0) = e(1,0) =
82  e(0,1) = e(1,1) = val;
83  return *this;
84  }
85  //-----------------------------------------------------------------------------
86  T_Scalar diff(const Matrix2& other) const
87  {
88  T_Scalar err = 0;
89  for(int i=0; i<2; ++i)
90  for(int j=0; j<2; ++j)
91  if (e(j,i) > other.e(j,i)) // avoid fabs/abs
92  err += e(j,i) - other.e(j,i);
93  else
94  err += other.e(j,i) - e(j,i);
95  return err;
96  }
97  //-----------------------------------------------------------------------------
98  bool operator==(const Matrix2& m) const
99  {
100  return memcmp(m.mVec, mVec, sizeof(T_Scalar)*4) == 0;
101  }
102  //-----------------------------------------------------------------------------
103  bool operator!=(const Matrix2& m) const
104  {
105  return !operator==(m);
106  }
107  //-----------------------------------------------------------------------------
109  {
110  memcpy(mVec, m.mVec, sizeof(T_Scalar)*4);
111  return *this;
112  }
113  //-----------------------------------------------------------------------------
114  Matrix2 operator+(const Matrix2& m) const
115  {
116  Matrix2 t;
117  for(int i=0; i<2; ++i)
118  for(int j=0; j<2; ++j)
119  t.e(j,i) = e(j,i) + m.e(j,i);
120  return t;
121  }
122  //-----------------------------------------------------------------------------
124  {
125  for(int i=0; i<2; ++i)
126  for(int j=0; j<2; ++j)
127  e(j,i) += m.e(j,i);
128  return *this;
129  }
130  //-----------------------------------------------------------------------------
131  Matrix2 operator-(const Matrix2& m) const
132  {
133  Matrix2 t;
134  for(int i=0; i<2; ++i)
135  for(int j=0; j<2; ++j)
136  t.e(j,i) = e(j,i) - m.e(j,i);
137  return t;
138  }
139  //-----------------------------------------------------------------------------
141  {
142  for(int i=0; i<2; ++i)
143  for(int j=0; j<2; ++j)
144  e(j,i) -= m.e(j,i);
145  return *this;
146  }
147  //-----------------------------------------------------------------------------
149  {
150  return postMultiply(m);
151  }
152  //-----------------------------------------------------------------------------
154  {
155  Matrix2 t;
156  for(int i=0; i<2; ++i)
157  for(int j=0; j<2; ++j)
158  t.e(j,i) = -e(j,i);
159  return t;
160  }
161  //-----------------------------------------------------------------------------
162  Matrix2 operator+(T_Scalar d) const
163  {
164  Matrix2 t;
165  for(int i=0; i<2; ++i)
166  for(int j=0; j<2; ++j)
167  t.e(j,i) = e(j,i) + d;
168  return t;
169  }
170  //-----------------------------------------------------------------------------
171  Matrix2& operator+=(T_Scalar d)
172  {
173  for(int i=0; i<2; ++i)
174  for(int j=0; j<2; ++j)
175  e(j,i) += d;
176  return *this;
177  }
178  //-----------------------------------------------------------------------------
179  Matrix2 operator-(T_Scalar d) const
180  {
181  Matrix2 t;
182  for(int i=0; i<2; ++i)
183  for(int j=0; j<2; ++j)
184  t.e(j,i) = e(j,i) - d;
185  return t;
186  }
187  //-----------------------------------------------------------------------------
188  Matrix2& operator-=(T_Scalar d)
189  {
190  for(int i=0; i<2; ++i)
191  for(int j=0; j<2; ++j)
192  e(j,i) -= d;
193  return *this;
194  }
195  //-----------------------------------------------------------------------------
196  Matrix2 operator*(T_Scalar d) const
197  {
198  Matrix2 t;
199  for(int i=0; i<2; ++i)
200  for(int j=0; j<2; ++j)
201  t.e(j,i) = e(j,i) * d;
202  return t;
203  }
204  //-----------------------------------------------------------------------------
205  Matrix2& operator*=(T_Scalar d)
206  {
207  for(int i=0; i<2; ++i)
208  for(int j=0; j<2; ++j)
209  e(j,i) *= d;
210  return *this;
211  }
212  //-----------------------------------------------------------------------------
213  Matrix2 operator/(T_Scalar d) const
214  {
215  d = (T_Scalar)1 / d;
216  Matrix2 t;
217  for(int i=0; i<2; ++i)
218  for(int j=0; j<2; ++j)
219  t.e(j,i) = e(j,i) * d;
220  return t;
221  }
222  //-----------------------------------------------------------------------------
223  Matrix2& operator/=(T_Scalar d)
224  {
225  d = (T_Scalar)1 / d;
226  for(int i=0; i<2; ++i)
227  for(int j=0; j<2; ++j)
228  e(j,i) *= d;
229  return *this;
230  }
231  //-----------------------------------------------------------------------------
232  bool isIdentity() const
233  {
234  Matrix2 i;
235  return memcmp(ptr(), i.ptr(), sizeof(T_Scalar)*4) == 0;
236  }
237  //-----------------------------------------------------------------------------
238  T_Scalar* ptr()
239  {
240  return &e(0,0);
241  }
242  //-----------------------------------------------------------------------------
243  const T_Scalar* ptr() const
244  {
245  return &e(0,0);
246  }
247  //-----------------------------------------------------------------------------
249  {
250  T_Scalar tmp;
251  for(int i=0; i<2; ++i)
252  for(int j=i; j<2; ++j)
253  {
254  tmp = e(j,i);
255  e(j,i) = e(i,j);
256  e(i,j) = tmp;
257  }
258  return *this;
259  }
260  //-----------------------------------------------------------------------------
262  {
263  Matrix2 m;
264  for(int i=0; i<2; ++i)
265  for(int j=0; j<2; ++j)
266  m.e(j,i) = e(i,j);
267  return m;
268  }
269  //-----------------------------------------------------------------------------
271  {
272  for(int i=0; i<2; ++i)
273  for(int j=0; j<2; ++j)
274  dest.e(j,i) = e(i,j);
275  return dest;
276  }
277  //-----------------------------------------------------------------------------
278  bool isNull() const
279  {
280  for(int i=0; i<2; ++i)
281  for(int j=0; j<2; ++j)
282  if(mVec[j][i] != 0)
283  return false;
284  return true;
285  }
286  //-----------------------------------------------------------------------------
288  {
289  fill(0);
290  return *this;
291  }
292  //-----------------------------------------------------------------------------
293  static Matrix2& getNull(Matrix2& out)
294  {
295  out.fill(0);
296  return out;
297  }
298  //-----------------------------------------------------------------------------
299  static Matrix2 getNull()
300  {
301  return Matrix2().fill(0);
302  }
303  //-----------------------------------------------------------------------------
305  {
306  static const T_Scalar I2d[] =
307  {
308  (T_Scalar)1, (T_Scalar)0,
309  (T_Scalar)0, (T_Scalar)1
310  };
311  memcpy(mVec, I2d, sizeof(T_Scalar)*4);
312  return *this;
313  }
314  //-----------------------------------------------------------------------------
316  {
317  return Matrix2();
318  }
319  //-----------------------------------------------------------------------------
321  {
322  out.setIdentity();
323  return out;
324  }
325  //-----------------------------------------------------------------------------
326  T_Scalar getInverse(Matrix2& dest) const
327  {
328  if (&dest == this)
329  {
330  Matrix2 tmp;
331  T_Scalar det = getInverse(tmp);
332  dest = tmp;
333  return det;
334  }
335  else
336  {
337  const T_Scalar& a11 = e(0,0);
338  const T_Scalar& a12 = e(1,0);
339  const T_Scalar& a21 = e(0,1);
340  const T_Scalar& a22 = e(1,1);
341 
342  dest.fill(0);
343 
344  T_Scalar det = a11*a22-a12*a21;
345 
346  if (det != 0)
347  dest = Matrix2(+a22, -a12, -a21, +a11) / det;
348 
349  return det;
350  }
351  }
352  //-----------------------------------------------------------------------------
353  Matrix2 getInverse(T_Scalar *determinant=NULL) const
354  {
355  Matrix2 tmp;
356  T_Scalar det = getInverse(tmp);
357  if (determinant)
358  *determinant = det;
359  return tmp;
360  }
361  //-----------------------------------------------------------------------------
362  Matrix2& invert(T_Scalar *determinant=NULL)
363  {
364  T_Scalar det = getInverse(*this);
365  if (determinant)
366  *determinant = det;
367  return *this;
368  }
369  //-----------------------------------------------------------------------------
370  static Matrix2& multiply(Matrix2& out, const Matrix2& p, const Matrix2& q)
371  {
372  VL_CHECK(out.ptr() != p.ptr() && out.ptr() != q.ptr());
373 
374  out.e(0,0) = q.e(0,0)*p.e(0,0) + q.e(1,0)*p.e(0,1);
375  out.e(0,1) = q.e(0,1)*p.e(0,0) + q.e(1,1)*p.e(0,1);
376 
377  out.e(1,0) = q.e(0,0)*p.e(1,0) + q.e(1,0)*p.e(1,1);
378  out.e(1,1) = q.e(0,1)*p.e(1,0) + q.e(1,1)*p.e(1,1);
379 
380  return out;
381  }
382  //-----------------------------------------------------------------------------
384  {
386  return *this = multiply(t, *this, m);
387  }
388  //-----------------------------------------------------------------------------
390  {
392  return *this = multiply(t, m, *this);
393  }
394  //-----------------------------------------------------------------------------
395 
396  const T_Scalar& e(int i, int j) const { return mVec[j][i]; }
397  T_Scalar& e(int i, int j) { return mVec[j][i]; }
398 
399  private:
400  const Vector2<T_Scalar>& operator[](unsigned int i) const { VL_CHECK(i<2); return mVec[i]; }
401  Vector2<T_Scalar>& operator[](unsigned int i) { VL_CHECK(i<2); return mVec[i]; }
402 
403  protected:
405  };
406 
407  //-----------------------------------------------------------------------------
408  // OPERATORS
409  //-----------------------------------------------------------------------------
410  template<typename T_Scalar>
412  {
415  return t;
416  }
417  //-----------------------------------------------------------------------------
418  template<typename T_Scalar>
419  inline Matrix2<T_Scalar> operator+(T_Scalar d, const Matrix2<T_Scalar>& m)
420  {
421  return m + d;
422  }
423  //-----------------------------------------------------------------------------
424  template<typename T_Scalar>
425  inline Matrix2<T_Scalar> operator*(T_Scalar d, const Matrix2<T_Scalar>& m)
426  {
427  return m * d;
428  }
429  //-----------------------------------------------------------------------------
430  // post multiplication: matrix * column vector
431  template<typename T_Scalar>
433  {
435  t.x() = v.x()*m.e(0,0) + v.y()*m.e(0,1);
436  t.y() = v.x()*m.e(1,0) + v.y()*m.e(1,1);
437  return t;
438  }
439  //-----------------------------------------------------------------------------
440  // pre-multiplication: row vector * matrix
441  template<typename T_Scalar>
443  {
445  t.x() = v.x()*m.e(0,0) + v.y()*m.e(1,0);
446  t.y() = v.x()*m.e(0,1) + v.y()*m.e(1,1);
447  return t;
448  }
449  //-----------------------------------------------------------------------------
450 
459 
460  #if VL_PIPELINE_PRECISION == 2
461  typedef dmat2 mat2;
463  #else
464  typedef fmat2 mat2;
466  #endif
467 }
468 
469 #endif
bool operator!=(const Matrix2 &m) const
Definition: Matrix2.hpp:103
T_Scalar diff(const Matrix2 &other) const
Definition: Matrix2.hpp:86
Matrix2 operator/(T_Scalar d) const
Definition: Matrix2.hpp:213
Matrix2< double > dmat2
A 2x2 matrix using double precision.
Definition: Matrix2.hpp:452
Matrix2 & setNull()
Definition: Matrix2.hpp:287
bool isNull() const
Definition: Matrix2.hpp:278
static Matrix2 & getNull(Matrix2 &out)
Definition: Matrix2.hpp:293
Matrix2(const Matrix2< T > &m)
Definition: Matrix2.hpp:55
T_Scalar * ptr()
Definition: Matrix2.hpp:238
Matrix2 & operator*=(T_Scalar d)
Definition: Matrix2.hpp:205
static Matrix2 & multiply(Matrix2 &out, const Matrix2 &p, const Matrix2 &q)
Definition: Matrix2.hpp:370
Matrix2 operator+(const Matrix2 &m) const
Definition: Matrix2.hpp:114
Matrix2 & invert(T_Scalar *determinant=NULL)
Definition: Matrix2.hpp:362
Matrix2 & operator/=(T_Scalar d)
Definition: Matrix2.hpp:223
The Matrix2 class is a template class that implements a generic 2x2 matrix, see also vl::dmat2...
Definition: Matrix2.hpp:49
Matrix2 & operator+=(T_Scalar d)
Definition: Matrix2.hpp:171
Matrix2 operator+(T_Scalar d) const
Definition: Matrix2.hpp:162
bool isIdentity() const
Definition: Matrix2.hpp:232
Matrix2 & setIdentity()
Definition: Matrix2.hpp:304
Matrix2 & operator=(const Matrix2 &m)
Definition: Matrix2.hpp:108
Matrix2 operator-(const Matrix2 &m) const
Definition: Matrix2.hpp:131
Matrix2< int > imat2
A 2x2 matrix using int precision.
Definition: Matrix2.hpp:456
Vector2< T_Scalar > mVec[2]
Definition: Matrix2.hpp:404
Visualization Library main namespace.
Matrix2 & operator-=(const Matrix2 &m)
Definition: Matrix2.hpp:140
Matrix2 & operator-=(T_Scalar d)
Definition: Matrix2.hpp:188
const T_Scalar * ptr() const
Definition: Matrix2.hpp:243
Matrix2< unsigned int > umat2
A 2x2 matrix using unsigned int precision.
Definition: Matrix2.hpp:458
Matrix2 operator*(T_Scalar d) const
Definition: Matrix2.hpp:196
Matrix2< float > fmat2
A 2x2 matrix using float precision.
Definition: Matrix2.hpp:454
Matrix2 & transpose()
Definition: Matrix2.hpp:248
Matrix2(T_Scalar e00, T_Scalar e01, T_Scalar e10, T_Scalar e11)
Definition: Matrix2.hpp:72
fmat2 mat2
Defined as: &#39;typedef fmat2 mat2&#39;. See also VL_PIPELINE_PRECISION.
Definition: Matrix2.hpp:465
Matrix2 & preMultiply(const Matrix2 &m)
Definition: Matrix2.hpp:389
Matrix2 & fill(T_Scalar val)
Definition: Matrix2.hpp:79
const T_Scalar & e(int i, int j) const
Definition: Matrix2.hpp:396
bool operator==(const Matrix2 &m) const
Definition: Matrix2.hpp:98
Matrix2 getInverse(T_Scalar *determinant=NULL) const
Definition: Matrix2.hpp:353
T_Scalar & e(int i, int j)
Definition: Matrix2.hpp:397
#define NULL
Definition: OpenGLDefs.hpp:81
T_Scalar getInverse(Matrix2 &dest) const
Definition: Matrix2.hpp:326
static Matrix2 getIdentity()
Definition: Matrix2.hpp:315
Matrix2 & getTransposed(Matrix2 &dest) const
Definition: Matrix2.hpp:270
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
Matrix2(T_Scalar n)
Definition: Matrix2.hpp:66
Matrix2 operator-() const
Definition: Matrix2.hpp:153
Matrix2 & operator*=(const Matrix2 &m)
Definition: Matrix2.hpp:148
const T_Scalar & x() const
Definition: Vector2.hpp:132
static Matrix2 getNull()
Definition: Matrix2.hpp:299
Matrix2 operator-(T_Scalar d) const
Definition: Matrix2.hpp:179
Matrix2 & postMultiply(const Matrix2 &m)
Definition: Matrix2.hpp:383
const T_Scalar & y() const
Definition: Vector2.hpp:133
#define VL_CHECK(expr)
Definition: checks.hpp:73
Matrix2 & operator+=(const Matrix2 &m)
Definition: Matrix2.hpp:123
T_Scalar scalar_type
Definition: Matrix2.hpp:52
Matrix2 getTransposed() const
Definition: Matrix2.hpp:261
static Matrix2 & getIdentity(Matrix2 &out)
Definition: Matrix2.hpp:320