MorphoGraphX  2.0-1-227
SymmetricTensor.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2016 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef SYMMETRIC_TENSOR_HPP
12 #define SYMMETRIC_TENSOR_HPP
13 //#include <Geometry.hpp>
14 #include <Vector.hpp>
15 #include <gsl/gsl_matrix.h>
16 #include <gsl/gsl_vector.h>
17 #include <gsl/gsl_eigen.h>
18 
19 
20 namespace mgx
21 {
22  // RSS Currently this is only floats, it should be templated
23  typedef Vector<3, float> Point3f;
24  typedef Vector<3, double> Point3d;
25  typedef Matrix<3, 3, double> Matrix3d;
26  //AR: This isn't the best place for these functions, they should be moved to their own hpp/cpp later
27  inline void calcEigenVecsValues(Matrix3d& M, Point3d& p1, Point3d& p2, Point3d& p3, Point3d& ev){
28 
29  gsl_matrix* mat = gsl_matrix_alloc(3, 3);
30  gsl_vector* eval = gsl_vector_alloc(3);
31  gsl_matrix* evec = gsl_matrix_alloc(3, 3);
32  gsl_eigen_symmv_workspace* w = gsl_eigen_symmv_alloc(3);
33 
34 
35  for(int i = 0; i < 3; ++i)
36  for(int j = 0; j < 3; ++j)
37  gsl_matrix_set(mat, i, j, M[i][j]);
38 
39  gsl_eigen_symmv(mat, eval, evec, w);
40  gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_VAL_DESC);
41 
42  p1 = Point3d(gsl_matrix_get(evec, 0, 0), gsl_matrix_get(evec, 1, 0), gsl_matrix_get(evec, 2, 0));
43  p2 = Point3d(gsl_matrix_get(evec, 0, 1), gsl_matrix_get(evec, 1, 1), gsl_matrix_get(evec, 2, 1));
44  p3 = Point3d(gsl_matrix_get(evec, 0, 2), gsl_matrix_get(evec, 1, 2), gsl_matrix_get(evec, 2, 2));
45  ev = Point3d(gsl_vector_get(eval, 0), gsl_vector_get(eval, 1), gsl_vector_get(eval, 2));
46 
47  }
48 
50  Matrix3d mat;
51  for(int i=0;i<3;i++)
52  for(int j=0;j<3;j++){
53  mat[i][j] = p1[i]*p2[j];
54  }
55  return mat;
56  }
57 
58 
60  public:
61  SymmetricTensor() : _ev1(0, 0, 0), _ev2(0, 0, 0), _evals(0, 0, 0) {}
62 
64  : _ev1(ev1), _ev2(ev2), _evals(evals) {}
65 
67  : _ev1(copy._ev1), _ev2(copy._ev2), _evals(copy._evals) {}
68 
70  {
71  _ev1 = other._ev1;
72  _ev2 = other._ev2;
73  _evals = other._evals;
74  return *this;
75  }
76 
77  bool operator==(const SymmetricTensor &other) const
78  {
79  if(_ev1 == other._ev1 and _ev2 == other._ev2 and _evals == other._evals)
80  return true;
81  return false;
82  }
83 
84  bool operator!=(const SymmetricTensor &other) const
85  {
86  return (!((*this) == other));
87  }
88 
89 
90  Point3f &ev1() {
91  return _ev1;
92  }
93 
94  Point3f &ev2() {
95  return _ev2;
96  }
97 
98  const Point3f &ev1() const {
99  return _ev1;
100  }
101 
102  const Point3f &ev2() const {
103  return _ev2;
104  }
105 
106  Point3f ev3() const {
107  return _ev1 ^ _ev2;
108  }
109 
110  const Point3f &evals() const {
111  return _evals;
112  }
113 
115  return _evals;
116  }
117 
119  Matrix3d mat;
123  mat = e1+e2+e3;
124 
125  return mat;
126  }
127 
128  bool fromMatrix(Matrix3d mat) {
129  Point3d p1,p2,p3,ev;
130 
131  calcEigenVecsValues(mat, p1, p2, p3, ev);
132  _ev1 = Point3f(p1.x(), p1.y(), p1.z());
133  _ev2 = Point3f(p2.x(), p2.y(), p2.z());
134  _evals = Point3f(ev.x(), ev.y(), ev.z());
135 
136  //Should be some error checking here
137  return true;
138  }
139 
140  protected:
143  };
144 }
145 #endif
mgx::SymmetricTensor::operator=
SymmetricTensor & operator=(const SymmetricTensor &other)
Definition: SymmetricTensor.hpp:69
Vector.hpp
mgx::SymmetricTensor::evals
const Point3f & evals() const
Definition: SymmetricTensor.hpp:110
mgx::SymmetricTensor::SymmetricTensor
SymmetricTensor()
Definition: SymmetricTensor.hpp:61
mgx::SymmetricTensor::SymmetricTensor
SymmetricTensor(const SymmetricTensor &copy)
Definition: SymmetricTensor.hpp:66
mgx::SymmetricTensor
Definition: SymmetricTensor.hpp:59
mgx::Vector::z
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
Definition: Vector.hpp:739
mgx::Matrix3d
Matrix< 3, 3, double > Matrix3d
Definition: Geometry.hpp:111
mgx::SymmetricTensor::operator==
bool operator==(const SymmetricTensor &other) const
Definition: SymmetricTensor.hpp:77
mgx::calcEigenVecsValues
void calcEigenVecsValues(Matrix3d &M, Point3d &p1, Point3d &p2, Point3d &p3, Point3d &ev)
Definition: SymmetricTensor.hpp:27
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::SymmetricTensor::ev1
const Point3f & ev1() const
Definition: SymmetricTensor.hpp:98
mgx::SymmetricTensor::toMatrix
Matrix3d toMatrix()
Definition: SymmetricTensor.hpp:118
mgx::SymmetricTensor::_evals
Point3f _evals
Definition: SymmetricTensor.hpp:142
mgx::SymmetricTensor::evals
Point3f & evals()
Definition: SymmetricTensor.hpp:114
mgx::Point3f
Vector< 3, float > Point3f
Definition: CuttingSurface.hpp:25
mgx::Point3d
Vector< 3, double > Point3d
Definition: Geometry.hpp:56
mgx::Vector::y
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
Definition: Vector.hpp:730
mgx::SymmetricTensor::_ev2
Point3f _ev2
Definition: SymmetricTensor.hpp:141
mgx::SymmetricTensor::ev3
Point3f ev3() const
Definition: SymmetricTensor.hpp:106
mgx::Vector< 3, double >
mgx::SymmetricTensor::ev2
const Point3f & ev2() const
Definition: SymmetricTensor.hpp:102
mgx::SymmetricTensor::SymmetricTensor
SymmetricTensor(const Point3f &ev1, const Point3f &ev2, const Point3f &evals)
Definition: SymmetricTensor.hpp:63
mgx::OuterProduct
Matrix3d OuterProduct(Point3d p1, Point3d p2)
Definition: SymmetricTensor.hpp:49
mgx::SymmetricTensor::fromMatrix
bool fromMatrix(Matrix3d mat)
Definition: SymmetricTensor.hpp:128
mgx::SymmetricTensor::_ev1
Point3f _ev1
Definition: SymmetricTensor.hpp:141
mgx::SymmetricTensor::ev1
Point3f & ev1()
Definition: SymmetricTensor.hpp:90
mgx::SymmetricTensor::operator!=
bool operator!=(const SymmetricTensor &other) const
Definition: SymmetricTensor.hpp:84
mgx::Vector::x
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
Definition: Vector.hpp:721
mgx::Matrix
Definition: Matrix.hpp:39
mgx::SymmetricTensor::ev2
Point3f & ev2()
Definition: SymmetricTensor.hpp:94