ITK  4.13.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 
24 #include <vxl_version.h>
25 #if VXL_VERSION_DATE_FULL < 20160229
26 #include "vnl/vnl_matrix_fixed.txx" // Get the templates
27 #else
28 #include "vnl/vnl_matrix_fixed.hxx" // Get the templates
29 #endif
30 #include "vnl/vnl_transpose.h"
31 #include "vnl/algo/vnl_matrix_inverse.h"
32 #include "vnl/vnl_matrix.h"
33 #include "vnl/algo/vnl_determinant.h"
34 #include "itkMath.h"
35 
36 namespace itk
37 {
52 template< typename T, unsigned int NRows = 3, unsigned int NColumns = 3 >
53 class ITK_TEMPLATE_EXPORT Matrix
54 {
55 public:
57  typedef Matrix Self;
58 
60  typedef T ValueType;
61  typedef T ComponentType;
62 
64  itkStaticConstMacro(RowDimensions, unsigned int, NRows);
65  itkStaticConstMacro(ColumnDimensions, unsigned int, NColumns);
67 
69  typedef vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType;
70 
75 
78 
81 
84 
86  vnl_vector_fixed<T,NRows> operator *(const vnl_vector_fixed< T, NColumns > & vector) const;
87 
89  Self operator *(const CompatibleSquareMatrixType & matrix) const;
90 
91  template<unsigned int OuterDim>
92  Matrix<T, NRows, OuterDim> operator*(const vnl_matrix_fixed< T, NRows, OuterDim > & matrix) const
93  {
94  const Matrix<T, NRows, OuterDim> result ( m_Matrix * matrix );
95  return result;
96  }
97 
99  Self operator+(const Self & matrix) const;
100 
101  const Self & operator+=(const Self & matrix);
102 
104  Self operator-(const Self & matrix) const;
105 
106  const Self & operator-=(const Self & matrix);
107 
109  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
110 
112  void operator*=(const CompatibleSquareMatrixType & matrix);
113 
115  void operator*=(const vnl_matrix< T > & matrix);
116 
118  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
119 
121  void operator*=(const T & value)
122  { m_Matrix *= value; }
123 
125  Self operator*(const T & value) const
126  {
127  Self result(*this);
128 
129  result *= value;
130  return result;
131  }
132 
134  void operator/=(const T & value)
135  {
136  m_Matrix /= value;
137  }
138 
140  Self operator/(const T & value) const
141  {
142  Self result(*this);
143 
144  result /= value;
145  return result;
146  }
147 
149  inline T & operator()(unsigned int row, unsigned int col)
150  {
151  return m_Matrix(row, col);
152  }
153 
155  inline const T & operator()(unsigned int row, unsigned int col) const
156  {
157  return m_Matrix(row, col);
158  }
159 
161  inline T * operator[](unsigned int i)
162  {
163  return m_Matrix[i];
164  }
165 
167  inline const T * operator[](unsigned int i) const
168  {
169  return m_Matrix[i];
170  }
171 
174  {
175  return m_Matrix;
176  }
177 
179  inline const InternalMatrixType & GetVnlMatrix(void) const
180  {
181  return m_Matrix;
182  }
183 
185  inline void SetIdentity(void)
186  {
187  m_Matrix.set_identity();
188  }
189 
191  inline void Fill(const T & value)
192  {
193  m_Matrix.fill(value);
194  }
195 
197  inline const Self & operator=(const vnl_matrix< T > & matrix)
198  {
199  m_Matrix = matrix;
200  return *this;
201  }
202 
204  inline Matrix(const vnl_matrix< T > & matrix)
205  {
206  this->operator=(matrix);
207  }
208 
210  inline bool operator==(const Self & matrix) const
211  {
212  bool equal = true;
213 
214  for ( unsigned int r = 0; r < NRows; r++ )
215  {
216  for ( unsigned int c = 0; c < NColumns; c++ )
217  {
218  if ( Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)) )
219  {
220  equal = false;
221  break;
222  }
223  }
224  }
225  return equal;
226  }
227 
228  inline bool operator!=(const Self & matrix) const
229  {
230  return !this->operator==(matrix);
231  }
232 
233  inline const Self & operator=(const InternalMatrixType & matrix)
234  {
235  this->m_Matrix = matrix;
236  return *this;
237  }
238 
240  inline explicit Matrix(const InternalMatrixType & matrix)
241  {
242  this->operator=(matrix);
243  }
244 
246  inline const Self & operator=(const Self & matrix)
247  {
248  m_Matrix = matrix.m_Matrix;
249  return *this;
250  }
251 
253  inline vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
254  {
255  if ( vnl_determinant(m_Matrix) == 0.0 )
256  {
257  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
258  }
259  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
260  return temp;
261  }
263 
265  inline vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
266  {
267  return m_Matrix.transpose();
268  }
269 
271  Matrix():m_Matrix(NumericTraits< T >::ZeroValue()) {}
272 
274  Matrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
275 
276 private:
278 };
279 
280 template< typename T, unsigned int NRows, unsigned int NColumns >
281 std::ostream & operator<<(std::ostream & os, const Matrix< T, NRows, NColumns > & v)
282 {
283  os << v.GetVnlMatrix();
284  return os;
285 }
286 } // end namespace itk
287 
288 #ifndef ITK_MANUAL_INSTANTIATION
289 #include "itkMatrix.hxx"
290 #endif
291 
292 #endif
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:197
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:233
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
void operator*=(const T &value)
Definition: itkMatrix.h:121
Matrix(const Self &matrix)
Definition: itkMatrix.h:274
bool operator!=(const Self &matrix) const
Definition: itkMatrix.h:228
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
Definition: itkMatrix.h:253
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:149
vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType
Definition: itkMatrix.h:69
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
const InternalMatrixType & GetVnlMatrix(void) const
Definition: itkMatrix.h:179
InternalMatrixType m_Matrix
Definition: itkMatrix.h:277
Self operator*(const T &value) const
Definition: itkMatrix.h:125
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:240
Matrix< T, NColumns, NColumns > CompatibleSquareMatrixType
Definition: itkMatrix.h:74
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:155
const Self & operator=(const Self &matrix)
Definition: itkMatrix.h:246
vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
Definition: itkMatrix.h:265
T ComponentType
Definition: itkMatrix.h:61
Matrix< T, NRows, OuterDim > operator*(const vnl_matrix_fixed< T, NRows, OuterDim > &matrix) const
Definition: itkMatrix.h:92
T * operator[](unsigned int i)
Definition: itkMatrix.h:161
void SetIdentity(void)
Definition: itkMatrix.h:185
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:210
Self operator/(const T &value) const
Definition: itkMatrix.h:140
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:721
InternalMatrixType & GetVnlMatrix(void)
Definition: itkMatrix.h:173
Matrix Self
Definition: itkMatrix.h:57
void Fill(const T &value)
Definition: itkMatrix.h:191
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:204
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:52
A templated class holding a n-Dimensional covariant vector.
bool ITKIOXML_EXPORT operator==(itk::FancyString &s, const std::string &)
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:167
void operator/=(const T &value)
Definition: itkMatrix.h:134