diff options
author | tamasmeszaros <meszaros.q@gmail.com> | 2018-06-05 13:02:45 +0300 |
---|---|---|
committer | tamasmeszaros <meszaros.q@gmail.com> | 2018-06-05 13:02:45 +0300 |
commit | 34c850fa9da216070e07b7276fb1ba99c8dbe32b (patch) | |
tree | d6b06e2f8b01778185301363fb4107a655d5efcd /xs/src/libnest2d/tests/main.cpp | |
parent | fd829580e9c9c1f92e4a2338bcee35a5b319030a (diff) |
initial NFP method with convex polygons working.
Diffstat (limited to 'xs/src/libnest2d/tests/main.cpp')
-rw-r--r-- | xs/src/libnest2d/tests/main.cpp | 242 |
1 files changed, 35 insertions, 207 deletions
diff --git a/xs/src/libnest2d/tests/main.cpp b/xs/src/libnest2d/tests/main.cpp index 3d5baca76..633fe0c97 100644 --- a/xs/src/libnest2d/tests/main.cpp +++ b/xs/src/libnest2d/tests/main.cpp @@ -7,232 +7,56 @@ #include "printer_parts.h" #include "benchmark.h" +#include "svgtools.hpp" -namespace { using namespace libnest2d; using ItemGroup = std::vector<std::reference_wrapper<Item>>; -//using PackGroup = std::vector<ItemGroup>; -template<int SCALE, class Bin > -void exportSVG(PackGroup& result, const Bin& bin) { - - std::string loc = "out"; - - static std::string svg_header = -R"raw(<?xml version="1.0" encoding="UTF-8" standalone="yes"?> -<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> -<svg height="500" width="500" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> -)raw"; - - int i = 0; - for(auto r : result) { - std::fstream out(loc + std::to_string(i) + ".svg", std::fstream::out); - if(out.is_open()) { - out << svg_header; - Item rbin( Rectangle(bin.width(), bin.height()) ); - for(unsigned i = 0; i < rbin.vertexCount(); i++) { - auto v = rbin.vertex(i); - setY(v, -getY(v)/SCALE + 500 ); - setX(v, getX(v)/SCALE); - rbin.setVertex(i, v); - } - out << ShapeLike::serialize<Formats::SVG>(rbin.rawShape()) << std::endl; - for(Item& sh : r) { - Item tsh(sh.transformedShape()); - for(unsigned i = 0; i < tsh.vertexCount(); i++) { - auto v = tsh.vertex(i); - setY(v, -getY(v)/SCALE + 500); - setX(v, getX(v)/SCALE); - tsh.setVertex(i, v); - } - out << ShapeLike::serialize<Formats::SVG>(tsh.rawShape()) << std::endl; - } - out << "\n</svg>" << std::endl; - } - out.close(); - - i++; +std::vector<Item>& _parts(std::vector<Item>& ret, const TestData& data) +{ + if(ret.empty()) { + ret.reserve(data.size()); + for(auto& inp : data) + ret.emplace_back(inp); } -} - -template< int SCALE, class Bin> -void exportSVG(ItemGroup& result, const Bin& bin, int idx) { - - std::string loc = "out"; - static std::string svg_header = -R"raw(<?xml version="1.0" encoding="UTF-8" standalone="yes"?> -<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> -<svg height="500" width="500" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> -)raw"; - - int i = idx; - auto r = result; -// for(auto r : result) { - std::fstream out(loc + std::to_string(i) + ".svg", std::fstream::out); - if(out.is_open()) { - out << svg_header; - Item rbin( Rectangle(bin.width(), bin.height()) ); - for(unsigned i = 0; i < rbin.vertexCount(); i++) { - auto v = rbin.vertex(i); - setY(v, -getY(v)/SCALE + 500 ); - setX(v, getX(v)/SCALE); - rbin.setVertex(i, v); - } - out << ShapeLike::serialize<Formats::SVG>(rbin.rawShape()) << std::endl; - for(Item& sh : r) { - Item tsh(sh.transformedShape()); - for(unsigned i = 0; i < tsh.vertexCount(); i++) { - auto v = tsh.vertex(i); - setY(v, -getY(v)/SCALE + 500); - setX(v, getX(v)/SCALE); - tsh.setVertex(i, v); - } - out << ShapeLike::serialize<Formats::SVG>(tsh.rawShape()) << std::endl; - } - out << "\n</svg>" << std::endl; - } - out.close(); - -// i++; -// } + return ret; } -} - - -void findDegenerateCase() { - using namespace libnest2d; - - auto input = PRINTER_PART_POLYGONS; - - auto scaler = [](Item& item) { - for(unsigned i = 0; i < item.vertexCount(); i++) { - auto v = item.vertex(i); - setX(v, 100*getX(v)); setY(v, 100*getY(v)); - item.setVertex(i, v); - } - }; - - auto cmp = [](const Item& t1, const Item& t2) { - return t1.area() > t2.area(); - }; - - std::for_each(input.begin(), input.end(), scaler); - - std::sort(input.begin(), input.end(), cmp); - - Box bin(210*100, 250*100); - BottomLeftPlacer placer(bin); - - auto it = input.begin(); - auto next = it; - int i = 0; - while(it != input.end() && ++next != input.end()) { - placer.pack(*it); - placer.pack(*next); - - auto result = placer.getItems(); - bool valid = true; - - if(result.size() == 2) { - Item& r1 = result[0]; - Item& r2 = result[1]; - valid = !Item::intersects(r1, r2) || Item::touches(r1, r2); - valid = (valid && !r1.isInside(r2) && !r2.isInside(r1)); - if(!valid) { - std::cout << "error index: " << i << std::endl; - exportSVG<100>(result, bin, i); - } - } else { - std::cout << "something went terribly wrong!" << std::endl; - } +std::vector<Item>& prusaParts() { + static std::vector<Item> ret; + return _parts(ret, PRINTER_PART_POLYGONS); +} - placer.clearItems(); - it++; - i++; - } +std::vector<Item>& stegoParts() { + static std::vector<Item> ret; + return _parts(ret, STEGOSAUR_POLYGONS); } void arrangeRectangles() { using namespace libnest2d; - -// std::vector<Rectangle> input = { -// {80, 80}, -// {110, 10}, -// {200, 5}, -// {80, 30}, -// {60, 90}, -// {70, 30}, -// {80, 60}, -// {60, 60}, -// {60, 40}, -// {40, 40}, -// {10, 10}, -// {10, 10}, -// {10, 10}, -// {10, 10}, -// {10, 10}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {20, 20}, -// {80, 80}, -// {110, 10}, -// {200, 5}, -// {80, 30}, -// {60, 90}, -// {70, 30}, -// {80, 60}, -// {60, 60}, -// {60, 40}, -// {40, 40}, -// {10, 10}, -// {10, 10}, -// {10, 10}, -// {10, 10}, -// {10, 10}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {5, 5}, -// {20, 20} -// }; - - auto input = PRINTER_PART_POLYGONS; + auto input = stegoParts(); const int SCALE = 1000000; -// const int SCALE = 1; Box bin(210*SCALE, 250*SCALE); - auto scaler = [&SCALE, &bin](Item& item) { -// double max_area = 0; - for(unsigned i = 0; i < item.vertexCount(); i++) { - auto v = item.vertex(i); - setX(v, SCALE*getX(v)); setY(v, SCALE*getY(v)); - item.setVertex(i, v); -// double area = item.area(); -// if(max_area < area) { -// max_area = area; -// bin = item.boundingBox(); -// } - } - }; - - Coord min_obj_distance = 2*SCALE; + Coord min_obj_distance = 0; //6*SCALE; - std::for_each(input.begin(), input.end(), scaler); + NfpPlacer::Config pconf; + pconf.alignment = NfpPlacer::Config::Alignment::TOP_LEFT; + Arranger<NfpPlacer, DJDHeuristic> arrange(bin, min_obj_distance, pconf); - Arranger<BottomLeftPlacer, DJDHeuristic> arrange(bin, min_obj_distance); +// arrange.progressIndicator([&arrange, &bin](unsigned r){ +// svg::SVGWriter::Config conf; +// conf.mm_in_coord_units = SCALE; +// svg::SVGWriter svgw(conf); +// svgw.setSize(bin); +// svgw.writePackGroup(arrange.lastResult()); +// svgw.save("out"); +// std::cout << "Remaining items: " << r << std::endl; +// }); Benchmark bench; @@ -249,8 +73,12 @@ void arrangeRectangles() { std::cout << ret.second << std::endl; } - exportSVG<SCALE>(result, bin); - + svg::SVGWriter::Config conf; + conf.mm_in_coord_units = SCALE; + svg::SVGWriter svgw(conf); + svgw.setSize(bin); + svgw.writePackGroup(result); + svgw.save("out"); } int main(void /*int argc, char **argv*/) { |