ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkVariableSizeMatrix.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 itkVariableSizeMatrix_h
19#define itkVariableSizeMatrix_h
20
21#include "itkPoint.h"
22#include "itkCovariantVector.h"
23#include "vnl/vnl_matrix_fixed.h"
24#include "itkMathSVD.h"
25#if !defined(ITK_LEGACY_REMOVE) && !defined(ITK_FUTURE_LEGACY_REMOVE)
26# include "vnl/algo/vnl_matrix_inverse.h" // transitional transitive include; dropped on ITK legacy removal
27#endif
28#include "vnl/vnl_transpose.h"
29#include "vnl/vnl_matrix.h"
30#include "itkArray.h"
31#include "itkMath.h"
32
33namespace itk
34{
46
47template <typename T>
48class ITK_TEMPLATE_EXPORT VariableSizeMatrix
49{
50public:
53
55 using ValueType = T;
56 using ComponentType = T;
57
59 using InternalMatrixType = vnl_matrix<T>;
60
63 operator*(const Array<T> & vect) const;
64
66 Self
67 operator*(const Self & matrix) const;
68
70 Self
71 operator+(const Self & matrix) const;
72
73 const Self &
74 operator+=(const Self & matrix);
75
77 Self
78 operator-(const Self & matrix) const;
79
80 const Self &
81 operator-=(const Self & matrix);
82
84 Self &
86
88 vnl_matrix<T>
89 operator*(const vnl_matrix<T> & matrix) const;
90
92 void
93 operator*=(const Self & matrix);
94
96 void
97 operator*=(const vnl_matrix<T> & matrix);
98
100 vnl_vector<T>
101 operator*(const vnl_vector<T> & vc) const;
102
104 void
105 operator*=(const T & value)
106 {
107 m_Matrix *= value;
108 }
109
111 Self
112 operator*(const T & value) const
113 {
114 Self result(*this);
115
116 result *= value;
117 return result;
118 }
119
121 void
122 operator/=(const T & value)
123 {
124 m_Matrix /= value;
125 }
126
128 Self
129 operator/(const T & value) const
130 {
131 Self result(*this);
132
133 result /= value;
134 return result;
135 }
136
138 inline T &
139 operator()(unsigned int row, unsigned int col)
140 {
141 return m_Matrix(row, col);
142 }
143
145 inline const T &
146 operator()(unsigned int row, unsigned int col) const
147 {
148 return m_Matrix(row, col);
149 }
150
152 inline T *
153 operator[](unsigned int i)
154 {
155 return m_Matrix[i];
156 }
157
159 inline const T *
160 operator[](unsigned int i) const
161 {
162 return m_Matrix[i];
163 }
164
166 inline InternalMatrixType &
168 {
169 return m_Matrix;
170 }
171
173 [[nodiscard]] inline const InternalMatrixType &
175 {
176 return m_Matrix;
177 }
178
180 inline void
182 {
183 m_Matrix.set_identity();
184 }
185
187 inline void
188 Fill(const T & value)
189 {
190 m_Matrix.fill(value);
191 }
192
194 inline Self &
195 operator=(const vnl_matrix<T> & matrix)
196 {
197 m_Matrix = matrix;
198 return *this;
199 }
200
202 inline bool
203 operator==(const Self & matrix) const;
204
206
208 inline Self &
209 operator=(const Self & matrix) = default;
210
212 [[nodiscard]] inline vnl_matrix<T>
214 {
215 return itk::Math::SVD(m_Matrix).PseudoInverse();
216 }
217
219 [[nodiscard]] inline vnl_matrix<T>
221 {
222 return m_Matrix.transpose();
223 }
224
227 : m_Matrix()
228 {}
229
230 VariableSizeMatrix(unsigned int rows, unsigned int cols);
231
233 VariableSizeMatrix(const Self & matrix)
234 : m_Matrix(matrix.m_Matrix)
235 {}
236
238 [[nodiscard]] inline unsigned int
239 Rows() const
240 {
241 return m_Matrix.rows();
242 }
243
245 [[nodiscard]] inline unsigned int
246 Cols() const
247 {
248 return m_Matrix.cols();
249 }
250
252 inline bool
253 SetSize(unsigned int r, unsigned int c)
254 {
255 return m_Matrix.set_size(r, c);
256 }
257
258private:
260};
261
262template <typename T>
263std::ostream &
264operator<<(std::ostream & os, const VariableSizeMatrix<T> & v)
265{
266 os << v.GetVnlMatrix();
267 return os;
268}
269
273template <typename T>
274inline bool
276{
277 if ((matrix.Rows() != this->Rows()) || (matrix.Cols() != this->Cols()))
278 {
279 return false;
280 }
281 bool equal = true;
282
283 for (unsigned int r = 0; r < this->Rows(); ++r)
284 {
285 for (unsigned int c = 0; c < this->Cols(); ++c)
286 {
287 if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
288 {
289 equal = false;
290 break;
291 }
292 }
293 }
294 return equal;
295}
296} // end namespace itk
297
298#ifndef ITK_MANUAL_INSTANTIATION
299# include "itkVariableSizeMatrix.hxx"
300#endif
301
302#endif
Array class with size defined at construction time.
Definition itkArray.h:48
A templated class holding a M x N size Matrix.
void operator*=(const vnl_matrix< T > &matrix)
const Self & operator+=(const Self &matrix)
bool operator==(const Self &matrix) const
vnl_matrix< T > GetTranspose() const
Array< T > operator*(const Array< T > &vect) const
bool SetSize(unsigned int r, unsigned int c)
VariableSizeMatrix(unsigned int rows, unsigned int cols)
Self operator*(const T &value) const
vnl_matrix< T > operator*(const vnl_matrix< T > &matrix) const
void operator*=(const T &value)
VariableSizeMatrix(const Self &matrix)
vnl_vector< T > operator*(const vnl_vector< T > &vc) const
Self operator*(const Self &matrix) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
const Self & operator-=(const Self &matrix)
void operator/=(const T &value)
Self operator/(const T &value) const
Self operator+(const Self &matrix) const
T & operator()(unsigned int row, unsigned int col)
const T & operator()(unsigned int row, unsigned int col) const
const InternalMatrixType & GetVnlMatrix() const
vnl_matrix< T > GetInverse() const
void operator*=(const Self &matrix)
Self & operator=(const vnl_matrix< T > &matrix)
const T * operator[](unsigned int i) const
Self & operator=(const Self &matrix)=default
InternalMatrixType & GetVnlMatrix()
Self operator-(const Self &matrix) const
FixedSquareSVDResult< TReal, VDim > SVD(const vnl_matrix_fixed< TReal, VDim, VDim > &A, bool canonicalizeSigns=true)
Singular value decomposition A = U diag(W) V^T, backed by Eigen.
Definition itkMathSVD.h:574
constexpr bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition itkMath.h:754
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, AnatomicalOrientation::CoordinateEnum value)