Use multiple expolygons as inputs

This commit is contained in:
tamasmeszaros 2021-10-19 18:26:46 +02:00
parent 069ab8aca1
commit b5fe845668

View File

@ -28,13 +28,15 @@ constexpr const char * CLIPTYPE_STR[] = { "ctIntersection", "ctUnion", "ctDiffer
constexpr const char * JOINTYPE_STR[] = { "jtSquare", "jtRound", "jtMiter"}; constexpr const char * JOINTYPE_STR[] = { "jtSquare", "jtRound", "jtMiter"};
void eval_clipping(const char *prefix, const ExPolygon &a, const ExPolygon &b) void eval_clipping(const char *prefix, const ExPolygons &a, const ExPolygons &b)
{ {
BoundingBox bb{a}; bb.merge(BoundingBox{b}); BoundingBox bb = get_extents(a); bb.merge(get_extents(b));
SVG svgin(std::string(prefix) + "_input_clipping.svg", bb); SVG svgin(std::string(prefix) + "_input_clipping.svg", bb);
svgin.draw_outline(a); svgin.draw(a, "green", 0.5);
svgin.draw_outline(b); svgin.draw(b, "red", 0.5);
svgin.draw_outline(a, "black", "blue");
svgin.draw_outline(b, "yellow", "magenta");
svgin.Close(); svgin.Close();
for (auto bPolyType : {ClipperLib::ptSubject, ClipperLib::ptClip}) for (auto bPolyType : {ClipperLib::ptSubject, ClipperLib::ptClip})
@ -42,13 +44,17 @@ void eval_clipping(const char *prefix, const ExPolygon &a, const ExPolygon &b)
for (auto filltype : {ClipperLib::pftEvenOdd, ClipperLib::pftNegative, ClipperLib::pftNonZero, ClipperLib::pftPositive}) { for (auto filltype : {ClipperLib::pftEvenOdd, ClipperLib::pftNegative, ClipperLib::pftNonZero, ClipperLib::pftPositive}) {
ClipperLib::Clipper clipper; ClipperLib::Clipper clipper;
clipper.AddPath(a.contour.points, ClipperLib::ptSubject, true); for (auto &p : a) {
for (auto &h : a.holes) clipper.AddPath(p.contour.points, ClipperLib::ptSubject, true);
clipper.AddPath(h.points, ClipperLib::ptSubject, true); for (auto &h : p.holes)
clipper.AddPath(h.points, ClipperLib::ptSubject, true);
}
clipper.AddPath(b.contour.points, bPolyType, true); for (auto &p : b) {
for (auto &h : b.holes) clipper.AddPath(p.contour.points, bPolyType, true);
clipper.AddPath(h.points, bPolyType, true); for (auto &h : p.holes)
clipper.AddPath(h.points, bPolyType, true);
}
ClipperLib::PolyTree tree; ClipperLib::PolyTree tree;
clipper.Execute(cliptype, tree, filltype); clipper.Execute(cliptype, tree, filltype);
@ -59,9 +65,16 @@ void eval_clipping(const char *prefix, const ExPolygon &a, const ExPolygon &b)
} }
} }
constexpr const char * WEBPREFIX = "";//"https://cfl.prusa3d.com/download/attachments/73269824/";
void generate_clipping_report(std::fstream &report, const char * prefix) void generate_clipping_report(std::fstream &report, const char * prefix)
{ {
report << "Clipping input is: ![img](" << prefix << "_input_clipping.svg)\nA to the left, B to the right\n" << std::endl; report << "Clipping input is: ![img](" << WEBPREFIX << prefix
<< "_input_clipping.svg)\n**A** is filled with green and outlined "
"with black and blue (holes), **B** is filled with red and outined "
"with yellow and magenta (holes)\n"
<< std::endl;
for (auto ptype : PTYPE_STR) { for (auto ptype : PTYPE_STR) {
report << "When B is of type " << ptype << "\n\n"; report << "When B is of type " << ptype << "\n\n";
@ -76,7 +89,7 @@ void generate_clipping_report(std::fstream &report, const char * prefix)
for (auto cliptype : CLIPTYPE_STR) { for (auto cliptype : CLIPTYPE_STR) {
report << "|" << cliptype; report << "|" << cliptype;
for (auto filltype : FILLTYPE_STR) for (auto filltype : FILLTYPE_STR)
report << "|![img](" << prefix << "_clipping_" << cliptype << "_" << ptype << "_" << filltype << ".svg)"; report << "|![img](" << WEBPREFIX << prefix << "_clipping_" << cliptype << "_" << ptype << "_" << filltype << ".svg)";
report << "|\n"; report << "|\n";
} }
@ -89,6 +102,7 @@ void eval_offsetting(const char *prefix, const ExPolygons &polys)
auto bb = get_extents(polys); auto bb = get_extents(polys);
SVG svgin(std::string(prefix) + "_input_offsetting.svg", bb); SVG svgin(std::string(prefix) + "_input_offsetting.svg", bb);
svgin.draw(polys, "green", 0.5);
svgin.draw_outline(polys); svgin.draw_outline(polys);
svgin.Close(); svgin.Close();
@ -114,7 +128,7 @@ void eval_offsetting(const char *prefix, const ExPolygons &polys)
void generate_offsetting_report(std::fstream &report, const char * prefix) void generate_offsetting_report(std::fstream &report, const char * prefix)
{ {
report << "Offsetting input is: ![img](" << prefix << "_input_offsetting.svg)" << std::endl; report << "Offsetting input is: ![img](" << WEBPREFIX << prefix << "_input_offsetting.svg)\n" << std::endl;
report << "|Delta"; report << "|Delta";
for (auto jointype : JOINTYPE_STR) report << "|" << jointype; for (auto jointype : JOINTYPE_STR) report << "|" << jointype;
@ -127,7 +141,7 @@ void generate_offsetting_report(std::fstream &report, const char * prefix)
for (auto delta : {-1., 0., 1.}) { for (auto delta : {-1., 0., 1.}) {
report << "|" << delta; report << "|" << delta;
for (auto jtype : JOINTYPE_STR) for (auto jtype : JOINTYPE_STR)
report << "|![img](" << prefix << "_offsetting_delta_" << std::to_string(delta) << "_" << jtype << ".svg)"; report << "|![img](" << WEBPREFIX << prefix << "_offsetting_delta_" << std::to_string(delta) << "_" << jtype << ".svg)";
report << "|\n"; report << "|\n";
} }
@ -138,28 +152,32 @@ void generate_offsetting_report(std::fstream &report, const char * prefix)
int main() int main()
{ {
auto a = rectangle(10.), b = translate(rectangle(10.), 5., 5.); auto a = rectangle(10.), b = translate(rectangle(10.), 5., 5.);
a.holes.emplace_back(translate(rectangle(5), 2.5, 2.5));
b.holes.emplace_back(translate(rectangle(5), 7.5, 7.5));
std::reverse(a.holes.front().begin(), a.holes.front().end());
std::reverse(b.holes.front().begin(), b.holes.front().end());
ExPolygons A = {a, b};
ExPolygons B = A;
for (auto &b : B) b.translate(scaled(4.), scaled(-4.));
std::fstream report{"report.md", std::fstream::out}; std::fstream report{"report.md", std::fstream::out};
eval_offsetting("two_squares_implicit_union", {a, b}); constexpr const char *prefix = "two_squares_with_holes";
generate_offsetting_report(report, "two_squares_implicit_union"); eval_clipping(prefix, A, B);
generate_clipping_report(report, prefix);
ExPolygons ab = union_ex({a, b});
eval_offsetting("two_squares_explicit_union", ab);
generate_offsetting_report(report, "two_squares_explicit_union");
eval_clipping("two_squares", a, b);
generate_clipping_report(report, "two_squares");
report << "\n" << std::endl; report << "\n" << std::endl;
a.holes.emplace_back(translate(rectangle(5), 2.5, 2.5)); eval_offsetting(prefix, A);
std::reverse(a.holes.front().begin(), a.holes.front().end()); generate_offsetting_report(report, prefix);
report << "\n" << std::endl;
b.holes.emplace_back(translate(rectangle(5.), 7.5, 7.5)); // ExPolygons C = A;
std::reverse(b.holes.front().begin(), b.holes.front().end()); // for (auto &c : C) c.holes.clear();
// auto c = translate(rectangle(1.5, 1.5), 4.25, 4.25);
eval_clipping("two_squares_with_holes", a, b); // eval_offsetting("holed_square_with_rect_inside", {a, c});
generate_clipping_report(report, "two_squares_with_holes"); // generate_offsetting_report(report, "holed_square_with_rect_inside");
// report << "\n" << std::endl;
return 0; return 0;
} }