ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkVariableSizeMatrix.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 __itkVariableSizeMatrix_h
00019 #define __itkVariableSizeMatrix_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 "itkArray.h"
00028 
00029 namespace itk
00030 {
00042 template< class T >
00043 class VariableSizeMatrix
00044 {
00045 public:
00047   typedef VariableSizeMatrix Self;
00048 
00050   typedef T ValueType;
00051   typedef T ComponentType;
00052 
00054   typedef vnl_matrix< T > InternalMatrixType;
00055 
00057   Array< T > operator *(const Array< T > & vector) const;
00058 
00060   Self operator *(const Self & matrix) const;
00061 
00063   Self operator+(const Self & matrix) const;
00064 
00065   const Self & operator+=(const Self & matrix);
00066 
00068   Self operator-(const Self & matrix) const;
00069 
00070   const Self & operator-=(const Self & matrix);
00071 
00073   Self & operator-();
00074 
00076   vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
00077 
00079   void operator*=(const Self & matrix);
00080 
00082   void operator*=(const vnl_matrix< T > & matrix);
00083 
00085   vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
00086 
00088   void operator*=(const T & value) { m_Matrix *= value; }
00089 
00091   Self operator*(const T & value)
00092   {
00093     Self result(*this);
00094 
00095     result *= value;
00096     return result;
00097   }
00098 
00100   void operator/=(const T & value) { m_Matrix /= value; }
00101 
00103   Self operator/(const T & value)
00104   {
00105     Self result(*this);
00106 
00107     result /= value;
00108     return result;
00109   }
00110 
00112   inline T & operator()(unsigned int row, unsigned int col)
00113   {
00114     return m_Matrix(row, col);
00115   }
00116 
00118   inline const T & operator()(unsigned int row, unsigned int col) const
00119   {
00120     return m_Matrix(row, col);
00121   }
00122 
00124   inline T * operator[](unsigned int i)
00125   {
00126     return m_Matrix[i];
00127   }
00128 
00130   inline const T * operator[](unsigned int i) const
00131   {
00132     return m_Matrix[i];
00133   }
00134 
00136   inline InternalMatrixType & GetVnlMatrix(void)
00137   {
00138     return m_Matrix;
00139   }
00140 
00142   inline const InternalMatrixType & GetVnlMatrix(void) const
00143   {
00144     return m_Matrix;
00145   }
00146 
00148   inline void SetIdentity(void)
00149   {
00150     m_Matrix.set_identity();
00151   }
00152 
00154   inline void Fill(const T & value)
00155   {
00156     m_Matrix.fill(value);
00157   }
00158 
00160   inline const Self & operator=(const vnl_matrix< T > & matrix)
00161   {
00162     m_Matrix = matrix;
00163     return *this;
00164   }
00165 
00167   inline bool operator==(const Self & matrix) const;
00168 
00169   inline bool operator!=(const Self & matrix) const
00170   {
00171     return !this->operator==(matrix);
00172   }
00173 
00175   inline const Self & operator=(const Self & matrix)
00176   {
00177     m_Matrix = matrix.m_Matrix;
00178     return *this;
00179   }
00180 
00182   inline vnl_matrix< T > GetInverse(void) const
00183   {
00184     vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
00185     return temp;
00186   }
00188 
00190   inline vnl_matrix< T > GetTranspose(void) const
00191   {
00192     return m_Matrix.transpose();
00193   }
00194 
00196   VariableSizeMatrix():m_Matrix() {}
00197 
00198   VariableSizeMatrix(unsigned int rows, unsigned int cols);
00199 
00201   VariableSizeMatrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
00202 
00204   inline unsigned int Rows() const { return m_Matrix.rows(); }
00205 
00207   inline unsigned int Cols() const { return m_Matrix.cols(); }
00208 
00210   inline bool SetSize(unsigned int r, unsigned int c) { return m_Matrix.set_size(r, c); }
00211 private:
00212   InternalMatrixType m_Matrix;
00213 };
00214 
00215 template< class T >
00216 ITK_EXPORT std::ostream & operator<<(std::ostream & os,
00217                                      const VariableSizeMatrix< T > & v)
00218 {
00219   os << v.GetVnlMatrix(); return os;
00220 }
00221 
00225 template< class T >
00226 inline
00227 bool
00228 VariableSizeMatrix< T >
00229 ::operator==(const Self & matrix) const
00230 {
00231   if ( ( matrix.Rows() != this->Rows() )
00232        || ( matrix.Cols() != this->Cols() ) )
00233     {
00234     return false;
00235     }
00236   bool equal = true;
00238 
00239   for ( unsigned int r = 0; r < this->Rows(); r++ )
00240     {
00241     for ( unsigned int c = 0; c < this->Cols(); c++ )
00242       {
00243       if ( m_Matrix(r, c) != matrix.m_Matrix(r, c) )
00244         {
00245         equal = false;
00246         break;
00247         }
00248       }
00249     }
00250   return equal;
00251 }
00252 } // end namespace itk
00253 
00254 #ifndef ITK_MANUAL_INSTANTIATION
00255 #include "itkVariableSizeMatrix.hxx"
00256 #endif
00257 
00258 #endif
00259