3d/matrix.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2014 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 #ifndef __mia_3d_matrix_hh
21 #define __mia_3d_matrix_hh
22 
23 #ifdef __GNUC__
24 #pragma GCC diagnostic push
25 #ifndef __clang__
26 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
27 #endif
28 #endif
29 
30 #include <Eigen/Core>
31 #include <Eigen/Eigenvalues>
32 
33 #ifdef __GNUC__
34 #pragma GCC diagnostic pop
35 #endif
36 
37 #include <memory>
38 
39 #include <mia/3d/vector.hh>
40 #include <mia/core/msgstream.hh>
41 
43 
54 template <typename T>
55 class T3DMatrix: public T3DVector< T3DVector<T> > {
56 
57 
58 public:
59 
60  T3DMatrix() = default;
61  T3DMatrix(const T3DMatrix<T>& o);
62 
64 
65 
71  static T3DMatrix<T> diagonal(T value);
72 
78  static T3DMatrix<T> diagonal(const T3DVector<T>& values);
79 
80 
86  template <typename I>
87  T3DMatrix(const T3DMatrix<I>& o);
88 
89 
95  T3DMatrix(const T3DVector< T3DVector<T> >& other);
96 
97 
104  T3DMatrix(const T3DVector< T >& x, const T3DVector< T >& y, const T3DVector< T >& z );
105 
111  T3DMatrix<T>& operator -= (const T3DMatrix<T>& other);
112 
113 
118  void print( std::ostream& os) const;
119 
120 
124  T3DMatrix<T> transposed()const;
125 
126 
130  T get_det() const;
131 
132 
136  int get_rank()const;
137 
147  int get_eigenvalues(C3DFVector& v)const;
148 
154  C3DFVector get_eigenvector(int i)const;
155 
156 
158  static const T3DMatrix _1;
159 
161  static const T3DMatrix _0;
162 
163 private:
164  void evaluate_ev() const;
165 
166  typedef Eigen::Matrix<T, 3, 3> EMatrix3;
167  typedef Eigen::EigenSolver<EMatrix3> ESolver3;
168 
169  mutable std::unique_ptr<ESolver3> m_esolver;
170  mutable int m_ev_order[3];
171 };
172 
173 template <typename T>
174 template <typename I>
176  T3DVector<T3DVector<T> >(T3DVector<T>(o.x),
177  T3DVector<T>(o.y),
178  T3DVector<T>(o.z))
179 {
180 }
181 
182 template <typename T>
184 {
185  return T3DVector<T>(dot(m.x, x), dot(m.y, x), dot(m.z, x));
186 }
187 
188 template <typename T>
190 {
191  return T3DVector<T>(m.x.x * x.x + m.y.x * x.y + m.z.x * x.z,
192  m.x.y * x.x + m.y.y * x.y + m.z.y * x.z,
193  m.x.z * x.x + m.y.z * x.y + m.z.z * x.z);
194 }
195 
196 
197 template <typename T>
198 std::ostream& operator << (std::ostream& os, const T3DMatrix<T>& m)
199 {
200  m.print(os);
201  return os;
202 }
203 
204 template <typename T>
206 {
207  this->x -= o.x;
208  this->y -= o.y;
209  this->z -= o.z;
210  return *this;
211 }
212 
213 template <typename T>
215 {
216  return T3DMatrix<T>(T3DVector<T>(m.x.x * x.x.x + m.x.y * x.y.x + m.x.z * x.z.x,
217  m.x.x * x.x.y + m.x.y * x.y.y + m.x.z * x.z.y,
218  m.x.x * x.x.z + m.x.y * x.y.z + m.x.z * x.z.z),
219  T3DVector<T>(m.y.x * x.x.x + m.y.y * x.y.x + m.y.z * x.z.x,
220  m.y.x * x.x.y + m.y.y * x.y.y + m.y.z * x.z.y,
221  m.y.x * x.x.z + m.y.y * x.y.z + m.y.z * x.z.z),
222  T3DVector<T>(m.z.x * x.x.x + m.z.y * x.y.x + m.z.z * x.z.x,
223  m.z.x * x.x.y + m.z.y * x.y.y + m.z.z * x.z.y,
224  m.z.x * x.x.z + m.z.y * x.y.z + m.z.z * x.z.z));
225 }
226 
229 
232 
233 
234 template <typename T>
236  T3DVector< T >(0,1,0),
237  T3DVector< T >(0,0,1));
238 
239 template <typename T>
241 
242 extern template class EXPORT_3D T3DMatrix<float>;
243 extern template class EXPORT_3D T3DMatrix<double>;
244 
245 
247 
248 #endif
T3DMatrix< T > & operator-=(const T3DMatrix< T > &other)
Definition: 3d/matrix.hh:205
T3DMatrix< T > transposed() const
T3DVector< T > z
vector element
Definition: 3d/vector.hh:55
static const T3DMatrix _0
The zero matrix.
Definition: 3d/matrix.hh:161
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:43
static T3DMatrix< T > diagonal(T value)
T3DMatrix< float > C3DFMatrix
a simple 3x3 matrix with single precision floating point values
Definition: 3d/matrix.hh:228
#define EXPORT_3D
Definition: defines3d.hh:44
int get_rank() const
T3DMatrix< T > & operator=(const T3DMatrix< T > &o)
T3DMatrix< double > C3DDMatrix
a simple 3x3 matrix with double precision floating point values
Definition: 3d/matrix.hh:231
int get_eigenvalues(C3DFVector &v) const
T3DVector< T > y
vector element
Definition: 3d/vector.hh:53
T3DVector< T > operator*(const T3DMatrix< T > &m, const T3DVector< T > &x)
Definition: 3d/matrix.hh:183
T3DMatrix()=default
void print(std::ostream &os) const
C3DFVector get_eigenvector(int i) const
A simple 3D vector type.
Definition: 3d/vector.hh:48
T dot(const T2DVector< T > &a, const T2DVector< T > &b)
Definition: 2d/vector.hh:395
a simple 3x3 matrix
Definition: 3d/matrix.hh:55
T3DVector< T > x
vector element
Definition: 3d/vector.hh:51
T get_det() const
static const T3DMatrix _1
The unity matrix.
Definition: 3d/matrix.hh:158
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:46