22#ifndef DUMUX_GEOMETRY_GRAHAM_CONVEX_HULL_HH
23#define DUMUX_GEOMETRY_GRAHAM_CONVEX_HULL_HH
30#include <dune/common/exceptions.hh>
31#include <dune/common/fvector.hh>
46 const Dune::FieldVector<ctype, 3>& b,
47 const Dune::FieldVector<ctype, 3>& c,
48 const Dune::FieldVector<ctype, 3>&
normal)
53 const auto area = f*
normal;
64template<
int dim,
class ctype,
65 std::enable_if_t<(dim==2),
int> = 0>
66std::vector<Dune::FieldVector<ctype, 3>>
69 using Point = Dune::FieldVector<ctype, 3>;
70 std::vector<Point> convexHull;
73 if (points.size() < 3)
77 if (points.size() == 3)
81 const auto a = points[1] - points[0];
82 auto b = points[2] - points[0];
87 auto norm =
normal.two_norm();
88 while (norm == 0.0 && k < points.size()-1)
100 const auto eps = 1e-7*sqrt(norm);
104 auto minIt = std::min_element(points.begin(), points.end(), [&eps](
const auto& a,
const auto& b)
107 return (abs(a[0]-b[0]) > eps ? a[0] < b[0] : (abs(a[1]-b[1]) > eps ? a[1] < b[1] : (a[2] < b[2])));
111 std::iter_swap(minIt, points.begin());
115 const auto pivot = points[0];
116 std::sort(points.begin()+1, points.end(), [&](
const auto& a,
const auto& b)
118 const int order = getOrientation(pivot, a, b, normal);
120 return (a-pivot).two_norm() < (b-pivot).two_norm();
122 return (order == -1);
126 convexHull.reserve(50);
127 convexHull.push_back(points[0]);
128 convexHull.push_back(points[1]);
129 convexHull.push_back(points[2]);
134 for (std::size_t i = 3; i < points.size(); ++i)
136 Point p = convexHull.back();
137 convexHull.pop_back();
142 if (convexHull.size() == 1)
146 assert(i < points.size()-1);
151 p = convexHull.back();
152 convexHull.pop_back();
157 convexHull.emplace_back(std::move(p));
158 convexHull.push_back(points[i]);
170template<
int dim,
class ctype,
171 std::enable_if_t<(dim==2),
int> = 0>
172std::vector<Dune::FieldVector<ctype, 2>>
175 std::vector<Dune::FieldVector<ctype, 3>> points3D;
176 points3D.reserve(points.size());
177 std::transform(points.begin(), points.end(), std::back_inserter(points3D),
178 [](
const auto& p) { return Dune::FieldVector<ctype, 3>({p[0], p[1], 0.0}); });
180 const auto result3D = grahamConvexHullImpl<2>(points3D);
182 std::vector<Dune::FieldVector<ctype, 2>> result2D;
183 result2D.reserve(result3D.size());
184 std::transform(result3D.begin(), result3D.end(), std::back_inserter(result2D),
185 [](
const auto& p) { return Dune::FieldVector<ctype, 2>({p[0], p[1]}); });
195template<
int dim,
class ctype,
int dimWorld>
196std::vector<Dune::FieldVector<ctype, dimWorld>>
grahamConvexHull(std::vector<Dune::FieldVector<ctype, dimWorld>>& points)
198 return grahamConvexHullImpl<dim>(points);
207template<
int dim,
class ctype,
int dimWorld>
208std::vector<Dune::FieldVector<ctype, dimWorld>>
grahamConvexHull(
const std::vector<Dune::FieldVector<ctype, dimWorld>>& points)
210 auto copyPoints = points;
211 return grahamConvexHullImpl<dim>(copyPoints);
Define some often used mathematical functions.
std::vector< Dune::FieldVector< ctype, 3 > > grahamConvexHullImpl(std::vector< Dune::FieldVector< ctype, 3 > > &points)
Compute the points making up the convex hull around the given set of unordered points.
Definition: grahamconvexhull.hh:67
int getOrientation(const Dune::FieldVector< ctype, 3 > &a, const Dune::FieldVector< ctype, 3 > &b, const Dune::FieldVector< ctype, 3 > &c, const Dune::FieldVector< ctype, 3 > &normal)
Returns the orientation of a sequence a-->b-->c in one plane (defined by normal vector)
Definition: grahamconvexhull.hh:45
Vector normal(const Vector &v)
Create a vector normal to the given one (v is expected to be non-zero)
Definition: normal.hh:36
std::vector< Dune::FieldVector< ctype, dimWorld > > grahamConvexHull(const std::vector< Dune::FieldVector< ctype, dimWorld > > &points)
Compute the points making up the convex hull around the given set of unordered points.
Definition: grahamconvexhull.hh:208
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
constexpr int sign(const ValueType &value) noexcept
Sign or signum function.
Definition: math.hh:641