ITK  4.2.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 
29 namespace itk
30 {
42 template< class T >
44 {
45 public:
48 
50  typedef T ValueType;
51  typedef T ComponentType;
52 
55 
57  Array< T > operator *(const Array< T > & vector) const;
58 
60  Self operator *(const Self & matrix) const;
61 
63  Self operator+(const Self & matrix) const;
64 
65  const Self & operator+=(const Self & matrix);
66 
68  Self operator-(const Self & matrix) const;
69 
70  const Self & operator-=(const Self & matrix);
71 
73  Self & operator-();
74 
76  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
77 
79  void operator*=(const Self & matrix);
80 
82  void operator*=(const vnl_matrix< T > & matrix);
83 
85  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
86 
88  void operator*=(const T & value) { m_Matrix *= value; }
89 
91  Self operator*(const T & value)
92  {
93  Self result(*this);
94 
95  result *= value;
96  return result;
97  }
98 
100  void operator/=(const T & value) { m_Matrix /= value; }
101 
103  Self operator/(const T & value)
104  {
105  Self result(*this);
106 
107  result /= value;
108  return result;
109  }
110 
112  inline T & operator()(unsigned int row, unsigned int col)
113  {
114  return m_Matrix(row, col);
115  }
116 
118  inline const T & operator()(unsigned int row, unsigned int col) const
119  {
120  return m_Matrix(row, col);
121  }
122 
124  inline T * operator[](unsigned int i)
125  {
126  return m_Matrix[i];
127  }
128 
130  inline const T * operator[](unsigned int i) const
131  {
132  return m_Matrix[i];
133  }
134 
137  {
138  return m_Matrix;
139  }
140 
142  inline const InternalMatrixType & GetVnlMatrix(void) const
143  {
144  return m_Matrix;
145  }
146 
148  inline void SetIdentity(void)
149  {
150  m_Matrix.set_identity();
151  }
152 
154  inline void Fill(const T & value)
155  {
156  m_Matrix.fill(value);
157  }
158 
160  inline const Self & operator=(const vnl_matrix< T > & matrix)
161  {
162  m_Matrix = matrix;
163  return *this;
164  }
165 
167  inline bool operator==(const Self & matrix) const;
168 
169  inline bool operator!=(const Self & matrix) const
170  {
171  return !this->operator==(matrix);
172  }
173 
175  inline const Self & operator=(const Self & matrix)
176  {
177  m_Matrix = matrix.m_Matrix;
178  return *this;
179  }
180 
182  inline vnl_matrix< T > GetInverse(void) const
183  {
184  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
185  return temp;
186  }
188 
190  inline vnl_matrix< T > GetTranspose(void) const
191  {
192  return m_Matrix.transpose();
193  }
194 
197 
198  VariableSizeMatrix(unsigned int rows, unsigned int cols);
199 
201  VariableSizeMatrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
202 
204  inline unsigned int Rows() const { return m_Matrix.rows(); }
205 
207  inline unsigned int Cols() const { return m_Matrix.cols(); }
208 
210  inline bool SetSize(unsigned int r, unsigned int c) { return m_Matrix.set_size(r, c); }
211 private:
213 };
214 
215 template< class T >
216 ITK_EXPORT std::ostream & operator<<(std::ostream & os,
217  const VariableSizeMatrix< T > & v)
218 {
219  os << v.GetVnlMatrix(); return os;
220 }
221 
225 template< class T >
226 inline
227 bool
229 ::operator==(const Self & matrix) const
230 {
231  if ( ( matrix.Rows() != this->Rows() )
232  || ( matrix.Cols() != this->Cols() ) )
233  {
234  return false;
235  }
236  bool equal = true;
238 
239  for ( unsigned int r = 0; r < this->Rows(); r++ )
240  {
241  for ( unsigned int c = 0; c < this->Cols(); c++ )
242  {
243  if ( m_Matrix(r, c) != matrix.m_Matrix(r, c) )
244  {
245  equal = false;
246  break;
247  }
248  }
249  }
250  return equal;
251 }
252 } // end namespace itk
253 
254 #ifndef ITK_MANUAL_INSTANTIATION
255 #include "itkVariableSizeMatrix.hxx"
256 #endif
257 
258 #endif
259