ITK  6.0.0
Insight Toolkit
itkMatrix.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 #include <type_traits> // For is_same.
32 
33 namespace itk
34 {
51 template <typename T, unsigned int VRows = 3, unsigned int VColumns = 3>
52 class ITK_TEMPLATE_EXPORT Matrix
53 {
54 public:
56  using Self = Matrix;
57 
59  using ValueType = T;
60  using ComponentType = T;
61 
63  using pointer = ValueType *;
64 
66  using const_pointer = const ValueType *;
67 
69  using reference = ValueType &;
70 
72  using const_reference = const ValueType &;
73 
75  using iterator = ValueType *;
76 
78  using const_iterator = const ValueType *;
79 
81  static constexpr unsigned int RowDimensions = VRows;
82  static constexpr unsigned int ColumnDimensions = VColumns;
83 
85  using InternalMatrixType = vnl_matrix_fixed<T, VRows, VColumns>;
86 
91 
94 
96  Point<T, VRows> operator*(const Point<T, VColumns> & pnt) const;
97 
100 
102  vnl_vector_fixed<T, VRows> operator*(const vnl_vector_fixed<T, VColumns> & inVNLvect) const;
103 
105  Self operator*(const CompatibleSquareMatrixType & matrix) const;
106 
107  template <unsigned int OuterDim>
108  Matrix<T, VRows, OuterDim> operator*(const vnl_matrix_fixed<T, VRows, OuterDim> & matrix) const
109  {
110  const Matrix<T, VRows, OuterDim> result(m_Matrix * matrix);
111  return result;
112  }
113 
115  Self
116  operator+(const Self & matrix) const;
117 
118  const Self &
119  operator+=(const Self & matrix);
120 
122  Self
123  operator-(const Self & matrix) const;
124 
125  const Self &
126  operator-=(const Self & matrix);
127 
129  vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
130 
132  void
133  operator*=(const CompatibleSquareMatrixType & matrix);
134 
136  void
137  operator*=(const vnl_matrix<T> & matrix);
138 
140  vnl_vector<T> operator*(const vnl_vector<T> & vc) const;
141 
143  void
144  operator*=(const T & value)
145  {
146  m_Matrix *= value;
147  }
148 
150  Self operator*(const T & value) const
151  {
152  Self result(*this);
153 
154  result *= value;
155  return result;
156  }
157 
159  void
160  operator/=(const T & value)
161  {
162  m_Matrix /= value;
163  }
164 
166  Self
167  operator/(const T & value) const
168  {
169  Self result(*this);
170 
171  result /= value;
172  return result;
173  }
174 
176  inline T &
177  operator()(unsigned int row, unsigned int col)
178  {
179  return m_Matrix(row, col);
180  }
181 
183  inline const T &
184  operator()(unsigned int row, unsigned int col) const
185  {
186  return m_Matrix(row, col);
187  }
188 
190  inline T * operator[](unsigned int i) { return m_Matrix[i]; }
191 
193  inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
194 
196  inline InternalMatrixType &
198  {
199  return m_Matrix;
200  }
201 
203  inline const InternalMatrixType &
204  GetVnlMatrix() const
205  {
206  return m_Matrix;
207  }
208 
210  inline void
212  {
213  m_Matrix.set_identity();
214  }
215 
217  static Self
219  {
220  InternalMatrixType internalMatrix;
221  internalMatrix.set_identity();
222  return Self{ internalMatrix };
223  }
227  inline void
228  Fill(const T & value)
229  {
230  m_Matrix.fill(value);
231  }
232 
234  inline Self &
235  operator=(const vnl_matrix<T> & matrix)
236  {
237  m_Matrix = matrix;
238  return *this;
239  }
240 
243  inline explicit Matrix(const vnl_matrix<T> & matrix)
244  : m_Matrix(matrix)
245  {}
246 
251  template <typename TElement>
252  explicit Matrix(const TElement (&elements)[VRows][VColumns])
253  : m_Matrix(&elements[0][0])
254  {
255  static_assert(std::is_same_v<TElement, T>,
256  "The type of an element should correspond with this itk::Matrix instantiation.");
257  }
258 
260  inline bool
261  operator==(const Self & matrix) const
262  {
263  bool equal = true;
264 
265  for (unsigned int r = 0; r < VRows; ++r)
266  {
267  for (unsigned int c = 0; c < VColumns; ++c)
268  {
269  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
270  {
271  equal = false;
272  break;
273  }
274  }
275  }
276  return equal;
277  }
278 
279  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
280 
281 
282  inline Self &
284  {
285  this->m_Matrix = matrix;
286  return *this;
287  }
288 
290  inline Matrix(const InternalMatrixType & matrix)
291  : m_Matrix(matrix)
292  {}
293 
295  inline vnl_matrix_fixed<T, VColumns, VRows>
296  GetInverse() const
297  {
298  if (vnl_determinant(m_Matrix) == T{})
299  {
300  itkGenericExceptionMacro("Singular matrix. Determinant is 0.");
301  }
302  vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
303  return vnl_matrix_fixed<T, VColumns, VRows>{ inverse.as_matrix() };
304  }
308  inline vnl_matrix_fixed<T, VColumns, VRows>
309  GetTranspose() const
310  {
311  return vnl_matrix_fixed<T, VColumns, VRows>{ m_Matrix.transpose().as_matrix() };
312  }
320  Matrix() = default;
321 
323  constexpr unsigned int
324  size() const
325  {
326  return m_Matrix.size();
327  }
328 
330  iterator
332  {
333  return m_Matrix.begin();
334  }
335 
337  iterator
338  end()
339  {
340  return m_Matrix.end();
341  }
342 
344  const_iterator
345  begin() const
346  {
347  return m_Matrix.begin();
348  }
349 
351  const_iterator
352  end() const
353  {
354  return m_Matrix.end();
355  }
356 
358  const_iterator
359  cbegin() const
360  {
361  return m_Matrix.begin();
362  }
363 
365  const_iterator
366  cend() const
367  {
368  return m_Matrix.end();
369  }
370 
371  void
372  swap(Self & other)
373  {
374  // allow for augment dependent look up
375  using std::swap;
376  swap(this->m_Matrix, other.m_Matrix);
377  }
378 
379  inline void
380  PrintSelf(std::ostream & os, Indent indent) const
381  {
382  os << indent << "Matrix (" << VRows << "x" << VColumns << ")\n";
383  for (unsigned int r = 0; r < VRows; ++r)
384  {
385  os << indent << " ";
386  for (unsigned int c = 0; c < VColumns; ++c)
387  {
388  os << m_Matrix(r, c) << " ";
389  }
390  os << "\n";
391  }
392  }
393 
394 private:
395  InternalMatrixType m_Matrix{};
396 };
397 
398 template <typename T, unsigned int VRows, unsigned int VColumns>
399 std::ostream &
400 operator<<(std::ostream & os, const Matrix<T, VRows, VColumns> & v)
401 {
402  os << v.GetVnlMatrix();
403  return os;
404 }
405 
406 
407 template <typename T, unsigned int VRows, unsigned int VColumns>
408 inline void
410 {
411  a.swap(b);
412 }
413 
414 } // end namespace itk
415 
416 #ifndef ITK_MANUAL_INSTANTIATION
417 # include "itkMatrix.hxx"
418 #endif
419 
420 #endif
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::const_reference
const ValueType & const_reference
Definition: itkMatrix.h:72
itk::Matrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:184
itk::Matrix::begin
const_iterator begin() const
Definition: itkMatrix.h:345
itk::Matrix::operator=
Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:235
itk::Matrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:193
itkCovariantVector.h
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:268
itk::Matrix::operator/
Self operator/(const T &value) const
Definition: itkMatrix.h:167
itk::Matrix::size
constexpr unsigned int size() const
Definition: itkMatrix.h:324
itkPoint.h
itk::Matrix::operator/=
void operator/=(const T &value)
Definition: itkMatrix.h:160
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::ComponentType
double ComponentType
Definition: itkMatrix.h:60
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::InternalMatrixType
vnl_matrix_fixed< double, VRows, VColumns > InternalMatrixType
Definition: itkMatrix.h:85
itk::Matrix::begin
iterator begin()
Definition: itkMatrix.h:331
itk::Matrix::operator*
Self operator*(const T &value) const
Definition: itkMatrix.h:150
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:672
itk::Matrix::Matrix
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:243
itk::Matrix::SetIdentity
void SetIdentity()
Definition: itkMatrix.h:211
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::Matrix::operator==
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:261
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::const_pointer
const ValueType * const_pointer
Definition: itkMatrix.h:66
itk::Matrix::cbegin
const_iterator cbegin() const
Definition: itkMatrix.h:359
itk::Matrix::GetIdentity
static Self GetIdentity()
Definition: itkMatrix.h:218
itk::Matrix::end
const_iterator end() const
Definition: itkMatrix.h:352
itk::Matrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:177
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::ValueType
double ValueType
Definition: itkMatrix.h:59
itk::Matrix::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:204
itk::Matrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:197
itk::Matrix::operator*
Matrix< T, VRows, OuterDim > operator*(const vnl_matrix_fixed< T, VRows, OuterDim > &matrix) const
Definition: itkMatrix.h:108
itk::Matrix::swap
void swap(Self &other)
Definition: itkMatrix.h:372
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::reference
ValueType & reference
Definition: itkMatrix.h:69
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::iterator
ValueType * iterator
Definition: itkMatrix.h:75
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::const_iterator
const ValueType * const_iterator
Definition: itkMatrix.h:78
itk::Matrix::operator=
Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:283
itk::Matrix::operator[]
T * operator[](unsigned int i)
Definition: itkMatrix.h:190
itk::Matrix::Matrix
Matrix(const TElement(&elements)[VRows][VColumns])
Definition: itkMatrix.h:252
itk::Matrix
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:52
itk::Matrix::m_Matrix
InternalMatrixType m_Matrix
Definition: itkMatrix.h:395
itk::Matrix::Fill
void Fill(const T &value)
Definition: itkMatrix.h:228
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:70
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::pointer
ValueType * pointer
Definition: itkMatrix.h:63
itk::swap
void swap(const Matrix< T, VRows, VColumns > &a, const Matrix< T, VRows, VColumns > &b)
Definition: itkMatrix.h:409
itk::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::Matrix::PrintSelf
void PrintSelf(std::ostream &os, Indent indent) const
Definition: itkMatrix.h:380
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:654
itk::Point
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:53
itk::Matrix::operator*=
void operator*=(const T &value)
Definition: itkMatrix.h:144
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::Matrix::GetTranspose
vnl_matrix_fixed< T, VColumns, VRows > GetTranspose() const
Definition: itkMatrix.h:309
itk::Matrix::GetInverse
vnl_matrix_fixed< T, VColumns, VRows > GetInverse() const
Definition: itkMatrix.h:296
itkMath.h
itk::Matrix::cend
const_iterator cend() const
Definition: itkMatrix.h:366
itk::Matrix::end
iterator end()
Definition: itkMatrix.h:338
itk::Matrix::Matrix
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:290