-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolyBuilder.hpp
193 lines (180 loc) · 7.9 KB
/
PolyBuilder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "GeometryTypes.hpp"
#include "GeometryUtils.hpp"
#include <list>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/circulator.h>
// Simple helper for PolyBuilder to be used as the default Polygon type
template <class Point>
class Polyline3 : public std::vector<Point>
{
bool _open;
typedef typename std::vector<Point> Base;
public:
Polyline3(bool is_open=false) : Base(), _open(is_open) {}
Polyline3(const Polyline3& p) : Base(p.begin(), p.end()), _open(p._open) {}
template <class InputIterator> Polyline3(InputIterator first, InputIterator last, bool is_open=false) : Base(first, last), _open(is_open) {}
bool is_open() const { return this->_open; }
};
template <class Point, class Polygon=Polyline3<Point>>
class PolyBuilder
{
// The only requirement for Point is that it is comparable.
// The only requirement for Polygon is that there is a constructor:
// Polygon(InputIterator begin, InputIterator pts, bool is_open)
// where InputIterator has a value_type of Point. A default is provided.
protected:
typedef std::list<Point> PartialPolygon;
typedef std::unordered_map<Point, PartialPolygon*, boost::hash<Point>> pt2partialpoly;
pt2partialpoly starters, enders; // the partial polygons
std::vector<Polygon> polys;
typedef std::pair<const Point&, const Point&> simple_seg; // just a pair of points, should be "smaller" point first then "larger" (as defined by <).
std::unordered_set<simple_seg, boost::hash<simple_seg>> have_processed; // for finding duplicates
void add_seg(const simple_seg& seg)
{
// Add a segment to the set of intersections
if (!have_processed.insert(seg).second) { return; } // already done
auto ss = starters.find(seg.first), st = starters.find(seg.second);
auto es = enders.find(seg.first), et = enders.find(seg.second);
if (ss != starters.end())
{
PartialPolygon* p = ss->second;
starters.erase(ss);
if (et != enders.end())
{
PartialPolygon* p2 = et->second;
enders.erase(et);
if (p == p2) { this->add_polygon(p, false); delete p; } // closed the polygon
else { p2->splice(p2->end(), *p); enders[p2->back()] = p2; } // combined multiple partial polygons
}
else if (st != starters.end())
{
// combined multiple partial polygons (requires reversing)
PartialPolygon* p2 = st->second;
starters.erase(st);
p->reverse();
p2->splice(p2->begin(), *p);
enders.erase(p2->front());
starters[p2->front()] = p2;
//enders[p2->back()] = p2; // already valid
}
else { p->push_front(seg.second); starters[seg.second] = p; } // add the target to the beginning of the partial polygon
}
else if (es != enders.end())
{
PartialPolygon* p = es->second;
enders.erase(es);
if (st != starters.end())
{
PartialPolygon* p2 = st->second;
starters.erase(st);
if (p == p2) { this->add_polygon(p, false); delete p; } // closed the polygon
else { p->splice(p->end(), *p2); enders[p->back()] = p; } // combined multiple partial polygons
}
else if (et != enders.end())
{
// combined multiple partial polygons (requires reversing)
PartialPolygon* p2 = et->second;
enders.erase(et);
p->reverse();
p2->splice(p2->end(), *p);
starters.erase(p2->back());
//starters[p2->front()] = p2; // already valid
enders[p2->back()] = p2;
}
else { p->push_back(seg.second); enders[seg.second] = p; } // add the target to the end of the partial polygon
}
else if (st != starters.end())
{
// add the source to the beginning of the partial polygon
PartialPolygon* p = st->second;
starters.erase(st);
p->push_front(seg.first);
starters[seg.first] = p;
}
else if (et != enders.end())
{
// add the source to the end of the partial polygon
PartialPolygon* p = et->second;
enders.erase(et);
p->push_back(seg.first);
enders[seg.first] = p;
}
else
{
// neither endpoint currently exists, create a new partial polygon
PartialPolygon* p = new PartialPolygon();
p->push_front(seg.first);
p->push_back(seg.second);
starters[seg.first] = p;
enders[seg.second] = p;
}
}
virtual void add_polygon(std::list<Point>* pts, bool is_open)
{
this->remove_collinear_points(pts);
this->polys.push_back(Polygon(pts->begin(), pts->end(), is_open));
}
inline void remove_collinear_points(std::list<Point>* pts)
{
// does nothing unless we have Point2 or Point3
typedef std::integral_constant<bool, std::is_base_of<Point2, Point>::value || std::is_base_of<Point3, Point>::value> is_point23;
this->remove_collinear_points(pts, is_point23());
}
inline void remove_collinear_points(std::list<Point>* pts, const std::false_type &) { }
void remove_collinear_points(std::list<Point>* pts, const std::true_type &)
{
typedef std::list<Point2>::iterator iter;
if (pts->size() < 3) { return; }
// The three points we are considering at one point in time are p, q, r (in that order)
// If collinear we remove q and make: <p,q,r> = <p,r,r+1>
// If not collinear we advance each: <p,q,r> = <q,r,r+1>
// We need to make sure to also consider the triplets that cross the ends (at least two to check - more if one of those is collinear)
iter r = pts->begin(), p = std::prev(pts->end()), q = r++; // <p,q,r> starts out with the last item and the first two items of the list (if cyclic)
for (; r != pts->end(); ++r) { if (CGAL::collinear(*p, *q, *r)) { q = r = pts->erase(q); } else { p = q; q = r; } }
if (pts->size() >= 3 && CGAL::collinear(*p, *q, pts->front())) { pts->erase(q); }
}
inline void fix_holes() { this->fix_holes(std::is_base_of<Polygon2, Polygon>()); } // does nothing unless we have a Polygon2
inline void fix_holes(const std::false_type &) { }
void fix_holes(const std::true_type &)
{
// Check to see which polygons are holes (and reverse them so they have negative area)
size_t n = this->polys.size();
std::vector<Bbox2> boxes; boxes.reserve(n);
std::vector<Kernel::FT> areas; areas.reserve(n);
for (auto i = this->polys.begin(), end = this->polys.end(); i != end; ++i)
{
boxes.push_back(i->bbox()); areas.push_back(i->area());
}
for (size_t i = 0; i < n; ++i)
{
bool hole = false;
for (size_t j = 0; j < n; ++j)
{
if (is_inside(boxes[j], boxes[i]) && areas[j] > areas[i] &&
this->polys[j].bounded_side(this->polys[i][0]) == CGAL::ON_BOUNDED_SIDE)
{
hole = !hole;
}
}
if (hole) { this->polys[i].reverse_orientation(); }
}
}
public:
inline void add_seg(const Point& p, const Point& q) { this->add_seg(p < q ? simple_seg(p, q) : simple_seg(q, p)); }
std::vector<Polygon>& finish_up()
{
// Add the open contours
for (auto s = starters.begin(); s != starters.end(); ++s)
{
this->add_polygon(s->second, true);
delete s->second;
}
// Possibly fix holes
this->fix_holes();
// All done
return this->polys;
}
};