Wm4GMatrix.h

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.0 (2006/06/28)
00016 
00017 #ifndef WM4GMATRIX_H
00018 #define WM4GMATRIX_H
00019 
00020 // Matrix operations are applied on the left.  For example, given a matrix M
00021 // and a vector V, matrix-times-vector is M*V.  That is, V is treated as a
00022 // column vector.  Some graphics APIs use V*M where V is treated as a row
00023 // vector.  In this context the "M" matrix is really a transpose of the M as
00024 // represented in Wild Magic.  Similarly, to apply two matrix operations M0
00025 // and M1, in that order, you compute M1*M0 so that the transform of a vector
00026 // is (M1*M0)*V = M1*(M0*V).  Some graphics APIs use M0*M1, but again these
00027 // matrices are the transpose of those as represented in Wild Magic.  You
00028 // must therefore be careful about how you interface the transformation code
00029 // with graphics APIS.
00030 //
00031 // Matrices are stored in row-major order, matrix[row][col].
00032 
00033 #include "Wm4FoundationLIB.h"
00034 #include "Wm4GVector.h"
00035 
00036 namespace Wm4
00037 {
00038 
00039 template <class Real>
00040 class GMatrix
00041 {
00042 public:
00043     // construction and destruction
00044     GMatrix (int iRows = 0, int iCols = 0);
00045     GMatrix (int iRows, int iCols, const Real* afData);
00046     GMatrix (int iRows, int iCols, const Real** aafEntry);
00047     GMatrix (const GMatrix& rkM);
00048     ~GMatrix ();
00049 
00050     // member access
00051     void SetSize (int iRows, int iCols);
00052     void GetSize (int& riRows, int& riCols) const;
00053     int GetRows () const;
00054     int GetColumns () const;
00055     int GetQuantity () const;
00056     operator const Real* () const;
00057     operator Real* ();
00058     const Real* operator[] (int iRow) const;
00059     Real* operator[] (int iRow);
00060     void SwapRows (int iRow0, int iRow1);
00061     Real operator() (int iRow, int iCol) const;
00062     Real& operator() (int iRow, int iCol);
00063     void SetRow (int iRow, const GVector<Real>& rkV);
00064     GVector<Real> GetRow (int iRow) const;
00065     void SetColumn (int iCol, const GVector<Real>& rkV);
00066     GVector<Real> GetColumn (int iCol) const;
00067     void SetMatrix (int iRows, int iCols, const Real* afEntry);
00068     void SetMatrix (int iRows, int iCols, const Real** aafMatrix);
00069     void GetColumnMajor (Real* afCMajor) const;
00070 
00071     // assignment
00072     GMatrix& operator= (const GMatrix& rkM);
00073 
00074     // comparison
00075     bool operator== (const GMatrix& rkM) const;
00076     bool operator!= (const GMatrix& rkM) const;
00077     bool operator<  (const GMatrix& rkM) const;
00078     bool operator<= (const GMatrix& rkM) const;
00079     bool operator>  (const GMatrix& rkM) const;
00080     bool operator>= (const GMatrix& rkM) const;
00081 
00082     // arithmetic operations
00083     GMatrix operator+ (const GMatrix& rkM) const;
00084     GMatrix operator- (const GMatrix& rkM) const;
00085     GMatrix operator* (const GMatrix& rkM) const;
00086     GMatrix operator* (Real fScalar) const;
00087     GMatrix operator/ (Real fScalar) const;
00088     GMatrix operator- () const;
00089 
00090     // arithmetic updates
00091     GMatrix& operator+= (const GMatrix& rkM);
00092     GMatrix& operator-= (const GMatrix& rkM);
00093     GMatrix& operator*= (Real fScalar);
00094     GMatrix& operator/= (Real fScalar);
00095 
00096     // matrix products
00097     GMatrix Transpose () const;  // M^T
00098     GMatrix TransposeTimes (const GMatrix& rkM) const;  // this^T * M
00099     GMatrix TimesTranspose (const GMatrix& rkM) const;  // this * M^T
00100 
00101     // matrix-vector operations
00102     GVector<Real> operator* (const GVector<Real>& rkV) const;  // M * v
00103     Real QForm (const GVector<Real>& rkU, const GVector<Real>& rkV)
00104         const;  // u^T*M*v
00105 
00106     // Inversion.  The matrix must be square.  The function returns true
00107     // whenever the matrix is square and invertible.
00108     bool GetInverse (GMatrix<Real>& rkInverse) const;
00109 
00110 protected:
00111     // Support for allocation and deallocation.  The allocation call requires
00112     // m_iRows, m_iCols, and m_iQuantity to have already been correctly
00113     // initialized.
00114     void Allocate (bool bSetToZero);
00115     void Deallocate ();
00116 
00117     // support for comparisons
00118     int CompareArrays (const GMatrix& rkM) const;
00119 
00120     int m_iRows, m_iCols, m_iQuantity;
00121 
00122     // the matrix is stored in row-major form as a 1-dimensional array
00123     Real* m_afData;
00124 
00125     // An array of pointers to the rows of the matrix.  The separation of
00126     // row pointers and actual data supports swapping of rows in linear
00127     // algebraic algorithms such as solving linear systems of equations.
00128     Real** m_aafEntry;
00129 };
00130 
00131 // c * M
00132 template <class Real>
00133 GMatrix<Real> operator* (Real fScalar, const GMatrix<Real>& rkM);
00134 
00135 // v^T * M
00136 template <class Real>
00137 GVector<Real> operator* (const GVector<Real>& rkV, const GMatrix<Real>& rkM);
00138 
00139 } //namespace Wm4
00140 
00141 #include "Wm4GMatrix.inl"
00142 
00143 namespace Wm4
00144 {
00145 typedef GMatrix<float> GMatrixf;
00146 typedef GMatrix<double> GMatrixd;
00147 }
00148 
00149 #endif

Generated on Wed Nov 23 19:01:03 2011 for FreeCAD by  doxygen 1.6.1