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
97
100
102 vnl_vector_fixed<T, VRows> operator*(const vnl_vector_fixed<T, VColumns> & inVNLvect) const;
103
106
107 template <unsigned int OuterDim>
108 Matrix<T, VRows, OuterDim> operator*(const vnl_matrix_fixed<T, VRows, OuterDim> & matrix) const
109 {
110 const Matrix<T, VRows, OuterDim> result(m_Matrix * matrix);
111 return result;
112 }
113
115 Self
116 operator+(const Self & matrix) const;
117
118 const Self &
119 operator+=(const Self & matrix);
120
122 Self
123 operator-(const Self & matrix) const;
124
125 const Self &
126 operator-=(const Self & matrix);
127
129 vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
130
132 void
134
136 void
137 operator*=(const vnl_matrix<T> & matrix);
138
140 vnl_vector<T> operator*(const vnl_vector<T> & vc) const;
141
143 void
144 operator*=(const T & value)
145 {
146 m_Matrix *= value;
147 }
148
150 Self operator*(const T & value) const
151 {
152 Self result(*this);
153
154 result *= value;
155 return result;
156 }
157
159 void
160 operator/=(const T & value)
161 {
162 m_Matrix /= value;
163 }
164
166 Self
167 operator/(const T & value) const
168 {
169 Self result(*this);
170
171 result /= value;
172 return result;
173 }
174
176 inline T &
177 operator()(unsigned int row, unsigned int col)
178 {
179 return m_Matrix(row, col);
180 }
181
183 inline const T &
184 operator()(unsigned int row, unsigned int col) const
185 {
186 return m_Matrix(row, col);
187 }
188
190 inline T * operator[](unsigned int i) { return m_Matrix[i]; }
191
193 inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
194
196 inline InternalMatrixType &
198 {
199 return m_Matrix;
200 }
201
203 inline const InternalMatrixType &
205 {
206 return m_Matrix;
207 }
208
210 inline void
212 {
213 m_Matrix.set_identity();
214 }
215
217 static Self
219 {
220 InternalMatrixType internalMatrix;
221 internalMatrix.set_identity();
222 return Self{ internalMatrix };
223 }
227 inline void
228 Fill(const T & value)
229 {
230 m_Matrix.fill(value);
231 }
232
234 inline Self &
235 operator=(const vnl_matrix<T> & matrix)
236 {
237 m_Matrix = matrix;
238 return *this;
239 }
240
243 inline explicit Matrix(const vnl_matrix<T> & matrix)
244 : m_Matrix(matrix)
245 {}
246
251 template <typename TElement>
252 explicit Matrix(const TElement (&elements)[VRows][VColumns])
253 : m_Matrix(&elements[0][0])
254 {
255 static_assert(std::is_same_v<TElement, T>,
256 "The type of an element should correspond with this itk::Matrix instantiation.");
257 }
258
260 inline bool
261 operator==(const Self & matrix) const
262 {
263 bool equal = true;
264
265 for (unsigned int r = 0; r < VRows; ++r)
266 {
267 for (unsigned int c = 0; c < VColumns; ++c)
268 {
269 if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
270 {
271 equal = false;
272 break;
273 }
274 }
275 }
276 return equal;
277 }
278
280
281
282 inline Self &
284 {
285 this->m_Matrix = matrix;
286 return *this;
287 }
288
290 inline Matrix(const InternalMatrixType & matrix)
291 : m_Matrix(matrix)
292 {}
293
295 inline vnl_matrix_fixed<T, VColumns, VRows>
297 {
298 if (vnl_determinant(m_Matrix) == T{})
299 {
300 itkGenericExceptionMacro("Singular matrix. Determinant is 0.");
301 }
302 vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
303 return vnl_matrix_fixed<T, VColumns, VRows>{ inverse.as_matrix() };
304 }
308 inline vnl_matrix_fixed<T, VColumns, VRows>
310 {
311 return vnl_matrix_fixed<T, VColumns, VRows>{ m_Matrix.transpose().as_matrix() };
312 }
320 Matrix() = default;
321
323 constexpr unsigned int
324 size() const
325 {
326 return m_Matrix.size();
327 }
328
330 iterator
332 {
333 return m_Matrix.begin();
334 }
335
337 iterator
339 {
340 return m_Matrix.end();
341 }
342
344 const_iterator
345 begin() const
346 {
347 return m_Matrix.begin();
348 }
349
351 const_iterator
352 end() const
353 {
354 return m_Matrix.end();
355 }
356
358 const_iterator
359 cbegin() const
360 {
361 return m_Matrix.begin();
362 }
363
365 const_iterator
366 cend() const
367 {
368 return m_Matrix.end();
369 }
370
371 void
372 swap(Self & other)
373 {
374 // allow for augment dependent look up
375 using std::swap;
376 swap(this->m_Matrix, other.m_Matrix);
377 }
378
379 inline void
380 PrintSelf(std::ostream & os, Indent indent) const
381 {
382 os << indent << "Matrix (" << VRows << "x" << VColumns << ")\n";
383 for (unsigned int r = 0; r < VRows; ++r)
384 {
385 os << indent << " ";
386 for (unsigned int c = 0; c < VColumns; ++c)
387 {
388 os << m_Matrix(r, c) << " ";
389 }
390 os << "\n";
391 }
392 }
393
394private:
396};
397
398template <typename T, unsigned int VRows, unsigned int VColumns>
399std::ostream &
400operator<<(std::ostream & os, const Matrix<T, VRows, VColumns> & v)
401{
402 os << v.GetVnlMatrix();
403 return os;
404}
405
406
407template <typename T, unsigned int VRows, unsigned int VColumns>
408inline void
410{
411 a.swap(b);
412}
413
414} // end namespace itk
415
416#ifndef ITK_MANUAL_INSTANTIATION
417# include "itkMatrix.hxx"
418#endif
419
420#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:324
const_iterator cbegin() const
Definition: itkMatrix.h:359
iterator end()
Definition: itkMatrix.h:338
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:177
Self operator/(const T &value) const
Definition: itkMatrix.h:167
void operator/=(const T &value)
Definition: itkMatrix.h:160
T * operator[](unsigned int i)
Definition: itkMatrix.h:190
void Fill(const T &value)
Definition: itkMatrix.h:228
Self operator-(const Self &matrix) const
Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:283
const_iterator end() const
Definition: itkMatrix.h:352
Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:235
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:261
void SetIdentity()
Definition: itkMatrix.h:211
vnl_vector< T > operator*(const vnl_vector< T > &vc) const
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:184
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:197
const_iterator begin() const
Definition: itkMatrix.h:345
Matrix< T, VRows, OuterDim > operator*(const vnl_matrix_fixed< T, VRows, OuterDim > &matrix) const
Definition: itkMatrix.h:108
static Self GetIdentity()
Definition: itkMatrix.h:218
void swap(Self &other)
Definition: itkMatrix.h:372
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:290
const ValueType & const_reference
Definition: itkMatrix.h:72
void PrintSelf(std::ostream &os, Indent indent) const
Definition: itkMatrix.h:380
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:243
const_iterator cend() const
Definition: itkMatrix.h:366
Self operator*(const T &value) const
Definition: itkMatrix.h:150
Self operator+(const Self &matrix) const
vnl_matrix_fixed< T, VColumns, VRows > GetTranspose() const
Definition: itkMatrix.h:309
void operator*=(const CompatibleSquareMatrixType &matrix)
Matrix()=default
const ValueType * const_pointer
Definition: itkMatrix.h:66
vnl_matrix_fixed< T, VColumns, VRows > GetInverse() const
Definition: itkMatrix.h:296
vnl_matrix_fixed< T, VRows, VColumns > InternalMatrixType
Definition: itkMatrix.h:85
iterator begin()
Definition: itkMatrix.h:331
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:204
ValueType * iterator
Definition: itkMatrix.h:75
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:193
void operator*=(const T &value)
Definition: itkMatrix.h:144
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:252
InternalMatrixType m_Matrix
Definition: itkMatrix.h:395
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:737
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)
Definition: itkMatrix.h:409