mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-06-20 19:04:06 +00:00
Cover the libnest2d nesting engine and fix an NfpPlacer crash (#14267)
* fix(libnest2d): skip the excluded-region alignment pass when there are none NfpPlacer::finalAlign(), run from clearItems() and the destructor, always ran the "find a best position inside the NFP of fixed items" pass even when no items are fixed. With nothing to avoid, calcnfp() computes the inner-fit NFP of the pile and can feed clipper a coordinate outside its allowed range. On Linux/clang the value stays in range so it went unnoticed; on MSVC the clipper "Coordinate outside allowed range" exception escapes the noexcept destructor and aborts the process (exit 0xC0000409). Build the excluded set up front and only run the pass when it is non-empty. The block exists solely to keep the pile clear of fixed items (excluded regions / wipe tower), so it is a no-op when there are none and the wipe-tower behaviour is unchanged. * test(libnest2d): remove dead nesting tests and split the suite by feature Seven of the suite's hidden [.] test cases drove code paths Orca abandoned at the BambuStudio fork: BottomLeftPlacer (used nowhere in src/) and the stock default NfpPlacer backend, which returns zero bins in Orca. They have been red since the fork and are never registered with ctest. Remove them. Split the 1,000-line libnest2d_tests_main.cpp into per-feature files, per the repo convention, sharing a header for the no-fit-polygon backend setup that every translation unit must agree on (ODR): libnest2d_tests.cpp Item and nest() basics test_geometry.cpp geometry primitives test_nfp.cpp no-fit-polygon machinery libnest2d_test_utils.hpp shared includes and the NFP backend specialisation Along the way: drop a debug exportSVG() helper that only wrote a file on test failure (so the suite never leaves stray assets), convert the deprecated Catch::Approx to WithinRel/WithinAbs matchers, and give the tests descriptive names. * test(libnest2d): add NfpPlacer unit tests NfpPlacer is the placement engine the arranger drives, but the suite only covered the geometry primitives. Add a fixture and five tests that exercise pack()/accept() directly: a single item lands in the bin, an oversized item is rejected, the first item is seeded for every starting point, many items pack without overlap, and the rotation candidates are searched. This lifts nfpplacer.hpp line coverage from 42% to 87% in the libnest2d suite. * test(libslic3r): add arrangement::arrange() integration coverage The libnest2d suite cannot reach Orca's real nesting entry point because it does not link libslic3r. Add test_arrange.cpp driving arrangement::arrange(): items land on the bed and within bounds, do not overlap, are spaced by their inflation, an oversized item stays unplaced, overflow spills onto virtual beds, an empty input is a no-op, and the DONT_ALIGN and USER_DEFINED final-alignment paths are exercised. A self-test guards the overlap check the other cases use.
This commit is contained in:
@@ -1123,18 +1123,17 @@ private:
|
||||
|
||||
std::vector<RawShape> objs,excludes;
|
||||
for (const Item &item : items_) {
|
||||
if (item.isFixed()) continue;
|
||||
objs.push_back(item.transformedShape());
|
||||
if (item.isFixed())
|
||||
excludes.push_back(item.transformedShape());
|
||||
else
|
||||
objs.push_back(item.transformedShape());
|
||||
}
|
||||
if (objs.empty())
|
||||
return;
|
||||
// Without fixed items this inner-fit NFP can exceed clipper's range and crash MSVC.
|
||||
if (!excludes.empty())
|
||||
{ // find a best position inside NFP of fixed items (excluded regions), so the center of pile is cloest to bed center
|
||||
RawShape objs_convex_hull = sl::convexHull(objs);
|
||||
for (const Item &item : items_) {
|
||||
if (item.isFixed()) {
|
||||
excludes.push_back(item.transformedShape());
|
||||
}
|
||||
}
|
||||
|
||||
auto nfps = calcnfp(objs_convex_hull, excludes, bbin, Lvl<MaxNfpLevel::value>());
|
||||
if (nfps.empty()) {
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
get_filename_component(_TEST_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
|
||||
add_executable(${_TEST_NAME}_tests
|
||||
${_TEST_NAME}_tests_main.cpp
|
||||
${_TEST_NAME}_tests.cpp
|
||||
test_geometry.cpp
|
||||
test_nfp.cpp
|
||||
test_nfp_placer.cpp
|
||||
printer_parts.cpp
|
||||
printer_parts.hpp
|
||||
libnest2d_test_utils.hpp
|
||||
)
|
||||
|
||||
target_link_libraries(${_TEST_NAME}_tests test_common libnest2d Catch2::Catch2WithMain)
|
||||
|
||||
46
tests/libnest2d/libnest2d_test_utils.hpp
Normal file
46
tests/libnest2d/libnest2d_test_utils.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
// Shared setup for the libnest2d test suite.
|
||||
//
|
||||
// The no-fit-polygon numeric backend specialised below changes how NFP is
|
||||
// computed for the whole program, so every translation unit that instantiates
|
||||
// NFP (the geometry, nfp and placer tests) must see the same definition. Keep
|
||||
// it here and include this header from every libnest2d test file.
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <libnest2d/libnest2d.hpp>
|
||||
#include <libnest2d/utils/rotcalipers.hpp>
|
||||
|
||||
#if defined(_MSC_VER) && defined(__clang__)
|
||||
#define BOOST_NO_CXX17_HDR_STRING_VIEW
|
||||
#endif
|
||||
|
||||
#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;
|
||||
template<> struct _NumTag<LargeInt> { using Type = ScalarTag; };
|
||||
#endif
|
||||
template<class T> struct _NumTag<boost::rational<T>> { using Type = RationalTag; };
|
||||
|
||||
using RectangleItem = libnest2d::Rectangle;
|
||||
|
||||
namespace nfp {
|
||||
|
||||
// Use exact rational arithmetic for the convex NFP so the tests are not at the
|
||||
// mercy of floating-point rounding.
|
||||
template<class S>
|
||||
struct NfpImpl<S, NfpLevel::CONVEX_ONLY> {
|
||||
NfpResult<S> operator()(const S &sh, const S &other) {
|
||||
return nfpConvexOnly<S, boost::rational<LargeInt>>(sh, other);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nfp
|
||||
} // namespace libnest2d
|
||||
49
tests/libnest2d/libnest2d_tests.cpp
Normal file
49
tests/libnest2d/libnest2d_tests.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include "libnest2d_test_utils.hpp"
|
||||
|
||||
using namespace libnest2d;
|
||||
|
||||
// Basic behaviour of the Item type and the high-level nest() entry point:
|
||||
// items copy independently, and nest() leaves degenerate or oversized items
|
||||
// untouched.
|
||||
|
||||
TEST_CASE("Item construction and copy", "[Nesting]") {
|
||||
Item sh = { {0, 0}, {1, 0}, {1, 1}, {0, 1} };
|
||||
REQUIRE(sh.vertexCount() == 4u);
|
||||
|
||||
Item sh2({ {0, 0}, {1, 0}, {1, 1}, {0, 1} });
|
||||
REQUIRE(sh2.vertexCount() == 4u);
|
||||
|
||||
Item sh3 = sh2; // copy
|
||||
REQUIRE(sh3.vertexCount() == 4u);
|
||||
|
||||
sh2 = {}; // clearing the original leaves the copy intact
|
||||
REQUIRE(sh2.vertexCount() == 0u);
|
||||
REQUIRE(sh3.vertexCount() == 4u);
|
||||
}
|
||||
|
||||
TEST_CASE("nest() leaves an empty or zero-area item untouched", "[Nesting]") {
|
||||
auto bin = Box(250000000, 210000000);
|
||||
|
||||
std::vector<Item> items;
|
||||
items.emplace_back(Item{}); // empty item
|
||||
items.emplace_back(Item{ {0, 200} }); // zero-area item
|
||||
|
||||
size_t bins = nest(items, bin);
|
||||
|
||||
REQUIRE(bins == 0u);
|
||||
for (const auto &itm : items) REQUIRE(itm.binId() == BIN_ID_UNSET);
|
||||
}
|
||||
|
||||
TEST_CASE("nest() leaves an item larger than the bin untouched", "[Nesting]") {
|
||||
auto bin = Box(250000000, 210000000);
|
||||
|
||||
std::vector<Item> items;
|
||||
items.emplace_back(RectangleItem{250000001, 210000001}); // larger than the bin
|
||||
|
||||
size_t bins = nest(items, bin);
|
||||
|
||||
REQUIRE(bins == 0u);
|
||||
REQUIRE(items.front().binId() == BIN_ID_UNSET);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
190
tests/libnest2d/test_geometry.cpp
Normal file
190
tests/libnest2d/test_geometry.cpp
Normal file
@@ -0,0 +1,190 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include "libnest2d_test_utils.hpp"
|
||||
#include "printer_parts.hpp"
|
||||
|
||||
using namespace libnest2d;
|
||||
|
||||
namespace {
|
||||
|
||||
using Catch::Matchers::WithinAbs;
|
||||
using Catch::Matchers::WithinRel;
|
||||
|
||||
// Geometry values round-trip through floating point, so compare with a small
|
||||
// tolerance that works both near and away from zero.
|
||||
void require_close(double value, double expected) {
|
||||
REQUIRE_THAT(value, WithinRel(expected, 1e-9) || WithinAbs(expected, 1e-9));
|
||||
}
|
||||
|
||||
// The printer parts as nestable items, computed once.
|
||||
const std::vector<Item> &prusa_parts() {
|
||||
static const std::vector<Item> parts = [] {
|
||||
std::vector<Item> ret;
|
||||
ret.reserve(PRINTER_PART_POLYGONS.size());
|
||||
for (auto &inp : PRINTER_PART_POLYGONS) {
|
||||
auto inp_cpy = inp;
|
||||
if (ClosureTypeV<PathImpl> == Closure::OPEN)
|
||||
inp_cpy.points.pop_back();
|
||||
if constexpr (!is_clockwise<PathImpl>())
|
||||
std::reverse(inp_cpy.begin(), inp_cpy.end());
|
||||
ret.emplace_back(inp_cpy);
|
||||
}
|
||||
return ret;
|
||||
}();
|
||||
return parts;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("Degree and radian conversion round-trips", "[Geometry]") {
|
||||
Degrees deg(180);
|
||||
Radians rad(deg);
|
||||
|
||||
require_close(rad, Pi);
|
||||
require_close(deg, 180);
|
||||
require_close(Degrees(rad), 180);
|
||||
require_close(rad, Radians(deg));
|
||||
require_close(Degrees(rad), deg);
|
||||
REQUIRE(rad == deg);
|
||||
}
|
||||
|
||||
TEST_CASE("Segment angle to the X axis", "[Geometry]") {
|
||||
auto quadrant = [](Point to) { return Degrees(Segment({0, 0}, to).angleToXaxis()); };
|
||||
|
||||
REQUIRE(quadrant({12, -10}) > 270); REQUIRE(quadrant({12, -10}) < 360);
|
||||
REQUIRE(quadrant({12, 10}) > 0); REQUIRE(quadrant({12, 10}) < 90);
|
||||
REQUIRE(quadrant({-12, 10}) > 90); REQUIRE(quadrant({-12, 10}) < 180);
|
||||
REQUIRE(quadrant({-12, -10}) > 180); REQUIRE(quadrant({-12, -10}) < 270);
|
||||
|
||||
require_close(quadrant({1, 0}), 0);
|
||||
require_close(quadrant({0, 1}), 90);
|
||||
require_close(quadrant({-1, 0}), 180);
|
||||
require_close(quadrant({0, -1}), 270);
|
||||
}
|
||||
|
||||
TEST_CASE("Point to segment distance", "[Geometry]") {
|
||||
Point p2 = {10, 0};
|
||||
Segment seg({0, 0}, {10, 10});
|
||||
|
||||
auto check = [](TCompute<Coord> val, TCompute<Coord> expected) {
|
||||
if (std::is_floating_point<TCompute<Coord>>::value)
|
||||
require_close(double(val), double(expected));
|
||||
else
|
||||
REQUIRE(val == expected);
|
||||
};
|
||||
|
||||
auto h = pointlike::horizontalDistance(p2, seg);
|
||||
REQUIRE(h.second);
|
||||
check(h.first, 10);
|
||||
|
||||
auto v = pointlike::verticalDistance(p2, seg);
|
||||
REQUIRE(v.second);
|
||||
check(v.first, -10);
|
||||
|
||||
v = pointlike::verticalDistance(Point{10, 20}, seg);
|
||||
REQUIRE(v.second);
|
||||
check(v.first, 10);
|
||||
|
||||
Point p4 = {80, 0};
|
||||
Segment seg2({0, 0}, {0, 40});
|
||||
|
||||
h = pointlike::horizontalDistance(p4, seg2);
|
||||
REQUIRE(h.second);
|
||||
check(h.first, 80);
|
||||
|
||||
v = pointlike::verticalDistance(p4, seg2);
|
||||
REQUIRE_FALSE(v.second); // the point does not project onto the segment
|
||||
}
|
||||
|
||||
TEST_CASE("Item area", "[Geometry]") {
|
||||
require_close(RectangleItem(10, 10).area(), 100);
|
||||
require_close(RectangleItem(100, 100).area(), 10000);
|
||||
|
||||
Item item = {
|
||||
{61, 97}, {70, 151}, {176, 151}, {189, 138},
|
||||
{189, 59}, {70, 59}, {61, 77}, {61, 97}
|
||||
};
|
||||
REQUIRE(std::abs(shapelike::area(item.transformedShape())) > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Point inside polygon", "[Geometry]") {
|
||||
RectangleItem rect(10, 10);
|
||||
|
||||
REQUIRE(rect.isInside(Point{1, 1}));
|
||||
REQUIRE(rect.isInside(Point{3, 3}));
|
||||
REQUIRE_FALSE(rect.isInside(Point{11, 11}));
|
||||
REQUIRE_FALSE(rect.isInside(Point{11, 12}));
|
||||
}
|
||||
|
||||
TEST_CASE("Bounding circle of the printer parts", "[Geometry]") {
|
||||
PolygonImpl p = {{{0, 10}, {10, 0}, {0, -10}, {0, 10}}, {}};
|
||||
Circle c = placers::boundingCircle(p);
|
||||
|
||||
require_close(getX(c.center()), 0);
|
||||
require_close(getY(c.center()), 0);
|
||||
require_close(c.radius(), 10);
|
||||
|
||||
shapelike::translate(p, PointImpl{10, 10});
|
||||
c = placers::boundingCircle(p);
|
||||
require_close(getX(c.center()), 10);
|
||||
require_close(getY(c.center()), 10);
|
||||
require_close(c.radius(), 10);
|
||||
|
||||
for (auto &part : prusa_parts()) {
|
||||
c = placers::boundingCircle(part.transformedShape());
|
||||
REQUIRE_FALSE(std::isnan(c.radius()));
|
||||
for (auto v : shapelike::contour(part.transformedShape())) {
|
||||
auto d = pointlike::distance(v, c.center());
|
||||
if (d > c.radius())
|
||||
REQUIRE(std::abs(1.0 - d / c.radius()) <= 1e-3); // on the circle
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convex hull of a printer part", "[Geometry]") {
|
||||
PathImpl poly = PRINTER_PART_POLYGONS[0];
|
||||
auto chull = sl::convexHull(poly);
|
||||
|
||||
REQUIRE(chull.size() == poly.size()); // the part is already convex
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
using Unit = int64_t;
|
||||
using Ratio = boost::rational<boost::multiprecision::int128_t>;
|
||||
|
||||
// Reference minimum-area bounding box, found by brute force over every edge
|
||||
// direction, to validate the rotating-calipers implementation.
|
||||
long double ref_min_area_box(const PolygonImpl &p) {
|
||||
long double min_area = std::numeric_limits<long double>::max();
|
||||
|
||||
auto update_min = [&](const Point &a, const Point &b) {
|
||||
PolygonImpl rotated = p;
|
||||
sl::rotate(rotated, -Segment(a, b).angleToXaxis());
|
||||
min_area = std::min(min_area, cast<long double>(sl::area(sl::boundingBox(rotated))));
|
||||
};
|
||||
|
||||
auto it = sl::cbegin(p), itx = std::next(it);
|
||||
while (itx != sl::cend(p)) { update_min(*it, *itx); ++it; ++itx; }
|
||||
update_min(*std::prev(sl::cend(p)), *sl::cbegin(p));
|
||||
|
||||
return min_area;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("Minimum-area bounding box via rotating calipers", "[Geometry]") {
|
||||
const long double tolerance = 500e6l;
|
||||
|
||||
for (const PathImpl &part : PRINTER_PART_POLYGONS) {
|
||||
auto area = cast<long double>(minAreaBoundingBox<PathImpl, Unit, Ratio>(part).area());
|
||||
REQUIRE(std::abs(ref_min_area_box(PolygonImpl(part)) - area) < tolerance);
|
||||
}
|
||||
|
||||
for (PathImpl part : STEGOSAUR_POLYGONS) {
|
||||
std::reverse(part.begin(), part.end());
|
||||
PolygonImpl poly(removeCollinearPoints<PathImpl, PointImpl, Unit>(part, 1000000));
|
||||
auto area = cast<long double>(minAreaBoundingBox<PolygonImpl, Unit, Ratio>(poly).area());
|
||||
REQUIRE(std::abs(ref_min_area_box(poly) - area) < tolerance);
|
||||
}
|
||||
}
|
||||
267
tests/libnest2d/test_nfp.cpp
Normal file
267
tests/libnest2d/test_nfp.cpp
Normal file
@@ -0,0 +1,267 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include "libnest2d_test_utils.hpp"
|
||||
|
||||
using namespace libnest2d;
|
||||
|
||||
namespace {
|
||||
|
||||
struct ItemPair {
|
||||
Item orbiter;
|
||||
Item stationary;
|
||||
};
|
||||
|
||||
std::vector<ItemPair> nfp_testdata = {
|
||||
{
|
||||
{
|
||||
{80, 50},
|
||||
{100, 70},
|
||||
{120, 50}
|
||||
},
|
||||
{
|
||||
{10, 10},
|
||||
{10, 40},
|
||||
{40, 40},
|
||||
{40, 10}
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
{80, 50},
|
||||
{60, 70},
|
||||
{80, 90},
|
||||
{120, 90},
|
||||
{140, 70},
|
||||
{120, 50}
|
||||
},
|
||||
{
|
||||
{10, 10},
|
||||
{10, 40},
|
||||
{40, 40},
|
||||
{40, 10}
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
{40, 10},
|
||||
{30, 10},
|
||||
{20, 20},
|
||||
{20, 30},
|
||||
{30, 40},
|
||||
{40, 40},
|
||||
{50, 30},
|
||||
{50, 20}
|
||||
},
|
||||
{
|
||||
{80, 0},
|
||||
{80, 30},
|
||||
{110, 30},
|
||||
{110, 0}
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
{117, 107},
|
||||
{118, 109},
|
||||
{120, 112},
|
||||
{122, 113},
|
||||
{128, 113},
|
||||
{130, 112},
|
||||
{132, 109},
|
||||
{133, 107},
|
||||
{133, 103},
|
||||
{132, 101},
|
||||
{130, 98},
|
||||
{128, 97},
|
||||
{122, 97},
|
||||
{120, 98},
|
||||
{118, 101},
|
||||
{117, 103}
|
||||
},
|
||||
{
|
||||
{102, 116},
|
||||
{111, 126},
|
||||
{114, 126},
|
||||
{144, 106},
|
||||
{148, 100},
|
||||
{148, 85},
|
||||
{147, 84},
|
||||
{102, 84}
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
{99, 122},
|
||||
{108, 140},
|
||||
{110, 142},
|
||||
{139, 142},
|
||||
{151, 122},
|
||||
{151, 102},
|
||||
{142, 70},
|
||||
{139, 68},
|
||||
{111, 68},
|
||||
{108, 70},
|
||||
{99, 102}
|
||||
},
|
||||
{
|
||||
{107, 124},
|
||||
{128, 125},
|
||||
{133, 125},
|
||||
{136, 124},
|
||||
{140, 121},
|
||||
{142, 119},
|
||||
{143, 116},
|
||||
{143, 109},
|
||||
{141, 93},
|
||||
{139, 89},
|
||||
{136, 86},
|
||||
{134, 85},
|
||||
{108, 85},
|
||||
{107, 86}
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
{91, 100},
|
||||
{94, 144},
|
||||
{117, 153},
|
||||
{118, 153},
|
||||
{159, 112},
|
||||
{159, 110},
|
||||
{156, 66},
|
||||
{133, 57},
|
||||
{132, 57},
|
||||
{91, 98}
|
||||
},
|
||||
{
|
||||
{101, 90},
|
||||
{103, 98},
|
||||
{107, 113},
|
||||
{114, 125},
|
||||
{115, 126},
|
||||
{135, 126},
|
||||
{136, 125},
|
||||
{144, 114},
|
||||
{149, 90},
|
||||
{149, 89},
|
||||
{148, 87},
|
||||
{145, 84},
|
||||
{105, 84},
|
||||
{102, 87},
|
||||
{101, 89}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// libnest2d's vertex order depends on the backend; normalise to clockwise.
|
||||
Item reversed_if_ccw(Item it) {
|
||||
if (!is_clockwise<PolygonImpl>()) {
|
||||
auto raw = it.rawShape();
|
||||
std::reverse(sl::begin(raw), sl::end(raw));
|
||||
it = Item{raw};
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
// Sliding `orbiter` around `stationary` along their no-fit polygon must keep the
|
||||
// two shapes touching at every NFP vertex, and `stationary` must lie inside the
|
||||
// resulting inner-fit polygon.
|
||||
template<nfp::NfpLevel lvl, Coord SCALE>
|
||||
void check_nfp(const std::vector<ItemPair> &testdata) {
|
||||
auto check_pair = [](Item orbiter, Item stationary) {
|
||||
orbiter.translate({210 * SCALE, 0});
|
||||
|
||||
auto &&nfp = nfp::noFitPolygon<lvl>(stationary.rawShape(), orbiter.transformedShape());
|
||||
placers::correctNfpPosition(nfp, stationary, orbiter);
|
||||
REQUIRE(shapelike::isValid(nfp.first).first);
|
||||
|
||||
Item infp(nfp.first);
|
||||
REQUIRE(stationary.isInside(infp));
|
||||
|
||||
auto vo = nfp::referenceVertex(orbiter.transformedShape());
|
||||
for (auto v : infp) {
|
||||
Item moved = orbiter;
|
||||
moved.translate({getX(v) - getX(vo), getY(v) - getY(vo)});
|
||||
REQUIRE(Item::touches(moved, stationary));
|
||||
}
|
||||
};
|
||||
|
||||
for (const ItemPair &td : testdata) {
|
||||
check_pair(reversed_if_ccw(td.orbiter), reversed_if_ccw(td.stationary));
|
||||
check_pair(reversed_if_ccw(td.stationary), reversed_if_ccw(td.orbiter));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("No-fit polygon of convex shapes keeps the items touching", "[Geometry][NFP]") {
|
||||
check_nfp<nfp::NfpLevel::CONVEX_ONLY, 1>(nfp_testdata);
|
||||
}
|
||||
|
||||
TEST_CASE("BottomLeftPlacer left and down polygons", "[Geometry][NFP]") {
|
||||
Box bin(100, 100);
|
||||
BottomLeftPlacer placer(bin);
|
||||
|
||||
PathImpl pitem = {{70, 75}, {88, 60}, {65, 50}, {60, 30}, {80, 20},
|
||||
{42, 20}, {35, 35}, {35, 55}, {40, 75}};
|
||||
PathImpl left_control = {{40, 75}, {35, 55}, {35, 35}, {42, 20}, {0, 20}, {0, 75}};
|
||||
PathImpl down_control = {{88, 60}, {88, 0}, {35, 0}, {35, 35},
|
||||
{42, 20}, {80, 20}, {60, 30}, {65, 50}};
|
||||
|
||||
if constexpr (!is_clockwise<PathImpl>()) {
|
||||
std::reverse(sl::begin(pitem), sl::end(pitem));
|
||||
std::reverse(sl::begin(left_control), sl::end(left_control));
|
||||
std::reverse(sl::begin(down_control), sl::end(down_control));
|
||||
}
|
||||
if constexpr (ClosureTypeV<PathImpl> == Closure::CLOSED) {
|
||||
sl::addVertex(pitem, sl::front(pitem));
|
||||
sl::addVertex(left_control, sl::front(left_control));
|
||||
sl::addVertex(down_control, sl::front(down_control));
|
||||
}
|
||||
|
||||
auto require_same_vertices = [](const Item &got, const Item &expected) {
|
||||
REQUIRE(shapelike::isValid(got.rawShape()).first);
|
||||
REQUIRE(got.vertexCount() == expected.vertexCount());
|
||||
for (unsigned long i = 0; i < expected.vertexCount(); ++i) {
|
||||
REQUIRE(getX(got.vertex(i)) == getX(expected.vertex(i)));
|
||||
REQUIRE(getY(got.vertex(i)) == getY(expected.vertex(i)));
|
||||
}
|
||||
};
|
||||
|
||||
Item item{pitem};
|
||||
require_same_vertices(Item(placer.leftPoly(item)), Item{left_control});
|
||||
require_same_vertices(Item(placer.downPoly(item)), Item{down_control});
|
||||
}
|
||||
|
||||
TEST_CASE("EdgeCache maps a parameter to a contour point", "[Geometry][NFP]") {
|
||||
RectangleItem input(10, 10);
|
||||
placers::EdgeCache<PolygonImpl> ecache(input);
|
||||
|
||||
auto first = *input.begin();
|
||||
REQUIRE(getX(first) == getX(ecache.coords(0)));
|
||||
REQUIRE(getY(first) == getY(ecache.coords(0)));
|
||||
|
||||
auto last = *std::prev(input.end());
|
||||
REQUIRE(getX(last) == getX(ecache.coords(1.0)));
|
||||
REQUIRE(getY(last) == getY(ecache.coords(1.0)));
|
||||
|
||||
for (int i = 0; i <= 100; ++i)
|
||||
REQUIRE(shapelike::touches(ecache.coords(i * 0.01), input.transformedShape()));
|
||||
}
|
||||
|
||||
TEST_CASE("Merging a pile with a polygon", "[Geometry][NFP]") {
|
||||
RectangleItem rect1(10, 15), rect2(15, 15), rect3(20, 15);
|
||||
rect2.translate({10, 0});
|
||||
rect3.translate({25, 0});
|
||||
|
||||
TMultiShape<PolygonImpl> pile;
|
||||
pile.push_back(rect1.transformedShape());
|
||||
pile.push_back(rect2.transformedShape());
|
||||
|
||||
auto result = nfp::merge(pile, rect3.transformedShape());
|
||||
REQUIRE(result.size() == 1); // the three abutting rectangles merge into one
|
||||
|
||||
RectangleItem ref(45, 15);
|
||||
REQUIRE_THAT(shapelike::area(result.front()),
|
||||
Catch::Matchers::WithinRel(ref.area(), 1e-9));
|
||||
}
|
||||
140
tests/libnest2d/test_nfp_placer.cpp
Normal file
140
tests/libnest2d/test_nfp_placer.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include "libnest2d_test_utils.hpp"
|
||||
|
||||
using namespace libnest2d;
|
||||
|
||||
// NfpPlacer is the No-Fit-Polygon placement engine that Orca's arranger drives
|
||||
// (via _Nester/FirstFitSelection in Arrange.cpp). These exercise the placer
|
||||
// directly: pack()/accept() are the core geometric placement primitives.
|
||||
namespace {
|
||||
|
||||
struct NfpPlacerFixture {
|
||||
using Cfg = NfpPlacer::Config;
|
||||
Box bin{250000000, 210000000}; // 250 x 210 mm bed at 1e6 scale
|
||||
|
||||
NfpPlacer placer_with(Cfg cfg = {}) const {
|
||||
cfg.parallel = false; // deterministic, single-threaded for tests
|
||||
NfpPlacer p{bin};
|
||||
p.configure(cfg);
|
||||
return p;
|
||||
}
|
||||
|
||||
// pack + accept; returns whether the item was placed.
|
||||
static bool place(NfpPlacer &p, Item &item) {
|
||||
auto res = p.pack(item);
|
||||
if (res) p.accept(res);
|
||||
return bool(res);
|
||||
}
|
||||
|
||||
// Place every item and REQUIRE each one is packed.
|
||||
static void place_all(NfpPlacer &p, std::vector<RectangleItem> &items) {
|
||||
for (size_t i = 0; i < items.size(); ++i) {
|
||||
INFO("packing item " << i);
|
||||
REQUIRE(place(p, items[i]));
|
||||
}
|
||||
}
|
||||
|
||||
// No two items overlap (a shared edge is allowed) and each stays in the bin.
|
||||
void require_disjoint_in_bin(std::vector<RectangleItem> &items) const {
|
||||
for (size_t i = 0; i < items.size(); ++i) {
|
||||
REQUIRE(sl::isInside(items[i].boundingBox(), bin));
|
||||
for (size_t j = i + 1; j < items.size(); ++j) {
|
||||
const bool overlaps = Item::intersects(items[i], items[j]) &&
|
||||
!Item::touches(items[i], items[j]);
|
||||
INFO("items " << i << " and " << j);
|
||||
REQUIRE_FALSE(overlaps);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<RectangleItem> squares(size_t n, Coord side) {
|
||||
return std::vector<RectangleItem>(n, RectangleItem{side, side});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE_METHOD(NfpPlacerFixture, "NfpPlacer places a single item inside the bin", "[Nesting][Placer]") {
|
||||
NfpPlacer placer = placer_with();
|
||||
RectangleItem item{100000000, 100000000};
|
||||
|
||||
REQUIRE(place(placer, item));
|
||||
REQUIRE(placer.getItems().size() == 1u);
|
||||
REQUIRE(sl::isInside(item.boundingBox(), bin));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(NfpPlacerFixture, "NfpPlacer rejects an item larger than the bin", "[Nesting][Placer]") {
|
||||
NfpPlacer placer = placer_with();
|
||||
RectangleItem big{300000000, 300000000}; // wider and taller than the bin
|
||||
|
||||
auto res = placer.pack(big);
|
||||
REQUIRE_FALSE(bool(res));
|
||||
REQUIRE(placer.getItems().empty());
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(NfpPlacerFixture, "NfpPlacer positions the first item for any starting point", "[Nesting][Placer]") {
|
||||
// setInitialPosition() seeds the first item from the configured starting
|
||||
// corner; pack() (without accept()) drives that switch for every value.
|
||||
using A = Cfg::Alignment;
|
||||
auto start = GENERATE(A::CENTER, A::BOTTOM_LEFT, A::BOTTOM_RIGHT,
|
||||
A::TOP_LEFT, A::TOP_RIGHT, A::USER_DEFINED, A::DONT_ALIGN);
|
||||
CAPTURE(int(start));
|
||||
|
||||
Cfg cfg;
|
||||
cfg.starting_point = start;
|
||||
cfg.best_object_pos = bin.center();
|
||||
NfpPlacer placer = placer_with(cfg);
|
||||
|
||||
RectangleItem item{100000000, 100000000};
|
||||
auto res = placer.pack(item);
|
||||
REQUIRE(bool(res));
|
||||
REQUIRE(sl::isInside(item.boundingBox(), bin));
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(NfpPlacerFixture, "NfpPlacer packs many items without overlap", "[Nesting][Placer]") {
|
||||
// Each item is placed against the no-fit polygon of the growing pile.
|
||||
auto items = squares(GENERATE(2u, 6u, 9u), 60000000);
|
||||
NfpPlacer placer = placer_with();
|
||||
|
||||
place_all(placer, items);
|
||||
REQUIRE(placer.getItems().size() == items.size());
|
||||
require_disjoint_in_bin(items);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(NfpPlacerFixture, "NfpPlacer evaluates the rotation candidates", "[Nesting][Placer]") {
|
||||
Cfg cfg;
|
||||
cfg.rotations = {0.0, Pi / 2.0}; // exercise the rotation search loop
|
||||
NfpPlacer placer = placer_with(cfg);
|
||||
|
||||
std::vector<RectangleItem> rects = {
|
||||
{180000000, 40000000}, {180000000, 40000000}, {180000000, 40000000}};
|
||||
place_all(placer, rects);
|
||||
require_disjoint_in_bin(rects);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(NfpPlacerFixture, "NfpPlacer's final alignment keeps the pile clear of a fixed obstacle", "[Nesting][Placer]") {
|
||||
// A preloaded fixed item makes finalAlign's recentring keep the pile clear of
|
||||
// it instead of dropping it straight onto the bin centre. Box{w,h} centres on
|
||||
// the origin, so the obstacle sits there too; virtual keeps it in place.
|
||||
RectangleItem obstacle{80000000, 80000000};
|
||||
obstacle.translation({-40000000, -40000000}); // 80x80 mm centred in the bin (origin)
|
||||
obstacle.markAsFixedInBin(0);
|
||||
obstacle.is_virt_object = true;
|
||||
|
||||
auto items = squares(4, 30000000);
|
||||
{
|
||||
NfpPlacer placer = placer_with();
|
||||
NfpPlacer::ItemGroup fixed;
|
||||
fixed.emplace_back(obstacle);
|
||||
placer.preload(fixed);
|
||||
place_all(placer, items);
|
||||
} // the placer's destructor runs finalAlign, translating the packed items
|
||||
|
||||
for (size_t i = 0; i < items.size(); ++i) {
|
||||
INFO("item " << i);
|
||||
const bool overlaps = Item::intersects(items[i], obstacle) &&
|
||||
!Item::touches(items[i], obstacle);
|
||||
REQUIRE_FALSE(overlaps);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ add_executable(${_TEST_NAME}_tests
|
||||
test_aabbindirect.cpp
|
||||
test_appconfig.cpp
|
||||
test_arachne_walls.cpp
|
||||
test_arrange.cpp
|
||||
test_bambu_networking.cpp
|
||||
test_clipper_offset.cpp
|
||||
test_clipper_utils.cpp
|
||||
|
||||
224
tests/libslic3r/test_arrange.cpp
Normal file
224
tests/libslic3r/test_arrange.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include "libslic3r/Arrange.hpp"
|
||||
#include "libslic3r/BoundingBox.hpp"
|
||||
#include "libslic3r/ClipperUtils.hpp"
|
||||
#include "libslic3r/ExPolygon.hpp"
|
||||
|
||||
using namespace Slic3r;
|
||||
using namespace Slic3r::arrangement;
|
||||
|
||||
namespace {
|
||||
|
||||
using Catch::Matchers::WithinRel;
|
||||
|
||||
// Square of the given (scaled) side, lower-left at the origin. bed_idx starts at
|
||||
// 0 because arrange() seeds the nester's bin from it (see ModelArrange.cpp).
|
||||
ArrangePolygon make_square(coord_t side)
|
||||
{
|
||||
ArrangePolygon ap;
|
||||
Polygon p;
|
||||
p.points = {Point(0, 0), Point(side, 0), Point(side, side), Point(0, side)};
|
||||
ap.poly = ExPolygon(p);
|
||||
ap.bed_idx = 0;
|
||||
return ap;
|
||||
}
|
||||
|
||||
ArrangePolygons squares(int n, double side_mm)
|
||||
{
|
||||
ArrangePolygons items;
|
||||
for (int i = 0; i < n; ++i)
|
||||
items.emplace_back(make_square(scaled(side_mm)));
|
||||
return items;
|
||||
}
|
||||
|
||||
// Bed [0,0]..[w,h] in scaled coordinates.
|
||||
BoundingBox bed(double w_mm, double h_mm)
|
||||
{
|
||||
return BoundingBox(Point(0, 0), Point(scaled(w_mm), scaled(h_mm)));
|
||||
}
|
||||
|
||||
// The default progress callback prints to stdout; silence it.
|
||||
ArrangeParams quiet_params(coord_t min_dist = 0)
|
||||
{
|
||||
ArrangeParams p{min_dist};
|
||||
p.progressind = [](unsigned, std::string) {};
|
||||
return p;
|
||||
}
|
||||
|
||||
ExPolygons placed_shapes(const ArrangePolygons &items)
|
||||
{
|
||||
ExPolygons out;
|
||||
out.reserve(items.size());
|
||||
for (const ArrangePolygon &ap : items)
|
||||
out.emplace_back(ap.transformed_poly());
|
||||
return out;
|
||||
}
|
||||
|
||||
// Area double-counted across the shapes: the sum counts overlaps twice, the
|
||||
// union once, so the difference is the overlapping area (0 when disjoint).
|
||||
double overlap_area(const ExPolygons &shapes)
|
||||
{
|
||||
double sum = 0;
|
||||
for (const ExPolygon &e : shapes)
|
||||
sum += e.area();
|
||||
double uni = 0;
|
||||
for (const ExPolygon &e : union_ex(shapes))
|
||||
uni += e.area();
|
||||
return sum - uni;
|
||||
}
|
||||
|
||||
// Relative tolerance absorbs the area-unit rounding the clipper union introduces.
|
||||
bool disjoint(const ExPolygons &shapes)
|
||||
{
|
||||
double total = 0;
|
||||
for (const ExPolygon &e : shapes)
|
||||
total += e.area();
|
||||
return overlap_area(shapes) <= total * 1e-9;
|
||||
}
|
||||
|
||||
void require_no_overlap(const ArrangePolygons &items)
|
||||
{
|
||||
REQUIRE(disjoint(placed_shapes(items)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Prove the overlap check the other tests rely on actually detects overlap.
|
||||
TEST_CASE("overlap_area detects overlap and ignores touching edges", "[Arrange]")
|
||||
{
|
||||
auto square_at = [](double x_mm) {
|
||||
ArrangePolygon ap = make_square(scaled(20.));
|
||||
ap.translation = Vec2crd(scaled(x_mm), 0);
|
||||
return ap.transformed_poly();
|
||||
};
|
||||
ExPolygon a = square_at(0.);
|
||||
|
||||
SECTION("disjoint shapes are reported disjoint") {
|
||||
REQUIRE(disjoint({a, square_at(30.)}));
|
||||
}
|
||||
SECTION("edge-touching shapes are reported disjoint") {
|
||||
REQUIRE(disjoint({a, square_at(20.)}));
|
||||
}
|
||||
SECTION("overlapping shapes are not, and the area is measured") {
|
||||
REQUIRE_FALSE(disjoint({a, square_at(10.)}));
|
||||
REQUIRE_THAT(overlap_area({a, square_at(10.)}),
|
||||
WithinRel(double(scaled(10.)) * scaled(20.), 1e-9)); // 10x20 mm
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Arrange places every item on the physical bed", "[Arrange]")
|
||||
{
|
||||
ArrangePolygons items = squares(5, 20.);
|
||||
arrange(items, bed(200, 200), quiet_params(scaled(1.)));
|
||||
|
||||
for (const ArrangePolygon &ap : items)
|
||||
REQUIRE(ap.bed_idx == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Arranged items stay within the bed", "[Arrange]")
|
||||
{
|
||||
ArrangePolygons items = squares(6, 30.);
|
||||
arrange(items, bed(200, 200), quiet_params(scaled(1.)));
|
||||
|
||||
for (const ArrangePolygon &ap : items) {
|
||||
REQUIRE(ap.bed_idx == 0);
|
||||
REQUIRE(bed(200, 200).contains(ap.transformed_poly().contour.bounding_box()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Arranged items do not overlap", "[Arrange]")
|
||||
{
|
||||
ArrangePolygons items = squares(6, 40.);
|
||||
arrange(items, bed(250, 250), quiet_params(scaled(2.)));
|
||||
|
||||
require_no_overlap(items);
|
||||
}
|
||||
|
||||
TEST_CASE("Arrange spaces items by their inflation", "[Arrange]")
|
||||
{
|
||||
// Per-item inflation is how the arranger enforces clearance (the GUI fills it
|
||||
// from min_obj_distance). Two items inflated 4mm each end up >= 8mm apart.
|
||||
ArrangePolygons items = squares(4, 20.);
|
||||
for (ArrangePolygon &ap : items)
|
||||
ap.inflation = scaled(4.);
|
||||
arrange(items, bed(200, 200), quiet_params());
|
||||
|
||||
// Axis-aligned squares are their own bounding boxes, so the clearance between
|
||||
// a pair is the distance between their boxes (1mm slack for nester rounding).
|
||||
std::vector<BoundingBox> boxes;
|
||||
for (const ExPolygon &e : placed_shapes(items))
|
||||
boxes.push_back(e.contour.bounding_box());
|
||||
|
||||
double min_gap = std::numeric_limits<double>::max();
|
||||
for (size_t i = 0; i < boxes.size(); ++i)
|
||||
for (size_t j = i + 1; j < boxes.size(); ++j) {
|
||||
coord_t sx = std::max<coord_t>(0, std::max(boxes[j].min.x() - boxes[i].max.x(),
|
||||
boxes[i].min.x() - boxes[j].max.x()));
|
||||
coord_t sy = std::max<coord_t>(0, std::max(boxes[j].min.y() - boxes[i].max.y(),
|
||||
boxes[i].min.y() - boxes[j].max.y()));
|
||||
min_gap = std::min(min_gap, std::sqrt(double(sx) * sx + double(sy) * sy));
|
||||
}
|
||||
|
||||
REQUIRE(min_gap >= double(scaled(8.)) - double(scaled(0.5)));
|
||||
}
|
||||
|
||||
TEST_CASE("An item larger than the bed cannot be placed", "[Arrange]")
|
||||
{
|
||||
ArrangePolygons items;
|
||||
items.emplace_back(make_square(scaled(20.)));
|
||||
items.emplace_back(make_square(scaled(400.))); // far bigger than the bed
|
||||
|
||||
arrange(items, bed(200, 200), quiet_params(scaled(1.)));
|
||||
|
||||
REQUIRE(items[0].bed_idx == 0);
|
||||
REQUIRE(items[1].bed_idx == UNARRANGED);
|
||||
}
|
||||
|
||||
TEST_CASE("Items overflowing one bed spill onto virtual beds", "[Arrange]")
|
||||
{
|
||||
ArrangePolygons items = squares(8, 90.); // eight 90mm squares cannot share a 200x200 bed
|
||||
arrange(items, bed(200, 200), quiet_params(scaled(2.)));
|
||||
|
||||
int max_bed = 0;
|
||||
for (const ArrangePolygon &ap : items) {
|
||||
REQUIRE(ap.bed_idx >= 0); // placed somewhere
|
||||
max_bed = std::max(max_bed, ap.bed_idx);
|
||||
}
|
||||
REQUIRE(max_bed >= 1); // at least one on a virtual bed
|
||||
}
|
||||
|
||||
TEST_CASE("Arrange handles an empty input", "[Arrange]")
|
||||
{
|
||||
ArrangePolygons items;
|
||||
REQUIRE_NOTHROW(arrange(items, bed(200, 200), quiet_params()));
|
||||
REQUIRE(items.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Arrange without final alignment keeps items disjoint", "[Arrange]")
|
||||
{
|
||||
// do_final_align = false selects Alignment::DONT_ALIGN (skips recentering).
|
||||
ArrangePolygons items = squares(6, 40.);
|
||||
ArrangeParams params = quiet_params(scaled(2.));
|
||||
params.do_final_align = false;
|
||||
|
||||
arrange(items, bed(250, 250), params);
|
||||
|
||||
for (const ArrangePolygon &ap : items)
|
||||
REQUIRE(ap.bed_idx == 0);
|
||||
require_no_overlap(items);
|
||||
}
|
||||
|
||||
TEST_CASE("Arrange aligns the pile to a custom center", "[Arrange]")
|
||||
{
|
||||
// align_center != (0.5, 0.5) selects Alignment::USER_DEFINED.
|
||||
ArrangePolygons items = squares(5, 30.);
|
||||
ArrangeParams params = quiet_params(scaled(2.));
|
||||
params.align_center = Vec2d(0.3, 0.7);
|
||||
|
||||
arrange(items, bed(250, 250), params);
|
||||
|
||||
for (const ArrangePolygon &ap : items)
|
||||
REQUIRE(ap.bed_idx == 0);
|
||||
require_no_overlap(items);
|
||||
}
|
||||
Reference in New Issue
Block a user