1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* genprism.c - generate a prism. |
6 |
|
* 2D vertices in the xy plane are given on the |
11 |
|
|
12 |
|
#include <stdio.h> |
13 |
|
|
14 |
+ |
#include <stdlib.h> |
15 |
+ |
|
16 |
|
#include <math.h> |
17 |
|
|
18 |
|
#include <ctype.h> |
21 |
|
|
22 |
|
#define FTINY 1e-6 |
23 |
|
|
25 |
– |
#ifdef DCL_ATOF |
26 |
– |
extern double atof(); |
27 |
– |
#endif |
28 |
– |
|
24 |
|
char *pmtype; /* material type */ |
25 |
|
char *pname; /* name */ |
26 |
|
|
27 |
|
double lvect[3] = {0.0, 0.0, 1.0}; |
28 |
+ |
int lvdir = 1; |
29 |
|
double llen = 1.0; |
30 |
|
|
31 |
|
double vert[MAXVERT][2]; |
36 |
|
|
37 |
|
int do_ends = 1; /* include end caps */ |
38 |
|
int iscomplete = 0; /* polygon is already completed */ |
39 |
< |
double crad = 0.0; /* radius for corners */ |
39 |
> |
double crad = 0.0; /* radius for corners (sign from lvdir) */ |
40 |
|
|
41 |
< |
#define rounding (crad > FTINY) |
41 |
> |
extern double compute_rounding(); |
42 |
|
|
43 |
|
|
44 |
|
main(argc, argv) |
82 |
|
lvect[0] = atof(argv[++an]); |
83 |
|
lvect[1] = atof(argv[++an]); |
84 |
|
lvect[2] = atof(argv[++an]); |
85 |
< |
llen = sqrt(lvect[0]*lvect[0] + lvect[1]*lvect[1] + |
86 |
< |
lvect[2]*lvect[2]); |
87 |
< |
if (llen <= FTINY) { |
88 |
< |
fprintf(stderr, "%s: zero extrusion vector\n", |
85 |
> |
if (lvect[2] < -FTINY) |
86 |
> |
lvdir = -1; |
87 |
> |
else if (lvect[2] > FTINY) |
88 |
> |
lvdir = 1; |
89 |
> |
else { |
90 |
> |
fprintf(stderr, |
91 |
> |
"%s: illegal extrusion vector\n", |
92 |
|
argv[0]); |
93 |
|
exit(1); |
94 |
|
} |
95 |
+ |
llen = sqrt(lvect[0]*lvect[0] + lvect[1]*lvect[1] + |
96 |
+ |
lvect[2]*lvect[2]); |
97 |
|
break; |
98 |
|
case 'r': /* radius */ |
99 |
|
crad = atof(argv[++an]); |
108 |
|
goto userr; |
109 |
|
} |
110 |
|
} |
111 |
< |
if (rounding) { |
112 |
< |
if (crad > fabs(lvect[2])) { |
111 |
> |
if (crad > FTINY) { |
112 |
> |
if (crad > lvdir*lvect[2]) { |
113 |
|
fprintf(stderr, "%s: rounding greater than height\n", |
114 |
|
argv[0]); |
115 |
|
exit(1); |
116 |
|
} |
117 |
+ |
crad *= lvdir; /* simplifies formulas */ |
118 |
|
compute_rounding(); |
119 |
< |
} |
120 |
< |
printhead(argc, argv); |
119 |
< |
|
120 |
< |
if (do_ends) |
121 |
< |
if (rounding) |
119 |
> |
printhead(argc, argv); |
120 |
> |
if (do_ends) |
121 |
|
printrends(); |
122 |
< |
else |
122 |
> |
printsides(1); |
123 |
> |
} else { |
124 |
> |
printhead(argc, argv); |
125 |
> |
if (do_ends) |
126 |
|
printends(); |
127 |
< |
|
128 |
< |
printsides(rounding); |
127 |
< |
|
127 |
> |
printsides(0); |
128 |
> |
} |
129 |
|
exit(0); |
130 |
|
userr: |
131 |
|
fprintf(stderr, "Usage: %s material name ", argv[0]); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
+ |
double |
156 |
|
compute_rounding() /* compute vectors for rounding operations */ |
157 |
|
{ |
158 |
|
register int i; |
159 |
|
register double *v0, *v1; |
160 |
< |
double l; |
160 |
> |
double l, asum; |
161 |
|
|
162 |
|
v0 = vert[nverts-1]; |
163 |
|
for (i = 0; i < nverts; i++) { /* compute u[*] */ |
173 |
|
u[i][1] /= l; |
174 |
|
v0 = v1; |
175 |
|
} |
176 |
+ |
asum = 0.; |
177 |
|
v1 = u[0]; |
178 |
|
for (i = nverts; i--; ) { /* compute a[*] */ |
179 |
|
v0 = u[i]; |
186 |
|
a[i] = 0.; |
187 |
|
else { |
188 |
|
a[i] = sqrt((1-l)/(1+l)); |
189 |
< |
if ((v1[0]*v0[1]-v1[1]*v0[0] > 0.) != (lvect[2] > 0.)) |
189 |
> |
asum += l = v1[0]*v0[1]-v1[1]*v0[0]; |
190 |
> |
if (l < 0.) |
191 |
|
a[i] = -a[i]; |
192 |
|
} |
193 |
|
v1 = v0; |
194 |
|
} |
195 |
+ |
return(asum*.5); |
196 |
|
} |
197 |
|
|
198 |
|
|
227 |
|
vert[i][1] + crad*(a[i]*u[i][1] + u[i][0]), |
228 |
|
0.0); |
229 |
|
} |
230 |
+ |
/* top face */ |
231 |
+ |
printf("\n%s polygon %s.t\n", pmtype, pname); |
232 |
+ |
printf("0\n0\n%d\n", nverts*3); |
233 |
+ |
for (i = nverts; i--; ) { |
234 |
+ |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
235 |
+ |
vert[i][0] + lvect[0] + crad*(a[i]*u[i][0] - u[i][1]), |
236 |
+ |
vert[i][1] + lvect[1] + crad*(a[i]*u[i][1] + u[i][0]), |
237 |
+ |
lvect[2]); |
238 |
+ |
} |
239 |
|
/* bottom corners and edges */ |
240 |
|
c0[0] = cl[0] = vert[nverts-1][0] + |
241 |
|
crad*(a[nverts-1]*u[nverts-1][0] - u[nverts-1][1]); |
250 |
|
} else { |
251 |
|
c1[0] = cl[0]; c1[1] = cl[1]; c1[2] = cl[2]; |
252 |
|
} |
253 |
< |
if (a[i] > 0.) { |
253 |
> |
if (lvdir*a[i] > 0.) { |
254 |
|
printf("\n%s sphere %s.bc%d\n", pmtype, pname, i+1); |
255 |
|
printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n", |
256 |
< |
c1[0], c1[1], c1[2], crad); |
256 |
> |
c1[0], c1[1], c1[2], lvdir*crad); |
257 |
|
} |
258 |
|
printf("\n%s cylinder %s.be%d\n", pmtype, pname, i+1); |
259 |
|
printf("0\n0\n7\n"); |
260 |
|
printf("\t%18.12g\t%18.12g\t%18.12g\n", c0[0], c0[1], c0[2]); |
261 |
|
printf("\t%18.12g\t%18.12g\t%18.12g\n", c1[0], c1[1], c1[2]); |
262 |
< |
printf("\t%18.12g\n", crad); |
262 |
> |
printf("\t%18.12g\n", lvdir*crad); |
263 |
|
c0[0] = c1[0]; c0[1] = c1[1]; c0[2] = c1[2]; |
264 |
|
} |
251 |
– |
/* top face */ |
252 |
– |
printf("\n%s polygon %s.t\n", pmtype, pname); |
253 |
– |
printf("0\n0\n%d\n", nverts*3); |
254 |
– |
for (i = nverts; i--; ) { |
255 |
– |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
256 |
– |
vert[i][0] + lvect[0] + crad*(a[i]*u[i][0] - u[i][1]), |
257 |
– |
vert[i][1] + lvect[1] + crad*(a[i]*u[i][1] + u[i][0]), |
258 |
– |
lvect[2]); |
259 |
– |
} |
265 |
|
/* top corners and edges */ |
266 |
|
c0[0] = cl[0] = vert[nverts-1][0] + lvect[0] + |
267 |
|
crad*(a[nverts-1]*u[nverts-1][0] - u[nverts-1][1]); |
278 |
|
} else { |
279 |
|
c1[0] = cl[0]; c1[1] = cl[1]; c1[2] = cl[2]; |
280 |
|
} |
281 |
< |
if (a[i] > 0.) { |
281 |
> |
if (lvdir*a[i] > 0.) { |
282 |
|
printf("\n%s sphere %s.tc%d\n", pmtype, pname, i+1); |
283 |
|
printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n", |
284 |
< |
c1[0], c1[1], c1[2], crad); |
284 |
> |
c1[0], c1[1], c1[2], lvdir*crad); |
285 |
|
} |
286 |
|
printf("\n%s cylinder %s.te%d\n", pmtype, pname, i+1); |
287 |
|
printf("0\n0\n7\n"); |
288 |
|
printf("\t%18.12g\t%18.12g\t%18.12g\n", c0[0], c0[1], c0[2]); |
289 |
|
printf("\t%18.12g\t%18.12g\t%18.12g\n", c1[0], c1[1], c1[2]); |
290 |
< |
printf("\t%18.12g\n", crad); |
290 |
> |
printf("\t%18.12g\n", lvdir*crad); |
291 |
|
c0[0] = c1[0]; c0[1] = c1[1]; c0[2] = c1[2]; |
292 |
|
} |
293 |
|
} |
333 |
|
double s, c, t[3]; |
334 |
|
|
335 |
|
/* compute tanget offset vector */ |
336 |
< |
s = (lvect[1]*u[n1][0] - lvect[0]*u[n1][1])/llen; |
336 |
> |
s = lvdir*(lvect[1]*u[n1][0] - lvect[0]*u[n1][1])/llen; |
337 |
|
if (s < -FTINY || s > FTINY) { |
338 |
|
c = sqrt(1. - s*s); |
339 |
|
t[0] = (c - 1.)*u[n1][1]; |
361 |
|
vert[n1][1] + crad*(t[1] + a[n1]*u[n1][1]), |
362 |
|
crad*(t[2] + 1.)); |
363 |
|
/* output joining edge */ |
364 |
< |
if (a[n1] < 0.) |
364 |
> |
if (lvdir*a[n1] < 0.) |
365 |
|
return; |
366 |
|
printf("\n%s cylinder %s.e%d\n", pmtype, pname, n0+1); |
367 |
|
printf("0\n0\n7\n"); |
373 |
|
vert[n1][0] + lvect[0] + crad*(a[n1]*u[n1][0] - u[n1][1]), |
374 |
|
vert[n1][1] + lvect[1] + crad*(a[n1]*u[n1][1] + u[n1][0]), |
375 |
|
lvect[2] - crad); |
376 |
< |
printf("\t%18.12g\n", crad); |
376 |
> |
printf("\t%18.12g\n", lvdir*crad); |
377 |
|
} |
378 |
|
|
379 |
|
|