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 
30 namespace itk
31 {
44 template <typename T>
45 class ITK_TEMPLATE_EXPORT VariableSizeMatrix
46 {
47 public:
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 &
80  operator-();
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 &
158  GetVnlMatrix() const
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 
189  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
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>
201  GetInverse() const
202  {
203  return vnl_matrix_inverse<T>(m_Matrix).as_matrix();
204  }
205 
207  inline vnl_matrix<T>
208  GetTranspose() const
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 
246 private:
248 };
249 
250 template <typename T>
251 std::ostream &
252 operator<<(std::ostream & os, const VariableSizeMatrix<T> & v)
253 {
254  os << v.GetVnlMatrix();
255  return os;
256 }
257 
261 template <typename T>
262 inline 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
itk::VariableSizeMatrix::operator=
Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkVariableSizeMatrix.h:179
itk::VariableSizeMatrix::SetSize
bool SetSize(unsigned int r, unsigned int c)
Definition: itkVariableSizeMatrix.h:241
itk::VariableSizeMatrix::SetIdentity
void SetIdentity()
Definition: itkVariableSizeMatrix.h:165
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix(const Self &matrix)
Definition: itkVariableSizeMatrix.h:221
itkCovariantVector.h
itk::VariableSizeMatrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkVariableSizeMatrix.h:131
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:268
itkPoint.h
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix()
Definition: itkVariableSizeMatrix.h:214
itk::VariableSizeMatrix::operator=
Self & operator=(const Self &matrix)
Definition: itkVariableSizeMatrix.h:193
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:672
itk::VariableSizeMatrix::operator[]
T * operator[](unsigned int i)
Definition: itkVariableSizeMatrix.h:144
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:737
itk::VariableSizeMatrix::operator/=
void operator/=(const T &value)
Definition: itkVariableSizeMatrix.h:114
itk::VariableSizeMatrix
A templated class holding a M x N size Matrix.
Definition: itkVariableSizeMatrix.h:45
itk::VariableSizeMatrix< double >::ComponentType
double ComponentType
Definition: itkVariableSizeMatrix.h:53
itk::VariableSizeMatrix::operator/
Self operator/(const T &value) const
Definition: itkVariableSizeMatrix.h:121
itk::VariableSizeMatrix::GetInverse
vnl_matrix< T > GetInverse() const
Definition: itkVariableSizeMatrix.h:201
itk::VariableSizeMatrix::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkVariableSizeMatrix.h:158
itk::VariableSizeMatrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkVariableSizeMatrix.h:147
itk::VariableSizeMatrix::m_Matrix
InternalMatrixType m_Matrix
Definition: itkVariableSizeMatrix.h:247
itk::VariableSizeMatrix::operator*
Self operator*(const T &value) const
Definition: itkVariableSizeMatrix.h:104
itk::VariableSizeMatrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkVariableSizeMatrix.h:151
itk::VariableSizeMatrix::Cols
unsigned int Cols() const
Definition: itkVariableSizeMatrix.h:234
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:543
itk::VariableSizeMatrix< double >::InternalMatrixType
vnl_matrix< double > InternalMatrixType
Definition: itkVariableSizeMatrix.h:56
itk::VariableSizeMatrix::GetTranspose
vnl_matrix< T > GetTranspose() const
Definition: itkVariableSizeMatrix.h:208
itkArray.h
itk::VariableSizeMatrix< double >::ValueType
double ValueType
Definition: itkVariableSizeMatrix.h:52
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::VariableSizeMatrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkVariableSizeMatrix.h:138
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:654
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:47
itk::VariableSizeMatrix::Fill
void Fill(const T &value)
Definition: itkVariableSizeMatrix.h:172
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::VariableSizeMatrix::Rows
unsigned int Rows() const
Definition: itkVariableSizeMatrix.h:227
itk::VariableSizeMatrix::operator*=
void operator*=(const T &value)
Definition: itkVariableSizeMatrix.h:98
itkMath.h
itk::VariableSizeMatrix::operator==
bool operator==(const Self &matrix) const
Definition: itkVariableSizeMatrix.h:263