2016-09-13 11:30:00 +00:00
|
|
|
#include "BoundingBox.hpp"
|
2013-07-16 19:04:14 +00:00
|
|
|
#include "ClipperUtils.hpp"
|
|
|
|
#include "Polygon.hpp"
|
|
|
|
#include "Polyline.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2016-11-07 21:49:11 +00:00
|
|
|
Lines Polygon::lines() const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2016-11-07 21:49:11 +00:00
|
|
|
return to_lines(*this);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
Polyline Polygon::split_at_vertex(const Point &point) const
|
2013-08-26 20:39:35 +00:00
|
|
|
{
|
|
|
|
// find index of point
|
2018-08-15 11:51:40 +00:00
|
|
|
for (const Point &pt : this->points)
|
|
|
|
if (pt == point)
|
2019-10-15 16:08:32 +00:00
|
|
|
return this->split_at_index(int(&pt - &this->points.front()));
|
2018-09-18 08:09:58 +00:00
|
|
|
throw std::invalid_argument("Point not found");
|
2015-01-19 17:53:04 +00:00
|
|
|
return Polyline();
|
2013-08-26 20:39:35 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
// Split a closed polygon into an open polyline, with the split point duplicated at both ends.
|
2019-09-27 16:17:21 +00:00
|
|
|
Polyline Polygon::split_at_index(int index) const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2015-01-19 17:53:04 +00:00
|
|
|
Polyline polyline;
|
|
|
|
polyline.points.reserve(this->points.size() + 1);
|
2013-09-13 13:19:15 +00:00
|
|
|
for (Points::const_iterator it = this->points.begin() + index; it != this->points.end(); ++it)
|
2015-01-19 17:53:04 +00:00
|
|
|
polyline.points.push_back(*it);
|
2013-09-13 13:19:15 +00:00
|
|
|
for (Points::const_iterator it = this->points.begin(); it != this->points.begin() + index + 1; ++it)
|
2015-01-19 17:53:04 +00:00
|
|
|
polyline.points.push_back(*it);
|
|
|
|
return polyline;
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 11:04:23 +00:00
|
|
|
/*
|
|
|
|
int64_t Polygon::area2x() const
|
2013-08-26 20:44:40 +00:00
|
|
|
{
|
2016-10-20 11:04:23 +00:00
|
|
|
size_t n = poly.size();
|
|
|
|
if (n < 3)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int64_t a = 0;
|
|
|
|
for (size_t i = 0, j = n - 1; i < n; ++i)
|
2018-08-17 13:53:43 +00:00
|
|
|
a += int64_t(poly[j](0) + poly[i](0)) * int64_t(poly[j](1) - poly[i](1));
|
2016-10-20 11:04:23 +00:00
|
|
|
j = i;
|
|
|
|
}
|
|
|
|
return -a * 0.5;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
double Polygon::area(const Points &points)
|
2016-10-20 11:04:23 +00:00
|
|
|
{
|
|
|
|
size_t n = points.size();
|
|
|
|
if (n < 3)
|
|
|
|
return 0.;
|
2020-04-23 16:19:03 +00:00
|
|
|
|
2016-10-20 11:04:23 +00:00
|
|
|
double a = 0.;
|
|
|
|
for (size_t i = 0, j = n - 1; i < n; ++i) {
|
2018-08-17 13:53:43 +00:00
|
|
|
a += ((double)points[j](0) + (double)points[i](0)) * ((double)points[i](1) - (double)points[j](1));
|
2016-10-20 11:04:23 +00:00
|
|
|
j = i;
|
|
|
|
}
|
|
|
|
return 0.5 * a;
|
2013-08-26 20:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
double Polygon::area() const
|
|
|
|
{
|
|
|
|
return Polygon::area(points);
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
bool Polygon::is_counter_clockwise() const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2016-12-13 18:22:23 +00:00
|
|
|
return ClipperLib::Orientation(Slic3rMultiPoint_to_ClipperPath(*this));
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
bool Polygon::is_clockwise() const
|
2013-08-26 23:26:44 +00:00
|
|
|
{
|
|
|
|
return !this->is_counter_clockwise();
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
bool Polygon::make_counter_clockwise()
|
2013-07-16 19:09:29 +00:00
|
|
|
{
|
|
|
|
if (!this->is_counter_clockwise()) {
|
|
|
|
this->reverse();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
bool Polygon::make_clockwise()
|
2013-07-16 19:09:29 +00:00
|
|
|
{
|
|
|
|
if (this->is_counter_clockwise()) {
|
|
|
|
this->reverse();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
// Does an unoriented polygon contain a point?
|
|
|
|
// Tested by counting intersections along a horizontal line.
|
2019-09-27 16:17:21 +00:00
|
|
|
bool Polygon::contains(const Point &point) const
|
2013-11-21 15:21:42 +00:00
|
|
|
{
|
|
|
|
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
|
2013-11-21 17:42:16 +00:00
|
|
|
bool result = false;
|
2013-11-21 15:21:42 +00:00
|
|
|
Points::const_iterator i = this->points.begin();
|
|
|
|
Points::const_iterator j = this->points.end() - 1;
|
|
|
|
for (; i != this->points.end(); j = i++) {
|
2018-08-17 13:53:43 +00:00
|
|
|
//FIXME this test is not numerically robust. Particularly, it does not handle horizontal segments at y == point(1) well.
|
|
|
|
// Does the ray with y == point(1) intersect this line segment?
|
2016-09-13 11:30:00 +00:00
|
|
|
#if 1
|
2018-08-17 13:53:43 +00:00
|
|
|
if ( (((*i)(1) > point(1)) != ((*j)(1) > point(1)))
|
|
|
|
&& ((double)point(0) < (double)((*j)(0) - (*i)(0)) * (double)(point(1) - (*i)(1)) / (double)((*j)(1) - (*i)(1)) + (double)(*i)(0)) )
|
2013-11-21 15:21:42 +00:00
|
|
|
result = !result;
|
2016-09-13 11:30:00 +00:00
|
|
|
#else
|
2018-08-17 13:53:43 +00:00
|
|
|
if (((*i)(1) > point(1)) != ((*j)(1) > point(1))) {
|
2016-09-13 11:30:00 +00:00
|
|
|
// Orientation predicated relative to i-th point.
|
2018-08-17 13:53:43 +00:00
|
|
|
double orient = (double)(point(0) - (*i)(0)) * (double)((*j)(1) - (*i)(1)) - (double)(point(1) - (*i)(1)) * (double)((*j)(0) - (*i)(0));
|
|
|
|
if (((*i)(1) > (*j)(1)) ? (orient > 0.) : (orient < 0.))
|
2016-09-13 11:30:00 +00:00
|
|
|
result = !result;
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:33:20 +00:00
|
|
|
// this only works on CCW polygons as CW will be ripped out by Clipper's simplify_polygons()
|
2019-09-27 16:17:21 +00:00
|
|
|
Polygons Polygon::simplify(double tolerance) const
|
2013-11-21 19:25:24 +00:00
|
|
|
{
|
2014-12-07 18:53:22 +00:00
|
|
|
// repeat first point at the end in order to apply Douglas-Peucker
|
|
|
|
// on the whole polygon
|
|
|
|
Points points = this->points;
|
|
|
|
points.push_back(points.front());
|
|
|
|
Polygon p(MultiPoint::_douglas_peucker(points, tolerance));
|
|
|
|
p.points.pop_back();
|
2013-11-21 19:25:24 +00:00
|
|
|
|
2013-11-22 01:16:10 +00:00
|
|
|
Polygons pp;
|
|
|
|
pp.push_back(p);
|
2016-12-13 18:22:23 +00:00
|
|
|
return simplify_polygons(pp);
|
2013-11-22 01:16:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
void Polygon::simplify(double tolerance, Polygons &polygons) const
|
2013-11-22 01:16:10 +00:00
|
|
|
{
|
|
|
|
Polygons pp = this->simplify(tolerance);
|
|
|
|
polygons.reserve(polygons.size() + pp.size());
|
|
|
|
polygons.insert(polygons.end(), pp.begin(), pp.end());
|
2013-11-21 19:25:24 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 14:59:36 +00:00
|
|
|
// Only call this on convex polygons or it will return invalid results
|
2019-09-27 16:17:21 +00:00
|
|
|
void Polygon::triangulate_convex(Polygons* polygons) const
|
2014-04-24 14:59:36 +00:00
|
|
|
{
|
|
|
|
for (Points::const_iterator it = this->points.begin() + 2; it != this->points.end(); ++it) {
|
|
|
|
Polygon p;
|
|
|
|
p.points.reserve(3);
|
|
|
|
p.points.push_back(this->points.front());
|
|
|
|
p.points.push_back(*(it-1));
|
|
|
|
p.points.push_back(*it);
|
2014-04-24 15:06:16 +00:00
|
|
|
|
|
|
|
// this should be replaced with a more efficient call to a merge_collinear_segments() method
|
|
|
|
if (p.area() > 0) polygons->push_back(p);
|
2014-04-24 14:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 17:34:49 +00:00
|
|
|
// center of mass
|
2019-09-27 16:17:21 +00:00
|
|
|
Point Polygon::centroid() const
|
2014-05-22 17:34:49 +00:00
|
|
|
{
|
|
|
|
double area_temp = this->area();
|
|
|
|
double x_temp = 0;
|
|
|
|
double y_temp = 0;
|
|
|
|
|
2015-01-19 17:53:04 +00:00
|
|
|
Polyline polyline = this->split_at_first_point();
|
2014-05-22 17:34:49 +00:00
|
|
|
for (Points::const_iterator point = polyline.points.begin(); point != polyline.points.end() - 1; ++point) {
|
2018-08-14 16:33:26 +00:00
|
|
|
x_temp += (double)( point->x() + (point+1)->x() ) * ( (double)point->x()*(point+1)->y() - (double)(point+1)->x()*point->y() );
|
|
|
|
y_temp += (double)( point->y() + (point+1)->y() ) * ( (double)point->x()*(point+1)->y() - (double)(point+1)->x()*point->y() );
|
2014-05-22 17:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Point(x_temp/(6*area_temp), y_temp/(6*area_temp));
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:55:51 +00:00
|
|
|
// find all concave vertices (i.e. having an internal angle greater than the supplied angle)
|
|
|
|
// (external = right side, thus we consider ccw orientation)
|
2019-09-27 16:17:21 +00:00
|
|
|
Points Polygon::concave_points(double angle) const
|
2014-11-30 20:48:50 +00:00
|
|
|
{
|
2015-01-19 17:53:04 +00:00
|
|
|
Points points;
|
2019-10-15 07:40:40 +00:00
|
|
|
angle = 2. * PI - angle + EPSILON;
|
2014-12-21 22:10:38 +00:00
|
|
|
|
2014-11-30 20:48:50 +00:00
|
|
|
// check whether first point forms a concave angle
|
2014-12-21 22:10:38 +00:00
|
|
|
if (this->points.front().ccw_angle(this->points.back(), *(this->points.begin()+1)) <= angle)
|
2015-01-19 17:53:04 +00:00
|
|
|
points.push_back(this->points.front());
|
2014-11-30 20:48:50 +00:00
|
|
|
|
|
|
|
// check whether points 1..(n-1) form concave angles
|
2019-10-15 07:40:40 +00:00
|
|
|
for (Points::const_iterator p = this->points.begin()+1; p != this->points.end()-1; ++ p)
|
|
|
|
if (p->ccw_angle(*(p-1), *(p+1)) <= angle)
|
|
|
|
points.push_back(*p);
|
2014-11-30 20:48:50 +00:00
|
|
|
|
|
|
|
// check whether last point forms a concave angle
|
2014-12-21 22:10:38 +00:00
|
|
|
if (this->points.back().ccw_angle(*(this->points.end()-2), this->points.front()) <= angle)
|
2015-01-19 17:53:04 +00:00
|
|
|
points.push_back(this->points.back());
|
|
|
|
|
|
|
|
return points;
|
2014-11-30 20:48:50 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 15:55:51 +00:00
|
|
|
// find all convex vertices (i.e. having an internal angle smaller than the supplied angle)
|
|
|
|
// (external = right side, thus we consider ccw orientation)
|
2019-09-27 16:17:21 +00:00
|
|
|
Points Polygon::convex_points(double angle) const
|
2014-11-30 20:48:50 +00:00
|
|
|
{
|
2015-01-19 17:53:04 +00:00
|
|
|
Points points;
|
2019-10-15 07:40:40 +00:00
|
|
|
angle = 2*PI - angle - EPSILON;
|
2014-12-21 22:10:38 +00:00
|
|
|
|
2014-11-30 20:48:50 +00:00
|
|
|
// check whether first point forms a convex angle
|
2014-12-21 22:10:38 +00:00
|
|
|
if (this->points.front().ccw_angle(this->points.back(), *(this->points.begin()+1)) >= angle)
|
2015-01-19 17:53:04 +00:00
|
|
|
points.push_back(this->points.front());
|
2014-11-30 20:48:50 +00:00
|
|
|
|
|
|
|
// check whether points 1..(n-1) form convex angles
|
|
|
|
for (Points::const_iterator p = this->points.begin()+1; p != this->points.end()-1; ++p) {
|
2015-01-19 17:53:04 +00:00
|
|
|
if (p->ccw_angle(*(p-1), *(p+1)) >= angle) points.push_back(*p);
|
2014-11-30 20:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check whether last point forms a convex angle
|
2014-12-21 22:10:38 +00:00
|
|
|
if (this->points.back().ccw_angle(*(this->points.end()-2), this->points.front()) >= angle)
|
2015-01-19 17:53:04 +00:00
|
|
|
points.push_back(this->points.back());
|
|
|
|
|
|
|
|
return points;
|
2014-11-30 20:48:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 09:02:29 +00:00
|
|
|
// Projection of a point onto the polygon.
|
|
|
|
Point Polygon::point_projection(const Point &point) const
|
|
|
|
{
|
|
|
|
Point proj = point;
|
|
|
|
double dmin = std::numeric_limits<double>::max();
|
|
|
|
if (! this->points.empty()) {
|
|
|
|
for (size_t i = 0; i < this->points.size(); ++ i) {
|
|
|
|
const Point &pt0 = this->points[i];
|
|
|
|
const Point &pt1 = this->points[(i + 1 == this->points.size()) ? 0 : i + 1];
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double d = (point - pt0).cast<double>().norm();
|
2017-06-08 09:02:29 +00:00
|
|
|
if (d < dmin) {
|
|
|
|
dmin = d;
|
|
|
|
proj = pt0;
|
|
|
|
}
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
d = (point - pt1).cast<double>().norm();
|
2017-06-08 09:02:29 +00:00
|
|
|
if (d < dmin) {
|
|
|
|
dmin = d;
|
|
|
|
proj = pt1;
|
|
|
|
}
|
2018-08-21 19:05:24 +00:00
|
|
|
Vec2d v1(coordf_t(pt1(0) - pt0(0)), coordf_t(pt1(1) - pt0(1)));
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
coordf_t div = v1.squaredNorm();
|
2017-06-08 09:02:29 +00:00
|
|
|
if (div > 0.) {
|
2018-08-21 19:05:24 +00:00
|
|
|
Vec2d v2(coordf_t(point(0) - pt0(0)), coordf_t(point(1) - pt0(1)));
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
coordf_t t = v1.dot(v2) / div;
|
2017-06-08 09:02:29 +00:00
|
|
|
if (t > 0. && t < 1.) {
|
2018-08-17 13:53:43 +00:00
|
|
|
Point foot(coord_t(floor(coordf_t(pt0(0)) + t * v1(0) + 0.5)), coord_t(floor(coordf_t(pt0(1)) + t * v1(1) + 0.5)));
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
d = (point - foot).cast<double>().norm();
|
2017-06-08 09:02:29 +00:00
|
|
|
if (d < dmin) {
|
|
|
|
dmin = d;
|
|
|
|
proj = foot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return proj;
|
|
|
|
}
|
|
|
|
|
2019-11-01 18:59:09 +00:00
|
|
|
BoundingBox get_extents(const Points &points)
|
|
|
|
{
|
|
|
|
return BoundingBox(points);
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
BoundingBox get_extents(const Polygon &poly)
|
|
|
|
{
|
|
|
|
return poly.bounding_box();
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox get_extents(const Polygons &polygons)
|
|
|
|
{
|
|
|
|
BoundingBox bb;
|
|
|
|
if (! polygons.empty()) {
|
2016-11-07 21:49:11 +00:00
|
|
|
bb = get_extents(polygons.front());
|
|
|
|
for (size_t i = 1; i < polygons.size(); ++ i)
|
|
|
|
bb.merge(get_extents(polygons[i]));
|
|
|
|
}
|
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox get_extents_rotated(const Polygon &poly, double angle)
|
|
|
|
{
|
|
|
|
return get_extents_rotated(poly.points, angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox get_extents_rotated(const Polygons &polygons, double angle)
|
|
|
|
{
|
|
|
|
BoundingBox bb;
|
|
|
|
if (! polygons.empty()) {
|
|
|
|
bb = get_extents_rotated(polygons.front().points, angle);
|
2016-09-13 11:30:00 +00:00
|
|
|
for (size_t i = 1; i < polygons.size(); ++ i)
|
2016-11-07 21:49:11 +00:00
|
|
|
bb.merge(get_extents_rotated(polygons[i].points, angle));
|
2016-09-13 11:30:00 +00:00
|
|
|
}
|
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
2016-11-09 14:39:12 +00:00
|
|
|
extern std::vector<BoundingBox> get_extents_vector(const Polygons &polygons)
|
|
|
|
{
|
|
|
|
std::vector<BoundingBox> out;
|
|
|
|
out.reserve(polygons.size());
|
|
|
|
for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++ it)
|
|
|
|
out.push_back(get_extents(*it));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-10-20 11:04:23 +00:00
|
|
|
static inline bool is_stick(const Point &p1, const Point &p2, const Point &p3)
|
|
|
|
{
|
|
|
|
Point v1 = p2 - p1;
|
|
|
|
Point v2 = p3 - p2;
|
2018-08-17 13:53:43 +00:00
|
|
|
int64_t dir = int64_t(v1(0)) * int64_t(v2(0)) + int64_t(v1(1)) * int64_t(v2(1));
|
2016-10-20 11:04:23 +00:00
|
|
|
if (dir > 0)
|
|
|
|
// p3 does not turn back to p1. Do not remove p2.
|
|
|
|
return false;
|
2018-08-17 13:53:43 +00:00
|
|
|
double l2_1 = double(v1(0)) * double(v1(0)) + double(v1(1)) * double(v1(1));
|
|
|
|
double l2_2 = double(v2(0)) * double(v2(0)) + double(v2(1)) * double(v2(1));
|
2016-10-20 11:04:23 +00:00
|
|
|
if (dir == 0)
|
|
|
|
// p1, p2, p3 may make a perpendicular corner, or there is a zero edge length.
|
|
|
|
// Remove p2 if it is coincident with p1 or p2.
|
|
|
|
return l2_1 == 0 || l2_2 == 0;
|
|
|
|
// p3 turns back to p1 after p2. Are p1, p2, p3 collinear?
|
|
|
|
// Calculate distance from p3 to a segment (p1, p2) or from p1 to a segment(p2, p3),
|
|
|
|
// whichever segment is longer
|
2018-08-17 13:53:43 +00:00
|
|
|
double cross = double(v1(0)) * double(v2(1)) - double(v2(0)) * double(v1(1));
|
2016-10-20 11:04:23 +00:00
|
|
|
double dist2 = cross * cross / std::max(l2_1, l2_2);
|
|
|
|
return dist2 < EPSILON * EPSILON;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool remove_sticks(Polygon &poly)
|
|
|
|
{
|
|
|
|
bool modified = false;
|
|
|
|
size_t j = 1;
|
|
|
|
for (size_t i = 1; i + 1 < poly.points.size(); ++ i) {
|
|
|
|
if (! is_stick(poly[j-1], poly[i], poly[i+1])) {
|
|
|
|
// Keep the point.
|
|
|
|
if (j < i)
|
|
|
|
poly.points[j] = poly.points[i];
|
|
|
|
++ j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (++ j < poly.points.size()) {
|
|
|
|
poly.points[j-1] = poly.points.back();
|
|
|
|
poly.points.erase(poly.points.begin() + j, poly.points.end());
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
while (poly.points.size() >= 3 && is_stick(poly.points[poly.points.size()-2], poly.points.back(), poly.points.front())) {
|
|
|
|
poly.points.pop_back();
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
while (poly.points.size() >= 3 && is_stick(poly.points.back(), poly.points.front(), poly.points[1]))
|
|
|
|
poly.points.erase(poly.points.begin());
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool remove_sticks(Polygons &polys)
|
|
|
|
{
|
|
|
|
bool modified = false;
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < polys.size(); ++ i) {
|
|
|
|
modified |= remove_sticks(polys[i]);
|
|
|
|
if (polys[i].points.size() >= 3) {
|
|
|
|
if (j < i)
|
|
|
|
std::swap(polys[i].points, polys[j].points);
|
|
|
|
++ j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < polys.size())
|
|
|
|
polys.erase(polys.begin() + j, polys.end());
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool remove_degenerate(Polygons &polys)
|
|
|
|
{
|
|
|
|
bool modified = false;
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < polys.size(); ++ i) {
|
|
|
|
if (polys[i].points.size() >= 3) {
|
|
|
|
if (j < i)
|
|
|
|
std::swap(polys[i].points, polys[j].points);
|
|
|
|
++ j;
|
|
|
|
} else
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
if (j < polys.size())
|
|
|
|
polys.erase(polys.begin() + j, polys.end());
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool remove_small(Polygons &polys, double min_area)
|
|
|
|
{
|
|
|
|
bool modified = false;
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < polys.size(); ++ i) {
|
2016-10-20 15:44:46 +00:00
|
|
|
if (std::abs(polys[i].area()) >= min_area) {
|
2016-10-20 11:04:23 +00:00
|
|
|
if (j < i)
|
|
|
|
std::swap(polys[i].points, polys[j].points);
|
|
|
|
++ j;
|
|
|
|
} else
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
if (j < polys.size())
|
|
|
|
polys.erase(polys.begin() + j, polys.end());
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2019-10-15 16:08:32 +00:00
|
|
|
void remove_collinear(Polygon &poly)
|
|
|
|
{
|
|
|
|
if (poly.points.size() > 2) {
|
|
|
|
// copy points and append both 1 and last point in place to cover the boundaries
|
|
|
|
Points pp;
|
|
|
|
pp.reserve(poly.points.size()+2);
|
|
|
|
pp.push_back(poly.points.back());
|
|
|
|
pp.insert(pp.begin()+1, poly.points.begin(), poly.points.end());
|
|
|
|
pp.push_back(poly.points.front());
|
|
|
|
// delete old points vector. Will be re-filled in the loop
|
|
|
|
poly.points.clear();
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
size_t k = 0;
|
|
|
|
while (i < pp.size()-2) {
|
|
|
|
k = i+1;
|
|
|
|
const Point &p1 = pp[i];
|
|
|
|
while (k < pp.size()-1) {
|
|
|
|
const Point &p2 = pp[k];
|
|
|
|
const Point &p3 = pp[k+1];
|
|
|
|
Line l(p1, p3);
|
|
|
|
if(l.distance_to(p2) < SCALED_EPSILON) {
|
|
|
|
k++;
|
|
|
|
} else {
|
|
|
|
if(i > 0) poly.points.push_back(p1); // implicitly removes the first point we appended above
|
|
|
|
i = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(k > pp.size()-2) break; // all remaining points are collinear and can be skipped
|
|
|
|
}
|
|
|
|
poly.points.push_back(pp[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_collinear(Polygons &polys)
|
|
|
|
{
|
|
|
|
for (Polygon &poly : polys)
|
|
|
|
remove_collinear(poly);
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|