ITK  5.2.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  inline void
199  Fill(const T & value)
200  {
201  m_Matrix.fill(value);
202  }
203 
205  inline const Self &
206  operator=(const vnl_matrix<T> & matrix)
207  {
208  m_Matrix = matrix;
209  return *this;
210  }
211 
213  inline Matrix(const vnl_matrix<T> & matrix)
214  : m_Matrix(matrix)
215  {}
216 
218  inline bool
219  operator==(const Self & matrix) const
220  {
221  bool equal = true;
222 
223  for (unsigned int r = 0; r < NRows; r++)
224  {
225  for (unsigned int c = 0; c < NColumns; c++)
226  {
227  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
228  {
229  equal = false;
230  break;
231  }
232  }
233  }
234  return equal;
235  }
236 
237  inline bool
238  operator!=(const Self & matrix) const
239  {
240  return !this->operator==(matrix);
241  }
242 
243  inline const Self &
245  {
246  this->m_Matrix = matrix;
247  return *this;
248  }
249 
251  inline explicit Matrix(const InternalMatrixType & matrix)
252  : m_Matrix(matrix)
253  {}
254 
256  inline vnl_matrix_fixed<T, NColumns, NRows>
257  GetInverse() const
258  {
259  if (vnl_determinant(m_Matrix) == NumericTraits<T>::ZeroValue())
260  {
261  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
262  }
263  vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
264  return vnl_matrix_fixed<T, NColumns, NRows>{ inverse.as_matrix() };
265  }
267 
269  inline vnl_matrix_fixed<T, NColumns, NRows>
270  GetTranspose() const
271  {
272  return vnl_matrix_fixed<T, NColumns, NRows>{ m_Matrix.transpose().as_matrix() };
273  }
275 
281  Matrix() = default;
282 
283  void
284  swap(Self & other)
285  {
286  // allow for augment dependent look up
287  using std::swap;
288  swap(this->m_Matrix, other.m_Matrix);
289  }
290 
291 private:
292  InternalMatrixType m_Matrix{};
293 };
294 
295 template <typename T, unsigned int NRows, unsigned int NColumns>
296 std::ostream &
297 operator<<(std::ostream & os, const Matrix<T, NRows, NColumns> & v)
298 {
299  os << v.GetVnlMatrix();
300  return os;
301 }
302 
303 
304 template <typename T, unsigned int NRows, unsigned int NColumns>
305 inline void
307 {
308  a.swap(b);
309 }
310 
311 } // end namespace itk
312 
313 #ifndef ITK_MANUAL_INSTANTIATION
314 # include "itkMatrix.hxx"
315 #endif
316 
317 #endif
itk::Matrix::swap
void swap(Self &other)
Definition: itkMatrix.h:284
itk::swap
void swap(const Matrix< T, NRows, NColumns > &a, const Matrix< T, NRows, NColumns > &b)
Definition: itkMatrix.h:306
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:199
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::Matrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:174
itk::Matrix::operator!=
bool operator!=(const Self &matrix) const
Definition: itkMatrix.h:238
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:292
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:244
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:679
itk::Matrix::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:185
itk::Matrix::operator=
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:244
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:257
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:213
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:270
itk::Matrix::Matrix
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:251
itk::Matrix::operator=
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:206
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: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:219
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:661
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