Visual Computing Library
Loading...
Searching...
No Matches
ear_cut.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_CORE_POLYGON_EAR_CUT_H
24#define VCL_ALGORITHMS_CORE_POLYGON_EAR_CUT_H
25
26#include "projection.h"
27
28#include <vclib/concepts/mesh.h>
29#include <vclib/space/core/polygon.h>
30#include <vclib/views/mesh.h>
31
32#if __has_include(<mapbox/earcut.hpp>)
33#include <mapbox/earcut.hpp>
34#else
35// inclusion for usage of vclib without CMake - not ideal but necessary for
36// header only
37#include "../../../../../external/earcut.hpp-2.2.3/include/mapbox/earcut.hpp"
38#endif
39
40/* Structs to make working the mapbox earcut algorithm on vcl::Point2 */
41
42namespace mapbox::util {
43
44template<typename Scalar>
45struct nth<0, vcl::Point2<Scalar>>
46{
47 inline static auto get(const vcl::Point2<Scalar>& t) { return t.x(); };
48};
49
50template<typename Scalar>
51struct nth<1, vcl::Point2<Scalar>>
52{
53 inline static auto get(const vcl::Point2<Scalar>& t) { return t.y(); };
54};
55
56} // namespace mapbox::util
57
58namespace vcl {
59
91template<Point2IteratorConcept Iterator>
92std::vector<uint> earCut(Iterator begin, Iterator end)
93{
94 using PointT = Iterator::value_type;
95 using Scalar = PointT::ScalarType;
96
97 // Convert the polygon represented as a sequence of vertices to a vector
98 // of vectors of points. This is necessary because the earcut library
99 // expects the polygon to be represented as a vector of contours, where
100 // each contour is a vector of points. In this case, there is only one
101 // contour, which is represented as a vector of points.
102 std::vector<std::vector<Point2<Scalar>>> poly;
103 poly.emplace_back(begin, end);
104
105 // Use the earcut library to triangulate the polygon and return the
106 // result.
107 return mapbox::earcut<uint>(poly);
108}
109
145template<Point3IteratorConcept Iterator>
146std::vector<uint> earCut(Iterator begin, Iterator end)
147{
148 // Project the 3D polygon onto a 2D plane defined by its normal.
149 auto poly2D = project(begin, end);
150
151 // Use the ear-cut algorithm to triangulate the polygon in the 2D plane
152 // and return the result.
153 return earCut(poly2D.begin(), poly2D.end());
154}
155
166template<Range R>
167std::vector<uint> earCut(R&& range)
168{
169 return earCut(std::ranges::begin(range), std::ranges::end(range));
170}
171
192template<FaceConcept Face>
193std::vector<uint> earCut(const Face& polygon)
194{
195 using CoordType = Face::VertexType::CoordType;
196 return earCut(polygon.vertices() | views::coords);
197}
198
199} // namespace vcl
200
201#endif // VCL_ALGORITHMS_CORE_POLYGON_EAR_CUT_H
The Face class represents an Face element of the vcl::Mesh class.
Definition face.h:49
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
auto project(Iterator begin, Iterator end)
Project a 3D polygon onto a plane, and return the 2D polygon.
Definition projection.h:42
std::vector< uint > earCut(Iterator begin, Iterator end)
Triangulates a simple polygon with no holes using the ear-cutting algorithm.
Definition ear_cut.h:92
Point< Scalar, 2 > Point2
A convenience alias for a 2-dimensional Point.
Definition point.h:720