Replaced Moose with Moo => big performance boost and easier packaging

This commit is contained in:
Alessandro Ranellucci 2011-09-06 11:50:43 +02:00
parent bf5824781d
commit 26b05ab155
13 changed files with 67 additions and 75 deletions

View File

@ -20,9 +20,6 @@ algorithms in the future.
I also aim at implementing better support for hollow objects, as Skeinforge
isn't smart enough to generate internal support structures for horizontal
facets.
Of course, Perl's not that fast as C and usage of modules like Moose make
everything quite memory-hungry, but I'm happy with it. I want to build a "rapid
prototyping" architecture for a slicer.
Also, http://xkcd.com/224/

View File

@ -1,5 +1,5 @@
package Slic3r::ExtrusionPath;
use Moose;
use Moo;
extends 'Slic3r::Polyline';

View File

@ -1,5 +1,5 @@
package Slic3r::Fill;
use Moose;
use Moo;
use Slic3r::Fill::Rectilinear;

View File

@ -1,5 +1,5 @@
package Slic3r::Fill::Rectilinear;
use Moose;
use Moo;
use constant epsilon => 1E-10;
use constant PI => 4 * atan2(1, 1);
@ -45,10 +45,10 @@ sub make_fill {
# find out the coordinates
my @coordinates = map @$_, @$line;
Slic3r::debugf "Segment %d,%d - %d,%d\n", @coordinates;
# get the extents of the segment along the primary axis
my @line_c = sort ($coordinates[X1], $coordinates[X2]);
my @line_c = sort { $a <=> $b } @coordinates[X1, X2];
Slic3r::debugf "Segment %d,%d - %d,%d (extents: %f, %f)\n", @coordinates, @line_c;
for (my $c = $line_c[0]; $c <= $line_c[1]; $c += $distance_between_lines) {
my $i = sprintf('%.0f', $c / $distance_between_lines) - 1;

View File

@ -1,12 +1,12 @@
package Slic3r::Layer;
use Moose;
use Moo;
use XXX;
# a sequential number of layer, starting at 0
has 'id' => (
is => 'ro',
isa => 'Int',
#isa => 'Int',
required => 1,
);
@ -16,18 +16,15 @@ has 'id' => (
has 'pointmap' => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef[Slic3r::Point]',
#isa => 'HashRef[Slic3r::Point]',
default => sub { {} },
handles => {
points => 'values',
},
);
# collection of segments generated by slicing the original geometry
# each segment is part of a closed polyline
has 'lines' => (
is => 'rw',
isa => 'ArrayRef[Slic3r::Line]',
#isa => 'ArrayRef[Slic3r::Line]',
default => sub { [] },
);
@ -35,21 +32,21 @@ has 'lines' => (
has 'surfaces' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Slic3r::Surface]',
#isa => 'ArrayRef[Slic3r::Surface]',
default => sub { [] },
);
# ordered collection of extrusion paths to build all perimeters
has 'perimeters' => (
is => 'rw',
isa => 'ArrayRef[Slic3r::ExtrusionPath]',
#isa => 'ArrayRef[Slic3r::ExtrusionPath]',
default => sub { [] },
);
# ordered collection of extrusion paths to build skirt loops
has 'skirts' => (
is => 'rw',
isa => 'ArrayRef[Slic3r::ExtrusionPath]',
#isa => 'ArrayRef[Slic3r::ExtrusionPath]',
default => sub { [] },
);
@ -58,14 +55,14 @@ has 'skirts' => (
has 'fill_surfaces' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Slic3r::Surface]',
#isa => 'ArrayRef[Slic3r::Surface]',
default => sub { [] },
);
# ordered collection of extrusion paths to fill surfaces
has 'fills' => (
is => 'rw',
isa => 'ArrayRef[Slic3r::ExtrusionPath]',
#isa => 'ArrayRef[Slic3r::ExtrusionPath]',
default => sub { [] },
);
@ -74,6 +71,11 @@ sub z {
return $self->id * $Slic3r::layer_height / $Slic3r::resolution;
}
sub points {
my $self = shift;
return values %{ $self->pointmap };
}
sub add_surface {
my $self = shift;
my (@vertices) = @_;

View File

@ -1,32 +1,28 @@
package Slic3r::Line;
use Moose;
use Moose::Util::TypeConstraints;
use Moo;
use Scalar::Util qw(weaken);
subtype 'Slic3r::Line::Length', as 'Int';
coerce 'Slic3r::Line::Length', from 'Num', via { sprintf '%.0f', $_ };
has 'a' => (
is => 'ro',
isa => 'Slic3r::Point',
#isa => 'Slic3r::Point',
required => 1,
);
has 'b' => (
is => 'ro',
isa => 'Slic3r::Point',
#isa => 'Slic3r::Point',
required => 1,
);
has 'polyline' => (
is => 'rw',
isa => 'Slic3r::Polyline',
#isa => 'Slic3r::Polyline',
weak_ref => 1,
);
has 'solid_side' => (
is => 'rw',
isa => enum([qw(left right)]), # going from a to b
#isa => enum([qw(left right)]), # going from a to b
);
sub BUILD {

View File

@ -1,5 +1,5 @@
package Slic3r::Perimeter;
use Moose;
use Moo;
use Math::Geometry::Planar;
*Math::Geometry::Planar::OffsetPolygon = *Math::Geometry::Planar::Offset::OffsetPolygon;

View File

@ -1,28 +1,24 @@
package Slic3r::Point;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'Slic3r::Point::Coordinate', as 'Int';
coerce 'Slic3r::Point::Coordinate', from 'Num', via { sprintf '%.0f', $_ };
use Moo;
has 'x' => (
is => 'ro',
isa => 'Slic3r::Point::Coordinate',
#isa => 'Slic3r::Point::Coordinate',
required => 1,
coerce => 1,
coerce => sub { sprintf '%.0f', $_[0] },
);
has 'y' => (
is => 'ro',
isa => 'Slic3r::Point::Coordinate',
#isa => 'Slic3r::Point::Coordinate',
required => 1,
coerce => 1,
coerce => sub { sprintf '%.0f', $_[0] },
);
# this array contains weak references, so it can contain undef's as well
has 'lines' => (
is => 'rw',
isa => 'ArrayRef[Slic3r::Line]',
#isa => 'ArrayRef[Slic3r::Line]',
default => sub { [] },
);

View File

@ -1,23 +1,23 @@
package Slic3r::Polyline;
use Moose;
use Moo;
has 'lines' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Slic3r::Line]',
#isa => 'ArrayRef[Slic3r::Line]',
default => sub { [] },
handles => {
add_line => 'push',
},
);
after 'add_line' => sub {
sub add_line {
my $self = shift;
my ($line) = @_;
push @{ $self->lines }, $line;
# add a weak reference to this polyline in line objects
# (avoid circular refs)
$self->lines->[-1]->polyline($self);
};
}
sub BUILD {
my $self = shift;

View File

@ -1,30 +1,30 @@
package Slic3r::Polyline::Closed;
use Moose;
use Moo;
extends 'Slic3r::Polyline';
has 'contour_of' => (
is => 'rw',
isa => 'Slic3r::Surface',
#isa => 'Slic3r::Surface',
weak_ref => 1,
);
has 'hole_of' => (
is => 'rw',
isa => 'Slic3r::Surface',
#isa => 'Slic3r::Surface',
weak_ref => 1,
);
override 'new_from_points' => sub {
sub new_from_points {
my $class = shift;
my $polyline = super();
my $polyline = $class->SUPER::new_from_points(@_);
# polylines must be always closed, otherwise it means that our object is not manifold!
die "Polylines must be closed! Object not manifold?\n"
if ($polyline->lines->[0]->a != $polyline->lines->[-1]->b);
return $polyline;
};
}
sub encloses_point {
my $self = shift;

View File

@ -1,5 +1,5 @@
package Slic3r::Print;
use Moose;
use Moo;
use constant PI => 4 * atan2(1, 1);
use constant X => 0;
@ -7,37 +7,39 @@ use constant Y => 1;
has 'x_length' => (
is => 'ro',
isa => 'Slic3r::Line::Length',
#isa => 'Slic3r::Line::Length',
required => 1,
coerce => 1,
coerce => sub { sprintf '%.0f', $_[0] },
);
has 'y_length' => (
is => 'ro',
isa => 'Slic3r::Line::Length',
#isa => 'Slic3r::Line::Length',
required => 1,
coerce => 1,
coerce => sub { sprintf '%.0f', $_[0] },
);
has 'layers' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Slic3r::Layer]',
#isa => 'ArrayRef[Slic3r::Layer]',
default => sub { [] },
handles => {
layer_count => 'count',
add_layer => 'push',
},
);
sub layer_count {
my $self = shift;
return scalar @{ $self->layers };
}
sub layer {
my $self = shift;
my ($layer_id) = @_;
# extend our print by creating all necessary layers
if ($self->layer_count < $layer_id + 1) {
for (my $i = $self->layer_count; $i <= $layer_id; $i++) {
$self->add_layer(Slic3r::Layer->new(id => $i));
push @{ $self->layers }, Slic3r::Layer->new(id => $i);
}
}

View File

@ -1,5 +1,5 @@
package Slic3r::STL;
use Moose;
use Moo;
use CAD::Format::STL;
use XXX;

View File

@ -1,39 +1,38 @@
package Slic3r::Surface;
use Moose;
use Moo;
use Math::Geometry::Planar;
use Moose::Util::TypeConstraints;
has 'contour' => (
is => 'ro',
isa => 'Slic3r::Polyline::Closed',
#isa => 'Slic3r::Polyline::Closed',
required => 1,
);
has 'holes' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Slic3r::Polyline::Closed]',
#isa => 'ArrayRef[Slic3r::Polyline::Closed]',
default => sub { [] },
handles => {
'add_hole' => 'push',
},
);
# TODO: to allow for multiple solid skins to be filled near external
# surfaces, a new type should be defined: internal-solid
has 'surface_type' => (
is => 'rw',
isa => enum([qw(internal bottom top)]),
#isa => enum([qw(internal bottom top)]),
);
after 'add_hole' => sub {
sub add_hole {
my $self = shift;
my ($hole) = @_;
push @{ $self->holes }, $hole;
# add a weak reference to this surface in polyline objects
# (avoid circular refs)
$self->holes->[-1]->hole_of($self);
};
}
sub BUILD {
my $self = shift;