Translate Model class' storage to C++.

Some code copied from xs-model branch.

Also:
* Generate ::Ref classes programatically.
* Add separate __REGISTER_CLASS macro
    (for use where forward declaration won't work, i.e. typedefs)
This commit is contained in:
Y. Sapir 2014-04-30 02:04:49 +03:00
parent c72dc13d7e
commit 05b2993769
20 changed files with 1048 additions and 218 deletions

View File

@ -148,6 +148,8 @@ sub thread_cleanup {
*Slic3r::Geometry::BoundingBox::DESTROY = sub {}; *Slic3r::Geometry::BoundingBox::DESTROY = sub {};
*Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {}; *Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {};
*Slic3r::Line::DESTROY = sub {}; *Slic3r::Line::DESTROY = sub {};
*Slic3r::Model::DESTROY = sub {};
*Slic3r::Model::Object::DESTROY = sub {};
*Slic3r::Point::DESTROY = sub {}; *Slic3r::Point::DESTROY = sub {};
*Slic3r::Pointf::DESTROY = sub {}; *Slic3r::Pointf::DESTROY = sub {};
*Slic3r::Pointf3::DESTROY = sub {}; *Slic3r::Pointf3::DESTROY = sub {};

View File

@ -35,8 +35,8 @@ sub write_file {
printf $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n}; printf $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n};
printf $fh qq{<amf unit="millimeter">\n}; printf $fh qq{<amf unit="millimeter">\n};
printf $fh qq{ <metadata type="cad">Slic3r %s</metadata>\n}, $Slic3r::VERSION; printf $fh qq{ <metadata type="cad">Slic3r %s</metadata>\n}, $Slic3r::VERSION;
for my $material_id (sort keys %{ $model->materials }) { for my $material_id (sort @{ $model->material_names }) {
my $material = $model->materials->{$material_id}; my $material = $model->get_material($material_id);
printf $fh qq{ <material id="%s">\n}, $material_id; printf $fh qq{ <material id="%s">\n}, $material_id;
for (keys %{$material->attributes}) { for (keys %{$material->attributes}) {
printf $fh qq{ <metadata type=\"%s\">%s</metadata>\n}, $_, $material->attributes->{$_}; printf $fh qq{ <metadata type=\"%s\">%s</metadata>\n}, $_, $material->attributes->{$_};

View File

@ -413,7 +413,7 @@ sub load_model_objects {
} }
$self->{print}->auto_assign_extruders($o); $self->{print}->auto_assign_extruders($o);
$self->{print}->add_model_object($o); $self->{print}->add_model_object($self->{model}, $o);
} }
# if user turned autocentering off, automatic arranging would disappoint them # if user turned autocentering off, automatic arranging would disappoint them
@ -542,12 +542,12 @@ sub rotate {
{ {
my $new_angle = $model_instance->rotation + $angle; my $new_angle = $model_instance->rotation + $angle;
$_->rotation($new_angle) for @{ $model_object->instances }; $_->set_rotation($new_angle) for @{ $model_object->instances };
$model_object->update_bounding_box; $model_object->update_bounding_box;
# update print # update print
$self->{print}->delete_object($obj_idx); $self->{print}->delete_object($obj_idx);
$self->{print}->add_model_object($model_object, $obj_idx); $self->{print}->add_model_object($self->{model}, $model_object, $obj_idx);
$object->transform_thumbnail($self->{model}, $obj_idx); $object->transform_thumbnail($self->{model}, $obj_idx);
} }
@ -578,12 +578,13 @@ sub changescale {
$range->[0] *= $variation; $range->[0] *= $variation;
$range->[1] *= $variation; $range->[1] *= $variation;
} }
$_->scaling_factor($scale) for @{ $model_object->instances }; $_->set_scaling_factor($scale) for @{ $model_object->instances };
$model_object->update_bounding_box; $model_object->update_bounding_box;
# update print # update print
$self->{print}->delete_object($obj_idx); $self->{print}->delete_object($obj_idx);
$self->{print}->add_model_object($model_object, $obj_idx); $self->{print}->add_model_object($self->{model},
$model_object, $obj_idx);
$object->transform_thumbnail($self->{model}, $obj_idx); $object->transform_thumbnail($self->{model}, $obj_idx);
} }
@ -1108,10 +1109,11 @@ sub mouse_event {
return if !$self->{drag_start_pos}; # concurrency problems return if !$self->{drag_start_pos}; # concurrency problems
my ($obj_idx, $instance_idx) = @{ $self->{drag_object} }; my ($obj_idx, $instance_idx) = @{ $self->{drag_object} };
my $model_object = $parent->{model}->objects->[$obj_idx]; my $model_object = $parent->{model}->objects->[$obj_idx];
$model_object->instances->[$instance_idx]->offset([ $model_object->instances->[$instance_idx]->set_offset(
unscale($pos->[X] - $self->{drag_start_pos}[X]), Slic3r::Pointf->new(
unscale($pos->[Y] - $self->{drag_start_pos}[Y]), unscale($pos->[X] - $self->{drag_start_pos}[X]),
]); unscale($pos->[Y] - $self->{drag_start_pos}[Y]),
));
$model_object->update_bounding_box; $model_object->update_bounding_box;
$parent->Refresh; $parent->Refresh;
} elsif ($event->Moving) { } elsif ($event->Moving) {

View File

@ -158,7 +158,7 @@ sub selection_changed {
# attach volume material config to settings panel # attach volume material config to settings panel
my $volume = $self->{model_object}->volumes->[ $itemData->{volume_id} ]; my $volume = $self->{model_object}->volumes->[ $itemData->{volume_id} ];
my $material = $self->{model_object}->model->materials->{ $volume->material_id // '_' }; my $material = $self->{model_object}->model->get_material($volume->material_id // '_');
$material //= $volume->assign_unique_material; $material //= $volume->assign_unique_material;
$self->{staticbox}->SetLabel('Part Settings'); $self->{staticbox}->SetLabel('Part Settings');
$self->{settings_panel}->enable; $self->{settings_panel}->enable;
@ -211,7 +211,7 @@ sub on_btn_load {
$new_volume->mesh->translate(@{$self->{model_object}->origin_translation}, 0); $new_volume->mesh->translate(@{$self->{model_object}->origin_translation}, 0);
# set a default extruder value, since user can't add it manually # set a default extruder value, since user can't add it manually
my $material = $self->{model_object}->model->materials->{$new_volume->material_id}; my $material = $self->{model_object}->model->get_material($new_volume->material_id);
$material->config->set_ifndef('extruder', 1); $material->config->set_ifndef('extruder', 1);
$self->{parts_changed} = 1; $self->{parts_changed} = 1;

View File

@ -1,12 +1,8 @@
package Slic3r::Model; package Slic3r::Model;
use Moo;
use List::Util qw(first max); use List::Util qw(first max);
use Slic3r::Geometry qw(X Y Z move_points); use Slic3r::Geometry qw(X Y Z move_points);
has 'materials' => (is => 'ro', default => sub { {} });
has 'objects' => (is => 'ro', default => sub { [] });
sub read_from_file { sub read_from_file {
my $class = shift; my $class = shift;
my ($input_file) = @_; my ($input_file) = @_;
@ -16,7 +12,7 @@ sub read_from_file {
: $input_file =~ /\.amf(\.xml)?$/i ? Slic3r::Format::AMF->read_file($input_file) : $input_file =~ /\.amf(\.xml)?$/i ? Slic3r::Format::AMF->read_file($input_file)
: die "Input file must have .stl, .obj or .amf(.xml) extension\n"; : die "Input file must have .stl, .obj or .amf(.xml) extension\n";
$_->input_file($input_file) for @{$model->objects}; $_->set_input_file($input_file) for @{$model->objects};
return $model; return $model;
} }
@ -39,11 +35,12 @@ sub add_object {
if (@_ == 1) { if (@_ == 1) {
# we have a Model::Object # we have a Model::Object
my ($object) = @_; my ($object) = @_;
$new_object = $self->add_object( $new_object = $self->add_object(
input_file => $object->input_file, input_file => $object->input_file,
config => $object->config, config => $object->config,
layer_height_ranges => $object->layer_height_ranges, # TODO: clone! layer_height_ranges => $object->layer_height_ranges, # TODO: clone!
origin_translation => $object->origin_translation,
); );
foreach my $volume (@{$object->volumes}) { foreach my $volume (@{$object->volumes}) {
@ -51,51 +48,39 @@ sub add_object {
} }
$new_object->add_instance( $new_object->add_instance(
offset => $_->offset, offset => [ @{$_->offset} ],
rotation => $_->rotation, rotation => $_->rotation,
scaling_factor => $_->scaling_factor, scaling_factor => $_->scaling_factor,
) for @{ $object->instances // [] }; ) for @{ $object->instances // [] };
} else { } else {
push @{$self->objects}, $new_object = Slic3r::Model::Object->new(model => $self, @_); my (%args) = @_;
$new_object = $self->_add_object(
$args{input_file},
$args{config} // Slic3r::Config->new,
$args{layer_height_ranges} // [],
$args{origin_translation} // Slic3r::Point->new,
);
} }
return $new_object; return $new_object;
} }
sub delete_object {
my ($self, $obj_idx) = @_;
splice @{$self->objects}, $obj_idx, 1;
}
sub delete_all_objects {
my ($self) = @_;
@{$self->objects} = ();
}
sub set_material { sub set_material {
my $self = shift; my $self = shift;
my ($material_id, $attributes) = @_; my ($material_id, $attributes) = @_;
return $self->materials->{$material_id} = Slic3r::Model::Material->new( return $self->_set_material($material_id, $attributes || {});
model => $self,
attributes => $attributes || {},
);
}
sub get_material {
my ($self, $material_id) = @_;
return $self->materials->{$material_id};
} }
sub duplicate_objects_grid { sub duplicate_objects_grid {
my ($self, $grid, $distance) = @_; my ($self, $grid, $distance) = @_;
die "Grid duplication is not supported with multiple objects\n" die "Grid duplication is not supported with multiple objects\n"
if @{$self->objects} > 1; if @{$self->objects} > 1;
my $object = $self->objects->[0]; my $object = $self->objects->[0];
@{$object->instances} = (); $object->clear_instances;
my $size = $object->bounding_box->size; my $size = $object->bounding_box->size;
for my $x_copy (1..$grid->[X]) { for my $x_copy (1..$grid->[X]) {
for my $y_copy (1..$grid->[Y]) { for my $y_copy (1..$grid->[Y]) {
@ -144,7 +129,7 @@ sub arrange_objects {
my @positions = $self->_arrange(\@instance_sizes, $distance, $bb); my @positions = $self->_arrange(\@instance_sizes, $distance, $bb);
foreach my $object (@{$self->objects}) { foreach my $object (@{$self->objects}) {
$_->offset([ @{shift @positions} ]) for @{$object->instances}; $_->set_offset(Slic3r::Pointf->new(@{shift @positions})) for @{$object->instances};
$object->update_bounding_box; $object->update_bounding_box;
} }
} }
@ -231,8 +216,9 @@ sub center_instances_around_point {
foreach my $object (@{$self->objects}) { foreach my $object (@{$self->objects}) {
foreach my $instance (@{$object->instances}) { foreach my $instance (@{$object->instances}) {
$instance->offset->[X] += $shift[X]; $instance->set_offset(Slic3r::Pointf->new(
$instance->offset->[Y] += $shift[Y]; $instance->offset->x + $shift[X],
$instance->offset->y + $shift[Y]));
} }
$object->update_bounding_box; $object->update_bounding_box;
} }
@ -276,7 +262,7 @@ sub split_meshes {
if (@{$object->volumes} > 1) { if (@{$object->volumes} > 1) {
# We can't split meshes if there's more than one material, because # We can't split meshes if there's more than one material, because
# we can't group the resulting meshes by object afterwards # we can't group the resulting meshes by object afterwards
push @{$self->objects}, $object; $self->_add_object($object);
next; next;
} }
@ -286,6 +272,7 @@ sub split_meshes {
input_file => $object->input_file, input_file => $object->input_file,
config => $object->config->clone, config => $object->config->clone,
layer_height_ranges => $object->layer_height_ranges, # TODO: this needs to be cloned layer_height_ranges => $object->layer_height_ranges, # TODO: this needs to be cloned
origin_translation => $object->origin_translation,
); );
$new_object->add_volume( $new_object->add_volume(
mesh => $mesh, mesh => $mesh,
@ -312,57 +299,44 @@ sub get_material_name {
my ($material_id) = @_; my ($material_id) = @_;
my $name; my $name;
if (exists $self->materials->{$material_id}) { if ($self->has_material($material_id)) {
$name //= $self->materials->{$material_id}->attributes->{$_} for qw(Name name); $name //= $self->get_material($material_id)
->attributes->{$_} for qw(Name name);
} }
$name //= $material_id; $name //= $material_id;
return $name; return $name;
} }
package Slic3r::Model::Material; package Slic3r::Model::Material;
use Moo;
has 'model' => (is => 'ro', weak_ref => 1, required => 1); sub attributes {
has 'attributes' => (is => 'rw', default => sub { {} }); $_[0]->_attributes->to_hash;
has 'config' => (is => 'rw', default => sub { Slic3r::Config->new }); }
package Slic3r::Model::Object; package Slic3r::Model::Object;
use Moo;
use File::Basename qw(basename); use File::Basename qw(basename);
use List::Util qw(first sum); use List::Util qw(first sum);
use Slic3r::Geometry qw(X Y Z rad2deg); use Slic3r::Geometry qw(X Y Z rad2deg);
has 'input_file' => (is => 'rw');
has 'model' => (is => 'ro', weak_ref => 1, required => 1);
has 'volumes' => (is => 'ro', default => sub { [] });
has 'instances' => (is => 'rw');
has 'config' => (is => 'rw', default => sub { Slic3r::Config->new });
has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ]
has '_bounding_box' => (is => 'rw');
has 'origin_translation' => (is => 'ro', default => sub { Slic3r::Point->new }); # translation vector applied by center_around_origin()
sub add_volume { sub add_volume {
my $self = shift; my $self = shift;
my $new_volume; my $new_volume;
if (@_ == 1) { if (@_ == 1) {
# we have a Model::Volume # we have a Model::Volume
my ($volume) = @_; my ($volume) = @_;
$new_volume = Slic3r::Model::Volume->new( $new_volume = $self->_add_volume(
object => $self, $volume->material_id, $volume->mesh->clone, $volume->modifier);
material_id => $volume->material_id,
mesh => $volume->mesh->clone,
modifier => $volume->modifier,
);
# TODO: material_id can't be undef.
if (defined $volume->material_id) { if (defined $volume->material_id) {
# merge material attributes and config (should we rename materials in case of duplicates?) # merge material attributes and config (should we rename materials in case of duplicates?)
if (my $material = $volume->object->model->materials->{$volume->material_id}) { if (my $material = $volume->object->model->get_material($volume->material_id)) {
my %attributes = %{ $material->attributes }; my %attributes = %{ $material->attributes };
if (exists $self->model->materials->{$volume->material_id}) { if ($self->model->has_material($volume->material_id)) {
%attributes = (%attributes, %{ $self->model->materials->{$volume->material_id}->attributes }) %attributes = (%attributes, %{ $self->model->get_material($volume->material_id)->attributes })
} }
my $new_material = $self->model->set_material($volume->material_id, {%attributes}); my $new_material = $self->model->set_material($volume->material_id, {%attributes});
$new_material->config->apply($material->config); $new_material->config->apply($material->config);
@ -370,10 +344,10 @@ sub add_volume {
} }
} else { } else {
my %args = @_; my %args = @_;
$new_volume = Slic3r::Model::Volume->new( $new_volume = $self->_add_volume(
object => $self, $args{material_id},
%args, $args{mesh},
); $args{modifier} // 0);
} }
if (defined $new_volume->material_id && !defined $self->model->get_material($new_volume->material_id)) { if (defined $new_volume->material_id && !defined $self->model->get_material($new_volume->material_id)) {
@ -381,33 +355,19 @@ sub add_volume {
$self->model->set_material($new_volume->material_id); $self->model->set_material($new_volume->material_id);
} }
push @{$self->volumes}, $new_volume; $self->invalidate_bounding_box();
# invalidate cached bounding box
$self->_bounding_box(undef);
return $new_volume; return $new_volume;
} }
sub delete_volume {
my ($self, $i) = @_;
splice @{$self->volumes}, $i, 1;
}
sub add_instance { sub add_instance {
my $self = shift; my $self = shift;
my %params = @_; my %params = @_;
$self->instances([]) if !defined $self->instances;
push @{$self->instances}, my $i = Slic3r::Model::Instance->new(object => $self, %params);
$self->_bounding_box(undef);
return $i;
}
sub delete_last_instance { return $self->_add_instance(
my ($self) = @_; $params{rotation} // 0,
pop @{$self->instances}; $params{scaling_factor} // 1,
$self->_bounding_box(undef); $params{offset} // []);
} }
sub instances_count { sub instances_count {
@ -487,8 +447,9 @@ sub center_around_origin {
if (defined $self->instances) { if (defined $self->instances) {
foreach my $instance (@{ $self->instances }) { foreach my $instance (@{ $self->instances }) {
$instance->offset->[X] -= $shift[X]; $instance->set_offset(Slic3r::Pointf->new(
$instance->offset->[Y] -= $shift[Y]; $instance->offset->x - $shift[X],
$instance->offset->y - $shift[Y]));
} }
$self->update_bounding_box; $self->update_bounding_box;
} }
@ -511,7 +472,7 @@ sub rotate_x {
$angle = rad2deg($angle); $angle = rad2deg($angle);
$_->mesh->rotate_x($angle) for @{$self->volumes}; $_->mesh->rotate_x($angle) for @{$self->volumes};
$self->_bounding_box(undef); $self->invalidate_bounding_box;
} }
sub materials_count { sub materials_count {
@ -577,22 +538,22 @@ sub cut {
# clone this one # clone this one
my $upper = Slic3r::Model::Object->new( my $upper = Slic3r::Model::Object->new(
input_file => $self->input_file, $self->model,
model => $self->model, $self->input_file,
config => $self->config->clone, $self->config, # config is cloned by new()
layer_height_ranges => $self->layer_height_ranges, $self->layer_height_ranges,
origin_translation => $self->origin_translation->clone, $self->origin_translation,
); );
my $lower = Slic3r::Model::Object->new( my $lower = Slic3r::Model::Object->new(
input_file => $self->input_file, $self->model,
model => $self->model, $self->input_file,
config => $self->config->clone, $self->config, # config is cloned by new()
layer_height_ranges => $self->layer_height_ranges, $self->layer_height_ranges,
origin_translation => $self->origin_translation->clone, $self->origin_translation,
); );
foreach my $instance (@{$self->instances}) { foreach my $instance (@{$self->instances}) {
$upper->add_instance(offset => $instance->offset); $upper->add_instance(offset => [ @{$instance->offset} ]);
$lower->add_instance(offset => $instance->offset); $lower->add_instance(offset => [ @{$instance->offset} ]);
} }
foreach my $volume (@{$self->volumes}) { foreach my $volume (@{$self->volumes}) {
@ -632,29 +593,17 @@ sub cut {
} }
package Slic3r::Model::Volume; package Slic3r::Model::Volume;
use Moo;
has 'object' => (is => 'ro', weak_ref => 1, required => 1);
has 'material_id' => (is => 'rw');
has 'mesh' => (is => 'rw', required => 1);
has 'modifier' => (is => 'rw', defualt => sub { 0 });
sub assign_unique_material { sub assign_unique_material {
my ($self) = @_; my ($self) = @_;
my $model = $self->object->model; my $model = $self->object->model;
my $material_id = 1 + scalar keys %{$model->materials}; my $material_id = 1 + $model->material_count;
$self->material_id($material_id); $self->material_id($material_id);
return $model->set_material($material_id); return $model->set_material($material_id);
} }
package Slic3r::Model::Instance; package Slic3r::Model::Instance;
use Moo;
has 'object' => (is => 'ro', weak_ref => 1, required => 1);
has 'rotation' => (is => 'rw', default => sub { 0 }); # around mesh center point
has 'scaling_factor' => (is => 'rw', default => sub { 1 });
has 'offset' => (is => 'rw'); # must be arrayref in *unscaled* coordinates
sub transform_mesh { sub transform_mesh {
my ($self, $mesh, $dont_translate) = @_; my ($self, $mesh, $dont_translate) = @_;

View File

@ -86,7 +86,7 @@ sub apply_config {
$new->apply_dynamic($model_object_config); $new->apply_dynamic($model_object_config);
} }
if (defined $volume->material_id) { if (defined $volume->material_id) {
my $material_config = $object->model_object->model->materials->{$volume->material_id}->config->clone; my $material_config = $object->model_object->model->get_material($volume->material_id)->config->clone;
$material_config->normalize; $material_config->normalize;
$new->apply_dynamic($material_config); $new->apply_dynamic($material_config);
} }
@ -104,9 +104,9 @@ sub apply_config {
if ($rearrange_regions) { if ($rearrange_regions) {
# the current subdivision of regions does not make sense anymore. # the current subdivision of regions does not make sense anymore.
# we need to remove all objects and re-add them # we need to remove all objects and re-add them
my @model_objects = map $_->model_object, @{$self->objects}; my @models_objects = map [$_->model, $_->model_object], @{$self->objects};
$self->delete_all_objects; $self->delete_all_objects;
$self->add_model_object($_) for @model_objects; $self->add_model_object(@$_) for @models_objects;
} }
} }
@ -121,7 +121,7 @@ sub has_support_material {
# and have explicit instance positions # and have explicit instance positions
sub add_model_object { sub add_model_object {
my $self = shift; my $self = shift;
my ($object, $obj_idx) = @_; my ($model, $object, $obj_idx) = @_;
my $object_config = $object->config->clone; my $object_config = $object->config->clone;
$object_config->normalize; $object_config->normalize;
@ -138,7 +138,7 @@ sub add_model_object {
$config->apply_dynamic($object_config); $config->apply_dynamic($object_config);
if (defined $volume->material_id) { if (defined $volume->material_id) {
my $material_config = $object->model->materials->{ $volume->material_id }->config->clone; my $material_config = $object->model->get_material($volume->material_id)->config->clone;
$material_config->normalize; $material_config->normalize;
$config->apply_dynamic($material_config); $config->apply_dynamic($material_config);
} }
@ -170,6 +170,7 @@ sub add_model_object {
# initialize print object # initialize print object
my $o = Slic3r::Print::Object->new( my $o = Slic3r::Print::Object->new(
print => $self, print => $self,
model => $model, # for strong ref
model_object => $object, model_object => $object,
region_volumes => [ map $volumes{$_}, 0..$#{$self->regions} ], region_volumes => [ map $volumes{$_}, 0..$#{$self->regions} ],
copies => [ map Slic3r::Point->new_scale(@{ $_->offset }), @{ $object->instances } ], copies => [ map Slic3r::Point->new_scale(@{ $_->offset }), @{ $object->instances } ],
@ -220,9 +221,9 @@ sub reload_object {
# For now we just re-add all objects since we haven't implemented this incremental logic yet. # For now we just re-add all objects since we haven't implemented this incremental logic yet.
# This should also check whether object volumes (parts) have changed. # This should also check whether object volumes (parts) have changed.
my @model_objects = map $_->model_object, @{$self->objects}; my @models_objects = map [$_->model, $_->model_object], @{$self->objects};
$self->delete_all_objects; $self->delete_all_objects;
$self->add_model_object($_) for @model_objects; $self->add_model_object(@$_) for @models_objects;
} }
sub validate { sub validate {
@ -1112,7 +1113,7 @@ sub auto_assign_extruders {
foreach my $i (0..$#{$model_object->volumes}) { foreach my $i (0..$#{$model_object->volumes}) {
my $volume = $model_object->volumes->[$i]; my $volume = $model_object->volumes->[$i];
if (defined $volume->material_id) { if (defined $volume->material_id) {
my $material = $model_object->model->materials->{ $volume->material_id }; my $material = $model_object->model->get_material($volume->material_id);
my $config = $material->config; my $config = $material->config;
my $extruder_id = $i + 1; my $extruder_id = $i + 1;
$config->set_ifndef('extruder', $extruder_id); $config->set_ifndef('extruder', $extruder_id);

View File

@ -10,6 +10,7 @@ use Slic3r::Print::State ':steps';
use Slic3r::Surface ':types'; use Slic3r::Surface ':types';
has 'print' => (is => 'ro', weak_ref => 1, required => 1); has 'print' => (is => 'ro', weak_ref => 1, required => 1);
has 'model' => (is => 'ro', required => 1); # for strong ref so model subobjects are kept in memory
has 'model_object' => (is => 'ro', required => 1); has 'model_object' => (is => 'ro', required => 1);
has 'region_volumes' => (is => 'rw', default => sub { [] }); # by region_id has 'region_volumes' => (is => 'rw', default => sub { [] }); # by region_id
has 'copies' => (is => 'ro'); # Slic3r::Point objects in scaled G-code coordinates has 'copies' => (is => 'ro'); # Slic3r::Point objects in scaled G-code coordinates

View File

@ -51,8 +51,8 @@ sub set_model {
# apply scaling and rotation supplied from command line if any # apply scaling and rotation supplied from command line if any
foreach my $instance (map @{$_->instances}, @{$model->objects}) { foreach my $instance (map @{$_->instances}, @{$model->objects}) {
$instance->scaling_factor($instance->scaling_factor * $self->scale); $instance->set_scaling_factor($instance->scaling_factor * $self->scale);
$instance->rotation($instance->rotation + $self->rotate); $instance->set_rotation($instance->rotation + $self->rotate);
} }
if ($self->duplicate_grid->[X] > 1 || $self->duplicate_grid->[Y] > 1) { if ($self->duplicate_grid->[X] > 1 || $self->duplicate_grid->[Y] > 1) {
@ -67,7 +67,7 @@ sub set_model {
foreach my $model_object (@{$model->objects}) { foreach my $model_object (@{$model->objects}) {
$self->_print->auto_assign_extruders($model_object); $self->_print->auto_assign_extruders($model_object);
$self->_print->add_model_object($model_object); $self->_print->add_model_object($model, $model_object);
} }
} }

View File

@ -151,7 +151,7 @@ sub init_print {
$model->center_instances_around_point($print->config->print_center); $model->center_instances_around_point($print->config->print_center);
foreach my $model_object (@{$model->objects}) { foreach my $model_object (@{$model->objects}) {
$print->auto_assign_extruders($model_object); $print->auto_assign_extruders($model_object);
$print->add_model_object($model_object); $print->add_model_object($model, $model_object);
} }
} }
$print->validate; $print->validate;

View File

@ -1673,6 +1673,7 @@ src/Geometry.hpp
src/Layer.hpp src/Layer.hpp
src/Line.cpp src/Line.cpp
src/Line.hpp src/Line.hpp
src/Model.cpp
src/Model.hpp src/Model.hpp
src/MultiPoint.cpp src/MultiPoint.cpp
src/MultiPoint.hpp src/MultiPoint.hpp
@ -1705,6 +1706,8 @@ src/Print.cpp
src/Print.hpp src/Print.hpp
src/PrintConfig.cpp src/PrintConfig.cpp
src/PrintConfig.hpp src/PrintConfig.hpp
src/StringMap.cpp
src/StringMap.hpp
src/Surface.cpp src/Surface.cpp
src/Surface.hpp src/Surface.hpp
src/SurfaceCollection.cpp src/SurfaceCollection.cpp
@ -1742,6 +1745,7 @@ xsp/ExtrusionPath.xsp
xsp/Flow.xsp xsp/Flow.xsp
xsp/Geometry.xsp xsp/Geometry.xsp
xsp/Line.xsp xsp/Line.xsp
xsp/Model.xsp
xsp/my.map xsp/my.map
xsp/mytype.map xsp/mytype.map
xsp/Point.xsp xsp/Point.xsp
@ -1749,6 +1753,7 @@ xsp/Polygon.xsp
xsp/Polyline.xsp xsp/Polyline.xsp
xsp/PolylineCollection.xsp xsp/PolylineCollection.xsp
xsp/Print.xsp xsp/Print.xsp
xsp/StringMap.xsp
xsp/Surface.xsp xsp/Surface.xsp
xsp/SurfaceCollection.xsp xsp/SurfaceCollection.xsp
xsp/TriangleMesh.xsp xsp/TriangleMesh.xsp

View File

@ -13,31 +13,16 @@ use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
'fallback' => 1; 'fallback' => 1;
package Slic3r::Line::Ref;
our @ISA = 'Slic3r::Line';
sub DESTROY {}
package Slic3r::Point; package Slic3r::Point;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
'fallback' => 1; 'fallback' => 1;
package Slic3r::Point::Ref;
our @ISA = 'Slic3r::Point';
sub DESTROY {}
package Slic3r::Pointf; package Slic3r::Pointf;
use overload use overload
'@{}' => sub { [ $_[0]->x, $_[0]->y ] }, #, '@{}' => sub { [ $_[0]->x, $_[0]->y ] }, #,
'fallback' => 1; 'fallback' => 1;
package Slic3r::Pointf::Ref;
our @ISA = 'Slic3r::Pointf';
sub DESTROY {}
package Slic3r::Pointf3; package Slic3r::Pointf3;
use overload use overload
'@{}' => sub { [ $_[0]->x, $_[0]->y, $_[0]->z ] }, #, '@{}' => sub { [ $_[0]->x, $_[0]->y, $_[0]->z ] }, #,
@ -48,21 +33,11 @@ use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
'fallback' => 1; 'fallback' => 1;
package Slic3r::ExPolygon::Ref;
our @ISA = 'Slic3r::ExPolygon';
sub DESTROY {}
package Slic3r::Polyline; package Slic3r::Polyline;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
'fallback' => 1; 'fallback' => 1;
package Slic3r::Polyline::Ref;
our @ISA = 'Slic3r::Polyline';
sub DESTROY {}
package Slic3r::Polyline::Collection; package Slic3r::Polyline::Collection;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
@ -73,11 +48,6 @@ use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
'fallback' => 1; 'fallback' => 1;
package Slic3r::Polygon::Ref;
our @ISA = 'Slic3r::Polygon';
sub DESTROY {}
package Slic3r::ExPolygon::Collection; package Slic3r::ExPolygon::Collection;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
@ -96,11 +66,6 @@ sub new {
return $self; return $self;
} }
package Slic3r::ExtrusionPath::Collection::Ref;
our @ISA = 'Slic3r::ExtrusionPath::Collection';
sub DESTROY {}
package Slic3r::ExtrusionLoop; package Slic3r::ExtrusionLoop;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
@ -130,11 +95,6 @@ sub clone {
); );
} }
package Slic3r::ExtrusionLoop::Ref;
our @ISA = 'Slic3r::ExtrusionLoop';
sub DESTROY {}
package Slic3r::ExtrusionPath; package Slic3r::ExtrusionPath;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
@ -164,11 +124,6 @@ sub clone {
); );
} }
package Slic3r::ExtrusionPath::Ref;
our @ISA = 'Slic3r::ExtrusionPath';
sub DESTROY {}
package Slic3r::Flow; package Slic3r::Flow;
sub new { sub new {
@ -229,14 +184,53 @@ sub clone {
); );
} }
package Slic3r::Surface::Ref;
our @ISA = 'Slic3r::Surface';
sub DESTROY {}
package Slic3r::Surface::Collection; package Slic3r::Surface::Collection;
use overload use overload
'@{}' => sub { $_[0]->arrayref }, '@{}' => sub { $_[0]->arrayref },
'fallback' => 1; 'fallback' => 1;
package Slic3r::StringMap;
sub to_hash {
my $self = shift;
my %tiehash;
tie %tiehash, 'Slic3r::StringMap', $self;
return \%tiehash;
}
sub TIEHASH {
my ($class, $self) = @_;
return $self;
}
package main;
for my $class (qw(
Slic3r::Config
Slic3r::Config::Print
Slic3r::ExPolygon
Slic3r::ExtrusionLoop
Slic3r::ExtrusionPath
Slic3r::ExtrusionPath::Collection
Slic3r::Geometry::BoundingBoxf3
Slic3r::Line
Slic3r::Model
Slic3r::Model::Instance
Slic3r::Model::Material
Slic3r::Model::Object
Slic3r::Model::Volume
Slic3r::Point
Slic3r::Pointf
Slic3r::Pointf3
Slic3r::Polygon
Slic3r::Polyline
Slic3r::StringMap
Slic3r::Surface
Slic3r::TriangleMesh
))
{
no strict 'refs';
my $ref_class = $class . "::Ref";
eval "package $ref_class; our \@ISA = '$class'; sub DESTROY {};";
}
1; 1;

343
xs/src/Model.cpp Normal file
View File

@ -0,0 +1,343 @@
#include "Model.hpp"
#ifdef SLIC3RXS
#include "perlglue.hpp"
#endif
namespace Slic3r {
Model::Model()
{
}
Model::Model(const Model &other)
: materials(),
objects()
{
objects.reserve(other.objects.size());
for (ModelMaterialMap::const_iterator i = other.materials.begin();
i != other.materials.end(); ++i)
{
ModelMaterial *copy = new ModelMaterial(*i->second);
copy->model = this;
materials[i->first] = copy;
}
for (ModelObjectPtrs::const_iterator i = other.objects.begin();
i != other.objects.end(); ++i)
{
ModelObject *copy = new ModelObject(**i);
copy->model = this;
objects.push_back(copy);
}
}
Model::~Model()
{
delete_all_objects();
delete_all_materials();
}
ModelObject *
Model::add_object(std::string input_file, DynamicPrintConfig *config,
t_layer_height_ranges layer_height_ranges, Point origin_translation)
{
ModelObject *object = new ModelObject(this, input_file, config,
layer_height_ranges, origin_translation);
this->objects.push_back(object);
return object;
}
void
Model::delete_object(size_t idx)
{
ModelObjectPtrs::iterator i = this->objects.begin() + idx;
delete *i;
this->objects.erase(i);
}
void
Model::delete_all_objects()
{
for (ModelObjectPtrs::iterator i = this->objects.begin();
i != this->objects.end(); ++i)
{
delete *i;
}
objects.clear();
}
void
Model::delete_all_materials()
{
for (ModelMaterialMap::iterator i = this->materials.begin();
i != this->materials.end(); ++i)
{
delete i->second;
}
this->materials.clear();
}
ModelMaterial *
Model::set_material(t_model_material_id material_id,
const t_model_material_attributes &attributes)
{
ModelMaterialMap::iterator i = this->materials.find(material_id);
ModelMaterial *mat;
if (i == this->materials.end()) {
mat = this->materials[material_id] = new ModelMaterial(this);
} else {
mat = i->second;
}
mat->apply(attributes);
return mat;
}
/*
void
Model::duplicate_objects_grid(unsigned int x, unsigned int y, coordf_t distance)
{
if (this->objects.size() > 1) throw "Grid duplication is not supported with multiple objects";
if (this->objects.empty()) throw "No objects!";
ModelObject* object = this->objects.front();
object->clear_instances();
BoundingBoxf3 bb;
object->bounding_box(&bb);
Sizef3 size = bb.size();
for (unsigned int x_copy = 1; x_copy <= x; ++x_copy) {
for (unsigned int y_copy = 1; y_copy <= y; ++y_copy) {
ModelInstance* instance = object->add_instance();
instance->offset.x = (size.x + distance) * (x_copy-1);
instance->offset.y = (size.y + distance) * (y_copy-1);
}
}
}
*/
bool
Model::has_objects_with_no_instances() const
{
for (ModelObjectPtrs::const_iterator i = this->objects.begin();
i != this->objects.end(); ++i)
{
if ((*i)->instances.empty()) {
return true;
}
}
return false;
}
#ifdef SLIC3RXS
REGISTER_CLASS(Model, "Model");
#endif
ModelMaterial::ModelMaterial(Model *model)
: model(model),
attributes(),
config()
{
}
void
ModelMaterial::apply(const t_model_material_attributes &attributes)
{
this->attributes.insert(attributes.begin(), attributes.end());
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelMaterial, "Model::Material");
#endif
ModelObject::ModelObject(Model *model, std::string input_file,
DynamicPrintConfig *config, t_layer_height_ranges layer_height_ranges,
Point origin_translation)
: model(model),
input_file(input_file),
config(*config),
layer_height_ranges(layer_height_ranges),
origin_translation(origin_translation),
_bounding_box_valid(false)
{
}
ModelObject::ModelObject(const ModelObject &other)
: model(other.model),
input_file(other.input_file),
instances(),
volumes(),
config(other.config),
layer_height_ranges(other.layer_height_ranges),
origin_translation(other.origin_translation),
_bounding_box(other._bounding_box),
_bounding_box_valid(other._bounding_box_valid)
{
volumes.reserve(other.volumes.size());
instances.reserve(other.instances.size());
for (ModelVolumePtrs::const_iterator i = other.volumes.begin();
i != other.volumes.end(); ++i)
{
ModelVolume *v = new ModelVolume(**i);
v->object = this;
volumes.push_back(v);
}
for (ModelInstancePtrs::const_iterator i = other.instances.begin();
i != other.instances.end(); ++i)
{
ModelInstance *in = new ModelInstance(**i);
in->object = this;
instances.push_back(in);
}
}
ModelObject::~ModelObject()
{
this->clear_volumes();
this->clear_instances();
}
ModelVolume *
ModelObject::add_volume(t_model_material_id material_id,
TriangleMesh *mesh, bool modifier)
{
ModelVolume *v = new ModelVolume(this, material_id, mesh, modifier);
this->volumes.push_back(v);
this->invalidate_bounding_box();
return v;
}
void
ModelObject::delete_volume(size_t idx)
{
ModelVolumePtrs::iterator i = this->volumes.begin() + idx;
delete *i;
this->volumes.erase(i);
this->invalidate_bounding_box();
}
void
ModelObject::clear_volumes()
{
for (ModelVolumePtrs::iterator i = this->volumes.begin();
i != this->volumes.end(); ++i)
{
delete *i;
}
this->volumes.clear();
}
ModelInstance *
ModelObject::add_instance(double rotation, double scaling_factor,
Pointf offset)
{
ModelInstance *i = new ModelInstance(
this, rotation, scaling_factor, offset);
this->instances.push_back(i);
this->invalidate_bounding_box();
return i;
}
void
ModelObject::delete_last_instance()
{
delete this->instances.back();
this->instances.pop_back();
this->invalidate_bounding_box();
}
void
ModelObject::clear_instances()
{
for (ModelInstancePtrs::iterator i = this->instances.begin();
i != this->instances.end(); ++i)
{
delete *i;
}
this->instances.clear();
}
void
ModelObject::invalidate_bounding_box()
{
this->_bounding_box_valid = false;
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelObject, "Model::Object");
SV*
ModelObject::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, perl_class_name_ref(this), this );
return sv;
}
#endif
ModelVolume::ModelVolume(ModelObject *object, t_model_material_id material_id,
TriangleMesh *mesh, bool modifier)
: object(object),
material_id(material_id),
mesh(*mesh),
modifier(modifier)
{
}
ModelVolume::~ModelVolume()
{
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelVolume, "Model::Volume");
SV*
ModelVolume::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, perl_class_name_ref(this), this );
return sv;
}
#endif
ModelInstance::ModelInstance(ModelObject *object, double rotation,
double scaling_factor, Pointf offset)
: object(object),
rotation(rotation),
scaling_factor(scaling_factor),
offset(offset)
{
}
ModelInstance::~ModelInstance()
{
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelInstance, "Model::Instance");
SV*
ModelInstance::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, perl_class_name_ref(this), this );
return sv;
}
#endif
}

View File

@ -2,7 +2,7 @@
#define slic3r_Model_hpp_ #define slic3r_Model_hpp_
#include <myinit.h> #include <myinit.h>
#include "Config.hpp" #include "PrintConfig.hpp"
#include "Layer.hpp" #include "Layer.hpp"
#include "Point.hpp" #include "Point.hpp"
#include "TriangleMesh.hpp" #include "TriangleMesh.hpp"
@ -22,30 +22,40 @@ typedef std::string t_model_material_id;
typedef std::string t_model_material_attribute; typedef std::string t_model_material_attribute;
typedef std::map<t_model_material_attribute,std::string> t_model_material_attributes; typedef std::map<t_model_material_attribute,std::string> t_model_material_attributes;
typedef std::map<t_model_material_id,ModelMaterial*> ModelMaterialMap;
typedef std::vector<ModelObject*> ModelObjectPtrs;
typedef std::vector<ModelVolume*> ModelVolumePtrs;
typedef std::vector<ModelInstance*> ModelInstancePtrs;
class Model class Model
{ {
public: public:
std::map<t_model_material_id,ModelMaterial*> materials; ModelMaterialMap materials;
std::vector<ModelObject*> objects; ModelObjectPtrs objects;
Model(); Model();
Model(const Model &other);
~Model(); ~Model();
ModelObject* add_object(const ModelObject &object); ModelObject *add_object(std::string input_file, DynamicPrintConfig *config,
void delete_object(size_t obj_idx); t_layer_height_ranges layer_height_ranges, Point origin_translation);
void delete_object(size_t idx);
void delete_all_objects(); void delete_all_objects();
void set_material(t_model_material_id material_id, const t_model_material_attributes &attributes); void delete_all_materials();
void duplicate_objects_grid(coordf_t x, coordf_t y, coordf_t distance); ModelMaterial *set_material(t_model_material_id material_id,
void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBox &bb); const t_model_material_attributes &attributes);
void arrange_objects(coordf_t distance, const BoundingBox &bb); // void duplicate_objects_grid(unsigned int x, unsigned int y, coordf_t distance);
void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb); // void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBox &bb);
// void arrange_objects(coordf_t distance, const BoundingBox &bb);
// void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb);
bool has_objects_with_no_instances() const; bool has_objects_with_no_instances() const;
void bounding_box(BoundingBox* bb) const; // void bounding_box(BoundingBox* bb) const;
void align_to_origin(); // void align_to_origin();
void center_instances_around_point(const Pointf &point); // void center_instances_around_point(const Pointf &point);
void translate(coordf_t x, coordf_t y, coordf_t z); // void translate(coordf_t x, coordf_t y, coordf_t z);
void mesh(TriangleMesh* mesh) const; // void mesh(TriangleMesh* mesh) const;
void split_meshes(); // void split_meshes();
std::string get_material_name(t_model_material_id material_id); // std::string get_material_name(t_model_material_id material_id);
private: private:
void _arrange(const std::vector<Size> &sizes, coordf_t distance, const BoundingBox &bb) const; void _arrange(const std::vector<Size> &sizes, coordf_t distance, const BoundingBox &bb) const;
@ -56,7 +66,10 @@ class ModelMaterial
public: public:
Model* model; Model* model;
t_model_material_attributes attributes; t_model_material_attributes attributes;
DynamicConfig config; DynamicPrintConfig config;
ModelMaterial(Model *model);
void apply(const t_model_material_attributes &attributes);
}; };
class ModelObject class ModelObject
@ -64,16 +77,31 @@ class ModelObject
public: public:
Model* model; Model* model;
std::string input_file; std::string input_file;
std::vector<ModelInstance*> instances; ModelInstancePtrs instances;
std::vector<ModelVolume*> volumes; ModelVolumePtrs volumes;
DynamicConfig config; DynamicPrintConfig config;
t_layer_height_ranges layer_height_ranges; t_layer_height_ranges layer_height_ranges;
Point origin_translation;
BoundingBoxf3 _bounding_box;
bool _bounding_box_valid;
ModelObject(); ModelObject(Model *model, std::string input_file, DynamicPrintConfig *config,
t_layer_height_ranges layer_height_ranges, Point origin_translation);
ModelObject(const ModelObject &other);
~ModelObject(); ~ModelObject();
ModelInstance* add_instance(const ModelInstance &instance);
ModelVolume* add_volume(const ModelVolume &volume); ModelVolume *add_volume(t_model_material_id material_id,
TriangleMesh *mesh, bool modifier);
void delete_volume(size_t idx);
void clear_volumes();
ModelInstance *add_instance(double rotation=0, double scaling_factor=1,
Pointf offset=Pointf(0, 0));
void delete_last_instance(); void delete_last_instance();
void clear_instances();
void invalidate_bounding_box();
void raw_mesh(TriangleMesh* mesh) const; void raw_mesh(TriangleMesh* mesh) const;
void mesh(TriangleMesh* mesh) const; void mesh(TriangleMesh* mesh) const;
void instance_bounding_box(size_t instance_idx, BoundingBox* bb) const; void instance_bounding_box(size_t instance_idx, BoundingBox* bb) const;
@ -84,8 +112,11 @@ class ModelObject
size_t facets_count() const; size_t facets_count() const;
bool needed_repair() const; bool needed_repair() const;
#ifdef SLIC3RXS
SV* to_SV_ref();
#endif
private: private:
BoundingBox bb;
void update_bounding_box(); void update_bounding_box();
}; };
@ -95,18 +126,35 @@ class ModelVolume
ModelObject* object; ModelObject* object;
t_model_material_id material_id; t_model_material_id material_id;
TriangleMesh mesh; TriangleMesh mesh;
bool modifier;
ModelVolume(ModelObject *object, t_model_material_id material_id,
TriangleMesh *mesh, bool modifier);
~ModelVolume();
#ifdef SLIC3RXS
SV* to_SV_ref();
#endif
}; };
class ModelInstance class ModelInstance
{ {
public: public:
ModelObject* object; ModelObject* object;
double rotation; double rotation; // around mesh center point
double scaling_factor; double scaling_factor;
Pointf offset; Pointf offset; // in unscaled coordinates
ModelInstance(ModelObject *object, double rotation, double scaling_factor,
Pointf offset);
~ModelInstance();
void transform_mesh(TriangleMesh* mesh, bool dont_translate) const; void transform_mesh(TriangleMesh* mesh, bool dont_translate) const;
void transform_polygon(Polygon* polygon) const; void transform_polygon(Polygon* polygon) const;
#ifdef SLIC3RXS
SV* to_SV_ref();
#endif
}; };
} }

13
xs/src/StringMap.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "StringMap.hpp"
#ifdef SLIC3RXS
#include "perlglue.hpp"
#endif
namespace Slic3r {
#ifdef SLIC3RXS
__REGISTER_CLASS(StringMap, "StringMap");
#endif
}

16
xs/src/StringMap.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef slic3r_StringMap_hpp_
#define slic3r_StringMap_hpp_
#include <myinit.h>
#include <map>
#include <string>
namespace Slic3r {
// this is just for XSPP, because it chokes on the template typename
typedef std::map<std::string, std::string> StringMap;
}
#endif

View File

@ -8,12 +8,17 @@ struct ClassTraits {
static const char* name; static const char* name;
static const char* name_ref; static const char* name_ref;
}; };
#define REGISTER_CLASS(cname,perlname) \ // use this for typedefs for which the forward prototype
class cname; \ // in REGISTER_CLASS won't work
#define __REGISTER_CLASS(cname, perlname) \
template <>const char* ClassTraits<cname>::name = "Slic3r::" perlname; \ template <>const char* ClassTraits<cname>::name = "Slic3r::" perlname; \
template <>const char* ClassTraits<cname>::name_ref = "Slic3r::" perlname "::Ref"; template <>const char* ClassTraits<cname>::name_ref = "Slic3r::" perlname "::Ref";
#define REGISTER_CLASS(cname,perlname) \
class cname; \
__REGISTER_CLASS(cname, perlname);
template<class T> template<class T>
const char* perl_class_name(const T*) { return ClassTraits<T>::name; } const char* perl_class_name(const T*) { return ClassTraits<T>::name; }
template<class T> template<class T>

216
xs/xsp/Model.xsp Normal file
View File

@ -0,0 +1,216 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "Model.hpp"
#include "PrintConfig.hpp"
#include "StringMap.hpp"
#include "perlglue.hpp"
%}
%name{Slic3r::Model} class Model {
Model();
~Model();
Clone<Model> clone()
%code%{ RETVAL = THIS; %};
Ref<ModelObject> _add_object(std::string input_file,
DynamicPrintConfig *config,
t_layer_height_ranges layer_height_ranges,
Point *origin_translation)
%code%{
RETVAL = THIS->add_object(input_file, config, layer_height_ranges,
*origin_translation);
%};
void delete_object(size_t idx);
void delete_all_objects();
void delete_all_materials();
%name{_set_material} Ref<ModelMaterial> set_material(t_model_material_id material_id,
StringMap attributes)
%code%{ RETVAL = THIS->set_material(material_id, attributes); %};
Ref<ModelMaterial> get_material(t_model_material_id material_id)
%code%{
ModelMaterialMap::iterator i = THIS->materials.find(material_id);
if (i == THIS->materials.end()) {
XSRETURN_UNDEF;
}
RETVAL = i->second;
%};
bool has_material(t_model_material_id material_id) const
%code%{
RETVAL = (THIS->materials.find(material_id) != THIS->materials.end());
%};
std::vector<std::string> material_names() const
%code%{
for (ModelMaterialMap::iterator i = THIS->materials.begin();
i != THIS->materials.end(); ++i)
{
RETVAL.push_back(i->first);
}
%};
size_t material_count() const
%code%{ RETVAL = THIS->materials.size(); %};
// void duplicate_objects_grid(coordf_t x, coordf_t y, coordf_t distance);
// void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBox &bb);
// void arrange_objects(coordf_t distance, const BoundingBox &bb);
// void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb);
bool has_objects_with_no_instances() const;
// void bounding_box(BoundingBox* bb) const;
// void center_instances_around_point(const Pointf &point);
// void align_instances_to_origin();
// void translate(coordf_t x, coordf_t y, coordf_t z);
// void mesh(TriangleMesh* mesh) const;
// void split_meshes();
// std::string get_material_name(t_model_material_id material_id);
ModelObjectPtrs *objects()
%code%{
if (THIS->objects.empty()) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->objects;
%};
};
%name{Slic3r::Model::Material} class ModelMaterial {
~ModelMaterial();
Ref<Model> model()
%code%{ RETVAL = THIS->model; %};
Ref<StringMap> _attributes()
%code%{ RETVAL = &THIS->attributes; %};
Ref<DynamicPrintConfig> config()
%code%{ RETVAL = &THIS->config; %};
};
%name{Slic3r::Model::Object} class ModelObject {
ModelObject(Model *model, std::string input_file,
DynamicPrintConfig *config, t_layer_height_ranges layer_height_ranges,
Point *origin_translation)
%code%{
RETVAL = new ModelObject(model, input_file, config,
layer_height_ranges, *origin_translation);
%};
~ModelObject();
ModelVolumePtrs *volumes()
%code%{
if (THIS->volumes.empty()) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->volumes;
%};
ModelInstancePtrs *instances()
%code%{
if (THIS->instances.empty()) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->instances;
%};
void invalidate_bounding_box();
Ref<BoundingBoxf3> _bounding_box(BoundingBoxf3 *new_bbox = NULL)
%code{%
if (NULL != new_bbox) {
THIS->_bounding_box = *new_bbox;
THIS->_bounding_box_valid = true;
}
if (!THIS->_bounding_box_valid) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->_bounding_box;
%};
%name{_add_volume} Ref<ModelVolume> add_volume(
t_model_material_id material_id, TriangleMesh *mesh, bool modifier)
%code%{ RETVAL = THIS->add_volume(material_id, mesh, modifier); %};
void delete_volume(size_t idx);
void clear_volumes();
%name{_add_instance} Ref<ModelInstance> add_instance(
double rotation, double scaling_factor, std::vector<double> offset)
%code%{
RETVAL = THIS->add_instance(rotation, scaling_factor,
Pointf(offset[0], offset[1]));
%};
void delete_last_instance();
void clear_instances();
std::string input_file()
%code%{ RETVAL = THIS->input_file; %};
void set_input_file(std::string value)
%code%{ THIS->input_file = value; %};
Ref<DynamicPrintConfig> config()
%code%{ RETVAL = &THIS->config; %};
Ref<Model> model()
%code%{ RETVAL = THIS->model; %};
t_layer_height_ranges layer_height_ranges()
%code%{ RETVAL = THIS->layer_height_ranges; %};
Clone<Point> origin_translation()
%code%{ RETVAL = THIS->origin_translation; %};
};
%name{Slic3r::Model::Volume} class ModelVolume {
~ModelVolume();
Ref<ModelObject> object()
%code%{ RETVAL = THIS->object; %};
t_model_material_id material_id()
%code%{ RETVAL = THIS->material_id; %};
Ref<TriangleMesh> mesh()
%code%{ RETVAL = &THIS->mesh; %};
bool modifier()
%code%{ RETVAL = THIS->modifier; %};
};
%name{Slic3r::Model::Instance} class ModelInstance {
~ModelInstance();
Ref<ModelObject> object()
%code%{ RETVAL = THIS->object; %};
double rotation()
%code%{ RETVAL = THIS->rotation; %};
double scaling_factor()
%code%{ RETVAL = THIS->scaling_factor; %};
Clone<Pointf> offset()
%code%{ RETVAL = THIS->offset; %};
void set_rotation(double val)
%code%{ THIS->rotation = val; %};
void set_scaling_factor(double val)
%code%{ THIS->scaling_factor = val; %};
void set_offset(Pointf *offset)
%code%{ THIS->offset = *offset; %};
};

59
xs/xsp/StringMap.xsp Normal file
View File

@ -0,0 +1,59 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "StringMap.hpp"
#include "perlglue.hpp"
%}
%name{Slic3r::StringMap} class StringMap {
std::string FETCH(std::string key)
%code%{
StringMap::iterator i = THIS->find(key);
if (i == THIS->end()) {
XSRETURN_UNDEF;
}
RETVAL = i->second;
%};
void STORE(std::string key, std::string val)
%code%{ (*THIS)[key] = val; %};
void DELETE(std::string key)
%code%{ THIS->erase(key); %};
void CLEAR()
%code%{ THIS->clear(); %};
bool EXISTS(std::string key)
%code%{ RETVAL = (THIS->find(key) != THIS->end()); %};
std::string FIRSTKEY()
%code%{
StringMap::iterator i = THIS->begin();
if (i == THIS->end()) {
XSRETURN_UNDEF;
} else {
RETVAL = i->first;
}
%};
std::string NEXTKEY(std::string lastkey)
%code%{
StringMap::iterator i = THIS->find(lastkey);
if (i == THIS->end()) {
XSRETURN_UNDEF;
}
++i;
if (i == THIS->end()) {
XSRETURN_UNDEF;
} else {
RETVAL = i->first;
}
%};
bool SCALAR()
%code%{ RETVAL = !THIS->empty(); %};
};

View File

@ -1,6 +1,17 @@
coordf_t T_NV
std::vector<Points::size_type> T_STD_VECTOR_INT std::vector<Points::size_type> T_STD_VECTOR_INT
std::vector<size_t> T_STD_VECTOR_INT std::vector<size_t> T_STD_VECTOR_INT
t_config_option_key T_STD_STRING t_config_option_key T_STD_STRING
t_model_material_id T_STD_STRING
t_layer_height_ranges T_LAYER_HEIGHT_RANGES
StringMap T_STRING_MAP
StringMap* O_OBJECT_SLIC3R
Ref<StringMap> O_OBJECT_SLIC3R_T
BoundingBox* O_OBJECT_SLIC3R BoundingBox* O_OBJECT_SLIC3R
Ref<BoundingBox> O_OBJECT_SLIC3R_T Ref<BoundingBox> O_OBJECT_SLIC3R_T
@ -11,6 +22,7 @@ Ref<BoundingBoxf3> O_OBJECT_SLIC3R_T
Clone<BoundingBoxf3> O_OBJECT_SLIC3R_T Clone<BoundingBoxf3> O_OBJECT_SLIC3R_T
DynamicPrintConfig* O_OBJECT_SLIC3R DynamicPrintConfig* O_OBJECT_SLIC3R
Ref<DynamicPrintConfig> O_OBJECT_SLIC3R_T
PrintObjectConfig* O_OBJECT_SLIC3R PrintObjectConfig* O_OBJECT_SLIC3R
PrintRegionConfig* O_OBJECT_SLIC3R PrintRegionConfig* O_OBJECT_SLIC3R
PrintConfig* O_OBJECT_SLIC3R PrintConfig* O_OBJECT_SLIC3R
@ -83,6 +95,27 @@ SurfaceCollection* O_OBJECT_SLIC3R
Extruder* O_OBJECT_SLIC3R Extruder* O_OBJECT_SLIC3R
Model* O_OBJECT_SLIC3R
Ref<Model> O_OBJECT_SLIC3R_T
Clone<Model> O_OBJECT_SLIC3R_T
ModelMaterial* O_OBJECT_SLIC3R
Ref<ModelMaterial> O_OBJECT_SLIC3R_T
Clone<ModelMaterial> O_OBJECT_SLIC3R_T
ModelObject* O_OBJECT_SLIC3R
Ref<ModelObject> O_OBJECT_SLIC3R_T
Clone<ModelObject> O_OBJECT_SLIC3R_T
ModelVolume* O_OBJECT_SLIC3R
Ref<ModelVolume> O_OBJECT_SLIC3R_T
Clone<ModelVolume> O_OBJECT_SLIC3R_T
ModelInstance* O_OBJECT_SLIC3R
Ref<ModelInstance> O_OBJECT_SLIC3R_T
Clone<ModelInstance> O_OBJECT_SLIC3R_T
ExtrusionRole T_UV ExtrusionRole T_UV
FlowRole T_UV FlowRole T_UV
PrintStep T_UV PrintStep T_UV
@ -103,12 +136,16 @@ Surfaces T_ARRAYREF
# by reference and marked ::Ref because they're contained in another # by reference and marked ::Ref because they're contained in another
# Perl object # Perl object
Polygons* T_ARRAYREF_PTR Polygons* T_ARRAYREF_PTR
ModelObjectPtrs* T_PTR_ARRAYREF_PTR
ModelVolumePtrs* T_PTR_ARRAYREF_PTR
ModelInstancePtrs* T_PTR_ARRAYREF_PTR
# we return these types whenever we want the items to be returned # we return these types whenever we want the items to be returned
# by reference and not marked ::Ref because they're newly allocated # by reference and not marked ::Ref because they're newly allocated
# and not referenced by any Perl object # and not referenced by any Perl object
TriangleMeshPtrs T_PTR_ARRAYREF TriangleMeshPtrs T_PTR_ARRAYREF
INPUT INPUT
O_OBJECT_SLIC3R O_OBJECT_SLIC3R
@ -140,6 +177,75 @@ T_ARRAYREF
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\"); \"$var\");
# http://www.perlmonks.org/?node_id=853194
T_STRING_MAP
{
HV *hv;
HE *he;
std::map<std::string, std::string> t_sm;
if(SvROK($arg) && SvTYPE(SvRV($arg)) == SVt_PVHV) {
hv = (HV *)SvRV($arg);
hv_iterinit(hv);
} else {
warn(\"${Package}::$func_name() -- $var is not a hash reference\");
XSRETURN_UNDEF;
}
while((he = hv_iternext(hv)) != NULL) {
SV *svkey = HeSVKEY_force(he);
SV *svval = HeVAL(he);
t_sm[SvPV_nolen(svkey)] = SvPV_nolen(svval);
}
$var = t_sm;
}
T_LAYER_HEIGHT_RANGES
{
if (!SvROK($arg) || SvTYPE(SvRV($arg)) != SVt_PVAV) {
Perl_croak(aTHX_ \"%s: %s is not an array reference\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
}
AV* av = (AV*)SvRV($arg);
const unsigned int len = av_len(av)+1;
t_layer_height_ranges tmp_ranges;
for (unsigned int i = 0; i < len; i++) {
SV* elem = *av_fetch(av, i, 0);
if (!SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVAV) {
Perl_croak(
aTHX_ \"%s: %s contains something that is not an array reference\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
}
AV* elemAV = (AV*)SvRV(elem);
if (av_len(elemAV) + 1 != 3) {
Perl_croak(
aTHX_ \"%s: %s contains an array that isn't 3 elements long\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
}
coordf_t vals[3];
for (unsigned int j = 0; j < 3; ++j) {
SV *elem_elem = *av_fetch(elemAV, j, 0);
if (!SvNOK(elem_elem)) {
Perl_croak(
aTHX_ \"%s: layer ranges and heights must be numbers\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]});
}
vals[j] = SvNV(elem_elem);
}
tmp_ranges[t_layer_height_range(vals[0], vals[1])] = vals[2];
}
$var = tmp_ranges;
}
OUTPUT OUTPUT
# return object from pointer # return object from pointer
O_OBJECT_SLIC3R O_OBJECT_SLIC3R
@ -171,6 +277,16 @@ T_ARRAYREF_PTR
av_store(av, i++, it->to_SV_ref()); av_store(av, i++, it->to_SV_ref());
} }
T_PTR_ARRAYREF_PTR
AV* av = newAV();
$arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
av_extend(av, $var->size()-1);
int i = 0;
for (${ my $t = $type; $t =~ s/\*$//; \$t }::iterator it = $var->begin(); it != $var->end(); ++it) {
av_store(av, i++, (*it)->to_SV_ref());
}
T_PTR_ARRAYREF T_PTR_ARRAYREF
AV* av = newAV(); AV* av = newAV();
$arg = newRV_noinc((SV*)av); $arg = newRV_noinc((SV*)av);
@ -180,3 +296,30 @@ T_PTR_ARRAYREF
for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) { for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) {
av_store(av, i++, (*it)->to_SV()); av_store(av, i++, (*it)->to_SV());
} }
T_LAYER_HEIGHT_RANGES
AV* av = newAV();
$arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
av_extend(av, $var.size() - 1);
// map is sorted, so we can just copy it in order
int i = 0;
for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) {
const coordf_t range_values[] = {
it->first.first, // key's first = minz
it->first.second, // key's second = maxz
it->second, // value = height
};
AV *rangeAV = newAV();
SV *rangeAV_ref = newRV_noinc((SV*)rangeAV);
sv_2mortal(rangeAV_ref);
av_extend(rangeAV, 2);
for (int j = 0; j < 3; ++j) {
SV *val = sv_newmortal();
sv_setnv(val, range_values[j]);
av_store(rangeAV, j, val);
}
av_store(av, i++, (SV*)rangeAV_ref);
}

View File

@ -1,12 +1,19 @@
%typemap{bool}{simple}; %typemap{bool}{simple};
%typemap{size_t}{simple};
%typemap{coordf_t}{simple};
%typemap{std::string}; %typemap{std::string};
%typemap{t_config_option_key}; %typemap{t_config_option_key};
%typemap{t_model_material_id};
%typemap{std::vector<int>}; %typemap{std::vector<int>};
%typemap{std::vector<size_t>}; %typemap{std::vector<size_t>};
%typemap{std::vector<unsigned int>*}; %typemap{std::vector<unsigned int>*};
%typemap{std::vector<double>}; %typemap{std::vector<double>};
%typemap{std::vector<double>*}; %typemap{std::vector<double>*};
%typemap{std::vector<std::string>}; %typemap{std::vector<std::string>};
%typemap{t_layer_height_ranges};
%typemap{StringMap};
%typemap{StringMap*};
%typemap{Ref<StringMap>}{simple};
%typemap{SV*}; %typemap{SV*};
%typemap{AV*}; %typemap{AV*};
%typemap{Point*}; %typemap{Point*};
@ -25,6 +32,7 @@
%typemap{Ref<BoundingBoxf3>}{simple}; %typemap{Ref<BoundingBoxf3>}{simple};
%typemap{Clone<BoundingBoxf3>}{simple}; %typemap{Clone<BoundingBoxf3>}{simple};
%typemap{DynamicPrintConfig*}; %typemap{DynamicPrintConfig*};
%typemap{Ref<DynamicPrintConfig>}{simple};
%typemap{PrintObjectConfig*}; %typemap{PrintObjectConfig*};
%typemap{PrintRegionConfig*}; %typemap{PrintRegionConfig*};
%typemap{PrintConfig*}; %typemap{PrintConfig*};
@ -72,8 +80,33 @@
%typemap{ExPolygons}; %typemap{ExPolygons};
%typemap{Surfaces}; %typemap{Surfaces};
%typemap{Polygons*}; %typemap{Polygons*};
%typemap{TriangleMesh*};
%typemap{TriangleMeshPtrs}; %typemap{TriangleMeshPtrs};
%typemap{Extruder*}; %typemap{Extruder*};
%typemap{Model*};
%typemap{Ref<Model>}{simple};
%typemap{Clone<Model>}{simple};
%typemap{ModelMaterial*};
%typemap{Ref<ModelMaterial>}{simple};
%typemap{Clone<ModelMaterial>}{simple};
%typemap{ModelObject*};
%typemap{Ref<ModelObject>}{simple};
%typemap{Clone<ModelObject>}{simple};
%typemap{ModelObjectPtrs*};
%typemap{Ref<ModelObjectPtrs>}{simple};
%typemap{Clone<ModelObjectPtrs>}{simple};
%typemap{ModelVolume*};
%typemap{Ref<ModelVolume>}{simple};
%typemap{Clone<ModelVolume>}{simple};
%typemap{ModelVolumePtrs*};
%typemap{Ref<ModelVolumePtrs>}{simple};
%typemap{Clone<ModelVolumePtrs>}{simple};
%typemap{ModelInstance*};
%typemap{Ref<ModelInstance>}{simple};
%typemap{Clone<ModelInstance>}{simple};
%typemap{ModelInstancePtrs*};
%typemap{Ref<ModelInstancePtrs>}{simple};
%typemap{Clone<ModelInstancePtrs>}{simple};
%typemap{SurfaceType}{parsed}{ %typemap{SurfaceType}{parsed}{
%cpp_type{SurfaceType}; %cpp_type{SurfaceType};