From e1ca1a82fb57367fb54e417d2ca19d071e43755b Mon Sep 17 00:00:00 2001 From: bubnikv Date: Tue, 30 May 2017 18:33:17 +0200 Subject: [PATCH] Fixed a regression bug from the last commit. Some more code simplification and inlining. --- xs/src/libslic3r/Layer.cpp | 71 ++++--------------------- xs/src/libslic3r/Layer.hpp | 15 +++--- xs/src/libslic3r/Print.hpp | 1 - xs/src/libslic3r/PrintObject.cpp | 90 +++++++++++--------------------- xs/xsp/Print.xsp | 4 +- 5 files changed, 48 insertions(+), 133 deletions(-) diff --git a/xs/src/libslic3r/Layer.cpp b/xs/src/libslic3r/Layer.cpp index 3f724009b..94413c2bb 100644 --- a/xs/src/libslic3r/Layer.cpp +++ b/xs/src/libslic3r/Layer.cpp @@ -9,61 +9,22 @@ namespace Slic3r { -Layer::Layer(size_t id, PrintObject *object, coordf_t height, coordf_t print_z, - coordf_t slice_z) -: upper_layer(NULL), - lower_layer(NULL), - slicing_errors(false), - slice_z(slice_z), - print_z(print_z), - height(height), - _id(id), - _object(object) -{ -} - Layer::~Layer() { - // remove references to self - if (NULL != this->upper_layer) { - this->upper_layer->lower_layer = NULL; - } - - if (NULL != this->lower_layer) { - this->lower_layer->upper_layer = NULL; - } - - this->clear_regions(); -} - -void -Layer::clear_regions() -{ - for (size_t i = 0; i < this->regions.size(); ++ i) - delete this->regions[i]; + this->lower_layer = this->upper_layer = nullptr; + for (LayerRegion *region : this->regions) + delete region; this->regions.clear(); } -LayerRegion* -Layer::add_region(PrintRegion* print_region) +LayerRegion* Layer::add_region(PrintRegion* print_region) { - LayerRegion* region = new LayerRegion(this, print_region); - this->regions.push_back(region); - return region; -} - -void -Layer::delete_region(int idx) -{ - LayerRegionPtrs::iterator i = this->regions.begin() + idx; - LayerRegion* item = *i; - this->regions.erase(i); - delete item; + this->regions.emplace_back(new LayerRegion(this, print_region)); + return this->regions.back(); } // merge all regions' slices to get islands -void -Layer::make_slices() +void Layer::make_slices() { ExPolygons slices; if (this->regions.size() == 1) { @@ -95,8 +56,7 @@ Layer::make_slices() this->slices.expolygons.push_back(STDMOVE(slices[i])); } -void -Layer::merge_slices() +void Layer::merge_slices() { if (this->regions.size() == 1) { // Optimization, also more robust. Don't merge classified pieces of layerm->slices, @@ -111,8 +71,7 @@ Layer::merge_slices() } template -bool -Layer::any_internal_region_slice_contains(const T &item) const +bool Layer::any_internal_region_slice_contains(const T &item) const { FOREACH_LAYERREGION(this, layerm) { if ((*layerm)->slices.any_internal_contains(item)) return true; @@ -122,8 +81,7 @@ Layer::any_internal_region_slice_contains(const T &item) const template bool Layer::any_internal_region_slice_contains(const Polyline &item) const; template -bool -Layer::any_bottom_region_slice_contains(const T &item) const +bool Layer::any_bottom_region_slice_contains(const T &item) const { FOREACH_LAYERREGION(this, layerm) { if ((*layerm)->slices.any_bottom_contains(item)) return true; @@ -136,8 +94,7 @@ template bool Layer::any_bottom_region_slice_contains(const Polyline & // Here the perimeters are created cummulatively for all layer regions sharing the same parameters influencing the perimeters. // The perimeter paths and the thin fills (ExtrusionEntityCollection) are assigned to the first compatible layer region. // The resulting fill surface is split back among the originating regions. -void -Layer::make_perimeters() +void Layer::make_perimeters() { BOOST_LOG_TRIVIAL(trace) << "Generating perimeters for layer " << this->id(); @@ -276,10 +233,4 @@ void Layer::export_region_fill_surfaces_to_svg_debug(const char *name) this->export_region_fill_surfaces_to_svg(debug_out_path("Layer-fill_surfaces-%s-%d.svg", name, idx ++).c_str()); } -SupportLayer::SupportLayer(size_t id, PrintObject *object, coordf_t height, - coordf_t print_z, coordf_t slice_z) -: Layer(id, object, height, print_z, slice_z) -{ -} - } diff --git a/xs/src/libslic3r/Layer.hpp b/xs/src/libslic3r/Layer.hpp index ec34f3353..2f0228453 100644 --- a/xs/src/libslic3r/Layer.hpp +++ b/xs/src/libslic3r/Layer.hpp @@ -130,15 +130,13 @@ protected: size_t _id; // sequential number of layer, 0-based PrintObject *_object; - Layer(size_t id, PrintObject *object, coordf_t height, coordf_t print_z, - coordf_t slice_z); + Layer(size_t id, PrintObject *object, coordf_t height, coordf_t print_z, coordf_t slice_z) : + upper_layer(nullptr), lower_layer(nullptr), slicing_errors(false), + slice_z(slice_z), print_z(print_z), height(height), + _id(id), _object(object) {} virtual ~Layer(); - - void clear_regions(); - void delete_region(int idx); }; - class SupportLayer : public Layer { friend class PrintObject; @@ -149,12 +147,11 @@ public: ExtrusionEntityCollection support_fills; protected: - SupportLayer(size_t id, PrintObject *object, coordf_t height, coordf_t print_z, - coordf_t slice_z); + SupportLayer(size_t id, PrintObject *object, coordf_t height, coordf_t print_z, coordf_t slice_z) : + Layer(id, object, height, print_z, slice_z) {} virtual ~SupportLayer() {} }; - } #endif diff --git a/xs/src/libslic3r/Print.hpp b/xs/src/libslic3r/Print.hpp index 580de19a6..e108e4516 100644 --- a/xs/src/libslic3r/Print.hpp +++ b/xs/src/libslic3r/Print.hpp @@ -158,7 +158,6 @@ public: // print_z: top of the layer; slice_z: center of the layer. Layer* add_layer(int id, coordf_t height, coordf_t print_z, coordf_t slice_z); - void delete_layer(int idx); size_t support_layer_count() const { return this->support_layers.size(); } void clear_support_layers(); diff --git a/xs/src/libslic3r/PrintObject.cpp b/xs/src/libslic3r/PrintObject.cpp index b716846aa..71cd55a16 100644 --- a/xs/src/libslic3r/PrintObject.cpp +++ b/xs/src/libslic3r/PrintObject.cpp @@ -58,16 +58,14 @@ PrintObject::PrintObject(Print* print, ModelObject* model_object, const Bounding this->layer_height_profile = model_object->layer_height_profile; } -bool -PrintObject::add_copy(const Pointf &point) +bool PrintObject::add_copy(const Pointf &point) { Points points = this->_copies; points.push_back(Point::new_scale(point.x, point.y)); return this->set_copies(points); } -bool -PrintObject::delete_last_copy() +bool PrintObject::delete_last_copy() { Points points = this->_copies; points.pop_back(); @@ -86,7 +84,8 @@ bool PrintObject::set_copies(const Points &points) std::vector ordered_copies; Slic3r::Geometry::chained_path(points, ordered_copies); - for (Point copy : ordered_copies) { + for (size_t point_idx : ordered_copies) { + Point copy = points[point_idx]; copy.translate(this->_copies_shift); this->_shifted_copies.push_back(copy); } @@ -94,68 +93,39 @@ bool PrintObject::set_copies(const Points &points) return this->_print->invalidate_step(psSkirt) || this->_print->invalidate_step(psBrim); } -bool -PrintObject::reload_model_instances() +bool PrintObject::reload_model_instances() { Points copies; - for (ModelInstancePtrs::const_iterator i = this->_model_object->instances.begin(); i != this->_model_object->instances.end(); ++i) { - copies.push_back(Point::new_scale((*i)->offset.x, (*i)->offset.y)); - } + copies.reserve(this->_model_object->instances.size()); + for (const ModelInstance *mi : this->_model_object->instances) + copies.emplace_back(Point::new_scale(mi->offset.x, mi->offset.y)); return this->set_copies(copies); } -void -PrintObject::clear_layers() +void PrintObject::clear_layers() { - for (size_t i = 0; i < this->layers.size(); ++ i) { - Layer *layer = this->layers[i]; - layer->upper_layer = layer->lower_layer = nullptr; - delete layer; - } + for (Layer *l : this->layers) + delete l; this->layers.clear(); } -Layer* -PrintObject::add_layer(int id, coordf_t height, coordf_t print_z, coordf_t slice_z) +Layer* PrintObject::add_layer(int id, coordf_t height, coordf_t print_z, coordf_t slice_z) { - Layer* layer = new Layer(id, this, height, print_z, slice_z); - layers.push_back(layer); - return layer; + layers.push_back(new Layer(id, this, height, print_z, slice_z)); + return layers.back(); } -void -PrintObject::delete_layer(int idx) +void PrintObject::clear_support_layers() { - LayerPtrs::iterator i = this->layers.begin() + idx; - delete *i; - this->layers.erase(i); -} - -void -PrintObject::clear_support_layers() -{ - for (size_t i = 0; i < this->support_layers.size(); ++ i) { - Layer *layer = this->support_layers[i]; - layer->upper_layer = layer->lower_layer = nullptr; - delete layer; - } + for (Layer *l : this->support_layers) + delete l; this->support_layers.clear(); } -SupportLayer* -PrintObject::add_support_layer(int id, coordf_t height, coordf_t print_z) +SupportLayer* PrintObject::add_support_layer(int id, coordf_t height, coordf_t print_z) { - SupportLayer* layer = new SupportLayer(id, this, height, print_z, -1); - support_layers.push_back(layer); - return layer; -} - -void -PrintObject::delete_support_layer(int idx) -{ - SupportLayerPtrs::iterator i = this->support_layers.begin() + idx; - delete *i; - this->support_layers.erase(i); + support_layers.emplace_back(new SupportLayer(id, this, height, print_z, -1)); + return support_layers.back(); } bool PrintObject::invalidate_state_by_config_options(const std::vector &opt_keys) @@ -493,8 +463,7 @@ void PrintObject::detect_surfaces_type() } // for each $self->print->region_count } -void -PrintObject::process_external_surfaces() +void PrintObject::process_external_surfaces() { BOOST_LOG_TRIVIAL(info) << "Processing external surfaces..."; @@ -524,8 +493,7 @@ struct DiscoverVerticalShellsCacheEntry Polygons bottom_fill_surfaces; }; -void -PrintObject::discover_vertical_shells() +void PrintObject::discover_vertical_shells() { PROFILE_FUNC(); @@ -1085,7 +1053,9 @@ void PrintObject::_slice() if (layer->regions[region_id] != nullptr && ! layer->regions[region_id]->slices.empty()) // Non empty layer. goto end; - this->delete_layer(int(this->layers.size()) - 1); + delete layer; + this->layers.pop_back(); + this->layers.back()->upper_layer = nullptr; } end: ; @@ -1229,7 +1199,9 @@ std::string PrintObject::_fix_slicing_errors() // remove empty layers from bottom while (! this->layers.empty() && this->layers.front()->slices.expolygons.empty()) { - this->delete_layer(0); + delete this->layers.front(); + this->layers.erase(this->layers.begin()); + this->layers.front()->lower_layer = nullptr; for (size_t i = 0; i < this->layers.size(); ++ i) this->layers[i]->set_id(this->layers[i]->id() - 1); } @@ -1258,8 +1230,7 @@ void PrintObject::_simplify_slices(double distance) BOOST_LOG_TRIVIAL(debug) << "Slicing objects - siplifying slices in parallel - end"; } -void -PrintObject::_make_perimeters() +void PrintObject::_make_perimeters() { if (this->state.is_done(posPerimeters)) return; this->state.set_started(posPerimeters); @@ -1368,8 +1339,7 @@ PrintObject::_make_perimeters() this->state.set_done(posPerimeters); } -void -PrintObject::_infill() +void PrintObject::_infill() { if (this->state.is_done(posInfill)) return; this->state.set_started(posInfill); diff --git a/xs/xsp/Print.xsp b/xs/xsp/Print.xsp index 23c09dbdf..511e5243d 100644 --- a/xs/xsp/Print.xsp +++ b/xs/xsp/Print.xsp @@ -94,14 +94,12 @@ _constant() Ref get_layer(int idx); Ref add_layer(int id, coordf_t height, coordf_t print_z, coordf_t slice_z); - void delete_layer(int idx); size_t support_layer_count(); void clear_support_layers(); Ref get_support_layer(int idx); Ref add_support_layer(int id, coordf_t height, coordf_t print_z); - void delete_support_layer(int idx); - + bool invalidate_state_by_config_options(std::vector opt_keys); bool invalidate_step(PrintObjectStep step); bool invalidate_all_steps();