-
Notifications
You must be signed in to change notification settings - Fork 7
/
demos.cpp
371 lines (314 loc) · 15.1 KB
/
demos.cpp
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
#include "demos.h"
#include "NEPath-master/NEPathPlanner.h"
#include "NEPath-master/FileAgent.h"
#include <cmath>
void demo_Raster() {
NEPathPlanner planner;
// Set the contour
path contour;
contour.length = 1000; // the number of waypoints
contour.x = new double[contour.length](); // x-coordinate of waypoints
contour.y = new double[contour.length](); // y-coordinate of waypoints
const double pi = acos(-1.0); // pi == 3.1415926...
for (int i = 0; i < contour.length; ++i) {
double theta = 2.0 * pi * i / contour.length;
double r = 15.0 * (1.0 + 0.15 * cos(10.0 * theta));
contour.x[i] = r * cos(theta);
contour.y[i] = r * sin(theta);
}
planner.set_contour(contour);
// or `planner.set_contour(contour.x, contour.y, contour.length)`
// Set the toolpath parameters
DirectParallelOptions opts;
opts.delta = 1.0; // the line width of toolpaths
opts.angle = - pi / 3.0; // the angle of raster toolpaths, unit: rad
paths raster_paths = planner.Raster(opts); // all raster paths
cout << "There are " << raster_paths.size() << " continuous toolpaths in total." << endl;
FileAgent::delete_AllFiles(R"(.\data_examples\demo_raster\)");
FileAgent::write_csv(raster_paths, R"(.\data_examples\demo_raster\)", ".csv");
//FileAgent::write_csv(contour, R"(.\data_examples\contour.csv)");
}
void demo_Zigzag() {
NEPathPlanner planner;
// Set the contour
path contour;
contour.length = 1000; // the number of waypoints
contour.x = new double[contour.length](); // x-coordinate of waypoints
contour.y = new double[contour.length](); // y-coordinate of waypoints
const double pi = acos(-1.0); // pi == 3.1415926...
for (int i = 0; i < contour.length; ++i) {
double theta = 2.0 * pi * i / contour.length;
double r = 15.0 * (1.0 + 0.15 * cos(10.0 * theta));
contour.x[i] = r * cos(theta);
contour.y[i] = r * sin(theta);
}
planner.set_contour(contour);
// or `planner.set_contour(contour.x, contour.y, contour.length)`
// Set the toolpath parameters
DirectParallelOptions opts;
opts.delta = 1.0; // the line width of toolpaths
opts.angle = pi / 3.0; // the angle of Zigzag toolpaths, unit: rad
paths zigzag_paths = planner.Zigzag(opts); // all zigzag paths
cout << "There are " << zigzag_paths.size() << " continuous toolpaths in total." << endl;
for (int i = 0; i < zigzag_paths.size(); ++i) {
// zigzag_paths[i] is the i-th continuous toolpath
cout << "Toopath " << i << " has " << zigzag_paths[i].length << " waypoints." << endl;
}
FileAgent::delete_AllFiles(R"(.\data_examples\demo_zigzag\)");
FileAgent::write_csv(zigzag_paths, R"(.\data_examples\demo_zigzag\)", ".csv");
FileAgent::write_csv(contour, R"(.\data_examples\contour.csv)");
}
void demo_CP() {
NEPathPlanner planner;
// Obtain the contour of the outer boundary of slices
path contour;
contour.length = 1000; // the number of waypoints
contour.x = new double[contour.length](); // x-coordinate of waypoints
contour.y = new double[contour.length](); // y-coordinate of waypoints
const double pi = acos(-1.0); // pi == 3.1415926...
for (int i = 0; i < contour.length; ++i) {
double theta = 2.0 * pi * i / contour.length;
double r = 15.0 * (1.0 + 0.15 * cos(10.0 * theta));
contour.x[i] = r * cos(theta);
contour.y[i] = r * sin(theta);
}
// The out boundary should be offset with half of the line width to obtain the outmost toolpath
NEPathPlanner planner_toolcompensate;
planner_toolcompensate.set_contour(contour);
ContourParallelOptions opts_toolcompensate;
opts_toolcompensate.delta = -1.0 * 0.5; // half of the line width of toolpaths
opts_toolcompensate.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts_toolcompensate.washdis = 0.2;
paths path_outmost = planner_toolcompensate.tool_compensate(opts_toolcompensate);
planner.set_contour(path_outmost[0]);
// or `planner.set_contour(contour.x, contour.y, contour.length)`
// Set the toolpath parameters
ContourParallelOptions opts;
opts.delta = 1.0; // the line width of toolpaths
opts.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts.washdis = 0.2;
paths CP_paths = planner.CP(opts); // all CP paths
cout << "There are " << CP_paths.size() << " continuous toolpaths in total." << endl;
for (int i = 0; i < CP_paths.size(); ++i) {
// CP_paths[i] is the i-th continuous toolpath
cout << "Toopath " << i << " has " << CP_paths[i].length << " waypoints." << endl;
}
FileAgent::delete_AllFiles(R"(.\data_examples\demo_CP\)");
FileAgent::write_csv(CP_paths, R"(.\data_examples\demo_CP\)", ".csv");
// FileAgent::write_csv(contour, R"(.\data_examples\contour.csv)");
}
void demo_tool_compensate() {
NEPathPlanner planner;
// Obtain the contour of the outer boundary of slices
path contour;
contour.length = 1000; // the number of waypoints
contour.x = new double[contour.length](); // x-coordinate of waypoints
contour.y = new double[contour.length](); // y-coordinate of waypoints
const double pi = acos(-1.0); // pi == 3.1415926...
for (int i = 0; i < contour.length; ++i) {
double theta = 2.0 * pi * i / contour.length;
double r = 15.0 * (1.0 + 0.15 * cos(10.0 * theta));
contour.x[i] = r * cos(theta);
contour.y[i] = r * sin(theta);
}
planner.set_contour(contour);
// Obtain the hole
double x_hole[] = { -5,5,5,0,-5 };
double y_hole[] = { -5,-5,5,0,5 };
planner.addhole(x_hole, y_hole, 5);
// Tool compensate
ContourParallelOptions opts;
opts.delta = -1.5; // the offset distance
opts.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts.washdis = 0.2;
paths ps_toolcompensate = planner.tool_compensate(opts); // Tool compensate
cout << "There are " << ps_toolcompensate.size() << " continuous toolpaths in total." << endl;
for (int i = 0; i < ps_toolcompensate.size(); ++i) {
// ps_toolcompensate[i] is the i-th continuous toolpath
cout << "Toopath " << i << " has " << ps_toolcompensate[i].length << " waypoints." << endl;
}
FileAgent::delete_AllFiles(R"(.\data_examples\demo_toolcompensate\)");
FileAgent::write_csv(ps_toolcompensate, R"(.\data_examples\demo_toolcompensate\)", ".csv");
// FileAgent::write_csv(contour, R"(.\data_examples\contour.csv)");
FileAgent::write_csv(planner.holes[0], R"(.\data_examples\hole.csv)");
}
void demo_underfill() {
NEPathPlanner planner;
// Obtain the contour of the outer boundary of slices
path contour;
contour.length = 1000; // the number of waypoints
contour.x = new double[contour.length](); // x-coordinate of waypoints
contour.y = new double[contour.length](); // y-coordinate of waypoints
const double pi = acos(-1.0); // pi == 3.1415926...
for (int i = 0; i < contour.length; ++i) {
double theta = 2.0 * pi * i / contour.length;
double r = 15.0 * (1.0 + 0.15 * cos(10.0 * theta));
contour.x[i] = r * cos(theta);
contour.y[i] = r * sin(theta);
}
// The out boundary should be offset with half of the line width to obtain the outmost toolpath
NEPathPlanner planner_toolcompensate;
planner_toolcompensate.set_contour(contour);
ContourParallelOptions opts_toolcompensate;
opts_toolcompensate.delta = -1.0 * 0.5; // half of the line width of toolpaths
opts_toolcompensate.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts_toolcompensate.washdis = 0.2;
paths path_outmost = planner_toolcompensate.tool_compensate(opts_toolcompensate);
planner.set_contour(path_outmost[0]);
// or `planner.set_contour(contour.x, contour.y, contour.length)`
// Set the toolpath parameters
ContourParallelOptions opts;
opts.delta = 1.0; // the line width of toolpaths
opts.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts.washdis = 0.2;
paths CP_paths = planner.CP(opts); // all CP paths
cout << "There are " << CP_paths.size() << " continuous toolpaths in total." << endl;
double delta_underfill = opts.delta; // the line width for underfill computation
double reratio = 0.03; // resolution ratio for underfill computation
UnderFillSolution ufs = Curve::UnderFill(contour, paths(), CP_paths, delta_underfill, reratio); // Obtain the results of underfill
cout << "The underfill rate is " << ufs.underfillrate * 100 << "%." << endl;
// Output as files
std::ofstream xsoutFile(R"(.\data_examples\demo_underfill\underfill\xs.csv)", std::ios::out);
xsoutFile << "x\n";
for (int i = 0; i < ufs.nx; ++i) {
xsoutFile << ufs.xs[i] << '\n';
}
xsoutFile.close();
std::ofstream ysoutFile(R"(.\data_examples\demo_underfill\underfill\ys.csv)", std::ios::out);
ysoutFile << "y\n";
for (int i = 0; i < ufs.ny; ++i) {
ysoutFile << ufs.ys[i] << '\n';
}
ysoutFile.close();
std::ofstream mapsliceoutFile(R"(.\data_examples\demo_underfill\underfill\map_slice.csv)", std::ios::out);
for (int i = 0; i < ufs.nx; ++i) {
for (int j = 0; j < ufs.ny - 1; ++j) {
mapsliceoutFile << ufs.map_slice[i][j] << ',';
}
mapsliceoutFile << ufs.map_slice[i][ufs.ny - 1] << '\n';
}
mapsliceoutFile.close();
std::ofstream mapdeltaoutFile(R"(.\data_examples\demo_underfill\underfill\map_delta.csv)", std::ios::out);
for (int i = 0; i < ufs.nx; ++i) {
for (int j = 0; j < ufs.ny - 1; ++j) {
mapdeltaoutFile << ufs.map_delta[i][j] << ',';
}
mapdeltaoutFile << ufs.map_delta[i][ufs.ny - 1] << '\n';
}
mapdeltaoutFile.close();
FileAgent::delete_AllFiles(R"(.\data_examples\demo_underfill\paths\)");
FileAgent::write_csv(CP_paths, R"(.\data_examples\demo_underfill\paths\)", ".csv");
// FileAgent::write_csv(contour, R"(.\data_examples\contour.csv)");
}
void demo_sharpcorner() {
NEPathPlanner planner;
// Obtain the contour of the outer boundary of slices
path contour;
contour.length = 1000; // the number of waypoints
contour.x = new double[contour.length](); // x-coordinate of waypoints
contour.y = new double[contour.length](); // y-coordinate of waypoints
const double pi = acos(-1.0); // pi == 3.1415926...
for (int i = 0; i < contour.length; ++i) {
double theta = 2.0 * pi * i / contour.length;
double r = 15.0 * (1.0 + 0.15 * cos(10.0 * theta));
contour.x[i] = r * cos(theta);
contour.y[i] = r * sin(theta);
}
// The out boundary should be offset with half of the line width to obtain the outmost toolpath
NEPathPlanner planner_toolcompensate;
planner_toolcompensate.set_contour(contour);
ContourParallelOptions opts_toolcompensate;
opts_toolcompensate.delta = -1.0 * 0.5; // half of the line width of toolpaths
opts_toolcompensate.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts_toolcompensate.washdis = 0.2;
paths path_outmost = planner_toolcompensate.tool_compensate(opts_toolcompensate);
planner.set_contour(path_outmost[0]);
// or `planner.set_contour(contour.x, contour.y, contour.length)`
// Set the toolpath parameters
ContourParallelOptions opts;
opts.delta = 1.0; // the line width of toolpaths
opts.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts.washdis = 0.2;
paths CP_paths = planner.CP(opts); // all CP paths
cout << "There are " << CP_paths.size() << " continuous toolpaths in total." << endl;
double radius = 1.0; // radius of the rolling circle
double threshold = 0.3; // threshold of area on one side to determine a sharp corner
// Obtain the results of underfill
int num = 0;
paths ps_sharpturn;
for (int i = 0; i < CP_paths.size(); ++i) {
path p = path();
SharpTurnSolution sol = Curve::SharpTurn_Invariant(CP_paths[i], radius, threshold, true, 0.5);
for (int j = 0; j < sol.length; ++j) {
num += sol.SharpTurn[j];
}
p.length = sol.length;
p.x = new double[p.length];
p.y = new double[p.length];
for (int j = 0; j < p.length; ++j) {
p.x[j] = sol.SharpTurn[j];
p.y[j] = sol.AreaPercent[j];
}
ps_sharpturn.push_back(p);
}
cout << "There exist " << num << " sharp corners." << endl;
// Output as files
FileAgent::delete_AllFiles(R"(.\data_examples\demo_sharpcorner\sharpcorner\)");
FileAgent::write_csv(ps_sharpturn, R"(.\data_examples\demo_sharpcorner\sharpcorner\)", ".csv");
FileAgent::delete_AllFiles(R"(.\data_examples\demo_sharpcorner\paths\)");
FileAgent::write_csv(CP_paths, R"(.\data_examples\demo_sharpcorner\paths\)", ".csv");
// FileAgent::write_csv(contour, R"(.\data_examples\contour.csv)");
}
#ifdef IncludeGurobi
void demo_IQOP() {
NEPathPlanner planner;
// Obtain the contour of the outer boundary of slices
path contour;
contour.length = 1000; // the number of waypoints
contour.x = new double[contour.length](); // x-coordinate of waypoints
contour.y = new double[contour.length](); // y-coordinate of waypoints
const double pi = acos(-1.0); // pi == 3.1415926...
for (int i = 0; i < contour.length; ++i) {
double theta = 2.0 * pi * i / contour.length;
double r = 15.0 * (1.0 + 0.1 * cos(10.0 * theta));
contour.x[i] = r * cos(theta);
contour.y[i] = r * sin(theta);
}
// The out boundary should be offset with half of the line width to obtain the outmost toolpath
NEPathPlanner planner_toolcompensate;
planner_toolcompensate.set_contour(contour);
ContourParallelOptions opts_toolcompensate;
opts_toolcompensate.delta = -1.0 * 0.5; // half of the line width of toolpaths
opts_toolcompensate.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts_toolcompensate.washdis = 0.2;
paths path_outmost = planner_toolcompensate.tool_compensate(opts_toolcompensate);
planner.set_contour(path_outmost[0]);
// or `planner.set_contour(contour.x, contour.y, contour.length)`
// Set the toolpath parameters
NonEquidistantOptions opts;
opts.delta = 1.0; // the line width of toolpaths
opts.alpha = 0.5; // the scale of minimum distance
opts.dot_delta = 1.0; // the upper bound of \dot{delta_i}
opts.ddot_delta = 0.1; // the upper bound of \ddot{delta_i}
opts.optimize_Q = true; // the isoperimetric quotient is in the objective function
opts.optimize_S = false; // the area is not in the objective function
opts.optimize_L = false; // the length is not in the objective function
opts.lambda_Q = 1.0; // the weighting coefficient of the isoperimetric quotient
opts.wash = true; // it is recommended to set opt.wash=true
// if wash==true, then all toolpaths would have yniformly distributed waypoints, with a distance near opts.washdis
opts.washdis = 0.2;
paths IQOP_paths = planner.IQOP(opts, true); // all IQOP paths
cout << "There are " << IQOP_paths.size() << " continuous toolpaths in total." << endl;
FileAgent::delete_AllFiles(R"(.\data_examples\demo_IQOP\paths_IQ\)");
FileAgent::write_csv(IQOP_paths, R"(.\data_examples\demo_IQOP\paths_IQ\)", ".csv");
FileAgent::write_csv(contour, R"(.\data_examples\demo_IQOP\contour.csv)");
}
#endif