-
-
Notifications
You must be signed in to change notification settings - Fork 919
Expand file tree
/
Copy pathInfillOrderOptimizer.cpp
More file actions
559 lines (518 loc) · 19.2 KB
/
InfillOrderOptimizer.cpp
File metadata and controls
559 lines (518 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright (c) 2026 Ultimaker B.V.
// CuraEngine is released under the terms of the AGPLv3 or higher
#include "InfillOrderOptimizer.h"
#include <range/v3/algorithm/all_of.hpp>
#include <range/v3/algorithm/any_of.hpp>
#include <range/v3/algorithm/find_if.hpp>
#include <range/v3/algorithm/sort.hpp>
#include "LayerPlan.h"
#include "geometry/OpenLinesSet.h"
namespace cura
{
InfillOrderOptimizer::InfillOrderOptimizer()
{
}
void InfillOrderOptimizer::addPart(InfillPartArea part_area, OpenLinesSet& lines)
{
if (! lines.empty())
{
infill_parts_.push_back({ part_area, InfillPartType::Lines, { .lines = &lines } });
}
}
void InfillOrderOptimizer::addPart(InfillPartArea part_area, const Shape& polygons)
{
if (! polygons.empty())
{
infill_parts_.push_back({ part_area, InfillPartType::Polygons, { .polygons = &polygons } });
}
}
void InfillOrderOptimizer::addPart(InfillPartArea part_area, const std::vector<std::vector<VariableWidthLines>>& walls)
{
const bool has_walls = ranges::any_of(
walls,
[](const std::vector<VariableWidthLines>& tp)
{
return ! (
tp.empty()
|| ranges::all_of(
tp,
[](const VariableWidthLines& vwl)
{
return vwl.empty();
}));
});
if (has_walls)
{
infill_parts_.push_back({ part_area, InfillPartType::ExtrusionLines, { .extrusion_lines = &walls } });
}
}
void InfillOrderOptimizer::optimize(const bool skin_support_interlace_lines, const std::optional<Point2LL>& near_end_location)
{
if (infill_parts_.empty())
{
return;
}
// First order the parts in preferred order
ranges::sort(infill_parts_, &InfillOrderOptimizer::shouldPrintBefore);
if (! near_end_location.has_value())
{
// We can't really optimize the infill ordering, print it as is
return;
}
// Find the infill part that should be printed at last
auto closest_infill_part_iterator = infill_parts_.end();
std::optional<std::pair<size_t, size_t>> closest_line_point;
coord_t closest_distance_squared;
const auto walls_iterator = ranges::find_if(
infill_parts_,
[](const InfillPart& infill_part)
{
return infill_part.area == InfillPartArea::Infill && infill_part.type == InfillPartType::ExtrusionLines;
});
if (walls_iterator != infill_parts_.end())
{
// If we have walls, always print them at the end
closest_infill_part_iterator = walls_iterator;
}
else if (infill_parts_.size() > 1)
{
for (auto iterator = infill_parts_.begin(); iterator != infill_parts_.end(); ++iterator)
{
InfillPart& infill_part = *iterator;
switch (infill_part.type)
{
case InfillPartType::Lines:
if (skin_support_interlace_lines && infill_part.area == InfillPartArea::SkinSupport)
{
// Since we cannot order the skin support lines, don't account them for the global plan
break;
}
if (isCloserTo(*infill_part.paths.lines, *near_end_location, closest_line_point, closest_distance_squared))
{
closest_infill_part_iterator = iterator;
}
break;
case InfillPartType::Polygons:
if (isCloserTo(*infill_part.paths.polygons, *near_end_location, closest_line_point, closest_distance_squared))
{
closest_infill_part_iterator = iterator;
}
break;
case InfillPartType::ExtrusionLines:
assert(false && "We should never have extrusion lines at this points because having walls is filtered out above");
break;
}
}
}
if (closest_infill_part_iterator != infill_parts_.end() && closest_infill_part_iterator != std::prev(infill_parts_.end()))
{
// Move the part that is closest at the end of the list, if not already there
InfillPart closest_infill_part = *closest_infill_part_iterator;
infill_parts_.erase(closest_infill_part_iterator);
infill_parts_.push_back(closest_infill_part);
}
// If needed and possible, split the last part so that we provide an actual position to end close to the seam
InfillPart& last_part = infill_parts_.back();
if (last_part.type == InfillPartType::Lines && last_part.area == InfillPartArea::Infill)
{
if (! closest_line_point.has_value())
{
// Closest point was not previously calculated, maybe because there is only one element in the list, so do it now
isCloserTo(*last_part.paths.lines, *near_end_location, closest_line_point, closest_distance_squared);
}
if (closest_line_point.has_value())
{
last_part.paths.lines->split(closest_line_point->first, closest_line_point->second);
}
}
// Other parts don't require splitting: either polygons or skin support, which is only separate segments already
}
bool InfillOrderOptimizer::addToLayer(
LayerPlan& layer_plan,
const Settings& settings,
const std::optional<Point2LL>& near_end_location,
const EFillMethod infill_pattern,
const MeshPathConfigs& mesh_config,
const SliceDataStorage& storage,
const SliceMeshStorage& mesh,
const size_t extruder_nr,
const coord_t start_move_inwards_length,
const coord_t end_move_inwards_length,
const Shape& infill_inner_contour,
const coord_t skin_support_line_distance,
const Shape& infill_below_skin,
const AngleDegrees& skin_support_angle) const
{
if (infill_parts_.empty())
{
return false;
}
layer_plan.setIsInside(true); // going to print stuff inside print object
std::optional<Point2LL> near_start_location;
if (settings.get<InfillStartEndPreference>("infill_start_end_preference") == InfillStartEndPreference::START_RANDOM)
{
srand(layer_plan.getLayerNr());
const InfillPart& first_part = infill_parts_.front();
if (first_part.area == InfillPartArea::Infill)
{
switch (first_part.type)
{
case InfillPartType::Lines:
near_start_location = first_part.paths.lines->at(rand() % first_part.paths.lines->size())[0];
break;
case InfillPartType::Polygons:
{
const Polygon& start_poly = first_part.paths.polygons->at(rand() % first_part.paths.polygons->size());
near_start_location = start_poly[rand() % start_poly.size()];
break;
}
case InfillPartType::ExtrusionLines:
{
const std::vector<VariableWidthLines>* start_paths;
do
{
start_paths = &first_part.paths.extrusion_lines->at(rand() % first_part.paths.extrusion_lines->size());
} while (start_paths->empty() || start_paths->front().empty()); // We know for sure that one of them is not empty. So randomise until we hit
// it. Should almost always be very quick.
near_start_location = (*start_paths)[0][0].junctions_[0].p_;
break;
}
}
}
}
MixedLinesSet all_infill_lines;
for (const auto& [part_index, part] : infill_parts_ | ranges::views::enumerate)
{
const bool is_first = part_index == 0;
const bool is_last = part_index == infill_parts_.size() - 1;
bool reverse_print_direction = false;
std::optional<Point2LL> near_start_location_here;
if (is_last && near_end_location.has_value())
{
near_start_location_here = near_end_location;
reverse_print_direction = true;
}
else if (is_first)
{
near_start_location_here = near_start_location;
}
addToLayer(
part,
layer_plan,
settings,
near_start_location_here,
reverse_print_direction,
infill_pattern,
mesh_config,
storage,
mesh,
extruder_nr,
start_move_inwards_length,
end_move_inwards_length,
infill_inner_contour,
skin_support_line_distance,
infill_below_skin,
skin_support_angle);
if (part.area == InfillPartArea::Infill)
{
switch (part.type)
{
case InfillPartType::Lines:
all_infill_lines.push_back(*part.paths.lines);
break;
case InfillPartType::Polygons:
all_infill_lines.push_back(*part.paths.polygons);
break;
case InfillPartType::ExtrusionLines:
break;
}
}
}
layer_plan.setGeneratedInfillLines(&mesh, all_infill_lines);
return true;
}
void InfillOrderOptimizer::addToLayer(
const InfillPart& part,
LayerPlan& layer_plan,
const Settings& settings,
const std::optional<Point2LL>& near_start_location,
const bool reverse_print_direction,
const EFillMethod infill_pattern,
const MeshPathConfigs& mesh_config,
const SliceDataStorage& storage,
const SliceMeshStorage& mesh,
const size_t extruder_nr,
const coord_t start_move_inwards_length,
const coord_t end_move_inwards_length,
const Shape& infill_inner_contour,
const coord_t skin_support_line_distance,
const Shape& infill_below_skin,
const AngleDegrees& skin_support_angle) const
{
const bool enable_travel_optimization = settings.get<bool>("infill_enable_travel_optimization");
constexpr Ratio flow_ratio = 1.0_r;
constexpr double fan_speed = GCodePathConfig::FAN_SPEED_DEFAULT;
const std::unordered_multimap<const Polyline*, const Polyline*> order_requirements = PathOrderOptimizer<const Polyline*>::no_order_requirements_;
switch (part.area)
{
case InfillPartArea::Infill:
switch (part.type)
{
case InfillPartType::Lines:
{
addInfillLinesToLayer(
*part.paths.lines,
layer_plan,
settings,
near_start_location,
reverse_print_direction,
infill_pattern,
mesh_config,
start_move_inwards_length,
end_move_inwards_length,
infill_inner_contour,
enable_travel_optimization,
flow_ratio,
fan_speed,
order_requirements);
break;
}
case InfillPartType::Polygons:
{
OpenLinesSet remaining_lines;
layer_plan.addInfillPolygonsByOptimizer(
*part.paths.polygons,
remaining_lines,
mesh_config.infill_config[0],
settings,
start_move_inwards_length > 0 || end_move_inwards_length > 0,
near_start_location,
reverse_print_direction);
if (! remaining_lines.empty())
{
addInfillLinesToLayer(
remaining_lines,
layer_plan,
settings,
near_start_location,
reverse_print_direction,
infill_pattern,
mesh_config,
start_move_inwards_length,
end_move_inwards_length,
infill_inner_contour,
enable_travel_optimization,
flow_ratio,
fan_speed,
order_requirements);
}
break;
}
case InfillPartType::ExtrusionLines:
{
for (const std::vector<VariableWidthLines>& tool_paths : *part.paths.extrusion_lines)
{
constexpr coord_t wipe_dist = 0;
const ZSeamConfig z_seam_config(EZSeamType::USER_SPECIFIED, near_start_location.value_or(mesh.getZSeamHint()));
InsetOrderOptimizer wall_orderer(
storage,
layer_plan,
settings,
extruder_nr,
mesh_config.infill_config[0],
mesh_config.infill_config[0],
mesh_config.infill_config[0],
mesh_config.infill_config[0],
mesh_config.infill_config[0],
mesh_config.infill_config[0],
mesh_config.infill_config[0],
mesh_config.infill_config[0],
wipe_dist,
wipe_dist,
extruder_nr,
extruder_nr,
z_seam_config,
tool_paths,
mesh.bounding_box.flatten().getMiddle());
wall_orderer.addToLayer();
}
break;
}
}
break;
case InfillPartArea::SkinSupport:
{
switch (part.type)
{
case InfillPartType::Lines:
{
addSkinSupportLinesToLayer(
*part.paths.lines,
layer_plan,
settings,
near_start_location,
reverse_print_direction,
mesh_config,
skin_support_line_distance,
infill_below_skin,
skin_support_angle,
enable_travel_optimization,
flow_ratio);
break;
}
case InfillPartType::Polygons:
{
OpenLinesSet remaining_lines;
constexpr bool extra_inwards_move = false;
layer_plan.addInfillPolygonsByOptimizer(*part.paths.polygons, remaining_lines, mesh_config.skin_support_config, settings, extra_inwards_move, near_start_location);
if (! remaining_lines.empty())
{
addSkinSupportLinesToLayer(
remaining_lines,
layer_plan,
settings,
near_start_location,
reverse_print_direction,
mesh_config,
skin_support_line_distance,
infill_below_skin,
skin_support_angle,
enable_travel_optimization,
flow_ratio);
}
break;
}
}
break;
}
}
}
void InfillOrderOptimizer::addInfillLinesToLayer(
const OpenLinesSet& lines,
LayerPlan& layer_plan,
const Settings& settings,
const std::optional<Point2LL>& near_start_location,
const bool reverse_print_direction,
const EFillMethod infill_pattern,
const MeshPathConfigs& mesh_config,
const coord_t start_move_inwards_length,
const coord_t end_move_inwards_length,
const Shape& infill_inner_contour,
const bool enable_travel_optimization,
const Ratio& flow_ratio,
const double fan_speed,
const std::unordered_multimap<const Polyline*, const Polyline*>& order_requirements) const
{
SpaceFillType space_fill_type;
coord_t wipe_dist;
if (infill_pattern == EFillMethod::GRID || infill_pattern == EFillMethod::LINES || infill_pattern == EFillMethod::TRIANGLES || infill_pattern == EFillMethod::CUBIC
|| infill_pattern == EFillMethod::TETRAHEDRAL || infill_pattern == EFillMethod::QUARTER_CUBIC || infill_pattern == EFillMethod::CUBICSUBDIV
|| infill_pattern == EFillMethod::LIGHTNING)
{
space_fill_type = SpaceFillType::Lines;
wipe_dist = settings.get<coord_t>("infill_wipe_dist");
}
else
{
space_fill_type = (infill_pattern == EFillMethod::ZIG_ZAG) ? SpaceFillType::PolyLines : SpaceFillType::Lines;
wipe_dist = 0;
}
layer_plan.addLinesByOptimizer(
lines,
mesh_config.infill_config[0],
space_fill_type,
enable_travel_optimization,
wipe_dist,
flow_ratio,
near_start_location,
fan_speed,
reverse_print_direction,
order_requirements,
start_move_inwards_length,
end_move_inwards_length,
MendedShape(&settings, SectionType::INFILL, &infill_inner_contour));
}
void InfillOrderOptimizer::addSkinSupportLinesToLayer(
const OpenLinesSet& lines,
LayerPlan& layer_plan,
const Settings& settings,
const std::optional<Point2LL>& near_start_location,
const bool reverse_print_direction,
const MeshPathConfigs& mesh_config,
const coord_t skin_support_line_distance,
const Shape& infill_below_skin,
const AngleDegrees& skin_support_angle,
const bool enable_travel_optimization,
const Ratio& flow_ratio) const
{
const auto skin_support_fan_speed = settings.get<double>("skin_support_fan_speed");
constexpr SpaceFillType skin_support_space_fill_type = SpaceFillType::Lines;
constexpr coord_t skin_support_wipe_dist = 0;
const auto skin_support_interlace_lines = settings.get<bool>("skin_support_interlace_lines");
if (skin_support_interlace_lines)
{
const coord_t max_adjacent_distance = skin_support_line_distance * 1.1;
constexpr coord_t exclude_distance = 0;
constexpr bool skin_support_interlaced = true;
layer_plan.addLinesMonotonic(
infill_below_skin,
lines,
mesh_config.skin_support_config,
skin_support_space_fill_type,
skin_support_angle,
max_adjacent_distance,
exclude_distance,
skin_support_wipe_dist,
flow_ratio,
skin_support_fan_speed,
skin_support_interlaced);
}
else
{
layer_plan.addLinesByOptimizer(
lines,
mesh_config.skin_support_config,
skin_support_space_fill_type,
enable_travel_optimization,
skin_support_wipe_dist,
flow_ratio,
near_start_location,
skin_support_fan_speed,
reverse_print_direction);
}
}
bool InfillOrderOptimizer::shouldPrintBefore(const InfillPart& part1, const InfillPart& part2)
{
if (part1.area == part2.area)
{
return part1.type < part2.type;
}
else
{
return part1.area < part2.area;
}
}
template<class LinesSetType>
bool InfillOrderOptimizer::isCloserTo(
const LinesSetType& line_set,
const Point2LL& location,
std::optional<std::pair<size_t, size_t>>& closest_point,
coord_t& closest_distance_squared)
{
bool closer_found = false;
for (const auto& [line_index, line] : line_set | ranges::views::enumerate)
{
for (const auto& [point_index, point] : line | ranges::views::enumerate)
{
const coord_t distance_squared = vSize2(point - location);
if (! closest_point.has_value() || distance_squared < closest_distance_squared)
{
closer_found = true;
closest_point = std::make_optional(std::make_pair(line_index, point_index));
closest_distance_squared = distance_squared;
}
}
}
return closer_found;
}
} // namespace cura