ExtrusionEntity.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. ///|/ Copyright (c) Prusa Research 2016 - 2023 Vojtěch Bubník @bubnikv, Lukáš Matěna @lukasmatena, Enrico Turri @enricoturri1966
  2. ///|/ Copyright (c) SuperSlicer 2023 Remi Durand @supermerill
  3. ///|/ Copyright (c) Slic3r 2013 - 2016 Alessandro Ranellucci @alranel
  4. ///|/ Copyright (c) 2014 Petr Ledvina @ledvinap
  5. ///|/
  6. ///|/ PrusaSlicer is released under the terms of the AGPLv3 or higher
  7. ///|/
  8. #include "ExtrusionEntity.hpp"
  9. #include "ExtrusionEntityCollection.hpp"
  10. #include "ExPolygon.hpp"
  11. #include "ClipperUtils.hpp"
  12. #include "Config.hpp"
  13. #include "Exception.hpp"
  14. #include "Extruder.hpp"
  15. #include "Flow.hpp"
  16. #include <cmath>
  17. #include <limits>
  18. #include <sstream>
  19. namespace Slic3r {
  20. //// extrusion entity visitor
  21. void ExtrusionVisitor::use(ExtrusionPath &path) { default_use(path); };
  22. void ExtrusionVisitor::use(ExtrusionPath3D &path3D) { default_use(path3D); }
  23. void ExtrusionVisitor::use(ExtrusionMultiPath &multipath) { default_use(multipath); }
  24. void ExtrusionVisitor::use(ExtrusionMultiPath3D &multipath3D) { default_use(multipath3D); }
  25. void ExtrusionVisitor::use(ExtrusionLoop &loop) { default_use(loop); }
  26. void ExtrusionVisitor::use(ExtrusionEntityCollection &collection) { default_use(collection); }
  27. void ExtrusionVisitorConst::use(const ExtrusionPath &path) { default_use(path); }
  28. void ExtrusionVisitorConst::use(const ExtrusionPath3D &path3D) { default_use(path3D); }
  29. void ExtrusionVisitorConst::use(const ExtrusionMultiPath &multipath) { default_use(multipath); }
  30. void ExtrusionVisitorConst::use(const ExtrusionMultiPath3D &multipath3D) { default_use(multipath3D); }
  31. void ExtrusionVisitorConst::use(const ExtrusionLoop &loop) { default_use(loop); }
  32. void ExtrusionVisitorConst::use(const ExtrusionEntityCollection &collection) { default_use(collection); }
  33. void ExtrusionEntity::visit(ExtrusionVisitor &&visitor) {
  34. this->visit(visitor);
  35. }
  36. void ExtrusionEntity::visit(ExtrusionVisitorConst &&visitor) const {
  37. this->visit(visitor);
  38. }
  39. void ExtrusionPath::intersect_expolygons(const ExPolygons &collection, ExtrusionEntityCollection *retval) const
  40. {
  41. this->_inflate_collection(intersection_pl(Polylines{this->polyline.to_polyline()}, collection), retval);
  42. }
  43. void ExtrusionPath::subtract_expolygons(const ExPolygons &collection, ExtrusionEntityCollection *retval) const
  44. {
  45. this->_inflate_collection(diff_pl(Polylines{this->polyline.to_polyline()}, collection), retval);
  46. }
  47. void ExtrusionPath::clip_end(coordf_t distance) { this->polyline.clip_end(distance); }
  48. void ExtrusionPath::simplify(coordf_t tolerance, ArcFittingType with_fitting_arc, double fitting_arc_tolerance)
  49. {
  50. if (with_fitting_arc != ArcFittingType::Disabled) {
  51. if (role().is_sparse_infill())
  52. // Use 3x lower resolution than the object fine detail for sparse infill.
  53. tolerance *= 3.;
  54. else if (role().is_support())
  55. // Use 4x lower resolution than the object fine detail for support.
  56. tolerance *= 4.;
  57. else if (role().is_skirt())
  58. // Brim is currently marked as skirt.
  59. // Use 4x lower resolution than the object fine detail for skirt & brim.
  60. tolerance *= 4.;
  61. }
  62. this->polyline.make_arc(with_fitting_arc, tolerance, fitting_arc_tolerance);
  63. }
  64. void ExtrusionPath3D::simplify(coordf_t tolerance, ArcFittingType with_fitting_arc, double fitting_arc_tolerance)
  65. {
  66. this->polyline.make_arc(ArcFittingType::Disabled, tolerance, fitting_arc_tolerance);
  67. // TODO: simplify but only for sub-path with same zheight.
  68. // if (with_fitting_arc) {
  69. // this->polyline.simplify(tolerance, with_fitting_arc, fitting_arc_tolerance);
  70. //}
  71. }
  72. coordf_t ExtrusionPath::length() const { return this->polyline.length(); }
  73. void ExtrusionPath::_inflate_collection(const Polylines &polylines, ExtrusionEntityCollection *collection) const
  74. {
  75. ExtrusionEntitiesPtr to_add;
  76. for (const Polyline &polyline : polylines)
  77. to_add.push_back(new ExtrusionPath(ArcPolyline{polyline}, this->attributes(), this->can_reverse()));
  78. collection->append(std::move(to_add));
  79. }
  80. void ExtrusionPath::polygons_covered_by_width(Polygons &out, const float scaled_epsilon) const
  81. {
  82. polygons_append(out, offset(this->polyline.to_polyline(), double(scale_(m_attributes.width / 2)) + scaled_epsilon));
  83. }
  84. void ExtrusionPath::polygons_covered_by_spacing(Polygons &out, const float spacing_ratio, const float scaled_epsilon) const
  85. {
  86. // Instantiating the Flow class to get the line spacing.
  87. // Don't know the nozzle diameter, setting to zero. It shall not matter it shall be optimized out by the compiler.
  88. bool bridge = this->role().is_bridge() || (this->width() * 4 < this->height());
  89. assert(!bridge || m_attributes.width == m_attributes.height);
  90. // TODO: check BRIDGE_FLOW here
  91. auto flow = bridge ? Flow::bridging_flow(m_attributes.width, 0.f) :
  92. Flow::new_from_width(m_attributes.width, 0.f, m_attributes.height, spacing_ratio);
  93. polygons_append(out, offset(this->polyline.to_polyline(), 0.5f * float(flow.scaled_spacing()) + scaled_epsilon, Slic3r::ClipperLib::jtMiter, 10));
  94. }
  95. //note: don't suppport arc
  96. double ExtrusionLoop::area() const
  97. {
  98. double a = 0;
  99. for (const ExtrusionPath &path : this->paths) {
  100. assert(path.size() >= 2);
  101. if (path.size() >= 2) {
  102. if (path.polyline.has_arc()) {
  103. Polyline poly = path.polyline.to_polyline();
  104. Point prev = poly.front();
  105. for (size_t idx = 1; idx < poly.size(); ++idx) {
  106. const Point &curr = poly[idx];
  107. a += cross2(prev.cast<double>(), curr.cast<double>());
  108. prev = curr;
  109. }
  110. } else {
  111. // Assumming that the last point of one path segment is repeated at the start of the following path segment.
  112. Point prev = path.polyline.front();
  113. for (size_t idx = 1; idx < path.polyline.size(); ++idx) {
  114. const Point &curr = path.polyline.get_point(idx);
  115. a += cross2(prev.cast<double>(), curr.cast<double>());
  116. prev = curr;
  117. }
  118. }
  119. }
  120. }
  121. return a * 0.5;
  122. }
  123. bool ExtrusionLoop::is_counter_clockwise() const {
  124. return this->area() > 0;
  125. }
  126. bool ExtrusionLoop::is_clockwise() const { return !is_counter_clockwise(); }
  127. void ExtrusionLoop::reverse()
  128. {
  129. for (ExtrusionPath &path : this->paths)
  130. path.reverse();
  131. std::reverse(this->paths.begin(), this->paths.end());
  132. }
  133. Polygon ExtrusionLoop::polygon() const
  134. {
  135. Polygon polygon;
  136. for (const ExtrusionPath &path : this->paths) {
  137. // for each polyline, append all points except the last one (because it coincides with the first one of the next polyline)
  138. Polyline poly = path.polyline.to_polyline();
  139. polygon.points.insert(polygon.points.end(), poly.begin(), poly.end() - 1);
  140. }
  141. return polygon;
  142. }
  143. ArcPolyline ExtrusionLoop::as_polyline() const
  144. {
  145. ArcPolyline polyline;
  146. for (const ExtrusionPath &path : this->paths) {
  147. polyline.append(path.as_polyline());
  148. }
  149. return polyline;
  150. }
  151. double ExtrusionLoop::length() const
  152. {
  153. double len = 0;
  154. for (const ExtrusionPath &path : this->paths)
  155. len += path.polyline.length();
  156. return len;
  157. }
  158. ExtrusionRole ExtrusionLoop::role() const
  159. {
  160. if (this->paths.empty())
  161. return ExtrusionRole::None;
  162. ExtrusionRole role = this->paths.front().role();
  163. for (const ExtrusionPath &path : this->paths)
  164. if (role != path.role()) {
  165. return ExtrusionRole::Mixed;
  166. }
  167. return role;
  168. }
  169. bool ExtrusionLoop::split_at_vertex(const Point &point, const double scaled_epsilon)
  170. {
  171. for (ExtrusionPaths::iterator path = this->paths.begin(); path != this->paths.end(); ++path) {
  172. if (int idx = path->polyline.find_point(point, scaled_epsilon); idx != -1) {
  173. if (this->paths.size() == 1) {
  174. // just change the order of points
  175. ArcPolyline p1, p2;
  176. path->polyline.split_at_index(idx, p1, p2);
  177. if (p1.is_valid() && p2.is_valid()) {
  178. p2.append(std::move(p1));
  179. path->polyline.swap(p2); // swap points & fitting result
  180. }
  181. } else if (idx > 0) {
  182. if (idx < path->size() - 1) {
  183. // new paths list starts with the second half of current path
  184. ExtrusionPaths new_paths;
  185. ArcPolyline p1, p2;
  186. path->polyline.split_at_index(idx, p1, p2);
  187. new_paths.reserve(this->paths.size() + 1);
  188. {
  189. ExtrusionPath p = *path;
  190. p.polyline.swap(p2);
  191. if (p.polyline.is_valid())
  192. new_paths.push_back(p);
  193. }
  194. // then we add all paths until the end of current path list
  195. new_paths.insert(new_paths.end(), path + 1, this->paths.end()); // not including this path
  196. // then we add all paths since the beginning of current list up to the previous one
  197. new_paths.insert(new_paths.end(), this->paths.begin(), path); // not including this path
  198. // finally we add the first half of current path
  199. {
  200. ExtrusionPath p = *path;
  201. p.polyline.swap(p1);
  202. if (p.polyline.is_valid())
  203. new_paths.push_back(p);
  204. }
  205. // we can now override the old path list with the new one and stop looping
  206. this->paths = std::move(new_paths);
  207. } else {
  208. // last point
  209. assert((path)->last_point().coincides_with_epsilon(point));
  210. assert((path + 1)->first_point().coincides_with_epsilon(point));
  211. ExtrusionPaths new_paths;
  212. new_paths.reserve(this->paths.size());
  213. // then we add all paths until the end of current path list
  214. new_paths.insert(new_paths.end(), path + 1, this->paths.end()); // not including this path
  215. // then we add all paths since the beginning of current list up to the previous one
  216. new_paths.insert(new_paths.end(), this->paths.begin(), path + 1); // including this path
  217. // we can now override the old path list with the new one and stop looping
  218. this->paths = std::move(new_paths);
  219. }
  220. } else {
  221. // else first point ->
  222. // if first path - nothign to change.
  223. // else, then impossible as it's also the last point of the previous path.
  224. assert(path == this->paths.begin());
  225. assert(path->first_point().distance_to(point) <= scaled_epsilon);
  226. }
  227. assert(this->first_point().distance_to(point) <= scaled_epsilon);
  228. return true;
  229. }
  230. }
  231. // The point was not found.
  232. return false;
  233. }
  234. ExtrusionLoop::ClosestPathPoint ExtrusionLoop::get_closest_path_and_point(const Point &point, bool prefer_non_overhang) const
  235. {
  236. // Find the closest path and closest point belonging to that path. Avoid overhangs, if asked for.
  237. ClosestPathPoint out{0, 0};
  238. double min2 = std::numeric_limits<double>::max();
  239. ClosestPathPoint best_non_overhang{0, 0};
  240. double min2_non_overhang = std::numeric_limits<double>::max();
  241. for (const ExtrusionPath &path : this->paths) {
  242. std::pair<int, Point> foot_pt_ = path.polyline.foot_pt(point);
  243. double d2 = (foot_pt_.second - point).cast<double>().squaredNorm();
  244. if (d2 < min2) {
  245. out.foot_pt = foot_pt_.second;
  246. out.path_idx = &path - &this->paths.front();
  247. out.segment_idx = foot_pt_.first;
  248. min2 = d2;
  249. }
  250. if (prefer_non_overhang && !path.role().is_bridge() && d2 < min2_non_overhang) {
  251. best_non_overhang.foot_pt = foot_pt_.second;
  252. best_non_overhang.path_idx = &path - &this->paths.front();
  253. best_non_overhang.segment_idx = foot_pt_.first;
  254. min2_non_overhang = d2;
  255. }
  256. }
  257. if (prefer_non_overhang && min2_non_overhang != std::numeric_limits<double>::max()) {
  258. // Only apply the non-overhang point if there is one.
  259. out = best_non_overhang;
  260. }
  261. return out;
  262. }
  263. // Splitting an extrusion loop, possibly made of multiple segments, some of the segments may be bridging.
  264. void ExtrusionLoop::split_at(const Point &point, bool prefer_non_overhang, const double scaled_epsilon)
  265. {
  266. if (this->paths.empty())
  267. return;
  268. ExtrusionLoop::ClosestPathPoint close_p = get_closest_path_and_point(point, prefer_non_overhang);
  269. // Snap p to start or end of segment_idx if closer than scaled_epsilon.
  270. //{
  271. const Point pt1 = this->paths[close_p.path_idx].polyline.get_point(close_p.segment_idx);
  272. const Point pt2 = this->paths[close_p.path_idx].polyline.get_point(close_p.segment_idx + 1);
  273. // Use close_p.foot_pt instead of point for the comparison, as it's the one that will be used.
  274. double d2_1 = (close_p.foot_pt - pt1).cast<double>().squaredNorm();
  275. double d2_2 = (close_p.foot_pt - pt2).cast<double>().squaredNorm();
  276. const double thr2 = scaled_epsilon * scaled_epsilon;
  277. if (d2_1 < d2_2) {
  278. if (d2_1 < thr2)
  279. close_p.foot_pt = pt1;
  280. } else {
  281. if (d2_2 < thr2)
  282. close_p.foot_pt = pt2;
  283. }
  284. //}
  285. // now split path_idx in two parts
  286. const ExtrusionPath &path = this->paths[close_p.path_idx];
  287. assert(path.polyline.is_valid());
  288. ExtrusionPath p1(path.attributes(), can_reverse());
  289. ExtrusionPath p2(path.attributes(), can_reverse());
  290. path.polyline.split_at(close_p.foot_pt, p1.polyline, p2.polyline);
  291. if (this->paths.size() == 1) {
  292. if (p1.polyline.size() < 2) {
  293. this->paths.front().polyline = std::move(p2.polyline);
  294. } else if (p2.polyline.size() < 2) {
  295. this->paths.front().polyline = std::move(p1.polyline);
  296. } else {
  297. p2.polyline.append(std::move(p1.polyline));
  298. this->paths.front().polyline = std::move(p2.polyline);
  299. }
  300. } else {
  301. // install the begining of the new paths
  302. if (p2.polyline.size() >= 2) {
  303. this->paths[close_p.path_idx].polyline = std::move(p2.polyline);
  304. } else {
  305. this->paths.erase(this->paths.begin() + close_p.path_idx);
  306. }
  307. //rotate
  308. if (close_p.path_idx > 0) {
  309. std::rotate(this->paths.begin(), this->paths.begin() + close_p.path_idx, this->paths.end());
  310. }
  311. // install the end
  312. if (p1.polyline.size() >= 2) {
  313. this->paths.push_back(std::move(p1));
  314. }
  315. }
  316. // check if it's doing its job.
  317. #ifdef _DEBUG
  318. Point last_pt = this->last_point();
  319. for (const ExtrusionPath &path : paths) {
  320. assert(last_pt == path.first_point());
  321. for (int i = 1; i < path.polyline.size(); ++i)
  322. assert(!path.polyline.get_point(i - 1).coincides_with_epsilon(path.polyline.get_point(i)));
  323. last_pt = path.last_point();
  324. }
  325. assert(close_p.foot_pt.coincides_with_epsilon(this->first_point()));
  326. //assert(point.distance_to(this->first_point()) <= scaled_epsilon); // can be false, still ok?
  327. #endif
  328. }
  329. ExtrusionPaths clip_end(ExtrusionPaths &paths, coordf_t distance)
  330. {
  331. ExtrusionPaths removed;
  332. while (distance > 0 && !paths.empty()) {
  333. ExtrusionPath &last = paths.back();
  334. removed.push_back(last);
  335. coordf_t len = last.length();
  336. if (len <= distance) {
  337. paths.pop_back();
  338. distance -= len;
  339. } else {
  340. last.polyline.clip_end(distance);
  341. removed.back().polyline.clip_start(removed.back().polyline.length() - distance);
  342. break;
  343. }
  344. }
  345. for(auto& path : paths)
  346. DEBUG_VISIT(path, LoopAssertVisitor())
  347. std::reverse(removed.begin(), removed.end());
  348. return removed;
  349. }
  350. //bool ExtrusionLoop::has_overhang_point(const Point &point) const
  351. //{
  352. // for (const ExtrusionPath &path : this->paths) {
  353. // int pos = path.polyline.find_point(point);
  354. // if (pos != -1) {
  355. // // point belongs to this path
  356. // // we consider it overhang only if it's not an endpoint
  357. // return (path.role().is_bridge() && pos > 0 && pos != int(path.polyline.size()) - 1);
  358. // }
  359. // }
  360. // return false;
  361. //}
  362. void ExtrusionLoop::polygons_covered_by_width(Polygons &out, const float scaled_epsilon) const
  363. {
  364. for (const ExtrusionPath &path : this->paths)
  365. path.polygons_covered_by_width(out, scaled_epsilon);
  366. }
  367. void ExtrusionLoop::polygons_covered_by_spacing(Polygons &out, const float spacing_ratio, const float scaled_epsilon) const
  368. {
  369. for (const ExtrusionPath &path : this->paths)
  370. path.polygons_covered_by_spacing(out, spacing_ratio, scaled_epsilon);
  371. }
  372. //TODO del
  373. //double ExtrusionLoop::min_mm3_per_mm() const
  374. //{
  375. // double min_mm3_per_mm = std::numeric_limits<double>::max();
  376. // for (const ExtrusionPath &path : this->paths)
  377. // min_mm3_per_mm = std::min(min_mm3_per_mm, path.min_mm3_per_mm());
  378. // return min_mm3_per_mm;
  379. //}
  380. void ExtrusionPrinter::use(const ExtrusionPath &path)
  381. {
  382. ss << (json?"\"":"") << "ExtrusionPath" << (path.can_reverse()?"":"Oriented") << (json?"_":":") << role_to_code(path.role()) << (json?"\":":"") << "[";
  383. for (int i = 0; i < path.polyline.size(); i++) {
  384. if (i != 0)
  385. ss << ",";
  386. double x = (mult * (path.polyline.get_point(i).x()));
  387. double y = (mult * (path.polyline.get_point(i).y()));
  388. ss << std::fixed << "["<<(trunc>0?(int(x*trunc))/double(trunc):x) << "," << (trunc>0?(int(y*trunc))/double(trunc):y) <<"]";
  389. }
  390. ss << "]";
  391. }
  392. void ExtrusionPrinter::use(const ExtrusionPath3D &path3D)
  393. {
  394. ss << (json?"\"":"") << "ExtrusionPath3D" << (path3D.can_reverse()?"":"Oriented") << (json?"_":":") << role_to_code(path3D.role()) << (json?"\":":"") << "[";
  395. for (int i = 0; i < path3D.polyline.size(); i++) {
  396. if (i != 0)
  397. ss << ",";
  398. double x = (mult * (path3D.polyline.get_point(i).x()));
  399. double y = (mult * (path3D.polyline.get_point(i).y()));
  400. double z = (path3D.z_offsets.size() > i ? mult * (path3D.z_offsets[i]) : -1);
  401. ss << std::fixed << "[" << (trunc>0?(int(x*trunc))/double(trunc):x) << "," << (trunc>0?(int(y*trunc))/double(trunc):y) << "," << (trunc>0?(int(z*trunc))/double(trunc):z) << "]";
  402. }
  403. ss << "]";
  404. }
  405. void ExtrusionPrinter::use(const ExtrusionMultiPath &multipath)
  406. {
  407. ss << (json?"\"":"") << "ExtrusionMultiPath" << (multipath.can_reverse()?"":"Oriented") << (json?"_":":") << role_to_code(multipath.role()) << (json?"\":":"") << "{";
  408. for (int i = 0; i < multipath.paths.size(); i++) {
  409. if (i != 0)
  410. ss << ",";
  411. multipath.paths[i].visit(*this);
  412. }
  413. ss << "}";
  414. }
  415. void ExtrusionPrinter::use(const ExtrusionMultiPath3D &multipath3D)
  416. {
  417. ss << (json?"\"":"") << "multipath3D" << (multipath3D.can_reverse()?"":"Oriented") << (json?"_":":") << role_to_code(multipath3D.role()) << (json?"\":":"") << "{";
  418. for (int i = 0; i < multipath3D.paths.size(); i++) {
  419. if (i != 0)
  420. ss << ",";
  421. multipath3D.paths[i].visit(*this);
  422. }
  423. ss << "}";
  424. }
  425. void ExtrusionPrinter::use(const ExtrusionLoop &loop)
  426. {
  427. ss << (json?"\"":"") << "ExtrusionLoop" << (json?"_":":") << role_to_code(loop.role())<<"_" << looprole_to_code(loop.loop_role()) << (json?"\":":"") << "{";
  428. if(!loop.can_reverse()) ss << (json?"\"":"") << "oriented" << (json?"\":":"=") << "true,";
  429. for (int i = 0; i < loop.paths.size(); i++) {
  430. if (i != 0)
  431. ss << ",";
  432. loop.paths[i].visit(*this);
  433. }
  434. ss << "}";
  435. }
  436. void ExtrusionPrinter::use(const ExtrusionEntityCollection &collection)
  437. {
  438. ss << (json?"\"":"") << "ExtrusionEntityCollection" << (json?"_":":") << role_to_code(collection.role()) << (json?"\":":"") << "{";
  439. if(!collection.can_sort()) ss << (json?"\"":"") << "no_sort" << (json?"\":":"=") << "true,";
  440. if(!collection.can_reverse()) ss << (json?"\"":"") << "oriented" << (json?"\":":"=") << "true,";
  441. for (int i = 0; i < collection.entities().size(); i++) {
  442. if (i != 0)
  443. ss << ",";
  444. collection.entities()[i]->visit(*this);
  445. }
  446. ss << "}";
  447. }
  448. void ExtrusionLength::default_use(const ExtrusionEntity &entity) { dist += entity.length(); };
  449. void ExtrusionLength::use(const ExtrusionEntityCollection &collection)
  450. {
  451. for (int i = 0; i < collection.entities().size(); i++) {
  452. collection.entities()[i]->visit(*this);
  453. }
  454. }
  455. double ExtrusionVolume::get(const ExtrusionEntityCollection &coll) {
  456. for (const ExtrusionEntity *entity : coll.entities()) entity->visit(*this);
  457. return volume;
  458. }
  459. void ExtrusionModifyFlow::set(ExtrusionEntityCollection &coll) {
  460. for (ExtrusionEntity *entity : coll.entities()) entity->visit(*this);
  461. }
  462. void ExtrusionVisitorRecursiveConst::use(const ExtrusionMultiPath& multipath) {
  463. for (const ExtrusionPath &path : multipath.paths) {
  464. path.visit(*this);
  465. }
  466. }
  467. void ExtrusionVisitorRecursiveConst::use(const ExtrusionMultiPath3D &multipath3D)
  468. {
  469. for (const ExtrusionPath3D &path3D : multipath3D.paths) {
  470. path3D.visit(*this);
  471. }
  472. }
  473. void ExtrusionVisitorRecursiveConst::use(const ExtrusionLoop &loop)
  474. {
  475. for (const ExtrusionPath &path : loop.paths) {
  476. path.visit(*this);
  477. }
  478. }
  479. void ExtrusionVisitorRecursiveConst::use(const ExtrusionEntityCollection &collection)
  480. {
  481. for (const ExtrusionEntity *entity : collection.entities()) {
  482. entity->visit(*this);
  483. }
  484. }
  485. void ExtrusionVisitorRecursive::use(ExtrusionMultiPath &multipath)
  486. {
  487. for (ExtrusionPath &path : multipath.paths) {
  488. path.visit(*this);
  489. }
  490. }
  491. void ExtrusionVisitorRecursive::use(ExtrusionMultiPath3D &multipath3D)
  492. {
  493. for (ExtrusionPath3D &path3D : multipath3D.paths) {
  494. path3D.visit(*this);
  495. }
  496. }
  497. void ExtrusionVisitorRecursive::use(ExtrusionLoop &loop)
  498. {
  499. for (ExtrusionPath &path : loop.paths) {
  500. path.visit(*this);
  501. }
  502. }
  503. void ExtrusionVisitorRecursive::use(ExtrusionEntityCollection &collection)
  504. {
  505. for (ExtrusionEntity *entity : collection.entities()) {
  506. entity->visit(*this);
  507. }
  508. }
  509. void HasRoleVisitor::use(const ExtrusionMultiPath& multipath) {
  510. for (const ExtrusionPath& path : multipath.paths) {
  511. path.visit(*this);
  512. if(found) return;
  513. }
  514. }
  515. void HasRoleVisitor::use(const ExtrusionMultiPath3D& multipath3D) {
  516. for (const ExtrusionPath3D& path3D : multipath3D.paths) {
  517. path3D.visit(*this);
  518. if(found) return;
  519. }
  520. }
  521. void HasRoleVisitor::use(const ExtrusionLoop& loop) {
  522. for (const ExtrusionPath& path : loop.paths) {
  523. path.visit(*this);
  524. if(found) return;
  525. }
  526. }
  527. void HasRoleVisitor::use(const ExtrusionEntityCollection& collection) {
  528. for (const ExtrusionEntity* entity : collection.entities()) {
  529. entity->visit(*this);
  530. if(found) return;
  531. }
  532. }
  533. bool HasRoleVisitor::search(const ExtrusionEntity &entity, HasRoleVisitor&& visitor) {
  534. entity.visit(visitor);
  535. return visitor.found;
  536. }
  537. bool HasRoleVisitor::search(const ExtrusionEntitiesPtr &entities, HasRoleVisitor&& visitor) {
  538. for (ExtrusionEntity *ptr : entities) {
  539. ptr->visit(visitor);
  540. if (visitor.found) return true;
  541. }
  542. return visitor.found;
  543. }
  544. void SimplifyVisitor::use(ExtrusionPath& path) {
  545. if (m_min_path_size > 0 && path.length() < m_min_path_size) {
  546. m_last_deleted = true;
  547. return;
  548. }
  549. assert(m_scaled_resolution >= SCALED_EPSILON);
  550. path.simplify(m_scaled_resolution, m_use_arc_fitting, scale_d(m_arc_fitting_tolearance->get_abs_value(path.width())));
  551. for (int i = 1; i < path.polyline.size(); ++i)
  552. if (path.polyline.get_point(i - 1).coincides_with_epsilon(path.polyline.get_point(i))) {
  553. path.simplify(m_scaled_resolution, m_use_arc_fitting, scale_d(m_arc_fitting_tolearance->get_abs_value(path.width())));
  554. }
  555. for (int i = 1; i < path.polyline.size(); ++i)
  556. assert(!path.polyline.get_point(i - 1).coincides_with_epsilon(path.polyline.get_point(i)));
  557. }
  558. void SimplifyVisitor::use(ExtrusionPath3D& path3D) {
  559. if (m_min_path_size > 0 && path3D.length() < m_min_path_size) {
  560. m_last_deleted = true;
  561. return;
  562. }
  563. path3D.simplify(m_scaled_resolution, m_use_arc_fitting, scale_d(m_arc_fitting_tolearance->get_abs_value(path3D.width())));
  564. }
  565. void SimplifyVisitor::use(ExtrusionMultiPath &multipath)
  566. {
  567. for (size_t i = 0;i<multipath.paths.size() ;++i) {
  568. ExtrusionPath *path = &multipath.paths[i];
  569. //if (min_path_size > 0 && path.length() < min_path_size) {
  570. path->visit(*this);
  571. while (m_last_deleted) {
  572. ExtrusionPath *path_merged = nullptr;
  573. if (i > 0) {
  574. ExtrusionPath &path_previous = multipath.paths[i - 1];
  575. path_previous.polyline.append(path->polyline);
  576. // erase us, move to previous
  577. multipath.paths.erase(multipath.paths.begin() + i);
  578. --i;
  579. } else if (i + 1 < multipath.size()) {
  580. ExtrusionPath &path_next = multipath.paths[i];
  581. path->polyline.append(path_next.polyline);
  582. // erase next
  583. multipath.paths.erase(multipath.paths.begin() + i + 1);
  584. } else {
  585. //return, the caller need to delete me.
  586. return;
  587. }
  588. m_last_deleted = false;
  589. // refresh pointer, as multipath.paths was modified
  590. path = &multipath.paths[i];
  591. //visit again to remove small segments
  592. path->visit(*this);
  593. }
  594. }
  595. }
  596. void SimplifyVisitor::use(ExtrusionMultiPath3D &multipath3D)
  597. {
  598. for (size_t i = 0;i<multipath3D.paths.size() ;++i) {
  599. ExtrusionPath3D *path = &multipath3D.paths[i];
  600. //if (min_path_size > 0 && path.length() < min_path_size) {
  601. path->visit(*this);
  602. while (m_last_deleted) {
  603. ExtrusionPath *path_merged = nullptr;
  604. if (i > 0) {
  605. ExtrusionPath &path_previous = multipath3D.paths[i - 1];
  606. path_previous.polyline.append(path->polyline);
  607. // erase us, move to previous
  608. multipath3D.paths.erase(multipath3D.paths.begin() + i);
  609. --i;
  610. } else if (i + 1 < multipath3D.size()) {
  611. ExtrusionPath &path_next = multipath3D.paths[i];
  612. path->polyline.append(path_next.polyline);
  613. // erase next
  614. multipath3D.paths.erase(multipath3D.paths.begin() + i + 1);
  615. } else {
  616. //return, the caller need to delete me.
  617. return;
  618. }
  619. m_last_deleted = false;
  620. // refresh pointer, as multipath.paths was modified
  621. path = &multipath3D.paths[i];
  622. //visit again to remove small segments
  623. path->visit(*this);
  624. }
  625. }
  626. }
  627. void SimplifyVisitor::use(ExtrusionLoop &loop)
  628. {
  629. for (size_t i = 0;i<loop.paths.size() ;++i) {
  630. ExtrusionPath *path = &loop.paths[i];
  631. //if (min_path_size > 0 && path.length() < min_path_size) {
  632. path->visit(*this);
  633. while (m_last_deleted) {
  634. ExtrusionPath *path_merged = nullptr;
  635. if (i > 0) {
  636. ExtrusionPath &path_previous = loop.paths[i - 1];
  637. path_previous.polyline.append(path->polyline);
  638. // erase us, move to previous
  639. loop.paths.erase(loop.paths.begin() + i);
  640. --i;
  641. } else if (i + 1 < loop.paths.size()) {
  642. ExtrusionPath &path_next = loop.paths[i];
  643. path->polyline.append(path_next.polyline);
  644. // erase next
  645. loop.paths.erase(loop.paths.begin() + i + 1);
  646. } else {
  647. //return, the caller need to delete me.
  648. return;
  649. }
  650. m_last_deleted = false;
  651. // refresh pointer, as multipath.paths was modified
  652. path = &loop.paths[i];
  653. //visit again to remove small segments
  654. path->visit(*this);
  655. }
  656. }
  657. }
  658. void SimplifyVisitor::use(ExtrusionEntityCollection &collection)
  659. {
  660. for (size_t i = 0; i < collection.size(); ++i) {
  661. ExtrusionEntity *entity = collection.entities()[i];
  662. // if (min_path_size > 0 && path.length() < min_path_size) {
  663. entity->visit(*this);
  664. if (m_last_deleted) {
  665. // erase it, without any merge.
  666. collection.remove(i);
  667. --i;
  668. }
  669. }
  670. }
  671. //class ExtrusionTreeVisitor : ExtrusionVisitor {
  672. //public:
  673. // //virtual void use(ExtrusionEntity &entity) { assert(false); };
  674. // virtual void use(ExtrusionPath &path) override { const ExtrusionPath &constpath = path; use(constpath); };
  675. // virtual void use(ExtrusionPath3D &path3D) override { const ExtrusionPath3D &constpath3D = path3D; use(constpath3D); };
  676. // virtual void use(ExtrusionMultiPath &multipath) override { const ExtrusionMultiPath &constmultipath = multipath; use(constmultipath);
  677. // }; virtual void use(ExtrusionMultiPath3D &multipath3D) override { const ExtrusionMultiPath3D &constmultipath3D = multipath3D;
  678. // use(constmultipath3D); }; virtual void use(ExtrusionLoop &loop) override { const ExtrusionLoop &constloop = loop; use(constloop); };
  679. // virtual void use(ExtrusionEntityCollection &collection) { const ExtrusionEntityCollection &constcollection = collection;
  680. // use(constcollection); }; virtual void use(const ExtrusionPath &path) override { assert(false); }; virtual void use(const
  681. // ExtrusionPath3D &path3D) override { assert(false); }; virtual void use(const ExtrusionMultiPath &multipath) override { assert(false);
  682. // }; virtual void use(const ExtrusionMultiPath3D &multipath3D) { assert(false); }; virtual void use(const ExtrusionLoop &loop) override
  683. // { assert(false); }; virtual void use(const ExtrusionEntityCollection &collection) { assert(false); }; virtual void
  684. // use_default(ExtrusionEntity &entity) { const ExtrusionEntity &constentity = entity; use_default(constentity); }; virtual void
  685. // use_default(const ExtrusionEntity &entity) {};
  686. //
  687. //};
  688. } // namespace Slic3r