ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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{
50
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 }
241
243 inline void
244 Fill(const T & value)
245 {
246 m_Matrix.fill(value);
247 }
248
250 inline Self &
251 operator=(const vnl_matrix<T> & matrix)
252 {
253 m_Matrix = matrix;
254 return *this;
255 }
256
259 inline explicit Matrix(const vnl_matrix<T> & matrix)
260 : m_Matrix(matrix)
261 {}
262
267 template <typename TElement>
268 explicit Matrix(const TElement (&elements)[VRows][VColumns])
269 : m_Matrix(&elements[0][0])
270 {
271 static_assert(std::is_same_v<TElement, T>,
272 "The type of an element should correspond with this itk::Matrix instantiation.");
273 }
274
276 inline bool
277 operator==(const Self & matrix) const
278 {
279 bool equal = true;
280
281 for (unsigned int r = 0; r < VRows; ++r)
282 {
283 for (unsigned int c = 0; c < VColumns; ++c)
284 {
285 if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
286 {
287 equal = false;
288 break;
289 }
290 }
291 }
292 return equal;
293 }
294
296
297
298 inline Self &
300 {
301 this->m_Matrix = matrix;
302 return *this;
303 }
304
306 inline Matrix(const InternalMatrixType & matrix)
307 : m_Matrix(matrix)
308 {}
309
311 inline vnl_matrix_fixed<T, VColumns, VRows>
313 {
314 if (vnl_determinant(m_Matrix) == T{})
315 {
316 itkGenericExceptionMacro("Singular matrix. Determinant is 0.");
317 }
318 const vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
319 return vnl_matrix_fixed<T, VColumns, VRows>{ inverse.as_matrix() };
320 }
321
323 inline vnl_matrix_fixed<T, VColumns, VRows>
325 {
326 return vnl_matrix_fixed<T, VColumns, VRows>{ m_Matrix.transpose().as_matrix() };
327 }
328
334 Matrix() = default;
335
337 constexpr unsigned int
338 size() const
339 {
340 return m_Matrix.size();
341 }
342
344 iterator
346 {
347 return m_Matrix.begin();
348 }
349
351 iterator
353 {
354 return m_Matrix.end();
355 }
356
358 const_iterator
359 begin() const
360 {
361 return m_Matrix.begin();
362 }
363
365 const_iterator
366 end() const
367 {
368 return m_Matrix.end();
369 }
370
372 const_iterator
373 cbegin() const
374 {
375 return m_Matrix.begin();
376 }
377
379 const_iterator
380 cend() const
381 {
382 return m_Matrix.end();
383 }
384
385 void
386 swap(Self & other) noexcept
387 {
388 // allow for augment dependent look up
389 using std::swap;
390 swap(this->m_Matrix, other.m_Matrix);
391 }
392
393 inline void
394 PrintSelf(std::ostream & os, Indent indent) const
395 {
396 os << indent << "Matrix (" << VRows << "x" << VColumns << ")\n";
397 for (unsigned int r = 0; r < VRows; ++r)
398 {
399 os << indent << " ";
400 for (unsigned int c = 0; c < VColumns; ++c)
401 {
402 os << m_Matrix(r, c) << " ";
403 }
404 os << "\n";
405 }
406 }
407
408private:
410};
411
412template <typename T, unsigned int VRows, unsigned int VColumns>
413std::ostream &
414operator<<(std::ostream & os, const Matrix<T, VRows, VColumns> & v)
415{
416 os << v.GetVnlMatrix();
417 return os;
418}
419
420
421template <typename T, unsigned int VRows, unsigned int VColumns>
422inline void
424{
425 a.swap(b);
426}
427
428} // end namespace itk
429
430#ifndef ITK_MANUAL_INSTANTIATION
431# include "itkMatrix.hxx"
432#endif
433
434#endif
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:338
const_iterator cbegin() const
Definition itkMatrix.h:373
iterator end()
Definition itkMatrix.h:352
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:244
Self operator-(const Self &matrix) const
Self & operator=(const InternalMatrixType &matrix)
Definition itkMatrix.h:299
Matrix< VComponent, VColumns, VColumns > CompatibleSquareMatrixType
Definition itkMatrix.h:90
const_iterator end() const
Definition itkMatrix.h:366
Self & operator=(const vnl_matrix< T > &matrix)
Definition itkMatrix.h:251
bool operator==(const Self &matrix) const
Definition itkMatrix.h:277
static constexpr unsigned int ColumnDimensions
Definition itkMatrix.h:82
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:359
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)
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)
vnl_matrix< T > operator*(const vnl_matrix< T > &matrix) const
Matrix(const InternalMatrixType &matrix)
Definition itkMatrix.h:306
static constexpr unsigned int RowDimensions
Definition itkMatrix.h:81
void PrintSelf(std::ostream &os, Indent indent) const
Definition itkMatrix.h:394
Matrix(const vnl_matrix< T > &matrix)
Definition itkMatrix.h:259
const_iterator cend() const
Definition itkMatrix.h:380
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:324
void operator*=(const CompatibleSquareMatrixType &matrix)
Matrix()=default
void swap(Self &other) noexcept
Definition itkMatrix.h:386
vnl_matrix_fixed< T, VColumns, VRows > GetInverse() const
Definition itkMatrix.h:312
vnl_matrix_fixed< VComponent, VRows, VColumns > InternalMatrixType
Definition itkMatrix.h:85
iterator begin()
Definition itkMatrix.h:345
const InternalMatrixType & GetVnlMatrix() const
Definition itkMatrix.h:221
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:268
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:730
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition itkArray.h:247
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)