More integration of Slic3r::Point::XS

This commit is contained in:
Alessandro Ranellucci 2013-07-15 16:04:49 +02:00
parent 159a009f96
commit c9749ca3b3
9 changed files with 80 additions and 17 deletions

View File

@ -270,7 +270,7 @@ sub extrude_path {
# go to first point of extrusion path # go to first point of extrusion path
my $gcode = ""; my $gcode = "";
$gcode .= $self->travel_to($path->points->[0], $path->role, "move to first $description point") $gcode .= $self->travel_to($path->points->[0], $path->role, "move to first $description point")
if !$self->last_pos || !$self->last_pos->coincides_with($path->points->[0]); if !defined $self->last_pos || !$self->last_pos->coincides_with($path->points->[0]);
# compensate retraction # compensate retraction
$gcode .= $self->unretract; $gcode .= $self->unretract;

View File

@ -386,7 +386,10 @@ sub rotate_points {
sub move_points { sub move_points {
my ($shift, @points) = @_; my ($shift, @points) = @_;
return map Slic3r::Point->new($shift->[X] + $_->[X], $shift->[Y] + $_->[Y]), @points; return map {
my @p = @$_;
Slic3r::Point->new($shift->[X] + $p[X], $shift->[Y] + $p[Y]);
} @points;
} }
sub move_points_3D { sub move_points_3D {

View File

@ -12,7 +12,6 @@ sub new {
my $self; my $self;
$self = [ @_ ]; $self = [ @_ ];
bless $self, $class; bless $self, $class;
bless $_, 'Slic3r::Point' for @$self;
return $self; return $self;
} }

View File

@ -21,7 +21,7 @@ sub new {
} }
sub clone { sub clone {
Storable::dclone($_[0]) return Slic3r::Point::XS->new(@{$_[0]});
} }
sub threadsafe_clone { sub threadsafe_clone {

View File

@ -243,7 +243,7 @@ sub chained_path {
: [ map { $_->[0], $_->[-1] } @my_paths ]; : [ map { $_->[0], $_->[-1] } @my_paths ];
while (@my_paths) { while (@my_paths) {
# find nearest point # find nearest point
my $start_index = $start_near my $start_index = defined $start_near
? Slic3r::Geometry::nearest_point_index($start_near, $endpoints) ? Slic3r::Geometry::nearest_point_index($start_near, $endpoints)
: 0; : 0;

View File

@ -18,18 +18,41 @@ class Point
long x; long x;
long y; long y;
Point(long _x = 0, long _y = 0): x(_x), y(_y) {}; Point(long _x = 0, long _y = 0): x(_x), y(_y) {};
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
bool coincides_with(Point* point);
}; };
typedef std::vector<Point> Points; typedef std::vector<Point> Points;
void
Point::scale(double factor)
{
this->x *= factor;
this->y *= factor;
}
void
Point::translate(double x, double y)
{
this->x += x;
this->y += y;
}
void void
Point::rotate(double angle, Point* center) Point::rotate(double angle, Point* center)
{ {
double cur_x = (double)x; double cur_x = (double)this->x;
double cur_y = (double)y; double cur_y = (double)this->y;
x = (long)( (double)center->x + cos(angle) * (cur_x - (double)center->x) - sin(angle) * (cur_y - (double)center->y) ); this->x = (long)( (double)center->x + cos(angle) * (cur_x - (double)center->x) - sin(angle) * (cur_y - (double)center->y) );
y = (long)( (double)center->y + cos(angle) * (cur_y - (double)center->y) + sin(angle) * (cur_x - (double)center->x) ); this->y = (long)( (double)center->y + cos(angle) * (cur_y - (double)center->y) + sin(angle) * (cur_x - (double)center->x) );
}
bool
Point::coincides_with(Point* point)
{
return this->x == point->x && this->y == point->y;
} }
SV* SV*
@ -48,6 +71,16 @@ perl2point(SV* point_sv, Point& point)
point.y = (unsigned long)SvIV(*av_fetch(point_av, 1, 0)); point.y = (unsigned long)SvIV(*av_fetch(point_av, 1, 0));
} }
void
perl2point_check(SV* point_sv, Point& point)
{
if (sv_isobject(point_sv) && (SvTYPE(SvRV(point_sv)) == SVt_PVMG)) {
point = *(Point*)SvIV((SV*)SvRV( point_sv ));
} else {
perl2point(point_sv, point);
}
}
} }
#endif #endif

View File

@ -29,8 +29,7 @@ void
Polyline::scale(double factor) Polyline::scale(double factor)
{ {
for (Points::iterator it = points.begin(); it != points.end(); ++it) { for (Points::iterator it = points.begin(); it != points.end(); ++it) {
(*it).x *= factor; (*it).scale(factor);
(*it).y *= factor;
} }
} }
@ -38,8 +37,7 @@ void
Polyline::translate(double x, double y) Polyline::translate(double x, double y)
{ {
for (Points::iterator it = points.begin(); it != points.end(); ++it) { for (Points::iterator it = points.begin(); it != points.end(); ++it) {
(*it).x += x; (*it).translate(x, y);
(*it).y += y;
} }
} }

View File

@ -4,11 +4,21 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 2; use Test::More tests => 6;
my $point = Slic3r::Point::XS->new(10, 15); my $point = Slic3r::Point::XS->new(10, 15);
is_deeply [ @$point ], [10, 15], 'point roundtrip'; is_deeply [ @$point ], [10, 15], 'point roundtrip';
isa_ok $point->arrayref, 'Slic3r::Point', 'Perl point is blessed'; isa_ok $point->arrayref, 'Slic3r::Point', 'Perl point is blessed';
my $point2 = $point->clone;
$point2->scale(2);
is_deeply [ @$point2 ], [20, 30], 'scale';
$point2->translate(10, -15);
is_deeply [ @$point2 ], [30, 15], 'translate';
ok $point->coincides_with($point->clone), 'coincides_with';
ok !$point->coincides_with($point2), 'coincides_with';
__END__ __END__

View File

@ -8,16 +8,36 @@
%name{Slic3r::Point::XS} class Point { %name{Slic3r::Point::XS} class Point {
Point(unsigned long _x = 0, unsigned long _y = 0); Point(unsigned long _x = 0, unsigned long _y = 0);
~Point(); ~Point();
Point* clone()
%code{% const char* CLASS = "Slic3r::Point::XS"; RETVAL = new Point(*THIS); %};
void scale(double factor);
void translate(double x, double y);
SV* arrayref()
%code{% RETVAL = point2perl(*THIS); %};
%{ %{
SV* void
Point::arrayref() Point::rotate(angle, center_sv)
double angle;
SV* center_sv;
CODE: CODE:
RETVAL = point2perl(*THIS); Point center;
perl2point_check(center_sv, center);
THIS->rotate(angle, &center);
bool
Point::coincides_with(point_sv)
SV* point_sv;
CODE:
Point point;
perl2point_check(point_sv, point);
RETVAL = THIS->coincides_with(&point);
OUTPUT: OUTPUT:
RETVAL RETVAL
%} %}
}; };
%package{Slic3r::Point::XS}; %package{Slic3r::Point::XS};