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
33namespace itk
34{
51template <typename T, unsigned int VRows = 3, unsigned int VColumns = 3>
52class ITK_TEMPLATE_EXPORT Matrix
53{
54public:
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
70
72 using const_reference = const ValueType &;
73
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 operator*(const Vector<T, VColumns> & vect) const;
95
98 operator*(const Point<T, VColumns> & pnt) const;
99
103
105 vnl_vector_fixed<T, VRows>
106 operator*(const vnl_vector_fixed<T, VColumns> & inVNLvect) const;
107
109 Self
111
112 template <unsigned int OuterDim>
114 operator*(const vnl_matrix_fixed<T, VRows, OuterDim> & matrix) const
115 {
116 const Matrix<T, VRows, OuterDim> result(m_Matrix * matrix);
117 return result;
118 }
119
121 Self
122 operator+(const Self & matrix) const;
123
124 const Self &
125 operator+=(const Self & matrix);
126
128 Self
129 operator-(const Self & matrix) const;
130
131 const Self &
132 operator-=(const Self & matrix);
133
135 vnl_matrix<T>
136 operator*(const vnl_matrix<T> & matrix) const;
137
139 void
141
143 void
144 operator*=(const vnl_matrix<T> & matrix);
145
147 vnl_vector<T>
148 operator*(const vnl_vector<T> & vc) const;
149
151 void
152 operator*=(const T & value)
153 {
154 m_Matrix *= value;
155 }
156
158 Self
159 operator*(const T & value) const
160 {
161 Self result(*this);
162
163 result *= value;
164 return result;
165 }
166
168 void
169 operator/=(const T & value)
170 {
171 m_Matrix /= value;
172 }
173
175 Self
176 operator/(const T & value) const
177 {
178 Self result(*this);
179
180 result /= value;
181 return result;
182 }
183
185 inline T &
186 operator()(unsigned int row, unsigned int col)
187 {
188 return m_Matrix(row, col);
189 }
190
192 inline const T &
193 operator()(unsigned int row, unsigned int col) const
194 {
195 return m_Matrix(row, col);
196 }
197
199 inline T *
200 operator[](unsigned int i)
201 {
202 return m_Matrix[i];
203 }
204
206 inline const T *
207 operator[](unsigned int i) const
208 {
209 return m_Matrix[i];
210 }
211
213 inline InternalMatrixType &
215 {
216 return m_Matrix;
217 }
218
220 inline const InternalMatrixType &
222 {
223 return m_Matrix;
224 }
225
227 inline void
229 {
230 m_Matrix.set_identity();
231 }
232
234 static Self
236 {
237 InternalMatrixType internalMatrix;
238 internalMatrix.set_identity();
239 return Self{ internalMatrix };
240 }
244 inline void
245 Fill(const T & value)
246 {
247 m_Matrix.fill(value);
248 }
249
251 inline Self &
252 operator=(const vnl_matrix<T> & matrix)
253 {
254 m_Matrix = matrix;
255 return *this;
256 }
257
260 inline explicit Matrix(const vnl_matrix<T> & matrix)
261 : m_Matrix(matrix)
262 {}
263
268 template <typename TElement>
269 explicit Matrix(const TElement (&elements)[VRows][VColumns])
270 : m_Matrix(&elements[0][0])
271 {
272 static_assert(std::is_same_v<TElement, T>,
273 "The type of an element should correspond with this itk::Matrix instantiation.");
274 }
275
277 inline bool
278 operator==(const Self & matrix) const
279 {
280 bool equal = true;
281
282 for (unsigned int r = 0; r < VRows; ++r)
283 {
284 for (unsigned int c = 0; c < VColumns; ++c)
285 {
286 if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
287 {
288 equal = false;
289 break;
290 }
291 }
292 }
293 return equal;
294 }
295
297
298
299 inline Self &
301 {
302 this->m_Matrix = matrix;
303 return *this;
304 }
305
307 inline Matrix(const InternalMatrixType & matrix)
308 : m_Matrix(matrix)
309 {}
310
312 inline vnl_matrix_fixed<T, VColumns, VRows>
314 {
315 if (vnl_determinant(m_Matrix) == T{})
316 {
317 itkGenericExceptionMacro("Singular matrix. Determinant is 0.");
318 }
319 const vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
320 return vnl_matrix_fixed<T, VColumns, VRows>{ inverse.as_matrix() };
321 }
325 inline vnl_matrix_fixed<T, VColumns, VRows>
327 {
328 return vnl_matrix_fixed<T, VColumns, VRows>{ m_Matrix.transpose().as_matrix() };
329 }
337 Matrix() = default;
338
340 constexpr unsigned int
341 size() const
342 {
343 return m_Matrix.size();
344 }
345
347 iterator
349 {
350 return m_Matrix.begin();
351 }
352
354 iterator
356 {
357 return m_Matrix.end();
358 }
359
361 const_iterator
362 begin() const
363 {
364 return m_Matrix.begin();
365 }
366
368 const_iterator
369 end() const
370 {
371 return m_Matrix.end();
372 }
373
375 const_iterator
376 cbegin() const
377 {
378 return m_Matrix.begin();
379 }
380
382 const_iterator
383 cend() const
384 {
385 return m_Matrix.end();
386 }
387
388 void
389 swap(Self & other) noexcept
390 {
391 // allow for augment dependent look up
392 using std::swap;
393 swap(this->m_Matrix, other.m_Matrix);
394 }
395
396 inline void
397 PrintSelf(std::ostream & os, Indent indent) const
398 {
399 os << indent << "Matrix (" << VRows << "x" << VColumns << ")\n";
400 for (unsigned int r = 0; r < VRows; ++r)
401 {
402 os << indent << " ";
403 for (unsigned int c = 0; c < VColumns; ++c)
404 {
405 os << m_Matrix(r, c) << " ";
406 }
407 os << "\n";
408 }
409 }
410
411private:
413};
414
415template <typename T, unsigned int VRows, unsigned int VColumns>
416std::ostream &
417operator<<(std::ostream & os, const Matrix<T, VRows, VColumns> & v)
418{
419 os << v.GetVnlMatrix();
420 return os;
421}
422
423
424template <typename T, unsigned int VRows, unsigned int VColumns>
425inline void
427{
428 a.swap(b);
429}
430
431} // end namespace itk
432
433#ifndef ITK_MANUAL_INSTANTIATION
434# include "itkMatrix.hxx"
435#endif
436
437#endif
Pixel-wise addition of two images.
A templated class holding a n-Dimensional covariant vector.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
constexpr unsigned int size() const
Definition: itkMatrix.h:341
const_iterator cbegin() const
Definition: itkMatrix.h:376
iterator end()
Definition: itkMatrix.h:355
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:186
Self operator/(const T &value) const
Definition: itkMatrix.h:176
void operator/=(const T &value)
Definition: itkMatrix.h:169
T * operator[](unsigned int i)
Definition: itkMatrix.h:200
void Fill(const T &value)
Definition: itkMatrix.h:245
Self operator-(const Self &matrix) const
Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:300
const_iterator end() const
Definition: itkMatrix.h:369
Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:252
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:278
void SetIdentity()
Definition: itkMatrix.h:228
vnl_vector< T > operator*(const vnl_vector< T > &vc) const
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:193
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:214
const_iterator begin() const
Definition: itkMatrix.h:362
Matrix< T, VRows, OuterDim > operator*(const vnl_matrix_fixed< T, VRows, OuterDim > &matrix) const
Definition: itkMatrix.h:114
static Self GetIdentity()
Definition: itkMatrix.h:235
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
ValueType & reference
Definition: itkMatrix.h:69
CovariantVector< T, VRows > operator*(const CovariantVector< T, VColumns > &covect) const
const Self & operator+=(const Self &matrix)
vnl_vector_fixed< T, VRows > operator*(const vnl_vector_fixed< T, VColumns > &inVNLvect) const
void operator*=(const vnl_matrix< T > &matrix)
const Self & operator-=(const Self &matrix)
T ComponentType
Definition: itkMatrix.h:60
vnl_matrix< T > operator*(const vnl_matrix< T > &matrix) const
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:307
const ValueType & const_reference
Definition: itkMatrix.h:72
void PrintSelf(std::ostream &os, Indent indent) const
Definition: itkMatrix.h:397
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:260
const_iterator cend() const
Definition: itkMatrix.h:383
Self operator*(const T &value) const
Definition: itkMatrix.h:159
Self operator+(const Self &matrix) const
vnl_matrix_fixed< T, VColumns, VRows > GetTranspose() const
Definition: itkMatrix.h:326
void operator*=(const CompatibleSquareMatrixType &matrix)
Matrix()=default
const ValueType * const_pointer
Definition: itkMatrix.h:66
void swap(Self &other) noexcept
Definition: itkMatrix.h:389
vnl_matrix_fixed< T, VColumns, VRows > GetInverse() const
Definition: itkMatrix.h:313
vnl_matrix_fixed< T, VRows, VColumns > InternalMatrixType
Definition: itkMatrix.h:85
iterator begin()
Definition: itkMatrix.h:348
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:221
ValueType * iterator
Definition: itkMatrix.h:75
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:207
void operator*=(const T &value)
Definition: itkMatrix.h:152
Vector< T, VRows > operator*(const Vector< T, VColumns > &vect) const
Self operator*(const CompatibleSquareMatrixType &matrix) const
Matrix(const TElement(&elements)[VRows][VColumns])
Definition: itkMatrix.h:269
InternalMatrixType m_Matrix
Definition: itkMatrix.h:412
const ValueType * const_iterator
Definition: itkMatrix.h:78
ValueType * pointer
Definition: itkMatrix.h:63
Point< T, VRows > operator*(const Point< T, VColumns > &pnt) const
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:54
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:63
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
void swap(const Matrix< T, VRows, VColumns > &a, const Matrix< T, VRows, VColumns > &b) noexcept
Definition: itkMatrix.h:426