12#ifndef DUMUX_GEOMETRY_INTERSECTS_POINT_SIMPLEX_HH
13#define DUMUX_GEOMETRY_INTERSECTS_POINT_SIMPLEX_HH
16#include <dune/common/fvector.hh>
25template<
class ctype,
int dimworld,
typename std::enable_if_t<(dimworld == 3),
int> = 0>
27 const Dune::FieldVector<ctype, dimworld>& p0,
28 const Dune::FieldVector<ctype, dimworld>& p1,
29 const Dune::FieldVector<ctype, dimworld>& p2,
30 const Dune::FieldVector<ctype, dimworld>& p3)
34 using GlobalPosition = Dune::FieldVector<ctype, dimworld>;
35 static constexpr ctype eps_ = 1.0e-7;
38 const GlobalPosition *p[4] = {&p0, &p1, &p2, &p3};
41 for (
int i = 0; i < 4; ++i)
44 const GlobalPosition v1 = *p[(i + 1)%4] - *p[i];
45 const GlobalPosition v2 = *p[(i + 2)%4] - *p[i];
46 const GlobalPosition v3 = *p[(i + 3)%4] - *p[i];
47 const GlobalPosition v = point - *p[i];
52 const auto t1 = n1.dot(v);
53 const auto t2 = n1.dot(v3);
56 const auto eps = eps_ * v1.two_norm();
57 if ((t1 > eps || t1 < -eps) && std::signbit(t1) != std::signbit(t2))
67template<
class ctype,
int dimworld,
typename std::enable_if_t<(dimworld == 3),
int> = 0>
69 const Dune::FieldVector<ctype, dimworld>& p0,
70 const Dune::FieldVector<ctype, dimworld>& p1,
71 const Dune::FieldVector<ctype, dimworld>& p2)
75 constexpr ctype eps_ = 1.0e-7;
78 const auto v1 = p0 - p2;
80 const ctype nnorm = n.two_norm();
81 const ctype eps4 = eps_*nnorm*nnorm;
94 const auto a = p0 - point;
95 const auto b = p1 - point;
96 const auto c = p2 - point;
103 if (u*v < 0.0 - eps4)
110 if (u*w < 0.0 - eps4)
114 if (u.two_norm2() < eps4)
115 return b*c < 0.0 + eps_*nnorm;
116 if (v.two_norm2() < eps4)
117 return a*c < 0.0 + eps_*nnorm;
127template<
class ctype,
int dimworld,
typename std::enable_if_t<(dimworld == 2),
int> = 0>
129 const Dune::FieldVector<ctype, dimworld>& p0,
130 const Dune::FieldVector<ctype, dimworld>& p1,
131 const Dune::FieldVector<ctype, dimworld>& p2)
133 static constexpr ctype eps_ = 1.0e-7;
136 const ctype A = 0.5*(-p1[1]*p2[0] + p0[1]*(p2[0] - p1[0])
137 +p1[0]*p2[1] + p0[0]*(p1[1] - p2[1]));
138 const ctype
sign = std::copysign(1.0, A);
139 const ctype s =
sign*(p0[1]*p2[0] + point[0]*(p2[1]-p0[1])
140 -p0[0]*p2[1] + point[1]*(p0[0]-p2[0]));
141 const ctype t =
sign*(p0[0]*p1[1] + point[0]*(p0[1]-p1[1])
142 -p0[1]*p1[0] + point[1]*(p1[0]-p0[0]));
143 const ctype eps =
sign*A*eps_;
147 && (s + t) < 2*A*
sign + eps);
155template<
class ctype,
int dimworld,
typename std::enable_if_t<(dimworld == 3 || dimworld == 2),
int> = 0>
157 const Dune::FieldVector<ctype, dimworld>& p0,
158 const Dune::FieldVector<ctype, dimworld>& p1)
160 using GlobalPosition = Dune::FieldVector<ctype, dimworld>;
161 static constexpr ctype eps_ = 1.0e-7;
164 const GlobalPosition v1 = p1 - p0;
165 const GlobalPosition v2 = point - p0;
167 const ctype v1norm = v1.two_norm();
168 const ctype v2norm = v2.two_norm();
171 if (v2norm < v1norm*eps_)
176 if (v1.dot(v2) < 0.0 || v2norm > v1norm*(1.0 + eps_))
182 const auto eps2 = v1norm*v1norm*eps_;
183 if constexpr (dimworld == 3)
184 return n.two_norm2() < eps2*eps2;
188 return abs(n) < eps2;
196template<
class ctype,
int dimworld,
typename std::enable_if_t<(dimworld == 1),
int> = 0>
198 const Dune::FieldVector<ctype, dimworld>& p0,
199 const Dune::FieldVector<ctype, dimworld>& p1)
201 static constexpr ctype eps_ = 1.0e-7;
204 const ctype *interval[2] = {&p0[0], &p1[0]};
205 if (*interval[0] > *interval[1])
206 std::swap(interval[0], interval[1]);
208 const ctype v1 = point[0] - *interval[0];
209 const ctype v2 = *interval[1] - *interval[0];
213 if (abs(v1) < v2*eps_)
225 return (!signbit(v1) && abs(v1) < v2*(1.0 + eps_));
Dune::FieldVector< Scalar, 3 > crossProduct(const Dune::FieldVector< Scalar, 3 > &vec1, const Dune::FieldVector< Scalar, 3 > &vec2)
Cross product of two vectors in three-dimensional Euclidean space.
Definition: math.hh:642
constexpr int sign(const ValueType &value) noexcept
Sign or signum function.
Definition: math.hh:629
bool intersectsPointSimplex(const Dune::FieldVector< ctype, dimworld > &point, const Dune::FieldVector< ctype, dimworld > &p0, const Dune::FieldVector< ctype, dimworld > &p1, const Dune::FieldVector< ctype, dimworld > &p2, const Dune::FieldVector< ctype, dimworld > &p3)
Find out whether a point is inside the tetrahedron (p0, p1, p2, p3) (dimworld is 3)
Definition: intersectspointsimplex.hh:26
Define some often used mathematical functions.