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
|
#include <stdio.h>
#include "../ClipperUtils.hpp"
#include "../Surface.hpp"
#include "../PrintConfig.hpp"
#include "../ExtrusionEntityCollection.hpp"
#include "FillBase.hpp"
#include "FillConcentric.hpp"
#include "FillHoneycomb.hpp"
#include "Fill3DHoneycomb.hpp"
#include "FillGyroid.hpp"
#include "FillPlanePath.hpp"
#include "FillRectilinear.hpp"
#include "FillRectilinear2.hpp"
#include "FillRectilinear3.hpp"
#include "FillSmooth.hpp"
namespace Slic3r {
Fill* Fill::new_from_type(const InfillPattern type)
{
switch (type) {
case ipConcentric: return new FillConcentric();
case ipConcentricGapFill: return new FillConcentricWGapFill();
case ipHoneycomb: return new FillHoneycomb();
case ip3DHoneycomb: return new Fill3DHoneycomb();
case ipGyroid: return new FillGyroid();
case ipRectilinear: return new FillRectilinear2();
// case ipRectilinear: return new FillRectilinear();
case ipLine: return new FillLine();
case ipGrid: return new FillGrid2();
case ipTriangles: return new FillTriangles();
case ipStars: return new FillStars();
case ipCubic: return new FillCubic();
// case ipGrid: return new FillGrid();
case ipArchimedeanChords: return new FillArchimedeanChords();
case ipHilbertCurve: return new FillHilbertCurve();
case ipOctagramSpiral: return new FillOctagramSpiral();
case ipSmooth: return new FillSmooth();
case ipSmoothTriple: return new FillSmoothTriple();
case ipSmoothHilbert: return new FillSmoothHilbert();
case ipRectiWithPerimeter: return new FillRectilinear2Peri();
default: throw std::invalid_argument("unknown type");
}
}
Fill* Fill::new_from_type(const std::string &type)
{
const t_config_enum_values &enum_keys_map = ConfigOptionEnum<InfillPattern>::get_enum_values();
t_config_enum_values::const_iterator it = enum_keys_map.find(type);
return (it == enum_keys_map.end()) ? nullptr : new_from_type(InfillPattern(it->second));
}
Polylines Fill::fill_surface(const Surface *surface, const FillParams ¶ms)
{
// Perform offset.
Slic3r::ExPolygons expp = offset_ex(surface->expolygon, float(scale_(0 - 0.5 * this->spacing)));
// Create the infills for each of the regions.
Polylines polylines_out;
for (size_t i = 0; i < expp.size(); ++ i)
_fill_surface_single(
params,
surface->thickness_layers,
_infill_direction(surface),
expp[i],
polylines_out);
return polylines_out;
}
// Calculate a new spacing to fill width with possibly integer number of lines,
// the first and last line being centered at the interval ends.
// This function possibly increases the spacing, never decreases,
// and for a narrow width the increase in spacing may become severe,
// therefore the adjustment is limited to 20% increase.
coord_t Fill::_adjust_solid_spacing(const coord_t width, const coord_t distance)
{
assert(width >= 0);
assert(distance > 0);
// floor(width / distance)
coord_t number_of_intervals = (coord_t)((width - EPSILON) / distance);
coord_t distance_new = (number_of_intervals == 0) ?
distance :
(coord_t)(((width - EPSILON) / number_of_intervals));
const coordf_t factor = coordf_t(distance_new) / coordf_t(distance);
assert(factor > 1. - 1e-5);
// How much could the extrusion width be increased? By 20%.
const coordf_t factor_max = 1.2;
if (factor > factor_max)
distance_new = coord_t(floor((coordf_t(distance) * factor_max + 0.5)));
return distance_new;
}
// Returns orientation of the infill and the reference point of the infill pattern.
// For a normal print, the reference point is the center of a bounding box of the STL.
std::pair<float, Point> Fill::_infill_direction(const Surface *surface) const
{
// set infill angle
float out_angle = this->angle;
if (out_angle == FLT_MAX) {
//FIXME Vojtech: Add a warning?
printf("Using undefined infill angle\n");
out_angle = 0.f;
}
// Bounding box is the bounding box of a perl object Slic3r::Print::Object (c++ object Slic3r::PrintObject)
// The bounding box is only undefined in unit tests.
Point out_shift = empty(this->bounding_box) ?
surface->expolygon.contour.bounding_box().center() :
this->bounding_box.center();
#if 0
if (empty(this->bounding_box)) {
printf("Fill::_infill_direction: empty bounding box!");
} else {
printf("Fill::_infill_direction: reference point %d, %d\n", out_shift.x, out_shift.y);
}
#endif
if (surface->bridge_angle >= 0) {
// use bridge angle
//FIXME Vojtech: Add a debugf?
// Slic3r::debugf "Filling bridge with angle %d\n", rad2deg($surface->bridge_angle);
#ifdef SLIC3R_DEBUG
printf("Filling bridge with angle %f\n", surface->bridge_angle);
#endif /* SLIC3R_DEBUG */
out_angle = (float)(surface->bridge_angle);
} else if (this->layer_id != size_t(-1)) {
// alternate fill direction
out_angle += this->_layer_angle(this->layer_id / surface->thickness_layers);
} else {
// printf("Layer_ID undefined!\n");
}
out_angle += float(M_PI/2.);
return std::pair<float, Point>(out_angle, out_shift);
}
void Fill::fill_surface_extrusion(const Surface *surface, const FillParams ¶ms, const Flow &flow, const ExtrusionRole &role, ExtrusionEntitiesPtr &out) {
//add overlap & call fill_surface
Polylines polylines = this->fill_surface(surface, params);
if (polylines.empty())
return;
// ensure it doesn't over or under-extrude
double multFlow = 1;
if (!params.dont_adjust && params.full_infill() && !flow.bridge && params.fill_exactly){
// compute the path of the nozzle -> extruded volume
double lengthTot = 0;
int nbLines = 0;
for (auto pline = polylines.begin(); pline != polylines.end(); ++pline){
Lines lines = pline->lines();
for (auto line = lines.begin(); line != lines.end(); ++line){
lengthTot += unscaled(line->length());
nbLines++;
}
}
double extrudedVolume = flow.mm3_per_mm() * lengthTot;
// compute real volume
double poylineVolume = 0;
for (auto poly = this->no_overlap_expolygons.begin(); poly != this->no_overlap_expolygons.end(); ++poly) {
poylineVolume += flow.height*unscaled(unscaled(poly->area()));
// add external "perimeter gap"
double perimeterRoundGap = unscaled(poly->contour.length()) * flow.height * (1 - 0.25*PI) * 0.5;
// add holes "perimeter gaps"
double holesGaps = 0;
for (auto hole = poly->holes.begin(); hole != poly->holes.end(); ++hole) {
holesGaps += unscaled(hole->length()) * flow.height * (1 - 0.25*PI) * 0.5;
}
poylineVolume += perimeterRoundGap + holesGaps;
}
//printf("process want %f mm3 extruded for a volume of %f space : we mult by %f %i\n",
// extrudedVolume,
// (poylineVolume),
// (poylineVolume) / extrudedVolume,
// this->no_overlap_expolygons.size());
if (extrudedVolume != 0 && poylineVolume != 0) multFlow = poylineVolume / extrudedVolume;
//failsafe, it can happen
if (multFlow > 1.3) multFlow = 1.3;
if (multFlow < 0.8) multFlow = 0.8;
}
// Save into layer.
auto *eec = new ExtrusionEntityCollection();
/// pass the no_sort attribute to the extrusion path
eec->no_sort = this->no_sort();
/// add it into the collection
out.push_back(eec);
//get the role
ExtrusionRole good_role = role;
if (good_role == erNone || good_role == erCustom) {
good_role = (flow.bridge ? erBridgeInfill :
(surface->is_solid() ?
((surface->is_top()) ? erTopSolidInfill : erSolidInfill) :
erInternalInfill));
}
/// push the path
extrusion_entities_append_paths(
eec->entities, std::move(polylines),
good_role,
flow.mm3_per_mm() * params.flow_mult * multFlow,
flow.width * params.flow_mult * multFlow,
flow.height);
}
/// cut poly between poly.point[idx_1] & poly.point[idx_1+1]
/// add p1+-width to one part and p2+-width to the other one.
/// add the "new" polyline to polylines (to part cut from poly)
/// p1 & p2 have to be between poly.point[idx_1] & poly.point[idx_1+1]
/// if idx_1 is ==0 or == size-1, then we don't need to create a new polyline.
void cut_polyline(Polyline &poly, Polylines &polylines, size_t idx_1, Point p1, Point p2) {
//reorder points
if (p1.distance_to_square(poly.points[idx_1]) > p2.distance_to_square(poly.points[idx_1])) {
Point temp = p2;
p2 = p1;
p1 = temp;
}
if (idx_1 == 0) {
poly.points.insert(poly.points.begin(), p2);
} else if (idx_1 == poly.points.size() - 1) {
poly.points.push_back(p1);
} else {
// create new polyline
Polyline new_poly;
//put points in new_poly
new_poly.points.push_back(p2);
new_poly.points.insert(new_poly.points.end(), poly.points.begin() + idx_1 + 1, poly.points.end());
//erase&put points in poly
poly.points.erase(poly.points.begin() + idx_1 + 1, poly.points.end());
poly.points.push_back(p1);
polylines.emplace_back(new_poly);
}
}
/// the poly is like a polygon but with first_point != last_point (already removed)
void cut_polygon(Polyline &poly, size_t idx_1, Point p1, Point p2) {
//reorder points
if (p1.distance_to_square(poly.points[idx_1]) > p2.distance_to_square(poly.points[idx_1])) {
Point temp = p2;
p2 = p1;
p1 = temp;
}
//check if we need to rotate before cutting
if (idx_1 != poly.size() - 1) {
//put points in new_poly
poly.points.insert(poly.points.end(), poly.points.begin(), poly.points.begin() + idx_1 + 1);
poly.points.erase(poly.points.begin(), poly.points.begin() + idx_1 + 1);
}
//put points in poly
poly.points.push_back(p1);
poly.points.insert(poly.points.begin(), p2);
}
/// check if the polyline from pts_to_check may be at 'width' distance of a point in polylines_blocker
/// it use equally_spaced_points with width/2 precision, so don't worry with pts_to_check number of points.
/// it use the given polylines_blocker points, be sure to put enough of them to be reliable.
/// complexity : N(pts_to_check.equally_spaced_points(width / 2)) x N(polylines_blocker.points)
bool collision(const Points &pts_to_check, const Polylines &polylines_blocker, const coordf_t width) {
//check if it's not too close to a polyline
coordf_t min_dist = width * width * 0.9 - SCALED_EPSILON;
Polyline better_polylines(pts_to_check);
Points better_pts = better_polylines.equally_spaced_points(width / 2);
for (const Point &p : better_pts) {
for (const Polyline &poly2 : polylines_blocker) {
for (const Point &p2 : poly2.points) {
if (p.distance_to_square(p2) < min_dist) {
return true;
}
}
}
}
return false;
}
/// Try to find a path inside polylines that allow to go from p1 to p2.
/// width if the width of the extrusion
/// polylines_blockers are the array of polylines to check if the path isn't blocked by something.
/// complexity: N(polylines.points) + a collision check after that if we finded a path: N(2(p2-p1)/width) x N(polylines_blocker.points)
Points getFrontier(Polylines &polylines, const Point& p1, const Point& p2, const coord_t width, const Polylines &polylines_blockers) {
for (size_t idx_poly = 0; idx_poly < polylines.size(); ++idx_poly) {
Polyline &poly = polylines[idx_poly];
if (poly.size() <= 1) continue;
//loop?
if (poly.first_point() == poly.last_point()) {
//polygon : try to find a line for p1 & p2.
size_t idx_11, idx_12, idx_21, idx_22;
idx_11 = poly.closest_point_index(p1);
idx_12 = idx_11;
if (Line(poly.points[idx_11], poly.points[(idx_11 + 1) % (poly.points.size() - 1)]).distance_to(p1) < SCALED_EPSILON) {
idx_12 = (idx_11 + 1) % (poly.points.size() - 1);
} else if (Line(poly.points[(idx_11 > 0) ? (idx_11 - 1) : (poly.points.size() - 2)], poly.points[idx_11]).distance_to(p1) < SCALED_EPSILON) {
idx_11 = (idx_11 > 0) ? (idx_11 - 1) : (poly.points.size() - 2);
} else {
continue;
}
idx_21 = poly.closest_point_index(p2);
idx_22 = idx_21;
if (Line(poly.points[idx_21], poly.points[(idx_21 + 1) % (poly.points.size() - 1)]).distance_to(p2) < SCALED_EPSILON) {
idx_22 = (idx_21 + 1) % (poly.points.size() - 1);
} else if (Line(poly.points[(idx_21 > 0) ? (idx_21 - 1) : (poly.points.size() - 2)], poly.points[idx_21]).distance_to(p2) < SCALED_EPSILON) {
idx_21 = (idx_21 > 0) ? (idx_21 - 1) : (poly.points.size() - 2);
} else {
continue;
}
//edge case: on the same line
if (idx_11 == idx_21 && idx_12 == idx_22) {
if (collision(Points() = { p1, p2 }, polylines_blockers, width)) return Points();
//break loop
poly.points.erase(poly.points.end() - 1);
cut_polygon(poly, idx_11, p1, p2);
return Points() = { Line(p1, p2).midpoint() };
}
//compute distance & array for the ++ path
Points ret_1_to_2;
double dist_1_to_2 = p1.distance_to(poly.points[idx_12]);
ret_1_to_2.push_back(poly.points[idx_12]);
size_t max = idx_12 <= idx_21 ? idx_21 : poly.points.size() - 2;
for (size_t i = idx_12 + 1; i < max; i++) {
dist_1_to_2 += poly.points[i - 1].distance_to(poly.points[i]);
ret_1_to_2.push_back(poly.points[i]);
}
if (idx_12 > idx_21) {
dist_1_to_2 += poly.points.back().distance_to(poly.points.front());
ret_1_to_2.push_back(poly.points[0]);
for (size_t i = 1; i <= idx_21; i++) {
dist_1_to_2 += poly.points[i - 1].distance_to(poly.points[i]);
ret_1_to_2.push_back(poly.points[i]);
}
}
dist_1_to_2 += p2.distance_to(poly.points[idx_21]);
//compute distance & array for the -- path
Points ret_2_to_1;
double dist_2_to_1 = p1.distance_to(poly.points[idx_11]);
ret_2_to_1.push_back(poly.points[idx_11]);
size_t min = idx_22 <= idx_11 ? idx_22 : 0;
for (size_t i = idx_11; i > min; i--) {
dist_2_to_1 += poly.points[i - 1].distance_to(poly.points[i]);
ret_2_to_1.push_back(poly.points[i - 1]);
}
if (idx_22 > idx_11) {
dist_2_to_1 += poly.points.back().distance_to(poly.points.front());
ret_2_to_1.push_back(poly.points[poly.points.size() - 1]);
for (size_t i = poly.points.size() - 2; i > idx_22; i--) {
dist_2_to_1 += poly.points[i - 1].distance_to(poly.points[i]);
ret_2_to_1.push_back(poly.points[i - 1]);
}
}
dist_2_to_1 += p2.distance_to(poly.points[idx_22]);
//choose between the two direction (keep the short one)
if (dist_1_to_2 < dist_2_to_1) {
if (collision(ret_1_to_2, polylines_blockers, width)) return Points();
//break loop
poly.points.erase(poly.points.end() - 1);
//remove points
if (idx_12 <= idx_21) {
poly.points.erase(poly.points.begin() + idx_12, poly.points.begin() + idx_21 + 1);
cut_polygon(poly, idx_11, p1, p2);
} else {
poly.points.erase(poly.points.begin() + idx_12, poly.points.end());
poly.points.erase(poly.points.begin(), poly.points.begin() + idx_21);
cut_polygon(poly, poly.points.size() - 1, p1, p2);
}
return ret_1_to_2;
} else {
if (collision(ret_2_to_1, polylines_blockers, width)) return Points();
//break loop
poly.points.erase(poly.points.end() - 1);
//remove points
if (idx_22 <= idx_11) {
poly.points.erase(poly.points.begin() + idx_22, poly.points.begin() + idx_11 + 1);
cut_polygon(poly, idx_21, p1, p2);
} else {
poly.points.erase(poly.points.begin() + idx_22, poly.points.end());
poly.points.erase(poly.points.begin(), poly.points.begin() + idx_11);
cut_polygon(poly, poly.points.size() - 1, p1, p2);
}
return ret_2_to_1;
}
} else {
//polyline : try to find a line for p1 & p2.
size_t idx_1, idx_2;
idx_1 = poly.closest_point_index(p1);
if (idx_1 < poly.points.size() - 1 && Line(poly.points[idx_1], poly.points[idx_1 + 1]).distance_to(p1) < SCALED_EPSILON) {
} else if (idx_1 > 0 && Line(poly.points[idx_1 - 1], poly.points[idx_1]).distance_to(p1) < SCALED_EPSILON) {
idx_1 = idx_1 - 1;
} else {
continue;
}
idx_2 = poly.closest_point_index(p2);
if (idx_2 < poly.points.size() - 1 && Line(poly.points[idx_2], poly.points[idx_2 + 1]).distance_to(p2) < SCALED_EPSILON) {
} else if (idx_2 > 0 && Line(poly.points[idx_2 - 1], poly.points[idx_2]).distance_to(p2) < SCALED_EPSILON) {
idx_2 = idx_2 - 1;
} else {
continue;
}
//edge case: on the same line
if (idx_1 == idx_2) {
if (collision(Points() = { p1, p2 }, polylines_blockers, width)) return Points();
cut_polyline(poly, polylines, idx_1, p1, p2);
return Points() = { Line(p1, p2).midpoint() };
}
//create ret array
size_t first_idx = idx_1;
size_t last_idx = idx_2 + 1;
if (idx_1 > idx_2) {
first_idx = idx_2;
last_idx = idx_1 + 1;
}
Points p_ret;
p_ret.insert(p_ret.end(), poly.points.begin() + first_idx + 1, poly.points.begin() + last_idx);
if (collision(p_ret, polylines_blockers, width)) return Points();
//cut polyline
poly.points.erase(poly.points.begin() + first_idx + 1, poly.points.begin() + last_idx);
cut_polyline(poly, polylines, first_idx, p1, p2);
//order the returned array to be p1->p2
if (idx_1 > idx_2) {
std::reverse(p_ret.begin(), p_ret.end());
}
return p_ret;
}
}
return Points();
}
/// Connect the infill_ordered polylines, in this order, from the back point to the next front point.
/// It uses only the boundary polygons to do so, and can't pass two times at the same place.
/// It avoid passing over the infill_ordered's polylines (preventing local over-extrusion).
/// return the connected polylines in polylines_out. Can output polygons (stored as polylines with first_point = last_point).
/// complexity: worst: N(infill_ordered.points) x N(boundary.points)
/// typical: N(infill_ordered) x ( N(boundary.points) + N(infill_ordered.points) )
void Fill::connect_infill(const Polylines &infill_ordered, const ExPolygon &boundary, Polylines &polylines_out) {
//TODO: fallback to the quick & dirty old algorithm when n(points) is too high.
Polylines polylines_frontier = to_polylines(((Polygons)boundary));
Polylines polylines_blocker;
coord_t clip_size = scale_(this->spacing) * 2;
for (const Polyline &polyline : infill_ordered) {
if (polyline.length() > 1.8 * clip_size) {
polylines_blocker.push_back(polyline);
polylines_blocker.back().clip_end(clip_size);
polylines_blocker.back().clip_start(clip_size);
}
}
Polylines polylines_connected;
bool first = true;
for (const Polyline &polyline : infill_ordered) {
if (!first) {
// Try to connect the lines.
Points &pts_end = polylines_connected.back().points;
const Point &first_point = polyline.points.front();
const Point &last_point = pts_end.back();
Points pts = getFrontier(polylines_frontier, last_point, first_point, scale_(this->spacing), polylines_blocker);
if (!pts.empty()) {
pts_end.insert(pts_end.end(), pts.begin(), pts.end());
pts_end.insert(pts_end.end(), polyline.points.begin(), polyline.points.end());
continue;
}
}
// The lines cannot be connected.
polylines_connected.emplace_back(std::move(polyline));
first = false;
}
//try to create some loops if possible
for (Polyline &polyline : polylines_connected) {
Points pts = getFrontier(polylines_frontier, polyline.last_point(), polyline.first_point(), scale_(this->spacing), polylines_blocker);
if (!pts.empty()) {
polyline.points.insert(polyline.points.end(), pts.begin(), pts.end());
polyline.points.insert(polyline.points.begin(), polyline.points.back());
}
polylines_out.emplace_back(polyline);
}
}
} // namespace Slic3r
|