1 #ifndef slic3r_ExPolygon_hpp_
2 #define slic3r_ExPolygon_hpp_
3 
4 #include "libslic3r.h"
5 #include "Polygon.hpp"
6 #include "Polyline.hpp"
7 #include <vector>
8 
9 // polygon class of the polypartition library
10 class TPPLPoly;
11 
12 namespace Slic3r {
13 
14 class ExPolygon;
15 typedef std::vector<ExPolygon> ExPolygons;
16 
17 class ExPolygon
18 {
19 public:
20     ExPolygon() = default;
21 	ExPolygon(const ExPolygon &other) = default;
22     ExPolygon(ExPolygon &&other) = default;
ExPolygon(const Polygon & contour)23 	explicit ExPolygon(const Polygon &contour) : contour(contour) {}
ExPolygon(Polygon && contour)24 	explicit ExPolygon(Polygon &&contour) : contour(std::move(contour)) {}
ExPolygon(const Points & contour)25 	explicit ExPolygon(const Points &contour) : contour(contour) {}
ExPolygon(Points && contour)26 	explicit ExPolygon(Points &&contour) : contour(std::move(contour)) {}
ExPolygon(const Polygon & contour,const Polygon & hole)27 	explicit ExPolygon(const Polygon &contour, const Polygon &hole) : contour(contour) { holes.emplace_back(hole); }
ExPolygon(Polygon && contour,Polygon && hole)28 	explicit ExPolygon(Polygon &&contour, Polygon &&hole) : contour(std::move(contour)) { holes.emplace_back(std::move(hole)); }
ExPolygon(const Points & contour,const Points & hole)29 	explicit ExPolygon(const Points &contour, const Points &hole) : contour(contour) { holes.emplace_back(hole); }
ExPolygon(Points && contour,Polygon && hole)30 	explicit ExPolygon(Points &&contour, Polygon &&hole) : contour(std::move(contour)) { holes.emplace_back(std::move(hole)); }
ExPolygon(std::initializer_list<Point> contour)31 	ExPolygon(std::initializer_list<Point> contour) : contour(contour) {}
ExPolygon(std::initializer_list<Point> contour,std::initializer_list<Point> hole)32 	ExPolygon(std::initializer_list<Point> contour, std::initializer_list<Point> hole) : contour(contour), holes({ hole }) {}
33 
34     ExPolygon& operator=(const ExPolygon &other) = default;
35     ExPolygon& operator=(ExPolygon &&other) = default;
36 
37     Polygon  contour;
38     Polygons holes;
39 
40     operator Points() const;
41     operator Polygons() const;
42     operator Polylines() const;
clear()43     void clear() { contour.points.clear(); holes.clear(); }
44     void scale(double factor);
translate(double x,double y)45     void translate(double x, double y) { this->translate(Point(coord_t(x), coord_t(y))); }
46     void translate(const Point &vector);
47     void rotate(double angle);
48     void rotate(double angle, const Point &center);
49     double area() const;
empty() const50     bool empty() const { return contour.points.empty(); }
51     bool is_valid() const;
52 
53     // Contains the line / polyline / polylines etc COMPLETELY.
54     bool contains(const Line &line) const;
55     bool contains(const Polyline &polyline) const;
56     bool contains(const Polylines &polylines) const;
57     bool contains(const Point &point) const;
58     bool contains_b(const Point &point) const;
59     bool has_boundary_point(const Point &point) const;
60 
61     // Does this expolygon overlap another expolygon?
62     // Either the ExPolygons intersect, or one is fully inside the other,
63     // and it is not inside a hole of the other expolygon.
64     bool overlaps(const ExPolygon &other) const;
65 
66     void simplify_p(double tolerance, Polygons* polygons) const;
67     Polygons simplify_p(double tolerance) const;
68     ExPolygons simplify(double tolerance) const;
69     void simplify(double tolerance, ExPolygons* expolygons) const;
70     void medial_axis(double max_width, double min_width, ThickPolylines* polylines) const;
71     void medial_axis(double max_width, double min_width, Polylines* polylines) const;
72 //    void get_trapezoids(Polygons* polygons) const;
73 //    void get_trapezoids(Polygons* polygons, double angle) const;
74     void get_trapezoids2(Polygons* polygons) const;
75     void get_trapezoids2(Polygons* polygons, double angle) const;
76     void triangulate(Polygons* polygons) const;
77     // Triangulate into triples of points.
78     void triangulate_pp(Points *triangles) const;
79     void triangulate_p2t(Polygons* polygons) const;
80     Lines lines() const;
81 
82     // Number of contours (outer contour with holes).
num_contours() const83     size_t   		num_contours() const { return this->holes.size() + 1; }
contour_or_hole(size_t idx)84     Polygon& 		contour_or_hole(size_t idx) 		{ return (idx == 0) ? this->contour : this->holes[idx - 1]; }
contour_or_hole(size_t idx) const85     const Polygon& 	contour_or_hole(size_t idx) const 	{ return (idx == 0) ? this->contour : this->holes[idx - 1]; }
86 };
87 
operator ==(const ExPolygon & lhs,const ExPolygon & rhs)88 inline bool operator==(const ExPolygon &lhs, const ExPolygon &rhs) { return lhs.contour == rhs.contour && lhs.holes == rhs.holes; }
operator !=(const ExPolygon & lhs,const ExPolygon & rhs)89 inline bool operator!=(const ExPolygon &lhs, const ExPolygon &rhs) { return lhs.contour != rhs.contour || lhs.holes != rhs.holes; }
90 
91 // Count a nuber of polygons stored inside the vector of expolygons.
92 // Useful for allocating space for polygons when converting expolygons to polygons.
number_polygons(const ExPolygons & expolys)93 inline size_t number_polygons(const ExPolygons &expolys)
94 {
95     size_t n_polygons = 0;
96     for (ExPolygons::const_iterator it = expolys.begin(); it != expolys.end(); ++ it)
97         n_polygons += it->holes.size() + 1;
98     return n_polygons;
99 }
100 
to_lines(const ExPolygon & src)101 inline Lines to_lines(const ExPolygon &src)
102 {
103     size_t n_lines = src.contour.points.size();
104     for (size_t i = 0; i < src.holes.size(); ++ i)
105         n_lines += src.holes[i].points.size();
106     Lines lines;
107     lines.reserve(n_lines);
108     for (size_t i = 0; i <= src.holes.size(); ++ i) {
109         const Polygon &poly = (i == 0) ? src.contour : src.holes[i - 1];
110         for (Points::const_iterator it = poly.points.begin(); it != poly.points.end()-1; ++it)
111             lines.push_back(Line(*it, *(it + 1)));
112         lines.push_back(Line(poly.points.back(), poly.points.front()));
113     }
114     return lines;
115 }
116 
to_lines(const ExPolygons & src)117 inline Lines to_lines(const ExPolygons &src)
118 {
119     size_t n_lines = 0;
120     for (ExPolygons::const_iterator it_expoly = src.begin(); it_expoly != src.end(); ++ it_expoly) {
121         n_lines += it_expoly->contour.points.size();
122         for (size_t i = 0; i < it_expoly->holes.size(); ++ i)
123             n_lines += it_expoly->holes[i].points.size();
124     }
125     Lines lines;
126     lines.reserve(n_lines);
127     for (ExPolygons::const_iterator it_expoly = src.begin(); it_expoly != src.end(); ++ it_expoly) {
128         for (size_t i = 0; i <= it_expoly->holes.size(); ++ i) {
129             const Points &points = ((i == 0) ? it_expoly->contour : it_expoly->holes[i - 1]).points;
130             for (Points::const_iterator it = points.begin(); it != points.end()-1; ++it)
131                 lines.push_back(Line(*it, *(it + 1)));
132             lines.push_back(Line(points.back(), points.front()));
133         }
134     }
135     return lines;
136 }
137 
to_polylines(const ExPolygon & src)138 inline Polylines to_polylines(const ExPolygon &src)
139 {
140     Polylines polylines;
141     polylines.assign(src.holes.size() + 1, Polyline());
142     size_t idx = 0;
143     Polyline &pl = polylines[idx ++];
144     pl.points = src.contour.points;
145     pl.points.push_back(pl.points.front());
146     for (Polygons::const_iterator ith = src.holes.begin(); ith != src.holes.end(); ++ith) {
147         Polyline &pl = polylines[idx ++];
148         pl.points = ith->points;
149         pl.points.push_back(ith->points.front());
150     }
151     assert(idx == polylines.size());
152     return polylines;
153 }
154 
to_polylines(const ExPolygons & src)155 inline Polylines to_polylines(const ExPolygons &src)
156 {
157     Polylines polylines;
158     polylines.assign(number_polygons(src), Polyline());
159     size_t idx = 0;
160     for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++it) {
161         Polyline &pl = polylines[idx ++];
162         pl.points = it->contour.points;
163         pl.points.push_back(pl.points.front());
164         for (Polygons::const_iterator ith = it->holes.begin(); ith != it->holes.end(); ++ith) {
165             Polyline &pl = polylines[idx ++];
166             pl.points = ith->points;
167             pl.points.push_back(ith->points.front());
168         }
169     }
170     assert(idx == polylines.size());
171     return polylines;
172 }
173 
to_polylines(ExPolygon && src)174 inline Polylines to_polylines(ExPolygon &&src)
175 {
176     Polylines polylines;
177     polylines.assign(src.holes.size() + 1, Polyline());
178     size_t idx = 0;
179     Polyline &pl = polylines[idx ++];
180     pl.points = std::move(src.contour.points);
181     pl.points.push_back(pl.points.front());
182     for (Polygons::const_iterator ith = src.holes.begin(); ith != src.holes.end(); ++ith) {
183         Polyline &pl = polylines[idx ++];
184         pl.points = std::move(ith->points);
185         pl.points.push_back(ith->points.front());
186     }
187     assert(idx == polylines.size());
188     return polylines;
189 }
190 
to_polylines(ExPolygons && src)191 inline Polylines to_polylines(ExPolygons &&src)
192 {
193     Polylines polylines;
194     polylines.assign(number_polygons(src), Polyline());
195     size_t idx = 0;
196     for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++it) {
197         Polyline &pl = polylines[idx ++];
198         pl.points = std::move(it->contour.points);
199         pl.points.push_back(pl.points.front());
200         for (Polygons::const_iterator ith = it->holes.begin(); ith != it->holes.end(); ++ith) {
201             Polyline &pl = polylines[idx ++];
202             pl.points = std::move(ith->points);
203             pl.points.push_back(ith->points.front());
204         }
205     }
206     assert(idx == polylines.size());
207     return polylines;
208 }
209 
to_polygons(const ExPolygon & src)210 inline Polygons to_polygons(const ExPolygon &src)
211 {
212     Polygons polygons;
213     polygons.reserve(src.holes.size() + 1);
214     polygons.push_back(src.contour);
215     polygons.insert(polygons.end(), src.holes.begin(), src.holes.end());
216     return polygons;
217 }
218 
to_polygons(const ExPolygons & src)219 inline Polygons to_polygons(const ExPolygons &src)
220 {
221     Polygons polygons;
222     polygons.reserve(number_polygons(src));
223     for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++it) {
224         polygons.push_back(it->contour);
225         polygons.insert(polygons.end(), it->holes.begin(), it->holes.end());
226     }
227     return polygons;
228 }
229 
to_polygons(ExPolygon && src)230 inline Polygons to_polygons(ExPolygon &&src)
231 {
232     Polygons polygons;
233     polygons.reserve(src.holes.size() + 1);
234     polygons.push_back(std::move(src.contour));
235     std::move(std::begin(src.holes), std::end(src.holes), std::back_inserter(polygons));
236     src.holes.clear();
237     return polygons;
238 }
239 
to_polygons(ExPolygons && src)240 inline Polygons to_polygons(ExPolygons &&src)
241 {
242     Polygons polygons;
243     polygons.reserve(number_polygons(src));
244     for (ExPolygons::iterator it = src.begin(); it != src.end(); ++it) {
245         polygons.push_back(std::move(it->contour));
246         std::move(std::begin(it->holes), std::end(it->holes), std::back_inserter(polygons));
247         it->holes.clear();
248     }
249     return polygons;
250 }
251 
polygons_append(Polygons & dst,const ExPolygon & src)252 inline void polygons_append(Polygons &dst, const ExPolygon &src)
253 {
254     dst.reserve(dst.size() + src.holes.size() + 1);
255     dst.push_back(src.contour);
256     dst.insert(dst.end(), src.holes.begin(), src.holes.end());
257 }
258 
polygons_append(Polygons & dst,const ExPolygons & src)259 inline void polygons_append(Polygons &dst, const ExPolygons &src)
260 {
261     dst.reserve(dst.size() + number_polygons(src));
262     for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++ it) {
263         dst.push_back(it->contour);
264         dst.insert(dst.end(), it->holes.begin(), it->holes.end());
265     }
266 }
267 
polygons_append(Polygons & dst,ExPolygon && src)268 inline void polygons_append(Polygons &dst, ExPolygon &&src)
269 {
270     dst.reserve(dst.size() + src.holes.size() + 1);
271     dst.push_back(std::move(src.contour));
272     std::move(std::begin(src.holes), std::end(src.holes), std::back_inserter(dst));
273     src.holes.clear();
274 }
275 
polygons_append(Polygons & dst,ExPolygons && src)276 inline void polygons_append(Polygons &dst, ExPolygons &&src)
277 {
278     dst.reserve(dst.size() + number_polygons(src));
279     for (ExPolygons::iterator it = src.begin(); it != src.end(); ++ it) {
280         dst.push_back(std::move(it->contour));
281         std::move(std::begin(it->holes), std::end(it->holes), std::back_inserter(dst));
282         it->holes.clear();
283     }
284 }
285 
expolygons_append(ExPolygons & dst,const ExPolygons & src)286 inline void expolygons_append(ExPolygons &dst, const ExPolygons &src)
287 {
288     dst.insert(dst.end(), src.begin(), src.end());
289 }
290 
expolygons_append(ExPolygons & dst,ExPolygons && src)291 inline void expolygons_append(ExPolygons &dst, ExPolygons &&src)
292 {
293     if (dst.empty()) {
294         dst = std::move(src);
295     } else {
296         std::move(std::begin(src), std::end(src), std::back_inserter(dst));
297         src.clear();
298     }
299 }
300 
expolygons_rotate(ExPolygons & expolys,double angle)301 inline void expolygons_rotate(ExPolygons &expolys, double angle)
302 {
303     for (ExPolygons::iterator p = expolys.begin(); p != expolys.end(); ++p)
304         p->rotate(angle);
305 }
306 
expolygons_contain(ExPolygons & expolys,const Point & pt)307 inline bool expolygons_contain(ExPolygons &expolys, const Point &pt)
308 {
309     for (ExPolygons::iterator p = expolys.begin(); p != expolys.end(); ++p)
310         if (p->contains(pt))
311             return true;
312     return false;
313 }
314 
expolygons_simplify(const ExPolygons & expolys,double tolerance)315 inline ExPolygons expolygons_simplify(const ExPolygons &expolys, double tolerance)
316 {
317 	ExPolygons out;
318 	out.reserve(expolys.size());
319 	for (const ExPolygon &exp : expolys)
320 		exp.simplify(tolerance, &out);
321 	return out;
322 }
323 
324 extern BoundingBox get_extents(const ExPolygon &expolygon);
325 extern BoundingBox get_extents(const ExPolygons &expolygons);
326 extern BoundingBox get_extents_rotated(const ExPolygon &poly, double angle);
327 extern BoundingBox get_extents_rotated(const ExPolygons &polygons, double angle);
328 extern std::vector<BoundingBox> get_extents_vector(const ExPolygons &polygons);
329 
330 extern bool        remove_sticks(ExPolygon &poly);
331 extern void 	   keep_largest_contour_only(ExPolygons &polygons);
332 
333 extern std::list<TPPLPoly> expoly_to_polypartition_input(const ExPolygons &expp);
334 extern std::list<TPPLPoly> expoly_to_polypartition_input(const ExPolygon &ex);
335 extern std::vector<Point> polypartition_output_to_triangles(const std::list<TPPLPoly> &output);
336 
area(const ExPolygons & polys)337 inline double area(const ExPolygons &polys)
338 {
339     double s = 0.;
340     for (auto &p : polys) s += p.area();
341 
342     return s;
343 }
344 
345 } // namespace Slic3r
346 
347 // start Boost
348 #include <boost/polygon/polygon.hpp>
349 namespace boost { namespace polygon {
350     template <>
351         struct polygon_traits<Slic3r::ExPolygon> {
352         typedef coord_t coordinate_type;
353         typedef Slic3r::Points::const_iterator iterator_type;
354         typedef Slic3r::Point point_type;
355 
356         // Get the begin iterator
begin_pointsboost::polygon::polygon_traits357         static inline iterator_type begin_points(const Slic3r::ExPolygon& t) {
358             return t.contour.points.begin();
359         }
360 
361         // Get the end iterator
end_pointsboost::polygon::polygon_traits362         static inline iterator_type end_points(const Slic3r::ExPolygon& t) {
363             return t.contour.points.end();
364         }
365 
366         // Get the number of sides of the polygon
sizeboost::polygon::polygon_traits367         static inline std::size_t size(const Slic3r::ExPolygon& t) {
368             return t.contour.points.size();
369         }
370 
371         // Get the winding direction of the polygon
windingboost::polygon::polygon_traits372         static inline winding_direction winding(const Slic3r::ExPolygon& /* t */) {
373             return unknown_winding;
374         }
375     };
376 
377     template <>
378     struct polygon_mutable_traits<Slic3r::ExPolygon> {
379         //expects stl style iterators
380         template <typename iT>
set_pointsboost::polygon::polygon_mutable_traits381         static inline Slic3r::ExPolygon& set_points(Slic3r::ExPolygon& expolygon, iT input_begin, iT input_end) {
382             expolygon.contour.points.assign(input_begin, input_end);
383             // skip last point since Boost will set last point = first point
384             expolygon.contour.points.pop_back();
385             return expolygon;
386         }
387     };
388 
389 
390     template <>
391     struct geometry_concept<Slic3r::ExPolygon> { typedef polygon_with_holes_concept type; };
392 
393     template <>
394     struct polygon_with_holes_traits<Slic3r::ExPolygon> {
395         typedef Slic3r::Polygons::const_iterator iterator_holes_type;
396         typedef Slic3r::Polygon hole_type;
begin_holesboost::polygon::polygon_with_holes_traits397         static inline iterator_holes_type begin_holes(const Slic3r::ExPolygon& t) {
398             return t.holes.begin();
399         }
end_holesboost::polygon::polygon_with_holes_traits400         static inline iterator_holes_type end_holes(const Slic3r::ExPolygon& t) {
401             return t.holes.end();
402         }
size_holesboost::polygon::polygon_with_holes_traits403         static inline unsigned int size_holes(const Slic3r::ExPolygon& t) {
404             return (int)t.holes.size();
405         }
406     };
407 
408     template <>
409     struct polygon_with_holes_mutable_traits<Slic3r::ExPolygon> {
410          template <typename iT>
set_holesboost::polygon::polygon_with_holes_mutable_traits411          static inline Slic3r::ExPolygon& set_holes(Slic3r::ExPolygon& t, iT inputBegin, iT inputEnd) {
412               t.holes.assign(inputBegin, inputEnd);
413               return t;
414          }
415     };
416 
417     //first we register CPolygonSet as a polygon set
418     template <>
419     struct geometry_concept<Slic3r::ExPolygons> { typedef polygon_set_concept type; };
420 
421     //next we map to the concept through traits
422     template <>
423     struct polygon_set_traits<Slic3r::ExPolygons> {
424         typedef coord_t coordinate_type;
425         typedef Slic3r::ExPolygons::const_iterator iterator_type;
426         typedef Slic3r::ExPolygons operator_arg_type;
427 
beginboost::polygon::polygon_set_traits428         static inline iterator_type begin(const Slic3r::ExPolygons& polygon_set) {
429             return polygon_set.begin();
430         }
431 
endboost::polygon::polygon_set_traits432         static inline iterator_type end(const Slic3r::ExPolygons& polygon_set) {
433             return polygon_set.end();
434         }
435 
436         //don't worry about these, just return false from them
cleanboost::polygon::polygon_set_traits437         static inline bool clean(const Slic3r::ExPolygons& /* polygon_set */) { return false; }
sortedboost::polygon::polygon_set_traits438         static inline bool sorted(const Slic3r::ExPolygons& /* polygon_set */) { return false; }
439     };
440 
441     template <>
442     struct polygon_set_mutable_traits<Slic3r::ExPolygons> {
443         template <typename input_iterator_type>
setboost::polygon::polygon_set_mutable_traits444         static inline void set(Slic3r::ExPolygons& expolygons, input_iterator_type input_begin, input_iterator_type input_end) {
445             expolygons.assign(input_begin, input_end);
446         }
447     };
448 } }
449 // end Boost
450 
451 #endif
452