PrintObject.cpp 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. #include "Print.hpp"
  2. #include "BoundingBox.hpp"
  3. #include "ClipperUtils.hpp"
  4. #include "Geometry.hpp"
  5. #include "SupportMaterial.hpp"
  6. #include "Surface.hpp"
  7. #include <utility>
  8. #include <boost/log/trivial.hpp>
  9. #include <float.h>
  10. #include <tbb/task_scheduler_init.h>
  11. #include <tbb/parallel_for.h>
  12. #include <tbb/atomic.h>
  13. #include <Shiny/Shiny.h>
  14. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  15. #define SLIC3R_DEBUG
  16. #endif
  17. // #define SLIC3R_DEBUG
  18. // Make assert active if SLIC3R_DEBUG
  19. #ifdef SLIC3R_DEBUG
  20. #undef NDEBUG
  21. #define DEBUG
  22. #define _DEBUG
  23. #include "SVG.hpp"
  24. #undef assert
  25. #include <cassert>
  26. #endif
  27. namespace Slic3r {
  28. PrintObject::PrintObject(Print* print, ModelObject* model_object, const BoundingBoxf3 &modobj_bbox) :
  29. typed_slices(false),
  30. _print(print),
  31. _model_object(model_object),
  32. layer_height_profile_valid(false)
  33. {
  34. // Compute the translation to be applied to our meshes so that we work with smaller coordinates
  35. {
  36. // Translate meshes so that our toolpath generation algorithms work with smaller
  37. // XY coordinates; this translation is an optimization and not strictly required.
  38. // A cloned mesh will be aligned to 0 before slicing in _slice_region() since we
  39. // don't assume it's already aligned and we don't alter the original position in model.
  40. // We store the XY translation so that we can place copies correctly in the output G-code
  41. // (copies are expressed in G-code coordinates and this translation is not publicly exposed).
  42. this->_copies_shift = Point::new_scale(modobj_bbox.min.x, modobj_bbox.min.y);
  43. // Scale the object size and store it
  44. Pointf3 size = modobj_bbox.size();
  45. this->size = Point3::new_scale(size.x, size.y, size.z);
  46. }
  47. this->reload_model_instances();
  48. this->layer_height_ranges = model_object->layer_height_ranges;
  49. this->layer_height_profile = model_object->layer_height_profile;
  50. }
  51. bool PrintObject::add_copy(const Pointf &point)
  52. {
  53. Points points = this->_copies;
  54. points.push_back(Point::new_scale(point.x, point.y));
  55. return this->set_copies(points);
  56. }
  57. bool PrintObject::delete_last_copy()
  58. {
  59. Points points = this->_copies;
  60. points.pop_back();
  61. return this->set_copies(points);
  62. }
  63. bool PrintObject::set_copies(const Points &points)
  64. {
  65. this->_copies = points;
  66. // order copies with a nearest neighbor search and translate them by _copies_shift
  67. this->_shifted_copies.clear();
  68. this->_shifted_copies.reserve(points.size());
  69. // order copies with a nearest-neighbor search
  70. std::vector<Points::size_type> ordered_copies;
  71. Slic3r::Geometry::chained_path(points, ordered_copies);
  72. for (size_t point_idx : ordered_copies) {
  73. Point copy = points[point_idx];
  74. copy.translate(this->_copies_shift);
  75. this->_shifted_copies.push_back(copy);
  76. }
  77. bool invalidated = this->_print->invalidate_step(psSkirt);
  78. invalidated |= this->_print->invalidate_step(psBrim);
  79. return invalidated;
  80. }
  81. bool PrintObject::reload_model_instances()
  82. {
  83. Points copies;
  84. copies.reserve(this->_model_object->instances.size());
  85. for (const ModelInstance *mi : this->_model_object->instances)
  86. copies.emplace_back(Point::new_scale(mi->offset.x, mi->offset.y));
  87. return this->set_copies(copies);
  88. }
  89. void PrintObject::clear_layers()
  90. {
  91. for (Layer *l : this->layers)
  92. delete l;
  93. this->layers.clear();
  94. }
  95. Layer* PrintObject::add_layer(int id, coordf_t height, coordf_t print_z, coordf_t slice_z)
  96. {
  97. layers.push_back(new Layer(id, this, height, print_z, slice_z));
  98. return layers.back();
  99. }
  100. void PrintObject::clear_support_layers()
  101. {
  102. for (Layer *l : this->support_layers)
  103. delete l;
  104. this->support_layers.clear();
  105. }
  106. SupportLayer* PrintObject::add_support_layer(int id, coordf_t height, coordf_t print_z)
  107. {
  108. support_layers.emplace_back(new SupportLayer(id, this, height, print_z, -1));
  109. return support_layers.back();
  110. }
  111. // Called by Print::apply_config().
  112. // This method only accepts PrintObjectConfig and PrintRegionConfig option keys.
  113. bool PrintObject::invalidate_state_by_config_options(const std::vector<t_config_option_key> &opt_keys)
  114. {
  115. if (opt_keys.empty())
  116. return false;
  117. std::vector<PrintObjectStep> steps;
  118. bool invalidated = false;
  119. for (const t_config_option_key &opt_key : opt_keys) {
  120. if ( opt_key == "perimeters"
  121. || opt_key == "extra_perimeters"
  122. || opt_key == "gap_fill_speed"
  123. || opt_key == "overhangs"
  124. || opt_key == "first_layer_extrusion_width"
  125. || opt_key == "perimeter_extrusion_width"
  126. || opt_key == "infill_overlap"
  127. || opt_key == "thin_walls"
  128. || opt_key == "external_perimeters_first") {
  129. steps.emplace_back(posPerimeters);
  130. } else if (
  131. opt_key == "layer_height"
  132. || opt_key == "first_layer_height"
  133. || opt_key == "raft_layers") {
  134. steps.emplace_back(posSlice);
  135. this->reset_layer_height_profile();
  136. }
  137. else if (
  138. opt_key == "clip_multipart_objects"
  139. || opt_key == "elefant_foot_compensation"
  140. || opt_key == "support_material_contact_distance"
  141. || opt_key == "xy_size_compensation") {
  142. steps.emplace_back(posSlice);
  143. } else if (
  144. opt_key == "support_material"
  145. || opt_key == "support_material_angle"
  146. || opt_key == "support_material_buildplate_only"
  147. || opt_key == "support_material_enforce_layers"
  148. || opt_key == "support_material_extruder"
  149. || opt_key == "support_material_extrusion_width"
  150. || opt_key == "support_material_interface_layers"
  151. || opt_key == "support_material_interface_contact_loops"
  152. || opt_key == "support_material_interface_extruder"
  153. || opt_key == "support_material_interface_spacing"
  154. || opt_key == "support_material_pattern"
  155. || opt_key == "support_material_xy_spacing"
  156. || opt_key == "support_material_spacing"
  157. || opt_key == "support_material_synchronize_layers"
  158. || opt_key == "support_material_threshold"
  159. || opt_key == "support_material_with_sheath"
  160. || opt_key == "dont_support_bridges"
  161. || opt_key == "first_layer_extrusion_width") {
  162. steps.emplace_back(posSupportMaterial);
  163. } else if (
  164. opt_key == "interface_shells"
  165. || opt_key == "infill_only_where_needed"
  166. || opt_key == "infill_every_layers"
  167. || opt_key == "solid_infill_every_layers"
  168. || opt_key == "bottom_solid_layers"
  169. || opt_key == "top_solid_layers"
  170. || opt_key == "solid_infill_below_area"
  171. || opt_key == "infill_extruder"
  172. || opt_key == "solid_infill_extruder"
  173. || opt_key == "infill_extrusion_width"
  174. || opt_key == "ensure_vertical_shell_thickness"
  175. || opt_key == "bridge_angle") {
  176. steps.emplace_back(posPrepareInfill);
  177. } else if (
  178. opt_key == "external_fill_pattern"
  179. || opt_key == "external_fill_link_max_length"
  180. || opt_key == "fill_angle"
  181. || opt_key == "fill_pattern"
  182. || opt_key == "fill_link_max_length"
  183. || opt_key == "top_infill_extrusion_width"
  184. || opt_key == "first_layer_extrusion_width") {
  185. steps.emplace_back(posInfill);
  186. } else if (
  187. opt_key == "fill_density"
  188. || opt_key == "solid_infill_extrusion_width") {
  189. steps.emplace_back(posPerimeters);
  190. steps.emplace_back(posPrepareInfill);
  191. } else if (
  192. opt_key == "external_perimeter_extrusion_width"
  193. || opt_key == "perimeter_extruder") {
  194. steps.emplace_back(posPerimeters);
  195. steps.emplace_back(posSupportMaterial);
  196. } else if (opt_key == "bridge_flow_ratio") {
  197. steps.emplace_back(posPerimeters);
  198. steps.emplace_back(posInfill);
  199. } else if (
  200. opt_key == "seam_position"
  201. || opt_key == "seam_preferred_direction"
  202. || opt_key == "seam_preferred_direction_jitter"
  203. || opt_key == "support_material_speed"
  204. || opt_key == "support_material_interface_speed"
  205. || opt_key == "bridge_speed"
  206. || opt_key == "external_perimeter_speed"
  207. || opt_key == "infill_speed"
  208. || opt_key == "perimeter_speed"
  209. || opt_key == "small_perimeter_speed"
  210. || opt_key == "solid_infill_speed"
  211. || opt_key == "top_solid_infill_speed") {
  212. // these options only affect G-code export, so nothing to invalidate
  213. } else {
  214. // for legacy, if we can't handle this option let's invalidate all steps
  215. this->reset_layer_height_profile();
  216. this->invalidate_all_steps();
  217. invalidated = true;
  218. }
  219. }
  220. sort_remove_duplicates(steps);
  221. for (PrintObjectStep step : steps)
  222. invalidated |= this->invalidate_step(step);
  223. return invalidated;
  224. }
  225. bool PrintObject::invalidate_step(PrintObjectStep step)
  226. {
  227. bool invalidated = this->state.invalidate(step);
  228. // propagate to dependent steps
  229. if (step == posPerimeters) {
  230. invalidated |= this->invalidate_step(posPrepareInfill);
  231. invalidated |= this->_print->invalidate_step(psSkirt);
  232. invalidated |= this->_print->invalidate_step(psBrim);
  233. } else if (step == posPrepareInfill) {
  234. invalidated |= this->invalidate_step(posInfill);
  235. } else if (step == posInfill) {
  236. invalidated |= this->_print->invalidate_step(psSkirt);
  237. invalidated |= this->_print->invalidate_step(psBrim);
  238. } else if (step == posSlice) {
  239. invalidated |= this->invalidate_step(posPerimeters);
  240. invalidated |= this->invalidate_step(posSupportMaterial);
  241. invalidated |= this->_print->invalidate_step(psWipeTower);
  242. } else if (step == posSupportMaterial) {
  243. invalidated |= this->_print->invalidate_step(psSkirt);
  244. invalidated |= this->_print->invalidate_step(psBrim);
  245. }
  246. // Wipe tower depends on the ordering of extruders, which in turn depends on everything.
  247. invalidated |= this->_print->invalidate_step(psWipeTower);
  248. return invalidated;
  249. }
  250. bool PrintObject::has_support_material() const
  251. {
  252. return this->config.support_material
  253. || this->config.raft_layers > 0
  254. || this->config.support_material_enforce_layers > 0;
  255. }
  256. void PrintObject::_prepare_infill()
  257. {
  258. // This will assign a type (top/bottom/internal) to $layerm->slices.
  259. // Then the classifcation of $layerm->slices is transfered onto
  260. // the $layerm->fill_surfaces by clipping $layerm->fill_surfaces
  261. // by the cummulative area of the previous $layerm->fill_surfaces.
  262. this->detect_surfaces_type();
  263. // Decide what surfaces are to be filled.
  264. // Here the S_TYPE_TOP / S_TYPE_BOTTOMBRIDGE / S_TYPE_BOTTOM infill is turned to just S_TYPE_INTERNAL if zero top / bottom infill layers are configured.
  265. // Also tiny S_TYPE_INTERNAL surfaces are turned to S_TYPE_INTERNAL_SOLID.
  266. BOOST_LOG_TRIVIAL(info) << "Preparing fill surfaces...";
  267. for (auto *layer : this->layers)
  268. for (auto *region : layer->regions)
  269. region->prepare_fill_surfaces();
  270. // this will detect bridges and reverse bridges
  271. // and rearrange top/bottom/internal surfaces
  272. // It produces enlarged overlapping bridging areas.
  273. //
  274. // 1) S_TYPE_BOTTOMBRIDGE / S_TYPE_BOTTOM infill is grown by 3mm and clipped by the total infill area. Bridges are detected. The areas may overlap.
  275. // 2) S_TYPE_TOP is grown by 3mm and clipped by the grown bottom areas. The areas may overlap.
  276. // 3) Clip the internal surfaces by the grown top/bottom surfaces.
  277. // 4) Merge surfaces with the same style. This will mostly get rid of the overlaps.
  278. //FIXME This does not likely merge surfaces, which are supported by a material with different colors, but same properties.
  279. this->process_external_surfaces();
  280. // Add solid fills to ensure the shell vertical thickness.
  281. this->discover_vertical_shells();
  282. // Debugging output.
  283. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  284. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  285. for (const Layer *layer : this->layers) {
  286. LayerRegion *layerm = layer->regions[region_id];
  287. layerm->export_region_slices_to_svg_debug("6_discover_vertical_shells-final");
  288. layerm->export_region_fill_surfaces_to_svg_debug("6_discover_vertical_shells-final");
  289. } // for each layer
  290. } // for each region
  291. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  292. // Detect, which fill surfaces are near external layers.
  293. // They will be split in internal and internal-solid surfaces.
  294. // The purpose is to add a configurable number of solid layers to support the TOP surfaces
  295. // and to add a configurable number of solid layers above the BOTTOM / BOTTOMBRIDGE surfaces
  296. // to close these surfaces reliably.
  297. //FIXME Vojtech: Is this a good place to add supporting infills below sloping perimeters?
  298. this->discover_horizontal_shells();
  299. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  300. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  301. for (const Layer *layer : this->layers) {
  302. LayerRegion *layerm = layer->regions[region_id];
  303. layerm->export_region_slices_to_svg_debug("7_discover_horizontal_shells-final");
  304. layerm->export_region_fill_surfaces_to_svg_debug("7_discover_horizontal_shells-final");
  305. } // for each layer
  306. } // for each region
  307. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  308. // Only active if config->infill_only_where_needed. This step trims the sparse infill,
  309. // so it acts as an internal support. It maintains all other infill types intact.
  310. // Here the internal surfaces and perimeters have to be supported by the sparse infill.
  311. //FIXME The surfaces are supported by a sparse infill, but the sparse infill is only as large as the area to support.
  312. // Likely the sparse infill will not be anchored correctly, so it will not work as intended.
  313. // Also one wishes the perimeters to be supported by a full infill.
  314. this->clip_fill_surfaces();
  315. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  316. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  317. for (const Layer *layer : this->layers) {
  318. LayerRegion *layerm = layer->regions[region_id];
  319. layerm->export_region_slices_to_svg_debug("8_clip_surfaces-final");
  320. layerm->export_region_fill_surfaces_to_svg_debug("8_clip_surfaces-final");
  321. } // for each layer
  322. } // for each region
  323. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  324. // the following step needs to be done before combination because it may need
  325. // to remove only half of the combined infill
  326. this->bridge_over_infill();
  327. // combine fill surfaces to honor the "infill every N layers" option
  328. this->combine_infill();
  329. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  330. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  331. for (const Layer *layer : this->layers) {
  332. LayerRegion *layerm = layer->regions[region_id];
  333. layerm->export_region_slices_to_svg_debug("9_prepare_infill-final");
  334. layerm->export_region_fill_surfaces_to_svg_debug("9_prepare_infill-final");
  335. } // for each layer
  336. } // for each region
  337. for (const Layer *layer : this->layers) {
  338. layer->export_region_slices_to_svg_debug("9_prepare_infill-final");
  339. layer->export_region_fill_surfaces_to_svg_debug("9_prepare_infill-final");
  340. } // for each layer
  341. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  342. }
  343. // This function analyzes slices of a region (SurfaceCollection slices).
  344. // Each region slice (instance of Surface) is analyzed, whether it is supported or whether it is the top surface.
  345. // Initially all slices are of type stInternal.
  346. // Slices are compared against the top / bottom slices and regions and classified to the following groups:
  347. // stTop - Part of a region, which is not covered by any upper layer. This surface will be filled with a top solid infill.
  348. // stBottomBridge - Part of a region, which is not fully supported, but it hangs in the air, or it hangs losely on a support or a raft.
  349. // stBottom - Part of a region, which is not supported by the same region, but it is supported either by another region, or by a soluble interface layer.
  350. // stInternal - Part of a region, which is supported by the same region type.
  351. // If a part of a region is of stBottom and stTop, the stBottom wins.
  352. void PrintObject::detect_surfaces_type()
  353. {
  354. BOOST_LOG_TRIVIAL(info) << "Detecting solid surfaces...";
  355. // Interface shells: the intersecting parts are treated as self standing objects supporting each other.
  356. // Each of the objects will have a full number of top / bottom layers, even if these top / bottom layers
  357. // are completely hidden inside a collective body of intersecting parts.
  358. // This is useful if one of the parts is to be dissolved, or if it is transparent and the internal shells
  359. // should be visible.
  360. bool interface_shells = this->config.interface_shells.value;
  361. for (int idx_region = 0; idx_region < this->_print->regions.size(); ++ idx_region) {
  362. BOOST_LOG_TRIVIAL(debug) << "Detecting solid surfaces for region " << idx_region << " in parallel - start";
  363. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  364. for (Layer *layer : this->layers)
  365. layer->regions[idx_region]->export_region_fill_surfaces_to_svg_debug("1_detect_surfaces_type-initial");
  366. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  367. // If interface shells are allowed, the region->surfaces cannot be overwritten as they may be used by other threads.
  368. // Cache the result of the following parallel_loop.
  369. std::vector<Surfaces> surfaces_new;
  370. if (interface_shells)
  371. surfaces_new.assign(this->layers.size(), Surfaces());
  372. tbb::parallel_for(
  373. tbb::blocked_range<size_t>(0, this->layers.size()),
  374. [this, idx_region, interface_shells, &surfaces_new](const tbb::blocked_range<size_t>& range) {
  375. // If we have raft layers, consider bottom layer as a bridge just like any other bottom surface lying on the void.
  376. SurfaceType surface_type_bottom_1st =
  377. (this->config.raft_layers.value > 0 && this->config.support_material_contact_distance.value > 0) ?
  378. stBottomBridge : stBottom;
  379. // If we have soluble support material, don't bridge. The overhang will be squished against a soluble layer separating
  380. // the support from the print.
  381. SurfaceType surface_type_bottom_other =
  382. (this->config.support_material.value && this->config.support_material_contact_distance.value == 0) ?
  383. stBottom : stBottomBridge;
  384. for (size_t idx_layer = range.begin(); idx_layer < range.end(); ++ idx_layer) {
  385. // BOOST_LOG_TRIVIAL(trace) << "Detecting solid surfaces for region " << idx_region << " and layer " << layer->print_z;
  386. Layer *layer = this->layers[idx_layer];
  387. LayerRegion *layerm = layer->get_region(idx_region);
  388. // comparison happens against the *full* slices (considering all regions)
  389. // unless internal shells are requested
  390. Layer *upper_layer = (idx_layer + 1 < this->layer_count()) ? this->layers[idx_layer + 1] : nullptr;
  391. Layer *lower_layer = (idx_layer > 0) ? this->layers[idx_layer - 1] : nullptr;
  392. // collapse very narrow parts (using the safety offset in the diff is not enough)
  393. float offset = layerm->flow(frExternalPerimeter).scaled_width() / 10.f;
  394. Polygons layerm_slices_surfaces = to_polygons(layerm->slices.surfaces);
  395. // find top surfaces (difference between current surfaces
  396. // of current layer and upper one)
  397. Surfaces top;
  398. if (upper_layer) {
  399. Polygons upper_slices = interface_shells ?
  400. to_polygons(upper_layer->get_region(idx_region)->slices.surfaces) :
  401. to_polygons(upper_layer->slices);
  402. surfaces_append(top,
  403. //FIXME implement offset2_ex working over ExPolygons, that should be a bit more efficient than calling offset_ex twice.
  404. offset_ex(offset_ex(diff_ex(layerm_slices_surfaces, upper_slices, true), -offset), offset),
  405. stTop);
  406. } else {
  407. // if no upper layer, all surfaces of this one are solid
  408. // we clone surfaces because we're going to clear the slices collection
  409. top = layerm->slices.surfaces;
  410. for (Surface &surface : top)
  411. surface.surface_type = stTop;
  412. }
  413. // Find bottom surfaces (difference between current surfaces of current layer and lower one).
  414. Surfaces bottom;
  415. if (lower_layer) {
  416. #if 0
  417. //FIXME Why is this branch failing t\multi.t ?
  418. Polygons lower_slices = interface_shells ?
  419. to_polygons(lower_layer->get_region(idx_region)->slices.surfaces) :
  420. to_polygons(lower_layer->slices);
  421. surfaces_append(bottom,
  422. offset2_ex(diff(layerm_slices_surfaces, lower_slices, true), -offset, offset),
  423. surface_type_bottom_other);
  424. #else
  425. // Any surface lying on the void is a true bottom bridge (an overhang)
  426. surfaces_append(
  427. bottom,
  428. offset2_ex(
  429. diff(layerm_slices_surfaces, to_polygons(lower_layer->slices), true),
  430. -offset, offset),
  431. surface_type_bottom_other);
  432. // if user requested internal shells, we need to identify surfaces
  433. // lying on other slices not belonging to this region
  434. if (interface_shells) {
  435. // non-bridging bottom surfaces: any part of this layer lying
  436. // on something else, excluding those lying on our own region
  437. surfaces_append(
  438. bottom,
  439. offset2_ex(
  440. diff(
  441. intersection(layerm_slices_surfaces, to_polygons(lower_layer->slices)), // supported
  442. to_polygons(lower_layer->get_region(idx_region)->slices.surfaces),
  443. true),
  444. -offset, offset),
  445. stBottom);
  446. }
  447. #endif
  448. } else {
  449. // if no lower layer, all surfaces of this one are solid
  450. // we clone surfaces because we're going to clear the slices collection
  451. bottom = layerm->slices.surfaces;
  452. for (Surface &surface : bottom)
  453. surface.surface_type = surface_type_bottom_1st;
  454. }
  455. // now, if the object contained a thin membrane, we could have overlapping bottom
  456. // and top surfaces; let's do an intersection to discover them and consider them
  457. // as bottom surfaces (to allow for bridge detection)
  458. if (! top.empty() && ! bottom.empty()) {
  459. // Polygons overlapping = intersection(to_polygons(top), to_polygons(bottom));
  460. // Slic3r::debugf " layer %d contains %d membrane(s)\n", $layerm->layer->id, scalar(@$overlapping)
  461. // if $Slic3r::debug;
  462. Polygons top_polygons = to_polygons(std::move(top));
  463. top.clear();
  464. surfaces_append(top,
  465. diff_ex(top_polygons, to_polygons(bottom), false),
  466. stTop);
  467. }
  468. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  469. {
  470. static int iRun = 0;
  471. std::vector<std::pair<Slic3r::ExPolygons, SVG::ExPolygonAttributes>> expolygons_with_attributes;
  472. expolygons_with_attributes.emplace_back(std::make_pair(union_ex(top), SVG::ExPolygonAttributes("green")));
  473. expolygons_with_attributes.emplace_back(std::make_pair(union_ex(bottom), SVG::ExPolygonAttributes("brown")));
  474. expolygons_with_attributes.emplace_back(std::make_pair(to_expolygons(layerm->slices.surfaces), SVG::ExPolygonAttributes("black")));
  475. SVG::export_expolygons(debug_out_path("1_detect_surfaces_type_%d_region%d-layer_%f.svg", iRun ++, idx_region, layer->print_z).c_str(), expolygons_with_attributes);
  476. }
  477. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  478. // save surfaces to layer
  479. Surfaces &surfaces_out = interface_shells ? surfaces_new[idx_layer] : layerm->slices.surfaces;
  480. surfaces_out.clear();
  481. // find internal surfaces (difference between top/bottom surfaces and others)
  482. {
  483. Polygons topbottom = to_polygons(top);
  484. polygons_append(topbottom, to_polygons(bottom));
  485. surfaces_append(surfaces_out,
  486. diff_ex(layerm_slices_surfaces, topbottom, false),
  487. stInternal);
  488. }
  489. surfaces_append(surfaces_out, std::move(top));
  490. surfaces_append(surfaces_out, std::move(bottom));
  491. // Slic3r::debugf " layer %d has %d bottom, %d top and %d internal surfaces\n",
  492. // $layerm->layer->id, scalar(@bottom), scalar(@top), scalar(@internal) if $Slic3r::debug;
  493. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  494. layerm->export_region_slices_to_svg_debug("detect_surfaces_type-final");
  495. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  496. }
  497. }
  498. ); // for each layer of a region
  499. if (interface_shells) {
  500. // Move surfaces_new to layerm->slices.surfaces
  501. for (size_t idx_layer = 0; idx_layer < this->layers.size(); ++ idx_layer)
  502. this->layers[idx_layer]->get_region(idx_region)->slices.surfaces = std::move(surfaces_new[idx_layer]);
  503. }
  504. BOOST_LOG_TRIVIAL(debug) << "Detecting solid surfaces for region " << idx_region << " - clipping in parallel - start";
  505. // Fill in layerm->fill_surfaces by trimming the layerm->slices by the cummulative layerm->fill_surfaces.
  506. tbb::parallel_for(
  507. tbb::blocked_range<size_t>(0, this->layers.size()),
  508. [this, idx_region, interface_shells, &surfaces_new](const tbb::blocked_range<size_t>& range) {
  509. for (size_t idx_layer = range.begin(); idx_layer < range.end(); ++ idx_layer) {
  510. LayerRegion *layerm = this->layers[idx_layer]->get_region(idx_region);
  511. layerm->slices_to_fill_surfaces_clipped();
  512. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  513. layerm->export_region_fill_surfaces_to_svg_debug("1_detect_surfaces_type-final");
  514. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  515. } // for each layer of a region
  516. });
  517. BOOST_LOG_TRIVIAL(debug) << "Detecting solid surfaces for region " << idx_region << " - clipping in parallel - end";
  518. } // for each this->print->region_count
  519. // Mark the object to have the region slices classified (typed, which also means they are split based on whether they are supported, bridging, top layers etc.)
  520. this->typed_slices = true;
  521. }
  522. void PrintObject::process_external_surfaces()
  523. {
  524. BOOST_LOG_TRIVIAL(info) << "Processing external surfaces...";
  525. FOREACH_REGION(this->_print, region) {
  526. int region_id = int(region - this->_print->regions.begin());
  527. BOOST_LOG_TRIVIAL(debug) << "Processing external surfaces for region " << region_id << " in parallel - start";
  528. tbb::parallel_for(
  529. tbb::blocked_range<size_t>(0, this->layers.size()),
  530. [this, region_id](const tbb::blocked_range<size_t>& range) {
  531. for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) {
  532. // BOOST_LOG_TRIVIAL(trace) << "Processing external surface, layer" << this->layers[layer_idx]->print_z;
  533. this->layers[layer_idx]->get_region(region_id)->process_external_surfaces((layer_idx == 0) ? NULL : this->layers[layer_idx - 1]);
  534. }
  535. }
  536. );
  537. BOOST_LOG_TRIVIAL(debug) << "Processing external surfaces for region " << region_id << " in parallel - end";
  538. }
  539. }
  540. void PrintObject::discover_vertical_shells()
  541. {
  542. PROFILE_FUNC();
  543. BOOST_LOG_TRIVIAL(info) << "Discovering vertical shells...";
  544. struct DiscoverVerticalShellsCacheEntry
  545. {
  546. // Collected polygons, offsetted
  547. Polygons top_surfaces;
  548. Polygons bottom_surfaces;
  549. Polygons holes;
  550. };
  551. std::vector<DiscoverVerticalShellsCacheEntry> cache_top_botom_regions(this->layers.size(), DiscoverVerticalShellsCacheEntry());
  552. bool top_bottom_surfaces_all_regions = this->_print->regions.size() > 1 && ! this->config.interface_shells.value;
  553. if (top_bottom_surfaces_all_regions) {
  554. // This is a multi-material print and interface_shells are disabled, meaning that the vertical shell thickness
  555. // is calculated over all materials.
  556. // Is the "ensure vertical wall thickness" applicable to any region?
  557. bool has_extra_layers = false;
  558. for (size_t idx_region = 0; idx_region < this->_print->regions.size(); ++ idx_region) {
  559. const PrintRegion &region = *this->_print->get_region(idx_region);
  560. if (region.config.ensure_vertical_shell_thickness.value &&
  561. (region.config.top_solid_layers.value > 1 || region.config.bottom_solid_layers.value > 1)) {
  562. has_extra_layers = true;
  563. }
  564. }
  565. if (! has_extra_layers)
  566. // The "ensure vertical wall thickness" feature is not applicable to any of the regions. Quit.
  567. return;
  568. BOOST_LOG_TRIVIAL(debug) << "Discovering vertical shells in parallel - start : cache top / bottom";
  569. //FIXME Improve the heuristics for a grain size.
  570. size_t grain_size = std::max(this->layers.size() / 16, size_t(1));
  571. tbb::parallel_for(
  572. tbb::blocked_range<size_t>(0, this->layers.size(), grain_size),
  573. [this, &cache_top_botom_regions](const tbb::blocked_range<size_t>& range) {
  574. const SurfaceType surfaces_bottom[2] = { stBottom, stBottomBridge };
  575. const size_t num_regions = this->_print->regions.size();
  576. for (size_t idx_layer = range.begin(); idx_layer < range.end(); ++ idx_layer) {
  577. const Layer &layer = *this->layers[idx_layer];
  578. DiscoverVerticalShellsCacheEntry &cache = cache_top_botom_regions[idx_layer];
  579. // Simulate single set of perimeters over all merged regions.
  580. float perimeter_offset = 0.f;
  581. float perimeter_min_spacing = FLT_MAX;
  582. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  583. static size_t debug_idx = 0;
  584. ++ debug_idx;
  585. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  586. for (size_t idx_region = 0; idx_region < num_regions; ++ idx_region) {
  587. LayerRegion &layerm = *layer.regions[idx_region];
  588. float min_perimeter_infill_spacing = float(layerm.flow(frSolidInfill).scaled_spacing()) * 1.05f;
  589. // Top surfaces.
  590. append(cache.top_surfaces, offset(to_expolygons(layerm.slices.filter_by_type(stTop)), min_perimeter_infill_spacing));
  591. append(cache.top_surfaces, offset(to_expolygons(layerm.fill_surfaces.filter_by_type(stTop)), min_perimeter_infill_spacing));
  592. // Bottom surfaces.
  593. append(cache.bottom_surfaces, offset(to_expolygons(layerm.slices.filter_by_types(surfaces_bottom, 2)), min_perimeter_infill_spacing));
  594. append(cache.bottom_surfaces, offset(to_expolygons(layerm.fill_surfaces.filter_by_types(surfaces_bottom, 2)), min_perimeter_infill_spacing));
  595. // Calculate the maximum perimeter offset as if the slice was extruded with a single extruder only.
  596. // First find the maxium number of perimeters per region slice.
  597. unsigned int perimeters = 0;
  598. for (Surface &s : layerm.slices.surfaces)
  599. perimeters = std::max<unsigned int>(perimeters, s.extra_perimeters);
  600. perimeters += layerm.region()->config.perimeters.value;
  601. // Then calculate the infill offset.
  602. if (perimeters > 0) {
  603. Flow extflow = layerm.flow(frExternalPerimeter);
  604. Flow flow = layerm.flow(frPerimeter);
  605. perimeter_offset = std::max(perimeter_offset,
  606. 0.5f * float(extflow.scaled_width() + extflow.scaled_spacing()) + (float(perimeters) - 1.f) * flow.scaled_spacing());
  607. perimeter_min_spacing = std::min(perimeter_min_spacing, float(std::min(extflow.scaled_spacing(), flow.scaled_spacing())));
  608. }
  609. polygons_append(cache.holes, to_polygons(layerm.fill_expolygons));
  610. }
  611. // Save some computing time by reducing the number of polygons.
  612. cache.top_surfaces = union_(cache.top_surfaces, false);
  613. cache.bottom_surfaces = union_(cache.bottom_surfaces, false);
  614. // For a multi-material print, simulate perimeter / infill split as if only a single extruder has been used for the whole print.
  615. if (perimeter_offset > 0.) {
  616. // The layer.slices are forced to merge by expanding them first.
  617. polygons_append(cache.holes, offset(offset_ex(layer.slices, 0.3f * perimeter_min_spacing), - perimeter_offset - 0.3f * perimeter_min_spacing));
  618. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  619. {
  620. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-extra-holes-%d.svg", debug_idx), get_extents(layer.slices.expolygons));
  621. svg.draw(layer.slices.expolygons, "blue");
  622. svg.draw(union_ex(cache.holes), "red");
  623. svg.draw_outline(union_ex(cache.holes), "black", "blue", scale_(0.05));
  624. svg.Close();
  625. }
  626. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  627. }
  628. cache.holes = union_(cache.holes, false);
  629. }
  630. });
  631. BOOST_LOG_TRIVIAL(debug) << "Discovering vertical shells in parallel - end : cache top / bottom";
  632. }
  633. for (size_t idx_region = 0; idx_region < this->_print->regions.size(); ++ idx_region) {
  634. PROFILE_BLOCK(discover_vertical_shells_region);
  635. const PrintRegion &region = *this->_print->get_region(idx_region);
  636. if (! region.config.ensure_vertical_shell_thickness.value)
  637. // This region will be handled by discover_horizontal_shells().
  638. continue;
  639. int n_extra_top_layers = std::max(0, region.config.top_solid_layers.value - 1);
  640. int n_extra_bottom_layers = std::max(0, region.config.bottom_solid_layers.value - 1);
  641. if (n_extra_top_layers + n_extra_bottom_layers == 0)
  642. // Zero or 1 layer, there is no additional vertical wall thickness enforced.
  643. continue;
  644. //FIXME Improve the heuristics for a grain size.
  645. size_t grain_size = std::max(this->layers.size() / 16, size_t(1));
  646. if (! top_bottom_surfaces_all_regions) {
  647. // This is either a single material print, or a multi-material print and interface_shells are enabled, meaning that the vertical shell thickness
  648. // is calculated over a single material.
  649. BOOST_LOG_TRIVIAL(debug) << "Discovering vertical shells for region " << idx_region << " in parallel - start : cache top / bottom";
  650. tbb::parallel_for(
  651. tbb::blocked_range<size_t>(0, this->layers.size(), grain_size),
  652. [this, idx_region, &cache_top_botom_regions](const tbb::blocked_range<size_t>& range) {
  653. const SurfaceType surfaces_bottom[2] = { stBottom, stBottomBridge };
  654. for (size_t idx_layer = range.begin(); idx_layer < range.end(); ++ idx_layer) {
  655. Layer &layer = *this->layers[idx_layer];
  656. LayerRegion &layerm = *layer.regions[idx_region];
  657. float min_perimeter_infill_spacing = float(layerm.flow(frSolidInfill).scaled_spacing()) * 1.05f;
  658. // Top surfaces.
  659. auto &cache = cache_top_botom_regions[idx_layer];
  660. cache.top_surfaces = offset(to_expolygons(layerm.slices.filter_by_type(stTop)), min_perimeter_infill_spacing);
  661. append(cache.top_surfaces, offset(to_expolygons(layerm.fill_surfaces.filter_by_type(stTop)), min_perimeter_infill_spacing));
  662. // Bottom surfaces.
  663. cache.bottom_surfaces = offset(to_expolygons(layerm.slices.filter_by_types(surfaces_bottom, 2)), min_perimeter_infill_spacing);
  664. append(cache.bottom_surfaces, offset(to_expolygons(layerm.fill_surfaces.filter_by_types(surfaces_bottom, 2)), min_perimeter_infill_spacing));
  665. // Holes over all regions. Only collect them once, they are valid for all idx_region iterations.
  666. if (cache.holes.empty()) {
  667. for (size_t idx_region = 0; idx_region < layer.regions.size(); ++ idx_region)
  668. polygons_append(cache.holes, to_polygons(layer.regions[idx_region]->fill_expolygons));
  669. }
  670. }
  671. });
  672. BOOST_LOG_TRIVIAL(debug) << "Discovering vertical shells for region " << idx_region << " in parallel - end : cache top / bottom";
  673. }
  674. BOOST_LOG_TRIVIAL(debug) << "Discovering vertical shells for region " << idx_region << " in parallel - start : ensure vertical wall thickness";
  675. tbb::parallel_for(
  676. tbb::blocked_range<size_t>(0, this->layers.size(), grain_size),
  677. [this, idx_region, n_extra_top_layers, n_extra_bottom_layers, &cache_top_botom_regions]
  678. (const tbb::blocked_range<size_t>& range) {
  679. // printf("discover_vertical_shells from %d to %d\n", range.begin(), range.end());
  680. for (size_t idx_layer = range.begin(); idx_layer < range.end(); ++ idx_layer) {
  681. PROFILE_BLOCK(discover_vertical_shells_region_layer);
  682. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  683. static size_t debug_idx = 0;
  684. ++ debug_idx;
  685. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  686. Layer *layer = this->layers[idx_layer];
  687. LayerRegion *layerm = layer->regions[idx_region];
  688. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  689. layerm->export_region_slices_to_svg_debug("4_discover_vertical_shells-initial");
  690. layerm->export_region_fill_surfaces_to_svg_debug("4_discover_vertical_shells-initial");
  691. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  692. Flow solid_infill_flow = layerm->flow(frSolidInfill);
  693. coord_t infill_line_spacing = solid_infill_flow.scaled_spacing();
  694. // Find a union of perimeters below / above this surface to guarantee a minimum shell thickness.
  695. Polygons shell;
  696. Polygons holes;
  697. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  698. ExPolygons shell_ex;
  699. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  700. float min_perimeter_infill_spacing = float(infill_line_spacing) * 1.05f;
  701. {
  702. PROFILE_BLOCK(discover_vertical_shells_region_layer_collect);
  703. #if 0
  704. // #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  705. {
  706. Slic3r::SVG svg_cummulative(debug_out_path("discover_vertical_shells-perimeters-before-union-run%d.svg", debug_idx), this->bounding_box());
  707. for (int n = (int)idx_layer - n_extra_bottom_layers; n <= (int)idx_layer + n_extra_top_layers; ++ n) {
  708. if (n < 0 || n >= (int)this->layers.size())
  709. continue;
  710. ExPolygons &expolys = this->layers[n]->perimeter_expolygons;
  711. for (size_t i = 0; i < expolys.size(); ++ i) {
  712. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-perimeters-before-union-run%d-layer%d-expoly%d.svg", debug_idx, n, i), get_extents(expolys[i]));
  713. svg.draw(expolys[i]);
  714. svg.draw_outline(expolys[i].contour, "black", scale_(0.05));
  715. svg.draw_outline(expolys[i].holes, "blue", scale_(0.05));
  716. svg.Close();
  717. svg_cummulative.draw(expolys[i]);
  718. svg_cummulative.draw_outline(expolys[i].contour, "black", scale_(0.05));
  719. svg_cummulative.draw_outline(expolys[i].holes, "blue", scale_(0.05));
  720. }
  721. }
  722. }
  723. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  724. // Reset the top / bottom inflated regions caches of entries, which are out of the moving window.
  725. bool hole_first = true;
  726. for (int n = (int)idx_layer - n_extra_bottom_layers; n <= (int)idx_layer + n_extra_top_layers; ++ n)
  727. if (n >= 0 && n < (int)this->layers.size()) {
  728. Layer &neighbor_layer = *this->layers[n];
  729. const DiscoverVerticalShellsCacheEntry &cache = cache_top_botom_regions[n];
  730. if (hole_first) {
  731. hole_first = false;
  732. polygons_append(holes, cache.holes);
  733. }
  734. else if (! holes.empty()) {
  735. holes = intersection(holes, cache.holes);
  736. }
  737. size_t n_shell_old = shell.size();
  738. if (n > int(idx_layer))
  739. // Collect top surfaces.
  740. polygons_append(shell, cache.top_surfaces);
  741. else if (n < int(idx_layer))
  742. // Collect bottom and bottom bridge surfaces.
  743. polygons_append(shell, cache.bottom_surfaces);
  744. // Running the union_ using the Clipper library piece by piece is cheaper
  745. // than running the union_ all at once.
  746. if (n_shell_old < shell.size())
  747. shell = union_(shell, false);
  748. }
  749. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  750. {
  751. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-perimeters-before-union-%d.svg", debug_idx), get_extents(shell));
  752. svg.draw(shell);
  753. svg.draw_outline(shell, "black", scale_(0.05));
  754. svg.Close();
  755. }
  756. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  757. #if 0
  758. {
  759. PROFILE_BLOCK(discover_vertical_shells_region_layer_shell_);
  760. // shell = union_(shell, true);
  761. shell = union_(shell, false);
  762. }
  763. #endif
  764. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  765. shell_ex = union_ex(shell, true);
  766. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  767. }
  768. //if (shell.empty())
  769. // continue;
  770. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  771. {
  772. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-perimeters-after-union-%d.svg", debug_idx), get_extents(shell));
  773. svg.draw(shell_ex);
  774. svg.draw_outline(shell_ex, "black", "blue", scale_(0.05));
  775. svg.Close();
  776. }
  777. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  778. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  779. {
  780. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-internal-wshell-%d.svg", debug_idx), get_extents(shell));
  781. svg.draw(layerm->fill_surfaces.filter_by_type(stInternal), "yellow", 0.5);
  782. svg.draw_outline(layerm->fill_surfaces.filter_by_type(stInternal), "black", "blue", scale_(0.05));
  783. svg.draw(shell_ex, "blue", 0.5);
  784. svg.draw_outline(shell_ex, "black", "blue", scale_(0.05));
  785. svg.Close();
  786. }
  787. {
  788. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-internalvoid-wshell-%d.svg", debug_idx), get_extents(shell));
  789. svg.draw(layerm->fill_surfaces.filter_by_type(stInternalVoid), "yellow", 0.5);
  790. svg.draw_outline(layerm->fill_surfaces.filter_by_type(stInternalVoid), "black", "blue", scale_(0.05));
  791. svg.draw(shell_ex, "blue", 0.5);
  792. svg.draw_outline(shell_ex, "black", "blue", scale_(0.05));
  793. svg.Close();
  794. }
  795. {
  796. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-internalvoid-wshell-%d.svg", debug_idx), get_extents(shell));
  797. svg.draw(layerm->fill_surfaces.filter_by_type(stInternalVoid), "yellow", 0.5);
  798. svg.draw_outline(layerm->fill_surfaces.filter_by_type(stInternalVoid), "black", "blue", scale_(0.05));
  799. svg.draw(shell_ex, "blue", 0.5);
  800. svg.draw_outline(shell_ex, "black", "blue", scale_(0.05));
  801. svg.Close();
  802. }
  803. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  804. // Trim the shells region by the internal & internal void surfaces.
  805. const SurfaceType surfaceTypesInternal[] = { stInternal, stInternalVoid, stInternalSolid };
  806. const Polygons polygonsInternal = to_polygons(layerm->fill_surfaces.filter_by_types(surfaceTypesInternal, 3));
  807. shell = intersection(shell, polygonsInternal, true);
  808. polygons_append(shell, diff(polygonsInternal, holes));
  809. if (shell.empty())
  810. continue;
  811. // Append the internal solids, so they will be merged with the new ones.
  812. polygons_append(shell, to_polygons(layerm->fill_surfaces.filter_by_type(stInternalSolid)));
  813. // These regions will be filled by a rectilinear full infill. Currently this type of infill
  814. // only fills regions, which fit at least a single line. To avoid gaps in the sparse infill,
  815. // make sure that this region does not contain parts narrower than the infill spacing width.
  816. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  817. Polygons shell_before = shell;
  818. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  819. #if 1
  820. // Intentionally inflate a bit more than how much the region has been shrunk,
  821. // so there will be some overlap between this solid infill and the other infill regions (mainly the sparse infill).
  822. shell = offset2(shell, - 0.5f * min_perimeter_infill_spacing, 0.8f * min_perimeter_infill_spacing, ClipperLib::jtSquare);
  823. if (shell.empty())
  824. continue;
  825. #else
  826. // Ensure each region is at least 3x infill line width wide, so it could be filled in.
  827. // float margin = float(infill_line_spacing) * 3.f;
  828. float margin = float(infill_line_spacing) * 1.5f;
  829. // we use a higher miterLimit here to handle areas with acute angles
  830. // in those cases, the default miterLimit would cut the corner and we'd
  831. // get a triangle in $too_narrow; if we grow it below then the shell
  832. // would have a different shape from the external surface and we'd still
  833. // have the same angle, so the next shell would be grown even more and so on.
  834. Polygons too_narrow = diff(shell, offset2(shell, -margin, margin, ClipperLib::jtMiter, 5.), true);
  835. if (! too_narrow.empty()) {
  836. // grow the collapsing parts and add the extra area to the neighbor layer
  837. // as well as to our original surfaces so that we support this
  838. // additional area in the next shell too
  839. // make sure our grown surfaces don't exceed the fill area
  840. polygons_append(shell, intersection(offset(too_narrow, margin), polygonsInternal));
  841. }
  842. #endif
  843. ExPolygons new_internal_solid = intersection_ex(polygonsInternal, shell, false);
  844. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  845. {
  846. Slic3r::SVG svg(debug_out_path("discover_vertical_shells-regularized-%d.svg", debug_idx), get_extents(shell_before));
  847. // Source shell.
  848. svg.draw(union_ex(shell_before, true));
  849. // Shell trimmed to the internal surfaces.
  850. svg.draw_outline(union_ex(shell, true), "black", "blue", scale_(0.05));
  851. // Regularized infill region.
  852. svg.draw_outline(new_internal_solid, "red", "magenta", scale_(0.05));
  853. svg.Close();
  854. }
  855. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  856. // Trim the internal & internalvoid by the shell.
  857. Slic3r::ExPolygons new_internal = diff_ex(
  858. to_polygons(layerm->fill_surfaces.filter_by_type(stInternal)),
  859. shell,
  860. false
  861. );
  862. Slic3r::ExPolygons new_internal_void = diff_ex(
  863. to_polygons(layerm->fill_surfaces.filter_by_type(stInternalVoid)),
  864. shell,
  865. false
  866. );
  867. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  868. {
  869. SVG::export_expolygons(debug_out_path("discover_vertical_shells-new_internal-%d.svg", debug_idx), get_extents(shell), new_internal, "black", "blue", scale_(0.05));
  870. SVG::export_expolygons(debug_out_path("discover_vertical_shells-new_internal_void-%d.svg", debug_idx), get_extents(shell), new_internal_void, "black", "blue", scale_(0.05));
  871. SVG::export_expolygons(debug_out_path("discover_vertical_shells-new_internal_solid-%d.svg", debug_idx), get_extents(shell), new_internal_solid, "black", "blue", scale_(0.05));
  872. }
  873. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  874. // Assign resulting internal surfaces to layer.
  875. const SurfaceType surfaceTypesKeep[] = { stTop, stBottom, stBottomBridge };
  876. layerm->fill_surfaces.keep_types(surfaceTypesKeep, sizeof(surfaceTypesKeep)/sizeof(SurfaceType));
  877. layerm->fill_surfaces.append(new_internal, stInternal);
  878. layerm->fill_surfaces.append(new_internal_void, stInternalVoid);
  879. layerm->fill_surfaces.append(new_internal_solid, stInternalSolid);
  880. } // for each layer
  881. });
  882. BOOST_LOG_TRIVIAL(debug) << "Discovering vertical shells for region " << idx_region << " in parallel - end";
  883. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  884. for (size_t idx_layer = 0; idx_layer < this->layers.size(); ++idx_layer) {
  885. LayerRegion *layerm = this->layers[idx_layer]->get_region(idx_region);
  886. layerm->export_region_slices_to_svg_debug("4_discover_vertical_shells-final");
  887. layerm->export_region_fill_surfaces_to_svg_debug("4_discover_vertical_shells-final");
  888. }
  889. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  890. } // for each region
  891. // Write the profiler measurements to file
  892. // PROFILE_UPDATE();
  893. // PROFILE_OUTPUT(debug_out_path("discover_vertical_shells-profile.txt").c_str());
  894. }
  895. /* This method applies bridge flow to the first internal solid layer above
  896. sparse infill */
  897. void PrintObject::bridge_over_infill()
  898. {
  899. BOOST_LOG_TRIVIAL(info) << "Bridge over infill...";
  900. FOREACH_REGION(this->_print, region) {
  901. size_t region_id = region - this->_print->regions.begin();
  902. // skip bridging in case there are no voids
  903. if ((*region)->config.fill_density.value == 100) continue;
  904. // get bridge flow
  905. Flow bridge_flow = (*region)->flow(
  906. frSolidInfill,
  907. -1, // layer height, not relevant for bridge flow
  908. true, // bridge
  909. false, // first layer
  910. -1, // custom width, not relevant for bridge flow
  911. *this
  912. );
  913. FOREACH_LAYER(this, layer_it) {
  914. // skip first layer
  915. if (layer_it == this->layers.begin()) continue;
  916. Layer* layer = *layer_it;
  917. LayerRegion* layerm = layer->regions[region_id];
  918. // extract the stInternalSolid surfaces that might be transformed into bridges
  919. Polygons internal_solid;
  920. layerm->fill_surfaces.filter_by_type(stInternalSolid, &internal_solid);
  921. // check whether the lower area is deep enough for absorbing the extra flow
  922. // (for obvious physical reasons but also for preventing the bridge extrudates
  923. // from overflowing in 3D preview)
  924. ExPolygons to_bridge;
  925. {
  926. Polygons to_bridge_pp = internal_solid;
  927. // iterate through lower layers spanned by bridge_flow
  928. double bottom_z = layer->print_z - bridge_flow.height;
  929. for (int i = int(layer_it - this->layers.begin()) - 1; i >= 0; --i) {
  930. const Layer* lower_layer = this->layers[i];
  931. // stop iterating if layer is lower than bottom_z
  932. if (lower_layer->print_z < bottom_z) break;
  933. // iterate through regions and collect internal surfaces
  934. Polygons lower_internal;
  935. FOREACH_LAYERREGION(lower_layer, lower_layerm_it)
  936. (*lower_layerm_it)->fill_surfaces.filter_by_type(stInternal, &lower_internal);
  937. // intersect such lower internal surfaces with the candidate solid surfaces
  938. to_bridge_pp = intersection(to_bridge_pp, lower_internal);
  939. }
  940. // there's no point in bridging too thin/short regions
  941. //FIXME Vojtech: The offset2 function is not a geometric offset,
  942. // therefore it may create 1) gaps, and 2) sharp corners, which are outside the original contour.
  943. // The gaps will be filled by a separate region, which makes the infill less stable and it takes longer.
  944. {
  945. float min_width = float(bridge_flow.scaled_width()) * 3.f;
  946. to_bridge_pp = offset2(to_bridge_pp, -min_width, +min_width);
  947. }
  948. if (to_bridge_pp.empty()) continue;
  949. // convert into ExPolygons
  950. to_bridge = union_ex(to_bridge_pp);
  951. }
  952. #ifdef SLIC3R_DEBUG
  953. printf("Bridging " PRINTF_ZU " internal areas at layer " PRINTF_ZU "\n", to_bridge.size(), layer->id());
  954. #endif
  955. // compute the remaning internal solid surfaces as difference
  956. ExPolygons not_to_bridge = diff_ex(internal_solid, to_polygons(to_bridge), true);
  957. to_bridge = intersection_ex(to_polygons(to_bridge), internal_solid, true);
  958. // build the new collection of fill_surfaces
  959. layerm->fill_surfaces.remove_type(stInternalSolid);
  960. for (ExPolygon &ex : to_bridge)
  961. layerm->fill_surfaces.surfaces.push_back(Surface(stInternalBridge, ex));
  962. for (ExPolygon &ex : not_to_bridge)
  963. layerm->fill_surfaces.surfaces.push_back(Surface(stInternalSolid, ex));
  964. /*
  965. # exclude infill from the layers below if needed
  966. # see discussion at https://github.com/alexrj/Slic3r/issues/240
  967. # Update: do not exclude any infill. Sparse infill is able to absorb the excess material.
  968. if (0) {
  969. my $excess = $layerm->extruders->{infill}->bridge_flow->width - $layerm->height;
  970. for (my $i = $layer_id-1; $excess >= $self->get_layer($i)->height; $i--) {
  971. Slic3r::debugf " skipping infill below those areas at layer %d\n", $i;
  972. foreach my $lower_layerm (@{$self->get_layer($i)->regions}) {
  973. my @new_surfaces = ();
  974. # subtract the area from all types of surfaces
  975. foreach my $group (@{$lower_layerm->fill_surfaces->group}) {
  976. push @new_surfaces, map $group->[0]->clone(expolygon => $_),
  977. @{diff_ex(
  978. [ map $_->p, @$group ],
  979. [ map @$_, @$to_bridge ],
  980. )};
  981. push @new_surfaces, map Slic3r::Surface->new(
  982. expolygon => $_,
  983. surface_type => S_TYPE_INTERNALVOID,
  984. ), @{intersection_ex(
  985. [ map $_->p, @$group ],
  986. [ map @$_, @$to_bridge ],
  987. )};
  988. }
  989. $lower_layerm->fill_surfaces->clear;
  990. $lower_layerm->fill_surfaces->append($_) for @new_surfaces;
  991. }
  992. $excess -= $self->get_layer($i)->height;
  993. }
  994. }
  995. */
  996. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  997. layerm->export_region_slices_to_svg_debug("7_bridge_over_infill");
  998. layerm->export_region_fill_surfaces_to_svg_debug("7_bridge_over_infill");
  999. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  1000. }
  1001. }
  1002. }
  1003. SlicingParameters PrintObject::slicing_parameters() const
  1004. {
  1005. return SlicingParameters::create_from_config(
  1006. this->print()->config, this->config,
  1007. unscale(this->size.z), this->print()->object_extruders());
  1008. }
  1009. bool PrintObject::update_layer_height_profile(std::vector<coordf_t> &layer_height_profile) const
  1010. {
  1011. bool updated = false;
  1012. // If the layer height profile is not set, try to use the one stored at the ModelObject.
  1013. if (layer_height_profile.empty() && layer_height_profile.data() != this->model_object()->layer_height_profile.data()) {
  1014. layer_height_profile = this->model_object()->layer_height_profile;
  1015. updated = true;
  1016. }
  1017. // Verify the layer_height_profile.
  1018. SlicingParameters slicing_params = this->slicing_parameters();
  1019. if (! layer_height_profile.empty() &&
  1020. // Must not be of even length.
  1021. ((layer_height_profile.size() & 1) != 0 ||
  1022. // Last entry must be at the top of the object.
  1023. std::abs(layer_height_profile[layer_height_profile.size() - 2] - slicing_params.object_print_z_height()) > 1e-3))
  1024. layer_height_profile.clear();
  1025. if (layer_height_profile.empty()) {
  1026. if (0)
  1027. // if (this->layer_height_profile.empty())
  1028. layer_height_profile = layer_height_profile_adaptive(slicing_params, this->layer_height_ranges,
  1029. this->model_object()->volumes);
  1030. else
  1031. layer_height_profile = layer_height_profile_from_ranges(slicing_params, this->layer_height_ranges);
  1032. updated = true;
  1033. }
  1034. return updated;
  1035. }
  1036. // This must be called from the main thread as it modifies the layer_height_profile.
  1037. bool PrintObject::update_layer_height_profile()
  1038. {
  1039. // If the layer height profile has been marked as invalid for some reason (modified at the UI level
  1040. // or invalidated due to the slicing parameters), clear it now.
  1041. if (! this->layer_height_profile_valid) {
  1042. this->layer_height_profile.clear();
  1043. this->layer_height_profile_valid = true;
  1044. }
  1045. return this->update_layer_height_profile(this->layer_height_profile);
  1046. }
  1047. // 1) Decides Z positions of the layers,
  1048. // 2) Initializes layers and their regions
  1049. // 3) Slices the object meshes
  1050. // 4) Slices the modifier meshes and reclassifies the slices of the object meshes by the slices of the modifier meshes
  1051. // 5) Applies size compensation (offsets the slices in XY plane)
  1052. // 6) Replaces bad slices by the slices reconstructed from the upper/lower layer
  1053. // Resulting expolygons of layer regions are marked as Internal.
  1054. //
  1055. // this should be idempotent
  1056. void PrintObject::_slice()
  1057. {
  1058. BOOST_LOG_TRIVIAL(info) << "Slicing objects...";
  1059. this->typed_slices = false;
  1060. #if 0
  1061. // Disable parallelization for debugging purposes.
  1062. static tbb::task_scheduler_init *tbb_init = nullptr;
  1063. tbb_init = new tbb::task_scheduler_init(1);
  1064. #endif
  1065. SlicingParameters slicing_params = this->slicing_parameters();
  1066. // 1) Initialize layers and their slice heights.
  1067. std::vector<float> slice_zs;
  1068. {
  1069. this->clear_layers();
  1070. // Object layers (pairs of bottom/top Z coordinate), without the raft.
  1071. std::vector<coordf_t> object_layers = generate_object_layers(slicing_params, this->layer_height_profile);
  1072. // Reserve object layers for the raft. Last layer of the raft is the contact layer.
  1073. int id = int(slicing_params.raft_layers());
  1074. slice_zs.reserve(object_layers.size());
  1075. Layer *prev = nullptr;
  1076. for (size_t i_layer = 0; i_layer < object_layers.size(); i_layer += 2) {
  1077. coordf_t lo = object_layers[i_layer];
  1078. coordf_t hi = object_layers[i_layer + 1];
  1079. coordf_t slice_z = 0.5 * (lo + hi);
  1080. Layer *layer = this->add_layer(id ++, hi - lo, hi + slicing_params.object_print_z_min, slice_z);
  1081. slice_zs.push_back(float(slice_z));
  1082. if (prev != nullptr) {
  1083. prev->upper_layer = layer;
  1084. layer->lower_layer = prev;
  1085. }
  1086. // Make sure all layers contain layer region objects for all regions.
  1087. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id)
  1088. layer->add_region(this->print()->regions[region_id]);
  1089. prev = layer;
  1090. }
  1091. }
  1092. // Slice all non-modifier volumes.
  1093. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  1094. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - region " << region_id;
  1095. std::vector<ExPolygons> expolygons_by_layer = this->_slice_region(region_id, slice_zs, false);
  1096. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - append slices " << region_id << " start";
  1097. for (size_t layer_id = 0; layer_id < expolygons_by_layer.size(); ++ layer_id)
  1098. this->layers[layer_id]->regions[region_id]->slices.append(std::move(expolygons_by_layer[layer_id]), stInternal);
  1099. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - append slices " << region_id << " end";
  1100. }
  1101. // Slice all modifier volumes.
  1102. if (this->print()->regions.size() > 1) {
  1103. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  1104. BOOST_LOG_TRIVIAL(debug) << "Slicing modifier volumes - region " << region_id;
  1105. std::vector<ExPolygons> expolygons_by_layer = this->_slice_region(region_id, slice_zs, true);
  1106. // loop through the other regions and 'steal' the slices belonging to this one
  1107. BOOST_LOG_TRIVIAL(debug) << "Slicing modifier volumes - stealing " << region_id << " start";
  1108. for (size_t other_region_id = 0; other_region_id < this->print()->regions.size(); ++ other_region_id) {
  1109. if (region_id == other_region_id)
  1110. continue;
  1111. for (size_t layer_id = 0; layer_id < expolygons_by_layer.size(); ++ layer_id) {
  1112. Layer *layer = layers[layer_id];
  1113. LayerRegion *layerm = layer->regions[region_id];
  1114. LayerRegion *other_layerm = layer->regions[other_region_id];
  1115. if (layerm == nullptr || other_layerm == nullptr)
  1116. continue;
  1117. Polygons other_slices = to_polygons(other_layerm->slices);
  1118. ExPolygons my_parts = intersection_ex(other_slices, to_polygons(expolygons_by_layer[layer_id]));
  1119. if (my_parts.empty())
  1120. continue;
  1121. // Remove such parts from original region.
  1122. other_layerm->slices.set(diff_ex(other_slices, to_polygons(my_parts)), stInternal);
  1123. // Append new parts to our region.
  1124. layerm->slices.append(std::move(my_parts), stInternal);
  1125. }
  1126. }
  1127. BOOST_LOG_TRIVIAL(debug) << "Slicing modifier volumes - stealing " << region_id << " end";
  1128. }
  1129. }
  1130. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - removing top empty layers";
  1131. while (! this->layers.empty()) {
  1132. const Layer *layer = this->layers.back();
  1133. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id)
  1134. if (layer->regions[region_id] != nullptr && ! layer->regions[region_id]->slices.empty())
  1135. // Non empty layer.
  1136. goto end;
  1137. delete layer;
  1138. this->layers.pop_back();
  1139. if (! this->layers.empty())
  1140. this->layers.back()->upper_layer = nullptr;
  1141. }
  1142. end:
  1143. ;
  1144. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - make_slices in parallel - begin";
  1145. tbb::parallel_for(
  1146. tbb::blocked_range<size_t>(0, this->layers.size()),
  1147. [this](const tbb::blocked_range<size_t>& range) {
  1148. for (size_t layer_id = range.begin(); layer_id < range.end(); ++ layer_id) {
  1149. Layer *layer = this->layers[layer_id];
  1150. // Apply size compensation and perform clipping of multi-part objects.
  1151. float delta = float(scale_(this->config.xy_size_compensation.value));
  1152. if (layer_id == 0)
  1153. delta -= float(scale_(this->config.elefant_foot_compensation.value));
  1154. bool scale = delta != 0.f;
  1155. bool clip = this->config.clip_multipart_objects.value || delta > 0.f;
  1156. if (layer->regions.size() == 1) {
  1157. if (scale) {
  1158. // Single region, growing or shrinking.
  1159. LayerRegion *layerm = layer->regions.front();
  1160. layerm->slices.set(offset_ex(to_expolygons(std::move(layerm->slices.surfaces)), delta), stInternal);
  1161. }
  1162. } else if (scale || clip) {
  1163. // Multiple regions, growing, shrinking or just clipping one region by the other.
  1164. // When clipping the regions, priority is given to the first regions.
  1165. Polygons processed;
  1166. for (size_t region_id = 0; region_id < layer->regions.size(); ++ region_id) {
  1167. LayerRegion *layerm = layer->regions[region_id];
  1168. ExPolygons slices = to_expolygons(std::move(layerm->slices.surfaces));
  1169. if (scale)
  1170. slices = offset_ex(slices, delta);
  1171. if (region_id > 0 && clip)
  1172. // Trim by the slices of already processed regions.
  1173. slices = diff_ex(to_polygons(std::move(slices)), processed);
  1174. if (clip && region_id + 1 < layer->regions.size())
  1175. // Collect the already processed regions to trim the to be processed regions.
  1176. polygons_append(processed, slices);
  1177. layerm->slices.set(std::move(slices), stInternal);
  1178. }
  1179. }
  1180. // Merge all regions' slices to get islands, chain them by a shortest path.
  1181. layer->make_slices();
  1182. }
  1183. });
  1184. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - make_slices in parallel - end";
  1185. }
  1186. std::vector<ExPolygons> PrintObject::_slice_region(size_t region_id, const std::vector<float> &z, bool modifier)
  1187. {
  1188. std::vector<ExPolygons> layers;
  1189. if (region_id < this->region_volumes.size()) {
  1190. std::vector<int> &volumes = this->region_volumes[region_id];
  1191. if (! volumes.empty()) {
  1192. // Compose mesh.
  1193. //FIXME better to perform slicing over each volume separately and then to use a Boolean operation to merge them.
  1194. TriangleMesh mesh;
  1195. for (int volume_id : volumes) {
  1196. ModelVolume *volume = this->model_object()->volumes[volume_id];
  1197. if (volume->modifier == modifier)
  1198. mesh.merge(volume->mesh);
  1199. }
  1200. if (mesh.stl.stats.number_of_facets > 0) {
  1201. // transform mesh
  1202. // we ignore the per-instance transformations currently and only
  1203. // consider the first one
  1204. this->model_object()->instances.front()->transform_mesh(&mesh, true);
  1205. // align mesh to Z = 0 (it should be already aligned actually) and apply XY shift
  1206. mesh.translate(- float(unscale(this->_copies_shift.x)), - float(unscale(this->_copies_shift.y)), -float(this->model_object()->bounding_box().min.z));
  1207. // perform actual slicing
  1208. TriangleMeshSlicer mslicer(&mesh);
  1209. mslicer.slice(z, &layers);
  1210. }
  1211. }
  1212. }
  1213. return layers;
  1214. }
  1215. std::string PrintObject::_fix_slicing_errors()
  1216. {
  1217. // Collect layers with slicing errors.
  1218. // These layers will be fixed in parallel.
  1219. std::vector<size_t> buggy_layers;
  1220. buggy_layers.reserve(this->layers.size());
  1221. for (size_t idx_layer = 0; idx_layer < this->layers.size(); ++ idx_layer)
  1222. if (this->layers[idx_layer]->slicing_errors)
  1223. buggy_layers.push_back(idx_layer);
  1224. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - fixing slicing errors in parallel - begin";
  1225. tbb::parallel_for(
  1226. tbb::blocked_range<size_t>(0, buggy_layers.size()),
  1227. [this, &buggy_layers](const tbb::blocked_range<size_t>& range) {
  1228. for (size_t buggy_layer_idx = range.begin(); buggy_layer_idx < range.end(); ++ buggy_layer_idx) {
  1229. size_t idx_layer = buggy_layers[buggy_layer_idx];
  1230. Layer *layer = this->layers[idx_layer];
  1231. assert(layer->slicing_errors);
  1232. // Try to repair the layer surfaces by merging all contours and all holes from neighbor layers.
  1233. // BOOST_LOG_TRIVIAL(trace) << "Attempting to repair layer" << idx_layer;
  1234. for (size_t region_id = 0; region_id < layer->regions.size(); ++ region_id) {
  1235. LayerRegion *layerm = layer->regions[region_id];
  1236. // Find the first valid layer below / above the current layer.
  1237. const Surfaces *upper_surfaces = nullptr;
  1238. const Surfaces *lower_surfaces = nullptr;
  1239. for (size_t j = idx_layer + 1; j < this->layers.size(); ++ j)
  1240. if (! this->layers[j]->slicing_errors) {
  1241. upper_surfaces = &this->layers[j]->regions[region_id]->slices.surfaces;
  1242. break;
  1243. }
  1244. for (int j = int(idx_layer) - 1; j >= 0; -- j)
  1245. if (! this->layers[j]->slicing_errors) {
  1246. lower_surfaces = &this->layers[j]->regions[region_id]->slices.surfaces;
  1247. break;
  1248. }
  1249. // Collect outer contours and holes from the valid layers above & below.
  1250. Polygons outer;
  1251. outer.reserve(
  1252. ((upper_surfaces == nullptr) ? 0 : upper_surfaces->size()) +
  1253. ((lower_surfaces == nullptr) ? 0 : lower_surfaces->size()));
  1254. size_t num_holes = 0;
  1255. if (upper_surfaces)
  1256. for (const auto &surface : *upper_surfaces) {
  1257. outer.push_back(surface.expolygon.contour);
  1258. num_holes += surface.expolygon.holes.size();
  1259. }
  1260. if (lower_surfaces)
  1261. for (const auto &surface : *lower_surfaces) {
  1262. outer.push_back(surface.expolygon.contour);
  1263. num_holes += surface.expolygon.holes.size();
  1264. }
  1265. Polygons holes;
  1266. holes.reserve(num_holes);
  1267. if (upper_surfaces)
  1268. for (const auto &surface : *upper_surfaces)
  1269. polygons_append(holes, surface.expolygon.holes);
  1270. if (lower_surfaces)
  1271. for (const auto &surface : *lower_surfaces)
  1272. polygons_append(holes, surface.expolygon.holes);
  1273. layerm->slices.set(diff_ex(union_(outer), holes, false), stInternal);
  1274. }
  1275. // Update layer slices after repairing the single regions.
  1276. layer->make_slices();
  1277. }
  1278. });
  1279. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - fixing slicing errors in parallel - end";
  1280. // remove empty layers from bottom
  1281. while (! this->layers.empty() && this->layers.front()->slices.expolygons.empty()) {
  1282. delete this->layers.front();
  1283. this->layers.erase(this->layers.begin());
  1284. this->layers.front()->lower_layer = nullptr;
  1285. for (size_t i = 0; i < this->layers.size(); ++ i)
  1286. this->layers[i]->set_id(this->layers[i]->id() - 1);
  1287. }
  1288. return buggy_layers.empty() ? "" :
  1289. "The model has overlapping or self-intersecting facets. I tried to repair it, "
  1290. "however you might want to check the results or repair the input file and retry.\n";
  1291. }
  1292. // Simplify the sliced model, if "resolution" configuration parameter > 0.
  1293. // The simplification is problematic, because it simplifies the slices independent from each other,
  1294. // which makes the simplified discretization visible on the object surface.
  1295. void PrintObject::_simplify_slices(double distance)
  1296. {
  1297. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - siplifying slices in parallel - begin";
  1298. tbb::parallel_for(
  1299. tbb::blocked_range<size_t>(0, this->layers.size()),
  1300. [this, distance](const tbb::blocked_range<size_t>& range) {
  1301. for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) {
  1302. Layer *layer = this->layers[layer_idx];
  1303. for (size_t region_idx = 0; region_idx < layer->regions.size(); ++ region_idx)
  1304. layer->regions[region_idx]->slices.simplify(distance);
  1305. layer->slices.simplify(distance);
  1306. }
  1307. });
  1308. BOOST_LOG_TRIVIAL(debug) << "Slicing objects - siplifying slices in parallel - end";
  1309. }
  1310. void PrintObject::_make_perimeters()
  1311. {
  1312. if (this->state.is_done(posPerimeters)) return;
  1313. this->state.set_started(posPerimeters);
  1314. BOOST_LOG_TRIVIAL(info) << "Generating perimeters...";
  1315. // merge slices if they were split into types
  1316. if (this->typed_slices) {
  1317. FOREACH_LAYER(this, layer_it)
  1318. (*layer_it)->merge_slices();
  1319. this->typed_slices = false;
  1320. this->state.invalidate(posPrepareInfill);
  1321. }
  1322. //if we want only one perimeter for each top surface, we need to know who is a top surface before the infill
  1323. // ! detect_surfaces_type is done is every case in the infill() call chain.
  1324. if(_print->default_region_config.only_one_perimeter_top){
  1325. // This will assign a type (top/bottom/internal) to $layerm->slices.
  1326. // Then the classifcation of $layerm->slices is transfered onto
  1327. // the $layerm->fill_surfaces by clipping $layerm->fill_surfaces
  1328. // by the cummulative area of the previous $layerm->fill_surfaces.
  1329. this->detect_surfaces_type();
  1330. }
  1331. // compare each layer to the one below, and mark those slices needing
  1332. // one additional inner perimeter, like the top of domed objects-
  1333. // this algorithm makes sure that at least one perimeter is overlapping
  1334. // but we don't generate any extra perimeter if fill density is zero, as they would be floating
  1335. // inside the object - infill_only_where_needed should be the method of choice for printing
  1336. // hollow objects
  1337. FOREACH_REGION(this->_print, region_it) {
  1338. size_t region_id = region_it - this->_print->regions.begin();
  1339. const PrintRegion &region = **region_it;
  1340. if (!region.config.extra_perimeters
  1341. || region.config.perimeters == 0
  1342. || region.config.fill_density == 0
  1343. || this->layer_count() < 2)
  1344. continue;
  1345. BOOST_LOG_TRIVIAL(debug) << "Generating extra perimeters for region " << region_id << " in parallel - start";
  1346. tbb::parallel_for(
  1347. tbb::blocked_range<size_t>(0, this->layers.size() - 1),
  1348. [this, &region, region_id](const tbb::blocked_range<size_t>& range) {
  1349. for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) {
  1350. LayerRegion &layerm = *this->layers[layer_idx]->regions[region_id];
  1351. const LayerRegion &upper_layerm = *this->layers[layer_idx+1]->regions[region_id];
  1352. const Polygons upper_layerm_polygons = upper_layerm.slices;
  1353. // Filter upper layer polygons in intersection_ppl by their bounding boxes?
  1354. // my $upper_layerm_poly_bboxes= [ map $_->bounding_box, @{$upper_layerm_polygons} ];
  1355. const double total_loop_length = total_length(upper_layerm_polygons);
  1356. const coord_t perimeter_spacing = layerm.flow(frPerimeter).scaled_spacing();
  1357. const Flow ext_perimeter_flow = layerm.flow(frExternalPerimeter);
  1358. const coord_t ext_perimeter_width = ext_perimeter_flow.scaled_width();
  1359. const coord_t ext_perimeter_spacing = ext_perimeter_flow.scaled_spacing();
  1360. for (Surface &slice : layerm.slices.surfaces) {
  1361. for (;;) {
  1362. // compute the total thickness of perimeters
  1363. const coord_t perimeters_thickness = ext_perimeter_width/2 + ext_perimeter_spacing/2
  1364. + (region.config.perimeters-1 + slice.extra_perimeters) * perimeter_spacing;
  1365. // define a critical area where we don't want the upper slice to fall into
  1366. // (it should either lay over our perimeters or outside this area)
  1367. const coord_t critical_area_depth = coord_t(perimeter_spacing * 1.5);
  1368. const Polygons critical_area = diff(
  1369. offset(slice.expolygon, float(- perimeters_thickness)),
  1370. offset(slice.expolygon, float(- perimeters_thickness - critical_area_depth))
  1371. );
  1372. // check whether a portion of the upper slices falls inside the critical area
  1373. const Polylines intersection = intersection_pl(to_polylines(upper_layerm_polygons), critical_area);
  1374. // only add an additional loop if at least 30% of the slice loop would benefit from it
  1375. if (total_length(intersection) <= total_loop_length*0.3)
  1376. break;
  1377. /*
  1378. if (0) {
  1379. require "Slic3r/SVG.pm";
  1380. Slic3r::SVG::output(
  1381. "extra.svg",
  1382. no_arrows => 1,
  1383. expolygons => union_ex($critical_area),
  1384. polylines => [ map $_->split_at_first_point, map $_->p, @{$upper_layerm->slices} ],
  1385. );
  1386. }
  1387. */
  1388. ++ slice.extra_perimeters;
  1389. }
  1390. #ifdef DEBUG
  1391. if (slice.extra_perimeters > 0)
  1392. printf(" adding %d more perimeter(s) at layer %zu\n", slice.extra_perimeters, layer_idx);
  1393. #endif
  1394. }
  1395. }
  1396. });
  1397. BOOST_LOG_TRIVIAL(debug) << "Generating extra perimeters for region " << region_id << " in parallel - end";
  1398. }
  1399. BOOST_LOG_TRIVIAL(debug) << "Generating perimeters in parallel - start";
  1400. tbb::parallel_for(
  1401. tbb::blocked_range<size_t>(0, this->layers.size()),
  1402. [this](const tbb::blocked_range<size_t>& range) {
  1403. for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx)
  1404. this->layers[layer_idx]->make_perimeters();
  1405. }
  1406. );
  1407. BOOST_LOG_TRIVIAL(debug) << "Generating perimeters in parallel - end";
  1408. /*
  1409. simplify slices (both layer and region slices),
  1410. we only need the max resolution for perimeters
  1411. ### This makes this method not-idempotent, so we keep it disabled for now.
  1412. ###$self->_simplify_slices(&Slic3r::SCALED_RESOLUTION);
  1413. */
  1414. this->state.set_done(posPerimeters);
  1415. }
  1416. void PrintObject::_infill()
  1417. {
  1418. if (this->state.is_done(posInfill)) return;
  1419. this->state.set_started(posInfill);
  1420. BOOST_LOG_TRIVIAL(debug) << "Filling layers in parallel - start";
  1421. tbb::parallel_for(
  1422. tbb::blocked_range<size_t>(0, this->layers.size()),
  1423. [this](const tbb::blocked_range<size_t>& range) {
  1424. for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx)
  1425. this->layers[layer_idx]->make_fills();
  1426. }
  1427. );
  1428. BOOST_LOG_TRIVIAL(debug) << "Filling layers in parallel - end";
  1429. /* we could free memory now, but this would make this step not idempotent
  1430. ### $_->fill_surfaces->clear for map @{$_->regions}, @{$object->layers};
  1431. */
  1432. this->state.set_done(posInfill);
  1433. }
  1434. // Only active if config->infill_only_where_needed. This step trims the sparse infill,
  1435. // so it acts as an internal support. It maintains all other infill types intact.
  1436. // Here the internal surfaces and perimeters have to be supported by the sparse infill.
  1437. //FIXME The surfaces are supported by a sparse infill, but the sparse infill is only as large as the area to support.
  1438. // Likely the sparse infill will not be anchored correctly, so it will not work as intended.
  1439. // Also one wishes the perimeters to be supported by a full infill.
  1440. // Idempotence of this method is guaranteed by the fact that we don't remove things from
  1441. // fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
  1442. void PrintObject::clip_fill_surfaces()
  1443. {
  1444. if (! this->config.infill_only_where_needed.value ||
  1445. ! std::any_of(this->print()->regions.begin(), this->print()->regions.end(),
  1446. [](const PrintRegion *region) { return region->config.fill_density > 0; }))
  1447. return;
  1448. // We only want infill under ceilings; this is almost like an
  1449. // internal support material.
  1450. // Proceed top-down, skipping the bottom layer.
  1451. Polygons upper_internal;
  1452. for (int layer_id = int(this->layers.size()) - 1; layer_id > 0; -- layer_id) {
  1453. Layer *layer = this->layers[layer_id];
  1454. Layer *lower_layer = this->layers[layer_id - 1];
  1455. // Detect things that we need to support.
  1456. // Cummulative slices.
  1457. Polygons slices;
  1458. for (const ExPolygon &expoly : layer->slices.expolygons)
  1459. polygons_append(slices, to_polygons(expoly));
  1460. // Cummulative fill surfaces.
  1461. Polygons fill_surfaces;
  1462. // Solid surfaces to be supported.
  1463. Polygons overhangs;
  1464. for (const LayerRegion *layerm : layer->regions)
  1465. for (const Surface &surface : layerm->fill_surfaces.surfaces) {
  1466. Polygons polygons = to_polygons(surface.expolygon);
  1467. if (surface.is_solid())
  1468. polygons_append(overhangs, polygons);
  1469. polygons_append(fill_surfaces, std::move(polygons));
  1470. }
  1471. Polygons lower_layer_fill_surfaces;
  1472. Polygons lower_layer_internal_surfaces;
  1473. for (const LayerRegion *layerm : lower_layer->regions)
  1474. for (const Surface &surface : layerm->fill_surfaces.surfaces) {
  1475. Polygons polygons = to_polygons(surface.expolygon);
  1476. if (surface.surface_type == stInternal || surface.surface_type == stInternalVoid)
  1477. polygons_append(lower_layer_internal_surfaces, polygons);
  1478. polygons_append(lower_layer_fill_surfaces, std::move(polygons));
  1479. }
  1480. // We also need to support perimeters when there's at least one full unsupported loop
  1481. {
  1482. // Get perimeters area as the difference between slices and fill_surfaces
  1483. // Only consider the area that is not supported by lower perimeters
  1484. Polygons perimeters = intersection(diff(slices, fill_surfaces), lower_layer_fill_surfaces);
  1485. // Only consider perimeter areas that are at least one extrusion width thick.
  1486. //FIXME Offset2 eats out from both sides, while the perimeters are create outside in.
  1487. //Should the pw not be half of the current value?
  1488. float pw = FLT_MAX;
  1489. for (const LayerRegion *layerm : layer->regions)
  1490. pw = std::min<float>(pw, layerm->flow(frPerimeter).scaled_width());
  1491. // Append such thick perimeters to the areas that need support
  1492. polygons_append(overhangs, offset2(perimeters, -pw, +pw));
  1493. }
  1494. // Find new internal infill.
  1495. polygons_append(overhangs, std::move(upper_internal));
  1496. upper_internal = intersection(overhangs, lower_layer_internal_surfaces);
  1497. // Apply new internal infill to regions.
  1498. for (LayerRegion *layerm : lower_layer->regions) {
  1499. if (layerm->region()->config.fill_density.value == 0)
  1500. continue;
  1501. SurfaceType internal_surface_types[] = { stInternal, stInternalVoid };
  1502. Polygons internal;
  1503. for (Surface &surface : layerm->fill_surfaces.surfaces)
  1504. if (surface.surface_type == stInternal || surface.surface_type == stInternalVoid)
  1505. polygons_append(internal, std::move(surface.expolygon));
  1506. layerm->fill_surfaces.remove_types(internal_surface_types, 2);
  1507. layerm->fill_surfaces.append(intersection_ex(internal, upper_internal, true), stInternal);
  1508. layerm->fill_surfaces.append(diff_ex (internal, upper_internal, true), stInternalVoid);
  1509. // If there are voids it means that our internal infill is not adjacent to
  1510. // perimeters. In this case it would be nice to add a loop around infill to
  1511. // make it more robust and nicer. TODO.
  1512. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  1513. layerm->export_region_fill_surfaces_to_svg_debug("6_clip_fill_surfaces");
  1514. #endif
  1515. }
  1516. }
  1517. }
  1518. void PrintObject::discover_horizontal_shells()
  1519. {
  1520. BOOST_LOG_TRIVIAL(trace) << "discover_horizontal_shells()";
  1521. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  1522. for (int i = 0; i < int(this->layers.size()); ++ i) {
  1523. LayerRegion *layerm = this->layers[i]->regions[region_id];
  1524. PrintRegionConfig &region_config = layerm->region()->config;
  1525. if (region_config.solid_infill_every_layers.value > 0 && region_config.fill_density.value > 0 &&
  1526. (i % region_config.solid_infill_every_layers) == 0) {
  1527. // Insert a solid internal layer. Mark stInternal surfaces as stInternalSolid or stInternalBridge.
  1528. SurfaceType type = (region_config.fill_density == 100) ? stInternalSolid : stInternalBridge;
  1529. for (Surface &surface : layerm->fill_surfaces.surfaces)
  1530. if (surface.surface_type == stInternal)
  1531. surface.surface_type = type;
  1532. }
  1533. // If ensure_vertical_shell_thickness, then the rest has already been performed by discover_vertical_shells().
  1534. if (region_config.ensure_vertical_shell_thickness.value)
  1535. continue;
  1536. for (int idx_surface_type = 0; idx_surface_type < 3; ++ idx_surface_type) {
  1537. SurfaceType type = (idx_surface_type == 0) ? stTop : (idx_surface_type == 1) ? stBottom : stBottomBridge;
  1538. // Find slices of current type for current layer.
  1539. // Use slices instead of fill_surfaces, because they also include the perimeter area,
  1540. // which needs to be propagated in shells; we need to grow slices like we did for
  1541. // fill_surfaces though. Using both ungrown slices and grown fill_surfaces will
  1542. // not work in some situations, as there won't be any grown region in the perimeter
  1543. // area (this was seen in a model where the top layer had one extra perimeter, thus
  1544. // its fill_surfaces were thinner than the lower layer's infill), however it's the best
  1545. // solution so far. Growing the external slices by EXTERNAL_INFILL_MARGIN will put
  1546. // too much solid infill inside nearly-vertical slopes.
  1547. // Surfaces including the area of perimeters. Everything, that is visible from the top / bottom
  1548. // (not covered by a layer above / below).
  1549. // This does not contain the areas covered by perimeters!
  1550. Polygons solid;
  1551. for (const Surface &surface : layerm->slices.surfaces)
  1552. if (surface.surface_type == type)
  1553. polygons_append(solid, to_polygons(surface.expolygon));
  1554. // Infill areas (slices without the perimeters).
  1555. for (const Surface &surface : layerm->fill_surfaces.surfaces)
  1556. if (surface.surface_type == type)
  1557. polygons_append(solid, to_polygons(surface.expolygon));
  1558. if (solid.empty())
  1559. continue;
  1560. // Slic3r::debugf "Layer %d has %s surfaces\n", $i, ($type == S_TYPE_TOP) ? 'top' : 'bottom';
  1561. size_t solid_layers = (type == stTop) ? region_config.top_solid_layers.value : region_config.bottom_solid_layers.value;
  1562. for (int n = (type == stTop) ? i-1 : i+1; std::abs(n - i) < solid_layers; (type == stTop) ? -- n : ++ n) {
  1563. if (n < 0 || n >= int(this->layers.size()))
  1564. continue;
  1565. // Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
  1566. // Reference to the lower layer of a TOP surface, or an upper layer of a BOTTOM surface.
  1567. LayerRegion *neighbor_layerm = this->layers[n]->regions[region_id];
  1568. // find intersection between neighbor and current layer's surfaces
  1569. // intersections have contours and holes
  1570. // we update $solid so that we limit the next neighbor layer to the areas that were
  1571. // found on this one - in other words, solid shells on one layer (for a given external surface)
  1572. // are always a subset of the shells found on the previous shell layer
  1573. // this approach allows for DWIM in hollow sloping vases, where we want bottom
  1574. // shells to be generated in the base but not in the walls (where there are many
  1575. // narrow bottom surfaces): reassigning $solid will consider the 'shadow' of the
  1576. // upper perimeter as an obstacle and shell will not be propagated to more upper layers
  1577. //FIXME How does it work for S_TYPE_INTERNALBRIDGE? This is set for sparse infill. Likely this does not work.
  1578. Polygons new_internal_solid;
  1579. {
  1580. Polygons internal;
  1581. for (const Surface &surface : neighbor_layerm->fill_surfaces.surfaces)
  1582. if (surface.surface_type == stInternal || surface.surface_type == stInternalSolid)
  1583. polygons_append(internal, to_polygons(surface.expolygon));
  1584. new_internal_solid = intersection(solid, internal, true);
  1585. }
  1586. if (new_internal_solid.empty()) {
  1587. // No internal solid needed on this layer. In order to decide whether to continue
  1588. // searching on the next neighbor (thus enforcing the configured number of solid
  1589. // layers, use different strategies according to configured infill density:
  1590. if (region_config.fill_density.value == 0) {
  1591. // If user expects the object to be void (for example a hollow sloping vase),
  1592. // don't continue the search. In this case, we only generate the external solid
  1593. // shell if the object would otherwise show a hole (gap between perimeters of
  1594. // the two layers), and internal solid shells are a subset of the shells found
  1595. // on each previous layer.
  1596. goto EXTERNAL;
  1597. } else {
  1598. // If we have internal infill, we can generate internal solid shells freely.
  1599. continue;
  1600. }
  1601. }
  1602. if (region_config.fill_density.value == 0) {
  1603. // if we're printing a hollow object we discard any solid shell thinner
  1604. // than a perimeter width, since it's probably just crossing a sloping wall
  1605. // and it's not wanted in a hollow print even if it would make sense when
  1606. // obeying the solid shell count option strictly (DWIM!)
  1607. float margin = float(neighbor_layerm->flow(frExternalPerimeter).scaled_width());
  1608. Polygons too_narrow = diff(
  1609. new_internal_solid,
  1610. offset2(new_internal_solid, -margin, +margin, jtMiter, 5),
  1611. true);
  1612. // Trim the regularized region by the original region.
  1613. if (! too_narrow.empty())
  1614. new_internal_solid = solid = diff(new_internal_solid, too_narrow);
  1615. }
  1616. // make sure the new internal solid is wide enough, as it might get collapsed
  1617. // when spacing is added in Fill.pm
  1618. {
  1619. //FIXME Vojtech: Disable this and you will be sorry.
  1620. // https://github.com/prusa3d/Slic3r/issues/26 bottom
  1621. float margin = 3.f * layerm->flow(frSolidInfill).scaled_width(); // require at least this size
  1622. // we use a higher miterLimit here to handle areas with acute angles
  1623. // in those cases, the default miterLimit would cut the corner and we'd
  1624. // get a triangle in $too_narrow; if we grow it below then the shell
  1625. // would have a different shape from the external surface and we'd still
  1626. // have the same angle, so the next shell would be grown even more and so on.
  1627. Polygons too_narrow = diff(
  1628. new_internal_solid,
  1629. offset2(new_internal_solid, -margin, +margin, ClipperLib::jtMiter, 5),
  1630. true);
  1631. if (! too_narrow.empty()) {
  1632. // grow the collapsing parts and add the extra area to the neighbor layer
  1633. // as well as to our original surfaces so that we support this
  1634. // additional area in the next shell too
  1635. // make sure our grown surfaces don't exceed the fill area
  1636. Polygons internal;
  1637. for (const Surface &surface : neighbor_layerm->fill_surfaces.surfaces)
  1638. if (surface.is_internal() && !surface.is_bridge())
  1639. polygons_append(internal, to_polygons(surface.expolygon));
  1640. polygons_append(new_internal_solid,
  1641. intersection(
  1642. offset(too_narrow, +margin),
  1643. // Discard bridges as they are grown for anchoring and we can't
  1644. // remove such anchors. (This may happen when a bridge is being
  1645. // anchored onto a wall where little space remains after the bridge
  1646. // is grown, and that little space is an internal solid shell so
  1647. // it triggers this too_narrow logic.)
  1648. internal));
  1649. solid = new_internal_solid;
  1650. }
  1651. }
  1652. // internal-solid are the union of the existing internal-solid surfaces
  1653. // and new ones
  1654. SurfaceCollection backup = std::move(neighbor_layerm->fill_surfaces);
  1655. polygons_append(new_internal_solid, to_polygons(backup.filter_by_type(stInternalSolid)));
  1656. ExPolygons internal_solid = union_ex(new_internal_solid, false);
  1657. // assign new internal-solid surfaces to layer
  1658. neighbor_layerm->fill_surfaces.set(internal_solid, stInternalSolid);
  1659. // subtract intersections from layer surfaces to get resulting internal surfaces
  1660. Polygons polygons_internal = to_polygons(std::move(internal_solid));
  1661. ExPolygons internal = diff_ex(
  1662. to_polygons(backup.filter_by_type(stInternal)),
  1663. polygons_internal,
  1664. true);
  1665. // assign resulting internal surfaces to layer
  1666. neighbor_layerm->fill_surfaces.append(internal, stInternal);
  1667. polygons_append(polygons_internal, to_polygons(std::move(internal)));
  1668. // assign top and bottom surfaces to layer
  1669. SurfaceType surface_types_solid[] = { stTop, stBottom, stBottomBridge };
  1670. backup.keep_types(surface_types_solid, 3);
  1671. std::vector<SurfacesPtr> top_bottom_groups;
  1672. backup.group(&top_bottom_groups);
  1673. for (SurfacesPtr &group : top_bottom_groups)
  1674. neighbor_layerm->fill_surfaces.append(
  1675. diff_ex(to_polygons(group), polygons_internal),
  1676. // Use an existing surface as a template, it carries the bridge angle etc.
  1677. *group.front());
  1678. }
  1679. EXTERNAL:;
  1680. } // foreach type (stTop, stBottom, stBottomBridge)
  1681. } // for each layer
  1682. } // for each region
  1683. #ifdef SLIC3R_DEBUG_SLICE_PROCESSING
  1684. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  1685. for (const Layer *layer : this->layers) {
  1686. const LayerRegion *layerm = layer->regions[region_id];
  1687. layerm->export_region_slices_to_svg_debug("5_discover_horizontal_shells");
  1688. layerm->export_region_fill_surfaces_to_svg_debug("5_discover_horizontal_shells");
  1689. } // for each layer
  1690. } // for each region
  1691. #endif /* SLIC3R_DEBUG_SLICE_PROCESSING */
  1692. }
  1693. // combine fill surfaces across layers to honor the "infill every N layers" option
  1694. // Idempotence of this method is guaranteed by the fact that we don't remove things from
  1695. // fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
  1696. void PrintObject::combine_infill()
  1697. {
  1698. // Work on each region separately.
  1699. for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) {
  1700. const PrintRegion *region = this->print()->regions[region_id];
  1701. const int every = region->config.infill_every_layers.value;
  1702. if (every < 2 || region->config.fill_density == 0.)
  1703. continue;
  1704. // Limit the number of combined layers to the maximum height allowed by this regions' nozzle.
  1705. //FIXME limit the layer height to max_layer_height
  1706. double nozzle_diameter = std::min(
  1707. this->print()->config.nozzle_diameter.get_at(region->config.infill_extruder.value - 1),
  1708. this->print()->config.nozzle_diameter.get_at(region->config.solid_infill_extruder.value - 1));
  1709. // define the combinations
  1710. std::vector<size_t> combine(this->layers.size(), 0);
  1711. {
  1712. double current_height = 0.;
  1713. size_t num_layers = 0;
  1714. for (size_t layer_idx = 0; layer_idx < this->layers.size(); ++ layer_idx) {
  1715. const Layer *layer = this->layers[layer_idx];
  1716. if (layer->id() == 0)
  1717. // Skip first print layer (which may not be first layer in array because of raft).
  1718. continue;
  1719. // Check whether the combination of this layer with the lower layers' buffer
  1720. // would exceed max layer height or max combined layer count.
  1721. if (current_height + layer->height >= nozzle_diameter + EPSILON || num_layers >= every) {
  1722. // Append combination to lower layer.
  1723. combine[layer_idx - 1] = num_layers;
  1724. current_height = 0.;
  1725. num_layers = 0;
  1726. }
  1727. current_height += layer->height;
  1728. ++ num_layers;
  1729. }
  1730. // Append lower layers (if any) to uppermost layer.
  1731. combine[this->layers.size() - 1] = num_layers;
  1732. }
  1733. // loop through layers to which we have assigned layers to combine
  1734. for (size_t layer_idx = 0; layer_idx < this->layers.size(); ++ layer_idx) {
  1735. size_t num_layers = combine[layer_idx];
  1736. if (num_layers <= 1)
  1737. continue;
  1738. // Get all the LayerRegion objects to be combined.
  1739. std::vector<LayerRegion*> layerms;
  1740. layerms.reserve(num_layers);
  1741. for (size_t i = layer_idx + 1 - num_layers; i <= layer_idx; ++ i)
  1742. layerms.emplace_back(this->layers[i]->regions[region_id]);
  1743. // We need to perform a multi-layer intersection, so let's split it in pairs.
  1744. // Initialize the intersection with the candidates of the lowest layer.
  1745. ExPolygons intersection = to_expolygons(layerms.front()->fill_surfaces.filter_by_type(stInternal));
  1746. // Start looping from the second layer and intersect the current intersection with it.
  1747. for (size_t i = 1; i < layerms.size(); ++ i)
  1748. intersection = intersection_ex(
  1749. to_polygons(intersection),
  1750. to_polygons(layerms[i]->fill_surfaces.filter_by_type(stInternal)),
  1751. false);
  1752. double area_threshold = layerms.front()->infill_area_threshold();
  1753. if (! intersection.empty() && area_threshold > 0.)
  1754. intersection.erase(std::remove_if(intersection.begin(), intersection.end(),
  1755. [area_threshold](const ExPolygon &expoly) { return expoly.area() <= area_threshold; }),
  1756. intersection.end());
  1757. if (intersection.empty())
  1758. continue;
  1759. // Slic3r::debugf " combining %d %s regions from layers %d-%d\n",
  1760. // scalar(@$intersection),
  1761. // ($type == S_TYPE_INTERNAL ? 'internal' : 'internal-solid'),
  1762. // $layer_idx-($every-1), $layer_idx;
  1763. // intersection now contains the regions that can be combined across the full amount of layers,
  1764. // so let's remove those areas from all layers.
  1765. Polygons intersection_with_clearance;
  1766. intersection_with_clearance.reserve(intersection.size());
  1767. float clearance_offset =
  1768. 0.5f * layerms.back()->flow(frPerimeter).scaled_width() +
  1769. // Because fill areas for rectilinear and honeycomb are grown
  1770. // later to overlap perimeters, we need to counteract that too.
  1771. ((region->config.fill_pattern == ipRectilinear ||
  1772. region->config.fill_pattern == ipGrid ||
  1773. region->config.fill_pattern == ipLine ||
  1774. region->config.fill_pattern == ipHoneycomb) ? 1.5f : 0.5f) *
  1775. layerms.back()->flow(frSolidInfill).scaled_width();
  1776. for (ExPolygon &expoly : intersection)
  1777. polygons_append(intersection_with_clearance, offset(expoly, clearance_offset));
  1778. for (LayerRegion *layerm : layerms) {
  1779. Polygons internal = to_polygons(layerm->fill_surfaces.filter_by_type(stInternal));
  1780. layerm->fill_surfaces.remove_type(stInternal);
  1781. layerm->fill_surfaces.append(diff_ex(internal, intersection_with_clearance, false), stInternal);
  1782. if (layerm == layerms.back()) {
  1783. // Apply surfaces back with adjusted depth to the uppermost layer.
  1784. Surface templ(stInternal, ExPolygon());
  1785. templ.thickness = 0.;
  1786. for (LayerRegion *layerm2 : layerms)
  1787. templ.thickness += layerm2->layer()->height;
  1788. templ.thickness_layers = (unsigned short)layerms.size();
  1789. layerm->fill_surfaces.append(intersection, templ);
  1790. } else {
  1791. // Save void surfaces.
  1792. layerm->fill_surfaces.append(
  1793. intersection_ex(internal, intersection_with_clearance, false),
  1794. stInternalVoid);
  1795. }
  1796. }
  1797. }
  1798. }
  1799. }
  1800. void PrintObject::_generate_support_material()
  1801. {
  1802. PrintObjectSupportMaterial support_material(this, PrintObject::slicing_parameters());
  1803. support_material.generate(*this);
  1804. }
  1805. void PrintObject::reset_layer_height_profile()
  1806. {
  1807. // Reset the layer_heigth_profile.
  1808. this->layer_height_profile.clear();
  1809. this->layer_height_profile_valid = false;
  1810. // Reset the source layer_height_profile if it exists at the ModelObject.
  1811. this->model_object()->layer_height_profile.clear();
  1812. this->model_object()->layer_height_profile_valid = false;
  1813. }
  1814. } // namespace Slic3r