ITK  5.2.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  * http://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)
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)
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 const 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  inline bool
190  operator!=(const Self & matrix) const
191  {
192  return !this->operator==(matrix);
193  }
194 
196  inline const Self &
197  operator=(const Self & matrix)
198  {
199  m_Matrix = matrix.m_Matrix;
200  return *this;
201  }
202 
204  inline vnl_matrix<T>
205  GetInverse() const
206  {
207  return vnl_matrix_inverse<T>(m_Matrix).as_matrix();
208  }
209 
211  inline vnl_matrix<T>
212  GetTranspose() const
213  {
214  return m_Matrix.transpose();
215  }
216 
219  : m_Matrix()
220  {}
221 
222  VariableSizeMatrix(unsigned int rows, unsigned int cols);
223 
225  VariableSizeMatrix(const Self & matrix)
226  : m_Matrix(matrix.m_Matrix)
227  {}
228 
230  inline unsigned int
231  Rows() const
232  {
233  return m_Matrix.rows();
234  }
235 
237  inline unsigned int
238  Cols() const
239  {
240  return m_Matrix.cols();
241  }
242 
244  inline bool
245  SetSize(unsigned int r, unsigned int c)
246  {
247  return m_Matrix.set_size(r, c);
248  }
249 
250 private:
252 };
253 
254 template <typename T>
255 std::ostream &
256 operator<<(std::ostream & os, const VariableSizeMatrix<T> & v)
257 {
258  os << v.GetVnlMatrix();
259  return os;
260 }
261 
265 template <typename T>
266 inline bool
268 {
269  if ((matrix.Rows() != this->Rows()) || (matrix.Cols() != this->Cols()))
270  {
271  return false;
272  }
273  bool equal = true;
275 
276  for (unsigned int r = 0; r < this->Rows(); r++)
277  {
278  for (unsigned int c = 0; c < this->Cols(); c++)
279  {
280  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
281  {
282  equal = false;
283  break;
284  }
285  }
286  }
287  return equal;
288 }
289 } // end namespace itk
290 
291 #ifndef ITK_MANUAL_INSTANTIATION
292 # include "itkVariableSizeMatrix.hxx"
293 #endif
294 
295 #endif
itk::VariableSizeMatrix::operator=
const Self & operator=(const Self &matrix)
Definition: itkVariableSizeMatrix.h:197
itk::VariableSizeMatrix::SetSize
bool SetSize(unsigned int r, unsigned int c)
Definition: itkVariableSizeMatrix.h:245
itk::VariableSizeMatrix::SetIdentity
void SetIdentity()
Definition: itkVariableSizeMatrix.h:165
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix(const Self &matrix)
Definition: itkVariableSizeMatrix.h:225
itkCovariantVector.h
itk::VariableSizeMatrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkVariableSizeMatrix.h:131
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itkPoint.h
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix()
Definition: itkVariableSizeMatrix.h:218
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:679
itk::VariableSizeMatrix::operator[]
T * operator[](unsigned int i)
Definition: itkVariableSizeMatrix.h:144
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
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::GetInverse
vnl_matrix< T > GetInverse() const
Definition: itkVariableSizeMatrix.h:205
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:251
itk::VariableSizeMatrix::operator!=
bool operator!=(const Self &matrix) const
Definition: itkVariableSizeMatrix.h:190
itk::VariableSizeMatrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkVariableSizeMatrix.h:151
itk::VariableSizeMatrix::Cols
unsigned int Cols() const
Definition: itkVariableSizeMatrix.h:238
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:532
itk::operator*
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
Definition: itkCovariantVector.h:275
itk::VariableSizeMatrix< double >::InternalMatrixType
vnl_matrix< double > InternalMatrixType
Definition: itkVariableSizeMatrix.h:56
itk::VariableSizeMatrix::GetTranspose
vnl_matrix< T > GetTranspose() const
Definition: itkVariableSizeMatrix.h:212
itkArray.h
itk::VariableSizeMatrix< double >::ValueType
double ValueType
Definition: itkVariableSizeMatrix.h:52
itk::VariableSizeMatrix::operator/
Self operator/(const T &value)
Definition: itkVariableSizeMatrix.h:121
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::VariableSizeMatrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkVariableSizeMatrix.h:138
itk::VariableSizeMatrix::operator*
Self operator*(const T &value)
Definition: itkVariableSizeMatrix.h:104
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:661
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
itk::VariableSizeMatrix::operator=
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkVariableSizeMatrix.h:179
itk::VariableSizeMatrix::Rows
unsigned int Rows() const
Definition: itkVariableSizeMatrix.h:231
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:267