mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-13 09:47:58 -06:00
Fixing the "last item doesn't fit" problem.
This commit is contained in:
parent
6cdec7ac9a
commit
a7ba51bd11
5 changed files with 399 additions and 219 deletions
|
@ -684,6 +684,20 @@ struct ShapeLike {
|
||||||
return PointLike::distance(point, circ.center()) < circ.radius();
|
return PointLike::distance(point, circ.center()) < circ.radius();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class RawShape>
|
||||||
|
static bool isInside(const TPoint<RawShape>& point,
|
||||||
|
const _Box<TPoint<RawShape>>& box)
|
||||||
|
{
|
||||||
|
auto px = getX(point);
|
||||||
|
auto py = getY(point);
|
||||||
|
auto minx = getX(box.minCorner());
|
||||||
|
auto miny = getY(box.minCorner());
|
||||||
|
auto maxx = getX(box.maxCorner());
|
||||||
|
auto maxy = getY(box.maxCorner());
|
||||||
|
|
||||||
|
return px > minx && px < maxx && py > miny && py < maxy;
|
||||||
|
}
|
||||||
|
|
||||||
template<class RawShape>
|
template<class RawShape>
|
||||||
static bool isInside(const RawShape& sh,
|
static bool isInside(const RawShape& sh,
|
||||||
const _Circle<TPoint<RawShape>>& circ)
|
const _Circle<TPoint<RawShape>>& circ)
|
||||||
|
@ -702,6 +716,23 @@ struct ShapeLike {
|
||||||
isInside<RawShape>(box.maxCorner(), circ);
|
isInside<RawShape>(box.maxCorner(), circ);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class RawShape>
|
||||||
|
static bool isInside(const _Box<TPoint<RawShape>>& ibb,
|
||||||
|
const _Box<TPoint<RawShape>>& box)
|
||||||
|
{
|
||||||
|
auto iminX = getX(ibb.minCorner());
|
||||||
|
auto imaxX = getX(ibb.maxCorner());
|
||||||
|
auto iminY = getY(ibb.minCorner());
|
||||||
|
auto imaxY = getY(ibb.maxCorner());
|
||||||
|
|
||||||
|
auto minX = getX(box.minCorner());
|
||||||
|
auto maxX = getX(box.maxCorner());
|
||||||
|
auto minY = getY(box.minCorner());
|
||||||
|
auto maxY = getY(box.maxCorner());
|
||||||
|
|
||||||
|
return iminX > minX && imaxX < maxX && iminY > minY && imaxY < maxY;
|
||||||
|
}
|
||||||
|
|
||||||
template<class RawShape> // Potential O(1) implementation may exist
|
template<class RawShape> // Potential O(1) implementation may exist
|
||||||
static inline TPoint<RawShape>& vertex(RawShape& sh, unsigned long idx)
|
static inline TPoint<RawShape>& vertex(RawShape& sh, unsigned long idx)
|
||||||
{
|
{
|
||||||
|
|
|
@ -473,8 +473,7 @@ public:
|
||||||
|
|
||||||
template<class RawShape>
|
template<class RawShape>
|
||||||
inline bool _Item<RawShape>::isInside(const _Box<TPoint<RawShape>>& box) const {
|
inline bool _Item<RawShape>::isInside(const _Box<TPoint<RawShape>>& box) const {
|
||||||
_Rectangle<RawShape> rect(box.width(), box.height());
|
return ShapeLike::isInside<RawShape>(boundingBox(), box);
|
||||||
return _Item<RawShape>::isInside(rect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class RawShape> inline bool
|
template<class RawShape> inline bool
|
||||||
|
|
|
@ -166,7 +166,7 @@ template<class RawShape> class EdgeCache {
|
||||||
using std::pow;
|
using std::pow;
|
||||||
|
|
||||||
return static_cast<Coord>(
|
return static_cast<Coord>(
|
||||||
round( N/(ceil(pow(accuracy_, 2)*(N-1)) + 1) )
|
std::round(N/std::pow(N, std::pow(accuracy_, 1.0/3.0)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,6 +178,7 @@ template<class RawShape> class EdgeCache {
|
||||||
|
|
||||||
contour_.corners.reserve(N / S + 1);
|
contour_.corners.reserve(N / S + 1);
|
||||||
auto N_1 = N-1;
|
auto N_1 = N-1;
|
||||||
|
contour_.corners.emplace_back(0.0);
|
||||||
for(size_t i = 0; i < N_1; i += S) {
|
for(size_t i = 0; i < N_1; i += S) {
|
||||||
contour_.corners.emplace_back(
|
contour_.corners.emplace_back(
|
||||||
contour_.distances.at(i) / contour_.full_distance);
|
contour_.distances.at(i) / contour_.full_distance);
|
||||||
|
@ -192,6 +193,7 @@ template<class RawShape> class EdgeCache {
|
||||||
const auto S = stride(N);
|
const auto S = stride(N);
|
||||||
auto N_1 = N-1;
|
auto N_1 = N-1;
|
||||||
hc.corners.reserve(N / S + 1);
|
hc.corners.reserve(N / S + 1);
|
||||||
|
hc.corners.emplace_back(0.0);
|
||||||
for(size_t i = 0; i < N_1; i += S) {
|
for(size_t i = 0; i < N_1; i += S) {
|
||||||
hc.corners.emplace_back(
|
hc.corners.emplace_back(
|
||||||
hc.distances.at(i) / hc.full_distance);
|
hc.distances.at(i) / hc.full_distance);
|
||||||
|
@ -484,7 +486,7 @@ public:
|
||||||
bool static inline wouldFit(const RawShape& chull, const RawShape& bin) {
|
bool static inline wouldFit(const RawShape& chull, const RawShape& bin) {
|
||||||
auto bbch = sl::boundingBox<RawShape>(chull);
|
auto bbch = sl::boundingBox<RawShape>(chull);
|
||||||
auto bbin = sl::boundingBox<RawShape>(bin);
|
auto bbin = sl::boundingBox<RawShape>(bin);
|
||||||
auto d = bbin.center() - bbch.center();
|
auto d = bbch.center() - bbin.center();
|
||||||
auto chullcpy = chull;
|
auto chullcpy = chull;
|
||||||
sl::translate(chullcpy, d);
|
sl::translate(chullcpy, d);
|
||||||
return sl::isInside<RawShape>(chullcpy, bin);
|
return sl::isInside<RawShape>(chullcpy, bin);
|
||||||
|
@ -579,17 +581,21 @@ public:
|
||||||
pile_area += mitem.area();
|
pile_area += mitem.area();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto merged_pile = Nfp::merge(pile);
|
||||||
|
|
||||||
// This is the kernel part of the object function that is
|
// This is the kernel part of the object function that is
|
||||||
// customizable by the library client
|
// customizable by the library client
|
||||||
auto _objfunc = config_.object_function?
|
auto _objfunc = config_.object_function?
|
||||||
config_.object_function :
|
config_.object_function :
|
||||||
[this](Nfp::Shapes<RawShape>& pile, const Item& item,
|
[this, &merged_pile](
|
||||||
double occupied_area, double /*norm*/,
|
Nfp::Shapes<RawShape>& /*pile*/,
|
||||||
double penality)
|
const Item& item,
|
||||||
|
double occupied_area, double norm,
|
||||||
|
double /*penality*/)
|
||||||
{
|
{
|
||||||
pile.emplace_back(item.transformedShape());
|
merged_pile.emplace_back(item.transformedShape());
|
||||||
auto ch = sl::convexHull(pile);
|
auto ch = sl::convexHull(merged_pile);
|
||||||
pile.pop_back();
|
merged_pile.pop_back();
|
||||||
|
|
||||||
// The pack ratio -- how much is the convex hull occupied
|
// The pack ratio -- how much is the convex hull occupied
|
||||||
double pack_rate = occupied_area/sl::area(ch);
|
double pack_rate = occupied_area/sl::area(ch);
|
||||||
|
@ -602,7 +608,7 @@ public:
|
||||||
// (larger) values.
|
// (larger) values.
|
||||||
auto score = std::sqrt(waste);
|
auto score = std::sqrt(waste);
|
||||||
|
|
||||||
if(!wouldFit(ch, bin_)) score = 2*penality - score;
|
if(!wouldFit(ch, bin_)) score += norm;
|
||||||
|
|
||||||
return score;
|
return score;
|
||||||
};
|
};
|
||||||
|
@ -622,9 +628,22 @@ public:
|
||||||
return score;
|
return score;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
auto boundaryCheck = [&](const Optimum& o) {
|
||||||
|
auto v = getNfpPoint(o);
|
||||||
|
auto d = v - iv;
|
||||||
|
d += startpos;
|
||||||
|
item.translation(d);
|
||||||
|
|
||||||
|
merged_pile.emplace_back(item.transformedShape());
|
||||||
|
auto chull = sl::convexHull(merged_pile);
|
||||||
|
merged_pile.pop_back();
|
||||||
|
|
||||||
|
return wouldFit(chull, bin_);
|
||||||
|
};
|
||||||
|
|
||||||
opt::StopCriteria stopcr;
|
opt::StopCriteria stopcr;
|
||||||
stopcr.max_iterations = 1000;
|
stopcr.max_iterations = 100;
|
||||||
stopcr.absolute_score_difference = 1e-20*norm_;
|
stopcr.relative_score_difference = 1e-6;
|
||||||
opt::TOptimizer<opt::Method::L_SUBPLEX> solver(stopcr);
|
opt::TOptimizer<opt::Method::L_SUBPLEX> solver(stopcr);
|
||||||
|
|
||||||
Optimum optimum(0, 0);
|
Optimum optimum(0, 0);
|
||||||
|
@ -644,7 +663,7 @@ public:
|
||||||
std::for_each(cache.corners().begin(),
|
std::for_each(cache.corners().begin(),
|
||||||
cache.corners().end(),
|
cache.corners().end(),
|
||||||
[ch, &contour_ofn, &solver, &best_score,
|
[ch, &contour_ofn, &solver, &best_score,
|
||||||
&optimum] (double pos)
|
&optimum, &boundaryCheck] (double pos)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
auto result = solver.optimize_min(contour_ofn,
|
auto result = solver.optimize_min(contour_ofn,
|
||||||
|
@ -653,22 +672,15 @@ public:
|
||||||
);
|
);
|
||||||
|
|
||||||
if(result.score < best_score) {
|
if(result.score < best_score) {
|
||||||
|
Optimum o(std::get<0>(result.optimum), ch, -1);
|
||||||
|
if(boundaryCheck(o)) {
|
||||||
best_score = result.score;
|
best_score = result.score;
|
||||||
optimum.relpos = std::get<0>(result.optimum);
|
optimum = o;
|
||||||
optimum.nfpidx = ch;
|
}
|
||||||
optimum.hidx = -1;
|
|
||||||
}
|
}
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
derr() << "ERROR: " << e.what() << "\n";
|
derr() << "ERROR: " << e.what() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// auto sc = contour_ofn(pos);
|
|
||||||
// if(sc < best_score) {
|
|
||||||
// best_score = sc;
|
|
||||||
// optimum.relpos = pos;
|
|
||||||
// optimum.nfpidx = ch;
|
|
||||||
// optimum.hidx = -1;
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for(unsigned hidx = 0; hidx < cache.holeCount(); ++hidx) {
|
for(unsigned hidx = 0; hidx < cache.holeCount(); ++hidx) {
|
||||||
|
@ -683,7 +695,7 @@ public:
|
||||||
std::for_each(cache.corners(hidx).begin(),
|
std::for_each(cache.corners(hidx).begin(),
|
||||||
cache.corners(hidx).end(),
|
cache.corners(hidx).end(),
|
||||||
[&hole_ofn, &solver, &best_score,
|
[&hole_ofn, &solver, &best_score,
|
||||||
&optimum, ch, hidx]
|
&optimum, ch, hidx, &boundaryCheck]
|
||||||
(double pos)
|
(double pos)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -693,21 +705,16 @@ public:
|
||||||
);
|
);
|
||||||
|
|
||||||
if(result.score < best_score) {
|
if(result.score < best_score) {
|
||||||
best_score = result.score;
|
|
||||||
Optimum o(std::get<0>(result.optimum),
|
Optimum o(std::get<0>(result.optimum),
|
||||||
ch, hidx);
|
ch, hidx);
|
||||||
|
if(boundaryCheck(o)) {
|
||||||
|
best_score = result.score;
|
||||||
optimum = o;
|
optimum = o;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
derr() << "ERROR: " << e.what() << "\n";
|
derr() << "ERROR: " << e.what() << "\n";
|
||||||
}
|
}
|
||||||
// auto sc = hole_ofn(pos);
|
|
||||||
// if(sc < best_score) {
|
|
||||||
// best_score = sc;
|
|
||||||
// optimum.relpos = pos;
|
|
||||||
// optimum.nfpidx = ch;
|
|
||||||
// optimum.hidx = hidx;
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,237 @@ void toSVG(SVG& svg, const Model& model) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::tuple<double /*score*/, Box /*farthest point from bin center*/>
|
||||||
|
objfunc(const PointImpl& bincenter,
|
||||||
|
ShapeLike::Shapes<PolygonImpl>& pile, // The currently arranged pile
|
||||||
|
const Item &item,
|
||||||
|
double norm // A norming factor for physical dimensions
|
||||||
|
)
|
||||||
|
{
|
||||||
|
using pl = PointLike;
|
||||||
|
|
||||||
|
static const double BIG_ITEM_TRESHOLD = 0.2;
|
||||||
|
static const double ROUNDNESS_RATIO = 0.5;
|
||||||
|
static const double DENSITY_RATIO = 1.0 - ROUNDNESS_RATIO;
|
||||||
|
|
||||||
|
// We will treat big items (compared to the print bed) differently
|
||||||
|
NfpPlacer::Pile bigs;
|
||||||
|
bigs.reserve(pile.size());
|
||||||
|
for(auto& p : pile) {
|
||||||
|
auto pbb = ShapeLike::boundingBox(p);
|
||||||
|
auto na = std::sqrt(pbb.width()*pbb.height())/norm;
|
||||||
|
if(na > BIG_ITEM_TRESHOLD) bigs.emplace_back(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Candidate item bounding box
|
||||||
|
auto ibb = item.boundingBox();
|
||||||
|
|
||||||
|
// Calculate the full bounding box of the pile with the candidate item
|
||||||
|
pile.emplace_back(item.transformedShape());
|
||||||
|
auto fullbb = ShapeLike::boundingBox(pile);
|
||||||
|
pile.pop_back();
|
||||||
|
|
||||||
|
// The bounding box of the big items (they will accumulate in the center
|
||||||
|
// of the pile
|
||||||
|
auto bigbb = bigs.empty()? fullbb : ShapeLike::boundingBox(bigs);
|
||||||
|
|
||||||
|
// The size indicator of the candidate item. This is not the area,
|
||||||
|
// but almost...
|
||||||
|
auto itemnormarea = std::sqrt(ibb.width()*ibb.height())/norm;
|
||||||
|
|
||||||
|
// Will hold the resulting score
|
||||||
|
double score = 0;
|
||||||
|
|
||||||
|
if(itemnormarea > BIG_ITEM_TRESHOLD) {
|
||||||
|
// This branch is for the bigger items..
|
||||||
|
// Here we will use the closest point of the item bounding box to
|
||||||
|
// the already arranged pile. So not the bb center nor the a choosen
|
||||||
|
// corner but whichever is the closest to the center. This will
|
||||||
|
// prevent unwanted strange arrangements.
|
||||||
|
|
||||||
|
// Now the distance of the gravity center will be calculated to the
|
||||||
|
// five anchor points and the smallest will be chosen.
|
||||||
|
|
||||||
|
auto minc = ibb.minCorner(); // bottom left corner
|
||||||
|
auto maxc = ibb.maxCorner(); // top right corner
|
||||||
|
|
||||||
|
// top left and bottom right corners
|
||||||
|
auto top_left = PointImpl{getX(minc), getY(maxc)};
|
||||||
|
auto bottom_right = PointImpl{getX(maxc), getY(minc)};
|
||||||
|
|
||||||
|
// Now the distnce of the gravity center will be calculated to the
|
||||||
|
// five anchor points and the smallest will be chosen.
|
||||||
|
std::array<double, 5> dists;
|
||||||
|
auto cc = fullbb.center(); // The gravity center
|
||||||
|
dists[0] = pl::distance(minc, cc);
|
||||||
|
dists[1] = pl::distance(maxc, cc);
|
||||||
|
dists[2] = pl::distance(ibb.center(), cc);
|
||||||
|
dists[3] = pl::distance(top_left, cc);
|
||||||
|
dists[4] = pl::distance(bottom_right, cc);
|
||||||
|
|
||||||
|
auto dist = *(std::min_element(dists.begin(), dists.end())) / norm;
|
||||||
|
|
||||||
|
// Density is the pack density: how big is the arranged pile
|
||||||
|
auto density = std::sqrt(fullbb.width()*fullbb.height()) / norm;
|
||||||
|
|
||||||
|
// The score is a weighted sum of the distance from pile center
|
||||||
|
// and the pile size
|
||||||
|
score = ROUNDNESS_RATIO * dist + DENSITY_RATIO * density;
|
||||||
|
|
||||||
|
} else if(itemnormarea < BIG_ITEM_TRESHOLD && bigs.empty()) {
|
||||||
|
// If there are no big items, only small, we should consider the
|
||||||
|
// density here as well to not get silly results
|
||||||
|
auto bindist = pl::distance(ibb.center(), bincenter) / norm;
|
||||||
|
auto density = std::sqrt(fullbb.width()*fullbb.height()) / norm;
|
||||||
|
score = ROUNDNESS_RATIO * bindist + DENSITY_RATIO * density;
|
||||||
|
} else {
|
||||||
|
// Here there are the small items that should be placed around the
|
||||||
|
// already processed bigger items.
|
||||||
|
// No need to play around with the anchor points, the center will be
|
||||||
|
// just fine for small items
|
||||||
|
score = pl::distance(ibb.center(), bigbb.center()) / norm;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::make_tuple(score, fullbb);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class PConf>
|
||||||
|
void fillConfig(PConf& pcfg) {
|
||||||
|
|
||||||
|
// Align the arranged pile into the center of the bin
|
||||||
|
pcfg.alignment = PConf::Alignment::CENTER;
|
||||||
|
|
||||||
|
// Start placing the items from the center of the print bed
|
||||||
|
pcfg.starting_point = PConf::Alignment::CENTER;
|
||||||
|
|
||||||
|
// TODO cannot use rotations until multiple objects of same geometry can
|
||||||
|
// handle different rotations
|
||||||
|
// arranger.useMinimumBoundigBoxRotation();
|
||||||
|
pcfg.rotations = { 0.0 };
|
||||||
|
|
||||||
|
// The accuracy of optimization.
|
||||||
|
// Goes from 0.0 to 1.0 and scales performance as well
|
||||||
|
pcfg.accuracy = 0.35f;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TBin>
|
||||||
|
class AutoArranger {};
|
||||||
|
|
||||||
|
template<class TBin>
|
||||||
|
class _ArrBase {
|
||||||
|
protected:
|
||||||
|
using Placer = strategies::_NofitPolyPlacer<PolygonImpl, TBin>;
|
||||||
|
using Selector = FirstFitSelection;
|
||||||
|
using Packer = Arranger<Placer, Selector>;
|
||||||
|
using PConfig = typename Packer::PlacementConfig;
|
||||||
|
using Distance = TCoord<PointImpl>;
|
||||||
|
using Pile = ShapeLike::Shapes<PolygonImpl>;
|
||||||
|
|
||||||
|
Packer pck_;
|
||||||
|
PConfig pconf_; // Placement configuration
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
_ArrBase(const TBin& bin, Distance dist,
|
||||||
|
std::function<void(unsigned)> progressind):
|
||||||
|
pck_(bin, dist)
|
||||||
|
{
|
||||||
|
fillConfig(pconf_);
|
||||||
|
pck_.progressIndicator(progressind);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class...Args> inline IndexedPackGroup operator()(Args&&...args) {
|
||||||
|
return pck_.arrangeIndexed(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class AutoArranger<Box>: public _ArrBase<Box> {
|
||||||
|
public:
|
||||||
|
|
||||||
|
AutoArranger(const Box& bin, Distance dist,
|
||||||
|
std::function<void(unsigned)> progressind):
|
||||||
|
_ArrBase<Box>(bin, dist, progressind)
|
||||||
|
{
|
||||||
|
pconf_.object_function = [bin] (
|
||||||
|
Pile& pile,
|
||||||
|
const Item &item,
|
||||||
|
double /*occupied_area*/,
|
||||||
|
double norm,
|
||||||
|
double penality) {
|
||||||
|
|
||||||
|
auto result = objfunc(bin.center(), pile, item, norm);
|
||||||
|
double score = std::get<0>(result);
|
||||||
|
auto& fullbb = std::get<1>(result);
|
||||||
|
|
||||||
|
auto wdiff = fullbb.width() - bin.width();
|
||||||
|
auto hdiff = fullbb.height() - bin.height();
|
||||||
|
if(wdiff > 0) score += std::pow(wdiff, 2) / norm;
|
||||||
|
if(hdiff > 0) score += std::pow(hdiff, 2) / norm;
|
||||||
|
|
||||||
|
return score;
|
||||||
|
};
|
||||||
|
|
||||||
|
pck_.configure(pconf_);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class AutoArranger<PolygonImpl>: public _ArrBase<PolygonImpl> {
|
||||||
|
public:
|
||||||
|
AutoArranger(const PolygonImpl& bin, Distance dist,
|
||||||
|
std::function<void(unsigned)> progressind):
|
||||||
|
_ArrBase<PolygonImpl>(bin, dist, progressind)
|
||||||
|
{
|
||||||
|
pconf_.object_function = [&bin] (
|
||||||
|
Pile& pile,
|
||||||
|
const Item &item,
|
||||||
|
double /*area*/,
|
||||||
|
double norm,
|
||||||
|
double /*penality*/) {
|
||||||
|
|
||||||
|
auto binbb = ShapeLike::boundingBox(bin);
|
||||||
|
auto result = objfunc(binbb.center(), pile, item, norm);
|
||||||
|
double score = std::get<0>(result);
|
||||||
|
|
||||||
|
pile.emplace_back(item.transformedShape());
|
||||||
|
auto chull = ShapeLike::convexHull(pile);
|
||||||
|
pile.pop_back();
|
||||||
|
|
||||||
|
// If it does not fit into the print bed we will beat it with a
|
||||||
|
// large penality. If we would not do this, there would be only one
|
||||||
|
// big pile that doesn't care whether it fits onto the print bed.
|
||||||
|
if(!Placer::wouldFit(chull, bin)) score += norm;
|
||||||
|
|
||||||
|
return score;
|
||||||
|
};
|
||||||
|
|
||||||
|
pck_.configure(pconf_);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> // Specialization with no bin
|
||||||
|
class AutoArranger<bool>: public _ArrBase<Box> {
|
||||||
|
public:
|
||||||
|
|
||||||
|
AutoArranger(Distance dist, std::function<void(unsigned)> progressind):
|
||||||
|
_ArrBase<Box>(Box(0, 0), dist, progressind)
|
||||||
|
{
|
||||||
|
this->pconf_.object_function = [] (
|
||||||
|
Pile& pile,
|
||||||
|
const Item &item,
|
||||||
|
double /*area*/,
|
||||||
|
double norm,
|
||||||
|
double /*penality*/) {
|
||||||
|
|
||||||
|
auto result = objfunc({0, 0}, pile, item, norm);
|
||||||
|
return std::get<0>(result);
|
||||||
|
};
|
||||||
|
|
||||||
|
this->pck_.configure(pconf_);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// A container which stores a pointer to the 3D object and its projected
|
// A container which stores a pointer to the 3D object and its projected
|
||||||
// 2D shape from top view.
|
// 2D shape from top view.
|
||||||
using ShapeData2D =
|
using ShapeData2D =
|
||||||
|
@ -147,6 +378,44 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum BedShapeHint {
|
||||||
|
BOX,
|
||||||
|
CIRCLE,
|
||||||
|
IRREGULAR,
|
||||||
|
WHO_KNOWS
|
||||||
|
};
|
||||||
|
|
||||||
|
BedShapeHint bedShape(const Slic3r::Polyline& /*bed*/) {
|
||||||
|
// Determine the bed shape by hand
|
||||||
|
return BOX;
|
||||||
|
}
|
||||||
|
|
||||||
|
void applyResult(
|
||||||
|
IndexedPackGroup::value_type& group,
|
||||||
|
Coord batch_offset,
|
||||||
|
ShapeData2D& shapemap)
|
||||||
|
{
|
||||||
|
for(auto& r : group) {
|
||||||
|
auto idx = r.first; // get the original item index
|
||||||
|
Item& item = r.second; // get the item itself
|
||||||
|
|
||||||
|
// Get the model instance from the shapemap using the index
|
||||||
|
ModelInstance *inst_ptr = shapemap[idx].first;
|
||||||
|
|
||||||
|
// Get the tranformation data from the item object and scale it
|
||||||
|
// appropriately
|
||||||
|
auto off = item.translation();
|
||||||
|
Radians rot = item.rotation();
|
||||||
|
Pointf foff(off.X*SCALING_FACTOR + batch_offset,
|
||||||
|
off.Y*SCALING_FACTOR);
|
||||||
|
|
||||||
|
// write the tranformation data into the model instance
|
||||||
|
inst_ptr->rotation = rot;
|
||||||
|
inst_ptr->offset = foff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Arranges the model objects on the screen.
|
* \brief Arranges the model objects on the screen.
|
||||||
*
|
*
|
||||||
|
@ -170,7 +439,9 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model) {
|
||||||
* bed or leave them untouched (let the user arrange them by hand or remove
|
* bed or leave them untouched (let the user arrange them by hand or remove
|
||||||
* them).
|
* them).
|
||||||
*/
|
*/
|
||||||
bool arrange(Model &model, coordf_t dist, const Slic3r::BoundingBoxf* bb,
|
bool arrange(Model &model, coordf_t min_obj_distance,
|
||||||
|
const Slic3r::Polyline& bed,
|
||||||
|
BedShapeHint bedhint,
|
||||||
bool first_bin_only,
|
bool first_bin_only,
|
||||||
std::function<void(unsigned)> progressind)
|
std::function<void(unsigned)> progressind)
|
||||||
{
|
{
|
||||||
|
@ -178,34 +449,23 @@ bool arrange(Model &model, coordf_t dist, const Slic3r::BoundingBoxf* bb,
|
||||||
|
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
|
|
||||||
// Create the arranger config
|
|
||||||
auto min_obj_distance = static_cast<Coord>(dist/SCALING_FACTOR);
|
|
||||||
|
|
||||||
// Get the 2D projected shapes with their 3D model instance pointers
|
// Get the 2D projected shapes with their 3D model instance pointers
|
||||||
auto shapemap = arr::projectModelFromTop(model);
|
auto shapemap = arr::projectModelFromTop(model);
|
||||||
|
|
||||||
bool hasbin = bb != nullptr && bb->defined;
|
|
||||||
double area_max = 0;
|
|
||||||
|
|
||||||
// Copy the references for the shapes only as the arranger expects a
|
// Copy the references for the shapes only as the arranger expects a
|
||||||
// sequence of objects convertible to Item or ClipperPolygon
|
// sequence of objects convertible to Item or ClipperPolygon
|
||||||
std::vector<std::reference_wrapper<Item>> shapes;
|
std::vector<std::reference_wrapper<Item>> shapes;
|
||||||
shapes.reserve(shapemap.size());
|
shapes.reserve(shapemap.size());
|
||||||
std::for_each(shapemap.begin(), shapemap.end(),
|
std::for_each(shapemap.begin(), shapemap.end(),
|
||||||
[&shapes, min_obj_distance, &area_max, hasbin]
|
[&shapes] (ShapeData2D::value_type& it)
|
||||||
(ShapeData2D::value_type& it)
|
|
||||||
{
|
{
|
||||||
shapes.push_back(std::ref(it.second));
|
shapes.push_back(std::ref(it.second));
|
||||||
});
|
});
|
||||||
|
|
||||||
Box bin;
|
IndexedPackGroup result;
|
||||||
|
BoundingBox bbb(bed.points);
|
||||||
|
|
||||||
if(hasbin) {
|
auto binbb = Box({
|
||||||
// Scale up the bounding box to clipper scale.
|
|
||||||
BoundingBoxf bbb = *bb;
|
|
||||||
bbb.scale(1.0/SCALING_FACTOR);
|
|
||||||
|
|
||||||
bin = Box({
|
|
||||||
static_cast<libnest2d::Coord>(bbb.min.x),
|
static_cast<libnest2d::Coord>(bbb.min.x),
|
||||||
static_cast<libnest2d::Coord>(bbb.min.y)
|
static_cast<libnest2d::Coord>(bbb.min.y)
|
||||||
},
|
},
|
||||||
|
@ -213,180 +473,50 @@ bool arrange(Model &model, coordf_t dist, const Slic3r::BoundingBoxf* bb,
|
||||||
static_cast<libnest2d::Coord>(bbb.max.x),
|
static_cast<libnest2d::Coord>(bbb.max.x),
|
||||||
static_cast<libnest2d::Coord>(bbb.max.y)
|
static_cast<libnest2d::Coord>(bbb.max.y)
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// Will use the DJD selection heuristic with the BottomLeft placement
|
switch(bedhint) {
|
||||||
// strategy
|
case BOX: {
|
||||||
using Arranger = Arranger<NfpPlacer, FirstFitSelection>;
|
|
||||||
using PConf = Arranger::PlacementConfig;
|
|
||||||
using SConf = Arranger::SelectionConfig;
|
|
||||||
|
|
||||||
PConf pcfg; // Placement configuration
|
// Create the arranger for the box shaped bed
|
||||||
SConf scfg; // Selection configuration
|
AutoArranger<Box> arrange(binbb, min_obj_distance, progressind);
|
||||||
|
|
||||||
// Align the arranged pile into the center of the bin
|
|
||||||
pcfg.alignment = PConf::Alignment::CENTER;
|
|
||||||
|
|
||||||
// Start placing the items from the center of the print bed
|
|
||||||
pcfg.starting_point = PConf::Alignment::CENTER;
|
|
||||||
|
|
||||||
// TODO cannot use rotations until multiple objects of same geometry can
|
|
||||||
// handle different rotations
|
|
||||||
// arranger.useMinimumBoundigBoxRotation();
|
|
||||||
pcfg.rotations = { 0.0 };
|
|
||||||
|
|
||||||
// The accuracy of optimization. Goes from 0.0 to 1.0 and scales performance
|
|
||||||
pcfg.accuracy = 0.4f;
|
|
||||||
|
|
||||||
// Magic: we will specify what is the goal of arrangement... In this case
|
|
||||||
// we override the default object function to make the larger items go into
|
|
||||||
// the center of the pile and smaller items orbit it so the resulting pile
|
|
||||||
// has a circle-like shape. This is good for the print bed's heat profile.
|
|
||||||
// We alse sacrafice a bit of pack efficiency for this to work. As a side
|
|
||||||
// effect, the arrange procedure is a lot faster (we do not need to
|
|
||||||
// calculate the convex hulls)
|
|
||||||
pcfg.object_function = [bin, hasbin](
|
|
||||||
NfpPlacer::Pile& pile, // The currently arranged pile
|
|
||||||
const Item &item,
|
|
||||||
double /*area*/, // Sum area of items (not needed)
|
|
||||||
double norm, // A norming factor for physical dimensions
|
|
||||||
double penality) // Min penality in case of bad arrangement
|
|
||||||
{
|
|
||||||
using pl = PointLike;
|
|
||||||
|
|
||||||
static const double BIG_ITEM_TRESHOLD = 0.2;
|
|
||||||
static const double GRAVITY_RATIO = 0.5;
|
|
||||||
static const double DENSITY_RATIO = 1.0 - GRAVITY_RATIO;
|
|
||||||
|
|
||||||
// We will treat big items (compared to the print bed) differently
|
|
||||||
NfpPlacer::Pile bigs;
|
|
||||||
bigs.reserve(pile.size());
|
|
||||||
for(auto& p : pile) {
|
|
||||||
auto pbb = ShapeLike::boundingBox(p);
|
|
||||||
auto na = std::sqrt(pbb.width()*pbb.height())/norm;
|
|
||||||
if(na > BIG_ITEM_TRESHOLD) bigs.emplace_back(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Candidate item bounding box
|
|
||||||
auto ibb = item.boundingBox();
|
|
||||||
|
|
||||||
// Calculate the full bounding box of the pile with the candidate item
|
|
||||||
pile.emplace_back(item.transformedShape());
|
|
||||||
auto fullbb = ShapeLike::boundingBox(pile);
|
|
||||||
pile.pop_back();
|
|
||||||
|
|
||||||
// The bounding box of the big items (they will accumulate in the center
|
|
||||||
// of the pile
|
|
||||||
auto bigbb = bigs.empty()? fullbb : ShapeLike::boundingBox(bigs);
|
|
||||||
|
|
||||||
// The size indicator of the candidate item. This is not the area,
|
|
||||||
// but almost...
|
|
||||||
auto itemnormarea = std::sqrt(ibb.width()*ibb.height())/norm;
|
|
||||||
|
|
||||||
// Will hold the resulting score
|
|
||||||
double score = 0;
|
|
||||||
|
|
||||||
if(itemnormarea > BIG_ITEM_TRESHOLD) {
|
|
||||||
// This branch is for the bigger items..
|
|
||||||
// Here we will use the closest point of the item bounding box to
|
|
||||||
// the already arranged pile. So not the bb center nor the a choosen
|
|
||||||
// corner but whichever is the closest to the center. This will
|
|
||||||
// prevent unwanted strange arrangements.
|
|
||||||
|
|
||||||
auto minc = ibb.minCorner(); // bottom left corner
|
|
||||||
auto maxc = ibb.maxCorner(); // top right corner
|
|
||||||
|
|
||||||
// top left and bottom right corners
|
|
||||||
auto top_left = PointImpl{getX(minc), getY(maxc)};
|
|
||||||
auto bottom_right = PointImpl{getX(maxc), getY(minc)};
|
|
||||||
|
|
||||||
auto cc = fullbb.center(); // The gravity center
|
|
||||||
|
|
||||||
// Now the distnce of the gravity center will be calculated to the
|
|
||||||
// five anchor points and the smallest will be chosen.
|
|
||||||
std::array<double, 5> dists;
|
|
||||||
dists[0] = pl::distance(minc, cc);
|
|
||||||
dists[1] = pl::distance(maxc, cc);
|
|
||||||
dists[2] = pl::distance(ibb.center(), cc);
|
|
||||||
dists[3] = pl::distance(top_left, cc);
|
|
||||||
dists[4] = pl::distance(bottom_right, cc);
|
|
||||||
|
|
||||||
auto dist = *(std::min_element(dists.begin(), dists.end())) / norm;
|
|
||||||
|
|
||||||
// Density is the pack density: how big is the arranged pile
|
|
||||||
auto density = std::sqrt(fullbb.width()*fullbb.height()) / norm;
|
|
||||||
|
|
||||||
// The score is a weighted sum of the distance from pile center
|
|
||||||
// and the pile size
|
|
||||||
score = GRAVITY_RATIO * dist + DENSITY_RATIO * density;
|
|
||||||
|
|
||||||
} else if(itemnormarea < BIG_ITEM_TRESHOLD && bigs.empty()) {
|
|
||||||
// If there are no big items, only small, we should consider the
|
|
||||||
// density here as well to not get silly results
|
|
||||||
auto bindist = pl::distance(ibb.center(), bin.center()) / norm;
|
|
||||||
auto density = std::sqrt(fullbb.width()*fullbb.height()) / norm;
|
|
||||||
score = GRAVITY_RATIO * bindist + DENSITY_RATIO * density;
|
|
||||||
} else {
|
|
||||||
// Here there are the small items that should be placed around the
|
|
||||||
// already processed bigger items.
|
|
||||||
// No need to play around with the anchor points, the center will be
|
|
||||||
// just fine for small items
|
|
||||||
score = pl::distance(ibb.center(), bigbb.center()) / norm;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it does not fit into the print bed we will beat it
|
|
||||||
// with a large penality. If we would not do this, there would be only
|
|
||||||
// one big pile that doesn't care whether it fits onto the print bed.
|
|
||||||
if(!NfpPlacer::wouldFit(fullbb, bin)) score = 2*penality - score;
|
|
||||||
|
|
||||||
return score;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create the arranger object
|
|
||||||
Arranger arranger(bin, min_obj_distance, pcfg, scfg);
|
|
||||||
|
|
||||||
// Set the progress indicator for the arranger.
|
|
||||||
arranger.progressIndicator(progressind);
|
|
||||||
|
|
||||||
// Arrange and return the items with their respective indices within the
|
// Arrange and return the items with their respective indices within the
|
||||||
// input sequence.
|
// input sequence.
|
||||||
auto result = arranger.arrangeIndexed(shapes.begin(), shapes.end());
|
result = arrange(shapes.begin(), shapes.end());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CIRCLE:
|
||||||
|
break;
|
||||||
|
case IRREGULAR:
|
||||||
|
case WHO_KNOWS: {
|
||||||
|
using P = libnest2d::PolygonImpl;
|
||||||
|
|
||||||
auto applyResult = [&shapemap](ArrangeResult::value_type& group,
|
auto ctour = Slic3rMultiPoint_to_ClipperPath(bed);
|
||||||
Coord batch_offset)
|
P irrbed = ShapeLike::create<PolygonImpl>(std::move(ctour));
|
||||||
{
|
|
||||||
for(auto& r : group) {
|
|
||||||
auto idx = r.first; // get the original item index
|
|
||||||
Item& item = r.second; // get the item itself
|
|
||||||
|
|
||||||
// Get the model instance from the shapemap using the index
|
std::cout << ShapeLike::toString(irrbed) << std::endl;
|
||||||
ModelInstance *inst_ptr = shapemap[idx].first;
|
|
||||||
|
|
||||||
// Get the tranformation data from the item object and scale it
|
AutoArranger<P> arrange(irrbed, min_obj_distance, progressind);
|
||||||
// appropriately
|
|
||||||
auto off = item.translation();
|
|
||||||
Radians rot = item.rotation();
|
|
||||||
Pointf foff(off.X*SCALING_FACTOR + batch_offset,
|
|
||||||
off.Y*SCALING_FACTOR);
|
|
||||||
|
|
||||||
// write the tranformation data into the model instance
|
// Arrange and return the items with their respective indices within the
|
||||||
inst_ptr->rotation = rot;
|
// input sequence.
|
||||||
inst_ptr->offset = foff;
|
result = arrange(shapes.begin(), shapes.end());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if(first_bin_only) {
|
if(first_bin_only) {
|
||||||
applyResult(result.front(), 0);
|
applyResult(result.front(), 0, shapemap);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const auto STRIDE_PADDING = 1.2;
|
const auto STRIDE_PADDING = 1.2;
|
||||||
|
|
||||||
Coord stride = static_cast<Coord>(STRIDE_PADDING*
|
Coord stride = static_cast<Coord>(STRIDE_PADDING*
|
||||||
bin.width()*SCALING_FACTOR);
|
binbb.width()*SCALING_FACTOR);
|
||||||
Coord batch_offset = 0;
|
Coord batch_offset = 0;
|
||||||
|
|
||||||
for(auto& group : result) {
|
for(auto& group : result) {
|
||||||
applyResult(group, batch_offset);
|
applyResult(group, batch_offset, shapemap);
|
||||||
|
|
||||||
// Only the first pack group can be placed onto the print bed. The
|
// Only the first pack group can be placed onto the print bed. The
|
||||||
// other objects which could not fit will be placed next to the
|
// other objects which could not fit will be placed next to the
|
||||||
|
|
|
@ -294,6 +294,8 @@ void AppController::arrange_model()
|
||||||
supports_asynch()? std::launch::async : std::launch::deferred,
|
supports_asynch()? std::launch::async : std::launch::deferred,
|
||||||
[this]()
|
[this]()
|
||||||
{
|
{
|
||||||
|
using Coord = libnest2d::TCoord<libnest2d::PointImpl>;
|
||||||
|
|
||||||
unsigned count = 0;
|
unsigned count = 0;
|
||||||
for(auto obj : model_->objects) count += obj->instances.size();
|
for(auto obj : model_->objects) count += obj->instances.size();
|
||||||
|
|
||||||
|
@ -311,14 +313,25 @@ void AppController::arrange_model()
|
||||||
|
|
||||||
auto dist = print_ctl()->config().min_object_distance();
|
auto dist = print_ctl()->config().min_object_distance();
|
||||||
|
|
||||||
|
// Create the arranger config
|
||||||
|
auto min_obj_distance = static_cast<Coord>(dist/SCALING_FACTOR);
|
||||||
|
|
||||||
BoundingBoxf bb(print_ctl()->config().bed_shape.values);
|
auto& bedpoints = print_ctl()->config().bed_shape.values;
|
||||||
|
Polyline bed; bed.points.reserve(bedpoints.size());
|
||||||
|
for(auto& v : bedpoints)
|
||||||
|
bed.append(Point::new_scale(v.x, v.y));
|
||||||
|
|
||||||
if(pind) pind->update(0, _(L("Arranging objects...")));
|
if(pind) pind->update(0, _(L("Arranging objects...")));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
arr::arrange(*model_, dist, &bb, false, [pind, count](unsigned rem){
|
arr::arrange(*model_,
|
||||||
if(pind) pind->update(count - rem, _(L("Arranging objects...")));
|
min_obj_distance,
|
||||||
|
bed,
|
||||||
|
arr::BOX,
|
||||||
|
false, // create many piles not just one pile
|
||||||
|
[pind, count](unsigned rem) {
|
||||||
|
if(pind)
|
||||||
|
pind->update(count - rem, _(L("Arranging objects...")));
|
||||||
});
|
});
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue