ITK  5.1.0
Insight Toolkit
itkVariableSizeMatrix.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 {
43 template <typename T>
44 class ITK_TEMPLATE_EXPORT VariableSizeMatrix
45 {
46 public:
49 
51  using ValueType = T;
52  using ComponentType = T;
53 
55  using InternalMatrixType = vnl_matrix<T>;
56 
58  Array<T> operator*(const Array<T> & vector) const;
59 
61  Self operator*(const Self & matrix) const;
62 
64  Self
65  operator+(const Self & matrix) const;
66 
67  const Self &
68  operator+=(const Self & matrix);
69 
71  Self
72  operator-(const Self & matrix) const;
73 
74  const Self &
75  operator-=(const Self & matrix);
76 
78  Self &
79  operator-();
80 
82  vnl_matrix<T> operator*(const vnl_matrix<T> & matrix) const;
83 
85  void
86  operator*=(const Self & matrix);
87 
89  void
90  operator*=(const vnl_matrix<T> & matrix);
91 
93  vnl_vector<T> operator*(const vnl_vector<T> & matrix) const;
94 
96  void
97  operator*=(const T & value)
98  {
99  m_Matrix *= value;
100  }
101 
103  Self operator*(const T & value)
104  {
105  Self result(*this);
106 
107  result *= value;
108  return result;
109  }
110 
112  void
113  operator/=(const T & value)
114  {
115  m_Matrix /= value;
116  }
117 
119  Self
120  operator/(const T & value)
121  {
122  Self result(*this);
123 
124  result /= value;
125  return result;
126  }
127 
129  inline T &
130  operator()(unsigned int row, unsigned int col)
131  {
132  return m_Matrix(row, col);
133  }
134 
136  inline const T &
137  operator()(unsigned int row, unsigned int col) const
138  {
139  return m_Matrix(row, col);
140  }
141 
143  inline T * operator[](unsigned int i) { return m_Matrix[i]; }
144 
146  inline const T * operator[](unsigned int i) const { return m_Matrix[i]; }
147 
149  inline InternalMatrixType &
151  {
152  return m_Matrix;
153  }
154 
156  inline const InternalMatrixType &
157  GetVnlMatrix() const
158  {
159  return m_Matrix;
160  }
161 
163  inline void
165  {
166  m_Matrix.set_identity();
167  }
168 
170  inline void
171  Fill(const T & value)
172  {
173  m_Matrix.fill(value);
174  }
175 
177  inline const Self &
178  operator=(const vnl_matrix<T> & matrix)
179  {
180  m_Matrix = matrix;
181  return *this;
182  }
183 
185  inline bool
186  operator==(const Self & matrix) const;
187 
188  inline bool
189  operator!=(const Self & matrix) const
190  {
191  return !this->operator==(matrix);
192  }
193 
195  inline const Self &
196  operator=(const Self & matrix)
197  {
198  m_Matrix = matrix.m_Matrix;
199  return *this;
200  }
201 
203  inline vnl_matrix<T>
204  GetInverse() const
205  {
206  return vnl_matrix_inverse<T>(m_Matrix).as_matrix();
207  }
208 
210  inline vnl_matrix<T>
211  GetTranspose() const
212  {
213  return m_Matrix.transpose();
214  }
215 
218  : m_Matrix()
219  {}
220 
221  VariableSizeMatrix(unsigned int rows, unsigned int cols);
222 
224  VariableSizeMatrix(const Self & matrix)
225  : m_Matrix(matrix.m_Matrix)
226  {}
227 
229  inline unsigned int
230  Rows() const
231  {
232  return m_Matrix.rows();
233  }
234 
236  inline unsigned int
237  Cols() const
238  {
239  return m_Matrix.cols();
240  }
241 
243  inline bool
244  SetSize(unsigned int r, unsigned int c)
245  {
246  return m_Matrix.set_size(r, c);
247  }
248 
249 private:
251 };
252 
253 template <typename T>
254 std::ostream &
255 operator<<(std::ostream & os, const VariableSizeMatrix<T> & v)
256 {
257  os << v.GetVnlMatrix();
258  return os;
259 }
260 
264 template <typename T>
265 inline bool
267 {
268  if ((matrix.Rows() != this->Rows()) || (matrix.Cols() != this->Cols()))
269  {
270  return false;
271  }
272  bool equal = true;
274 
275  for (unsigned int r = 0; r < this->Rows(); r++)
276  {
277  for (unsigned int c = 0; c < this->Cols(); c++)
278  {
279  if (Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)))
280  {
281  equal = false;
282  break;
283  }
284  }
285  }
286  return equal;
287 }
288 } // end namespace itk
289 
290 #ifndef ITK_MANUAL_INSTANTIATION
291 # include "itkVariableSizeMatrix.hxx"
292 #endif
293 
294 #endif
itk::VariableSizeMatrix::operator=
const Self & operator=(const Self &matrix)
Definition: itkVariableSizeMatrix.h:196
itk::VariableSizeMatrix::SetSize
bool SetSize(unsigned int r, unsigned int c)
Definition: itkVariableSizeMatrix.h:244
itk::VariableSizeMatrix::SetIdentity
void SetIdentity()
Definition: itkVariableSizeMatrix.h:164
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix(const Self &matrix)
Definition: itkVariableSizeMatrix.h:224
itkCovariantVector.h
itk::VariableSizeMatrix::operator()
T & operator()(unsigned int row, unsigned int col)
Definition: itkVariableSizeMatrix.h:130
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:211
itkPoint.h
itk::VariableSizeMatrix::VariableSizeMatrix
VariableSizeMatrix()
Definition: itkVariableSizeMatrix.h:217
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:678
itk::VariableSizeMatrix::operator[]
T * operator[](unsigned int i)
Definition: itkVariableSizeMatrix.h:143
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:113
itk::VariableSizeMatrix
A templated class holding a M x N size Matrix.
Definition: itkVariableSizeMatrix.h:44
itk::VariableSizeMatrix< double >::ComponentType
double ComponentType
Definition: itkVariableSizeMatrix.h:52
itk::VariableSizeMatrix::GetInverse
vnl_matrix< T > GetInverse() const
Definition: itkVariableSizeMatrix.h:204
itk::VariableSizeMatrix::GetVnlMatrix
const InternalMatrixType & GetVnlMatrix() const
Definition: itkVariableSizeMatrix.h:157
itk::VariableSizeMatrix::operator[]
const T * operator[](unsigned int i) const
Definition: itkVariableSizeMatrix.h:146
itk::VariableSizeMatrix::m_Matrix
InternalMatrixType m_Matrix
Definition: itkVariableSizeMatrix.h:250
itk::VariableSizeMatrix::operator!=
bool operator!=(const Self &matrix) const
Definition: itkVariableSizeMatrix.h:189
itk::VariableSizeMatrix::GetVnlMatrix
InternalMatrixType & GetVnlMatrix()
Definition: itkVariableSizeMatrix.h:150
itk::VariableSizeMatrix::Cols
unsigned int Cols() const
Definition: itkVariableSizeMatrix.h:237
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:273
itk::VariableSizeMatrix< double >::InternalMatrixType
vnl_matrix< double > InternalMatrixType
Definition: itkVariableSizeMatrix.h:55
itk::VariableSizeMatrix::GetTranspose
vnl_matrix< T > GetTranspose() const
Definition: itkVariableSizeMatrix.h:211
itkArray.h
itk::VariableSizeMatrix< double >::ValueType
double ValueType
Definition: itkVariableSizeMatrix.h:51
itk::VariableSizeMatrix::operator/
Self operator/(const T &value)
Definition: itkVariableSizeMatrix.h:120
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::VariableSizeMatrix::operator()
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkVariableSizeMatrix.h:137
itk::VariableSizeMatrix::operator*
Self operator*(const T &value)
Definition: itkVariableSizeMatrix.h:103
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:660
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:46
itk::VariableSizeMatrix::Fill
void Fill(const T &value)
Definition: itkVariableSizeMatrix.h:171
itk::VariableSizeMatrix::operator=
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkVariableSizeMatrix.h:178
itk::VariableSizeMatrix::Rows
unsigned int Rows() const
Definition: itkVariableSizeMatrix.h:230
itk::VariableSizeMatrix::operator*=
void operator*=(const T &value)
Definition: itkVariableSizeMatrix.h:97
itkMath.h
itk::VariableSizeMatrix::operator==
bool operator==(const Self &matrix) const
Definition: itkVariableSizeMatrix.h:266