2013-07-16 19:04:14 +00:00
|
|
|
#include <myinit.h>
|
|
|
|
#include "ClipperUtils.hpp"
|
|
|
|
#include "Polygon.hpp"
|
|
|
|
#include "Polyline.hpp"
|
2014-04-27 17:18:53 +00:00
|
|
|
#ifdef SLIC3RXS
|
|
|
|
#include "perlglue.hpp"
|
|
|
|
#endif
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-01-07 14:40:38 +00:00
|
|
|
Polygon::operator Polygons() const
|
|
|
|
{
|
|
|
|
Polygons pp;
|
|
|
|
pp.push_back(*this);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
2014-04-28 18:14:20 +00:00
|
|
|
Point&
|
|
|
|
Polygon::operator[](Points::size_type idx)
|
|
|
|
{
|
|
|
|
return this->points[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
const Point&
|
|
|
|
Polygon::operator[](Points::size_type idx) const
|
|
|
|
{
|
|
|
|
return this->points[idx];
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
Point
|
2013-09-02 20:33:03 +00:00
|
|
|
Polygon::last_point() const
|
2013-09-02 18:22:20 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
return this->points.front(); // last point == first point for polygons
|
2013-09-02 18:22:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
Lines
|
2013-09-03 17:26:58 +00:00
|
|
|
Polygon::lines() const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
Lines lines;
|
2014-01-09 16:26:39 +00:00
|
|
|
this->lines(&lines);
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Polygon::lines(Lines* lines) const
|
|
|
|
{
|
|
|
|
lines->reserve(lines->size() + this->points.size());
|
2013-09-13 13:19:15 +00:00
|
|
|
for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) {
|
2014-01-09 16:26:39 +00:00
|
|
|
lines->push_back(Line(*it, *(it + 1)));
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
2014-01-09 16:26:39 +00:00
|
|
|
lines->push_back(Line(this->points.back(), this->points.front()));
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 20:39:35 +00:00
|
|
|
Polyline*
|
2014-04-24 14:40:10 +00:00
|
|
|
Polygon::split_at(const Point &point) const
|
2013-08-26 20:39:35 +00:00
|
|
|
{
|
|
|
|
// find index of point
|
|
|
|
for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
|
2013-09-13 13:19:15 +00:00
|
|
|
if (it->coincides_with(point))
|
2013-08-26 20:39:35 +00:00
|
|
|
return this->split_at_index(it - this->points.begin());
|
|
|
|
}
|
2013-09-13 13:19:15 +00:00
|
|
|
CONFESS("Point not found");
|
|
|
|
return NULL;
|
2013-08-26 20:39:35 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
Polyline*
|
2013-09-13 13:19:15 +00:00
|
|
|
Polygon::split_at_index(int index) const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
Polyline* poly = new Polyline;
|
2013-09-13 13:19:15 +00:00
|
|
|
poly->points.reserve(this->points.size() + 1);
|
|
|
|
for (Points::const_iterator it = this->points.begin() + index; it != this->points.end(); ++it)
|
|
|
|
poly->points.push_back(*it);
|
|
|
|
for (Points::const_iterator it = this->points.begin(); it != this->points.begin() + index + 1; ++it)
|
|
|
|
poly->points.push_back(*it);
|
2013-07-16 19:04:14 +00:00
|
|
|
return poly;
|
|
|
|
}
|
|
|
|
|
|
|
|
Polyline*
|
2013-09-13 13:19:15 +00:00
|
|
|
Polygon::split_at_first_point() const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
return this->split_at_index(0);
|
|
|
|
}
|
|
|
|
|
2013-11-11 19:59:58 +00:00
|
|
|
Points
|
|
|
|
Polygon::equally_spaced_points(double distance) const
|
|
|
|
{
|
|
|
|
Polyline* polyline = this->split_at_first_point();
|
|
|
|
Points pts = polyline->equally_spaced_points(distance);
|
|
|
|
delete polyline;
|
|
|
|
return pts;
|
|
|
|
}
|
|
|
|
|
2013-08-26 20:44:40 +00:00
|
|
|
double
|
2013-08-26 20:50:26 +00:00
|
|
|
Polygon::area() const
|
2013-08-26 20:44:40 +00:00
|
|
|
{
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Path p;
|
2013-11-21 13:15:38 +00:00
|
|
|
Slic3rMultiPoint_to_ClipperPath(*this, p);
|
2013-08-26 20:44:40 +00:00
|
|
|
return ClipperLib::Area(p);
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
bool
|
2013-08-26 20:50:26 +00:00
|
|
|
Polygon::is_counter_clockwise() const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperLib::Path p;
|
|
|
|
Slic3rMultiPoint_to_ClipperPath(*this, p);
|
|
|
|
return ClipperLib::Orientation(p);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 23:26:44 +00:00
|
|
|
bool
|
|
|
|
Polygon::is_clockwise() const
|
|
|
|
{
|
|
|
|
return !this->is_counter_clockwise();
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:09:29 +00:00
|
|
|
bool
|
|
|
|
Polygon::make_counter_clockwise()
|
|
|
|
{
|
|
|
|
if (!this->is_counter_clockwise()) {
|
|
|
|
this->reverse();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Polygon::make_clockwise()
|
|
|
|
{
|
|
|
|
if (this->is_counter_clockwise()) {
|
|
|
|
this->reverse();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-26 21:27:51 +00:00
|
|
|
bool
|
|
|
|
Polygon::is_valid() const
|
|
|
|
{
|
|
|
|
return this->points.size() >= 3;
|
|
|
|
}
|
|
|
|
|
2013-11-21 15:21:42 +00:00
|
|
|
bool
|
2014-04-24 14:40:10 +00:00
|
|
|
Polygon::contains_point(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++) {
|
2014-04-24 14:40:10 +00:00
|
|
|
if ( ((i->y > point.y) != (j->y > point.y))
|
|
|
|
&& (point.x < (j->x - i->x) * (point.y - i->y) / (j->y - i->y) + i->x) )
|
2013-11-21 15:21:42 +00:00
|
|
|
result = !result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:25:24 +00:00
|
|
|
Polygons
|
|
|
|
Polygon::simplify(double tolerance) const
|
|
|
|
{
|
2013-11-22 01:16:10 +00:00
|
|
|
Polygon p = *this;
|
|
|
|
p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
|
2013-11-21 19:25:24 +00:00
|
|
|
|
2013-11-22 01:16:10 +00:00
|
|
|
Polygons pp;
|
|
|
|
pp.push_back(p);
|
|
|
|
simplify_polygons(pp, pp);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Polygon::simplify(double tolerance, Polygons &polygons) const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
void
|
|
|
|
Polygon::triangulate_convex(Polygons* polygons) const
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 12:48:40 +00:00
|
|
|
#ifdef SLIC3RXS
|
2014-04-27 17:18:53 +00:00
|
|
|
|
|
|
|
REGISTER_CLASS(Polygon, "Polygon");
|
|
|
|
|
2013-09-13 12:48:40 +00:00
|
|
|
SV*
|
|
|
|
Polygon::to_SV_ref() {
|
|
|
|
SV* sv = newSV(0);
|
2014-04-27 17:18:53 +00:00
|
|
|
sv_setref_pv( sv, perl_class_name_ref(this), (void*)this );
|
2013-09-13 12:48:40 +00:00
|
|
|
return sv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
|
|
|
Polygon::to_SV_clone_ref() const {
|
|
|
|
SV* sv = newSV(0);
|
2014-04-27 17:18:53 +00:00
|
|
|
sv_setref_pv( sv, perl_class_name(this), new Polygon(*this) );
|
2013-09-13 12:48:40 +00:00
|
|
|
return sv;
|
|
|
|
}
|
2014-01-11 23:54:10 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Polygon::from_SV_check(SV* poly_sv)
|
|
|
|
{
|
2014-04-27 17:18:53 +00:00
|
|
|
if (sv_isobject(poly_sv) && !sv_isa(poly_sv, perl_class_name(this)) && !sv_isa(poly_sv, perl_class_name_ref(this)))
|
|
|
|
CONFESS("Not a valid %s object", perl_class_name(this));
|
2014-01-11 23:54:10 +00:00
|
|
|
|
|
|
|
MultiPoint::from_SV_check(poly_sv);
|
|
|
|
}
|
2013-09-13 12:48:40 +00:00
|
|
|
#endif
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|