2013-07-16 19:04:14 +00:00
|
|
|
#include "MultiPoint.hpp"
|
2014-05-22 17:34:49 +00:00
|
|
|
#include "BoundingBox.hpp"
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-05-22 17:34:49 +00:00
|
|
|
MultiPoint::operator Points() const
|
|
|
|
{
|
|
|
|
return this->points;
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
void MultiPoint::scale(double factor)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2018-08-14 19:33:41 +00:00
|
|
|
for (Point &pt : 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
|
|
|
pt *= factor;
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
void MultiPoint::translate(double x, double y)
|
2013-07-16 19:04:14 +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
|
|
|
Vector v(x, y);
|
2018-08-14 19:33:41 +00:00
|
|
|
for (Point &pt : 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
|
|
|
pt += v;
|
2013-07-16 19:04:14 +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 MultiPoint::translate(const Point &v)
|
2014-11-09 14:27:34 +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
|
|
|
for (Point &pt : points)
|
|
|
|
pt += v;
|
2014-11-09 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 12:02:37 +00:00
|
|
|
void MultiPoint::rotate(double cos_angle, double sin_angle)
|
2016-04-10 17:06:46 +00:00
|
|
|
{
|
2017-06-08 12:02:37 +00:00
|
|
|
for (Point &pt : this->points) {
|
2018-08-14 16:33:26 +00:00
|
|
|
double cur_x = double(pt.x());
|
|
|
|
double cur_y = double(pt.y());
|
|
|
|
pt.x() = coord_t(round(cos_angle * cur_x - sin_angle * cur_y));
|
|
|
|
pt.y() = coord_t(round(cos_angle * cur_y + sin_angle * cur_x));
|
2016-04-10 17:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
void MultiPoint::rotate(double angle, const Point ¢er)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2016-09-13 11:30:00 +00:00
|
|
|
double s = sin(angle);
|
|
|
|
double c = cos(angle);
|
2018-08-14 19:33:41 +00:00
|
|
|
for (Point &pt : points) {
|
2018-08-15 11:51:40 +00:00
|
|
|
Vec2crd v(pt - center);
|
|
|
|
pt.x() = (coord_t)round(double(center.x()) + c * v[0] - s * v[1]);
|
|
|
|
pt.y() = (coord_t)round(double(center.y()) + c * v[1] + s * v[0]);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
void MultiPoint::reverse()
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
std::reverse(this->points.begin(), this->points.end());
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
Point MultiPoint::first_point() const
|
2013-08-28 23:36:42 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
return this->points.front();
|
2013-08-28 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
2013-10-27 21:57:25 +00:00
|
|
|
double
|
|
|
|
MultiPoint::length() const
|
|
|
|
{
|
|
|
|
Lines lines = this->lines();
|
|
|
|
double len = 0;
|
|
|
|
for (Lines::iterator it = lines.begin(); it != lines.end(); ++it) {
|
|
|
|
len += it->length();
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-05-08 09:07:37 +00:00
|
|
|
int
|
|
|
|
MultiPoint::find_point(const Point &point) const
|
|
|
|
{
|
2018-08-15 11:51:40 +00:00
|
|
|
for (const Point &pt : this->points)
|
|
|
|
if (pt == point)
|
|
|
|
return &pt - &this->points.front();
|
2014-05-08 09:07:37 +00:00
|
|
|
return -1; // not found
|
|
|
|
}
|
|
|
|
|
2015-01-06 19:52:36 +00:00
|
|
|
bool
|
|
|
|
MultiPoint::has_boundary_point(const Point &point) const
|
|
|
|
{
|
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
|
|
|
double dist = (point.projection_onto(*this) - point).cast<double>().norm();
|
2015-01-06 19:52:36 +00:00
|
|
|
return dist < SCALED_EPSILON;
|
|
|
|
}
|
|
|
|
|
2015-01-19 17:53:04 +00:00
|
|
|
BoundingBox
|
|
|
|
MultiPoint::bounding_box() const
|
2014-05-22 17:34:49 +00:00
|
|
|
{
|
2015-01-19 17:53:04 +00:00
|
|
|
return BoundingBox(this->points);
|
2014-05-22 17:34:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-15 16:01:08 +00:00
|
|
|
bool
|
|
|
|
MultiPoint::has_duplicate_points() const
|
|
|
|
{
|
|
|
|
for (size_t i = 1; i < points.size(); ++i)
|
2018-08-15 11:51:40 +00:00
|
|
|
if (points[i-1] == points[i])
|
2016-04-15 16:01:08 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-25 14:21:45 +00:00
|
|
|
MultiPoint::remove_duplicate_points()
|
|
|
|
{
|
2016-04-15 16:01:08 +00:00
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 1; i < points.size(); ++i) {
|
2018-08-15 11:51:40 +00:00
|
|
|
if (points[j] == points[i]) {
|
2016-04-15 16:01:08 +00:00
|
|
|
// Just increase index i.
|
|
|
|
} else {
|
|
|
|
++ j;
|
|
|
|
if (j < i)
|
|
|
|
points[j] = points[i];
|
2015-01-25 14:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 16:01:08 +00:00
|
|
|
if (++ j < points.size()) {
|
|
|
|
points.erase(points.begin() + j, points.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-01-25 14:21:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 14:33:58 +00:00
|
|
|
bool
|
|
|
|
MultiPoint::intersection(const Line& line, Point* intersection) const
|
|
|
|
{
|
|
|
|
Lines lines = this->lines();
|
|
|
|
for (Lines::const_iterator it = lines.begin(); it != lines.end(); ++it) {
|
|
|
|
if (it->intersection(line, intersection)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-08 09:02:29 +00:00
|
|
|
bool MultiPoint::first_intersection(const Line& line, Point* intersection) const
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
double dmin = 0.;
|
|
|
|
for (const Line &l : this->lines()) {
|
|
|
|
Point ip;
|
|
|
|
if (l.intersection(line, &ip)) {
|
|
|
|
if (! found) {
|
|
|
|
found = true;
|
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
|
|
|
dmin = (line.a - ip).cast<double>().norm();
|
2017-06-08 09:02:29 +00:00
|
|
|
*intersection = ip;
|
|
|
|
} else {
|
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
|
|
|
double d = (line.a - ip).cast<double>().norm();
|
2017-06-08 09:02:29 +00:00
|
|
|
if (d < dmin) {
|
|
|
|
dmin = d;
|
|
|
|
*intersection = ip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2016-05-20 04:24:05 +00:00
|
|
|
std::string
|
|
|
|
MultiPoint::dump_perl() const
|
|
|
|
{
|
|
|
|
std::ostringstream ret;
|
|
|
|
ret << "[";
|
|
|
|
for (Points::const_iterator p = this->points.begin(); p != this->points.end(); ++p) {
|
|
|
|
ret << p->dump_perl();
|
|
|
|
if (p != this->points.end()-1) ret << ",";
|
|
|
|
}
|
|
|
|
ret << "]";
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2016-09-26 10:44:45 +00:00
|
|
|
//FIXME This is very inefficient in term of memory use.
|
|
|
|
// The recursive algorithm shall run in place, not allocating temporary data in each recursion.
|
2013-11-21 19:25:24 +00:00
|
|
|
Points
|
2013-11-22 15:01:50 +00:00
|
|
|
MultiPoint::_douglas_peucker(const Points &points, const double tolerance)
|
2013-11-21 19:25:24 +00:00
|
|
|
{
|
2016-09-26 10:44:45 +00:00
|
|
|
assert(points.size() >= 2);
|
2013-11-21 19:25:24 +00:00
|
|
|
Points results;
|
|
|
|
double dmax = 0;
|
2013-11-22 15:01:50 +00:00
|
|
|
size_t index = 0;
|
2013-11-21 19:25:24 +00:00
|
|
|
Line full(points.front(), points.back());
|
|
|
|
for (Points::const_iterator it = points.begin() + 1; it != points.end(); ++it) {
|
2015-01-03 14:03:53 +00:00
|
|
|
// we use shortest distance, not perpendicular 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
|
|
|
double d = full.distance_to(*it);
|
2013-11-21 19:25:24 +00:00
|
|
|
if (d > dmax) {
|
|
|
|
index = it - points.begin();
|
|
|
|
dmax = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dmax >= tolerance) {
|
|
|
|
Points dp0;
|
|
|
|
dp0.reserve(index + 1);
|
|
|
|
dp0.insert(dp0.end(), points.begin(), points.begin() + index + 1);
|
2016-09-26 10:44:45 +00:00
|
|
|
// Recursive call.
|
2013-11-21 19:25:24 +00:00
|
|
|
Points dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
|
|
|
|
results.reserve(results.size() + dp1.size() - 1);
|
|
|
|
results.insert(results.end(), dp1.begin(), dp1.end() - 1);
|
|
|
|
|
|
|
|
dp0.clear();
|
2016-09-26 10:44:45 +00:00
|
|
|
dp0.reserve(points.size() - index);
|
2013-11-21 19:25:24 +00:00
|
|
|
dp0.insert(dp0.end(), points.begin() + index, points.end());
|
2016-09-26 10:44:45 +00:00
|
|
|
// Recursive call.
|
2013-11-21 19:25:24 +00:00
|
|
|
dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
|
|
|
|
results.reserve(results.size() + dp1.size());
|
|
|
|
results.insert(results.end(), dp1.begin(), dp1.end());
|
|
|
|
} else {
|
|
|
|
results.push_back(points.front());
|
|
|
|
results.push_back(points.back());
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:44:10 +00:00
|
|
|
void MultiPoint3::translate(double x, double y)
|
|
|
|
{
|
2018-08-14 19:33:41 +00:00
|
|
|
for (Point3 &p : points) {
|
|
|
|
p.x() += x;
|
|
|
|
p.y() += y;
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiPoint3::translate(const Point& vector)
|
|
|
|
{
|
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
|
|
|
this->translate(vector.x(), vector.y());
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double MultiPoint3::length() const
|
|
|
|
{
|
|
|
|
double len = 0.0;
|
2018-08-14 19:33:41 +00:00
|
|
|
for (const Line3& line : this->lines())
|
2018-01-08 12:44:10 +00:00
|
|
|
len += line.length();
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox3 MultiPoint3::bounding_box() const
|
|
|
|
{
|
|
|
|
return BoundingBox3(points);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiPoint3::remove_duplicate_points()
|
|
|
|
{
|
|
|
|
size_t j = 0;
|
2018-08-15 11:51:40 +00:00
|
|
|
for (size_t i = 1; i < points.size(); ++i) {
|
|
|
|
if (points[j] == points[i]) {
|
2018-01-08 12:44:10 +00:00
|
|
|
// Just increase index i.
|
2018-08-15 11:51:40 +00:00
|
|
|
} else {
|
|
|
|
++ j;
|
2018-01-08 12:44:10 +00:00
|
|
|
if (j < i)
|
|
|
|
points[j] = points[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++j < points.size())
|
|
|
|
{
|
|
|
|
points.erase(points.begin() + j, points.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
BoundingBox get_extents(const MultiPoint &mp)
|
|
|
|
{
|
2016-11-07 21:49:11 +00:00
|
|
|
return BoundingBox(mp.points);
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox get_extents_rotated(const Points &points, double angle)
|
|
|
|
{
|
|
|
|
BoundingBox bbox;
|
|
|
|
if (! points.empty()) {
|
|
|
|
double s = sin(angle);
|
|
|
|
double c = cos(angle);
|
|
|
|
Points::const_iterator it = points.begin();
|
2018-08-14 16:33:26 +00:00
|
|
|
double cur_x = (double)it->x();
|
|
|
|
double cur_y = (double)it->y();
|
|
|
|
bbox.min.x() = bbox.max.x() = (coord_t)round(c * cur_x - s * cur_y);
|
|
|
|
bbox.min.y() = bbox.max.y() = (coord_t)round(c * cur_y + s * cur_x);
|
2016-11-07 21:49:11 +00:00
|
|
|
for (++it; it != points.end(); ++it) {
|
2018-08-14 16:33:26 +00:00
|
|
|
double cur_x = (double)it->x();
|
|
|
|
double cur_y = (double)it->y();
|
2016-11-07 21:49:11 +00:00
|
|
|
coord_t x = (coord_t)round(c * cur_x - s * cur_y);
|
|
|
|
coord_t y = (coord_t)round(c * cur_y + s * cur_x);
|
2018-08-14 16:33:26 +00:00
|
|
|
bbox.min.x() = std::min(x, bbox.min.x());
|
|
|
|
bbox.min.y() = std::min(y, bbox.min.y());
|
|
|
|
bbox.max.x() = std::max(x, bbox.max.x());
|
|
|
|
bbox.max.y() = std::max(y, bbox.max.y());
|
2016-11-07 21:49:11 +00:00
|
|
|
}
|
|
|
|
bbox.defined = true;
|
|
|
|
}
|
|
|
|
return bbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox get_extents_rotated(const MultiPoint &mp, double angle)
|
|
|
|
{
|
|
|
|
return get_extents_rotated(mp.points, angle);
|
2016-09-13 11:30:00 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|