update libslic3r

This commit is contained in:
QIDI TECH
2025-08-04 10:13:51 +08:00
parent e3f49c2fb5
commit 8d4d60ec48
96 changed files with 4993 additions and 1903 deletions

View File

@@ -22,7 +22,7 @@ set(LIBNEST2D_SRCFILES
)
add_library(libnest2d STATIC ${LIBNEST2D_SRCFILES})
message(STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}")
target_include_directories(libnest2d PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
target_link_libraries(libnest2d PUBLIC NLopt::nlopt TBB::tbb Boost::boost libslic3r)
target_compile_definitions(libnest2d PUBLIC LIBNEST2D_THREADING_tbb LIBNEST2D_STATIC LIBNEST2D_OPTIMIZER_nlopt LIBNEST2D_GEOMETRIES_libslic3r)

View File

@@ -11,10 +11,11 @@
#include <libnest2d/geometry_traits.hpp>
#define LARGE_COST_TO_REJECT 1e7
#define COST_OF_NEW_PLATE 0.1
#define MIN_SEPARATION scale_(0.5) // ensure minimal separation between items
namespace libnest2d {
static const constexpr int BIN_ID_UNSET = -1;
static const constexpr int BIN_ID_UNSET = -2;
static const constexpr int BIN_ID_UNFIT = -1;
/**
@@ -88,7 +89,7 @@ public:
bool is_wipe_tower{ false };
bool is_extrusion_cali_object{ false };
bool has_tried_without_extrusion_cali_obj{ false };
std::vector<double> allowed_rotations{0.};
std::vector<std::pair<double, Coord>> allowed_rotations{{0., 0.}}; // <rotation, inflation>
/// The type of the shape which was handed over as the template argument.
using ShapeType = RawShape;

View File

@@ -574,6 +574,10 @@ public:
if (r) {
r.item_ptr_->translation(r.move_);
r.item_ptr_->rotation(r.rot_);
if (r.item_ptr_->inflation() < MIN_SEPARATION) {
r.item_ptr_->inflation(MIN_SEPARATION);
}
items_.emplace_back(*(r.item_ptr_));
merged_pile_ = nfp::merge(merged_pile_, r.item_ptr_->transformedShape());
score_ += r.score();
@@ -721,6 +725,7 @@ private:
auto initial_rot = item.rotation();
Vertex final_tr = {0, 0};
Radians final_rot = initial_rot;
Coord final_infl = item.inflation();
Shapes nfps;
auto& bin = bin_;
@@ -775,53 +780,11 @@ private:
.str());
}
bool first_object = std::all_of(items_.begin(), items_.end(), [&](const Item &rawShape) { return rawShape.is_virt_object && !rawShape.is_wipe_tower; });
if (first_object) {
setInitialPosition(item);
auto best_tr = item.translation();
auto best_rot = item.rotation();
best_overfit = std::numeric_limits<double>::max();
// try normal inflation first, then 0 inflation if not fit. See STUDIO-5566.
// Note for by-object printing, bed is expanded by -config_.bed_shrink.x().
Coord inflation_back = item.inflation();
Coord inflations[2]={inflation_back, 0};
for (size_t i = 0; i < 2; i++) {
item.inflation(inflations[i]);
for (auto rot : item.allowed_rotations) {
item.translation(initial_tr);
item.rotation(initial_rot + rot);
setInitialPosition(item);
if (!overlapWithVirtObject(item, binbb)) {
double of = overfit(item.transformedShape(), bin_);
if (of < best_overfit) {
best_overfit = of;
best_tr = item.translation();
best_rot = item.rotation();
if (best_overfit <= 0) {
config_.progressFunc("First object " + item.name + " can fit with rot=" + std::to_string(rot));
break;
}
}
}
}
can_pack = best_overfit <= 0;
if(can_pack) break;
}
item.inflation(inflation_back);
if (can_pack)
global_score = 0.2;
item.rotation(best_rot);
item.translation(best_tr);
}
if (can_pack == false) {
{
Pile merged_pile = merged_pile_;
for(auto rot : item.allowed_rotations) {
for (auto [rot, infl] : item.allowed_rotations) {
item.inflation(infl);
item.translation(initial_tr);
item.rotation(initial_rot + rot);
item.boundingBox(); // fill the bb cache
@@ -829,10 +792,8 @@ private:
// place the new item outside of the print bed to make sure
// it is disjunct from the current merged pile
placeOutsideOfBin(item);
nfps = calcnfp(item, binbb, Lvl<MaxNfpLevel::value>());
auto iv = item.referenceVertex();
auto startpos = item.translation();
@@ -860,27 +821,6 @@ private:
ecache[opt.nfpidx].coords(opt.hidx, opt.relpos);
};
auto alignment = config_.alignment;
auto boundaryCheck = [alignment, &merged_pile, &getNfpPoint,
&item, &bin, &iv, &startpos] (const Optimum& o)
{
auto v = getNfpPoint(o);
auto d = (v - iv) + startpos;
item.translation(d);
merged_pile.emplace_back(item.transformedShape());
auto chull = sl::convexHull(merged_pile);
merged_pile.pop_back();
double miss = 0;
if(alignment == Config::Alignment::DONT_ALIGN)
miss = sl::isInside(chull, bin) ? -1.0 : 1.0;
else miss = overfit(chull, bin);
return miss;
};
Optimum optimum(0, 0);
double best_score = std::numeric_limits<double>::max();
std::launch policy = std::launch::deferred;
@@ -929,23 +869,16 @@ private:
}
}, policy);
auto resultcomp =
[]( const OptResult& r1, const OptResult& r2 ) {
auto resultcomp = []( const OptResult& r1, const OptResult& r2 ) {
return r1.score < r2.score;
};
auto mr = *std::min_element(results.begin(), results.end(),
resultcomp);
auto mr = *std::min_element(results.begin(), results.end(), resultcomp);
if(mr.score < best_score) {
Optimum o(std::get<0>(mr.optimum), ch, -1);
double miss = boundaryCheck(o);
if(miss <= 0) {
best_score = mr.score;
optimum = o;
} else {
best_overfit = std::min(miss, best_overfit);
}
best_score = mr.score;
optimum = o;
}
for(unsigned hidx = 0; hidx < cache.holeCount(); ++hidx) {
@@ -981,20 +914,12 @@ private:
}
}, policy);
auto hmr = *std::min_element(results.begin(),
results.end(),
resultcomp);
auto hmr = *std::min_element(results.begin(), results.end(), resultcomp);
if(hmr.score < best_score) {
Optimum o(std::get<0>(hmr.optimum),
ch, hidx);
double miss = boundaryCheck(o);
if(miss <= 0.0) {
best_score = hmr.score;
optimum = o;
} else {
best_overfit = std::min(miss, best_overfit);
}
Optimum o(std::get<0>(hmr.optimum), ch, hidx);
best_score = hmr.score;
optimum = o;
}
}
}
@@ -1003,12 +928,14 @@ private:
auto d = (getNfpPoint(optimum) - iv) + startpos;
final_tr = d;
final_rot = initial_rot + rot;
final_infl = infl;
can_pack = true;
global_score = best_score;
break;
}
}
item.inflation(final_infl);
item.translation(final_tr);
item.rotation(final_rot);
}
@@ -1038,6 +965,7 @@ private:
auto initial_rot = item.rotation();
Vertex final_tr = initial_tr;
Radians final_rot = initial_rot;
Coord final_infl = item.inflation();
Shapes nfps;
auto binbb = sl::boundingBox(bin_);
@@ -1047,9 +975,10 @@ private:
unscale_(it.get().translation()[1]) % plateID())
.str());
}
{
for (auto rot : item.allowed_rotations) {
for (auto [rot,infl] : item.allowed_rotations) {
item.inflation(infl);
item.translation(initial_tr);
item.rotation(initial_rot + rot);
@@ -1057,11 +986,13 @@ private:
can_pack = true;
final_tr = initial_tr;
final_rot = initial_rot + rot;
final_infl = infl;
global_score = 0.3;
break;
}
}
item.inflation(final_infl);
item.translation(final_tr);
item.rotation(final_rot);
}
@@ -1096,9 +1027,9 @@ private:
std::stringstream ss;
ss.setf(std::ios::fixed | std::ios::showpoint);
ss.precision(1);
ss << "t=" << round(item.translation().x() / 1e6) << ","
<< round(item.translation().y() / 1e6)
//<< "-rot=" << round(item.rotation().toDegrees())
ss << "t=" << round(unscale_( item.translation().x() )) << ","
<< round(unscale_(item.translation().y()))
<< "-rot=" << round(item.rotation())
<< "-sco=" << round(global_score);
svgwriter.draw_text(20, 20, ss.str(), "blue", 10);
ss.str("");

View File

@@ -89,7 +89,9 @@ public:
std::stringstream ss;
ss << "initial order: " << it->get().name << ", p=" << it->get().priority() << ", bed_temp=" << it->get().bed_temp << ", height=" << it->get().height
<< ", area=" << it->get().area() << ", allowed_rotations=";
for(auto r: it->get().allowed_rotations) ss << r << ", ";
for (auto [r, i] : it->get().allowed_rotations)
ss << "<" << r << "," << i << ">"
<< ", ";
if (this->unfitindicator_)
this->unfitindicator_(ss.str());
}
@@ -102,7 +104,9 @@ public:
for (auto it = fixed_bins[i].begin(); it != fixed_bins[i].end(); ++it) {
ss << it->get().name << ", p=" << it->get().priority() << ", bed_temp=" << it->get().bed_temp << ", height=" << it->get().height
<< ", area=" << it->get().area() << ", allowed_rotations=";
for(auto r: it->get().allowed_rotations) ss << r << ", ";
for (auto [r, i] : it->get().allowed_rotations)
ss << "<" << r << "," << i << ">"
<< ", ";
ss << "; ";
}
if (this->unfitindicator_)

View File

@@ -6,6 +6,7 @@
#include <string>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <libnest2d/nester.hpp>
namespace libnest2d { namespace svg {