The chaining and leftmost_point methods were rewritten as static methods, so they may be called on Polylines without having to convert to PolylineCollection first.

This commit is contained in:
bubnikv 2016-04-10 19:32:39 +02:00
parent 6c5c9eddba
commit d392858ee3
2 changed files with 123 additions and 38 deletions

View File

@ -2,50 +2,121 @@
namespace Slic3r { namespace Slic3r {
void struct Chaining
PolylineCollection::chained_path(PolylineCollection* retval, bool no_reverse) const
{ {
if (this->polylines.empty()) return; Point first;
this->chained_path_from(this->polylines.front().first_point(), retval, no_reverse); Point last;
} size_t idx;
};
void #ifndef sqr
PolylineCollection::chained_path_from(Point start_near, PolylineCollection* retval, bool no_reverse) const template<typename T>
inline sqr(T x) { return x * x; }
#endif /* sqr */
template<typename T>
inline int nearest_point_index(const std::vector<Chaining> &pairs, const Point &start_near, bool no_reverse)
{ {
Polylines my_paths = this->polylines; T dmin = std::numeric_limits<T>::max();
int idx = 0;
Points endpoints; for (std::vector<Chaining>::const_iterator it = pairs.begin(); it != pairs.end(); ++it) {
for (Polylines::const_iterator it = my_paths.begin(); it != my_paths.end(); ++it) { T d = sqr(T(start_near.x - it->first.x));
endpoints.push_back(it->first_point()); if (d <= dmin) {
if (no_reverse) { d += sqr(T(start_near.y - it->first.y));
endpoints.push_back(it->first_point()); if (d < dmin) {
} else { idx = (it - pairs.begin()) * 2;
endpoints.push_back(it->last_point()); dmin = d;
if (dmin < EPSILON)
break;
}
}
if (! no_reverse) {
d = sqr(T(start_near.x - it->last.x));
if (d <= dmin) {
d += sqr(T(start_near.y - it->last.y));
if (d < dmin) {
idx = (it - pairs.begin()) * 2 + 1;
dmin = d;
if (dmin < EPSILON)
break;
}
}
} }
} }
while (!my_paths.empty()) { return idx;
}
Polylines PolylineCollection::chained_path_from(
#if SLIC3R_CPPVER > 11
Polylines &&src,
#else
const Polylines &src,
#endif
Point start_near,
bool no_reverse)
{
std::vector<Chaining> endpoints;
endpoints.reserve(src.size());
for (size_t i = 0; i < src.size(); ++ i) {
Chaining c;
c.first = src[i].first_point();
if (! no_reverse)
c.last = src[i].last_point();
c.idx = i;
}
Polylines retval;
while (! endpoints.empty()) {
// find nearest point // find nearest point
int start_index = start_near.nearest_point_index(endpoints); int endpoint_index = nearest_point_index<double>(endpoints, start_near, no_reverse);
int path_index = start_index/2; #if SLIC3R_CPPVER > 11
if (start_index % 2 && !no_reverse) { retval.push_back(std::move(src[endpoints[endpoint_index/2].idx]));
my_paths.at(path_index).reverse(); #else
} retval.push_back(src[endpoints[endpoint_index/2].idx]);
retval->polylines.push_back(my_paths.at(path_index)); #endif
my_paths.erase(my_paths.begin() + path_index); if (endpoint_index & 1)
endpoints.erase(endpoints.begin() + 2*path_index, endpoints.begin() + 2*path_index + 2); retval.back().reverse();
start_near = retval->polylines.back().last_point(); endpoints.erase(endpoints.begin() + endpoint_index/2);
start_near = retval.back().last_point();
} }
} }
Point #if SLIC3R_CPPVER > 11
PolylineCollection::leftmost_point() const Polylines PolylineCollection::chained_path(Polylines &&src, bool no_reverse)
{ {
if (this->polylines.empty()) CONFESS("leftmost_point() called on empty PolylineCollection"); return (src.empty() || src.front().empty()) ?
Point p = this->polylines.front().leftmost_point(); Polylines() :
for (Polylines::const_iterator it = this->polylines.begin() + 1; it != this->polylines.end(); ++it) { chained_path_from(std::move(src), src.front().first_point(), no_reverse);
}
Polylines PolylineCollection::chained_path_from(Polylines src, Point start_near, bool no_reverse)
{
return chained_path_from(std::move(src), start_near, no_reverse);
}
Polylines PolylineCollection::chained_path(Polylines src, bool no_reverse)
{
return (src.empty() || src.front().empty()) ?
Polylines() :
chained_path_from(std::move(src), src.front().first_point(), no_reverse);
}
#else
Polylines PolylineCollection::chained_path(const Polylines &src, bool no_reverse)
{
return (src.empty() || src.front().points.empty()) ?
Polylines() :
chained_path_from(src, src.front().first_point(), no_reverse);
}
#endif
Point PolylineCollection::leftmost_point(const Polylines &polylines)
{
if (polylines.empty()) CONFESS("leftmost_point() called on empty PolylineCollection");
Polylines::const_iterator it = polylines.begin();
Point p = it->leftmost_point();
for (++ it; it != polylines.end(); ++it) {
Point p2 = it->leftmost_point(); Point p2 = it->leftmost_point();
if (p2.x < p.x) p = p2; if (p2.x < p.x)
p = p2;
} }
return p; return p;
} }
@ -56,4 +127,4 @@ PolylineCollection::append(const Polylines &pp)
this->polylines.insert(this->polylines.end(), pp.begin(), pp.end()); this->polylines.insert(this->polylines.end(), pp.begin(), pp.end());
} }
} } // namespace Slic3r

View File

@ -8,12 +8,26 @@ namespace Slic3r {
class PolylineCollection class PolylineCollection
{ {
public: public:
Polylines polylines; Polylines polylines;
void chained_path(PolylineCollection* retval, bool no_reverse = false) const; void chained_path(PolylineCollection* retval, bool no_reverse = false) const
void chained_path_from(Point start_near, PolylineCollection* retval, bool no_reverse = false) const; { retval->polylines = chained_path(this->polylines, no_reverse); }
Point leftmost_point() const; void chained_path_from(Point start_near, PolylineCollection* retval, bool no_reverse = false) const
{ retval->polylines = chained_path_from(this->polylines, start_near, no_reverse); }
Point leftmost_point() const
{ return leftmost_point(polylines); }
void append(const Polylines &polylines); void append(const Polylines &polylines);
static Point leftmost_point(const Polylines &polylines);
#if SLIC3R_CPPVER > 11
static Polylines chained_path(Polylines &&src, bool no_reverse = false);
static Polylines chained_path_from(Polylines &&src, Point start_near, bool no_reverse = false);
static Polylines chained_path(Polylines src, bool no_reverse = false);
static Polylines chained_path_from(Polylines src, Point start_near, bool no_reverse = false);
#else
static Polylines chained_path(const Polylines &src, bool no_reverse = false);
static Polylines chained_path_from(const Polylines &src, Point start_near, bool no_reverse = false);
#endif
}; };
} }