26#ifndef DUMUX_GEOMETRY_TRIANGULATION_HH
27#define DUMUX_GEOMETRY_TRIANGULATION_HH
35#include <dune/common/exceptions.hh>
36#include <dune/common/fvector.hh>
43namespace TriangulationPolicy {
58using DefaultDimPolicies = std::tuple<DelaunayPolicy, MidPointPolicy, ConvexHullPolicy>;
63template<
int dim,
int dimWorld>
64using DefaultPolicy = std::tuple_element_t<dim-1, Detail::DefaultDimPolicies>;
75template<
int dim,
int dimWorld,
class ctype>
76using Triangulation = std::vector< std::array<Dune::FieldVector<ctype, dimWorld>, dim+1> >;
88template<
int dim,
int dimWorld,
class Policy = TriangulationPolicy::DefaultPolicy<dim, dimWorld>,
89 class RandomAccessContainer,
90 std::enable_if_t< std::is_same_v<Policy, TriangulationPolicy::DelaunayPolicy>
91 && dim == 1,
int> = 0 >
95 using ctype =
typename RandomAccessContainer::value_type::value_type;
96 using Point = Dune::FieldVector<ctype, dimWorld>;
98 static_assert(std::is_same_v<typename RandomAccessContainer::value_type, Point>,
99 "Triangulation expects Dune::FieldVector as point type");
101 if (points.size() == 2)
105 assert(points.size() > 1);
106 DUNE_THROW(Dune::NotImplemented,
"1d triangulation for point cloud size > 2");
121template<
int dim,
int dimWorld,
class Policy = TriangulationPolicy::DefaultPolicy<dim, dimWorld>,
122 class RandomAccessContainer,
123 std::enable_if_t< std::is_same_v<Policy, TriangulationPolicy::M
idPo
intPolicy>
124 && dim == 2,
int> = 0 >
125inline Triangulation<dim, dimWorld, typename RandomAccessContainer::value_type::value_type>
126triangulate(
const RandomAccessContainer& convexHullPoints)
128 using ctype =
typename RandomAccessContainer::value_type::value_type;
129 using Point = Dune::FieldVector<ctype, dimWorld>;
130 using Triangle = std::array<Point, 3>;
132 static_assert(std::is_same_v<typename RandomAccessContainer::value_type, Point>,
133 "Triangulation expects Dune::FieldVector as point type");
135 if (convexHullPoints.size() < 3)
136 DUNE_THROW(Dune::InvalidStateException,
"Try to triangulate point cloud with less than 3 points!");
138 if (convexHullPoints.size() == 3)
139 return std::vector<Triangle>(1, {convexHullPoints[0], convexHullPoints[1], convexHullPoints[2]});
142 for (
const auto& p : convexHullPoints)
144 midPoint /= convexHullPoints.size();
146 std::vector<Triangle> triangulation;
147 triangulation.reserve(convexHullPoints.size());
149 for (std::size_t i = 0; i < convexHullPoints.size()-1; ++i)
150 triangulation.emplace_back(Triangle{midPoint, convexHullPoints[i], convexHullPoints[i+1]});
152 triangulation.emplace_back(Triangle{midPoint, convexHullPoints[convexHullPoints.size()-1], convexHullPoints[0]});
154 return triangulation;
169template<
int dim,
int dimWorld,
class Policy = TriangulationPolicy::DefaultPolicy<dim, dimWorld>,
170 class RandomAccessContainer,
171 std::enable_if_t< std::is_same_v<Policy, TriangulationPolicy::ConvexHullPolicy>
172 && dim == 2,
int> = 0 >
173inline Triangulation<dim, dimWorld, typename RandomAccessContainer::value_type::value_type>
176 const auto convexHullPoints = grahamConvexHull<2>(points);
177 return triangulate<dim, dimWorld, TriangulationPolicy::MidPointPolicy>(convexHullPoints);
190template<
int dim,
int dimWorld,
class Policy = TriangulationPolicy::DefaultPolicy<dim, dimWorld>,
191 class RandomAccessContainer,
192 std::enable_if_t< std::is_same_v<Policy, TriangulationPolicy::ConvexHullPolicy>
193 && dim == 3,
int> = 0 >
194inline Triangulation<dim, dimWorld, typename RandomAccessContainer::value_type::value_type>
197 using ctype =
typename RandomAccessContainer::value_type::value_type;
198 using Point = Dune::FieldVector<ctype, dimWorld>;
199 using Tetrahedron = std::array<Point, 4>;
201 static_assert(std::is_same_v<typename RandomAccessContainer::value_type, Point>,
202 "Triangulation expects Dune::FieldVector as point type");
204 const auto numPoints = points.size();
206 DUNE_THROW(Dune::InvalidStateException,
"Trying to create 3D triangulation of point cloud with less than 4 points!");
209 return std::vector<Tetrahedron>(1, {points[0], points[1], points[2], points[3]});
214 Point lowerLeft(1e100);
215 Point upperRight(-1e100);
216 for (
const auto& p : points)
219 for (
int i = 0; i < dimWorld; ++i)
221 using std::max;
using std::min;
222 lowerLeft[i] = min(p[i], lowerLeft[i]);
223 upperRight[i] = max(p[i], upperRight[i]);
226 midPoint /= numPoints;
228 auto magnitude = 0.0;
230 for (
int i = 0; i < dimWorld; ++i)
231 magnitude = max(upperRight[i] - lowerLeft[i], magnitude);
232 const auto eps = 1e-7*magnitude;
233 const auto eps2 = eps*magnitude;
236 std::vector<Tetrahedron> triangulation;
237 triangulation.reserve(numPoints);
240 std::vector<Point> coplanarPointBuffer;
241 coplanarPointBuffer.reserve(std::min<std::size_t>(12, numPoints-1));
246 std::vector<std::pair<int, Point>> coplanarClusters;
247 coplanarClusters.reserve(numPoints/3);
253 for (
int i = 0; i < numPoints; ++i)
255 for (
int j = i+1; j < numPoints; ++j)
257 for (
int k = j+1; k < numPoints; ++k)
259 const auto pointI = points[i];
260 const auto ab = points[j] - pointI;
261 const auto ac = points[k] - pointI;
265 coplanarPointBuffer.clear();
269 const bool isAdmissible = [&]()
273 if (
normal.two_norm2() < eps2*eps2)
277 for (
int m = 0; m < numPoints; ++m)
279 if (m != i && m != j && m != k)
282 const auto ad = points[m] - pointI;
283 const auto sp =
normal*ad;
286 using std::abs;
using std::signbit;
287 const bool coplanar = abs(sp) < eps2*magnitude;
288 int newMarker = coplanar ? 0 : signbit(sp) ? -1 : 1;
292 if (marker == 0 && newMarker != 0)
297 if (newMarker != 0 && marker != newMarker)
304 if (m < k && std::find_if(
305 coplanarClusters.begin(), coplanarClusters.end(),
306 [=](
const auto& c){ return c.first == std::min(m, i) && abs(ab*c.second) < eps2*magnitude; }
307 ) != coplanarClusters.end())
310 coplanarPointBuffer.clear();
314 coplanarPointBuffer.push_back(points[m]);
321 if (!coplanarPointBuffer.empty())
323 coplanarPointBuffer.insert(coplanarPointBuffer.end(), { points[i], points[j], points[k] });
324 coplanarClusters.emplace_back(std::make_pair(i,
normal));
338 if (!coplanarPointBuffer.empty())
340 const auto triangles = triangulate<2, 3, TriangulationPolicy::ConvexHullPolicy>(coplanarPointBuffer);
341 for (
const auto& triangle : triangles)
343 const auto ab = triangle[1] - triangle[0];
344 const auto ac = triangle[2] - triangle[0];
346 const auto am = midPoint - triangle[0];
347 const auto sp =
normal*am;
349 const bool isBelow = signbit(sp);
351 triangulation.emplace_back(Tetrahedron{
352 triangle[0], triangle[2], triangle[1], midPoint
355 triangulation.emplace_back(Tetrahedron{
356 triangle[0], triangle[1], triangle[2], midPoint
362 const auto am = midPoint - pointI;
363 const auto sp =
normal*am;
365 const bool isBelow = signbit(sp);
367 triangulation.emplace_back(Tetrahedron{
368 pointI, points[k], points[j], midPoint
371 triangulation.emplace_back(Tetrahedron{
372 pointI, points[j], points[k], midPoint
381 if (triangulation.size() < 4)
382 DUNE_THROW(Dune::InvalidStateException,
"Something went wrong with the triangulation!");
384 return triangulation;
Define some often used mathematical functions.
A function to compute the convex hull of a point cloud.
Detect if a point intersects a simplex (including boundary)
Triangulation< dim, dimWorld, typename RandomAccessContainer::value_type::value_type > triangulate(const RandomAccessContainer &points)
Triangulate area given points of a convex hull (1d)
Definition: triangulation.hh:93
Vector normal(const Vector &v)
Create a vector normal to the given one (v is expected to be non-zero)
Definition: normal.hh:38
std::vector< std::array< Dune::FieldVector< ctype, dimWorld >, dim+1 > > Triangulation
The default data type to store triangulations.
Definition: triangulation.hh:76
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:654
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
std::tuple_element_t< dim-1, Detail::DefaultDimPolicies > DefaultPolicy
Default policy for a given dimension.
Definition: triangulation.hh:64
Definition: triangulation.hh:47
Definition: triangulation.hh:51
Delaunay-type triangulations.
Definition: triangulation.hh:54