Files
OrcaSlicer/tests/libnest2d/test_nfp_placer.cpp
T
HanifKoh 84657ff11e Add Missing Includes Across the Remaining Sources and Tests (#16071)
* Ignore Clipper, libpng, mcut and Boost.Polygon Internals in clang-tidy

Each only works through a wrapper or umbrella header: libslic3r/clipper.hpp or clipper_z.hpp configure Clipper before including it, png.h pulls in libpng's config headers, and Boost.Polygon's headers only compile through polygon.hpp or voronoi.hpp.

* Ignore minilzo's Config Headers in clang-tidy

lzoconf.h and lzodefs.h are internal to minilzo.h, which is what the code includes.

* Add Missing Includes Across the Remaining Sources and Tests

Covers src/slic3r/Utils, src/slic3r/plugin, src/slic3r/Config, src/libvgcode, src/dev-utils, src/OrcaSlicer.cpp and tests/, the directories left after src/slic3r/GUI and src/libslic3r. Generated with clang-tidy misc-include-cleaner. libvgcode's own headers are included by relative path as in the rest of that library, and Catch2 and pybind11 with angle brackets as elsewhere in the repo.

* Make the GUI and Test Headers Compile on Their Own

Each now includes, or forward-declares, what it uses instead of relying on what its includers happened to include first. Headers that only compile on one platform, or that nothing built includes, are left alone.

* Keep Windows and nanosvg Setup Ahead of the Added Includes

OrcaSlicer.cpp and several tests set _WIN32_WINNT, WIN32_LEAN_AND_MEAN or NOMINMAX before including Windows.h, and the profile validator defines NANOSVG_IMPLEMENTATION before any libslic3r header. The added includes had landed above those blocks, which broke the Windows build.

* Add the GUI Includes the First Pass Missed

Covers headers that only became editable once they compiled on their own, and wx symbols whose suggested header changed as the clang-tidy ignore list grew after the src/slic3r/GUI pass.

* Keep the Added Test Includes Below the NOMINMAX Guard

test_marchingsquares.cpp and test_texture_displacement.cpp had includes inside #ifndef NOMINMAX, which the tests inherit as defined on Windows from libslic3r, so those were skipped there. .clang-tidy also ignores the MSVC STL and UCRT internals, Boost.Multiprecision's fwd.hpp and CPython's Windows include directory, as in #16068.
2026-10-03 13:45:21 +08:00

154 lines
5.8 KiB
C++

#include <catch2/catch_all.hpp>
#include "libnest2d/libnest2d.hpp"
#include <vector>
#include <cstddef>
#include "libnest2d/geometry_traits.hpp"
#include "libnest2d/common.hpp"
#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.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]") {
// The placer only keeps references to the items it packs and re-reads them
// from finalAlign() in its destructor, so the item must outlive the placer.
RectangleItem item{100000000, 100000000};
NfpPlacer placer = placer_with();
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]") {
// The placer re-reads its packed items from finalAlign() in its destructor,
// so the items must outlive the placer — declare them first.
std::vector<RectangleItem> rects = {
{180000000, 40000000}, {180000000, 40000000}, {180000000, 40000000}};
Cfg cfg;
cfg.rotations = {0.0, Pi / 2.0}; // exercise the rotation search loop
NfpPlacer placer = placer_with(cfg);
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);
}
}