ITK  5.1.0
Insight 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 #include "vnl/vnl_matrix_fixed.hxx" // Get the templates
26 #include "vnl/vnl_transpose.h"
27 #include "vnl/algo/vnl_matrix_inverse.h"
28 #include "vnl/vnl_matrix.h"
29 #include "vnl/algo/vnl_determinant.h"
30 #include "itkMath.h"
31 
32 namespace itk
33 {
49 template <typename T, unsigned int NRows = 3, unsigned int NColumns = 3>
50 class ITK_TEMPLATE_EXPORT Matrix
51 {
52 public:
54  using Self = Matrix;
55 
57  using ValueType = T;
58  using ComponentType = T;
59 
61  static constexpr unsigned int RowDimensions = NRows;
62  static constexpr unsigned int ColumnDimensions = NColumns;
63 
65  using InternalMatrixType = vnl_matrix_fixed<T, NRows, NColumns>;
66 
71 
73  Vector<T, NRows> operator*(const Vector<T, NColumns> & vector) const;
74 
76  Point<T, NRows> operator*(const Point<T, NColumns> & vector) const;
77 
80 
82  vnl_vector_fixed<T, NRows> operator*(const vnl_vector_fixed<T, NColumns> & vector) const;
83 
85  Self operator*(const CompatibleSquareMatrixType & matrix) const;
86 
87  template <unsigned int OuterDim>
88  Matrix<T, NRows, OuterDim> operator*(const vnl_matrix_fixed<T, NRows, OuterDim> & matrix) const
89  {
90  const Matrix<T, NRows, OuterDim> result(m_Matrix * matrix);
91  return result;
92  }
93 
95  Self
96  operator+(const Self & matrix) const;
97 
98  const Self &
99  operator+=(const Self & matrix);
100 
102  Self
103  operator-(const Self & matrix) const;
104 
105  const Self &
106  operator-=(const Self & matrix);
107 
109  vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
110 
112  void
113  operator*=(const CompatibleSquareMatrixType & matrix);
114 
116  void
117  operator*=(const vnl_matrix<T> & matrix);
118 
120  vnl_vector<T> operator*(const vnl_vector<T> & matrix) const;
121 
123  void
124  operator*=(const T & value)
125  {
126  m_Matrix *= value;
127  }
128 
130  Self operator*(const T & value) const
131  {
132  Self result(*this);
133 
134  result *= value;
135  return result;
136  }
137 
139  void
140  operator/=(const T & value)
141  {
142  m_Matrix /= value;
143  }
144 
146  Self
147  operator/(const T & value) const
148  {
149  Self result(*this);
150 
151  result /= value;
152  return result;
153  }
154 
156  inline T &
157  operator()(unsigned int row, unsigned int col)
158  {
159  return m_Matrix(row, col);
160  }
161 
163  inline const T &
164  operator()(unsigned int row, unsigned int col) const
165  {
166  return m_Matrix(row, col);
167  }
168 
170  inline T * operator[](unsigned int i) { return m_Matrix[i]; }
171 
173  inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
174 
176  inline InternalMatrixType &
178  {
179  return m_Matrix;
180  }
181 
183  inline const InternalMatrixType &
184  GetVnlMatrix() const
185  {
186  return m_Matrix;
187  }
188 
190  inline void
192  {
193  m_Matrix.set_identity();
194  }
195 
197  inline void
198  Fill(const T & value)
199  {
200  m_Matrix.fill(value);
201  }
202 
204  inline const Self &
205  operator=(const vnl_matrix<T> & matrix)
206  {
207  m_Matrix = matrix;
208  return *this;
209  }
210 
212  inline Matrix(const vnl_matrix<T> & matrix) { this->operator=(matrix); }
213 
215  inline bool
216  operator==(const Self & matrix) const
217  {
218  bool equal = true;
219 
220  for (unsigned int r = 0; r < NRows; r++)
221  {
222  for (unsigned int c = 0; c < NColumns; c++)
223  {
224  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
225  {
226  equal = false;
227  break;
228  }
229  }
230  }
231  return equal;
232  }
233 
234  inline bool
235  operator!=(const Self & matrix) const
236  {
237  return !this->operator==(matrix);
238  }
239 
240  inline const Self &
242  {
243  this->m_Matrix = matrix;
244  return *this;
245  }
246 
248  inline explicit Matrix(const InternalMatrixType & matrix) { this->operator=(matrix); }
249 
251  inline const Self &
252  operator=(const Self & matrix)
253  {
254  m_Matrix = matrix.m_Matrix;
255  return *this;
256  }
257 
259  inline vnl_matrix_fixed<T, NColumns, NRows>
260  GetInverse() const
261  {
262  if (vnl_determinant(m_Matrix) == NumericTraits<T>::ZeroValue())
263  {
264  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
265  }
266  vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
267  return vnl_matrix_fixed<T, NColumns, NRows>{ inverse.as_matrix() };
268  }
270 
272  inline vnl_matrix_fixed<T, NColumns, NRows>
273  GetTranspose() const
274  {
275  return vnl_matrix_fixed<T, NColumns, NRows>{ m_Matrix.transpose().as_matrix() };
276  }
278 
281  : m_Matrix(NumericTraits<T>::ZeroValue())
282  {}
283 
285  Matrix(const Self & matrix)
286  : m_Matrix(matrix.m_Matrix)
287  {}
288 
289  void
290  swap(Self & other)
291  {
292  // allow for augment dependent look up
293  using std::swap;
294  swap(this->m_Matrix, other.m_Matrix);
295  }
296 
297 private:
299 };
300 
301 template <typename T, unsigned int NRows, unsigned int NColumns>
302 std::ostream &
303 operator<<(std::ostream & os, const Matrix<T, NRows, NColumns> & v)
304 {
305  os << v.GetVnlMatrix();
306  return os;
307 }
308 
309 
310 template <typename T, unsigned int NRows, unsigned int NColumns>
311 inline void
313 {
314  a.swap(b);
315 }
316 
317 } // end namespace itk
318 
319 #ifndef ITK_MANUAL_INSTANTIATION
320 # include "itkMatrix.hxx"
321 #endif
322 
323 #endif
itk::Matrix::swap
void swap(Self &other)
Definition: itkMatrix.h:290
itk::swap
void swap(const Matrix< T, NRows, NColumns > &a, const Matrix< T, NRows, NColumns > &b)
Definition: itkMatrix.h:312
itk::Matrix::SetIdentity
void SetIdentity()
Definition: itkMatrix.h:191
itkCovariantVector.h
itk::Matrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:177
itk::Matrix::Fill
void Fill(const T &value)
Definition: itkMatrix.h:198
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:211
itk::Matrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:173
itk::Matrix::operator!=
bool operator!=(const Self &matrix) const
Definition: itkMatrix.h:235
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::InternalMatrixType
vnl_matrix_fixed< double, NRows, NColumns > InternalMatrixType
Definition: itkMatrix.h:65
itkPoint.h
itk::Matrix::operator*=
void operator*=(const T &value)
Definition: itkMatrix.h:124
itk::Matrix::m_Matrix
InternalMatrixType m_Matrix
Definition: itkMatrix.h:298
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:237
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itk::Matrix::Matrix
Matrix(const Self &matrix)
Definition: itkMatrix.h:285
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:678
itk::Matrix::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:184
itk::Matrix::operator=
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:241
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
itk::Matrix::operator=
const Self & operator=(const Self &matrix)
Definition: itkMatrix.h:252
itk::Matrix::GetInverse
vnl_matrix_fixed< T, NColumns, NRows > GetInverse() const
Definition: itkMatrix.h:260
itk::Matrix::operator*
Matrix< T, NRows, OuterDim > operator*(const vnl_matrix_fixed< T, NRows, OuterDim > &matrix) const
Definition: itkMatrix.h:88
itk::Matrix::Matrix
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:212
itk::Matrix::operator[]
T * operator[](unsigned int i)
Definition: itkMatrix.h:170
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::ComponentType
double ComponentType
Definition: itkMatrix.h:58
itk::Matrix::GetTranspose
vnl_matrix_fixed< T, NColumns, NRows > GetTranspose() const
Definition: itkMatrix.h:273
itk::Matrix::Matrix
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:248
itk::Matrix::operator=
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:205
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:532
itk::Matrix
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:50
itk::operator*
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
Definition: itkCovariantVector.h:273
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:68
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::Matrix::operator/=
void operator/=(const T &value)
Definition: itkMatrix.h:140
itk::Matrix::operator/
Self operator/(const T &value) const
Definition: itkMatrix.h:147
itk::Matrix::operator==
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:216
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:660
itk::Matrix::operator*
Self operator*(const T &value) const
Definition: itkMatrix.h:130
itk::Point
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::ValueType
double ValueType
Definition: itkMatrix.h:57
itk::Matrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:157
itk::Matrix::Matrix
Matrix()
Definition: itkMatrix.h:280
itkMath.h
itk::Matrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:164