ptrude/: proper command-line handling; open -d for debug mode

This commit is contained in:
Werner Almesberger 2011-07-29 00:25:28 -03:00
parent 188cf9d9d4
commit 3dcd5cc9aa
4 changed files with 130 additions and 17 deletions

View File

@ -42,7 +42,7 @@ endif
$(MAIN): $(OBJS)
try: $(MAIN)
./$(MAIN) <try | tee out
./$(MAIN) -d try 1 0.1 | tee out
clean:
rm -f $(OBJS) $(OBJS:.o=.d)

View File

@ -15,6 +15,7 @@
#include <string.h>
#include <math.h>
#include "ptrude.h"
#include "path.h"
@ -169,13 +170,16 @@ static const struct vertex *corner(struct path *path, const struct vertex *a,
*/
dd = hypot(r, s)-r;
fprintf(stderr, "a = (%g, %g)-(%g, %g) = (%g, %g); |a| = %g\n",
b->x, b->y, a->x, a->y, ax, ay, aa);
fprintf(stderr, "b = (%g, %g)-(%g, %g) = (%g, %g); |b| = %g\n",
c->x, c->y, b->x, b->y, bx, by, bb);
fprintf(stderr, "sin 2t = %g, cos 2t = %g, tan t = %g\n",
cp/aa/bb, dp/aa/bb, tt);
fprintf(stderr, "r = %g, d = %g, s = %g, dd = %g\n", r, d, s, dd);
if (debug) {
fprintf(stderr, "a = (%g, %g)-(%g, %g) = (%g, %g); |a| = %g\n",
b->x, b->y, a->x, a->y, ax, ay, aa);
fprintf(stderr, "b = (%g, %g)-(%g, %g) = (%g, %g); |b| = %g\n",
c->x, c->y, b->x, b->y, bx, by, bb);
fprintf(stderr, "sin 2t = %g, cos 2t = %g, tan t = %g\n",
cp/aa/bb, dp/aa/bb, tt);
fprintf(stderr, "r = %g, d = %g, s = %g, dd = %g\n",
r, d, s, dd);
}
/*
* We only know how to make a rounded corner if two vectors are
@ -228,8 +232,9 @@ static const struct vertex *corner(struct path *path, const struct vertex *a,
q = acos((r-d)/(r+d));
fprintf(stderr, "t2 = %g, p(max) = %g, q(max) = %g\n",
deg(t2), deg(p), deg(q));
if (debug)
fprintf(stderr, "t2 = %g, p(max) = %g, q(max) = %g\n",
deg(t2), deg(p), deg(q));
/*
* Step 4: emit the starting point of the arc
@ -279,8 +284,9 @@ static const struct vertex *corner(struct path *path, const struct vertex *a,
for (i = 0; i <= n; i++) {
x += f*ax*cos(ang-q)-dir*f*ay*sin(ang-q);
y += dir*f*ax*sin(ang-q)+f*ay*cos(ang-q);
fprintf(stderr, " %d/%d: %g %g @ %g\n", i, n,
x, y, deg(ang));
if (debug)
fprintf(stderr, " %d/%d: %g %g @ %g\n", i, n,
x, y, deg(ang));
add_vertex(path, x, y, 0, 0, NULL);
ang += 2*q;
f = (2*v)/aa;

View File

@ -10,18 +10,107 @@
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "path.h"
#include "ptrude.h"
int main(void)
int debug = 0;
static FILE *open_file(const char *name, const char *mode)
{
const struct path *path;
FILE *file;
path = load_path(stdin);
path = round_path(path, 1, 0.1);
save_path(stdout, path);
if (!strcmp(name, "-"))
return strcmp(mode, "r") ? stdout : stdin;
file = fopen(name, mode);
if (!file) {
perror(name);
exit(1);
}
return file;
}
static void close_file(FILE *file)
{
if (file == stdin || file == stdout)
return;
if (fclose(file) == EOF) {
perror("fclose");
exit(1);
}
}
static void usage(const char *name)
{
fprintf(stderr,
"usage: %s shape radius tolerance\n"
"%6s %s path shape radius tolerance\n",
name, "", name);
exit(1);
}
int main(int argc, char **argv)
{
FILE *file;
const struct path *path = NULL, *shape;
double r, d;
char *end;
int c;
while ((c = getopt(argc, argv, "d")) != EOF)
switch (c) {
case 'd':
debug = 1;
break;
default:
usage(*argv);
}
switch (argc-optind) {
case 3:
break;
case 4:
file = open_file(argv[optind], "r");
path = load_path(file);
close_file(file);
break;
default:
usage(*argv);
}
file = open_file(argv[argc-3], "r");
shape = load_path(file);
close_file(file);
r = strtod(argv[argc-2], &end);
if (*end || r <= 0)
usage(*argv);
d = strtod(argv[argc-1], &end);
if (*end || d <= 0)
usage (*argv);
if (path) {
/*
* We split the error budget evenly between path and shape.
*/
path = round_path(path, r, sqrt(d));
shape = round_path(shape, r, sqrt(d));
/* ... now all that's missing is the extrusion :-) */
} else {
shape = round_path(shape, r, d);
save_path(stdout, shape);
}
return 0;
}

18
ptrude/ptrude.h Normal file
View File

@ -0,0 +1,18 @@
/*
* ptrude.h - Extrusion of a 2D path along a perpendicular 2D path
*
* Written 2011 by Werner Almesberger
* Copyright 2011 by 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.
*/
#ifndef PTRUDE_H
#define PTRUDE_H
extern int debug;
#endif /* !PTRUDE_H */