2016-09-30 13:23:18 +00:00
|
|
|
#include "BoundingBox.hpp"
|
2013-07-16 19:04:14 +00:00
|
|
|
#include "Polyline.hpp"
|
2020-09-14 14:27:55 +00:00
|
|
|
#include "Exception.hpp"
|
2015-01-06 19:52:36 +00:00
|
|
|
#include "ExPolygon.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>
|
2016-03-20 19:20:32 +00:00
|
|
|
#include <utility>
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
const Point& Polyline::leftmost_point() const
|
2014-04-24 14:40:10 +00:00
|
|
|
{
|
2019-09-27 16:17:21 +00:00
|
|
|
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);
|
2014-04-24 14:40:10 +00:00
|
|
|
}
|
2019-09-27 16:17:21 +00:00
|
|
|
return *p;
|
2013-09-02 18:22:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 18:01:17 +00:00
|
|
|
double Polyline::length() const
|
|
|
|
{
|
|
|
|
double l = 0;
|
|
|
|
for (size_t i = 1; i < this->points.size(); ++ i)
|
|
|
|
l += (this->points[i] - this->points[i - 1]).cast<double>().norm();
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +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
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void Polyline::clip_end(double distance)
|
2013-10-27 21:57:25 +00:00
|
|
|
{
|
|
|
|
while (distance > 0) {
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
Vec2d last_point = this->last_point().cast<double>();
|
2013-10-27 21:57:25 +00:00
|
|
|
this->points.pop_back();
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
if (this->points.empty())
|
|
|
|
break;
|
|
|
|
Vec2d v = this->last_point().cast<double>() - last_point;
|
|
|
|
double lsqr = v.squaredNorm();
|
|
|
|
if (lsqr > distance * distance) {
|
|
|
|
this->points.emplace_back((last_point + v * (distance / sqrt(lsqr))).cast<coord_t>());
|
|
|
|
return;
|
2013-10-27 21:57:25 +00:00
|
|
|
}
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
distance -= sqrt(lsqr);
|
2013-10-27 21:57:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:30:45 +00:00
|
|
|
// removes the given distance from the start of the polyline
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void Polyline::clip_start(double distance)
|
2013-11-06 18:30:45 +00:00
|
|
|
{
|
|
|
|
this->reverse();
|
|
|
|
this->clip_end(distance);
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
if (this->points.size() >= 2)
|
|
|
|
this->reverse();
|
2013-11-06 18:30:45 +00:00
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void Polyline::extend_end(double distance)
|
2014-03-15 15:53:20 +00:00
|
|
|
{
|
|
|
|
// relocate last point by extending the last segment by the specified length
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
Vec2d v = (this->points.back() - *(this->points.end() - 2)).cast<double>().normalized();
|
|
|
|
this->points.back() += (v * distance).cast<coord_t>();
|
2014-03-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void Polyline::extend_start(double distance)
|
2014-03-15 15:53:20 +00:00
|
|
|
{
|
|
|
|
// relocate first point by extending the first segment by the specified length
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
Vec2d v = (this->points.front() - this->points[1]).cast<double>().normalized();
|
|
|
|
this->points.front() += (v * distance).cast<coord_t>();
|
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 */
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +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;
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
points.emplace_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) {
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
Vec2d p1 = (it-1)->cast<double>();
|
|
|
|
Vec2d v = it->cast<double>() - p1;
|
|
|
|
double segment_length = v.norm();
|
2013-11-11 19:59:58 +00:00
|
|
|
len += segment_length;
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
if (len < distance)
|
|
|
|
continue;
|
2013-11-11 19:59:58 +00:00
|
|
|
if (len == distance) {
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
points.emplace_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
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
points.emplace_back((p1 + v * (take / v.norm())).cast<coord_t>());
|
|
|
|
-- it;
|
|
|
|
len = - take;
|
2013-11-11 19:59:58 +00:00
|
|
|
}
|
2015-01-19 17:53:04 +00:00
|
|
|
return points;
|
2013-11-11 19:59:58 +00:00
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void Polyline::simplify(double tolerance)
|
2013-11-22 01:16:10 +00:00
|
|
|
{
|
|
|
|
this->points = MultiPoint::_douglas_peucker(this->points, tolerance);
|
|
|
|
}
|
|
|
|
|
2021-09-03 14:17:24 +00:00
|
|
|
#if 0
|
|
|
|
// This method simplifies all *lines* contained in the supplied area
|
2015-01-06 19:52:36 +00:00
|
|
|
template <class T>
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void Polyline::simplify_by_visibility(const T &area)
|
2015-01-06 19:52:36 +00:00
|
|
|
{
|
|
|
|
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);
|
2021-09-03 14:17:24 +00:00
|
|
|
#endif
|
2015-01-06 19:52:36 +00:00
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void Polyline::split_at(const Point &point, Polyline* p1, Polyline* p2) const
|
2014-05-22 10:28:12 +00:00
|
|
|
{
|
2022-06-24 15:46:26 +00:00
|
|
|
if (this->size() < 2) {
|
2022-06-24 15:28:09 +00:00
|
|
|
*p1 = *this;
|
|
|
|
p2->clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:46:26 +00:00
|
|
|
if (this->points.front() == point) {
|
2022-06-27 08:42:42 +00:00
|
|
|
*p1 = { point };
|
2022-06-24 15:46:26 +00:00
|
|
|
*p2 = *this;
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:28:09 +00:00
|
|
|
auto min_dist2 = std::numeric_limits<double>::max();
|
|
|
|
auto min_point_it = this->points.cbegin();
|
|
|
|
Point prev = this->points.front();
|
|
|
|
for (auto it = this->points.cbegin() + 1; it != this->points.cend(); ++ it) {
|
2022-11-14 18:01:17 +00:00
|
|
|
Point proj;
|
|
|
|
if (double d2 = line_alg::distance_to_squared(Line(prev, *it), point, &proj); d2 < min_dist2) {
|
2022-06-24 15:28:09 +00:00
|
|
|
min_dist2 = d2;
|
|
|
|
min_point_it = it;
|
2014-05-22 10:28:12 +00:00
|
|
|
}
|
2022-06-24 15:28:09 +00:00
|
|
|
prev = *it;
|
2014-05-22 10:28:12 +00:00
|
|
|
}
|
2022-06-24 15:28:09 +00:00
|
|
|
|
|
|
|
p1->points.assign(this->points.cbegin(), min_point_it);
|
|
|
|
if (p1->points.back() != point)
|
|
|
|
p1->points.emplace_back(point);
|
2014-05-22 10:28:12 +00:00
|
|
|
|
2022-06-24 15:28:09 +00:00
|
|
|
p2->points = { point };
|
|
|
|
if (*min_point_it == point)
|
|
|
|
++ min_point_it;
|
|
|
|
p2->points.insert(p2->points.end(), min_point_it, this->points.cend());
|
2014-05-22 10:28:12 +00:00
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
bool Polyline::is_straight() const
|
2014-11-15 22:06:15 +00:00
|
|
|
{
|
2018-09-17 13:12:13 +00:00
|
|
|
// 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.)
|
2014-11-15 22:06:15 +00:00
|
|
|
double dir = Line(this->first_point(), this->last_point()).direction();
|
2018-09-17 13:12:13 +00:00
|
|
|
for (const auto &line: this->lines())
|
|
|
|
if (! line.parallel_to(dir))
|
|
|
|
return false;
|
2014-11-15 22:06:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-30 13:23:18 +00:00
|
|
|
BoundingBox get_extents(const Polyline &polyline)
|
|
|
|
{
|
|
|
|
return polyline.bounding_box();
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox get_extents(const Polylines &polylines)
|
|
|
|
{
|
|
|
|
BoundingBox bb;
|
|
|
|
if (! polylines.empty()) {
|
|
|
|
bb = polylines.front().bounding_box();
|
|
|
|
for (size_t i = 1; i < polylines.size(); ++ i)
|
2020-11-24 15:00:46 +00:00
|
|
|
bb.merge(polylines[i].points);
|
2016-09-30 13:23:18 +00:00
|
|
|
}
|
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:17:21 +00:00
|
|
|
const Point& leftmost_point(const Polylines &polylines)
|
|
|
|
{
|
|
|
|
if (polylines.empty())
|
2022-05-04 13:05:56 +00:00
|
|
|
throw Slic3r::InvalidArgument("leftmost_point() called on empty Polylines");
|
2019-09-27 16:17:21 +00:00
|
|
|
Polylines::const_iterator it = polylines.begin();
|
|
|
|
const Point *p = &it->leftmost_point();
|
|
|
|
for (++ it; it != polylines.end(); ++it) {
|
|
|
|
const Point *p2 = &it->leftmost_point();
|
|
|
|
if (p2->x() < p->x())
|
|
|
|
p = p2;
|
|
|
|
}
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:23:57 +00:00
|
|
|
bool remove_degenerate(Polylines &polylines)
|
|
|
|
{
|
|
|
|
bool modified = false;
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < polylines.size(); ++ i) {
|
|
|
|
if (polylines[i].points.size() >= 2) {
|
|
|
|
if (j < i)
|
|
|
|
std::swap(polylines[i].points, polylines[j].points);
|
|
|
|
++ j;
|
|
|
|
} else
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
if (j < polylines.size())
|
|
|
|
polylines.erase(polylines.begin() + j, polylines.end());
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:53:33 +00:00
|
|
|
std::pair<int, Point> foot_pt(const Points &polyline, const Point &pt)
|
|
|
|
{
|
|
|
|
if (polyline.size() < 2)
|
|
|
|
return std::make_pair(-1, Point(0, 0));
|
|
|
|
|
|
|
|
auto d2_min = std::numeric_limits<double>::max();
|
|
|
|
Point foot_pt_min;
|
|
|
|
Point prev = polyline.front();
|
|
|
|
auto it = polyline.begin();
|
|
|
|
auto it_proj = polyline.begin();
|
|
|
|
for (++ it; it != polyline.end(); ++ it) {
|
2022-11-14 18:01:17 +00:00
|
|
|
Point foot_pt;
|
|
|
|
if (double d2 = line_alg::distance_to_squared(Line(prev, *it), pt, &foot_pt); d2 < d2_min) {
|
2022-06-27 12:53:33 +00:00
|
|
|
d2_min = d2;
|
|
|
|
foot_pt_min = foot_pt;
|
|
|
|
it_proj = it;
|
|
|
|
}
|
|
|
|
prev = *it;
|
|
|
|
}
|
|
|
|
return std::make_pair(int(it_proj - polyline.begin()) - 1, foot_pt_min);
|
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
ThickLines ThickPolyline::thicklines() const
|
2016-03-19 14:33:58 +00:00
|
|
|
{
|
|
|
|
ThickLines lines;
|
|
|
|
if (this->points.size() >= 2) {
|
|
|
|
lines.reserve(this->points.size() - 1);
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
for (size_t i = 0; i + 1 < this->points.size(); ++ i)
|
|
|
|
lines.emplace_back(this->points[i], this->points[i + 1], this->width[2 * i], this->width[2 * i + 1]);
|
2016-03-19 14:33:58 +00:00
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2022-05-24 10:28:14 +00:00
|
|
|
// Removes the given distance from the end of the ThickPolyline
|
|
|
|
void ThickPolyline::clip_end(double distance)
|
|
|
|
{
|
|
|
|
while (distance > 0) {
|
|
|
|
Vec2d last_point = this->last_point().cast<double>();
|
|
|
|
coordf_t last_width = this->width.back();
|
|
|
|
this->points.pop_back();
|
|
|
|
this->width.pop_back();
|
|
|
|
if (this->points.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
Vec2d vec = this->last_point().cast<double>() - last_point;
|
|
|
|
coordf_t width_diff = this->width.back() - last_width;
|
|
|
|
double vec_length_sqr = vec.squaredNorm();
|
|
|
|
if (vec_length_sqr > distance * distance) {
|
|
|
|
double t = (distance / std::sqrt(vec_length_sqr));
|
|
|
|
this->points.emplace_back((last_point + vec * t).cast<coord_t>());
|
|
|
|
this->width.emplace_back(last_width + width_diff * t);
|
|
|
|
assert(this->width.size() == (this->points.size() - 1) * 2);
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
this->width.pop_back();
|
|
|
|
|
|
|
|
distance -= std::sqrt(vec_length_sqr);
|
|
|
|
}
|
|
|
|
assert(this->width.size() == (this->points.size() - 1) * 2);
|
|
|
|
}
|
|
|
|
|
2022-11-14 18:01:17 +00:00
|
|
|
double Polyline3::length() const
|
|
|
|
{
|
|
|
|
double l = 0;
|
|
|
|
for (size_t i = 1; i < this->points.size(); ++ i)
|
|
|
|
l += (this->points[i] - this->points[i - 1]).cast<double>().norm();
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:44:10 +00:00
|
|
|
Lines3 Polyline3::lines() const
|
|
|
|
{
|
|
|
|
Lines3 lines;
|
|
|
|
if (points.size() >= 2)
|
|
|
|
{
|
|
|
|
lines.reserve(points.size() - 1);
|
|
|
|
for (Points3::const_iterator it = points.begin(); it != points.end() - 1; ++it)
|
|
|
|
{
|
|
|
|
lines.emplace_back(*it, *(it + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|