18 const std::vector<Eigen::Vector2d>& contour)
20 using V=Eigen::Vector2d;
21 std::vector<std::array<V,3>> triangles;
26 const V origin=contour.front();
28 for(
const auto& point : contour)
30 scale=std::max(scale,(point-origin).norm());
36 const double eps=128.0*std::numeric_limits<double>::epsilon();
37 const auto cross=[](
const V&
a,
const V&
b)
39 return a.x()*
b.y()-
a.y()*
b.x();
44 double x(
double y)
const
46 return a.x()+(
y-
a.y())*(
b.x()-
a.x())/(
b.y()-
a.y());
49 std::vector<Edge>
edges;
50 std::vector<double> levels;
51 for(
size_t i=0;
i<contour.size();++
i)
53 const V a=(contour[
i]-origin)/scale;
54 const V b=(contour[(
i+1)%contour.size()]-origin)/scale;
55 levels.push_back(
a.y());
56 if(std::abs(
b.y()-
a.y())>eps)
80 std::sort(levels.begin(),levels.end());
81 const auto append=[&](
const V&
a,
const V&
b,
const V&
c)
83 if(cross(
b-
a,
c-
a)>eps)
85 triangles.push_back({origin+scale*
a,origin+scale*
b,origin+scale*
c});
88 for(
size_t k=1;
k<levels.size();++
k)
90 const double low=levels[
k-1];
91 const double high=levels[
k];
96 const double mid=(low+high)*0.5;
97 std::vector<const Edge*> active;
98 for(
const auto& edge :
edges)
100 if(mid>std::min(edge.a.y(),edge.b.y()) && mid<std::max(edge.a.y(),edge.b.y()))
102 active.push_back(&edge);
105 std::stable_sort(active.begin(),active.end(),[&](
const Edge*
a,
const Edge*
b)
107 return a->x(mid)<b->x(mid);
110 for(
size_t i=0;
i+1<active.size();++
i)
112 winding+=active[
i]->b.y()>active[
i]->a.y()?1:-1;
113 if(winding!=0 && active[
i+1]->
x(mid)-active[
i]->
x(mid)>eps)
115 const V a(active[
i]->
x(low),low);
116 const V b(active[
i+1]->
x(low),low);
117 const V c(active[
i+1]->
x(high),high);
118 const V d(active[
i]->
x(high),high);