Root/gp2rml/gp2rml.c

1/*
2 * gp2rml.c - Convert from gnuplot to RML
3 *
4 * Written 2010-2013, 2015 by Werner Almesberger
5 * Copyright 2010-2013, 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 * When setting the pen up and pen down positions (!PZ), pen up must be a
15 * zero or positive coordinate relative to Z0 and pen down must be negative
16 * or zero.
17 *
18 * Our pen up position is defined as the maximum pen down height plus some
19 * clearance. All Z coordinates are relative to Z0.
20 *
21 * All this means that we have to set Z0 (with !ZO, that's zet-oh) such that
22 * it is between the maximum pen down and pen up. Since pen up is also defined
23 * via the maximum pen down, we have to read all coordinates before we can
24 * calculate Z0 and then emit the correct positioning commands. Yuck.
25 *
26 * We choose Z0 = maximum pen down.
27 */
28
29/*
30 * @@@ speed selection anomaly:
31 *
32 * Seems that !ZZ movements of the MDX-15 only use !VZ and VS is completely
33 * ignored.
34 */
35
36#include <stdlib.h>
37#include <stdio.h>
38#include <unistd.h>
39#include <string.h>
40#include <math.h>
41
42
43#define V_MAX 15.0
44
45
46struct segment {
47    double x, y, z;
48    struct segment *next;
49};
50
51struct path {
52    struct segment *segments;
53    struct path *next;
54};
55
56
57static struct path *paths = NULL, *curr_path;
58static struct segment **next_seg;
59static double z_max = -1e6; /* -1 km :) */
60static double z_scale = 1; /* < 1: compress; > 1: stretch */
61
62
63#define units(mm) ((int) round((mm) * 40.0))
64
65
66#define alloc_type(t) \
67    ({ t *alloc_tmp = malloc(sizeof(t)); \
68    if (!alloc_tmp) \
69        abort(); \
70    alloc_tmp; })
71
72
73static void new_path(void)
74{
75    struct path *new;
76
77    new = alloc_type(struct path);
78    if (paths)
79        curr_path->next = new;
80    else
81        paths = new;
82    curr_path = new;
83    new->segments = NULL;
84    new->next = NULL;
85    next_seg = &new->segments;
86}
87
88
89static void process_file(FILE *file)
90{
91    int lineno = 0;
92    char buf[1024];
93    double x, y, z;
94    int n;
95    struct segment *seg;
96
97    new_path();
98    while (fgets(buf, sizeof(buf), file)) {
99        lineno++;
100        if (*buf == '!' || *buf == '#')
101            continue;
102        n = sscanf(buf, "%lf %lf %lf\n", &x, &y, &z);
103        switch (n) {
104        case -1:
105            if (curr_path->segments)
106                new_path();
107            continue;
108        case 2:
109            z = 0;
110            /* fall through */
111        case 3:
112            break;
113        default:
114            fprintf(stderr, "invalid data at line %d\n", lineno);
115            exit(1);
116        }
117        seg = alloc_type(struct segment);
118        seg->x = x;
119        seg->y = y;
120        seg->z = z;
121        seg->next = NULL;
122        *next_seg = seg;
123        next_seg = &seg->next;
124        if (z_max < z)
125            z_max = z;
126    }
127}
128
129
130static void output_paths(double z_clear, double xy_speed, double z_speed)
131{
132    const struct path *path;
133    const struct segment *seg;
134    double x = 0, y = 0, z = 0;
135    double d, s = 0, t = 0;
136    double speed;
137
138    printf("IN;!MC1;PA\n");
139    printf("!ZO%d;!PZ0,%d;PU\n", units(z_max), units(z_clear));
140    printf("VS%f;!VZ%f\n", xy_speed, z_speed);
141
142    for (path = paths; path; path = path->next) {
143        seg = path->segments;
144        if (!seg)
145            continue;
146        printf("!PZ%d,%d;PA%d,%d;PD\n",
147            units(seg->z-z_max), units(z_clear),
148            units(seg->x), units(seg->y));
149        d = hypot(x - seg->x, y - seg->y);
150        s += d;
151        s += z - z_max;
152        t += d / V_MAX;
153        t += (z - z_max) / z_speed;
154        x = seg->x;
155        y = seg->y;
156        z = seg->z;
157        seg = seg->next;
158        while (seg) {
159            if (path->segments->z == z && z == seg->z) {
160                printf("PA%d,%d\n",
161                    units(seg->x), units(seg->y));
162                speed = xy_speed;
163            } else {
164                printf("!ZZ%d,%d,%d\n",
165                    units(seg->x), units(seg->y),
166                    units((seg->z - z_max) * z_scale));
167                speed = z_speed;
168            }
169            d = hypot(x - seg->x, y - seg->y);
170            d = hypot(d, z - seg->z);
171            t += d / speed;
172            s += d;
173            x = seg->x;
174            y = seg->y;
175            z = seg->z;
176            seg = seg->next;
177        }
178        printf("PU\n");
179        d = z_max + z_clear - z;
180        s += d;
181        t += d / V_MAX;
182        z = z_max + z_clear;
183    }
184
185    printf("!MC0;!ZO0;!PZ0,0;PU0,0;IN\n");
186    d = -z + hypot(x, y);
187    s += d;
188    t += d / V_MAX;
189    fprintf(stderr, "Distance %.1f mm, time %.1f s\n", s, t);
190}
191
192
193static void usage(const char *name)
194{
195    fprintf(stderr,
196"usage: %s [-s z_scale] z_clear[mm] xy_speed z_speed [file]\n\n"
197" -s z_scale scale Z axis by z_scale (default: 1.0)\n"
198" -t top set the top elevation, in mm (default: auto-detect)\n"
199" z_clear clearance above the highest peak, in mm (must be > 0)\n"
200" Unit (\"mm\") can optionally be specified\n"
201" xy_speed cutting speed, in mm/s\n"
202" z_speed vertical speed when lowering the pen, in mm/s\n"
203    , name);
204    exit(1);
205}
206
207
208int main(int argc, char *const *argv)
209{
210    FILE *file;
211    char *end;
212    int i;
213    double p[3];
214    int c;
215
216     while ((c = getopt(argc, argv, "s:t:")) != EOF)
217        switch (c) {
218        case 's':
219            z_scale = strtod(optarg, &end);
220            if (*end)
221                usage(*argv);
222            break;
223        case 't':
224            z_max = strtod(optarg, &end);
225            if (*end && !strcmp(end, "mm"))
226                usage(*argv);
227            break;
228        default:
229            usage(*argv);
230        }
231
232    switch (argc - optind) {
233    case 3:
234        file = stdin;
235        break;
236    case 4:
237        file = fopen(argv[optind + 3], "r");
238        if (!file) {
239            perror(argv[optind + 3]);
240            return 1;
241        }
242        break;
243    default:
244        usage(*argv);
245    }
246    for (i = 0; i != 3; i++) {
247        p[i] = strtod(argv[optind + i], &end);
248        /*
249         * Allow the clearance to have a unit, for consistency in
250         * mkmk-simple
251         */
252        if (!i && *end && !strcmp(end, "mm"))
253            continue;
254        if (*end || p[i] <= 0)
255            usage(*argv);
256    }
257
258    process_file(file);
259    output_paths(p[0], p[1], p[2]);
260
261    if (ferror(stdout))
262        perror("stdout");
263    if (fclose(stdout) == EOF)
264        perror("stdout");
265
266    return 0;
267}
268

Archive Download this file

Branches:
master



interactive