test_extrusion_entity.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include <catch2/catch.hpp>
  2. #include <cstdlib>
  3. #include "libslic3r/ExtrusionEntityCollection.hpp"
  4. #include "libslic3r/ExtrusionEntity.hpp"
  5. #include "libslic3r/Point.hpp"
  6. #include "libslic3r/ShortestPath.hpp"
  7. #include "libslic3r/libslic3r.h"
  8. #include "test_data.hpp"
  9. using namespace Slic3r;
  10. static inline Slic3r::Point random_point(float LO=-50, float HI=50)
  11. {
  12. Vec2f pt = Vec2f(LO, LO) + (Vec2d(rand(), rand()) * (HI-LO) / RAND_MAX).cast<float>();
  13. return pt.cast<coord_t>();
  14. }
  15. // build a sample extrusion entity collection with random start and end points.
  16. static Slic3r::ExtrusionPath random_path(size_t length = 20, float LO = -50, float HI = 50)
  17. {
  18. ExtrusionPath t{ ExtrusionAttributes{ ExtrusionRole::Perimeter, ExtrusionFlow{ 1.0, 1.0, 1.0 } } };
  19. for (size_t j = 0; j < length; ++ j)
  20. t.polyline.append(random_point(LO, HI));
  21. return t;
  22. }
  23. static Slic3r::ExtrusionPaths random_paths(size_t count = 10, size_t length = 20, float LO = -50, float HI = 50)
  24. {
  25. Slic3r::ExtrusionPaths p;
  26. for (size_t i = 0; i < count; ++ i)
  27. p.push_back(random_path(length, LO, HI));
  28. return p;
  29. }
  30. SCENARIO("ExtrusionPath", "[ExtrusionEntity]") {
  31. GIVEN("Simple path") {
  32. Slic3r::ExtrusionPath path{ { { 100, 100 }, { 200, 100 }, { 200, 200 } },
  33. ExtrusionAttributes{ ExtrusionRole::ExternalPerimeter, ExtrusionFlow{ 1., -1.f, -1.f } } };
  34. THEN("first point") {
  35. REQUIRE(path.first_point() == path.polyline.front());
  36. }
  37. THEN("cloned") {
  38. auto cloned = std::unique_ptr<ExtrusionEntity>(path.clone());
  39. REQUIRE(cloned->role() == path.role());
  40. }
  41. }
  42. }
  43. static ExtrusionPath new_extrusion_path(const Polyline &polyline, ExtrusionRole role, double mm3_per_mm)
  44. {
  45. return { polyline, ExtrusionAttributes{ role, ExtrusionFlow{ mm3_per_mm, -1.f, -1.f } } };
  46. }
  47. SCENARIO("ExtrusionLoop", "[ExtrusionEntity]")
  48. {
  49. GIVEN("Square") {
  50. Polygon square { { 100, 100 }, { 200, 100 }, { 200, 200 }, { 100, 200 } };
  51. ExtrusionLoop loop;
  52. loop.paths.emplace_back(new_extrusion_path(square.split_at_first_point(), ExtrusionRole::ExternalPerimeter, 1.));
  53. THEN("polygon area") {
  54. REQUIRE(loop.polygon().area() == Approx(square.area()));
  55. REQUIRE(loop.area() == Approx(square.area()));
  56. }
  57. THEN("loop length") {
  58. REQUIRE(loop.length() == Approx(square.length()));
  59. }
  60. WHEN("cloned") {
  61. auto loop2 = std::unique_ptr<ExtrusionLoop>(dynamic_cast<ExtrusionLoop*>(loop.clone()));
  62. THEN("cloning worked") {
  63. REQUIRE(loop2 != nullptr);
  64. }
  65. THEN("loop contains one path") {
  66. REQUIRE(loop2->paths.size() == 1);
  67. }
  68. THEN("cloned role") {
  69. REQUIRE(loop2->paths.front().role() == ExtrusionRole::ExternalPerimeter);
  70. }
  71. }
  72. WHEN("cloned and split") {
  73. auto loop2 = std::unique_ptr<ExtrusionLoop>(dynamic_cast<ExtrusionLoop*>(loop.clone()));
  74. loop2->split_at_vertex(square.points[2]);
  75. THEN("splitting a single-path loop results in a single path") {
  76. REQUIRE(loop2->paths.size() == 1);
  77. }
  78. THEN("path has correct number of points") {
  79. REQUIRE(loop2->paths.front().size() == 5);
  80. }
  81. THEN("expected point order") {
  82. REQUIRE(loop2->paths.front().polyline[0] == square.points[2]);
  83. REQUIRE(loop2->paths.front().polyline[1] == square.points[3]);
  84. REQUIRE(loop2->paths.front().polyline[2] == square.points[0]);
  85. REQUIRE(loop2->paths.front().polyline[3] == square.points[1]);
  86. REQUIRE(loop2->paths.front().polyline[4] == square.points[2]);
  87. }
  88. }
  89. }
  90. GIVEN("Loop with two pieces") {
  91. Polyline polyline1 { { 100, 100 }, { 200, 100 }, { 200, 200 } };
  92. Polyline polyline2 { { 200, 200 }, { 100, 200 }, { 100, 100 } };
  93. ExtrusionLoop loop;
  94. loop.paths.emplace_back(new_extrusion_path(polyline1, ExtrusionRole::ExternalPerimeter, 1.));
  95. loop.paths.emplace_back(new_extrusion_path(polyline2, ExtrusionRole::OverhangPerimeter, 1.));
  96. THEN("area") {
  97. REQUIRE(loop.area() == Approx(loop.polygon().area()));
  98. }
  99. double tot_len = polyline1.length() + polyline2.length();
  100. THEN("length") {
  101. REQUIRE(loop.length() == Approx(tot_len));
  102. }
  103. WHEN("splitting at intermediate point") {
  104. auto loop2 = std::unique_ptr<ExtrusionLoop>(dynamic_cast<ExtrusionLoop*>(loop.clone()));
  105. loop2->split_at_vertex(polyline1.points[1]);
  106. THEN("length after splitting is unchanged") {
  107. REQUIRE(loop2->length() == Approx(tot_len));
  108. }
  109. THEN("loop contains three paths after splitting") {
  110. REQUIRE(loop2->paths.size() == 3);
  111. }
  112. THEN("expected starting point") {
  113. REQUIRE(loop2->paths.front().polyline.front() == polyline1.points[1]);
  114. }
  115. THEN("expected ending point") {
  116. REQUIRE(loop2->paths.back().polyline.back() == polyline1.points[1]);
  117. }
  118. THEN("paths have common point") {
  119. REQUIRE(loop2->paths.front().polyline.back() == loop2->paths[1].polyline.front());
  120. REQUIRE(loop2->paths[1].polyline.back() == loop2->paths[2].polyline.front());
  121. }
  122. THEN("expected order after splitting") {
  123. REQUIRE(loop2->paths.front().role() == ExtrusionRole::ExternalPerimeter);
  124. REQUIRE(loop2->paths[1].role() == ExtrusionRole::OverhangPerimeter);
  125. REQUIRE(loop2->paths[2].role() == ExtrusionRole::ExternalPerimeter);
  126. }
  127. THEN("path has correct number of points") {
  128. REQUIRE(loop2->paths.front().polyline.size() == 2);
  129. REQUIRE(loop2->paths[1].polyline.size() == 3);
  130. REQUIRE(loop2->paths[2].polyline.size() == 2);
  131. }
  132. THEN("clipped path has expected length") {
  133. double l = loop2->length();
  134. ExtrusionPaths paths;
  135. loop2->clip_end(3, &paths);
  136. double l2 = 0;
  137. for (const ExtrusionPath &p : paths)
  138. l2 += p.length();
  139. REQUIRE(l2 == Approx(l - 3.));
  140. }
  141. }
  142. WHEN("splitting at endpoint") {
  143. auto loop2 = std::unique_ptr<ExtrusionLoop>(dynamic_cast<ExtrusionLoop*>(loop.clone()));
  144. loop2->split_at_vertex(polyline2.points.front());
  145. THEN("length after splitting is unchanged") {
  146. REQUIRE(loop2->length() == Approx(tot_len));
  147. }
  148. THEN("loop contains two paths after splitting") {
  149. REQUIRE(loop2->paths.size() == 2);
  150. }
  151. THEN("expected starting point") {
  152. REQUIRE(loop2->paths.front().polyline.front() == polyline2.points.front());
  153. }
  154. THEN("expected ending point") {
  155. REQUIRE(loop2->paths.back().polyline.back() == polyline2.points.front());
  156. }
  157. THEN("paths have common point") {
  158. REQUIRE(loop2->paths.front().polyline.back() == loop2->paths[1].polyline.front());
  159. REQUIRE(loop2->paths[1].polyline.back() == loop2->paths.front().polyline.front());
  160. }
  161. THEN("expected order after splitting") {
  162. REQUIRE(loop2->paths.front().role() == ExtrusionRole::OverhangPerimeter);
  163. REQUIRE(loop2->paths[1].role() == ExtrusionRole::ExternalPerimeter);
  164. }
  165. THEN("path has correct number of points") {
  166. REQUIRE(loop2->paths.front().polyline.size() == 3);
  167. REQUIRE(loop2->paths[1].polyline.size() == 3);
  168. }
  169. }
  170. WHEN("splitting at an edge") {
  171. Point point(250, 150);
  172. auto loop2 = std::unique_ptr<ExtrusionLoop>(dynamic_cast<ExtrusionLoop*>(loop.clone()));
  173. loop2->split_at(point, false, 0);
  174. THEN("length after splitting is unchanged") {
  175. REQUIRE(loop2->length() == Approx(tot_len));
  176. }
  177. Point expected_start_point(200, 150);
  178. THEN("expected starting point") {
  179. REQUIRE(loop2->paths.front().polyline.front() == expected_start_point);
  180. }
  181. THEN("expected ending point") {
  182. REQUIRE(loop2->paths.back().polyline.back() == expected_start_point);
  183. }
  184. }
  185. }
  186. GIVEN("Loop with four pieces") {
  187. Polyline polyline1 { { 59312736, 4821067 }, { 64321068, 4821067 }, { 64321068, 4821067 }, { 64321068, 9321068 }, { 59312736, 9321068 } };
  188. Polyline polyline2 { { 59312736, 9321068 }, { 9829401, 9321068 } };
  189. Polyline polyline3 { { 9829401, 9321068 }, { 4821067, 9321068 }, { 4821067, 4821067 }, { 9829401, 4821067 } };
  190. Polyline polyline4 { { 9829401, 4821067 }, { 59312736,4821067 } };
  191. ExtrusionLoop loop;
  192. loop.paths.emplace_back(new_extrusion_path(polyline1, ExtrusionRole::ExternalPerimeter, 1.));
  193. loop.paths.emplace_back(new_extrusion_path(polyline2, ExtrusionRole::OverhangPerimeter, 1.));
  194. loop.paths.emplace_back(new_extrusion_path(polyline3, ExtrusionRole::ExternalPerimeter, 1.));
  195. loop.paths.emplace_back(new_extrusion_path(polyline4, ExtrusionRole::OverhangPerimeter, 1.));
  196. double len = loop.length();
  197. THEN("area") {
  198. REQUIRE(loop.area() == Approx(loop.polygon().area()));
  199. }
  200. WHEN("splitting at vertex") {
  201. Point point(4821067, 9321068);
  202. if (! loop.split_at_vertex(point))
  203. loop.split_at(point, false, 0);
  204. THEN("total length is preserved after splitting") {
  205. REQUIRE(loop.length() == Approx(len));
  206. }
  207. THEN("order is correctly preserved after splitting") {
  208. REQUIRE(loop.paths.front().role() == ExtrusionRole::ExternalPerimeter);
  209. REQUIRE(loop.paths[1].role() == ExtrusionRole::OverhangPerimeter);
  210. REQUIRE(loop.paths[2].role() == ExtrusionRole::ExternalPerimeter);
  211. REQUIRE(loop.paths[3].role() == ExtrusionRole::OverhangPerimeter);
  212. }
  213. }
  214. }
  215. GIVEN("Some complex loop") {
  216. ExtrusionLoop loop;
  217. loop.paths.emplace_back(new_extrusion_path(
  218. Polyline { { 15896783, 15868739 }, { 24842049, 12117558 }, { 33853238, 15801279 }, { 37591780, 24780128 }, { 37591780, 24844970 },
  219. { 33853231, 33825297 }, { 24842049, 37509013 }, { 15896798, 33757841 }, { 12211841, 24812544 }, { 15896783, 15868739 } },
  220. ExtrusionRole::ExternalPerimeter, 1.));
  221. THEN("area") {
  222. REQUIRE(loop.area() == Approx(loop.polygon().area()));
  223. }
  224. double len = loop.length();
  225. THEN("split_at() preserves total length") {
  226. loop.split_at({ 15896783, 15868739 }, false, 0);
  227. REQUIRE(loop.length() == Approx(len));
  228. }
  229. }
  230. }
  231. SCENARIO("ExtrusionEntityCollection: Basics", "[ExtrusionEntity]")
  232. {
  233. Polyline polyline { { 100, 100 }, { 200, 100 }, { 200, 200 } };
  234. ExtrusionPath path = new_extrusion_path(polyline, ExtrusionRole::ExternalPerimeter, 1.);
  235. ExtrusionLoop loop;
  236. loop.paths.emplace_back(new_extrusion_path(Polygon(polyline.points).split_at_first_point(), ExtrusionRole::InternalInfill, 1.));
  237. ExtrusionEntityCollection collection;
  238. collection.append(path);
  239. THEN("no_sort is false by default") {
  240. REQUIRE(! collection.no_sort);
  241. }
  242. collection.append(collection);
  243. THEN("append ExtrusionEntityCollection") {
  244. REQUIRE(collection.entities.size() == 2);
  245. }
  246. collection.append(path);
  247. THEN("append ExtrusionPath") {
  248. REQUIRE(collection.entities.size() == 3);
  249. }
  250. collection.append(loop);
  251. THEN("append ExtrusionLoop") {
  252. REQUIRE(collection.entities.size() == 4);
  253. }
  254. THEN("appended collection was duplicated") {
  255. REQUIRE(dynamic_cast<ExtrusionEntityCollection*>(collection.entities[1])->entities.size() == 1);
  256. }
  257. WHEN("cloned") {
  258. auto coll2 = std::unique_ptr<ExtrusionEntityCollection>(dynamic_cast<ExtrusionEntityCollection*>(collection.clone()));
  259. THEN("expected no_sort value") {
  260. assert(! coll2->no_sort);
  261. }
  262. coll2->no_sort = true;
  263. THEN("no_sort is kept after clone") {
  264. auto coll3 = std::unique_ptr<ExtrusionEntityCollection>(dynamic_cast<ExtrusionEntityCollection*>(coll2->clone()));
  265. assert(coll3->no_sort);
  266. }
  267. }
  268. }
  269. SCENARIO("ExtrusionEntityCollection: Polygon flattening", "[ExtrusionEntity]")
  270. {
  271. srand(0xDEADBEEF); // consistent seed for test reproducibility.
  272. // Generate one specific random path set and save it for later comparison
  273. Slic3r::ExtrusionPaths nosort_path_set = random_paths();
  274. Slic3r::ExtrusionEntityCollection sub_nosort;
  275. sub_nosort.append(nosort_path_set);
  276. sub_nosort.no_sort = true;
  277. Slic3r::ExtrusionEntityCollection sub_sort;
  278. sub_sort.no_sort = false;
  279. sub_sort.append(random_paths());
  280. GIVEN("A Extrusion Entity Collection with a child that has one child that is marked as no-sort") {
  281. Slic3r::ExtrusionEntityCollection sample;
  282. Slic3r::ExtrusionEntityCollection output;
  283. sample.append(sub_sort);
  284. sample.append(sub_nosort);
  285. sample.append(sub_sort);
  286. WHEN("The EEC is flattened with default options (preserve_order=false)") {
  287. output = sample.flatten();
  288. THEN("The output EEC contains no Extrusion Entity Collections") {
  289. CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 0);
  290. }
  291. }
  292. WHEN("The EEC is flattened with preservation (preserve_order=true)") {
  293. output = sample.flatten(true);
  294. THEN("The output EECs contains one EEC.") {
  295. CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 1);
  296. }
  297. AND_THEN("The ordered EEC contains the same order of elements than the original") {
  298. // find the entity in the collection
  299. for (auto e : output.entities)
  300. if (e->is_collection()) {
  301. ExtrusionEntityCollection *temp = dynamic_cast<ExtrusionEntityCollection*>(e);
  302. // check each Extrusion path against nosort_path_set to see if the first and last match the same
  303. CHECK(nosort_path_set.size() == temp->entities.size());
  304. for (size_t i = 0; i < nosort_path_set.size(); ++ i) {
  305. CHECK(temp->entities[i]->first_point() == nosort_path_set[i].first_point());
  306. CHECK(temp->entities[i]->last_point() == nosort_path_set[i].last_point());
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. TEST_CASE("ExtrusionEntityCollection: Chained path", "[ExtrusionEntity]") {
  314. struct Test {
  315. Polylines unchained;
  316. Polylines chained;
  317. Point initial_point;
  318. };
  319. std::vector<Test> tests {
  320. {
  321. {
  322. { {0,15}, {0,18}, {0,20} },
  323. { {0,10}, {0,8}, {0,5} }
  324. },
  325. {
  326. { {0,20}, {0,18}, {0,15} },
  327. { {0,10}, {0,8}, {0,5} }
  328. },
  329. { 0, 30 }
  330. },
  331. {
  332. {
  333. { {4,0}, {10,0}, {15,0} },
  334. { {10,5}, {15,5}, {20,5} }
  335. },
  336. {
  337. { {20,5}, {15,5}, {10,5} },
  338. { {15,0}, {10,0}, {4,0} }
  339. },
  340. { 30, 0 }
  341. },
  342. {
  343. {
  344. { {15,0}, {10,0}, {4,0} },
  345. { {10,5}, {15,5}, {20,5} }
  346. },
  347. {
  348. { {20,5}, {15,5}, {10,5} },
  349. { {15,0}, {10,0}, {4,0} }
  350. },
  351. { 30, 0 }
  352. },
  353. };
  354. for (const Test &test : tests) {
  355. Polylines chained = chain_polylines(test.unchained, &test.initial_point);
  356. REQUIRE(chained == test.chained);
  357. ExtrusionEntityCollection unchained_extrusions;
  358. extrusion_entities_append_paths(unchained_extrusions.entities, test.unchained,
  359. ExtrusionAttributes{ ExtrusionRole::InternalInfill, ExtrusionFlow{ 0., 0.4f, 0.3f } });
  360. THEN("Chaining works") {
  361. ExtrusionEntityReferences chained_extrusions = chain_extrusion_references(unchained_extrusions, &test.initial_point);
  362. REQUIRE(chained_extrusions.size() == test.chained.size());
  363. for (size_t i = 0; i < chained_extrusions.size(); ++ i) {
  364. const Points &p1 = test.chained[i].points;
  365. Points p2 = chained_extrusions[i].cast<ExtrusionPath>()->polyline.points;
  366. if (chained_extrusions[i].flipped())
  367. std::reverse(p2.begin(), p2.end());
  368. REQUIRE(p1 == p2);
  369. }
  370. }
  371. THEN("Chaining produces no change with no_sort") {
  372. unchained_extrusions.no_sort = true;
  373. ExtrusionEntityReferences chained_extrusions = chain_extrusion_references(unchained_extrusions, &test.initial_point);
  374. REQUIRE(chained_extrusions.size() == test.unchained.size());
  375. for (size_t i = 0; i < chained_extrusions.size(); ++ i) {
  376. const Points &p1 = test.unchained[i].points;
  377. Points p2 = chained_extrusions[i].cast<ExtrusionPath>()->polyline.points;
  378. if (chained_extrusions[i].flipped())
  379. std::reverse(p2.begin(), p2.end());
  380. REQUIRE(p1 == p2);
  381. }
  382. }
  383. }
  384. }
  385. TEST_CASE("ExtrusionEntityCollection: Chained path with no explicit starting point", "[ExtrusionEntity]") {
  386. auto polylines = Polylines { { { 0, 15 }, {0, 18}, {0, 20} }, { { 0, 10 }, {0, 8}, {0, 5} } };
  387. auto target = Polylines { { {0, 5}, {0, 8}, { 0, 10 } }, { { 0, 15 }, {0, 18}, {0, 20} } };
  388. auto chained = chain_polylines(polylines);
  389. REQUIRE(chained == target);
  390. }