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>
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 &
82  operator-();
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  inline const InternalMatrixType &
171  GetVnlMatrix() const
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 
202  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
203 
205  inline Self &
206  operator=(const Self & matrix)
207  {
208  m_Matrix = matrix.m_Matrix;
209  return *this;
210  }
211 
213  inline vnl_matrix<T>
214  GetInverse() const
215  {
216  return vnl_matrix_inverse<T>(m_Matrix).as_matrix();
217  }
218 
220  inline vnl_matrix<T>
221  GetTranspose() const
222  {
223  return m_Matrix.transpose();
224  }
225 
228  : m_Matrix()
229  {}
230 
231  VariableSizeMatrix(unsigned int rows, unsigned int cols);
232 
234  VariableSizeMatrix(const Self & matrix)
235  : m_Matrix(matrix.m_Matrix)
236  {}
237 
239  inline unsigned int
240  Rows() const
241  {
242  return m_Matrix.rows();
243  }
244 
246  inline unsigned int
247  Cols() const
248  {
249  return m_Matrix.cols();
250  }
251 
253  inline bool
254  SetSize(unsigned int r, unsigned int c)
255  {
256  return m_Matrix.set_size(r, c);
257  }
258 
259 private:
261 };
262 
263 template <typename T>
264 std::ostream &
265 operator<<(std::ostream & os, const VariableSizeMatrix<T> & v)
266 {
267  os << v.GetVnlMatrix();
268  return os;
269 }
270 
274 template <typename T>
275 inline bool
277 {
278  if ((matrix.Rows() != this->Rows()) || (matrix.Cols() != this->Cols()))
279  {
280  return false;
281  }
282  bool equal = true;
285  for (unsigned int r = 0; r < this->Rows(); ++r)
286  {
287  for (unsigned int c = 0; c < this->Cols(); ++c)
288  {
289  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
290  {
291  equal = false;
292  break;
293  }
294  }
295  }
296  return equal;
297 }
298 } // end namespace itk
299 
300 #ifndef ITK_MANUAL_INSTANTIATION
301 # include "itkVariableSizeMatrix.hxx"
302 #endif
303 
304 #endif
itk::VariableSizeMatrix::operator=
Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkVariableSizeMatrix.h:192
itk::VariableSizeMatrix::SetSize
bool SetSize(unsigned int r, unsigned int c)
Definition: itkVariableSizeMatrix.h:254
itk::VariableSizeMatrix::SetIdentity
void SetIdentity()
Definition: itkVariableSizeMatrix.h:178
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix(const Self &matrix)
Definition: itkVariableSizeMatrix.h:234
itkCovariantVector.h
itk::VariableSizeMatrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkVariableSizeMatrix.h:136
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:272
itkPoint.h
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix()
Definition: itkVariableSizeMatrix.h:227
itk::VariableSizeMatrix::operator=
Self & operator=(const Self &matrix)
Definition: itkVariableSizeMatrix.h:206
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:150
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:733
itk::VariableSizeMatrix::operator/=
void operator/=(const T &value)
Definition: itkVariableSizeMatrix.h:119
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:126
itk::VariableSizeMatrix::GetInverse
vnl_matrix< T > GetInverse() const
Definition: itkVariableSizeMatrix.h:214
itk::VariableSizeMatrix::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkVariableSizeMatrix.h:171
itk::VariableSizeMatrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkVariableSizeMatrix.h:157
itk::VariableSizeMatrix::m_Matrix
InternalMatrixType m_Matrix
Definition: itkVariableSizeMatrix.h:260
itk::VariableSizeMatrix::operator*
Self operator*(const T &value) const
Definition: itkVariableSizeMatrix.h:109
itk::VariableSizeMatrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkVariableSizeMatrix.h:164
itk::VariableSizeMatrix::Cols
unsigned int Cols() const
Definition: itkVariableSizeMatrix.h:247
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:552
itk::VariableSizeMatrix< double >::InternalMatrixType
vnl_matrix< double > InternalMatrixType
Definition: itkVariableSizeMatrix.h:56
itk::VariableSizeMatrix::GetTranspose
vnl_matrix< T > GetTranspose() const
Definition: itkVariableSizeMatrix.h:221
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:143
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:185
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::VariableSizeMatrix::Rows
unsigned int Rows() const
Definition: itkVariableSizeMatrix.h:240
itk::VariableSizeMatrix::operator*=
void operator*=(const T &value)
Definition: itkVariableSizeMatrix.h:102
itkMath.h
itk::VariableSizeMatrix::operator==
bool operator==(const Self &matrix) const
Definition: itkVariableSizeMatrix.h:276