2010-12-14 19:15:52 +02:00
|
|
|
/*
|
|
|
|
* ops.c - Higher-level toolpath operations
|
|
|
|
*
|
2011-09-01 03:17:29 +03:00
|
|
|
* Written 2010-2011 by Werner Almesberger
|
|
|
|
* Copyright 2010-2011 Werner Almesberger
|
2010-12-14 19:15:52 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-12-15 09:56:58 +02:00
|
|
|
#include <stddef.h>
|
2010-12-16 19:19:46 +02:00
|
|
|
#include <math.h>
|
2010-12-15 09:56:58 +02:00
|
|
|
|
2010-12-14 19:15:52 +02:00
|
|
|
#include "path.h"
|
2010-12-15 09:56:58 +02:00
|
|
|
#include "shape.h"
|
2010-12-14 19:15:52 +02:00
|
|
|
#include "ops.h"
|
|
|
|
|
|
|
|
|
2011-09-05 13:54:29 +03:00
|
|
|
static struct path *tool_comp_1(const struct path *path, int inside,
|
|
|
|
int dog_bone)
|
2010-12-14 19:15:52 +02:00
|
|
|
{
|
|
|
|
int left;
|
|
|
|
|
|
|
|
left = path_tool_is_left(path);
|
|
|
|
if (inside)
|
2011-09-05 13:54:29 +03:00
|
|
|
return path_offset(path, !left, path->notch);
|
2010-12-14 19:15:52 +02:00
|
|
|
else
|
2011-09-05 13:54:29 +03:00
|
|
|
return path_offset(path, left, path->notch || dog_bone);
|
2010-12-14 19:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-05 13:54:29 +03:00
|
|
|
struct path *tool_comp_paths(const struct path *paths, int dog_bone,
|
|
|
|
int all_inside)
|
2010-12-14 19:15:52 +02:00
|
|
|
{
|
2011-09-05 13:54:29 +03:00
|
|
|
const struct path *leftmost, *path;
|
|
|
|
struct path *new = NULL, **anchor = &new;
|
2010-12-14 19:15:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't have an algorithm (yet) that can detect which paths are
|
|
|
|
* inside other paths. Therefore, we fake it by looking for the path
|
|
|
|
* that contains lowest x coordinate. This ought to be the outer
|
|
|
|
* boundary of the piece.
|
|
|
|
*
|
|
|
|
* Note that this heuristic falls apart when a job consists of
|
|
|
|
* multiple pieces. In this case, the #%outside hint can be used to
|
|
|
|
* explicitly tell cameo to treat the path as an outside edge.
|
|
|
|
*/
|
|
|
|
|
|
|
|
leftmost = path_find_leftmost(paths);
|
|
|
|
for (path = paths; path; path = path->next)
|
2011-09-05 13:54:29 +03:00
|
|
|
if (path != leftmost && (all_inside || !path->outside)) {
|
|
|
|
*anchor = tool_comp_1(path, 1, dog_bone);
|
|
|
|
anchor = &(*anchor)->next;
|
|
|
|
}
|
2010-12-15 04:37:31 +02:00
|
|
|
if (!all_inside)
|
|
|
|
for (path = paths; path; path = path->next)
|
2011-09-05 13:54:29 +03:00
|
|
|
if (path != leftmost && path->outside) {
|
|
|
|
*anchor = tool_comp_1(path, 0, dog_bone);
|
|
|
|
anchor = &(*anchor)->next;
|
|
|
|
}
|
|
|
|
*anchor = tool_comp_1(leftmost, all_inside, dog_bone);
|
|
|
|
return new;
|
2010-12-14 19:15:52 +02:00
|
|
|
}
|
2010-12-15 09:56:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
struct path *try_drill(struct path *path, double d_min, double d_max)
|
|
|
|
{
|
|
|
|
struct path *new;
|
|
|
|
|
|
|
|
if (path->r_tool*2 < d_min || path->r_tool*2 > d_max)
|
|
|
|
return NULL;
|
|
|
|
if (!path->first || path->first != path->last)
|
|
|
|
return NULL;
|
2011-09-05 11:46:25 +03:00
|
|
|
new = path_new((d_min+d_max)/2, path->id); /* @@@ fishy */
|
2010-12-15 09:56:58 +02:00
|
|
|
path_add(new, path->first->x, path->first->y, path->first->z);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-01 03:17:29 +03:00
|
|
|
struct path *try_mill(struct path *path, double diam, double step, int any)
|
2010-12-15 09:56:58 +02:00
|
|
|
{
|
2011-09-01 03:17:29 +03:00
|
|
|
if (!any && path->r_tool*2 < diam)
|
2010-12-15 09:56:58 +02:00
|
|
|
return NULL;
|
|
|
|
if (!path->first)
|
|
|
|
return NULL;
|
|
|
|
if (path->first == path->last)
|
|
|
|
return circle(path->first->x, path->first->y, path->first->z,
|
2011-09-05 11:46:25 +03:00
|
|
|
path->r_tool, diam/2, step, path->id);
|
2010-12-15 09:56:58 +02:00
|
|
|
if (path->first->next == path->last)
|
|
|
|
return slot(path->first->x, path->first->y,
|
|
|
|
path->first->next->x, path->first->next->y,
|
2011-09-05 11:46:25 +03:00
|
|
|
path->first->z, path->r_tool, diam/2, step, path->id);
|
2010-12-15 09:56:58 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-16 19:19:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|