-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvecsort.c
More file actions
591 lines (501 loc) · 10.5 KB
/
Copy pathvecsort.c
File metadata and controls
591 lines (501 loc) · 10.5 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/** \file
* Topologically sort vectors for faster laser cutting or plotting.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct _vector vector_t;
struct _vector
{
vector_t * next;
vector_t ** prev;
double x1;
double y1;
double x2;
double y2;
};
typedef struct
{
vector_t * vectors;
} vectors_t;
// Red/Green/Blue
#define VECTOR_PASSES 3
// close enough for floating point work
static inline int fpeq(double x, double y)
{
const double eps = 1e-8;
return fabs(x-y) < eps;
}
static double
vector_transit_len(
vector_t * v
)
{
double lx = 0;
double ly = 0;
double transit_len_sum = 0;
while (v)
{
double t_dx = lx - v->x1;
double t_dy = ly - v->y1;
double transit_len = sqrt(t_dx * t_dx + t_dy * t_dy);
if (transit_len != 0)
transit_len_sum += transit_len;
// Advance the point
lx = v->x2;
ly = v->y2;
v = v->next;
}
return transit_len_sum;
}
static void
vector_stats(
vector_t * v
)
{
double lx = 0;
double ly = 0;
double cut_len_sum = 0;
int cuts = 0;
double transit_len_sum = 0;
int transits = 0;
while (v)
{
double t_dx = lx - v->x1;
double t_dy = ly - v->y1;
double transit_len = sqrt(t_dx * t_dx + t_dy * t_dy);
if (transit_len != 0)
{
transits++;
transit_len_sum += transit_len;
}
double c_dx = v->x1 - v->x2;
double c_dy = v->y1 - v->y2;
double cut_len = sqrt(c_dx*c_dx + c_dy*c_dy);
if (cut_len != 0)
{
cuts++;
cut_len_sum += cut_len;
}
// Advance the point
lx = v->x2;
ly = v->y2;
v = v->next;
}
fprintf(stderr, "Cuts: %u len %.0f\n", cuts, cut_len_sum);
fprintf(stderr, "Move: %u len %.0f\n", transits, transit_len_sum);
}
static void
vector_create(
vectors_t * const vectors,
double x1,
double y1,
double x2,
double y2
)
{
// Find the end of the list and, if vector optimization is
// turned on, check for duplicates
vector_t ** iter = &vectors->vectors;
while (*iter)
{
vector_t * const p = *iter;
if (fpeq(p->x1,x1) && fpeq(p->y1,y1)
&& fpeq(p->x2,x2) && fpeq(p->y2,y2))
return;
if (fpeq(p->x1,x2) && fpeq(p->y1,y2)
&& fpeq(p->x2,x1) && fpeq(p->y2,y1))
return;
if (fpeq(x1,x2) && fpeq(y1,y2))
return;
iter = &p->next;
}
vector_t * const v = calloc(1, sizeof(*v));
if (!v)
return;
v->x1 = x1;
v->y1 = y1;
v->x2 = x2;
v->y2 = y2;
// Append it to the now known end of the list
v->next = NULL;
v->prev = iter;
*iter = v;
}
/**
* Generate a list of vectors.
*
* The vector format is:
* P r,g,b -- Color of the vector
* M x,y -- Move (start a line at x,y)
* L x,y -- Line to x,y from the current position
* Z -- Closing line segment to the starting position
*
* Multi segment vectors are split into individual vectors, which are
* then passed into the topological sort routine.
*
* Exact duplictes will be deleted to try to avoid double hits..
*/
static vectors_t *
vectors_parse(
FILE * const vector_file
)
{
vectors_t * const vectors = calloc(VECTOR_PASSES, sizeof(*vectors));
double mx = 0, my = 0;
double lx = 0, ly = 0;
int pass = 0;
int count = 0;
char buf[256];
while (fgets(buf, sizeof(buf), vector_file))
{
//fprintf(stderr, "read '%s'\n", buf);
const char cmd = buf[0];
double x, y;
switch (cmd)
{
case 'P':
{
// note that they will be in bgr order in the file
int r, g, b;
sscanf(buf+1, "%d %d %d", &b, &g, &r);
if (r == 0 && g != 0 && b == 0)
{
pass = 0;
} else
if (r != 0 && g == 0 && b == 0)
{
pass = 1;
} else
if (r == 0 && g == 0 && b != 0)
{
pass = 2;
} else {
fprintf(stderr, "non-red/green/blue vector? %d,%d,%d\n", r, g, b);
exit(-1);
}
break;
}
case 'M':
// Start a new line.
// This also implicitly sets the
// current laser position
sscanf(buf+1, "%lf %lf", &mx, &my);
lx = mx;
ly = my;
break;
case 'L':
// Add a line segment from the current
// point to the new point, and update
// the current point to the new point.
sscanf(buf+1, "%lf %lf", &x, &y);
vector_create(&vectors[pass], lx, ly, x, y);
count++;
lx = x;
ly = y;
break;
case 'Z':
// Closing segment from the current point
// back to the starting point
vector_create(&vectors[pass], lx, ly, mx, my);
lx = mx;
lx = my;
break;
case 'X':
goto done;
default:
fprintf(stderr, "Unknown command '%c'", cmd);
return NULL;
}
}
done:
fprintf(stderr, "read %u segments\n", count);
/*
for (int i = 0 ; i < VECTOR_PASSES ; i++)
{
vector_stats(vectors[i].vectors);
}
fprintf(stderr, "---\n");
*/
return vectors;
}
/** Find the closest vector to a given point and remove it from the list.
*
* This might reverse a vector if it is closest to draw it in reverse
* order.
*/
static vector_t *
vector_find_closest(
vector_t * v,
const double cx,
const double cy
)
{
double best_dist = 1e9;
vector_t * best = NULL;
int do_reverse = 0;
while (v)
{
double dx1 = cx - v->x1;
double dy1 = cy - v->y1;
double dist1 = dx1*dx1 + dy1*dy1;
if (dist1 < best_dist)
{
best = v;
best_dist = dist1;
do_reverse = 0;
}
double dx2 = cx - v->x2;
double dy2 = cy - v->y2;
double dist2 = dx2*dx2 + dy2*dy2;
if (dist2 < best_dist)
{
best = v;
best_dist = dist2;
do_reverse = 1;
}
v = v->next;
}
if (!best)
return NULL;
// Remove it from the list
if (best->prev)
*best->prev = best->next;
if (best->next)
best->next->prev = best->prev;
// If reversing is required, flip the x1/x2 and y1/y2
if (do_reverse)
{
double x1 = best->x1;
double y1 = best->y1;
best->x1 = best->x2;
best->y1 = best->y2;
best->x2 = x1;
best->y2 = y1;
}
best->next = NULL;
best->prev = NULL;
return best;
}
/**
* Optimize the cut order to minimize transit time.
*
* Simplistic greedy algorithm: look for the closest vector that starts
* or ends at the same point as the current point.
*
* This does not split vectors.
*/
static vector_t *
vector_optimize(
vector_t ** vectors,
double *cx_ptr,
double *cy_ptr
)
{
vector_t * vs = NULL;
vector_t * vs_tail = NULL;
double cx = *cx_ptr;
double cy = *cy_ptr;
while (*vectors)
{
vector_t * v = vector_find_closest(*vectors, cx, cy);
if (!v)
{
fprintf(stderr, "nothing close?\n");
abort();
}
if (!vs)
{
// Nothing on the list yet
vs = vs_tail = v;
} else {
// Add it to the tail of the list
v->next = NULL;
v->prev = &vs_tail->next;
vs_tail->next = v;
vs_tail = v;
}
// Move the current point to the end of the line segment
cx = v->x2;
cy = v->y2;
}
//vector_stats(vs);
*cx_ptr = cx;
*cy_ptr = cy;
// update the pointers
*vectors = vs;
if (vs)
vs->prev = vectors;
return vs;
}
/*
* Attempt to remove long transits.
* Find the longest transit and attempt to move that point
* to a closer point. Then check to see if it reduces the transit.
* returns the reduction in distance
*/
static double
vector_refine(
vector_t * const vector,
double *cx_ptr,
double *cy_ptr
)
{
const double initial_transit_len = vector_transit_len(vector);
double cx = *cx_ptr;
double cy = *cy_ptr;
// find the longest transit
vector_t * v = vector;
double max_transit = 0;
vector_t * transit_v = NULL;
while (v)
{
double t_dx = cx - v->x1;
double t_dy = cy - v->y1;
double transit_len = sqrt(t_dx * t_dx + t_dy * t_dy);
if (!fpeq(transit_len, 0) && max_transit < transit_len)
{
max_transit = transit_len;
transit_v = v;
}
// Advance the point
cx = v->x2;
cy = v->y2;
v = v->next;
}
if (!transit_v)
{
fprintf(stderr, "no longest transit?\n");
return 0;
}
fprintf(stderr, "Total transit: %.3f\n", initial_transit_len);
fprintf(stderr, "longest transit: %.3f: %.3f,%.3f\n",
max_transit,
transit_v->x1,
transit_v->y1
);
// then find the closest *end point* prior to this transit
vector_t * closest = NULL;
double min_dist = 1e9;
for(vector_t * v = vector ; v != transit_v ; v = v->next)
{
double dx = v->x2 - transit_v->x1;
double dy = v->y2 - transit_v->y1;
double dist = dx*dx + dy*dy;
if (min_dist < dist)
continue;
min_dist = dist;
closest = v;
}
if (!closest)
{
fprintf(stderr, "could not find a close one?\n");
return 0;
}
// move the longest transit destination to come after the
// one closest to it, then re-sort based on that point
// remove transit_v from the list
if (transit_v->next)
transit_v->next->prev = transit_v->prev;
if (transit_v->prev)
*transit_v->prev = transit_v->next;
// re-insert it after the closest one
transit_v->next = closest->next;
transit_v->prev = &closest->next;
closest->next = transit_v;
if (transit_v->next)
transit_v->next->prev = &transit_v->next;
// now sort the ones that come after it
double cx2 = transit_v->x2;
double cy2 = transit_v->y2;
vector_optimize(&transit_v->next, &cx2, &cy2);
const double new_transit_len = vector_transit_len(vector);
fprintf(stderr, "Refine transit %.3f\n", new_transit_len);
*cx_ptr = cx2;
*cy_ptr = cy2;
return initial_transit_len - new_transit_len;
}
static void
output_vector(
FILE * const pjl_file,
const vector_t * v
)
{
double lx = 0;
double ly = 0;
while (v)
{
if (fpeq(v->x1,lx) && fpeq(v->y1,ly))
{
// This is the continuation of a line, so
// just add additional points
fprintf(pjl_file, "L %.3f %.3f\n",
v->x2,
v->y2
);
} else {
// Stop the laser; we need to transit
// and then start the laser as we go to
// the next point. Note initial ";"
fprintf(pjl_file, "\nM %.3f %.3f\nL %.3f %.3f\n",
v->x1,
v->y1,
v->x2,
v->y2
);
}
// Changing power on the fly is not supported for now
// \todo: Check v->power and adjust ZS, XR, etc
// Move to the next vector, updating our current point
lx = v->x2;
ly = v->y2;
v = v->next;
}
fprintf(pjl_file, "\n");
}
static void
generate_vectors(
FILE * const vector_file,
FILE * const pjl_file
)
{
vectors_t * const vectors = vectors_parse(vector_file);
double lx = 0;
double ly = 0;
for (int i = 0 ; i < VECTOR_PASSES ; i++)
{
vectors_t * const vs = &vectors[i];
if (!vs->vectors)
continue;
fprintf(stderr, "Group %d\n", i);
vector_stats(vs->vectors);
vector_optimize(
&vs->vectors,
&lx, &ly
);
vector_stats(vs->vectors);
/*
for(int i = 0 ; i < 8 ; i++)
{
double sx = vs->vectors->x1;
double sy = vs->vectors->y1;
if (vector_refine(vs->vectors, &sx, &sy) <= 0)
break;
lx = sx;
ly = sy;
}
*/
fprintf(pjl_file, "P %d %d %d\n",
i == 0 ? 100 : 0,
i == 1 ? 100 : 0,
i == 2 ? 100 : 0
);
output_vector(pjl_file, vs->vectors);
fprintf(pjl_file, "\n\n");
}
}
int main(void)
{
generate_vectors(stdin, stdout);
return 0;
}