Orfeo Toolbox  3.16
itkMatrix.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMatrix.h,v $
5  Language: C++
6  Date: $Date: 2009-02-07 22:38:33 $
7  Version: $Revision: 1.31 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkMatrix_h
18 #define __itkMatrix_h
19 
20 
21 #include "itkPoint.h"
22 #include "itkVector.h"
23 #include "itkCovariantVector.h"
24 #include "vnl/vnl_matrix_fixed.h"
25 #include "vnl/algo/vnl_matrix_inverse.h"
26 #include "vnl/vnl_transpose.h"
27 #include "vnl/vnl_matrix.h"
28 #include <vnl/algo/vnl_determinant.h>
29 
30 namespace itk
31 {
32 
41 template<class T, unsigned int NRows=3, unsigned int NColumns=3>
42 class Matrix
43 {
44 public:
46  typedef Matrix Self;
47 
49  typedef T ValueType;
50  typedef T ComponentType;
51 
53  itkStaticConstMacro(RowDimensions, unsigned int, NRows);
54  itkStaticConstMacro(ColumnDimensions, unsigned int, NColumns);
55 
57  typedef vnl_matrix_fixed<T,NRows,NColumns> InternalMatrixType;
58 
60  Vector<T,NRows> operator*(const Vector<T,NColumns> & vector) const;
61 
63  Point<T,NRows> operator*(const Point<T,NColumns> & vector) const;
64 
67  operator*(const CovariantVector<T,NColumns> & vector) const;
68 
70  Self operator*(const Self & matrix) const;
71 
73  Self operator+(const Self & matrix) const;
74  const Self & operator+=(const Self & matrix );
75 
77  Self operator-(const Self & matrix) const;
78  const Self & operator-=(const Self & matrix );
79 
81  vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
82 
84  void operator*=(const Self & matrix);
85 
87  void operator*=(const vnl_matrix<T> & matrix);
88 
90  vnl_vector<T> operator*(const vnl_vector<T> & matrix) const;
91 
93  void operator*=(const T & value)
94  { m_Matrix *= value; }
95 
97  Self operator*(const T & value)
98  {
99  Self result( *this );
100  result *= value;
101  return result;
102  }
103 
105  void operator /= (const T & value)
106  {
107  m_Matrix /= value;
108  }
109 
111  Self operator/(const T & value)
112  {
113  Self result( *this );
114  result /= value;
115  return result;
116  }
117 
119  inline T & operator()( unsigned int row, unsigned int col )
120  {
121  return m_Matrix(row,col);
122  }
123 
125  inline const T & operator()( unsigned int row, unsigned int col ) const
126  { return m_Matrix(row,col); }
127 
129  inline T * operator[]( unsigned int i )
130  { return m_Matrix[i]; }
131 
133  inline const T * operator[]( unsigned int i ) const
134  { return m_Matrix[i]; }
135 
138  { return m_Matrix; }
139 
141  inline const InternalMatrixType & GetVnlMatrix( void ) const
142  { return m_Matrix; }
143 
145  inline void SetIdentity( void )
146  { m_Matrix.set_identity(); }
147 
149  inline void Fill( const T & value )
150  { m_Matrix.fill( value ); }
151 
153  inline const Self & operator=( const vnl_matrix<T> & matrix)
154  {
155  m_Matrix = matrix;
156  return *this;
157  }
158 
160  inline Matrix(const vnl_matrix<T> & matrix)
161  {
162  this->operator=(matrix);
163  }
164 
166  inline bool operator==( const Self & matrix) const
167  {
168  bool equal = true;
169  for( unsigned int r=0; r<NRows; r++)
170  {
171  for( unsigned int c=0; c<NColumns; c++ )
172  {
173  if (m_Matrix(r,c) != matrix.m_Matrix(r,c))
174  {
175  equal = false;
176  break;
177  }
178  }
179  }
180  return equal;
181  }
182 
183  inline bool operator!=( const Self & matrix) const
184  {
185  return !this->operator==(matrix);
186  }
187 
188  inline const Self & operator=( const InternalMatrixType & matrix )
189  {
190  this->m_Matrix = matrix;
191  return *this;
192  }
193 
195  inline explicit Matrix(const InternalMatrixType & matrix)
196  {
197  this->operator=(matrix);
198  }
200  inline const Self & operator=( const Self & matrix)
201  {
202  m_Matrix = matrix.m_Matrix;
203  return *this;
204  }
205 
207  inline vnl_matrix_fixed<T,NColumns,NRows> GetInverse( void ) const
208  {
209  if (vnl_determinant(m_Matrix) == 0.0)
210  {
211  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
212  }
213  vnl_matrix<T> temp = vnl_matrix_inverse<T>( m_Matrix );
214  return temp;
215  }
216 
218  inline vnl_matrix_fixed<T,NColumns,NRows> GetTranspose( void ) const
219  {
220  return m_Matrix.transpose();
221  }
222 
224  Matrix() : m_Matrix(NumericTraits<T>::Zero) {};
225 
227  Matrix(const Self & matrix) : m_Matrix( matrix.m_Matrix ) {};
228 
229 private:
231 
232 };
233 
234 template< class T, unsigned int NRows, unsigned int NColumns >
235 ITK_EXPORT std::ostream& operator<<( std::ostream& os, const Matrix<T,NRows,NColumns> & v)
236 {
237  os << v.GetVnlMatrix();
238  return os;
239 }
240 
241 
242 } // end namespace itk
243 
244 // Define instantiation macro for this template.
245 #define ITK_TEMPLATE_Matrix(_, EXPORT, x, y) namespace itk { \
246  _(3(class EXPORT Matrix< ITK_TEMPLATE_3 x >)) \
247  namespace Templates { typedef Matrix< ITK_TEMPLATE_3 x > Matrix##y; } \
248  }
249 
250 #if ITK_TEMPLATE_EXPLICIT
251 # include "Templates/itkMatrix+-.h"
252 #endif
253 
254 #if ITK_TEMPLATE_TXX
255 # include "itkMatrix.txx"
256 #endif
257 
258 #endif

Generated at Sat Feb 2 2013 23:51:46 for Orfeo Toolbox with doxygen 1.8.1.1