117 using ctype =
typename RandomAccessContainer::value_type::value_type;
118 using Point = Dune::FieldVector<ctype, dimWorld>;
119 using Triangle = std::array<Point, 3>;
121 static_assert(std::is_same_v<typename RandomAccessContainer::value_type, Point>,
122 "Triangulation expects Dune::FieldVector as point type");
124 if (convexHullPoints.size() < 3)
125 DUNE_THROW(Dune::InvalidStateException,
"Try to triangulate point cloud with less than 3 points!");
127 if (convexHullPoints.size() == 3)
128 return std::vector<Triangle>(1, {convexHullPoints[0], convexHullPoints[1], convexHullPoints[2]});
131 for (
const auto& p : convexHullPoints)
133 midPoint /= convexHullPoints.size();
135 std::vector<Triangle> triangulation;
136 triangulation.reserve(convexHullPoints.size());
138 for (std::size_t i = 0; i < convexHullPoints.size()-1; ++i)
139 triangulation.emplace_back(Triangle{midPoint, convexHullPoints[i], convexHullPoints[i+1]});
141 triangulation.emplace_back(Triangle{midPoint, convexHullPoints[convexHullPoints.size()-1], convexHullPoints[0]});
143 return triangulation;
186 using ctype =
typename RandomAccessContainer::value_type::value_type;
187 using Point = Dune::FieldVector<ctype, dimWorld>;
188 using Tetrahedron = std::array<Point, 4>;
190 static_assert(std::is_same_v<typename RandomAccessContainer::value_type, Point>,
191 "Triangulation expects Dune::FieldVector as point type");
193 const auto numPoints = points.size();
195 DUNE_THROW(Dune::InvalidStateException,
"Trying to create 3D triangulation of point cloud with less than 4 points!");
198 return std::vector<Tetrahedron>(1, {points[0], points[1], points[2], points[3]});
203 Point lowerLeft(1e100);
204 Point upperRight(-1e100);
205 for (
const auto& p : points)
208 for (
int i = 0; i < dimWorld; ++i)
210 using std::max;
using std::min;
211 lowerLeft[i] = min(p[i], lowerLeft[i]);
212 upperRight[i] = max(p[i], upperRight[i]);
215 midPoint /= numPoints;
217 auto magnitude = 0.0;
219 for (
int i = 0; i < dimWorld; ++i)
220 magnitude = max(upperRight[i] - lowerLeft[i], magnitude);
221 const auto eps = 1e-7*magnitude;
222 const auto eps2 = eps*eps;
223 const auto epsDist = 1e-8*magnitude;
226 std::vector<std::vector<int>> faces;
227 faces.reserve(2*numPoints);
228 std::vector<int> face;
229 face.reserve(numPoints);
231 const auto isOnFace = [&](
int i,
int j,
int k)
233 return std::any_of(faces.begin(), faces.end(), [&](
const auto& f)
235 return std::binary_search(f.begin(), f.end(), i)
236 && std::binary_search(f.begin(), f.end(), j)
237 && std::binary_search(f.begin(), f.end(), k);
245 const auto isSameFace = [&](
const auto& f,
const auto& g)
247 std::vector<int> shared;
248 std::set_intersection(f.begin(), f.end(), g.begin(), g.end(), std::back_inserter(shared));
249 if (shared.size() < 3)
252 std::size_t a = 0, b = 1;
253 for (std::size_t m = 0; m < shared.size(); ++m)
254 for (std::size_t n = m+1; n < shared.size(); ++n)
255 if ((points[shared[n]] - points[shared[m]]).two_norm2() > (points[shared[b]] - points[shared[a]]).two_norm2())
258 const auto line = points[shared[b]] - points[shared[a]];
259 const auto lineLength = line.two_norm();
260 return std::any_of(shared.begin(), shared.end(), [&](
int c)
261 { return crossProduct(line, points[c] - points[shared[a]]).two_norm() > 1e-6*magnitude*lineLength; });
268 for (
int i = 0; i < numPoints; ++i)
270 for (
int j = i+1; j < numPoints; ++j)
272 for (
int k = j+1; k < numPoints; ++k)
275 if (isOnFace(i, j, k))
278 const auto pointI = points[i];
279 const auto ab = points[j] - pointI;
280 const auto ac = points[k] - pointI;
285 if (
normal.two_norm2() < eps2*eps2)
291 face.assign({ i, j, k });
292 const bool isAdmissible = [&]()
295 for (
int m = 0; m < numPoints; ++m)
297 if (m != i && m != j && m != k)
300 const auto ad = points[m] - pointI;
301 const auto sp =
normal*ad;
305 using std::abs;
using std::signbit;
306 const bool coplanar = abs(sp) < epsDist*
normal.two_norm();
307 int newMarker = coplanar ? 0 : signbit(sp) ? -1 : 1;
311 if (marker == 0 && newMarker != 0)
316 if (newMarker != 0 && marker != newMarker)
333 std::sort(face.begin(), face.end());
334 faces.push_back(face);
340 for (
bool merged =
true; merged; )
343 for (std::size_t f = 0; f < faces.size() && !merged; ++f)
345 for (std::size_t g = f+1; g < faces.size() && !merged; ++g)
347 if (isSameFace(faces[f], faces[g]))
349 std::vector<int> united;
350 std::set_union(faces[f].begin(), faces[f].end(), faces[g].begin(), faces[g].end(), std::back_inserter(united));
351 faces[f] = std::move(united);
352 faces.erase(faces.begin() + g);
360 std::vector<Tetrahedron> triangulation;
361 triangulation.reserve(numPoints);
364 const auto addTetrahedron = [&](
const Point& a,
const Point& b,
const Point& c)
368 if (signbit(
normal*(midPoint - a)))
369 triangulation.emplace_back(Tetrahedron{ a, c, b, midPoint });
371 triangulation.emplace_back(Tetrahedron{ a, b, c, midPoint });
374 std::vector<Point> facePoints;
375 for (
const auto& f : faces)
378 addTetrahedron(points[f[0]], points[f[1]], points[f[2]]);
386 facePoints.push_back(points[idx]);
388 addTetrahedron(triangle[0], triangle[1], triangle[2]);
393 if (triangulation.size() < 4)
394 DUNE_THROW(Dune::InvalidStateException,
"Something went wrong with the triangulation!");
396 return triangulation;