New get_trapezoids() implementation. Maybe heavier but it doesn't fail with some versions of GCC like the one provided by Boost.Polygon. #1965

This commit is contained in:
Alessandro Ranellucci 2014-04-28 20:14:20 +02:00
parent d4e97d17d5
commit 2720000a17
7 changed files with 79 additions and 4 deletions

View file

@ -1,3 +1,4 @@
#include "BoundingBox.hpp"
#include "ExPolygon.hpp"
#include "Geometry.hpp"
#include "Polygon.hpp"
@ -8,6 +9,7 @@
#include "perlglue.hpp"
#endif
#include <algorithm>
#include <list>
namespace Slic3r {
@ -182,6 +184,61 @@ ExPolygon::get_trapezoids(Polygons* polygons, double angle) const
polygon->rotate(-(PI/2 - angle), Point(0,0));
}
// This algorithm may return more trapezoids than necessary
// (i.e. it may break a single trapezoid in several because
// other parts of the object have x coordinates in the middle)
void
ExPolygon::get_trapezoids2(Polygons* polygons) const
{
// get all points of this ExPolygon
Points pp = *this;
// build our bounding box
BoundingBox bb(pp);
// get all x coordinates
std::vector<coord_t> xx;
xx.reserve(pp.size());
for (Points::const_iterator p = pp.begin(); p != pp.end(); ++p)
xx.push_back(p->x);
std::sort(xx.begin(), xx.end());
// find trapezoids by looping from first to next-to-last coordinate
for (std::vector<coord_t>::const_iterator x = xx.begin(); x != xx.end()-1; ++x) {
coord_t next_x = *(x + 1);
if (*x == next_x) continue;
// build rectangle
Polygon poly;
poly.points.resize(4);
poly[0].x = *x;
poly[0].y = bb.min.y;
poly[1].x = next_x;
poly[1].y = bb.min.y;
poly[2].x = next_x;
poly[2].y = bb.max.y;
poly[3].x = *x;
poly[3].y = bb.max.y;
// intersect with this expolygon
Polygons trapezoids;
intersection(poly, *this, trapezoids);
// append results to return value
polygons->insert(polygons->end(), trapezoids.begin(), trapezoids.end());
}
}
void
ExPolygon::get_trapezoids2(Polygons* polygons, double angle) const
{
ExPolygon clone = *this;
clone.rotate(PI/2 - angle, Point(0,0));
clone.get_trapezoids2(polygons);
for (Polygons::iterator polygon = polygons->begin(); polygon != polygons->end(); ++polygon)
polygon->rotate(-(PI/2 - angle), Point(0,0));
}
void
ExPolygon::triangulate(Polygons* polygons) const
{

View file

@ -29,6 +29,8 @@ class ExPolygon
void medial_axis(double max_width, double min_width, Polylines* polylines) const;
void get_trapezoids(Polygons* polygons) const;
void get_trapezoids(Polygons* polygons, double angle) const;
void get_trapezoids2(Polygons* polygons) const;
void get_trapezoids2(Polygons* polygons, double angle) const;
void triangulate(Polygons* polygons) const;
void triangulate2(Polygons* polygons) const;

View file

@ -15,6 +15,18 @@ Polygon::operator Polygons() const
return pp;
}
Point&
Polygon::operator[](Points::size_type idx)
{
return this->points[idx];
}
const Point&
Polygon::operator[](Points::size_type idx) const
{
return this->points[idx];
}
Point
Polygon::last_point() const
{

View file

@ -15,6 +15,8 @@ typedef std::vector<Polygon> Polygons;
class Polygon : public MultiPoint {
public:
operator Polygons() const;
Point& operator[](Points::size_type idx);
const Point& operator[](Points::size_type idx) const;
Point last_point() const;
Lines lines() const;
void lines(Lines* lines) const;