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;
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
|
|
|
MultiPoint::scale(double factor)
|
|
|
|
{
|
|
|
|
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
|
|
|
|
(*it).scale(factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultiPoint::translate(double x, double y)
|
|
|
|
{
|
|
|
|
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
|
|
|
|
(*it).translate(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 14:27:34 +00:00
|
|
|
void
|
|
|
|
MultiPoint::translate(const Point &vector)
|
|
|
|
{
|
|
|
|
this->translate(vector.x, vector.y);
|
|
|
|
}
|
|
|
|
|
2016-04-10 17:06:46 +00:00
|
|
|
void
|
|
|
|
MultiPoint::rotate(double angle)
|
|
|
|
{
|
2016-09-13 11:30:00 +00:00
|
|
|
double s = sin(angle);
|
|
|
|
double c = cos(angle);
|
2016-04-10 17:06:46 +00:00
|
|
|
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
|
|
|
|
double cur_x = (double)it->x;
|
|
|
|
double cur_y = (double)it->y;
|
|
|
|
it->x = (coord_t)round(c * cur_x - s * cur_y);
|
|
|
|
it->y = (coord_t)round(c * cur_y + s * cur_x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
2014-04-24 11:43:24 +00:00
|
|
|
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);
|
2013-07-16 19:04:14 +00:00
|
|
|
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
|
2016-09-13 11:30:00 +00:00
|
|
|
double dx = double(it->x - center.x);
|
|
|
|
double dy = double(it->y - center.y);
|
|
|
|
it->x = (coord_t)round(double(center.x) + c * dx - s * dy);
|
|
|
|
it->y = (coord_t)round(double(center.y) + c * dy + s * dx);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultiPoint::reverse()
|
|
|
|
{
|
|
|
|
std::reverse(this->points.begin(), this->points.end());
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
Point
|
2013-09-02 18:22:20 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
|
|
|
|
if (it->coincides_with(point)) return it - this->points.begin();
|
|
|
|
}
|
|
|
|
return -1; // not found
|
|
|
|
}
|
|
|
|
|
2015-01-06 19:52:36 +00:00
|
|
|
bool
|
|
|
|
MultiPoint::has_boundary_point(const Point &point) const
|
|
|
|
{
|
|
|
|
double dist = point.distance_to(point.projection_onto(*this));
|
|
|
|
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)
|
|
|
|
if (points[i-1].coincides_with(points[i]))
|
|
|
|
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) {
|
|
|
|
if (points[j].coincides_with(points[i])) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-07-01 21:00:52 +00:00
|
|
|
void
|
|
|
|
MultiPoint::append(const Point &point)
|
|
|
|
{
|
|
|
|
this->points.push_back(point);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultiPoint::append(const Points &points)
|
|
|
|
{
|
|
|
|
this->append(points.begin(), points.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultiPoint::append(const Points::const_iterator &begin, const Points::const_iterator &end)
|
|
|
|
{
|
|
|
|
this->points.insert(this->points.end(), begin, end);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
2013-11-21 19:25:24 +00:00
|
|
|
double d = it->distance_to(full);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
BoundingBox get_extents(const MultiPoint &mp)
|
|
|
|
{
|
|
|
|
return mp.bounding_box();
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|