2013-07-16 19:04:14 +00:00
|
|
|
#include <myinit.h>
|
|
|
|
#include "ClipperUtils.hpp"
|
|
|
|
#include "Polygon.hpp"
|
|
|
|
#include "Polyline.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2013-09-02 18:22:20 +00:00
|
|
|
Point*
|
2013-09-02 20:33:03 +00:00
|
|
|
Polygon::last_point() const
|
2013-09-02 18:22:20 +00:00
|
|
|
{
|
2013-09-02 20:33:03 +00:00
|
|
|
return new Point(this->points.front()); // last point == first point for polygons
|
2013-09-02 18:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
|
|
|
Polygon::to_SV_ref() const {
|
|
|
|
SV* sv = newSV(0);
|
|
|
|
sv_setref_pv( sv, "Slic3r::Polygon::Ref", (void*)this );
|
|
|
|
return sv;
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
SV*
|
2013-09-02 18:22:20 +00:00
|
|
|
Polygon::to_SV_clone_ref() const {
|
2013-07-16 19:04:14 +00:00
|
|
|
SV* sv = newSV(0);
|
|
|
|
sv_setref_pv( sv, "Slic3r::Polygon", new Polygon(*this) );
|
|
|
|
return sv;
|
|
|
|
}
|
|
|
|
|
|
|
|
Lines
|
|
|
|
Polygon::lines()
|
|
|
|
{
|
|
|
|
Lines lines;
|
|
|
|
for (int i = 0; i < this->points.size()-1; i++) {
|
|
|
|
lines.push_back(Line(this->points[i], this->points[i+1]));
|
|
|
|
}
|
|
|
|
lines.push_back(Line(this->points.back(), this->points.front()));
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2013-08-26 20:39:35 +00:00
|
|
|
Polyline*
|
|
|
|
Polygon::split_at(const Point* point)
|
|
|
|
{
|
|
|
|
// find index of point
|
|
|
|
for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
|
|
|
|
if ((*it).coincides_with(point)) {
|
|
|
|
return this->split_at_index(it - this->points.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw "Point not found";
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
Polyline*
|
|
|
|
Polygon::split_at_index(int index)
|
|
|
|
{
|
|
|
|
Polyline* poly = new Polyline;
|
|
|
|
for (int i = index; i < this->points.size(); i++) {
|
|
|
|
poly->points.push_back( this->points[i] );
|
|
|
|
}
|
|
|
|
for (int i = 0; i <= index; i++) {
|
|
|
|
poly->points.push_back( this->points[i] );
|
|
|
|
}
|
|
|
|
return poly;
|
|
|
|
}
|
|
|
|
|
|
|
|
Polyline*
|
|
|
|
Polygon::split_at_first_point()
|
|
|
|
{
|
|
|
|
return this->split_at_index(0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ClipperLib::Polygon p;
|
|
|
|
Slic3rPolygon_to_ClipperPolygon(*this, p);
|
|
|
|
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
|
|
|
{
|
|
|
|
ClipperLib::Polygon* p = new ClipperLib::Polygon();
|
|
|
|
Slic3rPolygon_to_ClipperPolygon(*this, *p);
|
|
|
|
bool orientation = ClipperLib::Orientation(*p);
|
|
|
|
delete p;
|
|
|
|
return orientation;
|
|
|
|
}
|
|
|
|
|
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-07-16 19:04:14 +00:00
|
|
|
}
|