ITK  5.0.0
Insight Segmentation and Registration 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 operator+(const Self & matrix) const;
65 
66  const Self & operator+=(const Self & matrix);
67 
69  Self operator-(const Self & matrix) const;
70 
71  const Self & operator-=(const Self & matrix);
72 
74  Self & operator-();
75 
77  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
78 
80  void operator*=(const Self & matrix);
81 
83  void operator*=(const vnl_matrix< T > & matrix);
84 
86  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
87 
89  void operator*=(const T & value) { m_Matrix *= value; }
90 
92  Self operator*(const T & value)
93  {
94  Self result(*this);
95 
96  result *= value;
97  return result;
98  }
99 
101  void operator/=(const T & value) { m_Matrix /= value; }
102 
104  Self operator/(const T & value)
105  {
106  Self result(*this);
107 
108  result /= value;
109  return result;
110  }
111 
113  inline T & operator()(unsigned int row, unsigned int col)
114  {
115  return m_Matrix(row, col);
116  }
117 
119  inline const T & operator()(unsigned int row, unsigned int col) const
120  {
121  return m_Matrix(row, col);
122  }
123 
125  inline T * operator[](unsigned int i)
126  {
127  return m_Matrix[i];
128  }
129 
131  inline const T * operator[](unsigned int i) const
132  {
133  return m_Matrix[i];
134  }
135 
138  {
139  return m_Matrix;
140  }
141 
143  inline const InternalMatrixType & GetVnlMatrix() const
144  {
145  return m_Matrix;
146  }
147 
149  inline void SetIdentity()
150  {
151  m_Matrix.set_identity();
152  }
153 
155  inline void Fill(const T & value)
156  {
157  m_Matrix.fill(value);
158  }
159 
161  inline const Self & operator=(const vnl_matrix< T > & matrix)
162  {
163  m_Matrix = matrix;
164  return *this;
165  }
166 
168  inline bool operator==(const Self & matrix) const;
169 
170  inline bool operator!=(const Self & matrix) const
171  {
172  return !this->operator==(matrix);
173  }
174 
176  inline const Self & operator=(const Self & matrix)
177  {
178  m_Matrix = matrix.m_Matrix;
179  return *this;
180  }
181 
183  inline vnl_matrix< T > GetInverse() const
184  {
185  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
186  return temp;
187  }
189 
191  inline vnl_matrix< T > GetTranspose() const
192  {
193  return m_Matrix.transpose();
194  }
195 
197  VariableSizeMatrix():m_Matrix() {}
198 
199  VariableSizeMatrix(unsigned int rows, unsigned int cols);
200 
202  VariableSizeMatrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
203 
205  inline unsigned int Rows() const { return m_Matrix.rows(); }
206 
208  inline unsigned int Cols() const { return m_Matrix.cols(); }
209 
211  inline bool SetSize(unsigned int r, unsigned int c) { return m_Matrix.set_size(r, c); }
212 
213 private:
215 };
216 
217 template< typename T >
218 std::ostream & operator<<(std::ostream & os,
219  const VariableSizeMatrix< T > & v)
220 {
221  os << v.GetVnlMatrix(); return os;
222 }
223 
227 template< typename T >
228 inline
229 bool
231 ::operator==(const Self & matrix) const
232 {
233  if ( ( matrix.Rows() != this->Rows() )
234  || ( matrix.Cols() != this->Cols() ) )
235  {
236  return false;
237  }
238  bool equal = true;
240 
241  for ( unsigned int r = 0; r < this->Rows(); r++ )
242  {
243  for ( unsigned int c = 0; c < this->Cols(); c++ )
244  {
245  if ( Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)) )
246  {
247  equal = false;
248  break;
249  }
250  }
251  }
252  return equal;
253 }
254 } // end namespace itk
255 
256 #ifndef ITK_MANUAL_INSTANTIATION
257 #include "itkVariableSizeMatrix.hxx"
258 #endif
259 
260 #endif
Array class with size defined at construction time.
Definition: itkArray.h:46
vnl_matrix< T > GetTranspose() const
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:485
unsigned int Rows() const
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
T * operator[](unsigned int i)
A templated class holding a M x N size Matrix.
Self operator/(const T &value)
const Self & operator=(const vnl_matrix< T > &matrix)
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:188
unsigned int Cols() const
const T * operator[](unsigned int i) const
vnl_matrix< T > GetInverse() const
const Self & operator=(const Self &matrix)
bool SetSize(unsigned int r, unsigned int c)
const InternalMatrixType & GetVnlMatrix() const
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
void operator/=(const T &value)
const T & operator()(unsigned int row, unsigned int col) const
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:718
VariableSizeMatrix(const Self &matrix)
Self operator*(const T &value)
bool operator!=(const Self &matrix) const
bool operator==(const Self &matrix) const
T & operator()(unsigned int row, unsigned int col)
InternalMatrixType & GetVnlMatrix()
void operator*=(const T &value)