1 |
/* Copyright (c) 1989 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* genworm.c - program to generate worms (strings with varying thickness). |
9 |
* |
10 |
* The program takes as input the functions of t for x, y, |
11 |
* z and r (the radius). Cylinders and cones will be |
12 |
* joined by spheres. Negative radii are forbidden. |
13 |
* |
14 |
* 9/24/87 |
15 |
*/ |
16 |
|
17 |
#include <stdio.h> |
18 |
|
19 |
#include "fvect.h" |
20 |
|
21 |
#define XNAME "X_" /* x function name */ |
22 |
#define YNAME "Y_" /* y function name */ |
23 |
#define ZNAME "Z_" /* z function name */ |
24 |
#define RNAME "R_" /* r function name */ |
25 |
|
26 |
#define PI 3.14159265358979323846 |
27 |
|
28 |
#define FTINY 1e-7 |
29 |
|
30 |
#define max(a,b) ((a) > (b) ? (a) : (b)) |
31 |
|
32 |
|
33 |
double funvalue(), l_hermite(), argument(); |
34 |
|
35 |
|
36 |
main(argc, argv) |
37 |
int argc; |
38 |
char *argv[]; |
39 |
{ |
40 |
char stmp[256]; |
41 |
double t, f, lastr, r; |
42 |
FVECT lastp, p; |
43 |
int i, nseg; |
44 |
|
45 |
varset("PI", PI, NULL); |
46 |
funset("hermite", 5, l_hermite); |
47 |
|
48 |
if (argc < 8) |
49 |
goto userror; |
50 |
|
51 |
for (i = 8; i < argc; i++) |
52 |
if (!strcmp(argv[i], "-e")) |
53 |
scompile(NULL, argv[++i]); |
54 |
else if (!strcmp(argv[i], "-f")) |
55 |
fcompile(argv[++i]); |
56 |
else |
57 |
goto userror; |
58 |
|
59 |
sprintf(stmp, "%s(t)=%s;", XNAME, argv[3]); |
60 |
scompile(NULL, stmp); |
61 |
sprintf(stmp, "%s(t)=%s;", YNAME, argv[4]); |
62 |
scompile(NULL, stmp); |
63 |
sprintf(stmp, "%s(t)=%s;", ZNAME, argv[5]); |
64 |
scompile(NULL, stmp); |
65 |
sprintf(stmp, "%s(t)=%s;", RNAME, argv[6]); |
66 |
scompile(NULL, stmp); |
67 |
nseg = atoi(argv[7]); |
68 |
if (nseg <= 0) |
69 |
goto userror; |
70 |
|
71 |
printhead(argc, argv); |
72 |
|
73 |
for (i = 0; i <= nseg; i++) { |
74 |
t = (double)i/nseg; |
75 |
p[0] = funvalue(XNAME, 1, &t); |
76 |
p[1] = funvalue(YNAME, 1, &t); |
77 |
p[2] = funvalue(ZNAME, 1, &t); |
78 |
r = funvalue(RNAME, 1, &t); |
79 |
if (i) |
80 |
if (lastr <= r+FTINY && lastr >= r-FTINY) { |
81 |
printf("\n%s cylinder %s.c%d\n", |
82 |
argv[1], argv[2], i); |
83 |
printf("0\n0\n7\n"); |
84 |
printf("%18.12g %18.12g %18.12g\n", |
85 |
lastp[0], lastp[1], lastp[2]); |
86 |
printf("%18.12g %18.12g %18.12g\n", |
87 |
p[0], p[1], p[2]); |
88 |
printf("%18.12g\n", r); |
89 |
} else { |
90 |
printf("\n%s cone %s.c%d\n", |
91 |
argv[1], argv[2], i); |
92 |
printf("0\n0\n8\n"); |
93 |
f = (lastr - r)/dist2(lastp,p); |
94 |
printf("%18.12g %18.12g %18.12g\n", |
95 |
lastp[0] + f*lastr*(p[0] - lastp[0]), |
96 |
lastp[1] + f*lastr*(p[1] - lastp[1]), |
97 |
lastp[2] + f*lastr*(p[2] - lastp[2])); |
98 |
printf("%18.12g %18.12g %18.12g\n", |
99 |
p[0] + f*r*(p[0] - lastp[0]), |
100 |
p[1] + f*r*(p[1] - lastp[1]), |
101 |
p[2] + f*r*(p[2] - lastp[2])); |
102 |
f = 1.0 - (lastr-r)*f; |
103 |
f = f <= 0.0 ? 0.0 : sqrt(f); |
104 |
printf("%18.12g %18.12g\n", f*lastr, f*r); |
105 |
} |
106 |
printf("\n%s sphere %s.s%d\n", argv[1], argv[2], i); |
107 |
printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n", |
108 |
p[0], p[1], p[2], r); |
109 |
VCOPY(lastp, p); |
110 |
lastr = r; |
111 |
} |
112 |
quit(0); |
113 |
|
114 |
userror: |
115 |
fprintf(stderr, |
116 |
"Usage: %s material name x(t) y(t) z(t) r(t) nseg [-e expr] [-f file]\n", |
117 |
argv[0]); |
118 |
quit(1); |
119 |
} |
120 |
|
121 |
|
122 |
eputs(msg) |
123 |
char *msg; |
124 |
{ |
125 |
fputs(msg, stderr); |
126 |
} |
127 |
|
128 |
|
129 |
wputs(msg) |
130 |
char *msg; |
131 |
{ |
132 |
eputs(msg); |
133 |
} |
134 |
|
135 |
|
136 |
quit(code) |
137 |
{ |
138 |
exit(code); |
139 |
} |
140 |
|
141 |
|
142 |
printhead(ac, av) /* print command header */ |
143 |
register int ac; |
144 |
register char **av; |
145 |
{ |
146 |
putchar('#'); |
147 |
while (ac--) { |
148 |
putchar(' '); |
149 |
fputs(*av++, stdout); |
150 |
} |
151 |
putchar('\n'); |
152 |
} |
153 |
|
154 |
|
155 |
double |
156 |
l_hermite() |
157 |
{ |
158 |
double t; |
159 |
|
160 |
t = argument(5); |
161 |
return( argument(1)*((2.0*t-3.0)*t*t+1.0) + |
162 |
argument(2)*(-2.0*t+3.0)*t*t + |
163 |
argument(3)*((t-2.0)*t+1.0)*t + |
164 |
argument(4)*(t-1.0)*t*t ); |
165 |
} |