Refactoring. Point objects are now plain arrayrefs. Slicing is 30% faster.

This commit is contained in:
Alessandro Ranellucci 2011-10-12 14:54:49 +02:00
parent 774717c8bb
commit e410410dc7
10 changed files with 44 additions and 55 deletions

View file

@ -1,51 +1,46 @@
package Slic3r::Point;
use Moo;
use strict;
use warnings;
has 'x' => (
is => 'ro',
required => 1,
#coerce => sub { sprintf '%.0f', $_[0] },
);
has 'y' => (
is => 'ro',
required => 1,
#coerce => sub { sprintf '%.0f', $_[0] },
);
sub cast {
sub new {
my $class = shift;
my ($point) = @_;
return ref $point eq 'ARRAY'
? Slic3r::Point->new(x => $point->[0], y => $point->[1]) # ==
: $point;
my $self;
if (@_ == 2) {
$self = [@_];
} elsif (ref $_[0] eq 'ARRAY') {
$self = [@{$_[0]}];
} elsif ($_[0]->isa(__PACKAGE__)) {
return $_[0];
} else {
die "Invalid arguments for ${class}->new";
}
bless $self, $class;
return $self;
}
sub id {
my $self = shift;
return $self->x . "," . $self->y; #;;
return join ',', @$self;
}
sub coordinates {
my $self = shift;
return ($self->x, $self->y); #))
}
sub p {
my $self = shift;
return [ $self->coordinates ];
return @$self;
}
sub coincides_with {
my $self = shift;
my ($point) = @_;
return Slic3r::Geometry::points_coincide($self->p, $point->p);
return Slic3r::Geometry::points_coincide($self, $point);
}
sub distance_to {
my $self = shift;
my ($point) = @_;
return Slic3r::Geometry::distance_between_points($self->p, $point->p);
return Slic3r::Geometry::distance_between_points($self, $point);
}
sub x { $_[0]->[0] }
sub y { $_[0]->[1] }
1;