mirror of
git://projects.qi-hardware.com/cae-tools.git
synced 2025-04-21 12:27:27 +03:00
cameo: added optimization of movements between tool paths
- ops.h, ops.c (optimize_paths): reorder toolpaths to reduce the distance between them - lang.l, lang.y: added command "optimize" - README: documented "optimize"
This commit is contained in:
46
cameo/ops.c
46
cameo/ops.c
@@ -12,6 +12,7 @@
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "path.h"
|
||||
#include "shape.h"
|
||||
@@ -88,3 +89,48 @@ struct path *try_mill(struct path *path, double diam, double step)
|
||||
path->first->z, path->r_tool, diam/2, step);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This isn't a perfect solution for the traveling salesman problem, but it's
|
||||
* easy to implement and usually produces results that don't look overly
|
||||
* offensive.
|
||||
*/
|
||||
|
||||
struct path *optimize_paths(struct path *paths)
|
||||
{
|
||||
struct path **walk, **best = NULL;
|
||||
struct path *res = NULL, **anchor = &res;
|
||||
struct path *curr;
|
||||
struct point *p;
|
||||
double best_d = 0, d;
|
||||
|
||||
for (walk = &paths; *walk; walk = &(*walk)->next) {
|
||||
p = (*walk)->first;
|
||||
if (!p)
|
||||
continue;
|
||||
d = hypot(p->x, p->y);
|
||||
if (!best || d < best_d) {
|
||||
best = walk;
|
||||
best_d = d;
|
||||
}
|
||||
}
|
||||
while (best) {
|
||||
curr = *best;
|
||||
*anchor = *best;
|
||||
anchor = &curr->next;
|
||||
*best = curr->next;
|
||||
best = NULL;
|
||||
for (walk = &paths; *walk; walk = &(*walk)->next) {
|
||||
p = (*walk)->first;
|
||||
if (!p)
|
||||
continue;
|
||||
d = hypot(p->x-curr->last->x, p->y-curr->last->y);
|
||||
if (!best || d < best_d) {
|
||||
best = walk;
|
||||
best_d = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user