test_thin.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //#define CATCH_CONFIG_DISABLE
  2. #include <catch2/catch.hpp>
  3. #include "test_data.hpp"
  4. #include <libslic3r/ClipperUtils.hpp>
  5. #include <libslic3r/MedialAxis.hpp>
  6. #include <libslic3r/SVG.hpp>
  7. #include <libslic3r/GCode.hpp>
  8. using namespace Slic3r;
  9. using namespace Slic3r::Geometry;
  10. using namespace Slic3r::Test;
  11. class ExtrusionVolumeVisitor : public ExtrusionVisitorConst {
  12. double volume = 0;
  13. public:
  14. virtual void use(const ExtrusionPath &path) override {
  15. for (int i = 0; i < path.polyline.size() - 1; i++) volume += path.polyline.points[i].distance_to(path.polyline.points[i + 1]) * path.mm3_per_mm;
  16. };
  17. virtual void use(const ExtrusionPath3D &path3D) override { std::cout << "error, not supported"; };
  18. virtual void use(const ExtrusionMultiPath &multipath) override {
  19. for (const ExtrusionPath &path : multipath.paths) use(path);
  20. }
  21. virtual void use(const ExtrusionMultiPath3D &multipath) override { std::cout << "error, not supported"; };
  22. virtual void use(const ExtrusionLoop &loop) override {
  23. for (const ExtrusionEntity &path : loop.paths) path.visit(*this);
  24. }
  25. virtual void use(const ExtrusionEntityCollection &collection) override {
  26. for (const ExtrusionEntity *path : collection.entities) path->visit(*this);
  27. }
  28. double compute(const ExtrusionEntity &entity) && {
  29. entity.visit(*this);
  30. return volume;
  31. }
  32. };
  33. SCENARIO("extrude_thinwalls") {
  34. GIVEN("ThickLine") {
  35. ExPolygon expolygon;
  36. expolygon.contour = Slic3r::Polygon{ Points{
  37. Point::new_scale(-0.5, 0),
  38. Point::new_scale(0.5, 0),
  39. Point::new_scale(0.3, 10),
  40. Point::new_scale(-0.3, 10) } };
  41. ThickPolylines res;
  42. MedialAxis{ expolygon, scale_(1.1), scale_(0.5), scale_(0.2) }.build(res);
  43. Flow periflow{ 1.1, 0.2, 0.4 };
  44. ExtrusionEntityCollection gap_fill = thin_variable_width(res, erGapFill, periflow);
  45. //std::string gcode = gcodegen.get_visitor_gcode();
  46. THEN("analyse extrusion.") {
  47. ExtrusionVolumeVisitor vis;
  48. std::cout << " volume is " << ExtrusionVolumeVisitor{}.compute(gap_fill) << "\n";
  49. std::cout << " wanted volume is " << ((0.6*0.2 * 10) + (0.2*0.2 * 10)) << "\n";
  50. REQUIRE(std::abs(ExtrusionVolumeVisitor{}.compute(gap_fill) - ((0.6*0.2 * 10) + (0.2*0.2 * 10)))<0.01);
  51. }
  52. }
  53. }
  54. SCENARIO("thin walls: ")
  55. {
  56. GIVEN("Square")
  57. {
  58. Points test_set;
  59. test_set.reserve(4);
  60. Points square {Point::new_scale(100, 100),
  61. Point::new_scale(200, 100),
  62. Point::new_scale(200, 200),
  63. Point::new_scale(100, 200)};
  64. Slic3r::Polygon hole_in_square{ Points{
  65. Point::new_scale(140, 140),
  66. Point::new_scale(140, 160),
  67. Point::new_scale(160, 160),
  68. Point::new_scale(160, 140) } };
  69. ExPolygon expolygon;
  70. expolygon.contour = Slic3r::Polygon{ square };
  71. expolygon.holes = Slic3r::Polygons{ hole_in_square };
  72. WHEN("creating the medial axis"){
  73. Polylines res;
  74. expolygon.medial_axis(scale_(40), scale_(0.5), &res);
  75. THEN("medial axis of a square shape is a single path"){
  76. REQUIRE(res.size() == 1);
  77. }
  78. THEN("polyline forms a closed loop"){
  79. REQUIRE(res[0].first_point().coincides_with(res[0].last_point()) == true);
  80. }
  81. THEN("medial axis loop has reasonable length"){
  82. REQUIRE(res[0].length() > expolygon.holes[0].length());
  83. REQUIRE(res[0].length() < expolygon.contour.length());
  84. }
  85. }
  86. }
  87. GIVEN("narrow rectangle") {
  88. ExPolygon expolygon;
  89. expolygon.contour = Slic3r::Polygon{ Points{
  90. Point::new_scale(100, 100),
  91. Point::new_scale(120, 100),
  92. Point::new_scale(120, 200),
  93. Point::new_scale(100, 200) } };
  94. Polylines res;
  95. expolygon.medial_axis(scale_(20), scale_(0.5), &res);
  96. ExPolygon expolygon2;
  97. expolygon2.contour = Slic3r::Polygon{ Points{
  98. Point::new_scale(100, 100),
  99. Point::new_scale(120, 100),
  100. Point::new_scale(120, 200),
  101. Point::new_scale(105, 200), // extra point in the short side
  102. Point::new_scale(100, 200) } };
  103. Polylines res2;
  104. expolygon.medial_axis(scale_(20), scale_(0.5), &res2);
  105. WHEN("creating the medial axis") {
  106. THEN("medial axis of a narrow rectangle is a single line") {
  107. REQUIRE(res.size() == 1);
  108. THEN("medial axis has reasonable length") {
  109. REQUIRE(res[0].length() >= scale_(200 - 100 - (120 - 100)) - SCALED_EPSILON);
  110. }
  111. }
  112. THEN("medial axis of a narrow rectangle with an extra vertex is still a single line") {
  113. REQUIRE(res2.size() == 1);
  114. THEN("medial axis of a narrow rectangle with an extra vertex has reasonable length") {
  115. REQUIRE(res2[0].length() >= scale_(200 - 100 - (120 - 100)) - SCALED_EPSILON);
  116. }
  117. THEN("extra vertices don't influence medial axis") {
  118. REQUIRE(res2[0].length() - res[0].length() < SCALED_EPSILON);
  119. }
  120. }
  121. }
  122. }
  123. //TODO: compare with mainline slic3r
  124. GIVEN("semicicumference") {
  125. ExPolygon expolygon;
  126. expolygon.contour = Slic3r::Polygon{ Points{
  127. Point{ 1185881, 829367 }, Point{ 1421988, 1578184 }, Point{ 1722442, 2303558 }, Point{ 2084981, 2999998 }, Point{ 2506843, 3662186 }, Point{ 2984809, 4285086 }, Point{ 3515250, 4863959 }, Point{ 4094122, 5394400 }, Point{ 4717018, 5872368 },
  128. Point{ 5379210, 6294226 }, Point{ 6075653, 6656769 }, Point{ 6801033, 6957229 }, Point{ 7549842, 7193328 }, Point{ 8316383, 7363266 }, Point{ 9094809, 7465751 }, Point{ 9879211, 7500000 }, Point{ 10663611, 7465750 }, Point{ 11442038, 7363265 },
  129. Point{ 12208580, 7193327 }, Point{ 12957389, 6957228 }, Point{ 13682769, 6656768 }, Point{ 14379209, 6294227 }, Point{ 15041405, 5872366 }, Point{ 15664297, 5394401 }, Point{ 16243171, 4863960 }, Point{ 16758641, 4301424 }, Point{ 17251579, 3662185 },
  130. Point{ 17673439, 3000000 }, Point{ 18035980, 2303556 }, Point{ 18336441, 1578177 }, Point{ 18572539, 829368 }, Point{ 18750748, 0 }, Point{ 19758422, 0 }, Point{ 19727293, 236479 }, Point{ 19538467, 1088188 }, Point{ 19276136, 1920196 },
  131. Point{ 18942292, 2726179 }, Point{ 18539460, 3499999 }, Point{ 18070731, 4235755 }, Point{ 17539650, 4927877 }, Point{ 16950279, 5571067 }, Point{ 16307090, 6160437 }, Point{ 15614974, 6691519 }, Point{ 14879209, 7160248 }, Point{ 14105392, 7563079 },
  132. Point{ 13299407, 7896927 }, Point{ 12467399, 8159255 }, Point{ 11615691, 8348082 }, Point{ 10750769, 8461952 }, Point{ 9879211, 8500000 }, Point{ 9007652, 8461952 }, Point{ 8142729, 8348082 }, Point{ 7291022, 8159255 }, Point{ 6459015, 7896927 },
  133. Point{ 5653029, 7563079 }, Point{ 4879210, 7160247 }, Point{ 4143447, 6691519 }, Point{ 3451331, 6160437 }, Point{ 2808141, 5571066 }, Point{ 2218773, 4927878 }, Point{ 1687689, 4235755 }, Point{ 1218962, 3499999 }, Point{ 827499, 2748020 },
  134. Point{ 482284, 1920196 }, Point{ 219954, 1088186 }, Point{ 31126, 236479 }, Point{ 0, 0 }, Point{ 1005754, 0 }
  135. } };
  136. WHEN("creating the medial axis") {
  137. Polylines res;
  138. expolygon.medial_axis(scale_(1.324888), scale_(0.25), &res);
  139. THEN("medial axis of a semicircumference is a single line") {
  140. REQUIRE(res.size() == 1);
  141. }
  142. THEN("all medial axis segments of a semicircumference have the same orientation (but the 2 end points)") {
  143. Lines lines = res[0].lines();
  144. double min_angle = 1, max_angle = -1;
  145. //std::cout << "first angle=" << lines[0].ccw(lines[1].b) << "\n";
  146. for (int idx = 1; idx < lines.size() - 1; idx++) {
  147. double angle = lines[idx - 1].ccw(lines[idx].b);
  148. if (std::abs(angle) - EPSILON < 0) angle = 0;
  149. //if (angle < 0) std::cout << unscale_(lines[idx - 1].a.x()) << ":" << unscale_(lines[idx - 1].a.y()) << " -> " << unscale_(lines[idx - 1].b.x()) << ":" << unscale_(lines[idx - 1].b.y()) << " -> " << unscale_(lines[idx].b.x()) << ":" << unscale_(lines[idx].b.y()) << "\n";
  150. std::cout << "angle=" << 180 * lines[idx].a.ccw_angle(lines[idx - 1].a, lines[idx].b) / PI << "\n";
  151. min_angle = std::min(min_angle, angle);
  152. max_angle = std::max(max_angle, angle);
  153. }
  154. //std::cout << "last angle=" << lines[lines.size() - 2].ccw(lines[lines.size() - 1].b) << "\n";
  155. // check whether turns are all CCW or all CW
  156. bool allccw = (min_angle <= 0 && max_angle <= 0);
  157. bool allcw = (min_angle >= 0 && max_angle >= 0);
  158. bool allsame_orientation = allccw || allcw;
  159. REQUIRE(allsame_orientation);
  160. }
  161. }
  162. }
  163. GIVEN("round with large and very small distance between points"){
  164. ExPolygon expolygon;
  165. expolygon.contour = Slic3r::Polygon{ Points{
  166. Point::new_scale(15.181601,-2.389639), Point::new_scale(15.112616,-1.320034), Point::new_scale(14.024491,-0.644338), Point::new_scale(13.978982,-0.624495), Point::new_scale(9.993299,0.855584), Point::new_scale(9.941970,0.871195), Point::new_scale(5.796743,1.872643),
  167. Point::new_scale(5.743826,1.882168), Point::new_scale(1.509170,2.386464), Point::new_scale(1.455460,2.389639), Point::new_scale(-2.809359,2.389639), Point::new_scale(-2.862805,2.386464), Point::new_scale(-7.097726,1.882168), Point::new_scale(-7.150378,1.872643), Point::new_scale(-11.286344,0.873576),
  168. Point::new_scale(-11.335028,0.858759), Point::new_scale(-14.348632,-0.237938), Point::new_scale(-14.360538,-0.242436), Point::new_scale(-15.181601,-0.737570), Point::new_scale(-15.171309,-2.388509)
  169. } };
  170. expolygon.holes.push_back(Slic3r::Polygon{ Points{
  171. Point::new_scale( -11.023311,-1.034226 ), Point::new_scale( -6.920984,-0.042941 ), Point::new_scale( -2.768613,0.463207 ), Point::new_scale( 1.414714,0.463207 ), Point::new_scale( 5.567085,-0.042941 ), Point::new_scale( 9.627910,-1.047563 )
  172. } });
  173. WHEN("creating the medial axis"){
  174. Polylines res;
  175. expolygon.medial_axis(scale_(2.5), scale_(0.5), &res);
  176. THEN("medial axis of it is two line"){
  177. REQUIRE(res.size() == 2);
  178. }
  179. }
  180. }
  181. GIVEN("french cross")
  182. {
  183. ExPolygon expolygon;
  184. expolygon.contour = Slic3r::Polygon{ Points{
  185. Point::new_scale(4.3, 4), Point::new_scale(4.3, 0), Point::new_scale(4, 0), Point::new_scale(4, 4), Point::new_scale(0, 4), Point::new_scale(0, 4.5), Point::new_scale(4, 4.5), Point::new_scale(4, 10), Point::new_scale(4.3, 10), Point::new_scale(4.3, 4.5),
  186. Point::new_scale(6, 4.5), Point::new_scale(6, 10), Point::new_scale(6.2, 10), Point::new_scale(6.2, 4.5), Point::new_scale(10, 4.5), Point::new_scale(10, 4), Point::new_scale(6.2, 4), Point::new_scale(6.2, 0), Point::new_scale(6, 0), Point::new_scale(6, 4),
  187. } };
  188. expolygon.contour.make_counter_clockwise();
  189. WHEN("creating the medial axis"){
  190. Polylines res;
  191. expolygon.medial_axis(scale_(0.55), scale_(0.25), &res);
  192. THEN("medial axis of a (bit too narrow) french cross is two lines"){
  193. REQUIRE(res.size() == 2);
  194. }
  195. THEN("medial axis has reasonable length"){
  196. REQUIRE(res[0].length() >= scale_(9.9) - SCALED_EPSILON);
  197. REQUIRE(res[1].length() >= scale_(9.9) - SCALED_EPSILON);
  198. }
  199. THEN("medial axis of a (bit too narrow) french cross is two lines has only strait lines (first line)"){
  200. Lines lines = res[0].lines();
  201. double min_angle = 1, max_angle = -1;
  202. for (int idx = 1; idx < lines.size(); idx++){
  203. double angle = lines[idx - 1].ccw(lines[idx].b);
  204. min_angle = std::min(min_angle, angle);
  205. max_angle = std::max(max_angle, angle);
  206. }
  207. REQUIRE(min_angle == max_angle);
  208. REQUIRE(min_angle == 0);
  209. }
  210. THEN("medial axis of a (bit too narrow) french cross is two lines has only strait lines (second line)"){
  211. Lines lines = res[1].lines();
  212. double min_angle = 1, max_angle = -1;
  213. for (int idx = 1; idx < lines.size(); idx++){
  214. double angle = lines[idx - 1].ccw(lines[idx].b);
  215. min_angle = std::min(min_angle, angle);
  216. max_angle = std::max(max_angle, angle);
  217. }
  218. REQUIRE(min_angle == max_angle);
  219. REQUIRE(min_angle == 0);
  220. }
  221. }
  222. }
  223. //TODO: compare with mainline slic3r
  224. //GIVEN("tooth")
  225. //{
  226. // ExPolygon expolygon;
  227. // expolygon.contour = Slic3r::Polygon{ Points{
  228. // Point::new_scale(0.86526705, 1.4509841), Point::new_scale(0.57696039, 1.8637021),
  229. // Point::new_scale(0.4502297, 2.5569978), Point::new_scale(0.45626199, 3.2965596),
  230. // Point::new_scale(1.1218851, 3.3049455), Point::new_scale(0.96681072, 2.8243202),
  231. // Point::new_scale(0.86328971, 2.2056997), Point::new_scale(0.85367905, 1.7790778)
  232. // } };
  233. // expolygon.contour.make_counter_clockwise();
  234. // WHEN("creating the medial axis"){
  235. // Polylines res;
  236. // expolygon.medial_axis(scale_(1), scale_(0.25), &res);
  237. // THEN("medial axis of a tooth is two lines"){
  238. // REQUIRE(res.size() == 2);
  239. // THEN("medial axis has reasonable length") {
  240. // REQUIRE(res[0].length() >= scale_(1.4) - SCALED_EPSILON);
  241. // REQUIRE(res[1].length() >= scale_(1.4) - SCALED_EPSILON);
  242. // // TODO: check if min width is < 0.3 and max width is > 0.6 (min($res->[0]->width.front, $res->[0]->width.back) # problem: can't have access to width
  243. // //TODO: now i have access! correct it!
  244. // }
  245. // }
  246. // }
  247. //}
  248. GIVEN("Anchor & Tapers")
  249. {
  250. ExPolygon tooth;
  251. tooth.contour = Slic3r::Polygon{ Points{
  252. Point::new_scale(0,0), Point::new_scale(10,0), Point::new_scale(10,1.2), Point::new_scale(0,1.2)
  253. } };
  254. tooth.contour.make_counter_clockwise();
  255. ExPolygon base_part;
  256. base_part.contour = Slic3r::Polygon{ Points{
  257. Point::new_scale(0,-3), Point::new_scale(0,3), Point::new_scale(-2,3), Point::new_scale(-2,-3)
  258. } };
  259. base_part.contour.make_counter_clockwise();
  260. //expolygon.contour = Slic3r::Polygon{ Points{
  261. // //Point::new_scale(0, 13), Point::new_scale(-1, 13), Point::new_scale(-1, 0), Point::new_scale(0.0,0.0),
  262. // Point::new_scale(0,0.2), Point::new_scale(3,0.2), Point::new_scale(3,0.4), Point::new_scale(0,0.4),
  263. // Point::new_scale(0,1), Point::new_scale(3,1), Point::new_scale(3,1.3), Point::new_scale(0,1.3),
  264. // Point::new_scale(0,2), Point::new_scale(3,2), Point::new_scale(3,2.4), Point::new_scale(0,2.4),
  265. // Point::new_scale(0,3), Point::new_scale(3,3), Point::new_scale(3,3.5), Point::new_scale(0,3.5),
  266. // Point::new_scale(0,4), Point::new_scale(3,4), Point::new_scale(3,4.6), Point::new_scale(0,4.6),
  267. // Point::new_scale(0,5), Point::new_scale(3,5), Point::new_scale(3,5.7), Point::new_scale(0,5.7),
  268. // Point::new_scale(0,6), Point::new_scale(3,6), Point::new_scale(3,6.8), Point::new_scale(0,6.8),
  269. // Point::new_scale(0,7.5), Point::new_scale(3,7.5), Point::new_scale(3,8.4), Point::new_scale(0,8.4),
  270. // Point::new_scale(0,9), Point::new_scale(3,9), Point::new_scale(3,10), Point::new_scale(0,10),
  271. // Point::new_scale(0,11), Point::new_scale(3,11), Point::new_scale(3,12.2), Point::new_scale(0,12.2),
  272. //} };
  273. WHEN("1 nozzle, 0.2 layer height") {
  274. const coord_t nozzle_diam = scale_(1);
  275. ExPolygon anchor = union_ex(ExPolygons{ tooth }, intersection_ex(ExPolygons{ base_part }, offset_ex(tooth, nozzle_diam / 2)), true)[0];
  276. ThickPolylines res;
  277. //expolygon.medial_axis(scale_(1), scale_(0.5), &res);
  278. Slic3r::MedialAxis ma(tooth, nozzle_diam * 2, nozzle_diam/3, scale_(0.2));
  279. ma.use_bounds(anchor)
  280. .use_min_real_width(nozzle_diam)
  281. .use_tapers(0.25*nozzle_diam);
  282. ma.build(res);
  283. THEN("medial axis of a simple line is one line") {
  284. REQUIRE(res.size() == 1);
  285. THEN("medial axis has the length of the line + the length of the anchor") {
  286. std::cout << res[0].length() << "\n";
  287. REQUIRE(std::abs(res[0].length() - scale_(10.5)) < SCALED_EPSILON);
  288. }
  289. THEN("medial axis has the line width as max width") {
  290. double max_width = 0;
  291. for (coordf_t width : res[0].width) max_width = std::max(max_width, width);
  292. REQUIRE(std::abs(max_width - scale_(1.2)) < SCALED_EPSILON);
  293. }
  294. //compute the length of the tapers
  295. THEN("medial axis has good tapers length") {
  296. int l1 = 0;
  297. for (size_t idx = 0; idx < res[0].width.size() - 1 && res[0].width[idx] - nozzle_diam < SCALED_EPSILON; ++idx)
  298. l1 += res[0].lines()[idx].length();
  299. int l2 = 0;
  300. for (size_t idx = res[0].width.size() - 1; idx > 0 && res[0].width[idx] - nozzle_diam < SCALED_EPSILON; --idx)
  301. l2 += res[0].lines()[idx - 1].length();
  302. REQUIRE(std::abs(l1 - l2) < SCALED_EPSILON);
  303. REQUIRE(std::abs(l1 - scale_(0.25 - 0.1)) < SCALED_EPSILON);
  304. }
  305. }
  306. }
  307. WHEN("1.2 nozzle, 0.6 layer height") {
  308. const coord_t nozzle_diam = scale_(1.2);
  309. ExPolygon anchor = union_ex(ExPolygons{ tooth }, intersection_ex(ExPolygons{ base_part }, offset_ex(tooth, nozzle_diam / 4)), true)[0];
  310. ThickPolylines res;
  311. //expolygon.medial_axis(scale_(1), scale_(0.5), &res);
  312. Slic3r::MedialAxis ma(tooth, nozzle_diam * 2, nozzle_diam/3, scale_(0.6));
  313. ma.use_bounds(anchor)
  314. .use_min_real_width(nozzle_diam)
  315. .use_tapers(1.0*nozzle_diam);
  316. ma.build(res);
  317. THEN("medial axis of a simple line is one line") {
  318. REQUIRE(res.size() == 1);
  319. THEN("medial axis has the length of the line + the length of the anchor") {
  320. //0.3 because it's offseted by nozzle_diam / 4
  321. REQUIRE(std::abs(res[0].length() - scale_(10.3)) < SCALED_EPSILON);
  322. }
  323. THEN("medial axis can'ty have a line width below Flow::new_from_spacing(nozzle_diam).width") {
  324. double max_width = 0;
  325. for (coordf_t width : res[0].width) max_width = std::max(max_width, width);
  326. double min_width = Flow::new_from_spacing(float(unscale_(nozzle_diam)), float(unscale_(nozzle_diam)), 0.6f, false).scaled_width();
  327. REQUIRE(std::abs(max_width - min_width) < SCALED_EPSILON);
  328. REQUIRE(std::abs(max_width - nozzle_diam) > SCALED_EPSILON);
  329. }
  330. //compute the length of the tapers
  331. THEN("medial axis has a 45� taper and a shorter one") {
  332. int l1 = 0;
  333. for (size_t idx = 0; idx < res[0].width.size() - 1 && res[0].width[idx] - scale_(1.2) < SCALED_EPSILON; ++idx)
  334. l1 += res[0].lines()[idx].length();
  335. int l2 = 0;
  336. for (size_t idx = res[0].width.size() - 1; idx > 0 && res[0].width[idx] - scale_(1.2) < SCALED_EPSILON; --idx)
  337. l2 += res[0].lines()[idx - 1].length();
  338. //here the taper is limited by the 0-width spacing
  339. double min_width = Flow::new_from_spacing(float(unscale_(nozzle_diam)), float(unscale_(nozzle_diam)), 0.6f, false).scaled_width();
  340. REQUIRE(std::abs(l1 - l2) < SCALED_EPSILON);
  341. REQUIRE(l1 < scale_(0.6));
  342. REQUIRE(l1 > scale_(0.4));
  343. }
  344. }
  345. }
  346. }
  347. GIVEN("1� rotated tooths")
  348. {
  349. }
  350. GIVEN("narrow trapezoid")
  351. {
  352. ExPolygon expolygon;
  353. expolygon.contour = Slic3r::Polygon{ Points{
  354. Point::new_scale(100, 100),
  355. Point::new_scale(120, 100),
  356. Point::new_scale(112, 200),
  357. Point::new_scale(108, 200)
  358. } };
  359. WHEN("creating the medial axis"){
  360. Polylines res;
  361. expolygon.medial_axis(scale_(20), scale_(0.5), &res);
  362. THEN("medial axis of a narrow trapezoid is a single line"){
  363. REQUIRE(res.size() == 1);
  364. THEN("medial axis has reasonable length") {
  365. REQUIRE(res[0].length() >= scale_(200 - 100 - (120 - 100)) - SCALED_EPSILON);
  366. }
  367. }
  368. }
  369. }
  370. GIVEN("L shape")
  371. {
  372. ExPolygon expolygon;
  373. expolygon.contour = Slic3r::Polygon{ Points{
  374. Point::new_scale(100, 100),
  375. Point::new_scale(120, 100),
  376. Point::new_scale(120, 180),
  377. Point::new_scale(200, 180),
  378. Point::new_scale(200, 200),
  379. Point::new_scale(100, 200)
  380. } };
  381. WHEN("creating the medial axis"){
  382. Polylines res;
  383. expolygon.medial_axis(scale_(20), scale_(0.5), &res);
  384. THEN("medial axis of a L shape is a single line"){
  385. REQUIRE(res.size() == 1);
  386. THEN("medial axis has reasonable length") {
  387. // 20 is the thickness of the expolygon, which is subtracted from the ends
  388. REQUIRE(res[0].length() + 20 > scale_(80 * 2) - SCALED_EPSILON);
  389. REQUIRE(res[0].length() + 20 < scale_(100 * 2) + SCALED_EPSILON);
  390. }
  391. }
  392. }
  393. }
  394. GIVEN("shape"){
  395. ExPolygon expolygon;
  396. expolygon.contour = Slic3r::Polygon{ Points{
  397. Point{ -203064906, -51459966 }, Point{ -219312231, -51459966 }, Point{ -219335477, -51459962 }, Point{ -219376095, -51459962 }, Point{ -219412047, -51459966 },
  398. Point{ -219572094, -51459966 }, Point{ -219624814, -51459962 }, Point{ -219642183, -51459962 }, Point{ -219656665, -51459966 }, Point{ -220815482, -51459966 },
  399. Point{ -220815482, -37738966 }, Point{ -221117540, -37738966 }, Point{ -221117540, -51762024 }, Point{ -203064906, -51762024 },
  400. } };
  401. WHEN("creating the medial axis"){
  402. Polylines polylines;
  403. expolygon.medial_axis(819998, 102499.75, &polylines);
  404. double perimeter_len = expolygon.contour.split_at_first_point().length();
  405. THEN("medial axis has reasonable length"){
  406. double polyline_length = 0;
  407. for (Slic3r::Polyline &poly : polylines) polyline_length += poly.length();
  408. REQUIRE(polyline_length > perimeter_len * 3. / 8. - SCALED_EPSILON);
  409. }
  410. }
  411. }
  412. GIVEN("narrow triangle")
  413. {
  414. ExPolygon expolygon;
  415. expolygon.contour = Slic3r::Polygon{ Points{
  416. Point::new_scale(50, 100),
  417. Point::new_scale(1000, 102),
  418. Point::new_scale(50, 104)
  419. } };
  420. WHEN("creating the medial axis"){
  421. Polylines res;
  422. expolygon.medial_axis(scale_(4), scale_(0.5), &res);
  423. THEN("medial axis of a narrow triangle is a single line"){
  424. REQUIRE(res.size() == 1);
  425. THEN("medial axis has reasonable length") {
  426. REQUIRE(res[0].length() > scale_(200 - 100 - (120 - 100)) - SCALED_EPSILON);
  427. }
  428. }
  429. }
  430. }
  431. GIVEN("GH #2474")
  432. {
  433. ExPolygon expolygon;
  434. expolygon.contour = Slic3r::Polygon{ Points{
  435. Point{91294454, 31032190},
  436. Point{11294481, 31032190},
  437. Point{11294481, 29967810},
  438. Point{44969182, 29967810},
  439. Point{89909960, 29967808},
  440. Point{91294454, 29967808}
  441. } };
  442. WHEN("creating the medial axis") {
  443. Polylines res;
  444. expolygon.medial_axis(1871238, 500000, &res);
  445. THEN("medial axis is a single polyline") {
  446. REQUIRE(res.size() == 1);
  447. Slic3r::Polyline polyline = res[0];
  448. THEN("medial axis is horizontal and is centered") {
  449. double sum = 0;
  450. for (Line &l : polyline.lines()) sum += std::abs(l.b.y() - l.a.y());
  451. coord_t expected_y = expolygon.contour.bounding_box().center().y();
  452. REQUIRE((sum / polyline.size()) - expected_y < SCALED_EPSILON);
  453. }
  454. // order polyline from left to right
  455. if (polyline.first_point().x() > polyline.last_point().x()) polyline.reverse();
  456. THEN("expected x_min & x_max") {
  457. BoundingBox polyline_bb = polyline.bounding_box();
  458. REQUIRE(polyline.first_point().x() == polyline_bb.min.x());
  459. REQUIRE(polyline.last_point().x() == polyline_bb.max.x());
  460. }
  461. THEN("medial axis is not self-overlapping") {
  462. //TODO
  463. //REQUIRE(polyline.first_point().x() == polyline_bb.x_min());
  464. std::vector<coord_t> all_x;
  465. for (Point &p : polyline.points) all_x.push_back(p.x());
  466. std::vector<coord_t> sorted_x{ all_x };
  467. std::sort(sorted_x.begin(), sorted_x.end());
  468. for (size_t i = 0; i < all_x.size(); i++) {
  469. REQUIRE(all_x[i] == sorted_x[i]);
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }