3.2-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
triangulation.hh
Go to the documentation of this file.
1/*****************************************************************************
2 * See the file COPYING for full copying permissions. *
3 * *
4 * This program is free software: you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation, either version 3 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16 *****************************************************************************/
22#ifndef DUMUX_TRIANGULATION_HH
23#define DUMUX_TRIANGULATION_HH
24
25#include <vector>
26#include <array>
27#include <algorithm>
28#include <type_traits>
29
30#include <dune/common/exceptions.hh>
31#include <dune/common/fvector.hh>
32
33#include <dumux/common/math.hh>
34
35namespace Dumux {
36namespace TriangulationPolicy {
37
41
44
45template<int dim, int dimWorld>
46using DefaultPolicy = std::conditional_t< dim >= 2, MidPointPolicy, DelaunayPolicy >;
47
48} // end namespace TriangulationPolicy
49
51template<int dim, int dimWorld, class ctype>
52using Triangulation = std::vector< std::array<Dune::FieldVector<ctype, dimWorld>, dim+1> >;
53
65template< int dim, int dimWorld, class Policy = TriangulationPolicy::DefaultPolicy<dim, dimWorld>,
66 class RandomAccessContainer,
67 std::enable_if_t< std::is_same<Policy, TriangulationPolicy::MidPointPolicy>::value
68 && dim == 2, int> = 0 >
70triangulate(const RandomAccessContainer& convexHull)
71{
72 using ctype = typename RandomAccessContainer::value_type::value_type;
73 using Point = Dune::FieldVector<ctype, dimWorld>;
74 using Triangle = std::array<Point, 3>;
75
76 static_assert(std::is_same<typename RandomAccessContainer::value_type, Point>::value,
77 "Triangulation expects Dune::FieldVector as point type");
78
79 if (convexHull.size() < 3)
80 DUNE_THROW(Dune::InvalidStateException, "Try to triangulate point cloud with less than 3 points!");
81
82 if (convexHull.size() == 3)
83 return std::vector<Triangle>(1, {convexHull[0], convexHull[1], convexHull[2]});
84
85 Point midPoint(0.0);
86 for (const auto p : convexHull)
87 midPoint += p;
88 midPoint /= convexHull.size();
89
90 std::vector<Triangle> triangulation;
91 triangulation.reserve(convexHull.size());
92
93 for (std::size_t i = 0; i < convexHull.size()-1; ++i)
94 triangulation.emplace_back(Triangle{midPoint, convexHull[i], convexHull[i+1]});
95
96 triangulation.emplace_back(Triangle{midPoint, convexHull[convexHull.size()-1], convexHull[0]});
97
98 return triangulation;
99}
100
111template< int dim, int dimWorld, class Policy = TriangulationPolicy::DefaultPolicy<dim, dimWorld>,
112 class RandomAccessContainer,
113 std::enable_if_t< std::is_same<Policy, TriangulationPolicy::DelaunayPolicy>::value
114 && dim == 1, int> = 0 >
115inline Triangulation<dim, dimWorld, typename RandomAccessContainer::value_type::value_type>
116triangulate(const RandomAccessContainer& points)
117{
118 using ctype = typename RandomAccessContainer::value_type::value_type;
119 using Point = Dune::FieldVector<ctype, dimWorld>;
120
121 static_assert(std::is_same<typename RandomAccessContainer::value_type, Point>::value,
122 "Triangulation expects Dune::FieldVector as point type");
123
124 if (points.size() == 2)
125 return Triangulation<dim, dimWorld, ctype>({ {points[0], points[1]} });
126
128 assert(points.size() > 1);
129 DUNE_THROW(Dune::NotImplemented, "1d triangulation for point cloud size > 2");
130}
131
132} // end namespace Dumux
133
134# endif
Define some often used mathematical functions.
Triangulation< dim, dimWorld, typename RandomAccessContainer::value_type::value_type > triangulate(const RandomAccessContainer &convexHull)
Triangulate area given points of a convex hull.
Definition: triangulation.hh:70
Definition: adapt.hh:29
std::vector< std::array< Dune::FieldVector< ctype, dimWorld >, dim+1 > > Triangulation
The data type to store triangulations.
Definition: triangulation.hh:52
std::conditional_t< dim >=2, MidPointPolicy, DelaunayPolicy > DefaultPolicy
Definition: triangulation.hh:46
Definition: triangulation.hh:40
Delaunay-type triangulations.
Definition: triangulation.hh:43