From d1eb8444af532b06068e23c7d19757aa4ddbee39 Mon Sep 17 00:00:00 2001 From: "c.lamboo" Date: Wed, 28 Aug 2024 09:48:33 +0200 Subject: [PATCH 1/3] Prevent implicit sign casting Solves unexpected behaviour between different architechtures where the sizes of `size_t`, `maxint_t` etc may differ. NP-343 --- src/ConicalOverhang.cpp | 2 +- src/FffGcodeWriter.cpp | 26 +++++++++++++------------- src/FffPolygonGenerator.cpp | 8 ++++---- src/InterlockingGenerator.cpp | 4 ++-- src/slicer.cpp | 4 ++-- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/ConicalOverhang.cpp b/src/ConicalOverhang.cpp index 699d9697d7..ab4c60873d 100644 --- a/src/ConicalOverhang.cpp +++ b/src/ConicalOverhang.cpp @@ -23,7 +23,7 @@ void ConicalOverhang::apply(Slicer* slicer, const Mesh& mesh) const coord_t layer_thickness = mesh.settings_.get("layer_height"); coord_t max_dist_from_lower_layer = std::llround(tan_angle * static_cast(layer_thickness)); // max dist which can be bridged - for (LayerIndex layer_nr = slicer->layers.size() - 2; static_cast(layer_nr) >= 0; layer_nr--) + for (LayerIndex layer_nr = LayerIndex(slicer->layers.size()) - 2; layer_nr >= 0; layer_nr--) { SlicerLayer& layer = slicer->layers[static_cast(layer_nr)]; SlicerLayer& layer_above = slicer->layers[static_cast(layer_nr) + 1ul]; diff --git a/src/FffGcodeWriter.cpp b/src/FffGcodeWriter.cpp index c258d9aa3b..5a50c04452 100644 --- a/src/FffGcodeWriter.cpp +++ b/src/FffGcodeWriter.cpp @@ -575,7 +575,7 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage) const size_t surface_extruder_nr = mesh_group_settings.get("raft_surface_extruder_nr").extruder_nr_; coord_t z = 0; - const LayerIndex initial_raft_layer_nr = -Raft::getTotalExtraLayers(); + const LayerIndex initial_raft_layer_nr = -LayerIndex(Raft::getTotalExtraLayers()); const Settings& interface_settings = mesh_group_settings.get("raft_interface_extruder_nr").settings_; const size_t num_interface_layers = interface_settings.get("raft_interface_layers"); const Settings& surface_settings = mesh_group_settings.get("raft_surface_extruder_nr").settings_; @@ -764,7 +764,7 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage) z += raft_interface_z_offset; - for (LayerIndex raft_interface_layer = 1; static_cast(raft_interface_layer) <= num_interface_layers; ++raft_interface_layer) + for (LayerIndex raft_interface_layer = 1; raft_interface_layer <= LayerIndex(num_interface_layers); ++raft_interface_layer) { // raft interface layer const LayerIndex layer_nr = initial_raft_layer_nr + raft_interface_layer; z += interface_layer_height; @@ -925,7 +925,7 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage) z += raft_surface_z_offset; - for (LayerIndex raft_surface_layer = 1; static_cast(raft_surface_layer) <= num_surface_layers; raft_surface_layer++) + for (LayerIndex raft_surface_layer = 1; raft_surface_layer <= LayerIndex(num_surface_layers); raft_surface_layer++) { // raft surface layers const LayerIndex layer_nr = initial_raft_layer_nr + 1 + num_interface_layers + raft_surface_layer - 1; // +1: 1 base layer z += surface_layer_height; @@ -1538,7 +1538,7 @@ void FffGcodeWriter::calculateExtruderOrderPerLayer(const SliceDataStorage& stor extruder_order_per_layer.init(true, storage.print_layer_count); const std::vector extruders_used = storage.getExtrudersUsed(); - for (LayerIndex layer_nr = -Raft::getTotalExtraLayers(); layer_nr < static_cast(storage.print_layer_count); layer_nr++) + for (LayerIndex layer_nr = -LayerIndex(Raft::getTotalExtraLayers()); layer_nr < LayerIndex(storage.print_layer_count); layer_nr++) { std::vector extruder_order = getUsedExtrudersOnLayer(storage, last_extruder, layer_nr, extruders_used); extruder_order_per_layer.push_back(extruder_order); @@ -1557,7 +1557,7 @@ void FffGcodeWriter::calculateExtruderOrderPerLayer(const SliceDataStorage& stor void FffGcodeWriter::calculatePrimeLayerPerExtruder(const SliceDataStorage& storage) { - LayerIndex first_print_layer = -Raft::getTotalExtraLayers(); + LayerIndex first_print_layer = -LayerIndex(Raft::getTotalExtraLayers()); for (size_t extruder_nr = 0; extruder_nr < MAX_EXTRUDERS; ++extruder_nr) { if (getExtruderNeedPrimeBlobDuringFirstLayer(storage, extruder_nr)) @@ -1567,7 +1567,7 @@ void FffGcodeWriter::calculatePrimeLayerPerExtruder(const SliceDataStorage& stor } } - for (LayerIndex layer_nr = first_print_layer; layer_nr < static_cast(storage.print_layer_count); ++layer_nr) + for (LayerIndex layer_nr = first_print_layer; layer_nr < LayerIndex(storage.print_layer_count); ++layer_nr) { const std::vector used_extruders = storage.getExtrudersUsed(layer_nr); for (size_t extruder_nr = 0; extruder_nr < used_extruders.size(); ++extruder_nr) @@ -1591,7 +1591,7 @@ std::vector FffGcodeWriter::getUsedExtrudersOnLayer( assert(static_cast(extruder_count) > 0); std::vector ret; std::vector extruder_is_used_on_this_layer = storage.getExtrudersUsed(layer_nr); - const LayerIndex raft_base_layer_nr = -Raft::getTotalExtraLayers(); + const LayerIndex raft_base_layer_nr = -LayerIndex(Raft::getTotalExtraLayers()); Raft::LayerType layer_type = Raft::getLayerType(layer_nr); if (layer_type == Raft::RaftBase) @@ -1828,7 +1828,7 @@ void FffGcodeWriter::addMeshPartToGCode( // After a layer part, make sure the nozzle is inside the comb boundary, so we do not retract on the perimeter. if (added_something - && (! mesh_group_settings.get("magic_spiralize") || gcode_layer.getLayerNr() < static_cast(mesh.settings.get("initial_bottom_layers")))) + && (! mesh_group_settings.get("magic_spiralize") || gcode_layer.getLayerNr() < LayerIndex(mesh.settings.get("initial_bottom_layers")))) { coord_t innermost_wall_line_width = mesh.settings.get((mesh.settings.get("wall_line_count") > 1) ? "wall_line_width_x" : "wall_line_width_0"); if (gcode_layer.getLayerNr() == 0) @@ -2930,19 +2930,19 @@ bool FffGcodeWriter::processInsets( if (Application::getInstance().current_slice_->scene.current_mesh_group->settings.get("magic_spiralize")) { const size_t initial_bottom_layers = mesh.settings.get("initial_bottom_layers"); - const int layer_nr = gcode_layer.getLayerNr(); - if ((layer_nr < static_cast(initial_bottom_layers) + const auto layer_nr = gcode_layer.getLayerNr(); + if ((layer_nr < LayerIndex(initial_bottom_layers) && part.wall_toolpaths.empty()) // The bottom layers in spiralize mode are generated using the variable width paths - || (layer_nr >= static_cast(initial_bottom_layers) && part.spiral_wall.empty())) // The rest of the layers in spiralize mode are using the spiral wall + || (layer_nr >= LayerIndex(initial_bottom_layers) && part.spiral_wall.empty())) // The rest of the layers in spiralize mode are using the spiral wall { // nothing to do return false; } - if (gcode_layer.getLayerNr() >= static_cast(initial_bottom_layers)) + if (gcode_layer.getLayerNr() >= LayerIndex(initial_bottom_layers)) { spiralize = true; } - if (spiralize && gcode_layer.getLayerNr() == static_cast(initial_bottom_layers) + if (spiralize && gcode_layer.getLayerNr() == LayerIndex(initial_bottom_layers) && extruder_nr == mesh.settings.get("wall_0_extruder_nr").extruder_nr_) { // on the last normal layer first make the outer wall normally and then start a second outer wall from the same hight, but gradually moving upward added_something = true; diff --git a/src/FffPolygonGenerator.cpp b/src/FffPolygonGenerator.cpp index 10a1336995..0a0351e460 100644 --- a/src/FffPolygonGenerator.cpp +++ b/src/FffPolygonGenerator.cpp @@ -557,7 +557,7 @@ void FffPolygonGenerator::processInfillMesh(SliceDataStorage& storage, const siz coord_t surface_line_width = mesh.settings.get("wall_line_width_0"); mesh.layer_nr_max_filled_layer = -1; - for (LayerIndex layer_idx = 0; layer_idx < static_cast(mesh.layers.size()); layer_idx++) + for (LayerIndex layer_idx = 0; layer_idx < LayerIndex(mesh.layers.size()); layer_idx++) { SliceLayer& layer = mesh.layers[layer_idx]; @@ -585,7 +585,7 @@ void FffPolygonGenerator::processInfillMesh(SliceDataStorage& storage, const siz break; // all previous meshes have been processed } SliceMeshStorage& other_mesh = *storage.meshes[other_mesh_idx]; - if (layer_idx >= static_cast(other_mesh.layers.size())) + if (layer_idx >= LayerIndex(other_mesh.layers.size())) { // there can be no interaction between the infill mesh and this other non-infill mesh continue; } @@ -875,7 +875,7 @@ void FffPolygonGenerator::computePrintHeightStatistics(SliceDataStorage& storage } for (size_t extruder_nr = 0; extruder_nr < extruder_count; extruder_nr++) { - for (LayerIndex layer_nr = static_cast(mesh.layers.size()) - 1; layer_nr > max_print_height_per_extruder[extruder_nr]; layer_nr--) + for (LayerIndex layer_nr = LayerIndex(mesh.layers.size()) - 1; layer_nr > max_print_height_per_extruder[extruder_nr]; layer_nr--) { if (mesh.getExtruderIsUsed(extruder_nr, layer_nr)) { @@ -1097,7 +1097,7 @@ void FffPolygonGenerator::processFuzzyWalls(SliceMeshStorage& mesh) return false; }; - for (LayerIndex layer_nr = start_layer_nr; layer_nr < mesh.layers.size(); layer_nr++) + for (LayerIndex layer_nr = start_layer_nr; layer_nr < LayerIndex(mesh.layers.size()); layer_nr++) { SliceLayer& layer = mesh.layers[layer_nr]; for (SliceLayerPart& part : layer.parts) diff --git a/src/InterlockingGenerator.cpp b/src/InterlockingGenerator.cpp index abfab67ae7..92d921e0c4 100644 --- a/src/InterlockingGenerator.cpp +++ b/src/InterlockingGenerator.cpp @@ -222,10 +222,10 @@ void InterlockingGenerator::addBoundaryCells(const std::vector& layers, c std::vector InterlockingGenerator::computeUnionedVolumeRegions() const { - const size_t max_layer_count = std::max(mesh_a_.layers.size(), mesh_b_.layers.size()) + 1; // introduce ghost layer on top for correct skin computation of topmost layer. + const auto max_layer_count = std::max(mesh_a_.layers.size(), mesh_b_.layers.size()) + 1; // introduce ghost layer on top for correct skin computation of topmost layer. std::vector layer_regions(max_layer_count); - for (LayerIndex layer_nr = 0; layer_nr < max_layer_count; layer_nr++) + for (LayerIndex layer_nr = 0; layer_nr < LayerIndex(max_layer_count); layer_nr++) { Shape& layer_region = layer_regions[static_cast(layer_nr)]; for (Slicer* mesh : { &mesh_a_, &mesh_b_ }) diff --git a/src/slicer.cpp b/src/slicer.cpp index f66e2d40ec..aa8a4c64e9 100644 --- a/src/slicer.cpp +++ b/src/slicer.cpp @@ -1017,13 +1017,13 @@ void Slicer::makePolygons(Mesh& mesh, SlicingTolerance slicing_tolerance, std::v switch (slicing_tolerance) { case SlicingTolerance::INCLUSIVE: - for (LayerIndex layer_nr = 0; layer_nr + 1 < layers.size(); layer_nr++) + for (size_t layer_nr = 0; layer_nr + 1 < layers.size(); layer_nr++) { layers[layer_nr].polygons_ = layers[layer_nr].polygons_.unionPolygons(layers[layer_nr + 1].polygons_); } break; case SlicingTolerance::EXCLUSIVE: - for (LayerIndex layer_nr = 0; layer_nr + 1 < layers.size(); layer_nr++) + for (size_t layer_nr = 0; layer_nr + 1 < layers.size(); layer_nr++) { layers[layer_nr].polygons_ = layers[layer_nr].polygons_.intersection(layers[layer_nr + 1].polygons_); } From aa7cbbd849fad50bdd0b1f14e2d48e3cd0ab0854 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Wed, 28 Aug 2024 07:49:12 +0000 Subject: [PATCH 2/3] Applied clang-format. --- src/FffGcodeWriter.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/FffGcodeWriter.cpp b/src/FffGcodeWriter.cpp index 5a50c04452..9ad24e8ec0 100644 --- a/src/FffGcodeWriter.cpp +++ b/src/FffGcodeWriter.cpp @@ -1827,8 +1827,7 @@ void FffGcodeWriter::addMeshPartToGCode( added_something = added_something | processSkin(storage, gcode_layer, mesh, extruder_nr, mesh_config, part); // After a layer part, make sure the nozzle is inside the comb boundary, so we do not retract on the perimeter. - if (added_something - && (! mesh_group_settings.get("magic_spiralize") || gcode_layer.getLayerNr() < LayerIndex(mesh.settings.get("initial_bottom_layers")))) + if (added_something && (! mesh_group_settings.get("magic_spiralize") || gcode_layer.getLayerNr() < LayerIndex(mesh.settings.get("initial_bottom_layers")))) { coord_t innermost_wall_line_width = mesh.settings.get((mesh.settings.get("wall_line_count") > 1) ? "wall_line_width_x" : "wall_line_width_0"); if (gcode_layer.getLayerNr() == 0) @@ -2931,8 +2930,7 @@ bool FffGcodeWriter::processInsets( { const size_t initial_bottom_layers = mesh.settings.get("initial_bottom_layers"); const auto layer_nr = gcode_layer.getLayerNr(); - if ((layer_nr < LayerIndex(initial_bottom_layers) - && part.wall_toolpaths.empty()) // The bottom layers in spiralize mode are generated using the variable width paths + if ((layer_nr < LayerIndex(initial_bottom_layers) && part.wall_toolpaths.empty()) // The bottom layers in spiralize mode are generated using the variable width paths || (layer_nr >= LayerIndex(initial_bottom_layers) && part.spiral_wall.empty())) // The rest of the layers in spiralize mode are using the spiral wall { // nothing to do @@ -2942,8 +2940,7 @@ bool FffGcodeWriter::processInsets( { spiralize = true; } - if (spiralize && gcode_layer.getLayerNr() == LayerIndex(initial_bottom_layers) - && extruder_nr == mesh.settings.get("wall_0_extruder_nr").extruder_nr_) + if (spiralize && gcode_layer.getLayerNr() == LayerIndex(initial_bottom_layers) && extruder_nr == mesh.settings.get("wall_0_extruder_nr").extruder_nr_) { // on the last normal layer first make the outer wall normally and then start a second outer wall from the same hight, but gradually moving upward added_something = true; gcode_layer.setIsInside(true); // going to print stuff inside print object From 14630e645174c470026574922104f6d0e8e18c08 Mon Sep 17 00:00:00 2001 From: "c.lamboo" Date: Mon, 2 Sep 2024 11:37:32 +0200 Subject: [PATCH 3/3] Cast to `LayerIndex` earlier to prevent multiple LayerIndex casts NP-343 --- src/FffGcodeWriter.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/FffGcodeWriter.cpp b/src/FffGcodeWriter.cpp index 9ad24e8ec0..4f394d6f36 100644 --- a/src/FffGcodeWriter.cpp +++ b/src/FffGcodeWriter.cpp @@ -2928,25 +2928,25 @@ bool FffGcodeWriter::processInsets( bool spiralize = false; if (Application::getInstance().current_slice_->scene.current_mesh_group->settings.get("magic_spiralize")) { - const size_t initial_bottom_layers = mesh.settings.get("initial_bottom_layers"); + const auto initial_bottom_layers = LayerIndex(mesh.settings.get("initial_bottom_layers")); const auto layer_nr = gcode_layer.getLayerNr(); - if ((layer_nr < LayerIndex(initial_bottom_layers) && part.wall_toolpaths.empty()) // The bottom layers in spiralize mode are generated using the variable width paths - || (layer_nr >= LayerIndex(initial_bottom_layers) && part.spiral_wall.empty())) // The rest of the layers in spiralize mode are using the spiral wall + if ((layer_nr < initial_bottom_layers && part.wall_toolpaths.empty()) // The bottom layers in spiralize mode are generated using the variable width paths + || (layer_nr >= initial_bottom_layers && part.spiral_wall.empty())) // The rest of the layers in spiralize mode are using the spiral wall { // nothing to do return false; } - if (gcode_layer.getLayerNr() >= LayerIndex(initial_bottom_layers)) + if (gcode_layer.getLayerNr() >= initial_bottom_layers) { spiralize = true; } - if (spiralize && gcode_layer.getLayerNr() == LayerIndex(initial_bottom_layers) && extruder_nr == mesh.settings.get("wall_0_extruder_nr").extruder_nr_) + if (spiralize && gcode_layer.getLayerNr() == initial_bottom_layers && extruder_nr == mesh.settings.get("wall_0_extruder_nr").extruder_nr_) { // on the last normal layer first make the outer wall normally and then start a second outer wall from the same hight, but gradually moving upward added_something = true; gcode_layer.setIsInside(true); // going to print stuff inside print object // start this first wall at the same vertex the spiral starts const Polygon& spiral_inset = part.spiral_wall[0]; - const size_t spiral_start_vertex = storage.spiralize_seam_vertex_indices[initial_bottom_layers]; + const size_t spiral_start_vertex = storage.spiralize_seam_vertex_indices[static_cast(initial_bottom_layers.value)]; if (spiral_start_vertex < spiral_inset.size()) { gcode_layer.addTravel(spiral_inset[spiral_start_vertex]);