Xinqi Bao's Git
6 #include "Configurations.hh"
9 Gcode::Gcode(Layer
& layer
)
10 : loops(layer
.getLoops()),
11 commands(layer
.getCommands()),
12 parts(layer
.getParts()),
13 minX(layer
.getMinX()),
16 if (loops
.empty()) return;
18 for (const auto& lp
: loops
)
19 reserveSize
+= lp
.size() + int((lp
.getmaxX() - lp
.getminX()) / dp
) * 2;
20 commands
.reserve(reserveSize
);
21 G0_high(layer
.getZ());
28 void Gcode::G0(double x
, double y
, double z
)
30 commands
.push_back(Command(0, x
, y
));
31 // f << "G0 F6600 X" << x << " Y" << y << " Z" << z << '\n';
34 void Gcode::G0(double x
, double y
)
36 commands
.push_back(Command(0, x
, y
));
37 // f << "G0 F6600 X" << x << " Y" << y << '\n';
40 void Gcode::G0_high(double z
)
42 // f << "G0 F6600 Z" << z << '\n';
45 void Gcode::G1(double x
, double y
)
47 commands
.push_back(Command(1, x
, y
));
48 // f << "G1 F783 X" << x << " Y" << y << " E" << e << '\n';
53 // f << "G92 E0 ; reset the expected extruder position\n";
57 static double x
= 0, y
= 0;
61 #if 0 // by erasing the finished loop
62 while(!loops
.empty()){
63 double minDistance
= pow(loops
.front().midX
- x
, 2) + pow(loops
.front().midY
- y
, 2);
66 for(int i
= 1; i
< loops
.size(); i
++){
67 distance
= pow(loops
[i
].midX
- x
, 2) + pow(loops
[i
].midY
- y
, 2);
68 if(distance
< minDistance
){
69 minDistance
= distance
;
74 x
= commands
.back().x
;
75 y
= commands
.back().y
;
76 loops
.erase(loops
.begin() + k
);
79 #if 1 // by checking the visited value
80 int numLoop
= loops
.size();
84 double minDistance
, distance
;
85 while (loops
[i
].visited
) i
++;
86 minDistance
= pow(loops
[i
].midX
- x
, 2) + pow(loops
[i
].midY
- y
, 2);
88 for (i
++; i
< loops
.size(); i
++)
90 if (loops
[i
].visited
) continue;
91 distance
= pow(loops
[i
].midX
- x
, 2) + pow(loops
[i
].midY
- y
, 2);
92 if (distance
< minDistance
)
94 minDistance
= distance
;
99 loops
[k
].visited
= true; // mark as filled loop
100 x
= commands
.back().x
;
101 y
= commands
.back().y
;
107 void Gcode::fillLoop(Loop
& loop
)
110 ploops
.push_back(loop
);
111 int numIntersectionsTotal
= loop
.size();
112 for (auto& plp
: loop
.subLoops
)
114 ploops
.push_back(plp
);
115 numIntersectionsTotal
+= plp
.size();
119 double x1
, y1
, x2
, y2
;
121 int numLoop
= ploops
.size();
125 double minDistance
, distance
;
126 while (ploops
[i
].visited
) i
++;
127 minDistance
= pow(ploops
[i
].midX
- x
, 2) + pow(ploops
[i
].midY
- y
, 2);
129 for (i
++; i
< ploops
.size(); i
++)
131 if (ploops
[i
].visited
) continue;
132 distance
= pow(ploops
[i
].midX
- x
, 2) + pow(ploops
[i
].midY
- y
, 2);
133 if (distance
< minDistance
)
135 minDistance
= distance
;
139 // start to print this loop's outline
140 x1
= ploops
[k
].back().x
;
141 y1
= ploops
[k
].back().y
;
143 for (int i
= 0; i
< ploops
[k
].size(); i
++)
145 x2
= ploops
[k
].at(i
).x
;
146 y2
= ploops
[k
].at(i
).y
;
150 ploops
[k
].visited
= true;
151 x
= commands
.back().x
;
152 y
= commands
.back().y
;
156 for (auto& lp : ploops) {
160 for (int i = 0; i < lp.size(); i++) {
170 vector
<vector
<Cross
>> pparts
;
171 double invDp
= 1.0 / dp
;
172 numParts
= (loop
.getmaxX() - loop
.getminX()) * dp
+ 1;
173 pparts
.reserve(numParts
);
174 int meanIntersectionsPerPart
= numIntersectionsTotal
/ numParts
* 1.5;
175 for (int i
= 0; i
< numParts
; i
++)
177 pparts
.push_back(vector
<Cross
>());
178 pparts
.back().reserve(meanIntersectionsPerPart
);
180 for (const auto& lp
: ploops
)
181 { // assign all intersections to partial
182 Vec2d v1
, v2(lp
.back());
183 int index1
, index2
= (v2
.x
- loop
.getminX()) * invDp
;
186 for (auto& l
: lp
.head
)
191 index2
= (v2
.x
- loop
.getminX()) * invDp
;
202 for (int i
= imin
; i
<= imax
; i
++)
203 pparts
[i
].push_back(Cross(v1
, v2
));
209 for (double x
= loop
.getminX() + ingap
; x
< loop
.getmaxX(); x
+= ingap
)
213 for (auto& cr
: pparts
.at((x
- loop
.getminX()) * invDp
))
215 double x1
= cr
.p1
.x
, y1
= cr
.p1
.y
;
216 double x2
= cr
.p2
.x
, y2
= cr
.p2
.y
;
217 if ((x1
<= x
&& x
< x2
) || (x2
<= x
&& x
< x1
))
218 ys
.push_back((x
- x1
) * (y2
- y1
) / (x2
- x1
) + y1
);
220 std::sort(ys
.begin(), ys
.end());
221 if (ys
.size() == 0) continue;
228 for (int i
= 1; i
< ys
.size(); i
++)
247 y1
= ys
.at(ys
.size() - 1);
250 for (int i
= ys
.size() - 2; i
>= 0; i
--)
270 void Gcode::outline()
272 double x1
, y1
, x2
, y2
;
273 for (auto& lp
: loops
)
278 for (int i
= 0; i
< lp
.size(); i
++)
290 double invDp
= 1.0 / dp
;
291 minX
= loops
.front().getminX();
292 maxX
= loops
.front().getmaxX();
293 int numIntersectionsTotal
= loops
.front().size();
294 for (int i
= 1; i
< loops
.size(); i
++)
296 numIntersectionsTotal
+= loops
.at(i
).size();
297 if (minX
> loops
.at(i
).getminX()) minX
= loops
.at(i
).getminX();
298 if (maxX
< loops
.at(i
).getmaxX()) maxX
= loops
.at(i
).getmaxX();
300 numParts
= (maxX
- minX
) * dp
+ 1;
301 parts
.reserve(numParts
);
302 int meanIntersectionsPerPart
= numIntersectionsTotal
/ numParts
* 1.5;
303 for (int i
= 0; i
< numParts
; i
++)
305 parts
.push_back(std::vector
<Cross
>());
306 parts
.back().reserve(meanIntersectionsPerPart
);
308 for (const auto& lp
: loops
)
309 { // assign all intersections to partial
311 Vec2d
v2(lp
.front());
312 int index1
= (v1
.x
- minX
) * invDp
;
313 int index2
= (v2
.x
- minX
) * invDp
;
325 for (int i
= imin
; i
<= imax
; i
++) parts
[i
].push_back(Cross(v1
, v2
));
327 for (int i
= 1; i
< lp
.size(); i
++)
328 { // from the front to the item before the back
332 index2
= (v2
.x
- minX
) * invDp
;
343 for (int i
= imin
; i
<= imax
; i
++)
344 parts
[i
].push_back(Cross(v1
, v2
));
347 std::vector
<double> ys
;
349 for (double x
= minX
+ ingap
; x
< maxX
; x
+= ingap
)
353 for (auto& cr
: parts
.at((x
- minX
) * invDp
))
355 double x1
= cr
.p1
.x
, y1
= cr
.p1
.y
;
356 double x2
= cr
.p2
.x
, y2
= cr
.p2
.y
;
357 if ((x1
<= x
&& x
< x2
) || (x2
<= x
&& x
< x1
))
358 ys
.push_back((x
- x1
) * (y2
- y1
) / (x2
- x1
) + y1
);
360 std::sort(ys
.begin(), ys
.end());
361 if (ys
.size() == 0) continue;
368 for (int i
= 1; i
< ys
.size(); i
++)
387 y1
= ys
.at(ys
.size() - 1);
390 for (int i
= ys
.size() - 2; i
>= 0; i
--)