ITK  5.3.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  * 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 {
50 template <typename T, unsigned int NRows = 3, unsigned int NColumns = 3>
51 class ITK_TEMPLATE_EXPORT Matrix
52 {
53 public:
55  using Self = Matrix;
56 
58  using ValueType = T;
59  using ComponentType = T;
60 
62  static constexpr unsigned int RowDimensions = NRows;
63  static constexpr unsigned int ColumnDimensions = NColumns;
64 
66  using InternalMatrixType = vnl_matrix_fixed<T, NRows, NColumns>;
67 
72 
75 
77  Point<T, NRows> operator*(const Point<T, NColumns> & pnt) const;
78 
81 
83  vnl_vector_fixed<T, NRows> operator*(const vnl_vector_fixed<T, NColumns> & inVNLvect) const;
84 
86  Self operator*(const CompatibleSquareMatrixType & matrix) const;
87 
88  template <unsigned int OuterDim>
89  Matrix<T, NRows, OuterDim> operator*(const vnl_matrix_fixed<T, NRows, OuterDim> & matrix) const
90  {
91  const Matrix<T, NRows, OuterDim> result(m_Matrix * matrix);
92  return result;
93  }
94 
96  Self
97  operator+(const Self & matrix) const;
98 
99  const Self &
100  operator+=(const Self & matrix);
101 
103  Self
104  operator-(const Self & matrix) const;
105 
106  const Self &
107  operator-=(const Self & matrix);
108 
110  vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
111 
113  void
114  operator*=(const CompatibleSquareMatrixType & matrix);
115 
117  void
118  operator*=(const vnl_matrix<T> & matrix);
119 
121  vnl_vector<T> operator*(const vnl_vector<T> & vc) const;
122 
124  void
125  operator*=(const T & value)
126  {
127  m_Matrix *= value;
128  }
129 
131  Self operator*(const T & value) const
132  {
133  Self result(*this);
134 
135  result *= value;
136  return result;
137  }
138 
140  void
141  operator/=(const T & value)
142  {
143  m_Matrix /= value;
144  }
145 
147  Self
148  operator/(const T & value) const
149  {
150  Self result(*this);
151 
152  result /= value;
153  return result;
154  }
155 
157  inline T &
158  operator()(unsigned int row, unsigned int col)
159  {
160  return m_Matrix(row, col);
161  }
162 
164  inline const T &
165  operator()(unsigned int row, unsigned int col) const
166  {
167  return m_Matrix(row, col);
168  }
169 
171  inline T * operator[](unsigned int i) { return m_Matrix[i]; }
172 
174  inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
175 
177  inline InternalMatrixType &
179  {
180  return m_Matrix;
181  }
182 
184  inline const InternalMatrixType &
185  GetVnlMatrix() const
186  {
187  return m_Matrix;
188  }
189 
191  inline void
193  {
194  m_Matrix.set_identity();
195  }
196 
198  static Self
200  {
201  InternalMatrixType internalMatrix;
202  internalMatrix.set_identity();
203  return Self{ internalMatrix };
204  }
206 
208  inline void
209  Fill(const T & value)
210  {
211  m_Matrix.fill(value);
212  }
213 
215  inline const Self &
216  operator=(const vnl_matrix<T> & matrix)
217  {
218  m_Matrix = matrix;
219  return *this;
220  }
221 
224  inline explicit Matrix(const vnl_matrix<T> & matrix)
225  : m_Matrix(matrix)
226  {}
227 
229  inline bool
230  operator==(const Self & matrix) const
231  {
232  bool equal = true;
233 
234  for (unsigned int r = 0; r < NRows; ++r)
235  {
236  for (unsigned int c = 0; c < NColumns; ++c)
237  {
238  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
239  {
240  equal = false;
241  break;
242  }
243  }
244  }
245  return equal;
246  }
247 
248  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
249 
250 
251  inline const Self &
253  {
254  this->m_Matrix = matrix;
255  return *this;
256  }
257 
259  inline Matrix(const InternalMatrixType & matrix)
260  : m_Matrix(matrix)
261  {}
262 
264  inline vnl_matrix_fixed<T, NColumns, NRows>
265  GetInverse() const
266  {
267  if (vnl_determinant(m_Matrix) == NumericTraits<T>::ZeroValue())
268  {
269  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
270  }
271  vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
272  return vnl_matrix_fixed<T, NColumns, NRows>{ inverse.as_matrix() };
273  }
275 
277  inline vnl_matrix_fixed<T, NColumns, NRows>
278  GetTranspose() const
279  {
280  return vnl_matrix_fixed<T, NColumns, NRows>{ m_Matrix.transpose().as_matrix() };
281  }
283 
289  Matrix() = default;
290 
291  void
292  swap(Self & other)
293  {
294  // allow for augment dependent look up
295  using std::swap;
296  swap(this->m_Matrix, other.m_Matrix);
297  }
298 
299 private:
300  InternalMatrixType m_Matrix{};
301 };
302 
303 template <typename T, unsigned int NRows, unsigned int NColumns>
304 std::ostream &
305 operator<<(std::ostream & os, const Matrix<T, NRows, NColumns> & v)
306 {
307  os << v.GetVnlMatrix();
308  return os;
309 }
310 
311 
312 template <typename T, unsigned int NRows, unsigned int NColumns>
313 inline void
315 {
316  a.swap(b);
317 }
318 
319 } // end namespace itk
320 
321 #ifndef ITK_MANUAL_INSTANTIATION
322 # include "itkMatrix.hxx"
323 #endif
324 
325 #endif
itk::Matrix::swap
void swap(Self &other)
Definition: itkMatrix.h:292
itk::swap
void swap(const Matrix< T, NRows, NColumns > &a, const Matrix< T, NRows, NColumns > &b)
Definition: itkMatrix.h:314
itk::Matrix::SetIdentity
void SetIdentity()
Definition: itkMatrix.h:192
itkCovariantVector.h
itk::Matrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:178
itk::Matrix::Fill
void Fill(const T &value)
Definition: itkMatrix.h:209
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::Matrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:174
itk::Matrix::GetIdentity
static Self GetIdentity()
Definition: itkMatrix.h:199
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::InternalMatrixType
vnl_matrix_fixed< double, NRows, NColumns > InternalMatrixType
Definition: itkMatrix.h:66
itkPoint.h
itk::Matrix::operator*=
void operator*=(const T &value)
Definition: itkMatrix.h:125
itk::Matrix::m_Matrix
InternalMatrixType m_Matrix
Definition: itkMatrix.h:300
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::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:185
itk::Matrix::operator=
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:252
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
itk::Matrix::GetInverse
vnl_matrix_fixed< T, NColumns, NRows > GetInverse() const
Definition: itkMatrix.h:265
itk::Matrix::operator*
Matrix< T, NRows, OuterDim > operator*(const vnl_matrix_fixed< T, NRows, OuterDim > &matrix) const
Definition: itkMatrix.h:89
itk::Matrix::Matrix
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:224
itk::Matrix::operator[]
T * operator[](unsigned int i)
Definition: itkMatrix.h:171
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::ComponentType
double ComponentType
Definition: itkMatrix.h:59
itk::Matrix::GetTranspose
vnl_matrix_fixed< T, NColumns, NRows > GetTranspose() const
Definition: itkMatrix.h:278
itk::Matrix::Matrix
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:259
itk::Matrix::operator=
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:216
itk::Matrix
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:51
itk::operator*
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
Definition: itkCovariantVector.h:275
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:70
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Matrix::operator/=
void operator/=(const T &value)
Definition: itkMatrix.h:141
itk::Matrix::operator/
Self operator/(const T &value) const
Definition: itkMatrix.h:148
itk::Matrix::operator==
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:230
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:654
itk::Matrix::operator*
Self operator*(const T &value) const
Definition: itkMatrix.h:131
itk::Point
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:53
itk::Matrix< double, Self::ImageDimension, Self::ImageDimension >::ValueType
double ValueType
Definition: itkMatrix.h:58
itk::Matrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:158
itkMath.h
itk::Matrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:165