Orfeo Toolbox  4.0
itkMatrix.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkMatrix_h
19 #define __itkMatrix_h
20 
21 #include "itkPoint.h"
22 #include "itkCovariantVector.h"
23 #include "vnl/vnl_matrix_fixed.txx" // Get the templates
24 #include "vnl/vnl_transpose.h"
25 #include "vnl/algo/vnl_matrix_inverse.h"
26 #include "vnl/vnl_matrix.h"
27 #include "vnl/algo/vnl_determinant.h"
28 
29 namespace itk
30 {
45 template< typename T, unsigned int NRows = 3, unsigned int NColumns = 3 >
46 class Matrix
47 {
48 public:
50  typedef Matrix Self;
51 
53  typedef T ValueType;
54  typedef T ComponentType;
55 
57  itkStaticConstMacro(RowDimensions, unsigned int, NRows);
58  itkStaticConstMacro(ColumnDimensions, unsigned int, NColumns);
59 
61  typedef vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType;
62 
67 
70 
73 
76 
78  vnl_vector_fixed<T,NRows> operator *(const vnl_vector_fixed< T, NColumns > & vector) const;
79 
81  Self operator *(const CompatibleSquareMatrixType & matrix) const;
82 
83  template<unsigned int OuterDim>
84  Matrix<T, NRows, OuterDim> operator*(const vnl_matrix_fixed< T, NRows, OuterDim > & matrix) const
85  {
86  const Matrix<T, NRows, OuterDim> result ( m_Matrix * matrix );
87  return result;
88  }
89 
91  Self operator+(const Self & matrix) const;
92 
93  const Self & operator+=(const Self & matrix);
94 
96  Self operator-(const Self & matrix) const;
97 
98  const Self & operator-=(const Self & matrix);
99 
101  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
102 
104  void operator*=(const CompatibleSquareMatrixType & matrix);
105 
107  void operator*=(const vnl_matrix< T > & matrix);
108 
110  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
111 
113  void operator*=(const T & value)
114  { m_Matrix *= value; }
115 
117  Self operator*(const T & value)
118  {
119  Self result(*this);
120 
121  result *= value;
122  return result;
123  }
124 
126  void operator/=(const T & value)
127  {
128  m_Matrix /= value;
129  }
130 
132  Self operator/(const T & value)
133  {
134  Self result(*this);
135 
136  result /= value;
137  return result;
138  }
139 
141  inline T & operator()(unsigned int row, unsigned int col)
142  {
143  return m_Matrix(row, col);
144  }
145 
147  inline const T & operator()(unsigned int row, unsigned int col) const
148  {
149  return m_Matrix(row, col);
150  }
151 
153  inline T * operator[](unsigned int i)
154  {
155  return m_Matrix[i];
156  }
157 
159  inline const T * operator[](unsigned int i) const
160  {
161  return m_Matrix[i];
162  }
163 
166  {
167  return m_Matrix;
168  }
169 
171  inline const InternalMatrixType & GetVnlMatrix(void) const
172  {
173  return m_Matrix;
174  }
175 
177  inline void SetIdentity(void)
178  {
179  m_Matrix.set_identity();
180  }
181 
183  inline void Fill(const T & value)
184  {
185  m_Matrix.fill(value);
186  }
187 
189  inline const Self & operator=(const vnl_matrix< T > & matrix)
190  {
191  m_Matrix = matrix;
192  return *this;
193  }
194 
196  inline Matrix(const vnl_matrix< T > & matrix)
197  {
198  this->operator=(matrix);
199  }
200 
202  inline bool operator==(const Self & matrix) const
203  {
204  bool equal = true;
205 
206  for ( unsigned int r = 0; r < NRows; r++ )
207  {
208  for ( unsigned int c = 0; c < NColumns; c++ )
209  {
210  if ( m_Matrix(r, c) != matrix.m_Matrix(r, c) )
211  {
212  equal = false;
213  break;
214  }
215  }
216  }
217  return equal;
218  }
219 
220  inline bool operator!=(const Self & matrix) const
221  {
222  return !this->operator==(matrix);
223  }
224 
225  inline const Self & operator=(const InternalMatrixType & matrix)
226  {
227  this->m_Matrix = matrix;
228  return *this;
229  }
230 
232  inline explicit Matrix(const InternalMatrixType & matrix)
233  {
234  this->operator=(matrix);
235  }
236 
238  inline const Self & operator=(const Self & matrix)
239  {
240  m_Matrix = matrix.m_Matrix;
241  return *this;
242  }
243 
245  inline vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
246  {
247  if ( vnl_determinant(m_Matrix) == 0.0 )
248  {
249  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
250  }
251  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
252  return temp;
253  }
254 
256  inline vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
257  {
258  return m_Matrix.transpose();
259  }
260 
262  Matrix():m_Matrix(NumericTraits< T >::Zero) {}
263 
265  Matrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
266 
267 private:
269 };
270 
271 template< typename T, unsigned int NRows, unsigned int NColumns >
272 std::ostream & operator<<(std::ostream & os, const Matrix< T, NRows, NColumns > & v)
273 {
274  os << v.GetVnlMatrix();
275  return os;
276 }
277 } // end namespace itk
278 
279 #ifndef ITK_MANUAL_INSTANTIATION
280 #include "itkMatrix.hxx"
281 #endif
282 
283 #endif

Generated at Sat Mar 8 2014 15:11:18 for Orfeo Toolbox with doxygen 1.8.3.1