Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ compile_commands.json
# Legacy device_page bundle copies (runtime lives in resources/web/device_page/dist/)
resources/web/device_page/assets/
resources/web/device_page/index.html
src/sparrow_arrange/target/

1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ option(SLIC3R_MSVC_COMPILE_PARALLEL "Compile on Visual Studio in parallel" 1)
option(SLIC3R_MSVC_PDB "Generate PDB files on MSVC in Release mode" 1)
option(SLIC3R_PERL_XS "Compile XS Perl module and enable Perl unit and integration tests" 0)
option(SLIC3R_ASAN "Enable ASan on Clang and GCC" 0)
option(SLIC3R_SPARROW_ARRANGE "Build and use the sparrow (Rust) 2D packing backend for Arrange" ON)
# If SLIC3R_FHS is 1 -> SLIC3R_DESKTOP_INTEGRATION is always 0, othrewise variable.
CMAKE_DEPENDENT_OPTION(SLIC3R_DESKTOP_INTEGRATION "Allow performing desktop integration during runtime" 1 "NOT SLIC3R_FHS" 0)

Expand Down
21 changes: 21 additions & 0 deletions src/BambuStudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4911,6 +4911,23 @@ int CLI::run(int argc, char **argv)
bool user_center_specified = false;
Points beds = get_bed_shape(m_print_config);
ArrangeParams arrange_cfg;
//BBS: BBS_ARRANGE_SPARROW=1 selects the sparrow packer for the CLI. Set before
// any ArrangePolygon is collected so get_arrange_polygon() emits true outlines.
const char *sparrow_env = std::getenv("BBS_ARRANGE_SPARROW");
const bool use_sparrow = sparrow_env && std::string(sparrow_env) == "1";
arrangement::use_true_outline.store(use_sparrow, std::memory_order_relaxed);
if (use_sparrow)
BOOST_LOG_TRIVIAL(info) << "BBS_ARRANGE_SPARROW=1: using the sparrow packer";
//BBS: BBS_ARRANGE_SPARROW_TIME=<seconds> overrides the per-plate search budget.
float sparrow_time_limit_s = 8.f;
if (const char *t = std::getenv("BBS_ARRANGE_SPARROW_TIME")) {
try {
sparrow_time_limit_s = std::stof(t);
BOOST_LOG_TRIVIAL(info) << "BBS_ARRANGE_SPARROW_TIME=" << sparrow_time_limit_s << "s per plate";
} catch (const std::exception &) {
BOOST_LOG_TRIVIAL(warning) << "ignoring invalid BBS_ARRANGE_SPARROW_TIME=" << t;
}
}

BOOST_LOG_TRIVIAL(info) << "will start transforms, commands count " << m_transforms.size() << "\n";
#if defined(__linux__) || defined(__LINUX__)
Expand Down Expand Up @@ -5427,6 +5444,8 @@ int CLI::run(int argc, char **argv)

//Step-2:prepare the arrange params
arrange_cfg.allow_rotations = allow_rotations;
arrange_cfg.use_sparrow = use_sparrow;
arrange_cfg.sparrow_time_limit_s = sparrow_time_limit_s;
arrange_cfg.allow_multi_materials_on_same_plate = allow_multicolor_oneplate;
arrange_cfg.avoid_extrusion_cali_region = avoid_extrusion_cali_region;
arrange_cfg.clearance_height_to_rod = height_to_rod;
Expand Down Expand Up @@ -5879,6 +5898,8 @@ int CLI::run(int argc, char **argv)

//Step-2:prepare the arrange params
arrange_cfg.allow_rotations = allow_rotations;
arrange_cfg.use_sparrow = use_sparrow;
arrange_cfg.sparrow_time_limit_s = sparrow_time_limit_s;
arrange_cfg.allow_multi_materials_on_same_plate = allow_multicolor_oneplate;
arrange_cfg.avoid_extrusion_cali_region = avoid_extrusion_cali_region;
arrange_cfg.clearance_height_to_rod = height_to_rod;
Expand Down
22 changes: 22 additions & 0 deletions src/libslic3r/Arrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "Print.hpp"
#include "BoundingBox.hpp"

#ifdef SLIC3R_SPARROW_ARRANGE
#include "ArrangeSparrow.hpp"
#endif

#include <libnest2d/backends/libslic3r/geometries.hpp>
#include <libnest2d/optimizers/nlopt/subplex.hpp>
#include <libnest2d/placers/nfpplacer.hpp>
Expand Down Expand Up @@ -77,6 +81,8 @@ using SpatElement = std::pair<Box, unsigned>;
using SpatIndex = bgi::rtree< SpatElement, bgi::rstar<16, 4> >;
using ItemGroup = std::vector<std::reference_wrapper<Item>>;

std::atomic<bool> use_true_outline{false};

// A coefficient used in separating bigger items and smaller items.
const double BIG_ITEM_TRESHOLD = 0.02;
#define VITRIFY_TEMP_DIFF_THRSH 15 // bed temp can be higher than vitrify temp, but not higher than this thresh
Expand Down Expand Up @@ -1191,6 +1197,22 @@ void arrange(ArrangePolygons & arrangables,
{
namespace clppr = Slic3r::ClipperLib;

#ifdef SLIC3R_SPARROW_ARRANGE
// BBS: sparrow handles rectangular beds only (every Bambu printer); other bed
// types and a failed sparrow run fall through to libnest2d. It has no notion of
// print order, height or per-plate filament grouping, so sequential print and
// "one material per plate" also stay on libnest2d.
if (params.use_sparrow && !params.is_seq_print && params.allow_multi_materials_on_same_plate) {
if constexpr (std::is_same_v<BedT, BoundingBox>) {
if (arrange_sparrow(arrangables, excludes, bed, params))
return;
BOOST_LOG_TRIVIAL(warning) << "sparrow arrange unavailable, using libnest2d";
} else {
BOOST_LOG_TRIVIAL(warning) << "sparrow arrange needs a rectangular bed, using libnest2d";
}
}
#endif

std::vector<Item> items, fixeditems;
items.reserve(arrangables.size());

Expand Down
14 changes: 14 additions & 0 deletions src/libslic3r/Arrange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "ExPolygon.hpp"
#include "PrintConfig.hpp"

#include <atomic>

#define BED_SHRINK_SEQ_PRINT 0

namespace Slic3r {
Expand Down Expand Up @@ -31,6 +33,11 @@ struct InfiniteBed {
explicit InfiniteBed(const Point &p = {0, 0}): center{p} {}
};

/// Set by the arrange caller (ArrangeJob, the CLI) before ArrangePolygons are
/// collected; read by ModelInstance::get_arrange_polygon() to choose between the
/// convex hull and the true outline. A global because that function has no ArrangeParams.
extern std::atomic<bool> use_true_outline;

/// A logical bed representing an object not being arranged. Either the arrange
/// has not yet successfully run on this ArrangePolygon or it could not fit the
/// object due to overly large size or invalid geometry.
Expand Down Expand Up @@ -127,6 +134,11 @@ struct ArrangeParams {

bool allow_rotations = false;

//BBS: use the sparrow (Rust) packer instead of the libnest2d NFP placer
bool use_sparrow = false;
/// Sparrow search budget in seconds, per plate (not for the whole run).
float sparrow_time_limit_s = 8.f;

bool do_final_align = true;

//BBS: add specific arrange params
Expand Down Expand Up @@ -170,6 +182,8 @@ struct ArrangeParams {
ret += "\"accuracy\":" + std::to_string(accuracy) + ",";
ret += "\"parallel\":" + std::to_string(parallel) + ",";
ret += "\"allow_rotations\":" + std::to_string(allow_rotations) + ",";
ret += "\"use_sparrow\":" + std::to_string(use_sparrow) + ",";
ret += "\"sparrow_time_limit_s\":" + std::to_string(sparrow_time_limit_s) + ",";
ret += "\"do_final_align\":" + std::to_string(do_final_align) + ",";
ret += "\"allow_multi_materials_on_same_plate\":" + std::to_string(allow_multi_materials_on_same_plate) + ",";
ret += "\"avoid_extrusion_cali_region\":" + std::to_string(avoid_extrusion_cali_region) + ",";
Expand Down
Loading
Loading