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;

View file

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 7;
use Test::More tests => 8;
my $point = Slic3r::Point->new(10, 15);
is_deeply [ @$point ], [10, 15], 'point roundtrip';
@ -22,7 +22,12 @@ ok !$point->coincides_with($point2), 'coincides_with';
{
my $point3 = Slic3r::Point->new(4300000, -9880845);
is $point->[0], $point->x, 'x accessor';
is $point->[1], $point->y, 'y accessor';
is $point->[1], $point->y, 'y accessor'; #,,
}
{
my $nearest = $point->nearest_point([ $point2, Slic3r::Point->new(100, 200) ]);
ok $nearest->coincides_with($point2), 'nearest_point';
}
__END__

View file

@ -20,6 +20,9 @@
%code{% RETVAL = THIS->x; %};
long y()
%code{% RETVAL = THIS->y; %};
int nearest_point_index(Points points);
Point* nearest_point(Points points)
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->nearest_point(points))); %};
%{

View file

@ -28,6 +28,8 @@
bool make_counter_clockwise();
bool make_clockwise();
bool is_valid();
Point* first_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->first_point())); %};
%{
Polygon*

View file

@ -19,6 +19,8 @@
%code{% THIS->points.pop_back(); %};
void reverse();
Lines lines();
Point* first_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->first_point())); %};
%{
Polyline*

View file

@ -17,6 +17,7 @@ SurfaceType T_UV
ClipperLib::JoinType T_UV
ClipperLib::PolyFillType T_UV
Points T_ARRAYREF
Lines T_ARRAYREF
Polygons T_ARRAYREF
ExPolygons T_ARRAYREF

View file

@ -10,6 +10,7 @@
%typemap{ExtrusionEntityCollection*};
%typemap{ExtrusionPath*};
%typemap{ExtrusionLoop*};
%typemap{Points};
%typemap{Lines};
%typemap{Polygons};
%typemap{ExPolygons};