1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <iostream>
#include <boost/filesystem.hpp>
#include "libslic3r/ClipperUtils.hpp"
#include "libslic3r/ExPolygon.hpp"
#include "libslic3r/SVG.hpp"
using namespace Slic3r;
using namespace Catch;
// #define TESTS_EXPORT_SVGS
SCENARIO("Constant offset", "[ClipperUtils]") {
coord_t s = 1000000;
GIVEN("20mm box") {
ExPolygon box20mm;
box20mm.contour.points = { Vec2crd{ 0, 0 }, Vec2crd{ 20 * s, 0 }, Vec2crd{ 20 * s, 20 * s}, Vec2crd{ 0, 20 * s} };
std::vector<float> deltas_plus(box20mm.contour.points.size(), 1. * s);
std::vector<float> deltas_minus(box20mm.contour.points.size(), - 1. * s);
Polygons output;
WHEN("Slic3r::offset()") {
for (double miter : { 2.0, 1.5, 1.2 }) {
DYNAMIC_SECTION("plus 1mm, miter " << miter << "x") {
output = Slic3r::offset(box20mm, 1. * s, ClipperLib::jtMiter, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("constant_offset_box20mm_plus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(output, "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 22^2mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx(22. * 22. * s * s));
}
}
DYNAMIC_SECTION("minus 1mm, miter " << miter << "x") {
output = Slic3r::offset(box20mm, - 1. * s, ClipperLib::jtMiter, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("constant_offset_box20mm_minus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(output, "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 18^2mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx(18. * 18. * s * s));
}
}
}
}
WHEN("Slic3r::variable_offset_outer/inner") {
for (double miter : { 2.0, 1.5, 1.2 }) {
DYNAMIC_SECTION("plus 1mm, miter " << miter << "x") {
output = Slic3r::variable_offset_outer(box20mm, { deltas_plus }, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("variable_offset_box20mm_plus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(output, "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 22^2mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx(22. * 22. * s * s));
}
}
DYNAMIC_SECTION("minus 1mm, miter " << miter << "x") {
output = Slic3r::variable_offset_inner(box20mm, { deltas_minus }, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("variable_offset_box20mm_minus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(output, "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 18^2mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx(18. * 18. * s * s));
}
}
}
}
}
GIVEN("20mm box with 10mm hole") {
ExPolygon box20mm;
box20mm.contour.points = { Vec2crd{ 0, 0 }, Vec2crd{ 20 * s, 0 }, Vec2crd{ 20 * s, 20 * s}, Vec2crd{ 0, 20 * s} };
box20mm.holes.emplace_back(Slic3r::Polygon({ Vec2crd{ 5 * s, 5 * s }, Vec2crd{ 5 * s, 15 * s}, Vec2crd{ 15 * s, 15 * s}, Vec2crd{ 15 * s, 5 * s } }));
std::vector<float> deltas_plus(box20mm.contour.points.size(), 1. * s);
std::vector<float> deltas_minus(box20mm.contour.points.size(), -1. * s);
ExPolygons output;
SECTION("Slic3r::offset()") {
for (double miter : { 2.0, 1.5, 1.2 }) {
DYNAMIC_SECTION("miter " << miter << "x") {
WHEN("plus 1mm") {
output = Slic3r::offset_ex(box20mm, 1. * s, ClipperLib::jtMiter, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("constant_offset_box20mm_10mm_hole_plus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(to_polygons(output), "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 22^2-8^2 mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx((22. * 22. - 8. * 8.) * s * s));
}
}
WHEN("minus 1mm") {
output = Slic3r::offset_ex(box20mm, - 1. * s, ClipperLib::jtMiter, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("constant_offset_box20mm_10mm_hole_minus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(to_polygons(output), "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 18^2-12^2 mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx((18. * 18. - 12. * 12.) * s * s));
}
}
}
}
}
SECTION("Slic3r::variable_offset_outer()") {
for (double miter : { 2.0, 1.5, 1.2 }) {
DYNAMIC_SECTION("miter " << miter << "x") {
WHEN("plus 1mm") {
output = Slic3r::variable_offset_outer_ex(box20mm, { deltas_plus, deltas_plus }, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("variable_offset_box20mm_10mm_hole_plus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(to_polygons(output), "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 22^2-8^2 mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx((22. * 22. - 8. * 8.) * s * s));
}
}
WHEN("minus 1mm") {
output = Slic3r::variable_offset_inner_ex(box20mm, { deltas_minus, deltas_minus }, miter);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("variable_offset_box20mm_10mm_hole_minus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(box20mm, "blue");
svg.draw_outline(to_polygons(output), "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area is 18^2-12^2 mm2") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx((18. * 18. - 12. * 12.) * s * s));
}
}
}
}
}
}
GIVEN("20mm right angle triangle") {
ExPolygon triangle20mm;
triangle20mm.contour.points = { Vec2crd{ 0, 0 }, Vec2crd{ 20 * s, 0 }, Vec2crd{ 0, 20 * s } };
Polygons output;
double offset = 1.;
// Angle of the sharp corner bisector.
double angle_bisector = M_PI / 8.;
// Area tapered by mitering one sharp corner.
double area_tapered = pow(offset * (1. / sin(angle_bisector) - 1.), 2.) * tan(angle_bisector);
double l_triangle_side_offsetted = 20. + offset * (1. + 1. / tan(angle_bisector));
double area_offsetted = (0.5 * l_triangle_side_offsetted * l_triangle_side_offsetted - 2. * area_tapered) * s * s;
SECTION("Slic3r::offset()") {
for (double miter : { 2.0, 1.5, 1.2 }) {
DYNAMIC_SECTION("Outer offset 1mm, miter " << miter << "x") {
output = Slic3r::offset(triangle20mm, offset * s, ClipperLib::jtMiter, 2.0);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("constant_offset_triangle20mm_plus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(triangle20mm, "blue");
svg.draw_outline(output, "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area matches") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx(area_offsetted));
}
}
}
}
SECTION("Slic3r::variable_offset_outer()") {
std::vector<float> deltas(triangle20mm.contour.points.size(), 1. * s);
for (double miter : { 2.0, 1.5, 1.2 }) {
DYNAMIC_SECTION("Outer offset 1mm, miter " << miter << "x") {
output = Slic3r::variable_offset_outer(triangle20mm, { deltas }, 2.0);
#ifdef TESTS_EXPORT_SVGS
{
SVG svg(debug_out_path("variable_offset_triangle20mm_plus1mm_miter%lf.svg", miter).c_str(), get_extents(output));
svg.draw(triangle20mm, "blue");
svg.draw_outline(output, "black", coord_t(scale_(0.01)));
}
#endif
THEN("Area matches") {
REQUIRE(output.size() == 1);
REQUIRE(output.front().area() == Approx(area_offsetted));
}
}
}
}
}
}
|