Back to index

nux  3.0.0
Matrix2.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef MATRIX2_H
00024 #define MATRIX2_H
00025 
00026 
00027 #include "Vector2.h"
00028 
00029 namespace nux
00030 {
00031 
00032   template <typename T>
00033   class Matrix2x2
00034   {
00035   public:
00036     Matrix2x2<T>();
00037     ~Matrix2x2<T>();
00038 
00039     Matrix2x2<T> (const Matrix2x2<T>&);
00040     Matrix2x2<T>&   operator = (const Matrix2x2<T>&);
00041     bool          operator == (const Matrix2x2<T>&);
00042     Matrix2x2<T>    operator * (const Matrix2x2<T>&) const;
00043     Matrix2x2<T>    operator + (const Matrix2x2<T>&) const;
00044     Matrix2x2<T>    operator - (const Matrix2x2<T>&) const;
00045     Matrix2x2<T>&   operator *= (const Matrix2x2<T>&) const;
00046     Matrix2x2<T>&   operator += (const Matrix2x2<T>&) const;
00047     Matrix2x2<T>&   operator -= (const Matrix2x2<T>&) const;
00048 
00049     Matrix2x2<T>    operator * (const T &) const;
00050     Matrix2x2<T>    operator / (const T &) const;
00051     Matrix2x2<T>&   operator *= (const T &) const;
00052     Matrix2x2<T>&   operator /= (const T &) const;
00053 
00054     Vec2<T>         operator * (const Vec2<T>&) const;
00055     Matrix2x2<T>    operator - ();
00056 
00057     // Get the (i, j) element of the current matrix.
00058     T &operator() (unsigned int i, unsigned int j);
00059     T operator () (unsigned int i, unsigned int j) const;
00060 
00061     void Zero();
00062     void Identity();
00063     T Determinant() const ;
00064     void Inverse();
00065     Matrix2x2<T> GetInverse() const;
00066 
00067     static Matrix2x2<T> IDENTITY();
00068     static Matrix2x2<T> ZERO();
00069 
00070     T m[2][2];
00071   };
00072 
00073 
00074   /***************************************************************************************\
00075   Function:       Matrix2::Matrix2
00076 
00077   Description:    Constructor. Initialize the matrix to identity.
00078 
00079   Parameters:     None.
00080 
00081   Return Value:   None.
00082 
00083   Comments:       None.
00084   \***************************************************************************************/
00085   template <typename T>
00086   Matrix2x2<T>::Matrix2x2()
00087   {
00088     Identity();
00089   }
00090 
00091   template <typename T>
00092   T Matrix2x2<T>::Determinant() const
00093   {
00094     T det;
00095     det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
00096     return det;
00097   }
00098 
00099   template <typename T>
00100   void Matrix2x2<T>::Inverse()
00101   {
00102     T det = Determinant();
00103 
00104     if (det == T (0) )
00105     {
00106       // Determinant is null. Matrix cannot be inverted.
00107 #ifdef NUX_DEBUG
00108       NUX_HARDWARE_BREAK;
00109 #endif
00110       return;
00111     }
00112 
00113     Matrix2x2<T> Temp;
00114     Temp.m[0][0] = m[1][1];
00115     Temp.m[0][1] = -m[0][1];
00116     Temp.m[1][0] = -m[1][0];
00117     Temp.m[1][1] = m[0][0];
00118 
00119     *this = (T (1) / det) * Temp;
00120   }
00121 
00122   template <typename T>
00123   Matrix2x2<T> Matrix2x2<T>::GetInverse() const
00124   {
00125     Matrix2x2<T> Temp = *this;
00126     Temp.Inverse();
00127     return Temp;
00128   }
00129 
00130   /***************************************************************************************\
00131   Function:       Matrix2x2<T>::~Matrix2
00132 
00133   Description:    Destructor.
00134 
00135   Parameters:     None.
00136 
00137   Return Value:   None.
00138 
00139   Comments:       None.
00140   \***************************************************************************************/
00141   template <typename T>
00142   Matrix2x2<T>::~Matrix2x2()
00143   {
00144 
00145   }
00146 
00147   /***************************************************************************************\
00148   Function:       Matrix2x2<T>::Matrix2
00149 
00150   Description:    Copy constructor.
00151 
00152   Parameters:     - M
00153 
00154   Return Value:   None.
00155 
00156   Comments:       None.
00157   \***************************************************************************************/
00158   template <typename T>
00159   Matrix2x2<T>::Matrix2x2 (const Matrix2x2<T>& M)
00160   {
00161     m[0][0] = M.m[0][0];
00162     m[0][1] = M.m[0][1];
00163     m[1][0] = M.m[1][0];
00164     m[1][1] = M.m[1][1];
00165   }
00166 
00167   /***************************************************************************************\
00168   Function:       Matrix2x2<T>::operator =
00169 
00170   Description:    Assignment operator.
00171 
00172   Parameters:     - M
00173 
00174   Return Value:   Matrix2x2<T>.
00175 
00176   Comments:       None.
00177   \***************************************************************************************/
00178   template <typename T>
00179   Matrix2x2<T>& Matrix2x2<T>::operator = (const Matrix2x2<T>& M)
00180   {
00181     m[0][0] = M.m[0][0];
00182     m[0][1] = M.m[0][1];
00183     m[1][0] = M.m[1][0];
00184     m[1][1] = M.m[1][1];
00185 
00186     return (*this);
00187   }
00188 
00189   template <typename T>
00190   bool Matrix2x2<T>::operator == (const Matrix2x2<T>& M)
00191   {
00192     for (int i = 0; i < 2; i++)
00193       for (int j = 0; j < 2; j++)
00194       {
00195         if (m[i][j] != M.m[i][j])
00196           return false;
00197       }
00198 
00199     return true;
00200   }
00201 
00202   /***************************************************************************************\
00203   Function:       Matrix2x2<T>::operator *
00204 
00205   Description:    Multiply by matrix iM.
00206 
00207   Parameters:     - iM.
00208 
00209   Return Value:   Matrix2x2<T>.
00210 
00211   Comments:       None.
00212   \***************************************************************************************/
00213   template <typename T>
00214   Matrix2x2<T> Matrix2x2<T>::operator * (const Matrix2x2<T>& iM) const
00215   {
00216     Matrix2x2<T> oM;
00217 
00218     oM.m[0][0] = m[0][0] * iM.m[0][0] + m[0][1] * iM.m[1][0];
00219     oM.m[1][0] = m[1][0] * iM.m[0][0] + m[1][1] * iM.m[1][0];
00220 
00221     oM.m[0][1] = m[0][0] * iM.m[0][1] + m[0][1] * iM.m[1][1];
00222     oM.m[1][1] = m[1][0] * iM.m[0][1] + m[1][1] * iM.m[1][1];
00223 
00224     return oM;
00225   }
00226 
00227   /***************************************************************************************\
00228   Function:       Matrix2x2<T>::operator +
00229 
00230   Description:    Add matrix iM.
00231 
00232   Parameters:     - iM
00233 
00234   Return Value:   Matrix2x2<T>.
00235 
00236   Comments:       None.
00237   \***************************************************************************************/
00238   template <typename T>
00239   Matrix2x2<T> Matrix2x2<T>::operator + (const Matrix2x2<T>& iM) const
00240   {
00241     Matrix2x2<T> oM;
00242 
00243     oM.m[0][0] = m[0][0] + iM.m[0][0];
00244     oM.m[0][1] = m[0][1] + iM.m[0][1];
00245     oM.m[1][0] = m[1][0] + iM.m[1][0];
00246     oM.m[1][1] = m[1][1] + iM.m[1][1];
00247 
00248     return oM;
00249   }
00250 
00251   /***************************************************************************************\
00252   Function:       Matrix2x2<T>::operator -
00253 
00254   Description:    Substract matrix iM.
00255 
00256   Parameters:      -iM
00257 
00258   Return Value:   Matrix2x2<T>.
00259 
00260   Comments:       None.
00261   \***************************************************************************************/
00262   template <typename T>
00263   Matrix2x2<T> Matrix2x2<T>::operator - (const Matrix2x2<T>& iM) const
00264   {
00265     Matrix2x2<T> oM;
00266 
00267     oM.m[0][0] = m[0][0] - iM.m[0][0];
00268     oM.m[0][1] = m[0][1] - iM.m[0][1];
00269     oM.m[1][0] = m[1][0] - iM.m[1][0];
00270     oM.m[1][1] = m[1][1] - iM.m[1][1];
00271 
00272     return oM;
00273   }
00274 
00275   /***************************************************************************************\
00276   Function:       Matrix2x2<T>::operator *=
00277 
00278   Description:    Multiply by matrix iM.
00279 
00280   Parameters:     - iM.
00281 
00282   Return Value:   Matrix2x2<T>.
00283 
00284   Comments:       None.
00285   \***************************************************************************************/
00286   template <typename T>
00287   Matrix2x2<T>& Matrix2x2<T>::operator *= (const Matrix2x2<T>& iM) const
00288   {
00289     Matrix2x2<T> oM;
00290 
00291     oM.m[0][0] = m[0][0] * iM.m[0][0] + m[0][1] * iM.m[1][0];
00292     oM.m[1][0] = m[1][0] * iM.m[0][0] + m[1][1] * iM.m[1][0];
00293 
00294     oM.m[0][1] = m[0][0] * iM.m[0][1] + m[0][1] * iM.m[1][1];
00295     oM.m[1][1] = m[1][0] * iM.m[0][1] + m[1][1] * iM.m[1][1];
00296 
00297     *this = oM;
00298     return *this;
00299   }
00300 
00301   /***************************************************************************************\
00302   Function:       Matrix2x2<T>::operator +=
00303 
00304   Description:    Add matrix iM.
00305 
00306   Parameters:     - iM
00307 
00308   Return Value:   Matrix2x2<T>.
00309 
00310   Comments:       None.
00311   \***************************************************************************************/
00312   template <typename T>
00313   Matrix2x2<T>& Matrix2x2<T>::operator += (const Matrix2x2<T>& iM) const
00314   {
00315     Matrix2x2<T> oM;
00316 
00317     oM.m[0][0] = m[0][0] + iM.m[0][0];
00318     oM.m[0][1] = m[0][1] + iM.m[0][1];
00319     oM.m[1][0] = m[1][0] + iM.m[1][0];
00320     oM.m[1][1] = m[1][1] + iM.m[1][1];
00321 
00322     *this = oM;
00323     return *this;
00324   }
00325 
00326   /***************************************************************************************\
00327   Function:       Matrix2x2<T>::operator -=
00328 
00329   Description:    Substract matrix iM.
00330 
00331   Parameters:      -iM
00332 
00333   Return Value:   Matrix2x2<T>.
00334 
00335   Comments:       None.
00336   \***************************************************************************************/
00337   template <typename T>
00338   Matrix2x2<T>& Matrix2x2<T>::operator -= (const Matrix2x2<T>& iM) const
00339   {
00340     Matrix2x2<T> oM;
00341 
00342     oM.m[0][0] = m[0][0] - iM.m[0][0];
00343     oM.m[0][1] = m[0][1] - iM.m[0][1];
00344     oM.m[1][0] = m[1][0] - iM.m[1][0];
00345     oM.m[1][1] = m[1][1] - iM.m[1][1];
00346 
00347     *this = oM;
00348     return *this;
00349   }
00350 
00351   /***************************************************************************************\
00352   Function:       Matrix2x2<T>::operator *=
00353 
00354   Description:    Multiply all elements by f.
00355 
00356   Parameters:     - f.
00357 
00358   Return Value:   Matrix2x2<T>.
00359 
00360   Comments:       None.
00361   \***************************************************************************************/
00362   template <typename T>
00363   Matrix2x2<T>& Matrix2x2<T>::operator *= (const T &f) const
00364   {
00365     Matrix2x2<T> oM;
00366 
00367     oM.m[0][0] = m[0][0] * f;
00368     oM.m[0][1] = m[0][1] * f;
00369     oM.m[1][0] = m[1][0] * f;
00370     oM.m[1][1] = m[1][1] * f;
00371 
00372     *this = oM;
00373     return *this;
00374   }
00375 
00376   /***************************************************************************************\
00377   Function:       Matrix2x2<T>::operator /
00378 
00379   Description:    Divide all elements by f.
00380 
00381   Parameters:     - f
00382 
00383   Return Value:   Matrix2x2<T>.
00384 
00385   Comments:       None.
00386   \***************************************************************************************/
00387   template <typename T>
00388   Matrix2x2<T> Matrix2x2<T>::operator / (const T &f) const
00389   {
00390     Matrix2x2<T> oM;
00391 
00392     oM.m[0][0] = m[0][0] / f;
00393     oM.m[0][1] = m[0][1] / f;
00394     oM.m[1][0] = m[1][0] / f;
00395     oM.m[1][1] = m[1][1] / f;
00396 
00397     return oM;
00398   }
00399 
00400   /***************************************************************************************\
00401   Function:       Matrix2x2<T>::operator /=
00402 
00403   Description:    Divide all elements by f.
00404 
00405   Parameters:     - f
00406 
00407   Return Value:   Matrix2x2<T>.
00408 
00409   Comments:       None.
00410   \***************************************************************************************/
00411   template <typename T>
00412   Matrix2x2<T>& Matrix2x2<T>::operator /= (const T &f) const
00413   {
00414     Matrix2x2<T> oM;
00415 
00416     oM.m[0][0] = m[0][0] / f;
00417     oM.m[0][1] = m[0][1] / f;
00418     oM.m[1][0] = m[1][0] / f;
00419     oM.m[1][1] = m[1][1] / f;
00420 
00421     *this = oM;
00422     return *this;
00423   }
00424 
00425   /***************************************************************************************\
00426   Function:       Matrix2x2<T>::operator *
00427 
00428   Description:    Multiply a matrix by a vector.
00429 
00430   Parameters:     - V
00431 
00432   Return Value:   Vector2.
00433 
00434   Comments:       None.
00435   \***************************************************************************************/
00436   template <typename T>
00437   Vec2<T> Matrix2x2<T>::operator * (const Vec2<T>& V) const
00438   {
00439     Vec2<T> oV;
00440 
00441     oV.x = V.x * m[0][0] + V.y * m[0][1];
00442     oV.y = V.x * m[1][0] + V.y * m[1][1];
00443 
00444     return oV;
00445   }
00446 
00447   /***************************************************************************************\
00448   Function:       Matrix2x2<T>::operator -
00449 
00450   Description:    Negate all elements of the matrix.
00451 
00452   Parameters:     None.
00453 
00454   Return Value:   Matrix2x2<T>.
00455 
00456   Comments:       None.
00457   \***************************************************************************************/
00458   template <typename T>
00459   Matrix2x2<T> Matrix2x2<T>::operator - ()
00460   {
00461     Matrix2x2<T> oM;
00462 
00463     oM.m[0][0] = -m[0][0];
00464     oM.m[0][1] = -m[0][1];
00465     oM.m[1][0] = -m[1][0];
00466     oM.m[1][1] = -m[1][1];
00467 
00468     return oM;
00469   }
00470 
00471   template <typename T>
00472   T &Matrix2x2<T>::operator () (unsigned int i, unsigned int j)
00473   {
00474     return m[i][j];
00475   }
00476 
00477   template <typename T>
00478   T Matrix2x2<T>::operator () (unsigned int i, unsigned int j) const
00479   {
00480     return m[i][j];
00481   }
00482 
00483   /***************************************************************************************\
00484   Function:       Matrix2x2<T>::zero
00485 
00486   Description:    Set the matrix to zero.
00487 
00488   Parameters:     None.
00489 
00490   Return Value:   None.
00491 
00492   Comments:       None.
00493   \***************************************************************************************/
00494   template <typename T>
00495   void Matrix2x2<T>::Zero()
00496   {
00497     m[0][0] = 0.0;
00498     m[0][1] = 0.0;
00499     m[1][0] = 0.0;
00500     m[1][1] = 0.0;
00501 
00502     //memset(m, 0, sizeof(m));
00503   }
00504 
00505   /***************************************************************************************\
00506   Function:       Matrix2x2<T>::identity
00507 
00508   Description:    Set the matrix to identity.
00509 
00510   Parameters:     None.
00511 
00512   Return Value:   None.
00513 
00514   Comments:       None.
00515   \***************************************************************************************/
00516   template <typename T>
00517   void Matrix2x2<T>::Identity()
00518   {
00519     m[0][0] = 1.0;
00520     m[0][1] = 0.0;
00521     m[1][0] = 0.0;
00522     m[1][1] = 1.0;
00523   }
00524 
00525   template <typename T>
00526   Matrix2x2<T> Matrix2x2<T>::IDENTITY()
00527   {
00528     Matrix2x2<T> matrix;
00529     matrix.Identity();
00530     return matrix;
00531   }
00532 
00533   template <typename T>
00534   Matrix2x2<T> Matrix2x2<T>::ZERO()
00535   {
00536     Matrix2x2<T> matrix;
00537     matrix.Zero();
00538     return matrix;
00539   }
00540 
00541   /***************************************************************************************\
00542   Function:       Matrix2x2<T>::operator *
00543 
00544   Description:    Multiply matrix rhs by constant lhs.
00545   Allow "f * matrix" operation.
00546 
00547   Parameters:     None.
00548 
00549   Return Value:   Matrix2x2<T>.
00550 
00551   Comments:       None.
00552   \***************************************************************************************/
00553   template <typename T>
00554   Matrix2x2<T> operator * (const T &lhs, const Matrix2x2<T>& rhs)
00555   {
00556     Matrix2x2<T> oM;
00557 
00558     oM.m[0][0] = rhs.m[0][0] / lhs;
00559     oM.m[0][1] = rhs.m[0][1] / lhs;
00560     oM.m[1][0] = rhs.m[1][0] / lhs;
00561     oM.m[1][1] = rhs.m[1][1] / lhs;
00562 
00563     return oM;
00564   }
00565 
00566 }
00567 
00568 
00569 #endif // MATRIX2_H
00570