2013-07-16 19:04:14 +00:00
|
|
|
#include "Polyline.hpp"
|
2015-01-06 19:52:36 +00:00
|
|
|
#include "ExPolygon.hpp"
|
|
|
|
#include "ExPolygonCollection.hpp"
|
2014-11-15 21:41:22 +00:00
|
|
|
#include "Line.hpp"
|
2013-11-21 16:53:50 +00:00
|
|
|
#include "Polygon.hpp"
|
2014-11-15 22:06:15 +00:00
|
|
|
#include <iostream>
|
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-11-15 21:41:22 +00:00
|
|
|
Polyline::operator Line() const
|
|
|
|
{
|
|
|
|
if (this->points.size() > 2) CONFESS("Can't convert polyline with more than two points to a line");
|
|
|
|
return Line(this->points.front(), this->points.back());
|
|
|
|
}
|
|
|
|
|
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;
|
2014-05-13 18:06:01 +00:00
|
|
|
if (this->points.size() >= 2) {
|
|
|
|
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
|
2016-03-19 14:33:58 +00:00
|
|
|
Line line(
|
|
|
|
this->points.back(),
|
|
|
|
*(this->points.end() - 2)
|
|
|
|
);
|
|
|
|
this->points.back() = line.point_at(-distance);
|
2014-03-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Polyline::extend_start(double distance)
|
|
|
|
{
|
|
|
|
// relocate first point by extending the first segment by the specified length
|
2016-03-19 14:33:58 +00:00
|
|
|
this->points.front() = Line(this->points.front(), this->points[1]).point_at(-distance);
|
2014-03-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
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 */
|
2015-01-19 17:53:04 +00:00
|
|
|
Points
|
|
|
|
Polyline::equally_spaced_points(double distance) const
|
2013-11-11 19:59:58 +00:00
|
|
|
{
|
2015-01-19 17:53:04 +00:00
|
|
|
Points points;
|
|
|
|
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) {
|
2015-01-19 17:53:04 +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);
|
2015-01-19 17:53:04 +00:00
|
|
|
points.push_back(segment.point_at(take));
|
2015-07-26 20:36:26 +00:00
|
|
|
--it;
|
2013-11-11 19:59:58 +00:00
|
|
|
len = -take;
|
|
|
|
}
|
2015-01-19 17:53:04 +00:00
|
|
|
return points;
|
2013-11-11 19:59:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 01:16:10 +00:00
|
|
|
void
|
|
|
|
Polyline::simplify(double tolerance)
|
|
|
|
{
|
|
|
|
this->points = MultiPoint::_douglas_peucker(this->points, tolerance);
|
|
|
|
}
|
|
|
|
|
2015-01-06 19:52:36 +00:00
|
|
|
/* This method simplifies all *lines* contained in the supplied area */
|
|
|
|
template <class T>
|
|
|
|
void
|
|
|
|
Polyline::simplify_by_visibility(const T &area)
|
|
|
|
{
|
|
|
|
Points &pp = this->points;
|
|
|
|
|
2015-05-18 17:28:59 +00:00
|
|
|
size_t s = 0;
|
2015-12-21 13:46:35 +00:00
|
|
|
bool did_erase = false;
|
|
|
|
for (size_t i = s+2; i < pp.size(); i = s + 2) {
|
|
|
|
if (area.contains(Line(pp[s], pp[i]))) {
|
|
|
|
pp.erase(pp.begin() + s + 1, pp.begin() + i);
|
|
|
|
did_erase = true;
|
2015-05-18 17:28:59 +00:00
|
|
|
} else {
|
2015-12-21 13:46:35 +00:00
|
|
|
++s;
|
2015-01-06 19:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-21 13:46:35 +00:00
|
|
|
if (did_erase)
|
|
|
|
this->simplify_by_visibility(area);
|
2015-01-06 19:52:36 +00:00
|
|
|
}
|
2015-01-06 20:29:32 +00:00
|
|
|
template void Polyline::simplify_by_visibility<ExPolygon>(const ExPolygon &area);
|
2015-01-06 19:52:36 +00:00
|
|
|
template void Polyline::simplify_by_visibility<ExPolygonCollection>(const ExPolygonCollection &area);
|
|
|
|
|
2014-05-22 10:28:12 +00:00
|
|
|
void
|
|
|
|
Polyline::split_at(const Point &point, Polyline* p1, Polyline* p2) const
|
|
|
|
{
|
|
|
|
if (this->points.empty()) return;
|
|
|
|
|
|
|
|
// find the line to split at
|
|
|
|
size_t line_idx = 0;
|
|
|
|
Point p = this->first_point();
|
|
|
|
double min = point.distance_to(p);
|
|
|
|
Lines lines = this->lines();
|
|
|
|
for (Lines::const_iterator line = lines.begin(); line != lines.end(); ++line) {
|
|
|
|
Point p_tmp = point.projection_onto(*line);
|
|
|
|
if (point.distance_to(p_tmp) < min) {
|
|
|
|
p = p_tmp;
|
|
|
|
min = point.distance_to(p);
|
|
|
|
line_idx = line - lines.begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create first half
|
|
|
|
p1->points.clear();
|
|
|
|
for (Lines::const_iterator line = lines.begin(); line != lines.begin() + line_idx + 1; ++line) {
|
|
|
|
if (!line->a.coincides_with(p)) p1->points.push_back(line->a);
|
|
|
|
}
|
|
|
|
// we add point instead of p because they might differ because of numerical issues
|
|
|
|
// and caller might want to rely on point belonging to result polylines
|
|
|
|
p1->points.push_back(point);
|
|
|
|
|
|
|
|
// create second half
|
|
|
|
p2->points.clear();
|
|
|
|
p2->points.push_back(point);
|
|
|
|
for (Lines::const_iterator line = lines.begin() + line_idx; line != lines.end(); ++line) {
|
2015-01-05 14:51:57 +00:00
|
|
|
p2->points.push_back(line->b);
|
2014-05-22 10:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 22:06:15 +00:00
|
|
|
bool
|
|
|
|
Polyline::is_straight() const
|
|
|
|
{
|
|
|
|
/* Check that each segment's direction is equal to the line connecting
|
|
|
|
first point and last point. (Checking each line against the previous
|
|
|
|
one would cause the error to accumulate.) */
|
|
|
|
double dir = Line(this->first_point(), this->last_point()).direction();
|
|
|
|
|
|
|
|
Lines lines = this->lines();
|
|
|
|
for (Lines::const_iterator line = lines.begin(); line != lines.end(); ++line) {
|
|
|
|
if (!line->parallel_to(dir)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Polyline::wkt() const
|
|
|
|
{
|
|
|
|
std::ostringstream wkt;
|
|
|
|
wkt << "LINESTRING((";
|
|
|
|
for (Points::const_iterator p = this->points.begin(); p != this->points.end(); ++p) {
|
|
|
|
wkt << p->x << " " << p->y;
|
|
|
|
if (p != this->points.end()-1) wkt << ",";
|
|
|
|
}
|
|
|
|
wkt << "))";
|
|
|
|
return wkt.str();
|
|
|
|
}
|
|
|
|
|
2016-03-19 14:33:58 +00:00
|
|
|
ThickLines
|
|
|
|
ThickPolyline::thicklines() const
|
|
|
|
{
|
|
|
|
ThickLines lines;
|
|
|
|
if (this->points.size() >= 2) {
|
|
|
|
lines.reserve(this->points.size() - 1);
|
|
|
|
for (size_t i = 0; i < this->points.size()-1; ++i) {
|
|
|
|
ThickLine line(this->points[i], this->points[i+1]);
|
|
|
|
line.a_width = this->width[2*i];
|
|
|
|
line.b_width = this->width[2*i+1];
|
|
|
|
lines.push_back(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|