00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkMatrix_h
00018 #define __itkMatrix_h
00019
00020
00021 #include "itkPoint.h"
00022 #include "itkVector.h"
00023 #include "itkCovariantVector.h"
00024 #include "vnl/vnl_matrix_fixed.h"
00025 #include "vnl/algo/vnl_matrix_inverse.h"
00026 #include "vnl/vnl_transpose.h"
00027 #include "vnl/vnl_matrix.h"
00028 #include <vnl/algo/vnl_determinant.h>
00029
00030 namespace itk
00031 {
00032
00041 template<class T, unsigned int NRows=3, unsigned int NColumns=3>
00042 class Matrix {
00043 public:
00045 typedef Matrix Self;
00046
00048 typedef T ValueType;
00049 typedef T ComponentType;
00050
00052 itkStaticConstMacro(RowDimensions, unsigned int, NRows);
00053 itkStaticConstMacro(ColumnDimensions, unsigned int, NColumns);
00055
00057 typedef vnl_matrix_fixed<T,NRows,NColumns> InternalMatrixType;
00058
00060 Vector<T,NRows> operator*(const Vector<T,NColumns> & vector) const;
00061
00063 Point<T,NRows> operator*(const Point<T,NColumns> & vector) const;
00064
00066 CovariantVector<T,NRows>
00067 operator*(const CovariantVector<T,NColumns> & vector) const;
00068
00070 Self operator*(const Self & matrix) const;
00071
00073 Self operator+(const Self & matrix) const;
00074 const Self & operator+=(const Self & matrix );
00076
00078 Self operator-(const Self & matrix) const;
00079 const Self & operator-=(const Self & matrix );
00081
00083 vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
00084
00086 void operator*=(const Self & matrix);
00087
00089 void operator*=(const vnl_matrix<T> & matrix);
00090
00092 vnl_vector<T> operator*(const vnl_vector<T> & matrix) const;
00093
00095 void operator*=(const T & value)
00096 { m_Matrix *= value; }
00097
00099 Self operator*(const T & value)
00100 { Self result( *this );
00101 result *= value;
00102 return result; }
00104
00106 void operator/=(const T & value)
00107 { m_Matrix /= value; }
00108
00110 Self operator/(const T & value)
00111 { Self result( *this );
00112 result /= value;
00113 return result; }
00115
00116
00118 inline T & operator()( unsigned int row, unsigned int col )
00119 { return m_Matrix(row,col); }
00120
00122 inline const T & operator()( unsigned int row, unsigned int col ) const
00123 { return m_Matrix(row,col); }
00124
00126 inline T * operator[]( unsigned int i )
00127 { return m_Matrix[i]; }
00128
00130 inline const T * operator[]( unsigned int i ) const
00131 { return m_Matrix[i]; }
00132
00134 inline InternalMatrixType & GetVnlMatrix( void )
00135 { return m_Matrix; }
00136
00138 inline const InternalMatrixType & GetVnlMatrix( void ) const
00139 { return m_Matrix; }
00140
00142 inline void SetIdentity( void )
00143 { m_Matrix.set_identity(); }
00144
00146 inline void Fill( const T & value )
00147 { m_Matrix.fill( value ); }
00148
00150 inline const Self & operator=( const vnl_matrix<T> & matrix)
00151 {
00152 m_Matrix = matrix;
00153 return *this;
00154 }
00155
00157 inline Matrix(const vnl_matrix<T> & matrix) {
00158 this->operator=(matrix);
00159 }
00160
00162 inline bool operator==( const Self & matrix) const
00163 {
00164 bool equal = true;
00165 for( unsigned int r=0; r<NRows; r++)
00166 {
00167 for( unsigned int c=0; c<NColumns; c++ )
00168 {
00169 if (m_Matrix(r,c) != matrix.m_Matrix(r,c))
00170 {
00171 equal = false;
00172 break;
00173 }
00174 }
00175 }
00176 return equal;
00177 }
00178 inline bool operator!=( const Self & matrix) const
00179 {
00180 return !this->operator==(matrix);
00181 }
00183
00184 inline const Self & operator=( const InternalMatrixType & matrix )
00185 {
00186 this->m_Matrix = matrix;
00187 return *this;
00188 }
00189
00191 inline explicit Matrix(const InternalMatrixType & matrix)
00192 {
00193 this->operator=(matrix);
00194 }
00195
00197 inline const Self & operator=( const Self & matrix)
00198 {
00199 m_Matrix = matrix.m_Matrix;
00200 return *this;
00201 }
00202
00204 inline vnl_matrix_fixed<T,NColumns,NRows> GetInverse( void ) const
00205 {
00206 if (vnl_determinant(m_Matrix) == 0.0)
00207 {
00208 itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
00209 }
00210 vnl_matrix<T> temp = vnl_matrix_inverse<T>( m_Matrix );
00211 return temp;
00212 }
00214
00216 inline vnl_matrix_fixed<T,NColumns,NRows> GetTranspose( void ) const
00217 {
00218 return m_Matrix.transpose();
00219 }
00220
00222 Matrix() : m_Matrix(NumericTraits<T>::Zero) {};
00223
00225 Matrix(const Self & matrix) : m_Matrix( matrix.m_Matrix ) {};
00226
00227 private:
00228 InternalMatrixType m_Matrix;
00229
00230 };
00231
00232 template< class T, unsigned int NRows, unsigned int NColumns >
00233 ITK_EXPORT std::ostream& operator<<(std::ostream& os,
00234 const Matrix<T,NRows,NColumns> & v)
00235 { os << v.GetVnlMatrix(); return os; }
00236
00237
00238
00239 }
00240
00241
00242 #define ITK_TEMPLATE_Matrix(_, EXPORT, x, y) namespace itk { \
00243 _(3(class EXPORT Matrix< ITK_TEMPLATE_3 x >)) \
00244 namespace Templates { typedef Matrix< ITK_TEMPLATE_3 x > Matrix##y; } \
00245 }
00246
00247 #if ITK_TEMPLATE_EXPLICIT
00248 # include "Templates/itkMatrix+-.h"
00249 #endif
00250
00251 #if ITK_TEMPLATE_TXX
00252 # include "itkMatrix.txx"
00253 #endif
00254
00255 #endif
00256