r5706 - developers/werner/cncmap/zmap
werner at docs.openmoko.org
werner at docs.openmoko.org
Fri Oct 23 23:44:50 CEST 2009
Author: werner
Date: 2009-10-23 23:44:49 +0200 (Fri, 23 Oct 2009)
New Revision: 5706
Modified:
developers/werner/cncmap/zmap/try.c
developers/werner/cncmap/zmap/zline.c
developers/werner/cncmap/zmap/zline.h
Log:
Preparing zmap for batch use.
- zmap/zline.c (zpoint): map just a single point (for holes)
- zmap/try.c: added single point and file mode
Modified: developers/werner/cncmap/zmap/try.c
===================================================================
--- developers/werner/cncmap/zmap/try.c 2009-10-22 23:52:25 UTC (rev 5705)
+++ developers/werner/cncmap/zmap/try.c 2009-10-23 21:44:49 UTC (rev 5706)
@@ -1,18 +1,91 @@
+/*
+ * main.c - Command-line interface to zmap and zline
+ *
+ * Written 2009 by Werner Almesberger
+ * Copyright 2009 Werner Almesberger
+ *
+ * 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.
+ */
+
+
#include <stdlib.h>
#include <stdio.h>
+#include <math.h>
+#include "zmap.h"
#include "zline.h"
-static void point (void *user, double x, double y, double z)
+#define EPSILON 0.001 /* 1 um */
+
+
+struct line {
+ struct xyz a, b;
+};
+
+
+static void point(void *user, double x, double y, double z)
{
printf("%f %f %f\n", x, y, z);
}
+static void point_3d(void *user, double x, double y, double z)
+{
+ const struct line *line = user;
+ double len, pos;
+ double f;
+
+ len = hypot(line->b.x-line->a.x, line->b.y-line->a.y);
+ pos = hypot(x-line->a.x, y-line->a.y);
+ f = len < EPSILON ? 0.5 : pos/len;
+ f = 0.5;
+ z += f*line->b.z+(1-f)*line->a.z;
+ point(NULL, x, y, z);
+}
+
+
+static void process_file(FILE *file)
+{
+ int lineno = 0;
+ char buf[1024];
+ struct line line;
+ int first = 1;
+ int n;
+
+ while (fgets(buf, sizeof(buf), file)) {
+ lineno++;
+ n = sscanf(buf, "%lf %lf %lf\n",
+ &line.b.x, &line.b.y, &line.b.z);
+ switch (n) {
+ case -1:
+ printf("\n");
+ first = 1;
+ continue;
+ case 2:
+ line.b.z = 0;
+ /* fall through */
+ case 3:
+ break;
+ default:
+ fprintf(stderr, "invalid data at line %d\n", lineno);
+ exit(1);
+ }
+ if (!first)
+ zline(line.a.x, line.a.y, line.b.x, line.b.y,
+ point_3d, &line);
+ line.a = line.b;
+ first = 0;
+ }
+}
+
+
static void usage(const char *name)
{
- fprintf(stderr, "usage: %s zmap xa ya xb yb\n", name);
+ fprintf(stderr, "usage: %s zmap_file [xa ya [xb yb]]\n", name);
exit(1);
}
@@ -23,15 +96,32 @@
double p[4];
int i;
- if (argc != 6)
+ switch (argc) {
+ case 2:
+ break;
+ case 4:
+ case 6:
+ for (i = 2; i != argc; i++) {
+ p[i-2] = strtod(argv[i], &end);
+ if (*end)
+ usage(*argv);
+ }
+ break;
+ default:
usage(*argv);
+ }
zline_init(argv[1]);
- for (i = 0; i != 4; i++) {
- p[i] = strtod(argv[i+2], &end);
- if (*end)
- usage(*argv);
+ switch (argc) {
+ case 2:
+ process_file(stdin);
+ break;
+ case 4:
+ zpoint(p[0], p[1], point, NULL);
+ break;
+ case 6:
+ zline(p[0], p[1], p[2], p[3], point, NULL);
+ break;
}
- zline(p[0], p[1], p[2], p[3], point, NULL);
zline_end();
return 0;
}
Modified: developers/werner/cncmap/zmap/zline.c
===================================================================
--- developers/werner/cncmap/zmap/zline.c 2009-10-22 23:52:25 UTC (rev 5705)
+++ developers/werner/cncmap/zmap/zline.c 2009-10-23 21:44:49 UTC (rev 5706)
@@ -130,9 +130,9 @@
assert(map_n >= 3);
zmap_sort(map, map_n, xa, ya);
- proj = zmap_project(map, map_n, xa, ya, xb, yb);
point(user, xa, ya, zmap_point(map[0], map[1], map[2], xa, ya));
+ proj = zmap_project(map, map_n, xa, ya, xb, yb);
t = 0;
while (1) {
best_t = 1;
@@ -163,6 +163,19 @@
}
+/*
+ * Like zline, but do just the first point.
+ */
+
+void zpoint(double x, double y,
+ void (*point)(void *user, double x, double y, double z), void *user)
+{
+ assert(map_n >= 3);
+ zmap_sort(map, map_n, x, y);
+ point(user, x, y, zmap_point(map[0], map[1], map[2], x, y));
+}
+
+
void zline_end(void)
{
free(map);
Modified: developers/werner/cncmap/zmap/zline.h
===================================================================
--- developers/werner/cncmap/zmap/zline.h 2009-10-22 23:52:25 UTC (rev 5705)
+++ developers/werner/cncmap/zmap/zline.h 2009-10-23 21:44:49 UTC (rev 5706)
@@ -17,6 +17,8 @@
void zline_init(const char *name);
void zline(double xa, double ya, double xb, double yb,
void (*point)(void *user, double x, double y, double z), void *user);
+void zpoint(double x, double y,
+ void (*point)(void *user, double x, double y, double z), void *user);
void zline_end(void);
#endif /* ZLINE_H */
More information about the commitlog
mailing list