From 01dec1bd5cc49adde8bc42a98803db226cda6099 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Tue, 16 May 2023 11:06:27 +0200 Subject: [PATCH] Fixed crash in Organic supports. Fixes #10551 --- src/libslic3r/Support/TreeModelVolumes.cpp | 12 ++++---- src/libslic3r/Support/TreeModelVolumes.hpp | 33 ++++++++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/libslic3r/Support/TreeModelVolumes.cpp b/src/libslic3r/Support/TreeModelVolumes.cpp index 5df56cd62..b726dfa27 100644 --- a/src/libslic3r/Support/TreeModelVolumes.cpp +++ b/src/libslic3r/Support/TreeModelVolumes.cpp @@ -497,7 +497,7 @@ void TreeModelVolumes::calculateCollision(const coord_t radius, const LayerIndex const bool calculate_placable = m_support_rests_on_model && radius == 0; LayerPolygonCache data_placeable; if (calculate_placable) - data_placeable.allocate(data.idx_begin, data.idx_end); + data_placeable.allocate(data.begin(), data.end()); for (size_t outline_idx : layer_outline_indices) if (const std::vector &outlines = m_layer_outlines[outline_idx].second; ! outlines.empty()) { @@ -517,9 +517,9 @@ void TreeModelVolumes::calculateCollision(const coord_t radius, const LayerIndex // 1) Calculate offsets of collision areas in parallel. LayerPolygonCache collision_areas_offsetted; collision_areas_offsetted.allocate( - std::max(0, data.idx_begin - z_distance_bottom_layers), - std::min(outlines.size(), data.idx_end + z_distance_top_layers)); - tbb::parallel_for(tbb::blocked_range(collision_areas_offsetted.idx_begin, collision_areas_offsetted.idx_end), + std::max(0, data.begin() - z_distance_bottom_layers), + std::min(outlines.size(), data.end() + z_distance_top_layers)); + tbb::parallel_for(tbb::blocked_range(collision_areas_offsetted.begin(), collision_areas_offsetted.end()), [&outlines, &machine_border = std::as_const(m_machine_border), offset_value = radius + xy_distance, &collision_areas_offsetted, &throw_on_cancel] (const tbb::blocked_range &range) { for (LayerIndex layer_idx = range.begin(); layer_idx != range.end(); ++ layer_idx) { @@ -536,7 +536,7 @@ void TreeModelVolumes::calculateCollision(const coord_t radius, const LayerIndex // 2) Sum over top / bottom ranges. const bool processing_last_mesh = outline_idx == layer_outline_indices.size(); - tbb::parallel_for(tbb::blocked_range(data.idx_begin, data.idx_end), + tbb::parallel_for(tbb::blocked_range(data.begin(), data.end()), [&collision_areas_offsetted, &outlines, &machine_border = m_machine_border, &anti_overhang = m_anti_overhang, radius, xy_distance, z_distance_bottom_layers, z_distance_top_layers, min_resolution = m_min_resolution, &data, processing_last_mesh, &throw_on_cancel] (const tbb::blocked_range& range) { @@ -600,7 +600,7 @@ void TreeModelVolumes::calculateCollision(const coord_t radius, const LayerIndex // 3) Optionally calculate placables. if (calculate_placable) { // Now calculate the placable areas. - tbb::parallel_for(tbb::blocked_range(std::max(z_distance_bottom_layers + 1, data.idx_begin), data.idx_end), + tbb::parallel_for(tbb::blocked_range(std::max(z_distance_bottom_layers + 1, data.begin()), data.end()), [&collision_areas_offsetted, &outlines, &anti_overhang = m_anti_overhang, processing_last_mesh, min_resolution = m_min_resolution, z_distance_bottom_layers, xy_distance, &data_placeable, &throw_on_cancel] (const tbb::blocked_range& range) { diff --git a/src/libslic3r/Support/TreeModelVolumes.hpp b/src/libslic3r/Support/TreeModelVolumes.hpp index ecfa99971..1e4386468 100644 --- a/src/libslic3r/Support/TreeModelVolumes.hpp +++ b/src/libslic3r/Support/TreeModelVolumes.hpp @@ -332,23 +332,26 @@ public: private: // Caching polygons for a range of layers. - struct LayerPolygonCache { - std::vector polygons; - LayerIndex idx_begin; - LayerIndex idx_end; - + class LayerPolygonCache { + public: void allocate(LayerIndex aidx_begin, LayerIndex aidx_end) { - this->idx_begin = aidx_begin; - this->idx_end = aidx_end; - this->polygons.assign(aidx_end - aidx_begin, {}); + m_idx_begin = aidx_begin; + m_idx_end = aidx_end; + m_polygons.assign(aidx_end - aidx_begin, {}); } - LayerIndex begin() const { return idx_begin; } - LayerIndex end() const { return idx_end; } - size_t size() const { return polygons.size(); } + LayerIndex begin() const { return m_idx_begin; } + LayerIndex end() const { return m_idx_end; } + size_t size() const { return m_polygons.size(); } - bool has(LayerIndex idx) const { return idx >= idx_begin && idx < idx_end; } - Polygons& operator[](LayerIndex idx) { return polygons[idx + idx_begin]; } + bool has(LayerIndex idx) const { return idx >= m_idx_begin && idx < m_idx_end; } + Polygons& operator[](LayerIndex idx) { assert(idx >= m_idx_begin && idx < m_idx_end); return m_polygons[idx - m_idx_begin]; } + std::vector& polygons_mutable() { return m_polygons; } + + private: + std::vector m_polygons; + LayerIndex m_idx_begin; + LayerIndex m_idx_end; }; /*! @@ -388,9 +391,9 @@ private: } void insert(LayerPolygonCache &&in, coord_t radius) { std::lock_guard guard(m_mutex); - LayerIndex i = in.idx_begin; + LayerIndex i = in.begin(); allocate_layers(i + LayerIndex(in.size())); - for (auto &d : in.polygons) + for (auto &d : in.polygons_mutable()) m_data[i ++].emplace(radius, std::move(d)); } /*!