ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkMatrix.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 itkMatrix_h
19 #define itkMatrix_h
20 
21 #include "itkPoint.h"
22 #include "itkCovariantVector.h"
23 #include "vnl/vnl_matrix_fixed.txx" // Get the templates
24 #include "vnl/vnl_transpose.h"
25 #include "vnl/algo/vnl_matrix_inverse.h"
26 #include "vnl/vnl_matrix.h"
27 #include "vnl/algo/vnl_determinant.h"
28 #include "itkMath.h"
29 
30 namespace itk
31 {
46 template< typename T, unsigned int NRows = 3, unsigned int NColumns = 3 >
47 class Matrix
48 {
49 public:
51  typedef Matrix Self;
52 
54  typedef T ValueType;
55  typedef T ComponentType;
56 
58  itkStaticConstMacro(RowDimensions, unsigned int, NRows);
59  itkStaticConstMacro(ColumnDimensions, unsigned int, NColumns);
61 
63  typedef vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType;
64 
69 
72 
75 
78 
80  vnl_vector_fixed<T,NRows> operator *(const vnl_vector_fixed< T, NColumns > & vector) const;
81 
83  Self operator *(const CompatibleSquareMatrixType & matrix) const;
84 
85  template<unsigned int OuterDim>
86  Matrix<T, NRows, OuterDim> operator*(const vnl_matrix_fixed< T, NRows, OuterDim > & matrix) const
87  {
88  const Matrix<T, NRows, OuterDim> result ( m_Matrix * matrix );
89  return result;
90  }
91 
93  Self operator+(const Self & matrix) const;
94 
95  const Self & operator+=(const Self & matrix);
96 
98  Self operator-(const Self & matrix) const;
99 
100  const Self & operator-=(const Self & matrix);
101 
103  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
104 
106  void operator*=(const CompatibleSquareMatrixType & matrix);
107 
109  void operator*=(const vnl_matrix< T > & matrix);
110 
112  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
113 
115  void operator*=(const T & value)
116  { m_Matrix *= value; }
117 
119  Self operator*(const T & value) const
120  {
121  Self result(*this);
122 
123  result *= value;
124  return result;
125  }
126 
128  void operator/=(const T & value)
129  {
130  m_Matrix /= value;
131  }
132 
134  Self operator/(const T & value) const
135  {
136  Self result(*this);
137 
138  result /= value;
139  return result;
140  }
141 
143  inline T & operator()(unsigned int row, unsigned int col)
144  {
145  return m_Matrix(row, col);
146  }
147 
149  inline const T & operator()(unsigned int row, unsigned int col) const
150  {
151  return m_Matrix(row, col);
152  }
153 
155  inline T * operator[](unsigned int i)
156  {
157  return m_Matrix[i];
158  }
159 
161  inline const T * operator[](unsigned int i) const
162  {
163  return m_Matrix[i];
164  }
165 
168  {
169  return m_Matrix;
170  }
171 
173  inline const InternalMatrixType & GetVnlMatrix(void) const
174  {
175  return m_Matrix;
176  }
177 
179  inline void SetIdentity(void)
180  {
181  m_Matrix.set_identity();
182  }
183 
185  inline void Fill(const T & value)
186  {
187  m_Matrix.fill(value);
188  }
189 
191  inline const Self & operator=(const vnl_matrix< T > & matrix)
192  {
193  m_Matrix = matrix;
194  return *this;
195  }
196 
198  inline Matrix(const vnl_matrix< T > & matrix)
199  {
200  this->operator=(matrix);
201  }
202 
204  inline bool operator==(const Self & matrix) const
205  {
206  bool equal = true;
207 
208  for ( unsigned int r = 0; r < NRows; r++ )
209  {
210  for ( unsigned int c = 0; c < NColumns; c++ )
211  {
212  if ( Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)) )
213  {
214  equal = false;
215  break;
216  }
217  }
218  }
219  return equal;
220  }
221 
222  inline bool operator!=(const Self & matrix) const
223  {
224  return !this->operator==(matrix);
225  }
226 
227  inline const Self & operator=(const InternalMatrixType & matrix)
228  {
229  this->m_Matrix = matrix;
230  return *this;
231  }
232 
234  inline explicit Matrix(const InternalMatrixType & matrix)
235  {
236  this->operator=(matrix);
237  }
238 
240  inline const Self & operator=(const Self & matrix)
241  {
242  m_Matrix = matrix.m_Matrix;
243  return *this;
244  }
245 
247  inline vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
248  {
249  if ( vnl_determinant(m_Matrix) == 0.0 )
250  {
251  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
252  }
253  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
254  return temp;
255  }
257 
259  inline vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
260  {
261  return m_Matrix.transpose();
262  }
263 
265  Matrix():m_Matrix(NumericTraits< T >::ZeroValue()) {}
266 
268  Matrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
269 
270 private:
272 };
273 
274 template< typename T, unsigned int NRows, unsigned int NColumns >
275 std::ostream & operator<<(std::ostream & os, const Matrix< T, NRows, NColumns > & v)
276 {
277  os << v.GetVnlMatrix();
278  return os;
279 }
280 } // end namespace itk
281 
282 #ifndef ITK_MANUAL_INSTANTIATION
283 #include "itkMatrix.hxx"
284 #endif
285 
286 #endif
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:191
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:227
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:47
void operator*=(const T &value)
Definition: itkMatrix.h:115
Matrix(const Self &matrix)
Definition: itkMatrix.h:268
Self operator-(const Self &matrix) const
bool operator!=(const Self &matrix) const
Definition: itkMatrix.h:222
vnl_matrix_fixed< T, NColumns, NRows > GetInverse(void) const
Definition: itkMatrix.h:247
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:143
vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType
Definition: itkMatrix.h:63
const InternalMatrixType & GetVnlMatrix(void) const
Definition: itkMatrix.h:173
InternalMatrixType m_Matrix
Definition: itkMatrix.h:271
Self operator*(const T &value) const
Definition: itkMatrix.h:119
Vector< T, NRows > operator*(const Vector< T, NColumns > &vector) const
const Self & operator+=(const Self &matrix)
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:234
Matrix< T, NColumns, NColumns > CompatibleSquareMatrixType
Definition: itkMatrix.h:68
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:149
const Self & operator=(const Self &matrix)
Definition: itkMatrix.h:240
vnl_matrix_fixed< T, NColumns, NRows > GetTranspose(void) const
Definition: itkMatrix.h:259
T ComponentType
Definition: itkMatrix.h:55
Matrix< T, NRows, OuterDim > operator*(const vnl_matrix_fixed< T, NRows, OuterDim > &matrix) const
Definition: itkMatrix.h:86
T * operator[](unsigned int i)
Definition: itkMatrix.h:155
void SetIdentity(void)
Definition: itkMatrix.h:179
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:204
void operator*=(const CompatibleSquareMatrixType &matrix)
const Self & operator-=(const Self &matrix)
static const unsigned int ColumnDimensions
Definition: itkMatrix.h:59
Self operator/(const T &value) const
Definition: itkMatrix.h:134
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:676
InternalMatrixType & GetVnlMatrix(void)
Definition: itkMatrix.h:167
Matrix Self
Definition: itkMatrix.h:51
static const unsigned int RowDimensions
Definition: itkMatrix.h:58
void Fill(const T &value)
Definition: itkMatrix.h:185
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:198
Self operator+(const Self &matrix) const
Define additional traits for native types such as int or float.
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
A templated class holding a n-Dimensional covariant vector.
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:161
void operator/=(const T &value)
Definition: itkMatrix.h:128