2019-07-01 16:22:07 +00:00
|
|
|
#include "Arrange.hpp"
|
2019-04-18 10:45:43 +00:00
|
|
|
#include "Geometry.hpp"
|
2018-11-12 13:52:52 +00:00
|
|
|
#include "SVG.hpp"
|
2019-06-24 11:01:52 +00:00
|
|
|
#include "MTUtils.hpp"
|
2018-11-12 13:52:52 +00:00
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
#include <libnest2d/backends/clipper/geometries.hpp>
|
|
|
|
#include <libnest2d/optimizers/nlopt/subplex.hpp>
|
|
|
|
#include <libnest2d/placers/nfpplacer.hpp>
|
|
|
|
#include <libnest2d/selections/firstfit.hpp>
|
2018-11-12 13:52:52 +00:00
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
#include <ClipperUtils.hpp>
|
|
|
|
|
|
|
|
#include <boost/geometry/index/rtree.hpp>
|
2019-06-06 12:27:07 +00:00
|
|
|
#include <boost/multiprecision/integer.hpp>
|
|
|
|
#include <boost/rational.hpp>
|
|
|
|
|
|
|
|
namespace libnest2d {
|
|
|
|
#if !defined(_MSC_VER) && defined(__SIZEOF_INT128__) && !defined(__APPLE__)
|
|
|
|
using LargeInt = __int128;
|
|
|
|
#else
|
|
|
|
using LargeInt = boost::multiprecision::int128_t;
|
2019-06-28 15:03:50 +00:00
|
|
|
template<> struct _NumTag<LargeInt>
|
|
|
|
{
|
|
|
|
using Type = ScalarTag;
|
|
|
|
};
|
2019-06-06 12:27:07 +00:00
|
|
|
#endif
|
2019-06-28 15:03:50 +00:00
|
|
|
|
|
|
|
template<class T> struct _NumTag<boost::rational<T>>
|
|
|
|
{
|
|
|
|
using Type = RationalTag;
|
|
|
|
};
|
2019-06-06 12:27:07 +00:00
|
|
|
|
|
|
|
namespace nfp {
|
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
template<class S> struct NfpImpl<S, NfpLevel::CONVEX_ONLY>
|
2019-06-06 12:27:07 +00:00
|
|
|
{
|
|
|
|
NfpResult<S> operator()(const S &sh, const S &other)
|
|
|
|
{
|
|
|
|
return nfpConvexOnly<S, boost::rational<LargeInt>>(sh, other);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
} // namespace nfp
|
|
|
|
} // namespace libnest2d
|
2018-11-12 13:52:52 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
template<class Tout = double, class = FloatingOnly<Tout>>
|
|
|
|
inline SLIC3R_CONSTEXPR EigenVec<Tout, 2> unscaled(
|
|
|
|
const ClipperLib::IntPoint &v) SLIC3R_NOEXCEPT
|
|
|
|
{
|
|
|
|
return EigenVec<Tout, 2>{unscaled<Tout>(v.X), unscaled<Tout>(v.Y)};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace arrangement {
|
2018-11-12 13:52:52 +00:00
|
|
|
|
|
|
|
using namespace libnest2d;
|
2019-06-28 15:03:50 +00:00
|
|
|
namespace clppr = ClipperLib;
|
2018-11-12 13:52:52 +00:00
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
// Get the libnest2d types for clipper backend
|
2019-06-28 15:03:50 +00:00
|
|
|
using Item = _Item<clppr::Polygon>;
|
|
|
|
using Box = _Box<clppr::IntPoint>;
|
|
|
|
using Circle = _Circle<clppr::IntPoint>;
|
|
|
|
using Segment = _Segment<clppr::IntPoint>;
|
|
|
|
using MultiPolygon = TMultiShape<clppr::Polygon>;
|
2019-07-03 13:06:10 +00:00
|
|
|
|
|
|
|
// The return value of nesting, a vector (for each logical bed) of Item
|
|
|
|
// reference vectors.
|
2019-06-28 15:03:50 +00:00
|
|
|
using PackGroup = _PackGroup<clppr::Polygon>;
|
2019-06-27 19:13:44 +00:00
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
// Summon the spatial indexing facilities from boost
|
2018-11-12 13:52:52 +00:00
|
|
|
namespace bgi = boost::geometry::index;
|
|
|
|
using SpatElement = std::pair<Box, unsigned>;
|
|
|
|
using SpatIndex = bgi::rtree< SpatElement, bgi::rstar<16, 4> >;
|
2019-06-28 15:03:50 +00:00
|
|
|
using ItemGroup = std::vector<std::reference_wrapper<Item>>;
|
2018-11-12 13:52:52 +00:00
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
// A coefficient used in separating bigger items and smaller items.
|
2018-11-12 13:52:52 +00:00
|
|
|
const double BIG_ITEM_TRESHOLD = 0.02;
|
|
|
|
|
2019-01-21 09:34:10 +00:00
|
|
|
// Fill in the placer algorithm configuration with values carefully chosen for
|
|
|
|
// Slic3r.
|
2018-11-12 13:52:52 +00:00
|
|
|
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
|
2019-07-03 13:06:10 +00:00
|
|
|
// handle different rotations.
|
2018-11-12 13:52:52 +00:00
|
|
|
pcfg.rotations = { 0.0 };
|
|
|
|
|
|
|
|
// The accuracy of optimization.
|
|
|
|
// Goes from 0.0 to 1.0 and scales performance as well
|
|
|
|
pcfg.accuracy = 0.65f;
|
2019-07-03 13:06:10 +00:00
|
|
|
|
|
|
|
// Allow parallel execution.
|
2018-11-12 13:52:52 +00:00
|
|
|
pcfg.parallel = true;
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:24:40 +00:00
|
|
|
// Apply penality to object function result. This is used only when alignment
|
|
|
|
// after arrange is explicitly disabled (PConfig::Alignment::DONT_ALIGN)
|
|
|
|
double fixed_overfit(const std::tuple<double, Box>& result, const Box &binbb)
|
|
|
|
{
|
|
|
|
double score = std::get<0>(result);
|
|
|
|
Box pilebb = std::get<1>(result);
|
|
|
|
Box fullbb = sl::boundingBox(pilebb, binbb);
|
|
|
|
double diff = fullbb.area() - binbb.area();
|
|
|
|
if(diff > 0) score += diff;
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
2018-11-12 13:52:52 +00:00
|
|
|
|
2019-01-21 09:34:10 +00:00
|
|
|
// A class encapsulating the libnest2d Nester class and extending it with other
|
|
|
|
// management and spatial index structures for acceleration.
|
2018-11-12 13:52:52 +00:00
|
|
|
template<class TBin>
|
2019-07-02 10:15:53 +00:00
|
|
|
class AutoArranger {
|
2019-06-19 10:04:11 +00:00
|
|
|
public:
|
2019-01-21 09:34:10 +00:00
|
|
|
// Useful type shortcuts...
|
2019-06-28 15:03:50 +00:00
|
|
|
using Placer = typename placers::_NofitPolyPlacer<clppr::Polygon, TBin>;
|
|
|
|
using Selector = selections::_FirstFitSelection<clppr::Polygon>;
|
2019-07-02 08:32:01 +00:00
|
|
|
using Packer = Nester<Placer, Selector>;
|
|
|
|
using PConfig = typename Packer::PlacementConfig;
|
2018-11-12 13:52:52 +00:00
|
|
|
using Distance = TCoord<PointImpl>;
|
|
|
|
|
2019-07-02 08:32:01 +00:00
|
|
|
protected:
|
|
|
|
Packer m_pck;
|
|
|
|
PConfig m_pconf; // Placement configuration
|
2019-07-02 10:15:53 +00:00
|
|
|
TBin m_bin;
|
2019-07-02 13:24:40 +00:00
|
|
|
double m_bin_area;
|
2019-07-02 08:32:01 +00:00
|
|
|
SpatIndex m_rtree; // spatial index for the normal (bigger) objects
|
2019-01-21 09:34:10 +00:00
|
|
|
SpatIndex m_smallsrtree; // spatial index for only the smaller items
|
2019-07-02 08:32:01 +00:00
|
|
|
double m_norm; // A coefficient to scale distances
|
2019-06-28 15:03:50 +00:00
|
|
|
MultiPolygon m_merged_pile; // The already merged pile (vector of items)
|
2019-07-02 08:32:01 +00:00
|
|
|
Box m_pilebb; // The bounding box of the merged pile.
|
|
|
|
ItemGroup m_remaining; // Remaining items (m_items at the beginning)
|
|
|
|
ItemGroup m_items; // The items to be packed
|
2019-07-02 10:15:53 +00:00
|
|
|
|
|
|
|
// This is "the" object function which is evaluated many times for each
|
|
|
|
// vertex (decimated with the accuracy parameter) of each object.
|
|
|
|
// Therefore it is upmost crucial for this function to be as efficient
|
|
|
|
// as it possibly can be but at the same time, it has to provide
|
|
|
|
// reasonable results.
|
|
|
|
std::tuple<double /*score*/, Box /*farthest point from bin center*/>
|
2019-07-02 13:24:40 +00:00
|
|
|
objfunc(const Item &item, const clppr::IntPoint &bincenter)
|
2019-07-02 10:15:53 +00:00
|
|
|
{
|
|
|
|
const double bin_area = m_bin_area;
|
|
|
|
const SpatIndex& spatindex = m_rtree;
|
|
|
|
const SpatIndex& smalls_spatindex = m_smallsrtree;
|
|
|
|
const ItemGroup& remaining = m_remaining;
|
|
|
|
|
|
|
|
// We will treat big items (compared to the print bed) differently
|
|
|
|
auto isBig = [bin_area](double a) {
|
|
|
|
return a/bin_area > BIG_ITEM_TRESHOLD ;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Candidate item bounding box
|
2019-07-03 13:06:10 +00:00
|
|
|
auto ibb = item.boundingBox();
|
2019-07-02 10:15:53 +00:00
|
|
|
|
|
|
|
// Calculate the full bounding box of the pile with the candidate item
|
2019-07-02 13:24:40 +00:00
|
|
|
auto fullbb = sl::boundingBox(m_pilebb, ibb);
|
2019-07-02 10:15:53 +00:00
|
|
|
|
|
|
|
// The bounding box of the big items (they will accumulate in the center
|
|
|
|
// of the pile
|
|
|
|
Box bigbb;
|
|
|
|
if(spatindex.empty()) bigbb = fullbb;
|
|
|
|
else {
|
|
|
|
auto boostbb = spatindex.bounds();
|
|
|
|
boost::geometry::convert(boostbb, bigbb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Will hold the resulting score
|
|
|
|
double score = 0;
|
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
// Density is the pack density: how big is the arranged pile
|
|
|
|
double density = 0;
|
|
|
|
|
|
|
|
const double N = m_norm;
|
|
|
|
auto norm = [N](double val) { return val / N; };
|
|
|
|
|
|
|
|
// Distinction of cases for the arrangement scene
|
|
|
|
enum e_cases {
|
|
|
|
// This branch is for big items in a mixed (big and small) scene
|
|
|
|
// OR for all items in a small-only scene.
|
|
|
|
BIG_ITEM,
|
2019-07-02 10:15:53 +00:00
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
// This branch is for the last big item in a mixed scene
|
|
|
|
LAST_BIG_ITEM,
|
2019-07-02 10:15:53 +00:00
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
// For small items in a mixed scene.
|
|
|
|
SMALL_ITEM
|
|
|
|
} compute_case;
|
|
|
|
|
|
|
|
bool bigitems = isBig(item.area()) || spatindex.empty();
|
|
|
|
if(bigitems && !remaining.empty()) compute_case = BIG_ITEM;
|
|
|
|
else if (bigitems && remaining.empty()) compute_case = LAST_BIG_ITEM;
|
|
|
|
else compute_case = SMALL_ITEM;
|
|
|
|
|
|
|
|
switch (compute_case) {
|
|
|
|
case BIG_ITEM: {
|
|
|
|
const clppr::IntPoint& minc = ibb.minCorner(); // bottom left corner
|
|
|
|
const clppr::IntPoint& maxc = ibb.maxCorner(); // top right corner
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
// top left and bottom right corners
|
2019-07-03 13:06:10 +00:00
|
|
|
clppr::IntPoint top_left{getX(minc), getY(maxc)};
|
|
|
|
clppr::IntPoint bottom_right{getX(maxc), getY(minc)};
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
// Now the distance 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);
|
2019-07-03 13:06:10 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
// The smalles distance from the arranged pile center:
|
2019-07-03 13:06:10 +00:00
|
|
|
double dist = norm(*(std::min_element(dists.begin(), dists.end())));
|
|
|
|
double bindist = norm(pl::distance(ibb.center(), bincenter));
|
|
|
|
dist = 0.8 * dist + 0.2*bindist;
|
|
|
|
|
|
|
|
// Prepare a variable for the alignment score.
|
|
|
|
// This will indicate: how well is the candidate item
|
|
|
|
// aligned with its neighbors. We will check the alignment
|
|
|
|
// with all neighbors and return the score for the best
|
|
|
|
// alignment. So it is enough for the candidate to be
|
|
|
|
// aligned with only one item.
|
|
|
|
auto alignment_score = 1.0;
|
|
|
|
|
|
|
|
auto query = bgi::intersects(ibb);
|
|
|
|
auto& index = isBig(item.area()) ? spatindex : smalls_spatindex;
|
|
|
|
|
|
|
|
// Query the spatial index for the neighbors
|
|
|
|
std::vector<SpatElement> result;
|
|
|
|
result.reserve(index.size());
|
|
|
|
|
|
|
|
index.query(query, std::back_inserter(result));
|
|
|
|
|
|
|
|
// now get the score for the best alignment
|
|
|
|
for(auto& e : result) {
|
|
|
|
auto idx = e.second;
|
|
|
|
Item& p = m_items[idx];
|
|
|
|
auto parea = p.area();
|
|
|
|
if(std::abs(1.0 - parea/item.area()) < 1e-6) {
|
|
|
|
auto bb = sl::boundingBox(p.boundingBox(), ibb);
|
|
|
|
auto bbarea = bb.area();
|
|
|
|
auto ascore = 1.0 - (item.area() + parea)/bbarea;
|
|
|
|
|
|
|
|
if(ascore < alignment_score) alignment_score = ascore;
|
2019-07-02 10:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-03 13:06:10 +00:00
|
|
|
|
|
|
|
density = std::sqrt(norm(fullbb.width()) * norm(fullbb.height()));
|
|
|
|
|
|
|
|
// The final mix of the score is the balance between the
|
|
|
|
// distance from the full pile center, the pack density and
|
|
|
|
// the alignment with the neighbors
|
|
|
|
if (result.empty())
|
|
|
|
score = 0.5 * dist + 0.5 * density;
|
|
|
|
else
|
|
|
|
score = 0.40 * dist + 0.40 * density + 0.2 * alignment_score;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LAST_BIG_ITEM: {
|
|
|
|
auto mp = m_merged_pile;
|
|
|
|
mp.emplace_back(item.transformedShape());
|
|
|
|
auto chull = sl::convexHull(mp);
|
|
|
|
|
|
|
|
placers::EdgeCache<clppr::Polygon> ec(chull);
|
|
|
|
|
|
|
|
double circ = norm(ec.circumference());
|
|
|
|
double bcirc = 2.0 * norm(fullbb.width() + fullbb.height());
|
|
|
|
score = 0.5 * circ + 0.5 * bcirc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SMALL_ITEM: {
|
2019-07-02 10:15:53 +00:00
|
|
|
// 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
|
2019-07-03 13:06:10 +00:00
|
|
|
score = norm(pl::distance(ibb.center(), bigbb.center()));
|
|
|
|
break;
|
|
|
|
}
|
2019-07-02 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_tuple(score, fullbb);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<double(const Item&)> get_objfn();
|
2019-07-02 08:32:01 +00:00
|
|
|
|
2018-11-12 13:52:52 +00:00
|
|
|
public:
|
2019-07-02 10:15:53 +00:00
|
|
|
AutoArranger(const TBin & bin,
|
|
|
|
Distance dist,
|
|
|
|
std::function<void(unsigned)> progressind,
|
|
|
|
std::function<bool(void)> stopcond)
|
|
|
|
: m_pck(bin, dist)
|
|
|
|
, m_bin(bin)
|
2019-07-03 13:06:10 +00:00
|
|
|
, m_bin_area(sl::area(bin))
|
|
|
|
, m_norm(std::sqrt(m_bin_area))
|
2018-11-12 13:52:52 +00:00
|
|
|
{
|
|
|
|
fillConfig(m_pconf);
|
|
|
|
|
2019-01-21 09:34:10 +00:00
|
|
|
// Set up a callback that is called just before arranging starts
|
|
|
|
// This functionality is provided by the Nester class (m_pack).
|
2018-11-12 13:52:52 +00:00
|
|
|
m_pconf.before_packing =
|
2019-06-28 15:03:50 +00:00
|
|
|
[this](const MultiPolygon& merged_pile, // merged pile
|
2018-11-12 13:52:52 +00:00
|
|
|
const ItemGroup& items, // packed items
|
|
|
|
const ItemGroup& remaining) // future items to be packed
|
|
|
|
{
|
|
|
|
m_items = items;
|
|
|
|
m_merged_pile = merged_pile;
|
|
|
|
m_remaining = remaining;
|
|
|
|
|
|
|
|
m_pilebb = sl::boundingBox(merged_pile);
|
|
|
|
|
|
|
|
m_rtree.clear();
|
|
|
|
m_smallsrtree.clear();
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2018-11-12 13:52:52 +00:00
|
|
|
// We will treat big items (compared to the print bed) differently
|
|
|
|
auto isBig = [this](double a) {
|
2019-07-02 13:24:40 +00:00
|
|
|
return a / m_bin_area > BIG_ITEM_TRESHOLD ;
|
2018-11-12 13:52:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for(unsigned idx = 0; idx < items.size(); ++idx) {
|
2019-06-28 15:03:50 +00:00
|
|
|
Item& itm = items[idx];
|
2018-11-12 13:52:52 +00:00
|
|
|
if(isBig(itm.area())) m_rtree.insert({itm.boundingBox(), idx});
|
|
|
|
m_smallsrtree.insert({itm.boundingBox(), idx});
|
|
|
|
}
|
|
|
|
};
|
2019-07-01 16:22:07 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
m_pconf.object_function = get_objfn();
|
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
if (progressind) m_pck.progressIndicator(progressind);
|
|
|
|
if (stopcond) m_pck.stopCondition(stopcond);
|
2019-07-02 10:15:53 +00:00
|
|
|
|
|
|
|
m_pck.configure(m_pconf);
|
2018-11-12 13:52:52 +00:00
|
|
|
}
|
2019-06-27 19:13:44 +00:00
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
template<class...Args> inline PackGroup operator()(Args&&...args) {
|
2018-11-12 13:52:52 +00:00
|
|
|
m_rtree.clear();
|
2019-06-27 19:13:44 +00:00
|
|
|
return m_pck.execute(std::forward<Args>(args)...);
|
2018-11-12 13:52:52 +00:00
|
|
|
}
|
2019-06-27 19:13:44 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
inline void preload(std::vector<Item>& fixeditems) {
|
2019-01-22 16:50:33 +00:00
|
|
|
m_pconf.alignment = PConfig::Alignment::DONT_ALIGN;
|
2019-07-02 13:24:40 +00:00
|
|
|
auto bb = sl::boundingBox(m_bin);
|
|
|
|
auto bbcenter = bb.center();
|
|
|
|
m_pconf.object_function = [this, bb, bbcenter](const Item &item) {
|
|
|
|
return fixed_overfit(objfunc(item, bbcenter), bb);
|
|
|
|
};
|
2019-01-22 16:50:33 +00:00
|
|
|
|
|
|
|
// Build the rtree for queries to work
|
2019-07-02 10:15:53 +00:00
|
|
|
|
|
|
|
for(unsigned idx = 0; idx < fixeditems.size(); ++idx) {
|
|
|
|
Item& itm = fixeditems[idx];
|
|
|
|
itm.markAsFixed();
|
2019-01-22 16:50:33 +00:00
|
|
|
m_rtree.insert({itm.boundingBox(), idx});
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pck.configure(m_pconf);
|
|
|
|
}
|
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
bool is_colliding(const Item& item) {
|
2019-01-23 09:37:37 +00:00
|
|
|
if(m_rtree.empty()) return false;
|
2019-01-22 16:50:33 +00:00
|
|
|
std::vector<SpatElement> result;
|
|
|
|
m_rtree.query(bgi::intersects(item.boundingBox()),
|
|
|
|
std::back_inserter(result));
|
2019-01-23 09:37:37 +00:00
|
|
|
return !result.empty();
|
2019-01-22 16:50:33 +00:00
|
|
|
}
|
2018-11-12 13:52:52 +00:00
|
|
|
};
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
template<> std::function<double(const Item&)> AutoArranger<Box>::get_objfn()
|
|
|
|
{
|
2019-07-02 13:24:40 +00:00
|
|
|
auto bincenter = m_bin.center();
|
|
|
|
|
|
|
|
return [this, bincenter](const Item &itm) {
|
|
|
|
auto result = objfunc(itm, bincenter);
|
2019-07-02 10:15:53 +00:00
|
|
|
|
|
|
|
double score = std::get<0>(result);
|
|
|
|
auto& fullbb = std::get<1>(result);
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
double miss = Placer::overfit(fullbb, m_bin);
|
|
|
|
miss = miss > 0? miss : 0;
|
|
|
|
score += miss*miss;
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
return score;
|
|
|
|
};
|
|
|
|
}
|
2018-11-12 13:52:52 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
template<> std::function<double(const Item&)> AutoArranger<Circle>::get_objfn()
|
|
|
|
{
|
2019-07-02 13:24:40 +00:00
|
|
|
auto bincenter = m_bin.center();
|
|
|
|
return [this, bincenter](const Item &item) {
|
|
|
|
|
|
|
|
auto result = objfunc(item, bincenter);
|
2019-07-02 10:15:53 +00:00
|
|
|
|
|
|
|
double score = std::get<0>(result);
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
auto isBig = [this](const Item& itm) {
|
2019-07-02 13:24:40 +00:00
|
|
|
return itm.area() / m_bin_area > BIG_ITEM_TRESHOLD ;
|
2018-11-12 13:52:52 +00:00
|
|
|
};
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
if(isBig(item)) {
|
|
|
|
auto mp = m_merged_pile;
|
|
|
|
mp.push_back(item.transformedShape());
|
|
|
|
auto chull = sl::convexHull(mp);
|
|
|
|
double miss = Placer::overfit(chull, m_bin);
|
|
|
|
if(miss < 0) miss = 0;
|
|
|
|
score += miss*miss;
|
|
|
|
}
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
return score;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:24:40 +00:00
|
|
|
// Specialization for a generalized polygon.
|
|
|
|
// Warning: this is unfinished business. It may or may not work.
|
|
|
|
template<>
|
|
|
|
std::function<double(const Item &)> AutoArranger<clppr::Polygon>::get_objfn()
|
2019-07-02 10:15:53 +00:00
|
|
|
{
|
2019-07-02 13:24:40 +00:00
|
|
|
auto bincenter = sl::boundingBox(m_bin).center();
|
|
|
|
return [this, bincenter](const Item &item) {
|
|
|
|
return std::get<0>(objfunc(item, bincenter));
|
|
|
|
};
|
2019-07-02 10:15:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
inline Circle to_lnCircle(const CircleBed& circ) {
|
|
|
|
return Circle({circ.center()(0), circ.center()(1)}, circ.radius());
|
2018-11-12 13:52:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 09:34:10 +00:00
|
|
|
// Get the type of bed geometry from a simple vector of points.
|
2018-11-12 13:52:52 +00:00
|
|
|
BedShapeHint bedShape(const Polyline &bed) {
|
|
|
|
BedShapeHint ret;
|
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
auto x = [](const Point& p) { return p(X); };
|
|
|
|
auto y = [](const Point& p) { return p(Y); };
|
2018-11-12 13:52:52 +00:00
|
|
|
|
|
|
|
auto width = [x](const BoundingBox& box) {
|
|
|
|
return x(box.max) - x(box.min);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto height = [y](const BoundingBox& box) {
|
|
|
|
return y(box.max) - y(box.min);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto area = [&width, &height](const BoundingBox& box) {
|
|
|
|
double w = width(box);
|
|
|
|
double h = height(box);
|
2019-06-28 15:03:50 +00:00
|
|
|
return w * h;
|
2018-11-12 13:52:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto poly_area = [](Polyline p) {
|
|
|
|
Polygon pp; pp.points.reserve(p.points.size() + 1);
|
|
|
|
pp.points = std::move(p.points);
|
|
|
|
pp.points.emplace_back(pp.points.front());
|
|
|
|
return std::abs(pp.area());
|
|
|
|
};
|
|
|
|
|
|
|
|
auto distance_to = [x, y](const Point& p1, const Point& p2) {
|
|
|
|
double dx = x(p2) - x(p1);
|
|
|
|
double dy = y(p2) - y(p1);
|
|
|
|
return std::sqrt(dx*dx + dy*dy);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto bb = bed.bounding_box();
|
|
|
|
|
|
|
|
auto isCircle = [bb, distance_to](const Polyline& polygon) {
|
|
|
|
auto center = bb.center();
|
|
|
|
std::vector<double> vertex_distances;
|
|
|
|
double avg_dist = 0;
|
|
|
|
for (auto pt: polygon.points)
|
|
|
|
{
|
|
|
|
double distance = distance_to(center, pt);
|
|
|
|
vertex_distances.push_back(distance);
|
|
|
|
avg_dist += distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
avg_dist /= vertex_distances.size();
|
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
CircleBed ret(center, avg_dist);
|
2018-12-20 11:08:06 +00:00
|
|
|
for(auto el : vertex_distances)
|
2018-11-12 13:52:52 +00:00
|
|
|
{
|
2018-12-20 11:08:06 +00:00
|
|
|
if (std::abs(el - avg_dist) > 10 * SCALED_EPSILON) {
|
2019-06-28 15:03:50 +00:00
|
|
|
ret = CircleBed();
|
2018-12-20 11:08:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-11-12 13:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto parea = poly_area(bed);
|
|
|
|
|
|
|
|
if( (1.0 - parea/area(bb)) < 1e-3 ) {
|
|
|
|
ret.type = BedShapeType::BOX;
|
|
|
|
ret.shape.box = bb;
|
|
|
|
}
|
|
|
|
else if(auto c = isCircle(bed)) {
|
|
|
|
ret.type = BedShapeType::CIRCLE;
|
|
|
|
ret.shape.circ = c;
|
|
|
|
} else {
|
|
|
|
ret.type = BedShapeType::IRREGULAR;
|
|
|
|
ret.shape.polygon = bed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the bed shape by hand
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
template<class BinT> // Arrange for arbitrary bin type
|
2019-06-28 16:27:15 +00:00
|
|
|
PackGroup _arrange(std::vector<Item> & shapes,
|
2019-07-02 10:15:53 +00:00
|
|
|
std::vector<Item> & excludes,
|
2019-06-28 15:03:50 +00:00
|
|
|
const BinT & bin,
|
|
|
|
coord_t minobjd,
|
|
|
|
std::function<void(unsigned)> prind,
|
|
|
|
std::function<bool()> stopfn)
|
2019-07-01 16:22:07 +00:00
|
|
|
{
|
2019-06-28 16:27:15 +00:00
|
|
|
AutoArranger<BinT> arranger{bin, minobjd, prind, stopfn};
|
2019-06-26 09:16:20 +00:00
|
|
|
|
2019-07-02 13:24:40 +00:00
|
|
|
auto it = excludes.begin();
|
|
|
|
while (it != excludes.end())
|
|
|
|
sl::isInside(it->transformedShape(), bin) ?
|
|
|
|
++it : it = excludes.erase(it);
|
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
// If there is something on the plate
|
2019-07-02 13:24:40 +00:00
|
|
|
if(!excludes.empty())
|
|
|
|
{
|
|
|
|
arranger.preload(excludes);
|
|
|
|
auto binbb = sl::boundingBox(bin);
|
|
|
|
|
|
|
|
// Try to put the first item to the center, as the arranger
|
|
|
|
// will not do this for us.
|
|
|
|
for (auto it = shapes.begin(); it != shapes.end(); ++it) {
|
|
|
|
Item &itm = *it;
|
|
|
|
auto ibb = itm.boundingBox();
|
|
|
|
auto d = binbb.center() - ibb.center();
|
|
|
|
itm.translate(d);
|
|
|
|
|
|
|
|
if (!arranger.is_colliding(itm)) {
|
|
|
|
itm.markAsFixed();
|
|
|
|
|
|
|
|
// Write the transformation data into the item. The
|
|
|
|
// callback was set on the instantiation of Item and
|
|
|
|
// calls the Arrangeable interface.
|
|
|
|
it->callApplyFunction(0);
|
|
|
|
|
|
|
|
// Remove this item, as it is arranged now
|
|
|
|
it = shapes.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
2019-07-01 16:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 13:24:40 +00:00
|
|
|
|
|
|
|
std::vector<std::reference_wrapper<Item>> inp;
|
|
|
|
inp.reserve(shapes.size() + excludes.size());
|
|
|
|
for (auto &itm : shapes ) inp.emplace_back(itm);
|
|
|
|
for (auto &itm : excludes) inp.emplace_back(itm);
|
2019-06-26 09:16:20 +00:00
|
|
|
|
2019-07-02 13:24:40 +00:00
|
|
|
return arranger(inp.begin(), inp.end());
|
2019-06-28 16:27:15 +00:00
|
|
|
}
|
2019-06-26 09:16:20 +00:00
|
|
|
|
2019-06-27 19:13:44 +00:00
|
|
|
inline SLIC3R_CONSTEXPR coord_t stride_padding(coord_t w)
|
2019-06-26 09:16:20 +00:00
|
|
|
{
|
|
|
|
return w + w / 5;
|
|
|
|
}
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
// The final client function for arrangement. A progress indicator and
|
|
|
|
// a stop predicate can be also be passed to control the process.
|
2019-07-02 13:24:40 +00:00
|
|
|
bool arrange(ArrangeablePtrs & arrangables,
|
|
|
|
const ArrangeablePtrs & excludes,
|
2019-07-03 17:24:41 +00:00
|
|
|
coord_t min_obj_dist,
|
2019-06-28 16:27:15 +00:00
|
|
|
const BedShapeHint & bedhint,
|
2019-06-27 19:13:44 +00:00
|
|
|
std::function<void(unsigned)> progressind,
|
|
|
|
std::function<bool()> stopcondition)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
2019-06-28 15:03:50 +00:00
|
|
|
namespace clppr = ClipperLib;
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
std::vector<Item> items, fixeditems;
|
2019-06-28 15:03:50 +00:00
|
|
|
items.reserve(arrangables.size());
|
|
|
|
coord_t binwidth = 0;
|
2019-06-28 16:27:15 +00:00
|
|
|
|
|
|
|
auto process_arrangeable =
|
2019-07-01 16:22:07 +00:00
|
|
|
[](const Arrangeable * arrangeable,
|
|
|
|
std::vector<Item> & outp,
|
|
|
|
std::function<void(const Item &, unsigned)> applyfn)
|
2019-06-28 16:27:15 +00:00
|
|
|
{
|
2019-07-01 16:22:07 +00:00
|
|
|
assert(arrangeable);
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
auto arrangeitem = arrangeable->get_arrange_polygon();
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
Polygon & p = std::get<0>(arrangeitem);
|
|
|
|
const Vec2crd &offs = std::get<1>(arrangeitem);
|
|
|
|
double rotation = std::get<2>(arrangeitem);
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
if (p.is_counter_clockwise()) p.reverse();
|
2019-06-28 15:03:50 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
clppr::Polygon clpath(Slic3rMultiPoint_to_ClipperPath(p));
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
auto firstp = clpath.Contour.front();
|
|
|
|
clpath.Contour.emplace_back(firstp);
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
outp.emplace_back(applyfn, std::move(clpath));
|
2019-07-02 08:32:01 +00:00
|
|
|
outp.back().rotation(rotation);
|
|
|
|
outp.back().translation({offs.x(), offs.y()});
|
2019-07-01 16:22:07 +00:00
|
|
|
};
|
2019-06-28 16:27:15 +00:00
|
|
|
|
|
|
|
for (Arrangeable *arrangeable : arrangables) {
|
|
|
|
process_arrangeable(
|
|
|
|
arrangeable,
|
|
|
|
items,
|
2019-06-28 15:03:50 +00:00
|
|
|
// callback called by arrange to apply the result on the arrangeable
|
2019-07-03 17:24:41 +00:00
|
|
|
[arrangeable, &binwidth, &ret](const Item &itm, unsigned binidx) {
|
|
|
|
ret = !binidx; // Return value false more bed is required
|
2019-06-28 15:03:50 +00:00
|
|
|
clppr::cInt stride = binidx * stride_padding(binwidth);
|
|
|
|
|
|
|
|
clppr::IntPoint offs = itm.translation();
|
2019-07-01 16:22:07 +00:00
|
|
|
arrangeable->apply_arrange_result({unscaled(offs.X + stride),
|
2019-06-28 15:03:50 +00:00
|
|
|
unscaled(offs.Y)},
|
2019-07-03 17:24:41 +00:00
|
|
|
itm.rotation(), binidx);
|
2019-06-28 16:27:15 +00:00
|
|
|
});
|
2019-06-27 19:13:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 16:27:15 +00:00
|
|
|
for (const Arrangeable * fixed: excludes)
|
2019-07-02 10:15:53 +00:00
|
|
|
process_arrangeable(fixed, fixeditems, nullptr);
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-06-27 19:13:44 +00:00
|
|
|
// Integer ceiling the min distance from the bed perimeters
|
2019-07-03 17:24:41 +00:00
|
|
|
coord_t md = min_obj_dist - SCALED_EPSILON;
|
2019-06-27 19:13:44 +00:00
|
|
|
md = (md % 2) ? md / 2 + 1 : md / 2;
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-07-03 17:24:41 +00:00
|
|
|
auto &cfn = stopcondition;
|
|
|
|
auto &pri = progressind;
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-06-27 19:13:44 +00:00
|
|
|
switch (bedhint.type) {
|
2019-07-03 17:24:41 +00:00
|
|
|
case BedShapeType::BOX: {
|
|
|
|
// Create the arranger for the box shaped bed
|
|
|
|
BoundingBox bbb = bedhint.shape.box;
|
|
|
|
bbb.min -= Point{md, md}, bbb.max += Point{md, md};
|
|
|
|
Box binbb{{bbb.min(X), bbb.min(Y)}, {bbb.max(X), bbb.max(Y)}};
|
|
|
|
binwidth = coord_t(binbb.width());
|
2019-06-28 15:03:50 +00:00
|
|
|
|
2019-07-03 17:24:41 +00:00
|
|
|
_arrange(items, fixeditems, binbb, min_obj_dist, pri, cfn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BedShapeType::CIRCLE: {
|
|
|
|
auto c = bedhint.shape.circ;
|
|
|
|
auto cc = to_lnCircle(c);
|
|
|
|
binwidth = scaled(c.radius());
|
2019-07-01 16:22:07 +00:00
|
|
|
|
2019-07-03 17:24:41 +00:00
|
|
|
_arrange(items, fixeditems, cc, min_obj_dist, pri, cfn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BedShapeType::IRREGULAR: {
|
|
|
|
auto ctour = Slic3rMultiPoint_to_ClipperPath(bedhint.shape.polygon);
|
|
|
|
auto irrbed = sl::create<clppr::Polygon>(std::move(ctour));
|
|
|
|
BoundingBox polybb(bedhint.shape.polygon);
|
|
|
|
binwidth = (polybb.max(X) - polybb.min(X));
|
2019-07-01 16:22:07 +00:00
|
|
|
|
2019-07-03 17:24:41 +00:00
|
|
|
_arrange(items, fixeditems, irrbed, min_obj_dist, pri, cfn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BedShapeType::INFINITE: {
|
|
|
|
const InfiniteBed& nobin = bedhint.shape.infinite;
|
|
|
|
auto infbb = Box::infinite({nobin.center.x(), nobin.center.y()});
|
2019-07-01 16:22:07 +00:00
|
|
|
|
2019-07-03 17:24:41 +00:00
|
|
|
_arrange(items, fixeditems, infbb, min_obj_dist, pri, cfn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BedShapeType::UNKNOWN: {
|
|
|
|
// We know nothing about the bed, let it be infinite and zero centered
|
|
|
|
_arrange(items, fixeditems, Box::infinite(), min_obj_dist, pri, cfn);
|
2019-07-01 16:22:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-06-27 19:13:44 +00:00
|
|
|
};
|
|
|
|
|
2019-07-03 17:24:41 +00:00
|
|
|
if(stopcondition && stopcondition()) return false;
|
2019-06-27 19:13:44 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
// Arrange, without the fixed items (excludes)
|
2019-07-03 17:24:41 +00:00
|
|
|
bool arrange(ArrangeablePtrs & inp,
|
2019-06-28 16:27:15 +00:00
|
|
|
coord_t min_d,
|
|
|
|
const BedShapeHint & bedhint,
|
|
|
|
std::function<void(unsigned)> prfn,
|
|
|
|
std::function<bool()> stopfn)
|
|
|
|
{
|
|
|
|
return arrange(inp, {}, min_d, bedhint, prfn, stopfn);
|
2018-11-12 13:52:52 +00:00
|
|
|
}
|
2019-01-22 16:50:33 +00:00
|
|
|
|
2019-06-28 16:27:15 +00:00
|
|
|
} // namespace arr
|
|
|
|
} // namespace Slic3r
|