From 05b2993769ae78b74ee05d50108ec09d93dced64 Mon Sep 17 00:00:00 2001 From: "Y. Sapir" Date: Wed, 30 Apr 2014 02:04:49 +0300 Subject: [PATCH] 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) --- lib/Slic3r.pm | 2 + lib/Slic3r/Format/AMF.pm | 4 +- lib/Slic3r/GUI/Plater.pm | 20 +- lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm | 4 +- lib/Slic3r/Model.pm | 173 ++++------- lib/Slic3r/Print.pm | 17 +- lib/Slic3r/Print/Object.pm | 1 + lib/Slic3r/Print/Simple.pm | 6 +- lib/Slic3r/Test.pm | 2 +- xs/MANIFEST | 5 + xs/lib/Slic3r/XS.pm | 94 +++--- xs/src/Model.cpp | 343 ++++++++++++++++++++++ xs/src/Model.hpp | 102 +++++-- xs/src/StringMap.cpp | 13 + xs/src/StringMap.hpp | 16 + xs/src/perlglue.hpp | 13 +- xs/xsp/Model.xsp | 216 ++++++++++++++ xs/xsp/StringMap.xsp | 59 ++++ xs/xsp/my.map | 143 +++++++++ xs/xsp/typemap.xspt | 33 +++ 20 files changed, 1048 insertions(+), 218 deletions(-) create mode 100644 xs/src/Model.cpp create mode 100644 xs/src/StringMap.cpp create mode 100644 xs/src/StringMap.hpp create mode 100644 xs/xsp/Model.xsp create mode 100644 xs/xsp/StringMap.xsp diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 965d611b4..179405c97 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -148,6 +148,8 @@ sub thread_cleanup { *Slic3r::Geometry::BoundingBox::DESTROY = sub {}; *Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {}; *Slic3r::Line::DESTROY = sub {}; + *Slic3r::Model::DESTROY = sub {}; + *Slic3r::Model::Object::DESTROY = sub {}; *Slic3r::Point::DESTROY = sub {}; *Slic3r::Pointf::DESTROY = sub {}; *Slic3r::Pointf3::DESTROY = sub {}; diff --git a/lib/Slic3r/Format/AMF.pm b/lib/Slic3r/Format/AMF.pm index 4a2c1a61e..5dad1001e 100644 --- a/lib/Slic3r/Format/AMF.pm +++ b/lib/Slic3r/Format/AMF.pm @@ -35,8 +35,8 @@ sub write_file { printf $fh qq{\n}; printf $fh qq{\n}; printf $fh qq{ Slic3r %s\n}, $Slic3r::VERSION; - for my $material_id (sort keys %{ $model->materials }) { - my $material = $model->materials->{$material_id}; + for my $material_id (sort @{ $model->material_names }) { + my $material = $model->get_material($material_id); printf $fh qq{ \n}, $material_id; for (keys %{$material->attributes}) { printf $fh qq{ %s\n}, $_, $material->attributes->{$_}; diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 27beb25a7..708fdd63d 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -413,7 +413,7 @@ sub load_model_objects { } $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 @@ -542,12 +542,12 @@ sub rotate { { 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; # update print $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); } @@ -578,12 +578,13 @@ sub changescale { $range->[0] *= $variation; $range->[1] *= $variation; } - $_->scaling_factor($scale) for @{ $model_object->instances }; + $_->set_scaling_factor($scale) for @{ $model_object->instances }; $model_object->update_bounding_box; # update print $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); } @@ -1108,10 +1109,11 @@ sub mouse_event { return if !$self->{drag_start_pos}; # concurrency problems my ($obj_idx, $instance_idx) = @{ $self->{drag_object} }; my $model_object = $parent->{model}->objects->[$obj_idx]; - $model_object->instances->[$instance_idx]->offset([ - unscale($pos->[X] - $self->{drag_start_pos}[X]), - unscale($pos->[Y] - $self->{drag_start_pos}[Y]), - ]); + $model_object->instances->[$instance_idx]->set_offset( + Slic3r::Pointf->new( + unscale($pos->[X] - $self->{drag_start_pos}[X]), + unscale($pos->[Y] - $self->{drag_start_pos}[Y]), + )); $model_object->update_bounding_box; $parent->Refresh; } elsif ($event->Moving) { diff --git a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm index 7b62bea52..5713acaba 100644 --- a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm +++ b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm @@ -158,7 +158,7 @@ sub selection_changed { # attach volume material config to settings panel 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; $self->{staticbox}->SetLabel('Part Settings'); $self->{settings_panel}->enable; @@ -211,7 +211,7 @@ sub on_btn_load { $new_volume->mesh->translate(@{$self->{model_object}->origin_translation}, 0); # 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); $self->{parts_changed} = 1; diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 4c610540d..666a68b8d 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -1,12 +1,8 @@ package Slic3r::Model; -use Moo; use List::Util qw(first max); 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 { my $class = shift; my ($input_file) = @_; @@ -16,7 +12,7 @@ sub read_from_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"; - $_->input_file($input_file) for @{$model->objects}; + $_->set_input_file($input_file) for @{$model->objects}; return $model; } @@ -39,11 +35,12 @@ sub add_object { if (@_ == 1) { # we have a Model::Object my ($object) = @_; - + $new_object = $self->add_object( input_file => $object->input_file, config => $object->config, layer_height_ranges => $object->layer_height_ranges, # TODO: clone! + origin_translation => $object->origin_translation, ); foreach my $volume (@{$object->volumes}) { @@ -51,51 +48,39 @@ sub add_object { } $new_object->add_instance( - offset => $_->offset, + offset => [ @{$_->offset} ], rotation => $_->rotation, scaling_factor => $_->scaling_factor, ) for @{ $object->instances // [] }; } 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; } -sub delete_object { - my ($self, $obj_idx) = @_; - splice @{$self->objects}, $obj_idx, 1; -} - -sub delete_all_objects { - my ($self) = @_; - @{$self->objects} = (); -} - sub set_material { my $self = shift; my ($material_id, $attributes) = @_; - return $self->materials->{$material_id} = Slic3r::Model::Material->new( - model => $self, - attributes => $attributes || {}, - ); -} - -sub get_material { - my ($self, $material_id) = @_; - return $self->materials->{$material_id}; + return $self->_set_material($material_id, $attributes || {}); } sub duplicate_objects_grid { my ($self, $grid, $distance) = @_; - + die "Grid duplication is not supported with multiple objects\n" if @{$self->objects} > 1; - + my $object = $self->objects->[0]; - @{$object->instances} = (); - + $object->clear_instances; + my $size = $object->bounding_box->size; for my $x_copy (1..$grid->[X]) { for my $y_copy (1..$grid->[Y]) { @@ -144,7 +129,7 @@ sub arrange_objects { my @positions = $self->_arrange(\@instance_sizes, $distance, $bb); 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; } } @@ -231,8 +216,9 @@ sub center_instances_around_point { foreach my $object (@{$self->objects}) { foreach my $instance (@{$object->instances}) { - $instance->offset->[X] += $shift[X]; - $instance->offset->[Y] += $shift[Y]; + $instance->set_offset(Slic3r::Pointf->new( + $instance->offset->x + $shift[X], + $instance->offset->y + $shift[Y])); } $object->update_bounding_box; } @@ -276,7 +262,7 @@ sub split_meshes { if (@{$object->volumes} > 1) { # We can't split meshes if there's more than one material, because # we can't group the resulting meshes by object afterwards - push @{$self->objects}, $object; + $self->_add_object($object); next; } @@ -286,6 +272,7 @@ sub split_meshes { input_file => $object->input_file, config => $object->config->clone, layer_height_ranges => $object->layer_height_ranges, # TODO: this needs to be cloned + origin_translation => $object->origin_translation, ); $new_object->add_volume( mesh => $mesh, @@ -312,57 +299,44 @@ sub get_material_name { my ($material_id) = @_; my $name; - if (exists $self->materials->{$material_id}) { - $name //= $self->materials->{$material_id}->attributes->{$_} for qw(Name name); + if ($self->has_material($material_id)) { + $name //= $self->get_material($material_id) + ->attributes->{$_} for qw(Name name); } $name //= $material_id; return $name; } package Slic3r::Model::Material; -use Moo; -has 'model' => (is => 'ro', weak_ref => 1, required => 1); -has 'attributes' => (is => 'rw', default => sub { {} }); -has 'config' => (is => 'rw', default => sub { Slic3r::Config->new }); +sub attributes { + $_[0]->_attributes->to_hash; +} package Slic3r::Model::Object; -use Moo; use File::Basename qw(basename); use List::Util qw(first sum); 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 { my $self = shift; - + my $new_volume; if (@_ == 1) { # we have a Model::Volume my ($volume) = @_; - $new_volume = Slic3r::Model::Volume->new( - object => $self, - material_id => $volume->material_id, - mesh => $volume->mesh->clone, - modifier => $volume->modifier, - ); + $new_volume = $self->_add_volume( + $volume->material_id, $volume->mesh->clone, $volume->modifier); + # TODO: material_id can't be undef. if (defined $volume->material_id) { # 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 }; - if (exists $self->model->materials->{$volume->material_id}) { - %attributes = (%attributes, %{ $self->model->materials->{$volume->material_id}->attributes }) + if ($self->model->has_material($volume->material_id)) { + %attributes = (%attributes, %{ $self->model->get_material($volume->material_id)->attributes }) } my $new_material = $self->model->set_material($volume->material_id, {%attributes}); $new_material->config->apply($material->config); @@ -370,10 +344,10 @@ sub add_volume { } } else { my %args = @_; - $new_volume = Slic3r::Model::Volume->new( - object => $self, - %args, - ); + $new_volume = $self->_add_volume( + $args{material_id}, + $args{mesh}, + $args{modifier} // 0); } 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); } - push @{$self->volumes}, $new_volume; - - # invalidate cached bounding box - $self->_bounding_box(undef); + $self->invalidate_bounding_box(); return $new_volume; } -sub delete_volume { - my ($self, $i) = @_; - splice @{$self->volumes}, $i, 1; -} - sub add_instance { my $self = shift; 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 { - my ($self) = @_; - pop @{$self->instances}; - $self->_bounding_box(undef); + return $self->_add_instance( + $params{rotation} // 0, + $params{scaling_factor} // 1, + $params{offset} // []); } sub instances_count { @@ -487,8 +447,9 @@ sub center_around_origin { if (defined $self->instances) { foreach my $instance (@{ $self->instances }) { - $instance->offset->[X] -= $shift[X]; - $instance->offset->[Y] -= $shift[Y]; + $instance->set_offset(Slic3r::Pointf->new( + $instance->offset->x - $shift[X], + $instance->offset->y - $shift[Y])); } $self->update_bounding_box; } @@ -511,7 +472,7 @@ sub rotate_x { $angle = rad2deg($angle); $_->mesh->rotate_x($angle) for @{$self->volumes}; - $self->_bounding_box(undef); + $self->invalidate_bounding_box; } sub materials_count { @@ -577,22 +538,22 @@ sub cut { # clone this one my $upper = Slic3r::Model::Object->new( - input_file => $self->input_file, - model => $self->model, - config => $self->config->clone, - layer_height_ranges => $self->layer_height_ranges, - origin_translation => $self->origin_translation->clone, + $self->model, + $self->input_file, + $self->config, # config is cloned by new() + $self->layer_height_ranges, + $self->origin_translation, ); my $lower = Slic3r::Model::Object->new( - input_file => $self->input_file, - model => $self->model, - config => $self->config->clone, - layer_height_ranges => $self->layer_height_ranges, - origin_translation => $self->origin_translation->clone, + $self->model, + $self->input_file, + $self->config, # config is cloned by new() + $self->layer_height_ranges, + $self->origin_translation, ); foreach my $instance (@{$self->instances}) { - $upper->add_instance(offset => $instance->offset); - $lower->add_instance(offset => $instance->offset); + $upper->add_instance(offset => [ @{$instance->offset} ]); + $lower->add_instance(offset => [ @{$instance->offset} ]); } foreach my $volume (@{$self->volumes}) { @@ -632,29 +593,17 @@ sub cut { } 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 { my ($self) = @_; 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); return $model->set_material($material_id); } 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 { my ($self, $mesh, $dont_translate) = @_; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 230c8211d..7bb7b41fa 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -86,7 +86,7 @@ sub apply_config { $new->apply_dynamic($model_object_config); } 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; $new->apply_dynamic($material_config); } @@ -104,9 +104,9 @@ sub apply_config { if ($rearrange_regions) { # the current subdivision of regions does not make sense anymore. # 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->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 sub add_model_object { my $self = shift; - my ($object, $obj_idx) = @_; + my ($model, $object, $obj_idx) = @_; my $object_config = $object->config->clone; $object_config->normalize; @@ -138,7 +138,7 @@ sub add_model_object { $config->apply_dynamic($object_config); 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; $config->apply_dynamic($material_config); } @@ -170,6 +170,7 @@ sub add_model_object { # initialize print object my $o = Slic3r::Print::Object->new( print => $self, + model => $model, # for strong ref model_object => $object, region_volumes => [ map $volumes{$_}, 0..$#{$self->regions} ], 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. # 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->add_model_object($_) for @model_objects; + $self->add_model_object(@$_) for @models_objects; } sub validate { @@ -1112,7 +1113,7 @@ sub auto_assign_extruders { foreach my $i (0..$#{$model_object->volumes}) { my $volume = $model_object->volumes->[$i]; 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 $extruder_id = $i + 1; $config->set_ifndef('extruder', $extruder_id); diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index e41f6813f..0fe61cb5d 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -10,6 +10,7 @@ use Slic3r::Print::State ':steps'; use Slic3r::Surface ':types'; 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 'region_volumes' => (is => 'rw', default => sub { [] }); # by region_id has 'copies' => (is => 'ro'); # Slic3r::Point objects in scaled G-code coordinates diff --git a/lib/Slic3r/Print/Simple.pm b/lib/Slic3r/Print/Simple.pm index 794661d8a..d0a13682f 100644 --- a/lib/Slic3r/Print/Simple.pm +++ b/lib/Slic3r/Print/Simple.pm @@ -51,8 +51,8 @@ sub set_model { # apply scaling and rotation supplied from command line if any foreach my $instance (map @{$_->instances}, @{$model->objects}) { - $instance->scaling_factor($instance->scaling_factor * $self->scale); - $instance->rotation($instance->rotation + $self->rotate); + $instance->set_scaling_factor($instance->scaling_factor * $self->scale); + $instance->set_rotation($instance->rotation + $self->rotate); } if ($self->duplicate_grid->[X] > 1 || $self->duplicate_grid->[Y] > 1) { @@ -67,7 +67,7 @@ sub set_model { foreach my $model_object (@{$model->objects}) { $self->_print->auto_assign_extruders($model_object); - $self->_print->add_model_object($model_object); + $self->_print->add_model_object($model, $model_object); } } diff --git a/lib/Slic3r/Test.pm b/lib/Slic3r/Test.pm index 7d8ce6639..034379c7a 100644 --- a/lib/Slic3r/Test.pm +++ b/lib/Slic3r/Test.pm @@ -151,7 +151,7 @@ sub init_print { $model->center_instances_around_point($print->config->print_center); foreach my $model_object (@{$model->objects}) { $print->auto_assign_extruders($model_object); - $print->add_model_object($model_object); + $print->add_model_object($model, $model_object); } } $print->validate; diff --git a/xs/MANIFEST b/xs/MANIFEST index d5c52dd53..e3b436a6c 100644 --- a/xs/MANIFEST +++ b/xs/MANIFEST @@ -1673,6 +1673,7 @@ src/Geometry.hpp src/Layer.hpp src/Line.cpp src/Line.hpp +src/Model.cpp src/Model.hpp src/MultiPoint.cpp src/MultiPoint.hpp @@ -1705,6 +1706,8 @@ src/Print.cpp src/Print.hpp src/PrintConfig.cpp src/PrintConfig.hpp +src/StringMap.cpp +src/StringMap.hpp src/Surface.cpp src/Surface.hpp src/SurfaceCollection.cpp @@ -1742,6 +1745,7 @@ xsp/ExtrusionPath.xsp xsp/Flow.xsp xsp/Geometry.xsp xsp/Line.xsp +xsp/Model.xsp xsp/my.map xsp/mytype.map xsp/Point.xsp @@ -1749,6 +1753,7 @@ xsp/Polygon.xsp xsp/Polyline.xsp xsp/PolylineCollection.xsp xsp/Print.xsp +xsp/StringMap.xsp xsp/Surface.xsp xsp/SurfaceCollection.xsp xsp/TriangleMesh.xsp diff --git a/xs/lib/Slic3r/XS.pm b/xs/lib/Slic3r/XS.pm index 5529d24ba..83d03cb32 100644 --- a/xs/lib/Slic3r/XS.pm +++ b/xs/lib/Slic3r/XS.pm @@ -13,31 +13,16 @@ use overload '@{}' => sub { $_[0]->arrayref }, 'fallback' => 1; -package Slic3r::Line::Ref; -our @ISA = 'Slic3r::Line'; - -sub DESTROY {} - package Slic3r::Point; use overload '@{}' => sub { $_[0]->arrayref }, 'fallback' => 1; -package Slic3r::Point::Ref; -our @ISA = 'Slic3r::Point'; - -sub DESTROY {} - package Slic3r::Pointf; use overload '@{}' => sub { [ $_[0]->x, $_[0]->y ] }, #, 'fallback' => 1; -package Slic3r::Pointf::Ref; -our @ISA = 'Slic3r::Pointf'; - -sub DESTROY {} - package Slic3r::Pointf3; use overload '@{}' => sub { [ $_[0]->x, $_[0]->y, $_[0]->z ] }, #, @@ -48,21 +33,11 @@ use overload '@{}' => sub { $_[0]->arrayref }, 'fallback' => 1; -package Slic3r::ExPolygon::Ref; -our @ISA = 'Slic3r::ExPolygon'; - -sub DESTROY {} - package Slic3r::Polyline; use overload '@{}' => sub { $_[0]->arrayref }, 'fallback' => 1; -package Slic3r::Polyline::Ref; -our @ISA = 'Slic3r::Polyline'; - -sub DESTROY {} - package Slic3r::Polyline::Collection; use overload '@{}' => sub { $_[0]->arrayref }, @@ -73,11 +48,6 @@ use overload '@{}' => sub { $_[0]->arrayref }, 'fallback' => 1; -package Slic3r::Polygon::Ref; -our @ISA = 'Slic3r::Polygon'; - -sub DESTROY {} - package Slic3r::ExPolygon::Collection; use overload '@{}' => sub { $_[0]->arrayref }, @@ -96,11 +66,6 @@ sub new { return $self; } -package Slic3r::ExtrusionPath::Collection::Ref; -our @ISA = 'Slic3r::ExtrusionPath::Collection'; - -sub DESTROY {} - package Slic3r::ExtrusionLoop; use overload '@{}' => sub { $_[0]->arrayref }, @@ -130,11 +95,6 @@ sub clone { ); } -package Slic3r::ExtrusionLoop::Ref; -our @ISA = 'Slic3r::ExtrusionLoop'; - -sub DESTROY {} - package Slic3r::ExtrusionPath; use overload '@{}' => sub { $_[0]->arrayref }, @@ -164,11 +124,6 @@ sub clone { ); } -package Slic3r::ExtrusionPath::Ref; -our @ISA = 'Slic3r::ExtrusionPath'; - -sub DESTROY {} - package Slic3r::Flow; sub new { @@ -229,14 +184,53 @@ sub clone { ); } -package Slic3r::Surface::Ref; -our @ISA = 'Slic3r::Surface'; - -sub DESTROY {} - package Slic3r::Surface::Collection; use overload '@{}' => sub { $_[0]->arrayref }, '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; diff --git a/xs/src/Model.cpp b/xs/src/Model.cpp new file mode 100644 index 000000000..e14595582 --- /dev/null +++ b/xs/src/Model.cpp @@ -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 + +} diff --git a/xs/src/Model.hpp b/xs/src/Model.hpp index 6e16e8faa..8fbf4fcaf 100644 --- a/xs/src/Model.hpp +++ b/xs/src/Model.hpp @@ -2,7 +2,7 @@ #define slic3r_Model_hpp_ #include -#include "Config.hpp" +#include "PrintConfig.hpp" #include "Layer.hpp" #include "Point.hpp" #include "TriangleMesh.hpp" @@ -22,30 +22,40 @@ typedef std::string t_model_material_id; typedef std::string t_model_material_attribute; typedef std::map t_model_material_attributes; +typedef std::map ModelMaterialMap; +typedef std::vector ModelObjectPtrs; +typedef std::vector ModelVolumePtrs; +typedef std::vector ModelInstancePtrs; + class Model { public: - std::map materials; - std::vector objects; + ModelMaterialMap materials; + ModelObjectPtrs objects; Model(); + Model(const Model &other); ~Model(); - ModelObject* add_object(const ModelObject &object); - void delete_object(size_t obj_idx); + ModelObject *add_object(std::string input_file, DynamicPrintConfig *config, + t_layer_height_ranges layer_height_ranges, Point origin_translation); + void delete_object(size_t idx); void delete_all_objects(); - void set_material(t_model_material_id material_id, const t_model_material_attributes &attributes); - 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); + void delete_all_materials(); + ModelMaterial *set_material(t_model_material_id material_id, + const t_model_material_attributes &attributes); + // void duplicate_objects_grid(unsigned int x, unsigned int 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 align_to_origin(); - void center_instances_around_point(const Pointf &point); - 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); + // void bounding_box(BoundingBox* bb) const; + // void align_to_origin(); + // void center_instances_around_point(const Pointf &point); + // 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); + private: void _arrange(const std::vector &sizes, coordf_t distance, const BoundingBox &bb) const; @@ -56,7 +66,10 @@ class ModelMaterial public: Model* model; t_model_material_attributes attributes; - DynamicConfig config; + DynamicPrintConfig config; + + ModelMaterial(Model *model); + void apply(const t_model_material_attributes &attributes); }; class ModelObject @@ -64,16 +77,31 @@ class ModelObject public: Model* model; std::string input_file; - std::vector instances; - std::vector volumes; - DynamicConfig config; + ModelInstancePtrs instances; + ModelVolumePtrs volumes; + DynamicPrintConfig config; 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(); - 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 clear_instances(); + + void invalidate_bounding_box(); + void raw_mesh(TriangleMesh* mesh) const; void mesh(TriangleMesh* mesh) const; void instance_bounding_box(size_t instance_idx, BoundingBox* bb) const; @@ -84,8 +112,11 @@ class ModelObject size_t facets_count() const; bool needed_repair() const; + #ifdef SLIC3RXS + SV* to_SV_ref(); + #endif + private: - BoundingBox bb; void update_bounding_box(); }; @@ -95,18 +126,35 @@ class ModelVolume ModelObject* object; t_model_material_id material_id; 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 { public: ModelObject* object; - double rotation; + double rotation; // around mesh center point 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_polygon(Polygon* polygon) const; + + #ifdef SLIC3RXS + SV* to_SV_ref(); + #endif }; } diff --git a/xs/src/StringMap.cpp b/xs/src/StringMap.cpp new file mode 100644 index 000000000..7a8f02f94 --- /dev/null +++ b/xs/src/StringMap.cpp @@ -0,0 +1,13 @@ +#include "StringMap.hpp" +#ifdef SLIC3RXS +#include "perlglue.hpp" +#endif + + +namespace Slic3r { + +#ifdef SLIC3RXS +__REGISTER_CLASS(StringMap, "StringMap"); +#endif + +} diff --git a/xs/src/StringMap.hpp b/xs/src/StringMap.hpp new file mode 100644 index 000000000..f39bd7ce6 --- /dev/null +++ b/xs/src/StringMap.hpp @@ -0,0 +1,16 @@ +#ifndef slic3r_StringMap_hpp_ +#define slic3r_StringMap_hpp_ + +#include +#include +#include + +namespace Slic3r { + +// this is just for XSPP, because it chokes on the template typename +typedef std::map StringMap; + +} + + +#endif diff --git a/xs/src/perlglue.hpp b/xs/src/perlglue.hpp index d950dd256..ae99a5713 100644 --- a/xs/src/perlglue.hpp +++ b/xs/src/perlglue.hpp @@ -8,12 +8,17 @@ struct ClassTraits { static const char* name; static const char* name_ref; }; - -#define REGISTER_CLASS(cname,perlname) \ - class cname; \ + +// use this for typedefs for which the forward prototype +// in REGISTER_CLASS won't work +#define __REGISTER_CLASS(cname, perlname) \ template <>const char* ClassTraits::name = "Slic3r::" perlname; \ template <>const char* ClassTraits::name_ref = "Slic3r::" perlname "::Ref"; - + +#define REGISTER_CLASS(cname,perlname) \ + class cname; \ + __REGISTER_CLASS(cname, perlname); + template const char* perl_class_name(const T*) { return ClassTraits::name; } template diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp new file mode 100644 index 000000000..f9067c70f --- /dev/null +++ b/xs/xsp/Model.xsp @@ -0,0 +1,216 @@ +%module{Slic3r::XS}; + +%{ +#include +#include "Model.hpp" +#include "PrintConfig.hpp" +#include "StringMap.hpp" +#include "perlglue.hpp" +%} + +%name{Slic3r::Model} class Model { + Model(); + ~Model(); + + Clone clone() + %code%{ RETVAL = THIS; %}; + + Ref _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 set_material(t_model_material_id material_id, + StringMap attributes) + %code%{ RETVAL = THIS->set_material(material_id, attributes); %}; + + Ref 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 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() + %code%{ RETVAL = THIS->model; %}; + + Ref _attributes() + %code%{ RETVAL = &THIS->attributes; %}; + + Ref 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 _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 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 add_instance( + double rotation, double scaling_factor, std::vector 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 config() + %code%{ RETVAL = &THIS->config; %}; + + Ref model() + %code%{ RETVAL = THIS->model; %}; + + t_layer_height_ranges layer_height_ranges() + %code%{ RETVAL = THIS->layer_height_ranges; %}; + + Clone origin_translation() + %code%{ RETVAL = THIS->origin_translation; %}; +}; + + +%name{Slic3r::Model::Volume} class ModelVolume { + ~ModelVolume(); + + Ref object() + %code%{ RETVAL = THIS->object; %}; + + t_model_material_id material_id() + %code%{ RETVAL = THIS->material_id; %}; + + Ref mesh() + %code%{ RETVAL = &THIS->mesh; %}; + + bool modifier() + %code%{ RETVAL = THIS->modifier; %}; +}; + + +%name{Slic3r::Model::Instance} class ModelInstance { + ~ModelInstance(); + + Ref object() + %code%{ RETVAL = THIS->object; %}; + + double rotation() + %code%{ RETVAL = THIS->rotation; %}; + double scaling_factor() + %code%{ RETVAL = THIS->scaling_factor; %}; + Clone 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; %}; +}; diff --git a/xs/xsp/StringMap.xsp b/xs/xsp/StringMap.xsp new file mode 100644 index 000000000..874e2a7d2 --- /dev/null +++ b/xs/xsp/StringMap.xsp @@ -0,0 +1,59 @@ +%module{Slic3r::XS}; + +%{ +#include +#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(); %}; +}; diff --git a/xs/xsp/my.map b/xs/xsp/my.map index 27afe53bb..7075c6868 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -1,6 +1,17 @@ +coordf_t T_NV + std::vector T_STD_VECTOR_INT std::vector T_STD_VECTOR_INT + 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 O_OBJECT_SLIC3R_T + BoundingBox* O_OBJECT_SLIC3R Ref O_OBJECT_SLIC3R_T @@ -11,6 +22,7 @@ Ref O_OBJECT_SLIC3R_T Clone O_OBJECT_SLIC3R_T DynamicPrintConfig* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T PrintObjectConfig* O_OBJECT_SLIC3R PrintRegionConfig* O_OBJECT_SLIC3R PrintConfig* O_OBJECT_SLIC3R @@ -83,6 +95,27 @@ SurfaceCollection* O_OBJECT_SLIC3R Extruder* O_OBJECT_SLIC3R +Model* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T +Clone O_OBJECT_SLIC3R_T + +ModelMaterial* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T +Clone O_OBJECT_SLIC3R_T + +ModelObject* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T +Clone O_OBJECT_SLIC3R_T + +ModelVolume* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T +Clone O_OBJECT_SLIC3R_T + +ModelInstance* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T +Clone O_OBJECT_SLIC3R_T + + ExtrusionRole T_UV FlowRole T_UV PrintStep T_UV @@ -103,12 +136,16 @@ Surfaces T_ARRAYREF # by reference and marked ::Ref because they're contained in another # Perl object 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 # by reference and not marked ::Ref because they're newly allocated # and not referenced by any Perl object TriangleMeshPtrs T_PTR_ARRAYREF + INPUT O_OBJECT_SLIC3R @@ -140,6 +177,75 @@ T_ARRAYREF ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, \"$var\"); +# http://www.perlmonks.org/?node_id=853194 +T_STRING_MAP + { + HV *hv; + HE *he; + std::map 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 # return object from pointer O_OBJECT_SLIC3R @@ -171,6 +277,16 @@ T_ARRAYREF_PTR 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 AV* av = newAV(); $arg = newRV_noinc((SV*)av); @@ -180,3 +296,30 @@ T_PTR_ARRAYREF for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) { 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); + } diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 3aed02f7a..669987111 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -1,12 +1,19 @@ %typemap{bool}{simple}; +%typemap{size_t}{simple}; +%typemap{coordf_t}{simple}; %typemap{std::string}; %typemap{t_config_option_key}; +%typemap{t_model_material_id}; %typemap{std::vector}; %typemap{std::vector}; %typemap{std::vector*}; %typemap{std::vector}; %typemap{std::vector*}; %typemap{std::vector}; +%typemap{t_layer_height_ranges}; +%typemap{StringMap}; +%typemap{StringMap*}; +%typemap{Ref}{simple}; %typemap{SV*}; %typemap{AV*}; %typemap{Point*}; @@ -25,6 +32,7 @@ %typemap{Ref}{simple}; %typemap{Clone}{simple}; %typemap{DynamicPrintConfig*}; +%typemap{Ref}{simple}; %typemap{PrintObjectConfig*}; %typemap{PrintRegionConfig*}; %typemap{PrintConfig*}; @@ -72,8 +80,33 @@ %typemap{ExPolygons}; %typemap{Surfaces}; %typemap{Polygons*}; +%typemap{TriangleMesh*}; %typemap{TriangleMeshPtrs}; %typemap{Extruder*}; +%typemap{Model*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; +%typemap{ModelMaterial*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; +%typemap{ModelObject*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; +%typemap{ModelObjectPtrs*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; +%typemap{ModelVolume*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; +%typemap{ModelVolumePtrs*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; +%typemap{ModelInstance*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; +%typemap{ModelInstancePtrs*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; %typemap{SurfaceType}{parsed}{ %cpp_type{SurfaceType};