ITK  4.6.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< 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);
60 
62  typedef vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType;
63 
68 
71 
74 
77 
79  vnl_vector_fixed<T,NRows> operator *(const vnl_vector_fixed< T, NColumns > & vector) const;
80 
82  Self operator *(const CompatibleSquareMatrixType & matrix) const;
83 
84  template<unsigned int OuterDim>
85  Matrix<T, NRows, OuterDim> operator*(const vnl_matrix_fixed< T, NRows, OuterDim > & matrix) const
86  {
87  const Matrix<T, NRows, OuterDim> result ( m_Matrix * matrix );
88  return result;
89  }
90 
92  Self operator+(const Self & matrix) const;
93 
94  const Self & operator+=(const Self & matrix);
95 
97  Self operator-(const Self & matrix) const;
98 
99  const Self & operator-=(const Self & matrix);
100 
102  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
103 
105  void operator*=(const CompatibleSquareMatrixType & matrix);
106 
108  void operator*=(const vnl_matrix< T > & matrix);
109 
111  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
112 
114  void operator*=(const T & value)
115  { m_Matrix *= value; }
116 
118  Self operator*(const T & value)
119  {
120  Self result(*this);
121 
122  result *= value;
123  return result;
124  }
125 
127  void operator/=(const T & value)
128  {
129  m_Matrix /= value;
130  }
131 
133  Self operator/(const T & value)
134  {
135  Self result(*this);
136 
137  result /= value;
138  return result;
139  }
140 
142  inline T & operator()(unsigned int row, unsigned int col)
143  {
144  return m_Matrix(row, col);
145  }
146 
148  inline const T & operator()(unsigned int row, unsigned int col) const
149  {
150  return m_Matrix(row, col);
151  }
152 
154  inline T * operator[](unsigned int i)
155  {
156  return m_Matrix[i];
157  }
158 
160  inline const T * operator[](unsigned int i) const
161  {
162  return m_Matrix[i];
163  }
164 
167  {
168  return m_Matrix;
169  }
170 
172  inline const InternalMatrixType & GetVnlMatrix(void) const
173  {
174  return m_Matrix;
175  }
176 
178  inline void SetIdentity(void)
179  {
180  m_Matrix.set_identity();
181  }
182 
184  inline void Fill(const T & value)
185  {
186  m_Matrix.fill(value);
187  }
188 
190  inline const Self & operator=(const vnl_matrix< T > & matrix)
191  {
192  m_Matrix = matrix;
193  return *this;
194  }
195 
197  inline Matrix(const vnl_matrix< T > & matrix)
198  {
199  this->operator=(matrix);
200  }
201 
203  inline bool operator==(const Self & matrix) const
204  {
205  bool equal = true;
206 
207  for ( unsigned int r = 0; r < NRows; r++ )
208  {
209  for ( unsigned int c = 0; c < NColumns; c++ )
210  {
211  if ( m_Matrix(r, c) != matrix.m_Matrix(r, c) )
212  {
213  equal = false;
214  break;
215  }
216  }
217  }
218  return equal;
219  }
220 
221  inline bool operator!=(const Self & matrix) const
222  {
223  return !this->operator==(matrix);
224  }
225 
226  inline const Self & operator=(const InternalMatrixType & matrix)
227  {
228  this->m_Matrix = matrix;
229  return *this;
230  }
231 
233  inline explicit Matrix(const InternalMatrixType & matrix)
234  {
235  this->operator=(matrix);
236  }
237 
239  inline const Self & operator=(const Self & matrix)
240  {
241  m_Matrix = matrix.m_Matrix;
242  return *this;
243  }
244 
246  inline vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
247  {
248  if ( vnl_determinant(m_Matrix) == 0.0 )
249  {
250  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
251  }
252  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
253  return temp;
254  }
256 
258  inline vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
259  {
260  return m_Matrix.transpose();
261  }
262 
264  Matrix():m_Matrix(NumericTraits< T >::Zero) {}
265 
267  Matrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
268 
269 private:
271 };
272 
273 template< typename T, unsigned int NRows, unsigned int NColumns >
274 std::ostream & operator<<(std::ostream & os, const Matrix< T, NRows, NColumns > & v)
275 {
276  os << v.GetVnlMatrix();
277  return os;
278 }
279 } // end namespace itk
280 
281 #ifndef ITK_MANUAL_INSTANTIATION
282 #include "itkMatrix.hxx"
283 #endif
284 
285 #endif
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:190
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:226
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:46
void operator*=(const T &value)
Definition: itkMatrix.h:114
Matrix(const Self &matrix)
Definition: itkMatrix.h:267
Self operator-(const Self &matrix) const
bool operator!=(const Self &matrix) const
Definition: itkMatrix.h:221
vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
Definition: itkMatrix.h:246
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:142
vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType
Definition: itkMatrix.h:62
const InternalMatrixType & GetVnlMatrix(void) const
Definition: itkMatrix.h:172
InternalMatrixType m_Matrix
Definition: itkMatrix.h:270
Vector< T, NRows > operator*(const Vector< T, NColumns > &vector) const
const Self & operator+=(const Self &matrix)
Self operator*(const T &value)
Definition: itkMatrix.h:118
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:233
Matrix< T, NColumns, NColumns > CompatibleSquareMatrixType
Definition: itkMatrix.h:67
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:148
const Self & operator=(const Self &matrix)
Definition: itkMatrix.h:239
vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
Definition: itkMatrix.h:258
T ComponentType
Definition: itkMatrix.h:54
Matrix< T, NRows, OuterDim > operator*(const vnl_matrix_fixed< T, NRows, OuterDim > &matrix) const
Definition: itkMatrix.h:85
T * operator[](unsigned int i)
Definition: itkMatrix.h:154
void SetIdentity(void)
Definition: itkMatrix.h:178
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:203
void operator*=(const CompatibleSquareMatrixType &matrix)
const Self & operator-=(const Self &matrix)
static const unsigned int ColumnDimensions
Definition: itkMatrix.h:58
Self operator/(const T &value)
Definition: itkMatrix.h:133
InternalMatrixType & GetVnlMatrix(void)
Definition: itkMatrix.h:166
Matrix Self
Definition: itkMatrix.h:50
static const unsigned int RowDimensions
Definition: itkMatrix.h:57
void Fill(const T &value)
Definition: itkMatrix.h:184
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:197
Self operator+(const Self &matrix) const
Define additional traits for native types such as int or float.
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:51
A templated class holding a n-Dimensional covariant vector.
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:160
void operator/=(const T &value)
Definition: itkMatrix.h:127