2013-07-16 19:04:14 +00:00
|
|
|
#include "Polyline.hpp"
|
2013-11-21 16:53:50 +00:00
|
|
|
#include "Polygon.hpp"
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2013-11-21 16:53:50 +00:00
|
|
|
Polyline::operator Polylines() const
|
|
|
|
{
|
2014-01-16 18:02:50 +00:00
|
|
|
Polylines polylines;
|
2013-11-21 16:53:50 +00:00
|
|
|
polylines.push_back(*this);
|
|
|
|
return polylines;
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
Point
|
2013-09-02 18:22:20 +00:00
|
|
|
Polyline::last_point() const
|
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
return this->points.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point
|
|
|
|
Polyline::leftmost_point() const
|
|
|
|
{
|
|
|
|
Point p = this->points.front();
|
|
|
|
for (Points::const_iterator it = this->points.begin() + 1; it != this->points.end(); ++it) {
|
|
|
|
if (it->x < p.x) p = *it;
|
|
|
|
}
|
|
|
|
return p;
|
2013-09-02 18:22:20 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 13:19:15 +00:00
|
|
|
Lines
|
|
|
|
Polyline::lines() const
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2013-09-13 13:19:15 +00:00
|
|
|
Lines lines;
|
|
|
|
lines.reserve(this->points.size() - 1);
|
|
|
|
for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) {
|
|
|
|
lines.push_back(Line(*it, *(it + 1)));
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
2013-09-13 13:19:15 +00:00
|
|
|
return lines;
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2013-10-27 21:57:25 +00:00
|
|
|
// removes the given distance from the end of the polyline
|
|
|
|
void
|
|
|
|
Polyline::clip_end(double distance)
|
|
|
|
{
|
|
|
|
while (distance > 0) {
|
2014-04-24 14:40:10 +00:00
|
|
|
Point last_point = this->last_point();
|
2013-10-27 21:57:25 +00:00
|
|
|
this->points.pop_back();
|
|
|
|
if (this->points.empty()) break;
|
|
|
|
|
|
|
|
double last_segment_length = last_point.distance_to(this->last_point());
|
|
|
|
if (last_segment_length <= distance) {
|
|
|
|
distance -= last_segment_length;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
Line segment(last_point, this->last_point());
|
2014-03-15 15:53:20 +00:00
|
|
|
this->points.push_back(segment.point_at(distance));
|
2013-10-27 21:57:25 +00:00
|
|
|
distance = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:30:45 +00:00
|
|
|
// removes the given distance from the start of the polyline
|
|
|
|
void
|
|
|
|
Polyline::clip_start(double distance)
|
|
|
|
{
|
|
|
|
this->reverse();
|
|
|
|
this->clip_end(distance);
|
|
|
|
if (this->points.size() >= 2) this->reverse();
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:53:20 +00:00
|
|
|
void
|
|
|
|
Polyline::extend_end(double distance)
|
|
|
|
{
|
|
|
|
// relocate last point by extending the last segment by the specified length
|
|
|
|
Line line(this->points[ this->points.size()-2 ], this->points.back());
|
|
|
|
this->points.pop_back();
|
|
|
|
this->points.push_back(line.point_at(line.length() + distance));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Polyline::extend_start(double distance)
|
|
|
|
{
|
|
|
|
// relocate first point by extending the first segment by the specified length
|
|
|
|
Line line(this->points[1], this->points.front());
|
|
|
|
this->points[0] = line.point_at(line.length() + distance);
|
|
|
|
}
|
|
|
|
|
2013-11-11 19:59:58 +00:00
|
|
|
/* this method returns a collection of points picked on the polygon contour
|
|
|
|
so that they are evenly spaced according to the input distance */
|
2014-05-07 10:02:09 +00:00
|
|
|
void
|
|
|
|
Polyline::equally_spaced_points(double distance, Points* points) const
|
2013-11-11 19:59:58 +00:00
|
|
|
{
|
2014-05-07 10:02:09 +00:00
|
|
|
points->push_back(this->first_point());
|
2013-11-11 19:59:58 +00:00
|
|
|
double len = 0;
|
|
|
|
|
|
|
|
for (Points::const_iterator it = this->points.begin() + 1; it != this->points.end(); ++it) {
|
2014-04-24 14:40:10 +00:00
|
|
|
double segment_length = it->distance_to(*(it-1));
|
2013-11-11 19:59:58 +00:00
|
|
|
len += segment_length;
|
|
|
|
if (len < distance) continue;
|
|
|
|
|
|
|
|
if (len == distance) {
|
2014-05-07 10:02:09 +00:00
|
|
|
points->push_back(*it);
|
2013-11-11 19:59:58 +00:00
|
|
|
len = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
double take = segment_length - (len - distance); // how much we take of this segment
|
|
|
|
Line segment(*(it-1), *it);
|
2014-05-07 10:02:09 +00:00
|
|
|
points->push_back(segment.point_at(take));
|
2013-11-11 19:59:58 +00:00
|
|
|
it--;
|
|
|
|
len = -take;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 01:16:10 +00:00
|
|
|
void
|
|
|
|
Polyline::simplify(double tolerance)
|
|
|
|
{
|
|
|
|
this->points = MultiPoint::_douglas_peucker(this->points, tolerance);
|
|
|
|
}
|
|
|
|
|
2013-11-11 19:59:58 +00:00
|
|
|
|
2013-09-13 12:48:40 +00:00
|
|
|
#ifdef SLIC3RXS
|
2014-04-27 17:18:53 +00:00
|
|
|
REGISTER_CLASS(Polyline, "Polyline");
|
|
|
|
|
2014-01-11 23:54:10 +00:00
|
|
|
void
|
|
|
|
Polyline::from_SV_check(SV* poly_sv)
|
|
|
|
{
|
2014-04-27 17:18:53 +00:00
|
|
|
if (!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-08-29 22:06:10 +00:00
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|