ITK  4.3.0
Insight Segmentation and Registration Toolkit
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< class 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);
60 
62  typedef vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType;
63 
68 
71 
74 
77  operator *(const CovariantVector< T, NColumns > & vector) const;
78 
80  Self operator *(const CompatibleSquareMatrixType & matrix) const;
81 
83  Self operator+(const Self & matrix) const;
84 
85  const Self & operator+=(const Self & matrix);
86 
88  Self operator-(const Self & matrix) const;
89 
90  const Self & operator-=(const Self & matrix);
91 
93  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
94 
96  void operator*=(const CompatibleSquareMatrixType & matrix);
97 
99  void operator*=(const vnl_matrix< T > & matrix);
100 
102  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
103 
105  void operator*=(const T & value)
106  { m_Matrix *= value; }
107 
109  Self operator*(const T & value)
110  {
111  Self result(*this);
112 
113  result *= value;
114  return result;
115  }
116 
118  void operator/=(const T & value)
119  {
120  m_Matrix /= value;
121  }
122 
124  Self operator/(const T & value)
125  {
126  Self result(*this);
127 
128  result /= value;
129  return result;
130  }
131 
133  inline T & operator()(unsigned int row, unsigned int col)
134  {
135  return m_Matrix(row, col);
136  }
137 
139  inline const T & operator()(unsigned int row, unsigned int col) const
140  {
141  return m_Matrix(row, col);
142  }
143 
145  inline T * operator[](unsigned int i)
146  {
147  return m_Matrix[i];
148  }
149 
151  inline const T * operator[](unsigned int i) const
152  {
153  return m_Matrix[i];
154  }
155 
158  {
159  return m_Matrix;
160  }
161 
163  inline const InternalMatrixType & GetVnlMatrix(void) const
164  {
165  return m_Matrix;
166  }
167 
169  inline void SetIdentity(void)
170  {
171  m_Matrix.set_identity();
172  }
173 
175  inline void Fill(const T & value)
176  {
177  m_Matrix.fill(value);
178  }
179 
181  inline const Self & operator=(const vnl_matrix< T > & matrix)
182  {
183  m_Matrix = matrix;
184  return *this;
185  }
186 
188  inline Matrix(const vnl_matrix< T > & matrix)
189  {
190  this->operator=(matrix);
191  }
192 
194  inline bool operator==(const Self & matrix) const
195  {
196  bool equal = true;
197 
198  for ( unsigned int r = 0; r < NRows; r++ )
199  {
200  for ( unsigned int c = 0; c < NColumns; c++ )
201  {
202  if ( m_Matrix(r, c) != matrix.m_Matrix(r, c) )
203  {
204  equal = false;
205  break;
206  }
207  }
208  }
209  return equal;
210  }
211 
212  inline bool operator!=(const Self & matrix) const
213  {
214  return !this->operator==(matrix);
215  }
216 
217  inline const Self & operator=(const InternalMatrixType & matrix)
218  {
219  this->m_Matrix = matrix;
220  return *this;
221  }
222 
224  inline explicit Matrix(const InternalMatrixType & matrix)
225  {
226  this->operator=(matrix);
227  }
228 
230  inline const Self & operator=(const Self & matrix)
231  {
232  m_Matrix = matrix.m_Matrix;
233  return *this;
234  }
235 
237  inline vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
238  {
239  if ( vnl_determinant(m_Matrix) == 0.0 )
240  {
241  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
242  }
243  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
244  return temp;
245  }
247 
249  inline vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
250  {
251  return m_Matrix.transpose();
252  }
253 
255  Matrix():m_Matrix(NumericTraits< T >::Zero) {}
256 
258  Matrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
259 
260 private:
262 };
263 
264 template< class T, unsigned int NRows, unsigned int NColumns >
265 ITK_EXPORT std::ostream & operator<<(std::ostream & os, const Matrix< T, NRows, NColumns > & v)
266 {
267  os << v.GetVnlMatrix();
268  return os;
269 }
270 } // end namespace itk
271 
272 #ifndef ITK_MANUAL_INSTANTIATION
273 #include "itkMatrix.hxx"
274 #endif
275 
276 #endif
277