Ported nearest_point() and nearest_point_index()

This commit is contained in:
Alessandro Ranellucci 2013-08-27 00:52:20 +02:00
parent f1e9216c70
commit b11b595c97
18 changed files with 79 additions and 69 deletions

View file

@ -32,6 +32,12 @@ MultiPoint::reverse()
std::reverse(this->points.begin(), this->points.end());
}
const Point*
MultiPoint::first_point() const
{
return &(this->points.front());
}
void
MultiPoint::from_SV(SV* poly_sv)
{

View file

@ -19,6 +19,7 @@ class MultiPoint
void translate(double x, double y);
void rotate(double angle, Point* center);
void reverse();
const Point* first_point() const;
};
}

View file

@ -1,4 +1,5 @@
#include "Point.hpp"
#include <math.h>
namespace Slic3r {
@ -31,6 +32,38 @@ Point::coincides_with(const Point* point) const
return this->x == point->x && this->y == point->y;
}
int
Point::nearest_point_index(const Points points) const
{
int idx = -1;
long distance = -1;
for (Points::const_iterator it = points.begin(); it != points.end(); ++it) {
/* If the X distance of the candidate is > than the total distance of the
best previous candidate, we know we don't want it */
long d = pow(this->x - (*it).x, 2);
if (distance != -1 && d > distance) continue;
/* If the Y distance of the candidate is > than the total distance of the
best previous candidate, we know we don't want it */
d += pow(this->y - (*it).y, 2);
if (distance != -1 && d > distance) continue;
idx = it - points.begin();
distance = d;
if (distance < EPSILON) break;
}
return idx;
}
Point*
Point::nearest_point(Points points) const
{
return &(points.at(this->nearest_point_index(points)));
}
SV*
Point::to_SV_pureperl() {
AV* av = newAV();

View file

@ -7,6 +7,9 @@
namespace Slic3r {
class Point;
typedef std::vector<Point> Points;
class Point
{
public:
@ -20,10 +23,10 @@ class Point
void translate(double x, double y);
void rotate(double angle, Point* center);
bool coincides_with(const Point* point) const;
int nearest_point_index(const Points points) const;
Point* nearest_point(Points points) const;
};
typedef std::vector<Point> Points;
}
#endif

View file

@ -13,6 +13,8 @@ extern "C" {
#undef do_close
}
#define EPSILON 1e-4
namespace Slic3r {}
using namespace Slic3r;