1 |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* genprism.c - generate a prism. |
9 |
* 2D vertices in the xy plane are given on the |
10 |
* command line or from a file. Their order together |
11 |
* with the extrude direction will determine surface |
12 |
* orientation. |
13 |
*/ |
14 |
|
15 |
#include <stdio.h> |
16 |
|
17 |
#include <math.h> |
18 |
|
19 |
#include <ctype.h> |
20 |
|
21 |
#define MAXVERT 1024 /* maximum # vertices */ |
22 |
|
23 |
#ifdef DCL_ATOF |
24 |
extern double atof(); |
25 |
#endif |
26 |
|
27 |
char *pmtype; /* material type */ |
28 |
char *pname; /* name */ |
29 |
|
30 |
double lvect[3] = {0.0, 0.0, 1.0}; |
31 |
|
32 |
double vert[MAXVERT][2]; |
33 |
int nverts = 0; |
34 |
|
35 |
int do_ends = 1; /* include end caps */ |
36 |
int iscomplete = 0; /* polygon is already completed */ |
37 |
|
38 |
|
39 |
main(argc, argv) |
40 |
int argc; |
41 |
char **argv; |
42 |
{ |
43 |
int an; |
44 |
|
45 |
if (argc < 4) |
46 |
goto userr; |
47 |
|
48 |
pmtype = argv[1]; |
49 |
pname = argv[2]; |
50 |
|
51 |
if (!strcmp(argv[3], "-")) { |
52 |
readverts(NULL); |
53 |
an = 4; |
54 |
} else if (isdigit(argv[3][0])) { |
55 |
nverts = atoi(argv[3]); |
56 |
if (nverts > MAXVERT) { |
57 |
fprintf(stderr, "%s: too many vertices (%d limit)\n", |
58 |
argv[0], MAXVERT); |
59 |
exit(1); |
60 |
} |
61 |
if (argc-3 < 2*nverts) |
62 |
goto userr; |
63 |
for (an = 0; an < nverts; an++) { |
64 |
vert[an][0] = atof(argv[2*an+4]); |
65 |
vert[an][1] = atof(argv[2*an+5]); |
66 |
} |
67 |
an = 2*nverts+4; |
68 |
} else { |
69 |
readverts(argv[3]); |
70 |
an = 4; |
71 |
} |
72 |
if (nverts < 3) { |
73 |
fprintf(stderr, "%s: not enough vertices\n", argv[0]); |
74 |
exit(1); |
75 |
} |
76 |
|
77 |
for ( ; an < argc; an++) { |
78 |
if (argv[an][0] != '-') |
79 |
goto userr; |
80 |
switch (argv[an][1]) { |
81 |
case 'l': /* length vector */ |
82 |
lvect[0] = atof(argv[++an]); |
83 |
lvect[1] = atof(argv[++an]); |
84 |
lvect[2] = atof(argv[++an]); |
85 |
break; |
86 |
case 'e': /* ends */ |
87 |
do_ends = !do_ends; |
88 |
break; |
89 |
case 'c': /* complete */ |
90 |
iscomplete = !iscomplete; |
91 |
break; |
92 |
default: |
93 |
goto userr; |
94 |
} |
95 |
} |
96 |
|
97 |
printhead(argc, argv); |
98 |
|
99 |
if (do_ends) |
100 |
printends(); |
101 |
printsides(); |
102 |
|
103 |
return(0); |
104 |
userr: |
105 |
fprintf(stderr, "Usage: %s material name ", argv[0]); |
106 |
fprintf(stderr, "{ - | vfile | N v1 v2 .. vN } "); |
107 |
fprintf(stderr, "[-l lvect][-c][-e]\n"); |
108 |
exit(1); |
109 |
} |
110 |
|
111 |
|
112 |
readverts(fname) /* read vertices from a file */ |
113 |
char *fname; |
114 |
{ |
115 |
FILE *fp; |
116 |
|
117 |
if (fname == NULL) { |
118 |
fp = stdin; |
119 |
fname = "<stdin>"; |
120 |
} else if ((fp = fopen(fname, "r")) == NULL) { |
121 |
fprintf(stderr, "%s: cannot open\n", fname); |
122 |
exit(1); |
123 |
} |
124 |
while (fscanf(fp, "%lf %lf", &vert[nverts][0], &vert[nverts][1]) == 2) |
125 |
if (++nverts >= MAXVERT) { |
126 |
fprintf(stderr, "%s: too many vertices (%d limit)\n", |
127 |
fname, MAXVERT-1); |
128 |
exit(1); |
129 |
} |
130 |
fclose(fp); |
131 |
} |
132 |
|
133 |
|
134 |
printends() /* print ends of prism */ |
135 |
{ |
136 |
register int i; |
137 |
|
138 |
printf("\n%s polygon %s.b\n", pmtype, pname); |
139 |
printf("0\n0\n%d\n", nverts*3); |
140 |
for (i = 0; i < nverts; i++) { |
141 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
142 |
vert[i][0], |
143 |
vert[i][1], |
144 |
0.0); |
145 |
} |
146 |
printf("\n%s polygon %s.e\n", pmtype, pname); |
147 |
printf("0\n0\n%d\n", nverts*3); |
148 |
for (i = nverts-1; i >= 0; i--) { |
149 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
150 |
vert[i][0]+lvect[0], |
151 |
vert[i][1]+lvect[1], |
152 |
lvect[2]); |
153 |
} |
154 |
} |
155 |
|
156 |
|
157 |
printsides() /* print prism sides */ |
158 |
{ |
159 |
register int i; |
160 |
|
161 |
for (i = 0; i < nverts-1; i++) |
162 |
side(i, i+1); |
163 |
if (!iscomplete) |
164 |
side(nverts-1, 0); |
165 |
} |
166 |
|
167 |
|
168 |
side(n1, n2) /* print single side */ |
169 |
register int n1, n2; |
170 |
{ |
171 |
printf("\n%s polygon %s.%d\n", pmtype, pname, n1+1); |
172 |
printf("0\n0\n12\n"); |
173 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
174 |
vert[n1][0], |
175 |
vert[n1][1], |
176 |
0.0); |
177 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
178 |
vert[n1][0]+lvect[0], |
179 |
vert[n1][1]+lvect[1], |
180 |
lvect[2]); |
181 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
182 |
vert[n2][0]+lvect[0], |
183 |
vert[n2][1]+lvect[1], |
184 |
lvect[2]); |
185 |
printf("\t%18.12g\t%18.12g\t%18.12g\n", |
186 |
vert[n2][0], |
187 |
vert[n2][1], |
188 |
0.0); |
189 |
} |
190 |
|
191 |
|
192 |
printhead(ac, av) /* print command header */ |
193 |
register int ac; |
194 |
register char **av; |
195 |
{ |
196 |
putchar('#'); |
197 |
while (ac--) { |
198 |
putchar(' '); |
199 |
fputs(*av++, stdout); |
200 |
} |
201 |
putchar('\n'); |
202 |
} |