ITK  5.4.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  static constexpr unsigned int RowDimensions = VRows;
64  static constexpr unsigned int ColumnDimensions = VColumns;
65 
67  using InternalMatrixType = vnl_matrix_fixed<T, VRows, VColumns>;
68 
73 
76 
78  Point<T, VRows> operator*(const Point<T, VColumns> & pnt) const;
79 
82 
84  vnl_vector_fixed<T, VRows> operator*(const vnl_vector_fixed<T, VColumns> & inVNLvect) const;
85 
87  Self operator*(const CompatibleSquareMatrixType & matrix) const;
88 
89  template <unsigned int OuterDim>
90  Matrix<T, VRows, OuterDim> operator*(const vnl_matrix_fixed<T, VRows, OuterDim> & matrix) const
91  {
92  const Matrix<T, VRows, OuterDim> result(m_Matrix * matrix);
93  return result;
94  }
95 
97  Self
98  operator+(const Self & matrix) const;
99 
100  const Self &
101  operator+=(const Self & matrix);
102 
104  Self
105  operator-(const Self & matrix) const;
106 
107  const Self &
108  operator-=(const Self & matrix);
109 
111  vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
112 
114  void
115  operator*=(const CompatibleSquareMatrixType & matrix);
116 
118  void
119  operator*=(const vnl_matrix<T> & matrix);
120 
122  vnl_vector<T> operator*(const vnl_vector<T> & vc) const;
123 
125  void
126  operator*=(const T & value)
127  {
128  m_Matrix *= value;
129  }
130 
132  Self operator*(const T & value) const
133  {
134  Self result(*this);
135 
136  result *= value;
137  return result;
138  }
139 
141  void
142  operator/=(const T & value)
143  {
144  m_Matrix /= value;
145  }
146 
148  Self
149  operator/(const T & value) const
150  {
151  Self result(*this);
152 
153  result /= value;
154  return result;
155  }
156 
158  inline T &
159  operator()(unsigned int row, unsigned int col)
160  {
161  return m_Matrix(row, col);
162  }
163 
165  inline const T &
166  operator()(unsigned int row, unsigned int col) const
167  {
168  return m_Matrix(row, col);
169  }
170 
172  inline T * operator[](unsigned int i) { return m_Matrix[i]; }
173 
175  inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
176 
178  inline InternalMatrixType &
180  {
181  return m_Matrix;
182  }
183 
185  inline const InternalMatrixType &
186  GetVnlMatrix() const
187  {
188  return m_Matrix;
189  }
190 
192  inline void
194  {
195  m_Matrix.set_identity();
196  }
197 
199  static Self
201  {
202  InternalMatrixType internalMatrix;
203  internalMatrix.set_identity();
204  return Self{ internalMatrix };
205  }
209  inline void
210  Fill(const T & value)
211  {
212  m_Matrix.fill(value);
213  }
214 
216  inline Self &
217  operator=(const vnl_matrix<T> & matrix)
218  {
219  m_Matrix = matrix;
220  return *this;
221  }
222 
225  inline explicit Matrix(const vnl_matrix<T> & matrix)
226  : m_Matrix(matrix)
227  {}
228 
233  template <typename TElement>
234  explicit Matrix(const TElement (&elements)[VRows][VColumns])
235  : m_Matrix(&elements[0][0])
236  {
237  static_assert(std::is_same<TElement, T>::value,
238  "The type of an element should correspond with this itk::Matrix instantiation.");
239  }
240 
242  inline bool
243  operator==(const Self & matrix) const
244  {
245  bool equal = true;
246 
247  for (unsigned int r = 0; r < VRows; ++r)
248  {
249  for (unsigned int c = 0; c < VColumns; ++c)
250  {
251  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
252  {
253  equal = false;
254  break;
255  }
256  }
257  }
258  return equal;
259  }
260 
261  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
262 
263 
264  inline Self &
266  {
267  this->m_Matrix = matrix;
268  return *this;
269  }
270 
272  inline Matrix(const InternalMatrixType & matrix)
273  : m_Matrix(matrix)
274  {}
275 
277  inline vnl_matrix_fixed<T, VColumns, VRows>
278  GetInverse() const
279  {
280  if (vnl_determinant(m_Matrix) == NumericTraits<T>::ZeroValue())
281  {
282  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
283  }
284  vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
285  return vnl_matrix_fixed<T, VColumns, VRows>{ inverse.as_matrix() };
286  }
290  inline vnl_matrix_fixed<T, VColumns, VRows>
291  GetTranspose() const
292  {
293  return vnl_matrix_fixed<T, VColumns, VRows>{ m_Matrix.transpose().as_matrix() };
294  }
302  Matrix() = default;
303 
304  void
305  swap(Self & other)
306  {
307  // allow for augment dependent look up
308  using std::swap;
309  swap(this->m_Matrix, other.m_Matrix);
310  }
311 
312 private:
313  InternalMatrixType m_Matrix{};
314 };
315 
316 template <typename T, unsigned int VRows, unsigned int VColumns>
317 std::ostream &
318 operator<<(std::ostream & os, const Matrix<T, VRows, VColumns> & v)
319 {
320  os << v.GetVnlMatrix();
321  return os;
322 }
323 
324 
325 template <typename T, unsigned int VRows, unsigned int VColumns>
326 inline void
328 {
329  a.swap(b);
330 }
331 
332 } // end namespace itk
333 
334 #ifndef ITK_MANUAL_INSTANTIATION
335 # include "itkMatrix.hxx"
336 #endif
337 
338 #endif
itk::Matrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:166
itk::Matrix::operator=
Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:217
itk::Matrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:175
itkCovariantVector.h
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
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:149
itkPoint.h
itk::Matrix::operator/=
void operator/=(const T &value)
Definition: itkMatrix.h:142
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:67
itk::Matrix::operator*
Self operator*(const T &value) const
Definition: itkMatrix.h:132
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
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:225
itk::Matrix::SetIdentity
void SetIdentity()
Definition: itkMatrix.h:193
itk::Matrix::operator==
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:243
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
itk::Matrix::GetIdentity
static Self GetIdentity()
Definition: itkMatrix.h:200
itk::Matrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:159
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::ValueType
double ValueType
Definition: itkMatrix.h:59
itk::Matrix::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:186
itk::Matrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:179
itk::Matrix::operator*
Matrix< T, VRows, OuterDim > operator*(const vnl_matrix_fixed< T, VRows, OuterDim > &matrix) const
Definition: itkMatrix.h:90
itk::Matrix::swap
void swap(Self &other)
Definition: itkMatrix.h:305
itk::Matrix::operator=
Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:265
itk::Matrix::operator[]
T * operator[](unsigned int i)
Definition: itkMatrix.h:172
itk::Matrix::Matrix
Matrix(const TElement(&elements)[VRows][VColumns])
Definition: itkMatrix.h:234
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:313
itk::Matrix::Fill
void Fill(const T &value)
Definition: itkMatrix.h:210
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:59
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:70
itk::swap
void swap(const Matrix< T, VRows, VColumns > &a, const Matrix< T, VRows, VColumns > &b)
Definition: itkMatrix.h:327
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
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:126
AddImageFilter
Definition: itkAddImageFilter.h:80
itk::Matrix::GetTranspose
vnl_matrix_fixed< T, VColumns, VRows > GetTranspose() const
Definition: itkMatrix.h:291
itk::Matrix::GetInverse
vnl_matrix_fixed< T, VColumns, VRows > GetInverse() const
Definition: itkMatrix.h:278
itkMath.h
itk::Matrix::Matrix
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:272