Manicore
Library to implement schemes on n-dimensionnal manifolds.
geometry.hpp
Go to the documentation of this file.
1 #ifndef GEOMETRY_HPP_INCLUDED
2 #define GEOMETRY_HPP_INCLUDED
3 
4 #include <Eigen/Dense>
5 #include <Eigen/Geometry>
6 
7 #include "definitions.hpp"
8 
12 namespace Manicore::Geometry {
13 
15  inline double volume_tetrahedron(Eigen::Vector3d const & a,
16  Eigen::Vector3d const & b,
17  Eigen::Vector3d const & c,
18  Eigen::Vector3d const & d) {
19  return std::abs((a-d).dot((b-d).cross(c-d)))/6.;
20 
21  }
23  inline double volume_triangle(Eigen::Vector3d const &x1,
24  Eigen::Vector3d const &x2,
25  Eigen::Vector3d const &x3) {
26  double a = (x1 - x2).squaredNorm();
27  double b = (x1 - x3).squaredNorm();
28  double c = (x2 - x3).squaredNorm();
29  // Heron(s formula
30  return std::sqrt((a+b+c)*(a+b+c) - 2.*(a*a+b*b+c*c))*0.25;
31  }
33  inline double volume_triangle(Eigen::Vector2d const &x1,
34  Eigen::Vector2d const &x2,
35  Eigen::Vector2d const &x3) {
36  return std::abs((x1(0)-x3(0))*(x2(1)-x1(1)) - (x1(0)-x2(0))*(x3(1)-x1(1)))*0.5;
37  }
38 
40  template<typename T>
41  double diameter(std::vector<T> const & vl) {
42  double diam = 0.;
43  for (size_t i = 0; i < vl.size(); ++i) {
44  for (size_t j = 0; j < vl.size(); ++j) {
45  diam = std::max(diam,(vl[i]-vl[j]).squaredNorm());
46  }
47  }
48  return std::sqrt(diam);
49  }
51 
53  template<size_t dimension,size_t d>
54  Eigen::Matrix<double,dimension,d> tangent_space(std::vector<Eigen::Vector<double,dimension>> const & vl) {
55  assert(vl.size() > d);
56  assert(d>1);
57  Eigen::Matrix<double,dimension,d> rv;
58  rv.col(0) = (vl[1]-vl[0]).normalise();
59  size_t nb_filled = 1;
60  for (size_t j = 1; j < vl.size(); ++j) {
61  Eigen::Vector<double,dimension> tmp = vl[j]-vl[0];
62  for (size_t i = 0; i < nb_filled; ++i) {
63  double dotv = tmp.dot(rv.col(i));
64  tmp = tmp - dotv*rv.col(i);
65  }
66  if (tmp.squaredNorm() > 1e-10) {
67  rv.col(nb_filled) = tmp.normalise();
68  nb_filled++;
69  if (nb_filled == d) return rv;
70  }
71  }
72  return rv;
73  }
74 
76  template<size_t d>
77  Eigen::Vector<double,d> middleSimplex(Simplex<d> const & S)
78  {
79  Eigen::Vector<double,d> mid = S[d];
80  for (size_t i = 0; i < d; ++i) {
81  mid += S[i];
82  }
83  return mid/(d+1.);
84  }
85 
87  template<size_t d>
88  bool inside(Eigen::Vector<double,d> const &x, Simplex<d> const & S)
89  {
90  constexpr double esp = 1e-10;
91  Eigen::Matrix<double,d,d> T;
92  for (size_t i = 0; i < d; ++i) {
93  for (size_t j = 0; j < d; ++j) {
94  T(i,j) = S[j](i) - S[d](i);
95  }
96  }
97  Eigen::Vector<double,d> L = T.partialPivLu().solve(x - S[d]);
98  double p_sum = 0.;
99  for (size_t i = 0; i < d; ++i) {
100  if (L(i) < -esp || L(i) > 1. + esp) return false;
101  p_sum += L(i);
102  }
103  return p_sum < 1. + esp && p_sum > -esp;
104  }
105 
106 } // end namespace Volume
107 
108 #endif
109 
Reference for signature of the functions that must be provided alongside the mesh.
Definition: geometry.hpp:12
Eigen::Matrix< double, dimension, d > tangent_space(std::vector< Eigen::Vector< double, dimension >> const &vl)
Compute a possible basis for the tangent space from a vector of vertices.
Definition: geometry.hpp:54
double volume_tetrahedron(Eigen::Vector3d const &a, Eigen::Vector3d const &b, Eigen::Vector3d const &c, Eigen::Vector3d const &d)
Volume of a tetrahedron in 3D given by its 4 vertices.
Definition: geometry.hpp:15
double volume_triangle(Eigen::Vector3d const &x1, Eigen::Vector3d const &x2, Eigen::Vector3d const &x3)
Area of a triangle in 3D given by its 3 vertices.
Definition: geometry.hpp:23
double diameter(std::vector< T > const &vl)
Diameter of the convex hull of a vector of vertices in ND.
Definition: geometry.hpp:41
bool inside(Eigen::Vector< double, d > const &x, Simplex< d > const &S)
Is a vector x inside a simplex S.
Definition: geometry.hpp:88
Eigen::Vector< double, d > middleSimplex(Simplex< d > const &S)
Compute the centroid of a simplex S.
Definition: geometry.hpp:77
std::array< Eigen::Vector< double, d >, d+1 > Simplex
Array of d+1 points of .
Definition: definitions.hpp:16