ITK  4.8.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< typename T >
44 {
45 public:
48 
50  typedef T ValueType;
51  typedef T ComponentType;
52 
54  typedef vnl_matrix< T > InternalMatrixType;
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 
212 private:
214 };
215 
216 template< typename T >
217 std::ostream & operator<<(std::ostream & os,
218  const VariableSizeMatrix< T > & v)
219 {
220  os << v.GetVnlMatrix(); return os;
221 }
222 
226 template< typename T >
227 inline
228 bool
230 ::operator==(const Self & matrix) const
231 {
232  if ( ( matrix.Rows() != this->Rows() )
233  || ( matrix.Cols() != this->Cols() ) )
234  {
235  return false;
236  }
237  bool equal = true;
239 
240  for ( unsigned int r = 0; r < this->Rows(); r++ )
241  {
242  for ( unsigned int c = 0; c < this->Cols(); c++ )
243  {
244  if ( m_Matrix(r, c) != matrix.m_Matrix(r, c) )
245  {
246  equal = false;
247  break;
248  }
249  }
250  }
251  return equal;
252 }
253 } // end namespace itk
254 
255 #ifndef ITK_MANUAL_INSTANTIATION
256 #include "itkVariableSizeMatrix.hxx"
257 #endif
258 
259 #endif
Array class with size defined at construction time.
Definition: itkArray.h:50
void operator*=(const Self &matrix)
unsigned int Rows() const
T * operator[](unsigned int i)
const Self & operator-=(const Self &matrix)
A templated class holding a M x N size Matrix.
Self operator/(const T &value)
const Self & operator=(const vnl_matrix< T > &matrix)
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:184
unsigned int Cols() const
const T * operator[](unsigned int i) const
vnl_matrix< T > GetTranspose(void) const
const Self & operator=(const Self &matrix)
bool SetSize(unsigned int r, unsigned int c)
void operator/=(const T &value)
Self operator+(const Self &matrix) const
const T & operator()(unsigned int row, unsigned int col) const
const Self & operator+=(const Self &matrix)
vnl_matrix< T > GetInverse(void) const
const InternalMatrixType & GetVnlMatrix(void) const
VariableSizeMatrix(const Self &matrix)
Self operator*(const T &value)
InternalMatrixType & GetVnlMatrix(void)
bool operator!=(const Self &matrix) const
bool operator==(const Self &matrix) const
T & operator()(unsigned int row, unsigned int col)
Array< T > operator*(const Array< T > &vector) const
void operator*=(const T &value)
vnl_matrix< T > InternalMatrixType