diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 708fdd63d..c1d50a5f9 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -471,8 +471,8 @@ sub reset { my $self = shift; @{$self->{objects}} = (); - $self->{model}->delete_all_objects; - $self->{print}->delete_all_objects; + $self->{model}->clear_objects; + $self->{print}->clear_objects; $self->{list}->DeleteAllItems; $self->object_list_changed; diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 9f17b292f..92dfa7245 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -58,7 +58,7 @@ sub add_object { $args{input_file}, $args{config} // Slic3r::Config->new, $args{layer_height_ranges} // [], - $args{origin_translation} // Slic3r::Point->new, + $args{origin_translation} // Slic3r::Pointf->new, ); } diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 7bb7b41fa..8bd1bf4fc 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -105,7 +105,7 @@ sub apply_config { # the current subdivision of regions does not make sense anymore. # we need to remove all objects and re-add them my @models_objects = map [$_->model, $_->model_object], @{$self->objects}; - $self->delete_all_objects; + $self->clear_objects; $self->add_model_object(@$_) for @models_objects; } } @@ -202,7 +202,7 @@ sub delete_object { $self->_state->invalidate(STEP_BRIM); } -sub delete_all_objects { +sub clear_objects { my ($self) = @_; @{$self->objects} = (); @@ -222,7 +222,7 @@ sub reload_object { # This should also check whether object volumes (parts) have changed. my @models_objects = map [$_->model, $_->model_object], @{$self->objects}; - $self->delete_all_objects; + $self->clear_objects; $self->add_model_object(@$_) for @models_objects; } diff --git a/lib/Slic3r/Print/Simple.pm b/lib/Slic3r/Print/Simple.pm index d0a13682f..c3230149b 100644 --- a/lib/Slic3r/Print/Simple.pm +++ b/lib/Slic3r/Print/Simple.pm @@ -44,7 +44,7 @@ sub set_model { my ($self, $model) = @_; # make method idempotent so that the object is reusable - $self->_print->delete_all_objects; + $self->_print->clear_objects; # make sure all objects have at least one defined instance my $need_arrange = $model->add_default_instances; diff --git a/xs/src/Model.cpp b/xs/src/Model.cpp index 1991c052a..3fd752f28 100644 --- a/xs/src/Model.cpp +++ b/xs/src/Model.cpp @@ -6,13 +6,9 @@ namespace Slic3r { -Model::Model() -{ -} +Model::Model() {} Model::Model(const Model &other) -: materials(), - objects() { objects.reserve(other.objects.size()); @@ -36,15 +32,15 @@ Model::Model(const Model &other) Model::~Model() { - delete_all_objects(); - delete_all_materials(); + this->clear_objects(); + this->clear_materials(); } -ModelObject * -Model::add_object(std::string input_file, DynamicPrintConfig *config, - t_layer_height_ranges layer_height_ranges, Point origin_translation) +ModelObject* +Model::add_object(const std::string &input_file, const DynamicPrintConfig &config, + const t_layer_height_ranges &layer_height_ranges, const Pointf &origin_translation) { - ModelObject *object = new ModelObject(this, input_file, config, + ModelObject* object = new ModelObject(this, input_file, config, layer_height_ranges, origin_translation); this->objects.push_back(object); return object; @@ -59,27 +55,27 @@ Model::delete_object(size_t idx) } void -Model::delete_all_objects() +Model::clear_objects() { - for (ModelObjectPtrs::iterator i = this->objects.begin(); - i != this->objects.end(); ++i) - { - delete *i; - } - - objects.clear(); + for (size_t i = 0; i < this->objects.size(); ++i) + this->delete_object(i); } void -Model::delete_all_materials() +Model::delete_material(t_model_material_id material_id) { - for (ModelMaterialMap::iterator i = this->materials.begin(); - i != this->materials.end(); ++i) - { + ModelMaterialMap::iterator i = this->materials.find(material_id); + if (i != this->materials.end()) { delete i->second; + this->materials.erase(i); } +} - this->materials.clear(); +void +Model::clear_materials() +{ + while (!this->materials.empty()) + this->delete_material( this->materials.begin()->first ); } ModelMaterial * @@ -154,12 +150,12 @@ 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) +ModelObject::ModelObject(Model *model, const std::string &input_file, + const DynamicPrintConfig &config, const t_layer_height_ranges &layer_height_ranges, + const Pointf &origin_translation) : model(model), input_file(input_file), - config(*config), + config(config), layer_height_ranges(layer_height_ranges), origin_translation(origin_translation), _bounding_box_valid(false) @@ -205,8 +201,8 @@ ModelObject::~ModelObject() } ModelVolume * -ModelObject::add_volume(t_model_material_id material_id, - TriangleMesh *mesh, bool modifier) +ModelObject::add_volume(const t_model_material_id &material_id, + const TriangleMesh &mesh, bool modifier) { ModelVolume *v = new ModelVolume(this, material_id, mesh, modifier); this->volumes.push_back(v); @@ -226,13 +222,8 @@ ModelObject::delete_volume(size_t idx) void ModelObject::clear_volumes() { - for (ModelVolumePtrs::iterator i = this->volumes.begin(); - i != this->volumes.end(); ++i) - { - delete *i; - } - - this->volumes.clear(); + for (size_t i = 0; i < this->volumes.size(); ++i) + this->delete_volume(i); } ModelInstance * @@ -246,24 +237,26 @@ ModelObject::add_instance(double rotation, double scaling_factor, return i; } +void +ModelObject::delete_instance(size_t idx) +{ + ModelInstancePtrs::iterator i = this->instances.begin() + idx; + delete *i; + this->instances.erase(i); + this->invalidate_bounding_box(); +} + void ModelObject::delete_last_instance() { - delete this->instances.back(); - this->instances.pop_back(); - this->invalidate_bounding_box(); + this->delete_instance(this->instances.size() - 1); } void ModelObject::clear_instances() { - for (ModelInstancePtrs::iterator i = this->instances.begin(); - i != this->instances.end(); ++i) - { - delete *i; - } - - this->instances.clear(); + for (size_t i = 0; i < this->instances.size(); ++i) + this->delete_instance(i); } void @@ -284,19 +277,15 @@ ModelObject::to_SV_ref() { #endif -ModelVolume::ModelVolume(ModelObject *object, t_model_material_id material_id, - TriangleMesh *mesh, bool modifier) +ModelVolume::ModelVolume(ModelObject* object, const t_model_material_id &material_id, + const TriangleMesh &mesh, bool modifier) : object(object), material_id(material_id), - mesh(*mesh), + mesh(mesh), modifier(modifier) { } -ModelVolume::~ModelVolume() -{ -} - #ifdef SLIC3RXS REGISTER_CLASS(ModelVolume, "Model::Volume"); @@ -310,7 +299,7 @@ ModelVolume::to_SV_ref() { ModelInstance::ModelInstance(ModelObject *object, double rotation, - double scaling_factor, Pointf offset) + double scaling_factor, const Pointf &offset) : object(object), rotation(rotation), scaling_factor(scaling_factor), diff --git a/xs/src/Model.hpp b/xs/src/Model.hpp index 52179c9f6..b0e04732f 100644 --- a/xs/src/Model.hpp +++ b/xs/src/Model.hpp @@ -36,11 +36,12 @@ class Model Model(); Model(const Model &other); ~Model(); - ModelObject *add_object(std::string input_file, DynamicPrintConfig *config, - t_layer_height_ranges layer_height_ranges, Point origin_translation); + ModelObject* add_object(const std::string &input_file, const DynamicPrintConfig &config, + const t_layer_height_ranges &layer_height_ranges, const Pointf &origin_translation); void delete_object(size_t idx); - void delete_all_objects(); - void delete_all_materials(); + void clear_objects(); + void delete_material(t_model_material_id material_id); + void clear_materials(); ModelMaterial *set_material(t_model_material_id material_id); // 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); @@ -80,22 +81,23 @@ class ModelObject ModelVolumePtrs volumes; DynamicPrintConfig config; t_layer_height_ranges layer_height_ranges; - Point origin_translation; + Pointf origin_translation; BoundingBoxf3 _bounding_box; bool _bounding_box_valid; - ModelObject(Model *model, std::string input_file, DynamicPrintConfig *config, - t_layer_height_ranges layer_height_ranges, Point origin_translation); + ModelObject(Model *model, const std::string &input_file, const DynamicPrintConfig &config, + const t_layer_height_ranges &layer_height_ranges, const Pointf &origin_translation); ModelObject(const ModelObject &other); ~ModelObject(); - ModelVolume *add_volume(t_model_material_id material_id, - TriangleMesh *mesh, bool modifier); + ModelVolume* add_volume(const t_model_material_id &material_id, + const 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)); + ModelInstance *add_instance(double rotation=0, double scaling_factor = 1, + Pointf offset = Pointf(0, 0)); + void delete_instance(size_t idx); void delete_last_instance(); void clear_instances(); @@ -127,9 +129,8 @@ class ModelVolume TriangleMesh mesh; bool modifier; - ModelVolume(ModelObject *object, t_model_material_id material_id, - TriangleMesh *mesh, bool modifier); - ~ModelVolume(); + ModelVolume(ModelObject *object, const t_model_material_id &material_id, + const TriangleMesh &mesh, bool modifier); #ifdef SLIC3RXS SV* to_SV_ref(); @@ -145,7 +146,7 @@ class ModelInstance Pointf offset; // in unscaled coordinates ModelInstance(ModelObject *object, double rotation, double scaling_factor, - Pointf offset); + const Pointf &offset); ~ModelInstance(); void transform_mesh(TriangleMesh* mesh, bool dont_translate) const; diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index 7f636d858..43356a3bc 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -15,17 +15,18 @@ %code%{ RETVAL = THIS; %}; Ref _add_object(std::string input_file, - DynamicPrintConfig *config, + DynamicPrintConfig* config, t_layer_height_ranges layer_height_ranges, - Point *origin_translation) + Pointf* origin_translation) %code%{ - RETVAL = THIS->add_object(input_file, config, layer_height_ranges, + 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(); + void clear_objects(); + void delete_material(t_model_material_id material_id); + void clear_materials(); %name{_set_material} Ref set_material(t_model_material_id material_id) %code%{ RETVAL = THIS->set_material(material_id); %}; @@ -114,11 +115,11 @@ ModelMaterial::attributes() %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) + ModelObject(Model* model, std::string input_file, + DynamicPrintConfig* config, t_layer_height_ranges layer_height_ranges, + Pointf* origin_translation) %code%{ - RETVAL = new ModelObject(model, input_file, config, + RETVAL = new ModelObject(model, input_file, *config, layer_height_ranges, *origin_translation); %}; @@ -160,8 +161,8 @@ ModelMaterial::attributes() %}; %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); %}; + 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(); @@ -188,7 +189,7 @@ ModelMaterial::attributes() t_layer_height_ranges layer_height_ranges() %code%{ RETVAL = THIS->layer_height_ranges; %}; - Clone origin_translation() + Clone origin_translation() %code%{ RETVAL = THIS->origin_translation; %}; };