Ported Polygon->split_at()

This commit is contained in:
Alessandro Ranellucci 2013-08-26 22:39:35 +02:00
parent f8ac3aa68f
commit 1f734807b9
7 changed files with 19 additions and 20 deletions

View File

@ -96,23 +96,6 @@ sub is_valid {
return @$self >= 3;
}
sub split_at {
my $self = shift;
my ($point) = @_;
# find index of point
my $i = -1;
for (my $n = 0; $n <= $#$self; $n++) {
if (Slic3r::Geometry::same_point($point, $self->[$n])) {
$i = $n;
last;
}
}
die "Point not found" if $i == -1;
return $self->split_at_index($i);
}
# for cw polygons this will return convex points!
sub concave_points {
my $self = shift;

View File

@ -26,7 +26,7 @@ Point::rotate(double angle, Point* center)
}
bool
Point::coincides_with(Point* point)
Point::coincides_with(const Point* point) const
{
return this->x == point->x && this->y == point->y;
}

View File

@ -19,7 +19,7 @@ class Point
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center);
bool coincides_with(Point* point);
bool coincides_with(const Point* point) const;
};
typedef std::vector<Point> Points;

View File

@ -23,6 +23,18 @@ Polygon::lines()
return lines;
}
Polyline*
Polygon::split_at(const Point* point)
{
// find index of point
for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
if ((*it).coincides_with(point)) {
return this->split_at_index(it - this->points.begin());
}
}
throw "Point not found";
}
Polyline*
Polygon::split_at_index(int index)
{

View File

@ -13,6 +13,7 @@ class Polygon : public MultiPoint {
public:
SV* to_SV_ref();
Lines lines();
Polyline* split_at(const Point* point);
Polyline* split_at_index(int index);
Polyline* split_at_first_point();
bool is_counter_clockwise();

View File

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 10;
use Test::More tests => 11;
my $square = [ # ccw
[100, 100],
@ -29,6 +29,7 @@ is_deeply [ map $_->pp, @$lines ], [
is_deeply $polygon->split_at_first_point->pp, [ @$square[0,1,2,3,0] ], 'split_at_first_point';
is_deeply $polygon->split_at_index(2)->pp, [ @$square[2,3,0,1,2] ], 'split_at_index';
is_deeply $polygon->split_at(Slic3r::Point->new(@{$square->[2]}))->pp, [ @$square[2,3,0,1,2] ], 'split_at';
ok $polygon->is_counter_clockwise, 'is_counter_clockwise';
{

View File

@ -17,6 +17,8 @@
void translate(double x, double y);
void reverse();
Lines lines();
Polyline* split_at(Point* point)
%code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = THIS->split_at(point); %};
Polyline* split_at_index(int index)
%code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = THIS->split_at_index(index); %};
Polyline* split_at_first_point()