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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-24 11:43:24 +00:00
|
|
|
MultiPoint::rotate(double angle, const Point ¢er)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
|
|
|
|
(*it).rotate(angle, center);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:30:45 +00:00
|
|
|
bool
|
|
|
|
MultiPoint::is_valid() const
|
|
|
|
{
|
|
|
|
return this->points.size() >= 2;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-22 17:34:49 +00:00
|
|
|
void
|
|
|
|
MultiPoint::bounding_box(BoundingBox* bb) const
|
|
|
|
{
|
|
|
|
*bb = BoundingBox(this->points);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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) {
|
|
|
|
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);
|
|
|
|
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();
|
|
|
|
dp0.reserve(points.size() - index + 1);
|
|
|
|
dp0.insert(dp0.end(), points.begin() + index, points.end());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-19 14:19:47 +00:00
|
|
|
#ifdef SLIC3RXS
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
|
|
|
MultiPoint::from_SV(SV* poly_sv)
|
|
|
|
{
|
|
|
|
AV* poly_av = (AV*)SvRV(poly_sv);
|
|
|
|
const unsigned int num_points = av_len(poly_av)+1;
|
|
|
|
this->points.resize(num_points);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < num_points; i++) {
|
|
|
|
SV** point_sv = av_fetch(poly_av, i, 0);
|
|
|
|
this->points[i].from_SV_check(*point_sv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MultiPoint::from_SV_check(SV* poly_sv)
|
|
|
|
{
|
|
|
|
if (sv_isobject(poly_sv) && (SvTYPE(SvRV(poly_sv)) == SVt_PVMG)) {
|
|
|
|
*this = *(MultiPoint*)SvIV((SV*)SvRV( poly_sv ));
|
|
|
|
} else {
|
|
|
|
this->from_SV(poly_sv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
2013-09-09 20:27:58 +00:00
|
|
|
MultiPoint::to_AV() {
|
2013-07-16 19:04:14 +00:00
|
|
|
const unsigned int num_points = this->points.size();
|
|
|
|
AV* av = newAV();
|
|
|
|
av_extend(av, num_points-1);
|
|
|
|
for (unsigned int i = 0; i < num_points; i++) {
|
2014-05-08 12:52:48 +00:00
|
|
|
av_store(av, i, perl_to_SV_ref(this->points[i]));
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
return newRV_noinc((SV*)av);
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
2013-09-03 17:26:58 +00:00
|
|
|
MultiPoint::to_SV_pureperl() const {
|
2013-07-16 19:04:14 +00:00
|
|
|
const unsigned int num_points = this->points.size();
|
|
|
|
AV* av = newAV();
|
|
|
|
av_extend(av, num_points-1);
|
|
|
|
for (unsigned int i = 0; i < num_points; i++) {
|
|
|
|
av_store(av, i, this->points[i].to_SV_pureperl());
|
|
|
|
}
|
|
|
|
return newRV_noinc((SV*)av);
|
|
|
|
}
|
2013-09-19 14:19:47 +00:00
|
|
|
#endif
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
}
|