ITK  5.4.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 static constexpr unsigned int RowDimensions = VRows;
64 static constexpr unsigned int ColumnDimensions = VColumns;
65
67 using InternalMatrixType = vnl_matrix_fixed<T, VRows, VColumns>;
68
73
76
79
82
84 vnl_vector_fixed<T, VRows> operator*(const vnl_vector_fixed<T, VColumns> & inVNLvect) const;
85
88
89 template <unsigned int OuterDim>
90 Matrix<T, VRows, OuterDim> operator*(const vnl_matrix_fixed<T, VRows, OuterDim> & matrix) const
91 {
92 const Matrix<T, VRows, OuterDim> result(m_Matrix * matrix);
93 return result;
94 }
95
97 Self
98 operator+(const Self & matrix) const;
99
100 const Self &
101 operator+=(const Self & matrix);
102
104 Self
105 operator-(const Self & matrix) const;
106
107 const Self &
108 operator-=(const Self & matrix);
109
111 vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
112
114 void
116
118 void
119 operator*=(const vnl_matrix<T> & matrix);
120
122 vnl_vector<T> operator*(const vnl_vector<T> & vc) const;
123
125 void
126 operator*=(const T & value)
127 {
128 m_Matrix *= value;
129 }
130
132 Self operator*(const T & value) const
133 {
134 Self result(*this);
135
136 result *= value;
137 return result;
138 }
139
141 void
142 operator/=(const T & value)
143 {
144 m_Matrix /= value;
145 }
146
148 Self
149 operator/(const T & value) const
150 {
151 Self result(*this);
152
153 result /= value;
154 return result;
155 }
156
158 inline T &
159 operator()(unsigned int row, unsigned int col)
160 {
161 return m_Matrix(row, col);
162 }
163
165 inline const T &
166 operator()(unsigned int row, unsigned int col) const
167 {
168 return m_Matrix(row, col);
169 }
170
172 inline T * operator[](unsigned int i) { return m_Matrix[i]; }
173
175 inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
176
178 inline InternalMatrixType &
180 {
181 return m_Matrix;
182 }
183
185 inline const InternalMatrixType &
187 {
188 return m_Matrix;
189 }
190
192 inline void
194 {
195 m_Matrix.set_identity();
196 }
197
199 static Self
201 {
202 InternalMatrixType internalMatrix;
203 internalMatrix.set_identity();
204 return Self{ internalMatrix };
205 }
209 inline void
210 Fill(const T & value)
211 {
212 m_Matrix.fill(value);
213 }
214
216 inline Self &
217 operator=(const vnl_matrix<T> & matrix)
218 {
219 m_Matrix = matrix;
220 return *this;
221 }
222
225 inline explicit Matrix(const vnl_matrix<T> & matrix)
226 : m_Matrix(matrix)
227 {}
228
233 template <typename TElement>
234 explicit Matrix(const TElement (&elements)[VRows][VColumns])
235 : m_Matrix(&elements[0][0])
236 {
237 static_assert(std::is_same_v<TElement, T>,
238 "The type of an element should correspond with this itk::Matrix instantiation.");
239 }
240
242 inline bool
243 operator==(const Self & matrix) const
244 {
245 bool equal = true;
246
247 for (unsigned int r = 0; r < VRows; ++r)
248 {
249 for (unsigned int c = 0; c < VColumns; ++c)
250 {
251 if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
252 {
253 equal = false;
254 break;
255 }
256 }
257 }
258 return equal;
259 }
260
262
263
264 inline Self &
266 {
267 this->m_Matrix = matrix;
268 return *this;
269 }
270
272 inline Matrix(const InternalMatrixType & matrix)
273 : m_Matrix(matrix)
274 {}
275
277 inline vnl_matrix_fixed<T, VColumns, VRows>
279 {
280 if (vnl_determinant(m_Matrix) == T{})
281 {
282 itkGenericExceptionMacro("Singular matrix. Determinant is 0.");
283 }
284 vnl_matrix_inverse<T> inverse(m_Matrix.as_ref());
285 return vnl_matrix_fixed<T, VColumns, VRows>{ inverse.as_matrix() };
286 }
290 inline vnl_matrix_fixed<T, VColumns, VRows>
292 {
293 return vnl_matrix_fixed<T, VColumns, VRows>{ m_Matrix.transpose().as_matrix() };
294 }
302 Matrix() = default;
303
304 void
305 swap(Self & other)
306 {
307 // allow for augment dependent look up
308 using std::swap;
309 swap(this->m_Matrix, other.m_Matrix);
310 }
311
312private:
314};
315
316template <typename T, unsigned int VRows, unsigned int VColumns>
317std::ostream &
318operator<<(std::ostream & os, const Matrix<T, VRows, VColumns> & v)
319{
320 os << v.GetVnlMatrix();
321 return os;
322}
323
324
325template <typename T, unsigned int VRows, unsigned int VColumns>
326inline void
328{
329 a.swap(b);
330}
331
332} // end namespace itk
333
334#ifndef ITK_MANUAL_INSTANTIATION
335# include "itkMatrix.hxx"
336#endif
337
338#endif
Pixel-wise addition of two images.
A templated class holding a n-Dimensional covariant vector.
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:159
Self operator/(const T &value) const
Definition: itkMatrix.h:149
void operator/=(const T &value)
Definition: itkMatrix.h:142
T * operator[](unsigned int i)
Definition: itkMatrix.h:172
void Fill(const T &value)
Definition: itkMatrix.h:210
Self operator-(const Self &matrix) const
Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:265
Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:217
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:243
void SetIdentity()
Definition: itkMatrix.h:193
vnl_vector< T > operator*(const vnl_vector< T > &vc) const
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:166
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:179
Matrix< T, VRows, OuterDim > operator*(const vnl_matrix_fixed< T, VRows, OuterDim > &matrix) const
Definition: itkMatrix.h:90
static Self GetIdentity()
Definition: itkMatrix.h:200
void swap(Self &other)
Definition: itkMatrix.h:305
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)
T ComponentType
Definition: itkMatrix.h:60
vnl_matrix< T > operator*(const vnl_matrix< T > &matrix) const
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:272
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:225
Self operator*(const T &value) const
Definition: itkMatrix.h:132
Self operator+(const Self &matrix) const
vnl_matrix_fixed< T, VColumns, VRows > GetTranspose() const
Definition: itkMatrix.h:291
void operator*=(const CompatibleSquareMatrixType &matrix)
Matrix()=default
vnl_matrix_fixed< T, VColumns, VRows > GetInverse() const
Definition: itkMatrix.h:278
vnl_matrix_fixed< T, VRows, VColumns > InternalMatrixType
Definition: itkMatrix.h:67
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:186
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:175
void operator*=(const T &value)
Definition: itkMatrix.h:126
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:234
InternalMatrixType m_Matrix
Definition: itkMatrix.h:313
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)
Definition: itkArray.h:242
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
void swap(const Matrix< T, VRows, VColumns > &a, const Matrix< T, VRows, VColumns > &b)
Definition: itkMatrix.h:327