ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMatrix.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkMatrix_h
00019 #define __itkMatrix_h
00020 
00021 #include "itkPoint.h"
00022 #include "itkCovariantVector.h"
00023 #include "vnl/vnl_matrix_fixed.h"
00024 #include "vnl/algo/vnl_matrix_inverse.h"
00025 #include "vnl/vnl_transpose.h"
00026 #include "vnl/vnl_matrix.h"
00027 #include "vnl/algo/vnl_determinant.h"
00028 
00029 namespace itk
00030 {
00045 template< class T, unsigned int NRows = 3, unsigned int NColumns = 3 >
00046 class Matrix
00047 {
00048 public:
00050   typedef Matrix Self;
00051 
00053   typedef T ValueType;
00054   typedef T ComponentType;
00055 
00057   itkStaticConstMacro(RowDimensions,    unsigned int, NRows);
00058   itkStaticConstMacro(ColumnDimensions, unsigned int, NColumns);
00060 
00062   typedef vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType;
00063 
00067   typedef Matrix<T, NColumns, NColumns> CompatibleSquareMatrixType;
00068 
00070   Vector< T, NRows > operator *(const Vector< T, NColumns > & vector) const;
00071 
00073   Point< T, NRows > operator *(const Point< T, NColumns > & vector) const;
00074 
00076   CovariantVector< T, NRows >
00077   operator *(const CovariantVector< T, NColumns > & vector) const;
00078 
00080   Self operator *(const CompatibleSquareMatrixType & matrix) const;
00081 
00083   Self operator+(const Self & matrix) const;
00084 
00085   const Self & operator+=(const Self & matrix);
00086 
00088   Self operator-(const Self & matrix) const;
00089 
00090   const Self & operator-=(const Self & matrix);
00091 
00093   vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
00094 
00096   void operator*=(const CompatibleSquareMatrixType & matrix);
00097 
00099   void operator*=(const vnl_matrix< T > & matrix);
00100 
00102   vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
00103 
00105   void operator*=(const T & value)
00106   { m_Matrix *= value; }
00107 
00109   Self operator*(const T & value)
00110   {
00111     Self result(*this);
00112 
00113     result *= value;
00114     return result;
00115   }
00116 
00118   void operator/=(const T & value)
00119   {
00120     m_Matrix /= value;
00121   }
00122 
00124   Self operator/(const T & value)
00125   {
00126     Self result(*this);
00127 
00128     result /= value;
00129     return result;
00130   }
00131 
00133   inline T & operator()(unsigned int row, unsigned int col)
00134   {
00135     return m_Matrix(row, col);
00136   }
00137 
00139   inline const T & operator()(unsigned int row, unsigned int col) const
00140   {
00141     return m_Matrix(row, col);
00142   }
00143 
00145   inline T * operator[](unsigned int i)
00146   {
00147     return m_Matrix[i];
00148   }
00149 
00151   inline const T * operator[](unsigned int i) const
00152   {
00153     return m_Matrix[i];
00154   }
00155 
00157   inline InternalMatrixType & GetVnlMatrix(void)
00158   {
00159     return m_Matrix;
00160   }
00161 
00163   inline const InternalMatrixType & GetVnlMatrix(void) const
00164   {
00165     return m_Matrix;
00166   }
00167 
00169   inline void SetIdentity(void)
00170   {
00171     m_Matrix.set_identity();
00172   }
00173 
00175   inline void Fill(const T & value)
00176   {
00177     m_Matrix.fill(value);
00178   }
00179 
00181   inline const Self & operator=(const vnl_matrix< T > & matrix)
00182   {
00183     m_Matrix = matrix;
00184     return *this;
00185   }
00186 
00188   inline Matrix(const vnl_matrix< T > & matrix)
00189   {
00190     this->operator=(matrix);
00191   }
00192 
00194   inline bool operator==(const Self & matrix) const
00195   {
00196     bool equal = true;
00197 
00198     for ( unsigned int r = 0; r < NRows; r++ )
00199       {
00200       for ( unsigned int c = 0; c < NColumns; c++ )
00201         {
00202         if ( m_Matrix(r, c) != matrix.m_Matrix(r, c) )
00203           {
00204           equal = false;
00205           break;
00206           }
00207         }
00208       }
00209     return equal;
00210   }
00211 
00212   inline bool operator!=(const Self & matrix) const
00213   {
00214     return !this->operator==(matrix);
00215   }
00216 
00217   inline const Self & operator=(const InternalMatrixType & matrix)
00218   {
00219     this->m_Matrix = matrix;
00220     return *this;
00221   }
00222 
00224   inline explicit Matrix(const InternalMatrixType & matrix)
00225   {
00226     this->operator=(matrix);
00227   }
00228 
00230   inline const Self & operator=(const Self & matrix)
00231   {
00232     m_Matrix = matrix.m_Matrix;
00233     return *this;
00234   }
00235 
00237   inline vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
00238   {
00239     if ( vnl_determinant(m_Matrix) == 0.0 )
00240       {
00241       itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
00242       }
00243     vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
00244     return temp;
00245   }
00247 
00249   inline vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
00250   {
00251     return m_Matrix.transpose();
00252   }
00253 
00255   Matrix():m_Matrix(NumericTraits< T >::Zero) {}
00256 
00258   Matrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
00259 private:
00260   InternalMatrixType m_Matrix;
00261 };
00262 
00263 template< class T, unsigned int NRows, unsigned int NColumns >
00264 ITK_EXPORT std::ostream & operator<<(std::ostream & os, const Matrix< T, NRows, NColumns > & v)
00265 {
00266   os << v.GetVnlMatrix();
00267   return os;
00268 }
00269 } // end namespace itk
00270 
00271 // Define instantiation macro for this template.
00272 #define ITK_TEMPLATE_Matrix(_, EXPORT, TypeX, TypeY)      \
00273   namespace itk                                           \
00274   {                                                       \
00275   _( 3 ( class EXPORT Matrix< ITK_TEMPLATE_3 TypeX > ) )  \
00276   namespace Templates                                     \
00277   {                                                       \
00278   typedef Matrix< ITK_TEMPLATE_3 TypeX > Matrix##TypeY; \
00279   }                                                       \
00280   }
00281 
00282 #if ITK_TEMPLATE_EXPLICIT
00283 #include "Templates/itkMatrix+-.h"
00284 #endif
00285 
00286 #if ITK_TEMPLATE_TXX
00287 #include "itkMatrix.hxx"
00288 #endif
00289 
00290 #endif
00291