ITK  6.0.0
Insight Toolkit
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{
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
59 Array<T> operator*(const Array<T> & vect) const;
60
62 Self operator*(const Self & matrix) const;
63
65 Self
66 operator+(const Self & matrix) const;
67
68 const Self &
69 operator+=(const Self & matrix);
70
72 Self
73 operator-(const Self & matrix) const;
74
75 const Self &
76 operator-=(const Self & matrix);
77
79 Self &
81
83 vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
84
86 void
87 operator*=(const Self & matrix);
88
90 void
91 operator*=(const vnl_matrix<T> & matrix);
92
94 vnl_vector<T> operator*(const vnl_vector<T> & vc) const;
95
97 void
98 operator*=(const T & value)
99 {
100 m_Matrix *= value;
101 }
102
104 Self operator*(const T & value) const
105 {
106 Self result(*this);
107
108 result *= value;
109 return result;
110 }
111
113 void
114 operator/=(const T & value)
115 {
116 m_Matrix /= value;
117 }
118
120 Self
121 operator/(const T & value) const
122 {
123 Self result(*this);
124
125 result /= value;
126 return result;
127 }
128
130 inline T &
131 operator()(unsigned int row, unsigned int col)
132 {
133 return m_Matrix(row, col);
134 }
135
137 inline const T &
138 operator()(unsigned int row, unsigned int col) const
139 {
140 return m_Matrix(row, col);
141 }
142
144 inline T * operator[](unsigned int i) { return m_Matrix[i]; }
145
147 inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
148
150 inline InternalMatrixType &
152 {
153 return m_Matrix;
154 }
155
157 inline const InternalMatrixType &
159 {
160 return m_Matrix;
161 }
162
164 inline void
166 {
167 m_Matrix.set_identity();
168 }
169
171 inline void
172 Fill(const T & value)
173 {
174 m_Matrix.fill(value);
175 }
176
178 inline Self &
179 operator=(const vnl_matrix<T> & matrix)
180 {
181 m_Matrix = matrix;
182 return *this;
183 }
184
186 inline bool
187 operator==(const Self & matrix) const;
188
190
192 inline Self &
193 operator=(const Self & matrix)
194 {
195 m_Matrix = matrix.m_Matrix;
196 return *this;
197 }
198
200 inline vnl_matrix<T>
202 {
203 return vnl_matrix_inverse<T>(m_Matrix).as_matrix();
204 }
205
207 inline vnl_matrix<T>
209 {
210 return m_Matrix.transpose();
211 }
212
215 : m_Matrix()
216 {}
217
218 VariableSizeMatrix(unsigned int rows, unsigned int cols);
219
221 VariableSizeMatrix(const Self & matrix)
222 : m_Matrix(matrix.m_Matrix)
223 {}
224
226 inline unsigned int
227 Rows() const
228 {
229 return m_Matrix.rows();
230 }
231
233 inline unsigned int
234 Cols() const
235 {
236 return m_Matrix.cols();
237 }
238
240 inline bool
241 SetSize(unsigned int r, unsigned int c)
242 {
243 return m_Matrix.set_size(r, c);
244 }
245
246private:
248};
249
250template <typename T>
251std::ostream &
252operator<<(std::ostream & os, const VariableSizeMatrix<T> & v)
253{
254 os << v.GetVnlMatrix();
255 return os;
256}
257
261template <typename T>
262inline bool
264{
265 if ((matrix.Rows() != this->Rows()) || (matrix.Cols() != this->Cols()))
266 {
267 return false;
268 }
269 bool equal = true;
272 for (unsigned int r = 0; r < this->Rows(); ++r)
273 {
274 for (unsigned int c = 0; c < this->Cols(); ++c)
275 {
276 if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
277 {
278 equal = false;
279 break;
280 }
281 }
282 }
283 return equal;
284}
285} // end namespace itk
286
287#ifndef ITK_MANUAL_INSTANTIATION
288# include "itkVariableSizeMatrix.hxx"
289#endif
290
291#endif
Pixel-wise addition of two images.
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
vnl_matrix< T > InternalMatrixType
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
Self & operator=(const Self &matrix)
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
T * operator[](unsigned int i)
InternalMatrixType & GetVnlMatrix()
Self operator-(const Self &matrix) const
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)