1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: genrev.c,v 2.5 2003/06/08 12:03:09 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* genrev.c - program to generate functions of rotation about z |
6 |
* |
7 |
* The program takes as input the functions of t for z and r |
8 |
* (the radius). If z is increasing, the normal points |
9 |
* outward, inward for decreasing z. Negative radii are forbidden. |
10 |
* |
11 |
* 8/6/86 |
12 |
*/ |
13 |
|
14 |
#include <stdlib.h> |
15 |
#include <stdio.h> |
16 |
#include <string.h> |
17 |
#include <math.h> |
18 |
#include "calcomp.h" |
19 |
|
20 |
#define ZNAME "Z`SYS`" /* z function name */ |
21 |
#define RNAME "R`SYS`" /* r function name */ |
22 |
|
23 |
#define PI 3.14159265358979323846 |
24 |
|
25 |
#define FTINY 1e-9 |
26 |
/* orientation */ |
27 |
#define OUT 01 |
28 |
#define IN 02 |
29 |
#define UP 04 |
30 |
#define DOWN 010 |
31 |
|
32 |
void quit(), eputs(), wputs(); |
33 |
|
34 |
|
35 |
computen(nzp, nrp, z0, r0, z1, r1) /* compute normal */ |
36 |
double *nzp, *nrp, z0, r0, z1, r1; |
37 |
{ |
38 |
double dr, dz, len; |
39 |
|
40 |
dz = r0 - r1; /* right angle vector */ |
41 |
dr = z1 - z0; |
42 |
len = sqrt(dr*dr + dz*dz); |
43 |
*nzp = dz/len; |
44 |
*nrp = dr/len; |
45 |
} |
46 |
|
47 |
|
48 |
void |
49 |
eputs(msg) |
50 |
char *msg; |
51 |
{ |
52 |
fputs(msg, stderr); |
53 |
} |
54 |
|
55 |
|
56 |
void |
57 |
wputs(msg) |
58 |
char *msg; |
59 |
{ |
60 |
eputs(msg); |
61 |
} |
62 |
|
63 |
|
64 |
void |
65 |
quit(code) |
66 |
int code; |
67 |
{ |
68 |
exit(code); |
69 |
} |
70 |
|
71 |
|
72 |
printhead(ac, av) /* print command header */ |
73 |
register int ac; |
74 |
register char **av; |
75 |
{ |
76 |
putchar('#'); |
77 |
while (ac--) { |
78 |
putchar(' '); |
79 |
fputs(*av++, stdout); |
80 |
} |
81 |
putchar('\n'); |
82 |
} |
83 |
|
84 |
|
85 |
double |
86 |
l_hermite(char *nm) |
87 |
{ |
88 |
double t; |
89 |
|
90 |
t = argument(5); |
91 |
return( argument(1)*((2.0*t-3.0)*t*t+1.0) + |
92 |
argument(2)*(-2.0*t+3.0)*t*t + |
93 |
argument(3)*((t-2.0)*t+1.0)*t + |
94 |
argument(4)*(t-1.0)*t*t ); |
95 |
} |
96 |
|
97 |
|
98 |
double |
99 |
l_bezier(char *nm) |
100 |
{ |
101 |
double t; |
102 |
|
103 |
t = argument(5); |
104 |
return( argument(1) * (1.+t*(-3.+t*(3.-t))) + |
105 |
argument(2) * 3.*t*(1.+t*(-2.+t)) + |
106 |
argument(3) * 3.*t*t*(1.-t) + |
107 |
argument(4) * t*t*t ); |
108 |
} |
109 |
|
110 |
|
111 |
double |
112 |
l_bspline(char *nm) |
113 |
{ |
114 |
double t; |
115 |
|
116 |
t = argument(5); |
117 |
return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) + |
118 |
argument(2) * (2./3.+t*t*(-1.+1./2.*t)) + |
119 |
argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) + |
120 |
argument(4) * (1./6.*t*t*t) ); |
121 |
} |
122 |
|
123 |
|
124 |
main(argc, argv) |
125 |
int argc; |
126 |
char *argv[]; |
127 |
{ |
128 |
char stmp[256]; |
129 |
char *modname; |
130 |
int smooth = 0; |
131 |
double t, lastz, z, nextz, lastr, r, nextr; |
132 |
double lastnz, lastnr, nz, nr, nextnz, nextnr; |
133 |
int i, nseg; |
134 |
int orient; |
135 |
|
136 |
varset("PI", ':', PI); |
137 |
funset("hermite", 5, ':', l_hermite); |
138 |
funset("bezier", 5, ':', l_bezier); |
139 |
funset("bspline", 5, ':', l_bspline); |
140 |
|
141 |
if (argc < 6) |
142 |
goto userror; |
143 |
|
144 |
for (i = 6; i < argc; i++) |
145 |
if (!strcmp(argv[i], "-e")) |
146 |
scompile(argv[++i], NULL, 0); |
147 |
else if (!strcmp(argv[i], "-f")) |
148 |
fcompile(argv[++i]); |
149 |
else if (!strcmp(argv[i], "-s")) |
150 |
smooth = 1; |
151 |
else |
152 |
goto userror; |
153 |
|
154 |
sprintf(stmp, "%s(t)=%s;", ZNAME, argv[3]); |
155 |
scompile(stmp, NULL, 0); |
156 |
sprintf(stmp, "%s(t)=%s;", RNAME, argv[4]); |
157 |
scompile(stmp, NULL, 0); |
158 |
nseg = atoi(argv[5]); |
159 |
if (nseg <= 0) |
160 |
goto userror; |
161 |
modname = smooth ? "Phong" : argv[1]; |
162 |
|
163 |
printhead(argc, argv); |
164 |
eclock = 0; |
165 |
|
166 |
lastnz = lastnr = 0.0; |
167 |
t = 0.0; |
168 |
lastz = funvalue(ZNAME, 1, &t); |
169 |
lastr = funvalue(RNAME, 1, &t); |
170 |
t = 1.0/nseg; |
171 |
z = funvalue(ZNAME, 1, &t); |
172 |
r = funvalue(RNAME, 1, &t); |
173 |
computen(&nz, &nr, lastz, lastr, z, r); |
174 |
for (i = 1; i <= nseg; i++) { |
175 |
if (i < nseg) { |
176 |
t = (double)(i+1)/nseg; |
177 |
nextz = funvalue(ZNAME, 1, &t); |
178 |
nextr = funvalue(RNAME, 1, &t); |
179 |
computen(&nextnz, &nextnr, z, r, nextz, nextr); |
180 |
} else |
181 |
nextnz = nextnr = 0.0; |
182 |
orient = 0; |
183 |
if (z < lastz-FTINY) |
184 |
orient |= DOWN; |
185 |
else if (z > lastz+FTINY) |
186 |
orient |= UP; |
187 |
if (r < lastr-FTINY) |
188 |
orient |= IN; |
189 |
else if (r > lastr+FTINY) |
190 |
orient |= OUT; |
191 |
if (!orient) |
192 |
goto endfor; |
193 |
if (smooth) { |
194 |
printf("\n%s texfunc Phong\n", argv[1]); |
195 |
printf("4 rev_dx rev_dy rev_dz rev.cal\n"); |
196 |
printf("0\n4\n"); |
197 |
if (orient&(UP|DOWN)) { |
198 |
t = (nextnz - lastnz)/(z - lastz); |
199 |
printf("\t%15.9g\t%15.9g\n", |
200 |
t, lastnz - t*lastz); |
201 |
} else |
202 |
printf("\t0\t%d\n", orient&IN ? 1 : -1); |
203 |
if (orient&(OUT|IN)) { |
204 |
t = (nextnr - lastnr)/(r - lastr); |
205 |
printf("\t%15.9g\t%15.9g\n", |
206 |
t, lastnr - t*lastr); |
207 |
} else |
208 |
printf("\t0\t%d\n", orient&UP ? 1 : -1); |
209 |
} |
210 |
if (!(orient&(IN|OUT))) { |
211 |
printf("\n%s %s %s.%d\n", modname, |
212 |
orient&DOWN ? "tube" : "cylinder", |
213 |
argv[2], i); |
214 |
printf("0\n0\n7\n"); |
215 |
printf("\t0\t0\t%15.9g\n", lastz); |
216 |
printf("\t0\t0\t%15.9g\n", z); |
217 |
printf("\t%15.9g\n", r); |
218 |
} else if (!(orient&(UP|DOWN))) { |
219 |
printf("\n%s ring %s.%d\n", modname, argv[2], i); |
220 |
printf("0\n0\n8\n"); |
221 |
printf("\t0\t0\t%15.9g\n", z); |
222 |
printf("\t0\t0\t%15.9g\n", orient&IN ? 1.0 : -1.0); |
223 |
printf("\t%15.9g\t%15.9g\n", lastr, r); |
224 |
} else { |
225 |
printf("\n%s %s %s.%d\n", modname, |
226 |
orient&DOWN ? "cup" : "cone", |
227 |
argv[2], i); |
228 |
printf("0\n0\n8\n"); |
229 |
printf("\t0\t0\t%15.9g\n", lastz); |
230 |
printf("\t0\t0\t%15.9g\n", z); |
231 |
printf("\t%15.9g\t%15.9g\n", lastr, r); |
232 |
} |
233 |
endfor: |
234 |
lastz = z; lastr = r; |
235 |
z = nextz; r = nextr; |
236 |
lastnz = nz; lastnr = nr; |
237 |
nz = nextnz; nr = nextnr; |
238 |
} |
239 |
quit(0); |
240 |
|
241 |
userror: |
242 |
fprintf(stderr, |
243 |
"Usage: %s material name z(t) r(t) nseg [-e expr] [-f file] [-s]\n", |
244 |
argv[0]); |
245 |
quit(1); |
246 |
} |
247 |
|
248 |
|