From f76e2c2222986a6d62dc0f5c55f60e892dc2d7ed Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 8 May 2014 13:33:43 +0200 Subject: [PATCH] Several minor fixes to Model --- xs/src/Config.cpp | 12 ++++++++++++ xs/src/Config.hpp | 3 ++- xs/src/Model.cpp | 42 +++++++++++++++++++++++++++++++++++------ xs/src/Model.hpp | 10 +++++++--- xs/src/TriangleMesh.cpp | 17 +++++++++++++++++ xs/src/TriangleMesh.hpp | 2 ++ 6 files changed, 76 insertions(+), 10 deletions(-) diff --git a/xs/src/Config.cpp b/xs/src/Config.cpp index 1043beb4a..279862100 100644 --- a/xs/src/Config.cpp +++ b/xs/src/Config.cpp @@ -250,6 +250,18 @@ ConfigBase::set_deserialize(const t_config_option_key opt_key, SV* str) { } #endif +DynamicConfig& DynamicConfig::operator= (DynamicConfig other) +{ + this->swap(other); + return *this; +} + +void +DynamicConfig::swap(DynamicConfig &other) +{ + std::swap(this->options, other.options); +} + DynamicConfig::~DynamicConfig () { for (t_options_map::iterator it = this->options.begin(); it != this->options.end(); ++it) { ConfigOption* opt = it->second; diff --git a/xs/src/Config.hpp b/xs/src/Config.hpp index 452f602d4..fe521746e 100644 --- a/xs/src/Config.hpp +++ b/xs/src/Config.hpp @@ -488,6 +488,8 @@ class DynamicConfig : public ConfigBase public: DynamicConfig() {}; DynamicConfig(const DynamicConfig& other); + DynamicConfig& operator= (DynamicConfig other); + void swap(DynamicConfig &other); ~DynamicConfig(); ConfigOption* option(const t_config_option_key opt_key, bool create = false); const ConfigOption* option(const t_config_option_key opt_key) const; @@ -495,7 +497,6 @@ class DynamicConfig : public ConfigBase void erase(const t_config_option_key opt_key); private: - DynamicConfig& operator= (const DynamicConfig& other); // we disable this by making it private and unimplemented typedef std::map t_options_map; t_options_map options; }; diff --git a/xs/src/Model.cpp b/xs/src/Model.cpp index aee79702d..1e6be95ea 100644 --- a/xs/src/Model.cpp +++ b/xs/src/Model.cpp @@ -30,6 +30,19 @@ Model::Model(const Model &other) } } +Model& Model::operator= (Model other) +{ + this->swap(other); + return *this; +} + +void +Model::swap(Model &other) +{ + std::swap(this->materials, other.materials); + std::swap(this->objects, other.objects); +} + Model::~Model() { this->clear_objects(); @@ -57,7 +70,8 @@ Model::delete_object(size_t idx) void Model::clear_objects() { - for (size_t i = this->objects.size()-1; i >= 0; --i) + // int instead of size_t because it can be -1 when vector is empty + for (int i = this->objects.size()-1; i >= 0; --i) this->delete_object(i); } @@ -194,6 +208,25 @@ ModelObject::ModelObject(const ModelObject &other) } } +ModelObject& ModelObject::operator= (ModelObject other) +{ + this->swap(other); + return *this; +} + +void +ModelObject::swap(ModelObject &other) +{ + std::swap(this->input_file, other.input_file); + std::swap(this->instances, other.instances); + std::swap(this->volumes, other.volumes); + std::swap(this->config, other.config); + std::swap(this->layer_height_ranges, other.layer_height_ranges); + std::swap(this->origin_translation, other.origin_translation); + std::swap(this->_bounding_box, other._bounding_box); + std::swap(this->_bounding_box_valid, other._bounding_box_valid); +} + ModelObject::~ModelObject() { this->clear_volumes(); @@ -222,7 +255,8 @@ ModelObject::delete_volume(size_t idx) void ModelObject::clear_volumes() { - for (size_t i = this->volumes.size()-1; i >= 0; --i) + // int instead of size_t because it can be -1 when vector is empty + for (int i = this->volumes.size()-1; i >= 0; --i) this->delete_volume(i); } @@ -307,10 +341,6 @@ ModelInstance::ModelInstance(ModelObject *object, double rotation, { } -ModelInstance::~ModelInstance() -{ -} - #ifdef SLIC3RXS REGISTER_CLASS(ModelInstance, "Model::Instance"); diff --git a/xs/src/Model.hpp b/xs/src/Model.hpp index b0e04732f..b5d9c5fcd 100644 --- a/xs/src/Model.hpp +++ b/xs/src/Model.hpp @@ -35,6 +35,8 @@ class Model Model(); Model(const Model &other); + Model& operator= (Model other); + void swap(Model &other); ~Model(); ModelObject* add_object(const std::string &input_file, const DynamicPrintConfig &config, const t_layer_height_ranges &layer_height_ranges, const Pointf &origin_translation); @@ -82,12 +84,12 @@ class ModelObject DynamicPrintConfig config; t_layer_height_ranges layer_height_ranges; Pointf origin_translation; - BoundingBoxf3 _bounding_box; - bool _bounding_box_valid; 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& operator= (ModelObject other); + void swap(ModelObject &other); ~ModelObject(); ModelVolume* add_volume(const t_model_material_id &material_id, @@ -118,6 +120,9 @@ class ModelObject #endif private: + BoundingBoxf3 _bounding_box; + bool _bounding_box_valid; + void update_bounding_box(); }; @@ -147,7 +152,6 @@ class ModelInstance ModelInstance(ModelObject *object, double rotation, double scaling_factor, const Pointf &offset); - ~ModelInstance(); void transform_mesh(TriangleMesh* mesh, bool dont_translate) const; void transform_polygon(Polygon* polygon) const; diff --git a/xs/src/TriangleMesh.cpp b/xs/src/TriangleMesh.cpp index ee3122366..d16e2b6ff 100644 --- a/xs/src/TriangleMesh.cpp +++ b/xs/src/TriangleMesh.cpp @@ -50,6 +50,23 @@ TriangleMesh::TriangleMesh(const TriangleMesh &other) } } +TriangleMesh& TriangleMesh::operator= (TriangleMesh other) +{ + this->swap(other); + return *this; +} + +void +TriangleMesh::swap(TriangleMesh &other) +{ + std::swap(this->stl, other.stl); + std::swap(this->repaired, other.repaired); + std::swap(this->stl.facet_start, other.stl.facet_start); + std::swap(this->stl.neighbors_start, other.stl.neighbors_start); + std::swap(this->stl.v_indices, other.stl.v_indices); + std::swap(this->stl.v_shared, other.stl.v_shared); +} + TriangleMesh::~TriangleMesh() { stl_close(&this->stl); } diff --git a/xs/src/TriangleMesh.hpp b/xs/src/TriangleMesh.hpp index 2f79832c9..0d39453e1 100644 --- a/xs/src/TriangleMesh.hpp +++ b/xs/src/TriangleMesh.hpp @@ -20,6 +20,8 @@ class TriangleMesh public: TriangleMesh(); TriangleMesh(const TriangleMesh &other); + TriangleMesh& operator= (TriangleMesh other); + void swap(TriangleMesh &other); ~TriangleMesh(); void ReadSTLFile(char* input_file); void write_ascii(char* output_file);