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
|
#include "ExtrusionEntity.hpp"
#include "ExtrusionEntityCollection.hpp"
#include "ExPolygonCollection.hpp"
#include "ClipperUtils.hpp"
#include "Extruder.hpp"
#include "Flow.hpp"
#include <cmath>
#include <limits>
#include <sstream>
#define L(s) (s)
namespace Slic3r {
//// extrusion entity visitor
void ExtrusionVisitor::use(ExtrusionPath &path) { default_use(path); };
void ExtrusionVisitor::use(ExtrusionPath3D &path3D) { default_use(path3D); }
void ExtrusionVisitor::use(ExtrusionMultiPath &multipath) { default_use(multipath); }
void ExtrusionVisitor::use(ExtrusionMultiPath3D &multipath3D) { default_use(multipath3D); }
void ExtrusionVisitor::use(ExtrusionLoop &loop) { default_use(loop); }
void ExtrusionVisitor::use(ExtrusionEntityCollection &collection) { default_use(collection); }
void ExtrusionVisitorConst::use(const ExtrusionPath &path) { default_use(path); }
void ExtrusionVisitorConst::use(const ExtrusionPath3D &path3D) { default_use(path3D); }
void ExtrusionVisitorConst::use(const ExtrusionMultiPath &multipath) { default_use(multipath); }
void ExtrusionVisitorConst::use(const ExtrusionMultiPath3D &multipath3D) { default_use(multipath3D); }
void ExtrusionVisitorConst::use(const ExtrusionLoop &loop) { default_use(loop); }
void ExtrusionVisitorConst::use(const ExtrusionEntityCollection &collection) { default_use(collection); }
void
ExtrusionPath::intersect_expolygons(const ExPolygonCollection &collection, ExtrusionEntityCollection* retval) const
{
this->_inflate_collection(intersection_pl((Polylines)polyline, to_polygons(collection.expolygons)), retval);
}
void ExtrusionPath::subtract_expolygons(const ExPolygonCollection &collection, ExtrusionEntityCollection* retval) const
{
this->_inflate_collection(diff_pl((Polylines)this->polyline, to_polygons(collection.expolygons)), retval);
}
void ExtrusionPath::clip_end(double distance)
{
this->polyline.clip_end(distance);
}
void ExtrusionPath::simplify(double tolerance)
{
this->polyline.simplify(tolerance);
}
double ExtrusionPath::length() const
{
return this->polyline.length();
}
void ExtrusionPath::_inflate_collection(const Polylines &polylines, ExtrusionEntityCollection* collection) const
{
for (const Polyline &polyline : polylines)
collection->entities.emplace_back(new ExtrusionPath(polyline, *this));
}
void ExtrusionPath::polygons_covered_by_width(Polygons &out, const float scaled_epsilon) const
{
polygons_append(out, offset(this->polyline, double(scale_(this->width/2)) + scaled_epsilon));
}
void ExtrusionPath::polygons_covered_by_spacing(Polygons &out, const float spacing_ratio, const float scaled_epsilon) const
{
// Instantiating the Flow class to get the line spacing.
// Don't know the nozzle diameter, setting to zero. It shall not matter it shall be optimized out by the compiler.
// if the spacing is negative, use the width instead. can happen on ironing second pass.
Flow flow(this->width, this->height, 0.f, spacing_ratio, (this->width*4 < this->height)?true:is_bridge(this->role()));
polygons_append(out, offset(this->polyline, 0.5f * double(flow.scaled_spacing()) + scaled_epsilon));
}
bool ExtrusionLoop::make_clockwise()
{
bool was_ccw = this->polygon().is_counter_clockwise();
if (was_ccw) this->reverse();
return was_ccw;
}
bool ExtrusionLoop::make_counter_clockwise()
{
bool was_cw = this->polygon().is_clockwise();
if (was_cw) this->reverse();
return was_cw;
}
void ExtrusionLoop::reverse()
{
for (ExtrusionPath &path : this->paths)
path.reverse();
std::reverse(this->paths.begin(), this->paths.end());
}
Polygon ExtrusionLoop::polygon() const
{
Polygon polygon;
for (const ExtrusionPath &path : this->paths) {
// for each polyline, append all points except the last one (because it coincides with the first one of the next polyline)
polygon.points.insert(polygon.points.end(), path.polyline.points.begin(), path.polyline.points.end()-1);
}
return polygon;
}
double ExtrusionLoop::length() const
{
double len = 0;
for (const ExtrusionPath &path : this->paths)
len += path.polyline.length();
return len;
}
bool ExtrusionLoop::split_at_vertex(const Point &point)
{
for (ExtrusionPaths::iterator path = this->paths.begin(); path != this->paths.end(); ++path) {
int idx = path->polyline.find_point(point);
if (idx != -1) {
if (this->paths.size() == 1) {
// just change the order of points
path->polyline.points.insert(path->polyline.points.end(), path->polyline.points.begin() + 1, path->polyline.points.begin() + idx + 1);
path->polyline.points.erase(path->polyline.points.begin(), path->polyline.points.begin() + idx);
} else {
// new paths list starts with the second half of current path
ExtrusionPaths new_paths;
new_paths.reserve(this->paths.size() + 1);
{
ExtrusionPath p = *path;
p.polyline.points.erase(p.polyline.points.begin(), p.polyline.points.begin() + idx);
if (p.polyline.is_valid()) new_paths.push_back(p);
}
// then we add all paths until the end of current path list
new_paths.insert(new_paths.end(), path+1, this->paths.end()); // not including this path
// then we add all paths since the beginning of current list up to the previous one
new_paths.insert(new_paths.end(), this->paths.begin(), path); // not including this path
// finally we add the first half of current path
{
ExtrusionPath p = *path;
p.polyline.points.erase(p.polyline.points.begin() + idx + 1, p.polyline.points.end());
if (p.polyline.is_valid()) new_paths.push_back(p);
}
// we can now override the old path list with the new one and stop looping
std::swap(this->paths, new_paths);
}
return true;
}
}
return false;
}
// Splitting an extrusion loop, possibly made of multiple segments, some of the segments may be bridging.
void ExtrusionLoop::split_at(const Point &point, bool prefer_non_overhang)
{
if (this->paths.empty())
return;
// Find the closest path and closest point belonging to that path. Avoid overhangs, if asked for.
size_t path_idx = 0;
Point p;
{
double min = std::numeric_limits<double>::max();
Point p_non_overhang;
size_t path_idx_non_overhang = 0;
double min_non_overhang = std::numeric_limits<double>::max();
for (const ExtrusionPath &path : this->paths) {
Point p_tmp = point.projection_onto(path.polyline);
double dist = (p_tmp - point).cast<double>().norm();
if (dist < min) {
p = p_tmp;
min = dist;
path_idx = &path - &this->paths.front();
}
if (prefer_non_overhang && ! is_bridge(path.role()) && dist < min_non_overhang) {
p_non_overhang = p_tmp;
min_non_overhang = dist;
path_idx_non_overhang = &path - &this->paths.front();
}
}
if (prefer_non_overhang && min_non_overhang != std::numeric_limits<double>::max()) {
// Only apply the non-overhang point if there is one.
path_idx = path_idx_non_overhang;
p = p_non_overhang;
}
}
// now split path_idx in two parts
const ExtrusionPath &path = this->paths[path_idx];
ExtrusionPath p1(path.role(), path.mm3_per_mm, path.width, path.height);
ExtrusionPath p2(path.role(), path.mm3_per_mm, path.width, path.height);
path.polyline.split_at(p, &p1.polyline, &p2.polyline);
if (this->paths.size() == 1) {
if (! p1.polyline.is_valid())
std::swap(this->paths.front().polyline.points, p2.polyline.points);
else if (! p2.polyline.is_valid())
std::swap(this->paths.front().polyline.points, p1.polyline.points);
else {
p2.polyline.points.insert(p2.polyline.points.end(), p1.polyline.points.begin() + 1, p1.polyline.points.end());
std::swap(this->paths.front().polyline.points, p2.polyline.points);
}
} else {
// install the two paths
this->paths.erase(this->paths.begin() + path_idx);
if (p2.polyline.is_valid()) this->paths.insert(this->paths.begin() + path_idx, p2);
if (p1.polyline.is_valid()) this->paths.insert(this->paths.begin() + path_idx, p1);
}
// split at the new vertex
this->split_at_vertex(p);
}
ExtrusionPaths clip_end(ExtrusionPaths& paths, double distance)
{
ExtrusionPaths removed;
while (distance > 0 && !paths.empty()) {
ExtrusionPath& last = paths.back();
removed.push_back(last);
double len = last.length();
if (len <= distance) {
paths.pop_back();
distance -= len;
} else {
last.polyline.clip_end(distance);
removed.back().polyline.clip_start(removed.back().polyline.length() - distance);
break;
}
}
std::reverse(removed.begin(), removed.end());
return removed;
}
bool ExtrusionLoop::has_overhang_point(const Point &point) const
{
for (const ExtrusionPath &path : this->paths) {
int pos = path.polyline.find_point(point);
if (pos != -1) {
// point belongs to this path
// we consider it overhang only if it's not an endpoint
return (is_bridge(path.role()) && pos > 0 && pos != (int)(path.polyline.points.size())-1);
}
}
return false;
}
void ExtrusionLoop::polygons_covered_by_width(Polygons &out, const float scaled_epsilon) const
{
for (const ExtrusionPath &path : this->paths)
path.polygons_covered_by_width(out, scaled_epsilon);
}
void ExtrusionLoop::polygons_covered_by_spacing(Polygons &out, const float spacing_ratio, const float scaled_epsilon) const
{
for (const ExtrusionPath &path : this->paths)
path.polygons_covered_by_spacing(out, spacing_ratio, scaled_epsilon);
}
std::string ExtrusionEntity::role_to_string(ExtrusionRole role)
{
switch (role) {
case erNone : return L("Unknown");
case erPerimeter : return L("Internal perimeter");
case erExternalPerimeter : return L("External perimeter");
case erOverhangPerimeter : return L("Overhang perimeter");
case erInternalInfill : return L("Internal infill");
case erSolidInfill : return L("Solid infill");
case erTopSolidInfill : return L("Top solid infill");
case erIroning : return L("Ironing");
case erBridgeInfill : return L("Bridge infill");
case erInternalBridgeInfill : return L("Internal bridge infill");
case erThinWall : return L("Thin wall");
case erGapFill : return L("Gap fill");
case erSkirt : return L("Skirt");
case erSupportMaterial : return L("Support material");
case erSupportMaterialInterface : return L("Support material interface");
case erWipeTower : return L("Wipe tower");
case erMilling : return L("Mill");
case erCustom : return L("Custom");
case erMixed : return L("Mixed");
default : assert(false);
}
return "";
}
ExtrusionRole ExtrusionEntity::string_to_role(const std::string_view role)
{
if (role == L("Perimeter") || role == L("Internal perimeter"))
return erPerimeter;
else if (role == L("External perimeter"))
return erExternalPerimeter;
else if (role == L("Overhang perimeter"))
return erOverhangPerimeter;
else if (role == L("Internal infill"))
return erInternalInfill;
else if (role == L("Solid infill"))
return erSolidInfill;
else if (role == L("Top solid infill"))
return erTopSolidInfill;
else if (role == L("Ironing"))
return erIroning;
else if (role == L("Bridge infill"))
return erBridgeInfill;
else if (role == L("Internal bridge infill"))
return erInternalBridgeInfill;
else if (role == L("Thin wall"))
return erThinWall;
else if (role == L("Gap fill"))
return erGapFill;
else if (role == L("Skirt"))
return erSkirt;
else if (role == L("Support material"))
return erSupportMaterial;
else if (role == L("Support material interface"))
return erSupportMaterialInterface;
else if (role == L("Wipe tower"))
return erWipeTower;
else if (role == L("Mill"))
return erMilling;
else if (role == L("Custom"))
return erCustom;
else if (role == L("Mixed"))
return erMixed;
else
return erNone;
}
void ExtrusionPrinter::use(const ExtrusionPath &path) {
ss << "ExtrusionPath:" << (uint16_t)path.role() << "{";
for (int i = 0; i < path.polyline.points.size(); i++) {
if (i != 0) ss << ",";
double x = (mult * (path.polyline.points[i].x()));
double y = (mult * (path.polyline.points[i].y()));
ss << std::fixed << "{"<<(trunc?(int)x:x) << "," << (trunc ? (int)y : y) <<"}";
}
ss << "}";
}
void ExtrusionPrinter::use(const ExtrusionPath3D &path3D) {
ss << "ExtrusionPath3D:" << (uint16_t)path3D.role() << "{";
for (int i = 0; i < path3D.polyline.points.size();i++){
if (i != 0) ss << ",";
double x = (mult * (path3D.polyline.points[i].x()));
double y = (mult * (path3D.polyline.points[i].y()));
double z = (path3D.z_offsets.size() > i ? mult * (path3D.z_offsets[i]) : -1);
ss << std::fixed << "{" << (trunc ? (int)x : x) << "," << (trunc ? (int)y : y) << "," << (trunc ? (int)z : z) << "}";
}
ss << "}";
}
void ExtrusionPrinter::use(const ExtrusionMultiPath &multipath) {
ss << "ExtrusionMultiPath:" << (uint16_t)multipath.role() << "{";
for (int i = 0; i < multipath.paths.size(); i++) {
if (i != 0) ss << ",";
multipath.paths[i].visit(*this);
}
ss << "}";
}
void ExtrusionPrinter::use(const ExtrusionMultiPath3D &multipath3D) {
ss << "multipath3D:" << (uint16_t)multipath3D.role() << "{";
for (int i = 0; i < multipath3D.paths.size(); i++) {
if (i != 0) ss << ",";
multipath3D.paths[i].visit(*this);
}
ss << "}";
}
void ExtrusionPrinter::use(const ExtrusionLoop &loop) {
ss << "ExtrusionLoop:" << (uint16_t)loop.role()<<":" <<(uint16_t)loop.loop_role()<<"{";
for (int i = 0; i < loop.paths.size(); i++) {
if (i != 0) ss << ",";
loop.paths[i].visit(*this);
}
ss << "}";
}
void ExtrusionPrinter::use(const ExtrusionEntityCollection &collection) {
ss << "ExtrusionEntityCollection:" << (uint16_t)collection.role() << "{";
for (int i = 0; i < collection.entities.size(); i++) {
if (i != 0) ss << ",";
collection.entities[i]->visit(*this);
}
if(!collection.can_sort()) ss<<", no_sort=true";
ss << "}";
}
void ExtrusionLength::default_use(const ExtrusionEntity& entity) { dist += entity.length(); };
void ExtrusionLength::use(const ExtrusionEntityCollection& collection) {
for (int i = 0; i < collection.entities.size(); i++) {
collection.entities[i]->visit(*this);
}
}
void ExtrusionVisitorRecursiveConst::use(const ExtrusionMultiPath& multipath) {
for (const ExtrusionPath& path: multipath.paths) {
path.visit(*this);
}
}
void ExtrusionVisitorRecursiveConst::use(const ExtrusionMultiPath3D& multipath3D) {
for (const ExtrusionPath3D& path3D : multipath3D.paths) {
path3D.visit(*this);
}
}
void ExtrusionVisitorRecursiveConst::use(const ExtrusionLoop& loop) {
for (const ExtrusionPath& path : loop.paths) {
path.visit(*this);
}
}
void ExtrusionVisitorRecursiveConst::use(const ExtrusionEntityCollection& collection) {
for (const ExtrusionEntity* entity : collection.entities) {
entity->visit(*this);
}
}
//class ExtrusionTreeVisitor : ExtrusionVisitor {
//public:
// //virtual void use(ExtrusionEntity &entity) { assert(false); };
// virtual void use(ExtrusionPath &path) override { const ExtrusionPath &constpath = path; use(constpath); };
// virtual void use(ExtrusionPath3D &path3D) override { const ExtrusionPath3D &constpath3D = path3D; use(constpath3D); };
// virtual void use(ExtrusionMultiPath &multipath) override { const ExtrusionMultiPath &constmultipath = multipath; use(constmultipath); };
// virtual void use(ExtrusionMultiPath3D &multipath3D) override { const ExtrusionMultiPath3D &constmultipath3D = multipath3D; use(constmultipath3D); };
// virtual void use(ExtrusionLoop &loop) override { const ExtrusionLoop &constloop = loop; use(constloop); };
// virtual void use(ExtrusionEntityCollection &collection) { const ExtrusionEntityCollection &constcollection = collection; use(constcollection); };
// virtual void use(const ExtrusionPath &path) override { assert(false); };
// virtual void use(const ExtrusionPath3D &path3D) override { assert(false); };
// virtual void use(const ExtrusionMultiPath &multipath) override { assert(false); };
// virtual void use(const ExtrusionMultiPath3D &multipath3D) { assert(false); };
// virtual void use(const ExtrusionLoop &loop) override { assert(false); };
// virtual void use(const ExtrusionEntityCollection &collection) { assert(false); };
// virtual void use_default(ExtrusionEntity &entity) { const ExtrusionEntity &constentity = entity; use_default(constentity); };
// virtual void use_default(const ExtrusionEntity &entity) {};
//
//};
}
|