ITK  5.0.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 
24 #include <vxl_version.h>
25 #include "vnl/vnl_matrix_fixed.hxx" // Get the templates
26 #include "vnl/vnl_transpose.h"
27 #include "vnl/algo/vnl_matrix_inverse.h"
28 #include "vnl/vnl_matrix.h"
29 #include "vnl/algo/vnl_determinant.h"
30 #include "itkMath.h"
31 
32 namespace itk
33 {
48 template< typename T, unsigned int NRows = 3, unsigned int NColumns = 3 >
49 class ITK_TEMPLATE_EXPORT Matrix
50 {
51 public:
53  using Self = Matrix;
54 
56  using ValueType = T;
57  using ComponentType = T;
58 
60  static constexpr unsigned int RowDimensions = NRows;
61  static constexpr unsigned int ColumnDimensions = NColumns;
62 
64  using InternalMatrixType = vnl_matrix_fixed< T, NRows, NColumns >;
65 
70 
73 
76 
79 
81  vnl_vector_fixed<T,NRows> operator *(const vnl_vector_fixed< T, NColumns > & vector) const;
82 
84  Self operator *(const CompatibleSquareMatrixType & matrix) const;
85 
86  template<unsigned int OuterDim>
87  Matrix<T, NRows, OuterDim> operator*(const vnl_matrix_fixed< T, NRows, OuterDim > & matrix) const
88  {
89  const Matrix<T, NRows, OuterDim> result ( m_Matrix * matrix );
90  return result;
91  }
92 
94  Self operator+(const Self & matrix) const;
95 
96  const Self & operator+=(const Self & matrix);
97 
99  Self operator-(const Self & matrix) const;
100 
101  const Self & operator-=(const Self & matrix);
102 
104  vnl_matrix< T > operator *(const vnl_matrix< T > & matrix) const;
105 
107  void operator*=(const CompatibleSquareMatrixType & matrix);
108 
110  void operator*=(const vnl_matrix< T > & matrix);
111 
113  vnl_vector< T > operator *(const vnl_vector< T > & matrix) const;
114 
116  void operator*=(const T & value)
117  { m_Matrix *= value; }
118 
120  Self operator*(const T & value) const
121  {
122  Self result(*this);
123 
124  result *= value;
125  return result;
126  }
127 
129  void operator/=(const T & value)
130  {
131  m_Matrix /= value;
132  }
133 
135  Self operator/(const T & value) const
136  {
137  Self result(*this);
138 
139  result /= value;
140  return result;
141  }
142 
144  inline T & operator()(unsigned int row, unsigned int col)
145  {
146  return m_Matrix(row, col);
147  }
148 
150  inline const T & operator()(unsigned int row, unsigned int col) const
151  {
152  return m_Matrix(row, col);
153  }
154 
156  inline T * operator[](unsigned int i)
157  {
158  return m_Matrix[i];
159  }
160 
162  inline const T * operator[](unsigned int i) const
163  {
164  return m_Matrix[i];
165  }
166 
169  {
170  return m_Matrix;
171  }
172 
174  inline const InternalMatrixType & GetVnlMatrix() const
175  {
176  return m_Matrix;
177  }
178 
180  inline void SetIdentity()
181  {
182  m_Matrix.set_identity();
183  }
184 
186  inline void Fill(const T & value)
187  {
188  m_Matrix.fill(value);
189  }
190 
192  inline const Self & operator=(const vnl_matrix< T > & matrix)
193  {
194  m_Matrix = matrix;
195  return *this;
196  }
197 
199  inline Matrix(const vnl_matrix< T > & matrix)
200  {
201  this->operator=(matrix);
202  }
203 
205  inline bool operator==(const Self & matrix) const
206  {
207  bool equal = true;
208 
209  for ( unsigned int r = 0; r < NRows; r++ )
210  {
211  for ( unsigned int c = 0; c < NColumns; c++ )
212  {
213  if ( Math::NotExactlyEquals(m_Matrix(r, c), matrix.m_Matrix(r, c)) )
214  {
215  equal = false;
216  break;
217  }
218  }
219  }
220  return equal;
221  }
222 
223  inline bool operator!=(const Self & matrix) const
224  {
225  return !this->operator==(matrix);
226  }
227 
228  inline const Self & operator=(const InternalMatrixType & matrix)
229  {
230  this->m_Matrix = matrix;
231  return *this;
232  }
233 
235  inline explicit Matrix(const InternalMatrixType & matrix)
236  {
237  this->operator=(matrix);
238  }
239 
241  inline const Self & operator=(const Self & matrix)
242  {
243  m_Matrix = matrix.m_Matrix;
244  return *this;
245  }
246 
248  inline vnl_matrix_fixed< T, NColumns, NRows > GetInverse() const
249  {
250  if ( vnl_determinant(m_Matrix) == NumericTraits<T>::ZeroValue() )
251  {
252  itkGenericExceptionMacro(<< "Singular matrix. Determinant is 0.");
253  }
254  vnl_matrix< T > temp = vnl_matrix_inverse< T >(m_Matrix);
255  return temp;
256  }
258 
260  inline vnl_matrix_fixed< T, NColumns, NRows > GetTranspose() const
261  {
262  return m_Matrix.transpose();
263  }
264 
266  Matrix():m_Matrix(NumericTraits< T >::ZeroValue()) {}
267 
269  Matrix(const Self & matrix):m_Matrix(matrix.m_Matrix) {}
270 
271  void swap(Self &other)
272  {
273  // allow for augment dependent look up
274  using std::swap;
275  swap(this->m_Matrix, other.m_Matrix);
276  }
277 private:
279 };
280 
281 template< typename T, unsigned int NRows, unsigned int NColumns >
282 std::ostream & operator<<(std::ostream & os, const Matrix< T, NRows, NColumns > & v)
283 {
284  os << v.GetVnlMatrix();
285  return os;
286 }
287 
288 
289 template< typename T, unsigned int NRows, unsigned int NColumns >
291 {
292  a.swap(b);
293 }
294 
295 } // end namespace itk
296 
297 #ifndef ITK_MANUAL_INSTANTIATION
298 #include "itkMatrix.hxx"
299 #endif
300 
301 #endif
const Self & operator=(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:192
const Self & operator=(const InternalMatrixType &matrix)
Definition: itkMatrix.h:228
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:49
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:485
void operator*=(const T &value)
Definition: itkMatrix.h:116
Matrix(const Self &matrix)
Definition: itkMatrix.h:269
bool operator!=(const Self &matrix) const
Definition: itkMatrix.h:223
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Define numeric traits for std::vector.
void SetIdentity()
Definition: itkMatrix.h:180
T & operator()(unsigned int row, unsigned int col)
Definition: itkMatrix.h:144
vnl_matrix_fixed< T, NColumns, NRows > GetTranspose() const
Definition: itkMatrix.h:260
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
InternalMatrixType m_Matrix
Definition: itkMatrix.h:278
Self operator*(const T &value) const
Definition: itkMatrix.h:120
Matrix(const InternalMatrixType &matrix)
Definition: itkMatrix.h:235
const T & operator()(unsigned int row, unsigned int col) const
Definition: itkMatrix.h:150
const InternalMatrixType & GetVnlMatrix() const
Definition: itkMatrix.h:174
const Self & operator=(const Self &matrix)
Definition: itkMatrix.h:241
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
Matrix< T, NRows, OuterDim > operator*(const vnl_matrix_fixed< T, NRows, OuterDim > &matrix) const
Definition: itkMatrix.h:87
T * operator[](unsigned int i)
Definition: itkMatrix.h:156
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
bool operator==(const Self &matrix) const
Definition: itkMatrix.h:205
Self operator/(const T &value) const
Definition: itkMatrix.h:135
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:718
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:168
void Fill(const T &value)
Definition: itkMatrix.h:186
Matrix(const vnl_matrix< T > &matrix)
Definition: itkMatrix.h:199
vnl_matrix_fixed< T, NColumns, NRows > GetInverse() const
Definition: itkMatrix.h:248
void swap(Self &other)
Definition: itkMatrix.h:271
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
A templated class holding a n-Dimensional covariant vector.
vnl_matrix_fixed< double, NRows, NColumns > InternalMatrixType
Definition: itkMatrix.h:64
const T * operator[](unsigned int i) const
Definition: itkMatrix.h:162
void operator/=(const T &value)
Definition: itkMatrix.h:129