2
#include "ClipperUtils.hpp"
4
#include "Polyline.hpp"
8
Polygon::operator Polygons() const
15
Polygon::operator Polyline() const
18
this->split_at_first_point(&polyline);
23
Polygon::operator[](Points::size_type idx)
25
return this->points[idx];
29
Polygon::operator[](Points::size_type idx) const
31
return this->points[idx];
35
Polygon::last_point() const
37
return this->points.front(); // last point == first point for polygons
41
Polygon::lines() const
49
Polygon::lines(Lines* lines) const
51
lines->reserve(lines->size() + this->points.size());
52
for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) {
53
lines->push_back(Line(*it, *(it + 1)));
55
lines->push_back(Line(this->points.back(), this->points.front()));
59
Polygon::split_at_vertex(const Point &point, Polyline* polyline) const
61
// find index of point
62
for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
63
if (it->coincides_with(point)) {
64
this->split_at_index(it - this->points.begin(), polyline);
68
CONFESS("Point not found");
72
Polygon::split_at_index(int index, Polyline* polyline) const
74
polyline->points.reserve(this->points.size() + 1);
75
for (Points::const_iterator it = this->points.begin() + index; it != this->points.end(); ++it)
76
polyline->points.push_back(*it);
77
for (Points::const_iterator it = this->points.begin(); it != this->points.begin() + index + 1; ++it)
78
polyline->points.push_back(*it);
82
Polygon::split_at_first_point(Polyline* polyline) const
84
this->split_at_index(0, polyline);
88
Polygon::equally_spaced_points(double distance, Points* points) const
91
this->split_at_first_point(&polyline);
92
polyline.equally_spaced_points(distance, points);
99
Slic3rMultiPoint_to_ClipperPath(*this, p);
100
return ClipperLib::Area(p);
104
Polygon::is_counter_clockwise() const
107
Slic3rMultiPoint_to_ClipperPath(*this, p);
108
return ClipperLib::Orientation(p);
112
Polygon::is_clockwise() const
114
return !this->is_counter_clockwise();
118
Polygon::make_counter_clockwise()
120
if (!this->is_counter_clockwise()) {
128
Polygon::make_clockwise()
130
if (this->is_counter_clockwise()) {
138
Polygon::is_valid() const
140
return this->points.size() >= 3;
144
Polygon::contains_point(const Point &point) const
146
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
148
Points::const_iterator i = this->points.begin();
149
Points::const_iterator j = this->points.end() - 1;
150
for (; i != this->points.end(); j = i++) {
151
if ( ((i->y > point.y) != (j->y > point.y))
152
&& ((double)point.x < (double)(j->x - i->x) * (double)(point.y - i->y) / (double)(j->y - i->y) + (double)i->x) )
159
Polygon::simplify(double tolerance) const
162
p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
166
simplify_polygons(pp, pp);
171
Polygon::simplify(double tolerance, Polygons &polygons) const
173
Polygons pp = this->simplify(tolerance);
174
polygons.reserve(polygons.size() + pp.size());
175
polygons.insert(polygons.end(), pp.begin(), pp.end());
178
// Only call this on convex polygons or it will return invalid results
180
Polygon::triangulate_convex(Polygons* polygons) const
182
for (Points::const_iterator it = this->points.begin() + 2; it != this->points.end(); ++it) {
185
p.points.push_back(this->points.front());
186
p.points.push_back(*(it-1));
187
p.points.push_back(*it);
189
// this should be replaced with a more efficient call to a merge_collinear_segments() method
190
if (p.area() > 0) polygons->push_back(p);
196
Polygon::centroid() const
198
double area_temp = this->area();
203
this->split_at_first_point(&polyline);
204
for (Points::const_iterator point = polyline.points.begin(); point != polyline.points.end() - 1; ++point) {
205
x_temp += (double)( point->x + (point+1)->x ) * ( (double)point->x*(point+1)->y - (double)(point+1)->x*point->y );
206
y_temp += (double)( point->y + (point+1)->y ) * ( (double)point->x*(point+1)->y - (double)(point+1)->x*point->y );
209
return Point(x_temp/(6*area_temp), y_temp/(6*area_temp));
213
REGISTER_CLASS(Polygon, "Polygon");
216
Polygon::from_SV_check(SV* poly_sv)
218
if (sv_isobject(poly_sv) && !sv_isa(poly_sv, perl_class_name(this)) && !sv_isa(poly_sv, perl_class_name_ref(this)))
219
CONFESS("Not a valid %s object", perl_class_name(this));
221
MultiPoint::from_SV_check(poly_sv);