Ported nearest_point() and nearest_point_index()
This commit is contained in:
parent
f1e9216c70
commit
b11b595c97
18 changed files with 79 additions and 69 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -13,6 +13,8 @@ extern "C" {
|
|||
#undef do_close
|
||||
}
|
||||
|
||||
#define EPSILON 1e-4
|
||||
|
||||
namespace Slic3r {}
|
||||
using namespace Slic3r;
|
||||
|
||||
|
|
|
@ -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__
|
||||
|
|
|
@ -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))); %};
|
||||
|
||||
%{
|
||||
|
||||
|
|
|
@ -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*
|
||||
|
|
|
@ -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*
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
%typemap{ExtrusionEntityCollection*};
|
||||
%typemap{ExtrusionPath*};
|
||||
%typemap{ExtrusionLoop*};
|
||||
%typemap{Points};
|
||||
%typemap{Lines};
|
||||
%typemap{Polygons};
|
||||
%typemap{ExPolygons};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue