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 
74  Vector<T, NRows> operator*(const Vector<T, NColumns> & vector) const;
75 
77  Point<T, NRows> operator*(const Point<T, NColumns> & vector) const;
78 
81 
83  vnl_vector_fixed<T, NRows> operator*(const vnl_vector_fixed<T, NColumns> & vector) 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> & matrix) 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) { this->operator=(matrix); }
214 
216  inline bool
217  operator==(const Self & matrix) const
218  {
219  bool equal = true;
220 
221  for (unsigned int r = 0; r < NRows; r++)
222  {
223  for (unsigned int c = 0; c < NColumns; c++)
224  {
225  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
226  {
227  equal = false;
228  break;
229  }
230  }
231  }
232  return equal;
233  }
234 
235  inline bool
236  operator!=(const Self & matrix) const
237  {
238  return !this->operator==(matrix);
239  }
240 
241  inline const Self &
243  {
244  this->m_Matrix = matrix;
245  return *this;
246  }
247 
249  inline explicit Matrix(const InternalMatrixType & matrix) { this->operator=(matrix); }
250 
252  inline const Self &
253  operator=(const Self & matrix)
254  {
255  m_Matrix = matrix.m_Matrix;
256  return *this;
257  }
258 
260  inline vnl_matrix_fixed<T, NColumns, NRows>
261  GetInverse() const
262  {
263  if (vnl_determinant(m_Matrix) == NumericTraits<T>::ZeroValue())
264  {
265  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
266  }
267  vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
268  return vnl_matrix_fixed<T, NColumns, NRows>{ inverse.as_matrix() };
269  }
271 
273  inline vnl_matrix_fixed<T, NColumns, NRows>
274  GetTranspose() const
275  {
276  return vnl_matrix_fixed<T, NColumns, NRows>{ m_Matrix.transpose().as_matrix() };
277  }
279 
282  : m_Matrix(NumericTraits<T>::ZeroValue())
283  {}
284 
286  Matrix(const Self & matrix)
287  : m_Matrix(matrix.m_Matrix)
288  {}
289 
290  void
291  swap(Self & other)
292  {
293  // allow for augment dependent look up
294  using std::swap;
295  swap(this->m_Matrix, other.m_Matrix);
296  }
297 
298 private:
300 };
301 
302 template <typename T, unsigned int NRows, unsigned int NColumns>
303 std::ostream &
304 operator<<(std::ostream & os, const Matrix<T, NRows, NColumns> & v)
305 {
306  os << v.GetVnlMatrix();
307  return os;
308 }
309 
310 
311 template <typename T, unsigned int NRows, unsigned int NColumns>
312 inline void
314 {
315  a.swap(b);
316 }
317 
318 } // end namespace itk
319 
320 #ifndef ITK_MANUAL_INSTANTIATION
321 # include "itkMatrix.hxx"
322 #endif
323 
324 #endif
itk::Matrix::swap
void swap(Self &other)
Definition: itkMatrix.h:291
itk::swap
void swap(const Matrix< T, NRows, NColumns > &a, const Matrix< T, NRows, NColumns > &b)
Definition: itkMatrix.h:313
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:213
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:236
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:299
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:239
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itk::Matrix::Matrix
Matrix(const Self &matrix)
Definition: itkMatrix.h:286
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:185
itk::Matrix::operator=
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:242
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:253
itk::Matrix::GetInverse
vnl_matrix_fixed< T, NColumns, NRows > GetInverse() const
Definition: itkMatrix.h:261
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:274
itk::Matrix::Matrix
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:249
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:217
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: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
itk::Matrix::Matrix
Matrix()
Definition: itkMatrix.h:281
itkMath.h
itk::Matrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:165