Root/cameo/connect.c

1/*
2 * connect.c - Connect paths
3 *
4 * Written 2010-2012, 2015 by Werner Almesberger
5 * Copyright 2010-2012, 2015 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <math.h>
16
17#include "util.h"
18#include "path.h"
19
20
21static void path_reverse_inplace(struct path *path)
22{
23    struct point *points = NULL, *p, *next;
24
25    path->last = path->first;
26    for (p = path->first; p; p = next) {
27        next = p->next;
28        p->next = points;
29        points = p;
30    }
31    path->first = points;
32}
33
34
35static int attr_eq(const struct path *a, const struct path *b)
36{
37    return a->r_tool == b->r_tool && a->outside == b->outside &&
38        a->notch == b->notch;
39}
40
41
42struct path *path_connect(struct path *path)
43{
44    struct path **a, **b;
45    struct path *tmp;
46
47again:
48    for (a = &path; *a; a = &(*a)->next)
49        for (b = &(*a)->next; *b; b = &(*b)->next) {
50            if (!attr_eq(*a, *b))
51                continue;
52            if (points_eq((*a)->last, (*b)->last))
53                path_reverse_inplace(*b);
54            if (points_eq((*a)->last, (*b)->first)) {
55                (*a)->last->next = (*b)->first->next;
56                (*a)->last = (*b)->last;
57                free((*b)->first);
58                tmp = *b;
59                *b = tmp->next;
60                free(tmp);
61                goto again;
62            }
63            if (points_eq((*a)->first, (*b)->first))
64                path_reverse_inplace(*b);
65            if (points_eq((*a)->first, (*b)->last)) {
66                (*b)->last->next = (*a)->first->next;
67                (*b)->last = (*a)->last;
68                free((*a)->first);
69                tmp = *a;
70                *a = tmp->next;
71                free(tmp);
72                goto again;
73            }
74        }
75    return path;
76}
77

Archive Download this file

Branches:
master



interactive