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 "vnl/algo/vnl_matrix_inverse.h"
25#include "vnl/vnl_transpose.h"
26#include "vnl/vnl_matrix.h"
27#include "itkArray.h"
28#include "itkMath.h"
29
30namespace itk
31{
43
44template <typename T>
45class ITK_TEMPLATE_EXPORT VariableSizeMatrix
46{
47public:
50
52 using ValueType = T;
53 using ComponentType = T;
54
56 using InternalMatrixType = vnl_matrix<T>;
57
60 operator*(const Array<T> & vect) const;
61
63 Self
64 operator*(const Self & matrix) const;
65
67 Self
68 operator+(const Self & matrix) const;
69
70 const Self &
71 operator+=(const Self & matrix);
72
74 Self
75 operator-(const Self & matrix) const;
76
77 const Self &
78 operator-=(const Self & matrix);
79
81 Self &
83
85 vnl_matrix<T>
86 operator*(const vnl_matrix<T> & matrix) const;
87
89 void
90 operator*=(const Self & matrix);
91
93 void
94 operator*=(const vnl_matrix<T> & matrix);
95
97 vnl_vector<T>
98 operator*(const vnl_vector<T> & vc) const;
99
101 void
102 operator*=(const T & value)
103 {
104 m_Matrix *= value;
105 }
106
108 Self
109 operator*(const T & value) const
110 {
111 Self result(*this);
112
113 result *= value;
114 return result;
115 }
116
118 void
119 operator/=(const T & value)
120 {
121 m_Matrix /= value;
122 }
123
125 Self
126 operator/(const T & value) const
127 {
128 Self result(*this);
129
130 result /= value;
131 return result;
132 }
133
135 inline T &
136 operator()(unsigned int row, unsigned int col)
137 {
138 return m_Matrix(row, col);
139 }
140
142 inline const T &
143 operator()(unsigned int row, unsigned int col) const
144 {
145 return m_Matrix(row, col);
146 }
147
149 inline T *
150 operator[](unsigned int i)
151 {
152 return m_Matrix[i];
153 }
154
156 inline const T *
157 operator[](unsigned int i) const
158 {
159 return m_Matrix[i];
160 }
161
163 inline InternalMatrixType &
165 {
166 return m_Matrix;
167 }
168
170 [[nodiscard]] inline const InternalMatrixType &
172 {
173 return m_Matrix;
174 }
175
177 inline void
179 {
180 m_Matrix.set_identity();
181 }
182
184 inline void
185 Fill(const T & value)
186 {
187 m_Matrix.fill(value);
188 }
189
191 inline Self &
192 operator=(const vnl_matrix<T> & matrix)
193 {
194 m_Matrix = matrix;
195 return *this;
196 }
197
199 inline bool
200 operator==(const Self & matrix) const;
201
203
205 inline Self &
206 operator=(const Self & matrix) = default;
207
209 [[nodiscard]] inline vnl_matrix<T>
211 {
212 return vnl_matrix_inverse<T>(m_Matrix).as_matrix();
213 }
214
216 [[nodiscard]] inline vnl_matrix<T>
218 {
219 return m_Matrix.transpose();
220 }
221
224 : m_Matrix()
225 {}
226
227 VariableSizeMatrix(unsigned int rows, unsigned int cols);
228
230 VariableSizeMatrix(const Self & matrix)
231 : m_Matrix(matrix.m_Matrix)
232 {}
233
235 [[nodiscard]] inline unsigned int
236 Rows() const
237 {
238 return m_Matrix.rows();
239 }
240
242 [[nodiscard]] inline unsigned int
243 Cols() const
244 {
245 return m_Matrix.cols();
246 }
247
249 inline bool
250 SetSize(unsigned int r, unsigned int c)
251 {
252 return m_Matrix.set_size(r, c);
253 }
254
255private:
257};
258
259template <typename T>
260std::ostream &
261operator<<(std::ostream & os, const VariableSizeMatrix<T> & v)
262{
263 os << v.GetVnlMatrix();
264 return os;
265}
266
270template <typename T>
271inline bool
273{
274 if ((matrix.Rows() != this->Rows()) || (matrix.Cols() != this->Cols()))
275 {
276 return false;
277 }
278 bool equal = true;
279
280 for (unsigned int r = 0; r < this->Rows(); ++r)
281 {
282 for (unsigned int c = 0; c < this->Cols(); ++c)
283 {
284 if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
285 {
286 equal = false;
287 break;
288 }
289 }
290 }
291 return equal;
292}
293} // end namespace itk
294
295#ifndef ITK_MANUAL_INSTANTIATION
296# include "itkVariableSizeMatrix.hxx"
297#endif
298
299#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
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition itkMath.h:727
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)