2019-07-01 16:22:07 +00:00
|
|
|
#include "Arrange.hpp"
|
2018-11-12 13:52:52 +00:00
|
|
|
#include "SVG.hpp"
|
|
|
|
|
2020-05-23 22:50:11 +00:00
|
|
|
#include "BoundingBox.hpp"
|
|
|
|
|
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>
|
2020-11-20 08:28:09 +00:00
|
|
|
#include <libnest2d/utils/rotcalipers.hpp>
|
2018-11-12 13:52:52 +00:00
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
#include <ClipperUtils.hpp>
|
|
|
|
|
|
|
|
#include <boost/geometry/index/rtree.hpp>
|
2019-08-01 14:03:52 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER) && defined(__clang__)
|
|
|
|
#define BOOST_NO_CXX17_HDR_STRING_VIEW
|
|
|
|
#endif
|
|
|
|
|
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-17 14:47:09 +00:00
|
|
|
template<class Tout = double, class = FloatingOnly<Tout>, int...EigenArgs>
|
2020-05-21 15:45:00 +00:00
|
|
|
inline constexpr Eigen::Matrix<Tout, 2, EigenArgs...> unscaled(
|
|
|
|
const ClipperLib::IntPoint &v) noexcept
|
2019-07-01 16:22:07 +00:00
|
|
|
{
|
2019-07-17 14:47:09 +00:00
|
|
|
return Eigen::Matrix<Tout, 2, EigenArgs...>{unscaled<Tout>(v.X),
|
|
|
|
unscaled<Tout>(v.Y)};
|
2019-07-01 16:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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>
|
2020-11-17 23:06:34 +00:00
|
|
|
void fill_config(PConf& pcfg, const ArrangeParams ¶ms) {
|
2018-11-12 13:52:52 +00:00
|
|
|
|
|
|
|
// 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.
|
2020-11-17 23:06:34 +00:00
|
|
|
if (params.allow_rotations)
|
|
|
|
pcfg.rotations = {0., PI / 2., PI, 3. * PI / 2. };
|
|
|
|
else
|
|
|
|
pcfg.rotations = {0.};
|
2018-11-12 13:52:52 +00:00
|
|
|
|
|
|
|
// The accuracy of optimization.
|
|
|
|
// Goes from 0.0 to 1.0 and scales performance as well
|
2020-11-17 23:06:34 +00:00
|
|
|
pcfg.accuracy = params.accuracy;
|
2019-07-03 13:06:10 +00:00
|
|
|
|
|
|
|
// Allow parallel execution.
|
2020-11-17 23:06:34 +00:00
|
|
|
pcfg.parallel = params.parallel;
|
2018-11-12 13:52:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:31:11 +00:00
|
|
|
// Apply penalty to object function result. This is used only when alignment
|
2019-07-02 13:24:40 +00:00
|
|
|
// after arrange is explicitly disabled (PConfig::Alignment::DONT_ALIGN)
|
2020-12-02 16:53:22 +00:00
|
|
|
// Also, this will only work well for Box shaped beds.
|
2020-04-23 16:19:03 +00:00
|
|
|
static double fixed_overfit(const std::tuple<double, Box>& result, const Box &binbb)
|
2019-07-02 13:24:40 +00:00
|
|
|
{
|
|
|
|
double score = std::get<0>(result);
|
|
|
|
Box pilebb = std::get<1>(result);
|
|
|
|
Box fullbb = sl::boundingBox(pilebb, binbb);
|
2019-07-17 12:34:28 +00:00
|
|
|
auto diff = double(fullbb.area()) - binbb.area();
|
2019-07-02 13:24:40 +00:00
|
|
|
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-18 15:31:11 +00:00
|
|
|
using Packer = _Nester<Placer, Selector>;
|
2019-07-02 08:32:01 +00:00
|
|
|
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-08-16 14:17:37 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4244)
|
|
|
|
#pragma warning(disable: 4267)
|
|
|
|
#endif
|
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-08-16 14:17:37 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
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.
|
2019-08-21 12:07:56 +00:00
|
|
|
ItemGroup m_remaining; // Remaining items
|
|
|
|
ItemGroup m_items; // allready packed items
|
|
|
|
size_t m_item_count = 0; // Number of all items to be packed
|
2019-07-17 12:34:28 +00:00
|
|
|
|
2019-07-17 14:47:09 +00:00
|
|
|
template<class T> ArithmeticOnly<T, double> norm(T val)
|
2019-07-17 12:34:28 +00:00
|
|
|
{
|
|
|
|
return double(val) / m_norm;
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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();
|
2019-08-21 12:07:56 +00:00
|
|
|
if(bigitems && !m_remaining.empty()) compute_case = BIG_ITEM;
|
|
|
|
else if (bigitems && m_remaining.empty()) compute_case = LAST_BIG_ITEM;
|
2019-07-03 13:06:10 +00:00
|
|
|
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));
|
2019-08-21 12:07:56 +00:00
|
|
|
dist = 0.8 * dist + 0.2 * bindist;
|
2019-07-03 13:06:10 +00:00
|
|
|
|
|
|
|
// 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-08-21 12:07:56 +00:00
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
density = std::sqrt(norm(fullbb.width()) * norm(fullbb.height()));
|
2019-08-21 12:07:56 +00:00
|
|
|
double R = double(m_remaining.size()) / m_item_count;
|
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
// 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())
|
2019-08-21 12:07:56 +00:00
|
|
|
score = 0.50 * dist + 0.50 * density;
|
2019-07-03 13:06:10 +00:00
|
|
|
else
|
2020-11-20 08:35:04 +00:00
|
|
|
// Let the density matter more when fewer objects remain
|
|
|
|
score = 0.50 * dist + (1.0 - R) * 0.20 * density +
|
|
|
|
0.30 * alignment_score;
|
|
|
|
|
2019-07-03 13:06:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LAST_BIG_ITEM: {
|
2019-08-21 12:07:56 +00:00
|
|
|
score = norm(pl::distance(ibb.center(), m_pilebb.center()));
|
2019-07-03 13:06:10 +00:00
|
|
|
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,
|
2020-11-17 23:06:34 +00:00
|
|
|
const ArrangeParams ¶ms,
|
2020-11-26 12:58:21 +00:00
|
|
|
std::function<void(unsigned)> progressind,
|
2019-07-02 10:15:53 +00:00
|
|
|
std::function<bool(void)> stopcond)
|
2020-11-17 23:06:34 +00:00
|
|
|
: m_pck(bin, params.min_obj_distance)
|
2019-07-02 10:15:53 +00:00
|
|
|
, 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
|
|
|
{
|
2020-11-17 23:06:34 +00:00
|
|
|
fill_config(m_pconf, params);
|
2018-11-12 13:52:52 +00:00
|
|
|
|
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();
|
2020-11-26 12:58:21 +00:00
|
|
|
|
2020-12-02 16:53:22 +00:00
|
|
|
m_pconf.on_preload = [this](const ItemGroup &items, PConfig &cfg) {
|
|
|
|
if (items.empty()) return;
|
|
|
|
|
|
|
|
cfg.alignment = PConfig::Alignment::DONT_ALIGN;
|
|
|
|
auto bb = sl::boundingBox(m_bin);
|
|
|
|
auto bbcenter = bb.center();
|
|
|
|
cfg.object_function = [this, bb, bbcenter](const Item &item) {
|
|
|
|
return fixed_overfit(objfunc(item, bbcenter), bb);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-11-26 12:58:21 +00:00
|
|
|
auto on_packed = params.on_packed;
|
2019-07-02 10:15:53 +00:00
|
|
|
|
2020-11-26 12:58:21 +00:00
|
|
|
if (progressind || on_packed)
|
|
|
|
m_pck.progressIndicator([this, progressind, on_packed](unsigned rem) {
|
|
|
|
|
|
|
|
if (progressind)
|
|
|
|
progressind(rem);
|
|
|
|
|
|
|
|
if (on_packed) {
|
|
|
|
int last_bed = m_pck.lastPackedBinId();
|
|
|
|
if (last_bed >= 0) {
|
|
|
|
Item &last_packed = m_pck.lastResult()[last_bed].back();
|
|
|
|
ArrangePolygon ap;
|
|
|
|
ap.bed_idx = last_packed.binId();
|
|
|
|
ap.priority = last_packed.priority();
|
|
|
|
on_packed(ap);
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 13:41:36 +00:00
|
|
|
});
|
2020-11-26 12:58:21 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
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-09-10 11:31:29 +00:00
|
|
|
|
2019-08-21 12:07:56 +00:00
|
|
|
template<class It> inline void operator()(It from, It to) {
|
|
|
|
m_rtree.clear();
|
|
|
|
m_item_count += size_t(to - from);
|
|
|
|
m_pck.execute(from, to);
|
|
|
|
m_item_count = 0;
|
2018-11-12 13:52:52 +00:00
|
|
|
}
|
2019-06-27 19:13:44 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
PConfig& config() { return m_pconf; }
|
|
|
|
const PConfig& config() const { return m_pconf; }
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
inline void preload(std::vector<Item>& fixeditems) {
|
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];
|
2019-11-01 14:13:05 +00:00
|
|
|
itm.markAsFixedInBin(itm.binId());
|
2019-01-22 16:50:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 12:07:56 +00:00
|
|
|
m_item_count += fixeditems.size();
|
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
|
|
|
|
2020-05-06 14:44:52 +00:00
|
|
|
auto bin = m_bin;
|
|
|
|
sl::offset(bin, -EPSILON * (m_bin.width() + m_bin.height()));
|
|
|
|
|
|
|
|
double miss = Placer::overfit(fullbb, bin);
|
2019-07-02 10:15:53 +00:00
|
|
|
miss = miss > 0? miss : 0;
|
|
|
|
score += miss*miss;
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2020-05-06 14:44:52 +00:00
|
|
|
return score;
|
2019-07-02 10:15:53 +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<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-09-10 11:31:29 +00:00
|
|
|
template<class Bin> void remove_large_items(std::vector<Item> &items, Bin &&bin)
|
|
|
|
{
|
|
|
|
auto it = items.begin();
|
|
|
|
while (it != items.end())
|
|
|
|
sl::isInside(it->transformedShape(), bin) ?
|
|
|
|
++it : it = items.erase(it);
|
|
|
|
}
|
|
|
|
|
2020-11-20 08:28:09 +00:00
|
|
|
template<class S> Radians min_area_boundingbox_rotation(const S &sh)
|
|
|
|
{
|
|
|
|
return minAreaBoundingBox<S, TCompute<S>, boost::rational<LargeInt>>(sh)
|
|
|
|
.angleToX();
|
|
|
|
}
|
|
|
|
|
2019-07-02 10:15:53 +00:00
|
|
|
template<class BinT> // Arrange for arbitrary bin type
|
2019-07-12 19:03:49 +00:00
|
|
|
void _arrange(
|
2019-08-21 13:39:20 +00:00
|
|
|
std::vector<Item> & shapes,
|
|
|
|
std::vector<Item> & excludes,
|
|
|
|
const BinT & bin,
|
2020-11-17 23:06:34 +00:00
|
|
|
const ArrangeParams ¶ms,
|
2020-11-26 12:58:21 +00:00
|
|
|
std::function<void(unsigned)> progressfn,
|
2019-08-21 13:39:20 +00:00
|
|
|
std::function<bool()> stopfn)
|
2019-07-19 10:34:27 +00:00
|
|
|
{
|
|
|
|
// Integer ceiling the min distance from the bed perimeters
|
2020-04-23 16:19:03 +00:00
|
|
|
coord_t md = params.min_obj_distance;
|
2019-07-19 10:34:27 +00:00
|
|
|
md = (md % 2) ? md / 2 + 1 : md / 2;
|
|
|
|
|
|
|
|
auto corrected_bin = bin;
|
|
|
|
sl::offset(corrected_bin, md);
|
2020-11-17 23:06:34 +00:00
|
|
|
ArrangeParams mod_params = params;
|
|
|
|
mod_params.min_obj_distance = 0;
|
|
|
|
|
|
|
|
AutoArranger<BinT> arranger{corrected_bin, mod_params, progressfn, stopfn};
|
2020-04-23 16:19:03 +00:00
|
|
|
|
|
|
|
auto infl = coord_t(std::ceil(params.min_obj_distance / 2.0));
|
2019-07-19 10:34:27 +00:00
|
|
|
for (Item& itm : shapes) itm.inflate(infl);
|
|
|
|
for (Item& itm : excludes) itm.inflate(infl);
|
2019-06-26 09:16:20 +00:00
|
|
|
|
2019-09-10 11:31:29 +00:00
|
|
|
remove_large_items(excludes, corrected_bin);
|
2019-07-02 13:24:40 +00:00
|
|
|
|
2019-07-01 16:22:07 +00:00
|
|
|
// If there is something on the plate
|
2019-07-22 09:18:10 +00:00
|
|
|
if (!excludes.empty()) arranger.preload(excludes);
|
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
|
|
|
|
2020-11-20 08:28:09 +00:00
|
|
|
// Use the minimum bounding box rotation as a starting point.
|
|
|
|
// TODO: This only works for convex hull. If we ever switch to concave
|
|
|
|
// polygon nesting, a convex hull needs to be calculated.
|
|
|
|
if (params.allow_rotations)
|
|
|
|
for (auto &itm : shapes)
|
|
|
|
itm.rotation(min_area_boundingbox_rotation(itm.rawShape()));
|
|
|
|
|
2019-07-12 19:03:49 +00:00
|
|
|
arranger(inp.begin(), inp.end());
|
2019-07-19 10:34:27 +00:00
|
|
|
for (Item &itm : inp) itm.inflate(-infl);
|
2019-06-26 09:16:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
inline Box to_nestbin(const BoundingBox &bb) { return Box{{bb.min(X), bb.min(Y)}, {bb.max(X), bb.max(Y)}};}
|
|
|
|
inline Circle to_nestbin(const CircleBed &c) { return Circle({c.center()(0), c.center()(1)}, c.radius()); }
|
|
|
|
inline clppr::Polygon to_nestbin(const Polygon &p) { return sl::create<clppr::Polygon>(Slic3rMultiPoint_to_ClipperPath(p)); }
|
|
|
|
inline Box to_nestbin(const InfiniteBed &bed) { return Box::infinite({bed.center.x(), bed.center.y()}); }
|
|
|
|
|
|
|
|
inline coord_t width(const BoundingBox& box) { return box.max.x() - box.min.x(); }
|
|
|
|
inline coord_t height(const BoundingBox& box) { return box.max.y() - box.min.y(); }
|
|
|
|
inline double area(const BoundingBox& box) { return double(width(box)) * height(box); }
|
|
|
|
inline double poly_area(const Points &pts) { return std::abs(Polygon::area(pts)); }
|
|
|
|
inline double distance_to(const Point& p1, const Point& p2)
|
2019-06-27 19:13:44 +00:00
|
|
|
{
|
2020-04-23 16:19:03 +00:00
|
|
|
double dx = p2.x() - p1.x();
|
|
|
|
double dy = p2.y() - p1.y();
|
|
|
|
return std::sqrt(dx*dx + dy*dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static CircleBed to_circle(const Point ¢er, const Points& points) {
|
|
|
|
std::vector<double> vertex_distances;
|
|
|
|
double avg_dist = 0;
|
2019-06-28 15:03:50 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
for (auto pt : points)
|
|
|
|
{
|
|
|
|
double distance = distance_to(center, pt);
|
|
|
|
vertex_distances.push_back(distance);
|
|
|
|
avg_dist += distance;
|
|
|
|
}
|
2019-07-12 19:03:49 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
avg_dist /= vertex_distances.size();
|
|
|
|
|
|
|
|
CircleBed ret(center, avg_dist);
|
|
|
|
for(auto el : vertex_distances)
|
2019-06-28 16:27:15 +00:00
|
|
|
{
|
2020-04-23 16:19:03 +00:00
|
|
|
if (std::abs(el - avg_dist) > 10 * SCALED_EPSILON) {
|
|
|
|
ret = {};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
// Create Item from Arrangeable
|
|
|
|
static void process_arrangeable(const ArrangePolygon &arrpoly,
|
|
|
|
std::vector<Item> & outp)
|
|
|
|
{
|
|
|
|
Polygon p = arrpoly.poly.contour;
|
|
|
|
const Vec2crd &offs = arrpoly.translation;
|
|
|
|
double rotation = arrpoly.rotation;
|
2019-06-28 15:03:50 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
if (p.is_counter_clockwise()) p.reverse();
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
clppr::Polygon clpath(Slic3rMultiPoint_to_ClipperPath(p));
|
|
|
|
|
|
|
|
if (!clpath.Contour.empty()) {
|
|
|
|
auto firstp = clpath.Contour.front();
|
|
|
|
clpath.Contour.emplace_back(firstp);
|
|
|
|
}
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
outp.emplace_back(std::move(clpath));
|
|
|
|
outp.back().rotation(rotation);
|
|
|
|
outp.back().translation({offs.x(), offs.y()});
|
|
|
|
outp.back().binId(arrpoly.bed_idx);
|
|
|
|
outp.back().priority(arrpoly.priority);
|
|
|
|
}
|
|
|
|
|
2020-11-20 08:32:18 +00:00
|
|
|
template<class Fn> auto call_with_bed(const Points &bed, Fn &&fn)
|
2020-04-23 16:19:03 +00:00
|
|
|
{
|
|
|
|
if (bed.empty())
|
2020-11-20 08:32:18 +00:00
|
|
|
return fn(InfiniteBed{});
|
2020-04-23 16:19:03 +00:00
|
|
|
else if (bed.size() == 1)
|
2020-11-20 08:32:18 +00:00
|
|
|
return fn(InfiniteBed{bed.front()});
|
2020-04-23 16:19:03 +00:00
|
|
|
else {
|
|
|
|
auto bb = BoundingBox(bed);
|
|
|
|
CircleBed circ = to_circle(bb.center(), bed);
|
|
|
|
auto parea = poly_area(bed);
|
2020-11-20 08:32:18 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
if ((1.0 - parea / area(bb)) < 1e-3)
|
2020-11-20 08:32:18 +00:00
|
|
|
return fn(bb);
|
2020-04-23 16:19:03 +00:00
|
|
|
else if (!std::isnan(circ.radius()))
|
2020-11-20 08:32:18 +00:00
|
|
|
return fn(circ);
|
2020-04-23 16:19:03 +00:00
|
|
|
else
|
2020-11-20 08:32:18 +00:00
|
|
|
return fn(Polygon(bed));
|
2020-04-23 16:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 08:32:18 +00:00
|
|
|
template<>
|
|
|
|
void arrange(ArrangePolygons & items,
|
|
|
|
const ArrangePolygons &excludes,
|
|
|
|
const Points & bed,
|
|
|
|
const ArrangeParams & params)
|
|
|
|
{
|
|
|
|
call_with_bed(bed, [&](const auto &bin) {
|
|
|
|
arrange(items, excludes, bin, params);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
template<class BedT>
|
|
|
|
void arrange(ArrangePolygons & arrangables,
|
|
|
|
const ArrangePolygons &excludes,
|
|
|
|
const BedT & bed,
|
|
|
|
const ArrangeParams & params)
|
|
|
|
{
|
|
|
|
namespace clppr = ClipperLib;
|
|
|
|
|
|
|
|
std::vector<Item> items, fixeditems;
|
|
|
|
items.reserve(arrangables.size());
|
|
|
|
|
2019-07-12 19:03:49 +00:00
|
|
|
for (ArrangePolygon &arrangeable : arrangables)
|
2019-07-04 11:58:18 +00:00
|
|
|
process_arrangeable(arrangeable, items);
|
2019-06-27 19:13:44 +00:00
|
|
|
|
2019-07-12 19:03:49 +00:00
|
|
|
for (const ArrangePolygon &fixed: excludes)
|
2019-07-04 11:58:18 +00:00
|
|
|
process_arrangeable(fixed, fixeditems);
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2019-07-17 12:34:28 +00:00
|
|
|
for (Item &itm : fixeditems) itm.inflate(scaled(-2. * EPSILON));
|
2019-07-15 15:30:44 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
auto &cfn = params.stopcondition;
|
|
|
|
auto &pri = params.progressind;
|
2019-06-28 16:27:15 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
_arrange(items, fixeditems, to_nestbin(bed), params, pri, cfn);
|
2019-06-27 19:13:44 +00:00
|
|
|
|
2019-07-12 19:03:49 +00:00
|
|
|
for(size_t i = 0; i < items.size(); ++i) {
|
|
|
|
clppr::IntPoint tr = items[i].translation();
|
|
|
|
arrangables[i].translation = {coord_t(tr.X), coord_t(tr.Y)};
|
|
|
|
arrangables[i].rotation = items[i].rotation();
|
|
|
|
arrangables[i].bed_idx = items[i].binId();
|
|
|
|
}
|
2019-06-27 19:13:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const BoundingBox &bed, const ArrangeParams ¶ms);
|
|
|
|
template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const CircleBed &bed, const ArrangeParams ¶ms);
|
|
|
|
template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const Polygon &bed, const ArrangeParams ¶ms);
|
|
|
|
template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const InfiniteBed &bed, const ArrangeParams ¶ms);
|
2019-01-22 16:50:33 +00:00
|
|
|
|
2019-06-28 16:27:15 +00:00
|
|
|
} // namespace arr
|
|
|
|
} // namespace Slic3r
|