Ported Slic3r::Polyline::Collection

This commit is contained in:
Alessandro Ranellucci 2013-08-30 00:06:10 +02:00
parent fb82de9aaf
commit 1cfdf7e955
12 changed files with 194 additions and 46 deletions

View file

@ -12,4 +12,12 @@ Polyline::lines()
return lines;
}
SV*
Polyline::to_SV_ref() const
{
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Polyline", new Polyline(*this) );
return sv;
}
}

View file

@ -9,6 +9,7 @@ namespace Slic3r {
class Polyline : public MultiPoint {
public:
Lines lines();
SV* to_SV_ref() const;
};
typedef std::vector<Polyline> Polylines;

View file

@ -0,0 +1,46 @@
#include "PolylineCollection.hpp"
namespace Slic3r {
PolylineCollection*
PolylineCollection::chained_path(bool no_reverse) const
{
if (this->polylines.empty()) {
return new PolylineCollection ();
}
return this->chained_path_from(this->polylines.front().first_point(), no_reverse);
}
PolylineCollection*
PolylineCollection::chained_path_from(const Point* start_near, bool no_reverse) const
{
PolylineCollection* retval = new PolylineCollection;
Polylines my_paths = this->polylines;
Points endpoints;
for (Polylines::const_iterator it = my_paths.begin(); it != my_paths.end(); ++it) {
endpoints.push_back(*(*it).first_point());
if (no_reverse) {
endpoints.push_back(*(*it).first_point());
} else {
endpoints.push_back(*(*it).last_point());
}
}
while (!my_paths.empty()) {
// find nearest point
int start_index = start_near->nearest_point_index(endpoints);
int path_index = start_index/2;
if (start_index % 2 && !no_reverse) {
my_paths.at(path_index).reverse();
}
retval->polylines.push_back(my_paths.at(path_index));
my_paths.erase(my_paths.begin() + path_index);
endpoints.erase(endpoints.begin() + 2*path_index, endpoints.begin() + 2*path_index + 2);
start_near = retval->polylines.back().last_point();
}
return retval;
}
}

View file

@ -0,0 +1,19 @@
#ifndef slic3r_PolylineCollection_hpp_
#define slic3r_PolylineCollection_hpp_
#include <myinit.h>
#include "Polyline.hpp"
namespace Slic3r {
class PolylineCollection
{
public:
Polylines polylines;
PolylineCollection* chained_path(bool no_reverse) const;
PolylineCollection* chained_path_from(const Point* start_near, bool no_reverse) const;
};
}
#endif