From 72ce8cb28d2fdf23f2a937b3a6d88ba1f7e8a0b7 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Wed, 5 May 2021 14:17:36 +0200 Subject: [PATCH] PrintRegion refactoring: Getting rid of the Print pointer. --- src/libslic3r/LayerRegion.cpp | 5 +++-- src/libslic3r/Print.cpp | 6 +++--- src/libslic3r/Print.hpp | 15 ++++++--------- src/libslic3r/PrintObject.cpp | 2 +- src/libslic3r/PrintRegion.cpp | 15 ++++++++------- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/libslic3r/LayerRegion.cpp b/src/libslic3r/LayerRegion.cpp index b36daf421..a3b0890d7 100644 --- a/src/libslic3r/LayerRegion.cpp +++ b/src/libslic3r/LayerRegion.cpp @@ -29,11 +29,12 @@ Flow LayerRegion::bridging_flow(FlowRole role) const { const PrintRegion ®ion = *this->region(); const PrintRegionConfig ®ion_config = region.config(); - if (this->layer()->object()->config().thick_bridges) { + const PrintObject &print_object = *this->layer()->object(); + if (print_object.config().thick_bridges) { // The old Slic3r way (different from all other slicers): Use rounded extrusions. // Get the configured nozzle_diameter for the extruder associated to the flow role requested. // Here this->extruder(role) - 1 may underflow to MAX_INT, but then the get_at() will follback to zero'th element, so everything is all right. - auto nozzle_diameter = float(region.print()->config().nozzle_diameter.get_at(region.extruder(role) - 1)); + auto nozzle_diameter = float(print_object.print()->config().nozzle_diameter.get_at(region.extruder(role) - 1)); // Applies default bridge spacing. return Flow::bridging_flow(float(sqrt(region_config.bridge_flow_ratio)) * nozzle_diameter, nozzle_diameter); } else { diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 7fcb75297..e02bc65dd 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -47,13 +47,13 @@ void Print::clear() PrintRegion* Print::add_region() { - m_regions.emplace_back(new PrintRegion(this)); + m_regions.emplace_back(new PrintRegion()); return m_regions.back(); } PrintRegion* Print::add_region(const PrintRegionConfig &config) { - m_regions.emplace_back(new PrintRegion(this, config)); + m_regions.emplace_back(new PrintRegion(config)); return m_regions.back(); } @@ -281,7 +281,7 @@ std::vector Print::object_extruders() const region_used[&volumes_per_region - &object->region_volumes.front()] = true; for (size_t idx_region = 0; idx_region < m_regions.size(); ++ idx_region) if (region_used[idx_region]) - m_regions[idx_region]->collect_object_printing_extruders(extruders); + m_regions[idx_region]->collect_object_printing_extruders(*this, extruders); sort_remove_duplicates(extruders); return extruders; } diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index 91f86d010..ff4ae68f2 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -57,11 +57,13 @@ enum PrintObjectStep { // sharing the same config (including the same assigned extruder(s)) class PrintRegion { - friend class Print; +public: + PrintRegion() : m_refcnt(0) {} + PrintRegion(const PrintRegionConfig &config) : m_refcnt(0), m_config(config) {} + ~PrintRegion() = default; // Methods NOT modifying the PrintRegion's state: public: - const Print* print() const { return m_print; } const PrintRegionConfig& config() const { return m_config; } // 1-based extruder identifier for this region and role. unsigned int extruder(FlowRole role) const; @@ -72,27 +74,22 @@ public: coordf_t bridging_height_avg(const PrintConfig &print_config) const; // Collect 0-based extruder indices used to print this region's object. - void collect_object_printing_extruders(std::vector &object_extruders) const; + void collect_object_printing_extruders(const Print &print, std::vector &object_extruders) const; static void collect_object_printing_extruders(const PrintConfig &print_config, const PrintRegionConfig ®ion_config, const bool has_brim, std::vector &object_extruders); // Methods modifying the PrintRegion's state: public: - Print* print() { return m_print; } void set_config(const PrintRegionConfig &config) { m_config = config; } void set_config(PrintRegionConfig &&config) { m_config = std::move(config); } void config_apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent = false) { this->m_config.apply_only(other, keys, ignore_nonexistent); } protected: + friend Print; size_t m_refcnt; private: - Print *m_print; PrintRegionConfig m_config; - - PrintRegion(Print* print) : m_refcnt(0), m_print(print) {} - PrintRegion(Print* print, const PrintRegionConfig &config) : m_refcnt(0), m_print(print), m_config(config) {} - ~PrintRegion() = default; }; template diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index 0e9acbfdf..10eaf7c1f 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -1675,7 +1675,7 @@ std::vector PrintObject::object_extruders() const extruders.reserve(this->region_volumes.size() * 3); for (size_t idx_region = 0; idx_region < this->region_volumes.size(); ++ idx_region) if (! this->region_volumes[idx_region].empty()) - m_print->get_region(idx_region)->collect_object_printing_extruders(extruders); + m_print->get_region(idx_region)->collect_object_printing_extruders(*this->print(), extruders); sort_remove_duplicates(extruders); return extruders; } diff --git a/src/libslic3r/PrintRegion.cpp b/src/libslic3r/PrintRegion.cpp index 837200984..5dba1316e 100644 --- a/src/libslic3r/PrintRegion.cpp +++ b/src/libslic3r/PrintRegion.cpp @@ -20,11 +20,12 @@ unsigned int PrintRegion::extruder(FlowRole role) const Flow PrintRegion::flow(const PrintObject &object, FlowRole role, double layer_height, bool first_layer) const { - ConfigOptionFloatOrPercent config_width; + const PrintConfig &print_config = object.print()->config(); + ConfigOptionFloatOrPercent config_width; // Get extrusion width from configuration. // (might be an absolute value, or a percent value, or zero for auto) - if (first_layer && m_print->config().first_layer_extrusion_width.value > 0) { - config_width = m_print->config().first_layer_extrusion_width; + if (first_layer && print_config.first_layer_extrusion_width.value > 0) { + config_width = print_config.first_layer_extrusion_width; } else if (role == frExternalPerimeter) { config_width = m_config.external_perimeter_extrusion_width; } else if (role == frPerimeter) { @@ -44,7 +45,7 @@ Flow PrintRegion::flow(const PrintObject &object, FlowRole role, double layer_he // Get the configured nozzle_diameter for the extruder associated to the flow role requested. // Here this->extruder(role) - 1 may underflow to MAX_INT, but then the get_at() will follback to zero'th element, so everything is all right. - auto nozzle_diameter = float(m_print->config().nozzle_diameter.get_at(this->extruder(role) - 1)); + auto nozzle_diameter = float(print_config.nozzle_diameter.get_at(this->extruder(role) - 1)); return Flow::new_from_config_width(role, config_width, nozzle_diameter, float(layer_height)); } @@ -76,17 +77,17 @@ void PrintRegion::collect_object_printing_extruders(const PrintConfig &print_con emplace_extruder(region_config.solid_infill_extruder); } -void PrintRegion::collect_object_printing_extruders(std::vector &object_extruders) const +void PrintRegion::collect_object_printing_extruders(const Print &print, std::vector &object_extruders) const { // PrintRegion, if used by some PrintObject, shall have all the extruders set to an existing printer extruder. // If not, then there must be something wrong with the Print::apply() function. #ifndef NDEBUG - auto num_extruders = (int)print()->config().nozzle_diameter.size(); + auto num_extruders = int(print.config().nozzle_diameter.size()); assert(this->config().perimeter_extruder <= num_extruders); assert(this->config().infill_extruder <= num_extruders); assert(this->config().solid_infill_extruder <= num_extruders); #endif - collect_object_printing_extruders(print()->config(), this->config(), print()->has_brim(), object_extruders); + collect_object_printing_extruders(print.config(), this->config(), print.has_brim(), object_extruders); } }