1 |
greg |
1.1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* scanner.c - program to simulate bi-directional scanner. |
9 |
|
|
* |
10 |
|
|
* 6/10/86 |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include "stdio.h" |
14 |
|
|
|
15 |
|
|
#include "ctype.h" |
16 |
|
|
|
17 |
|
|
#include "signal.h" |
18 |
|
|
|
19 |
|
|
#include "random.h" |
20 |
|
|
|
21 |
|
|
|
22 |
|
|
#define PI 3.14159265358979324 |
23 |
|
|
|
24 |
|
|
#define ANGLE short |
25 |
|
|
#define AEND -1 |
26 |
|
|
|
27 |
|
|
#define atorad(a) ((PI/180.0) * (a)) |
28 |
|
|
|
29 |
|
|
char otcom[128] = "oconv"; |
30 |
|
|
char rtcom[256] = "rtrace -bf -ar .25 -ad 128"; |
31 |
|
|
|
32 |
|
|
char *sourcetemp, /* temp files */ |
33 |
|
|
*octreetemp; |
34 |
|
|
|
35 |
|
|
ANGLE alpha[181] = {90, AEND}; |
36 |
|
|
ANGLE beta[181] = {30, 60, 90, 120, 150, AEND}; |
37 |
|
|
ANGLE gamma[181] = {45, AEND}; |
38 |
|
|
|
39 |
|
|
char *target; /* target file name */ |
40 |
|
|
double targetw = 3.0; /* target width (inches) */ |
41 |
|
|
double targeth = 3.0; /* target height (inches) */ |
42 |
|
|
int xres = 16; /* x sample resolution */ |
43 |
|
|
int yres = 16; /* y sample resolution */ |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
main(argc, argv) |
47 |
|
|
int argc; |
48 |
|
|
char *argv[]; |
49 |
|
|
{ |
50 |
|
|
double atof(); |
51 |
|
|
char *strcat(), *mktemp(); |
52 |
|
|
int quit(); |
53 |
|
|
int i; |
54 |
|
|
|
55 |
|
|
signal(SIGHUP, quit); |
56 |
|
|
signal(SIGINT, quit); |
57 |
|
|
signal(SIGTERM, quit); |
58 |
|
|
signal(SIGXCPU, SIG_IGN); |
59 |
|
|
signal(SIGXFSZ, SIG_IGN); |
60 |
|
|
|
61 |
|
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
62 |
|
|
switch (argv[i][1]) { |
63 |
|
|
case 't': /* target */ |
64 |
|
|
switch (argv[i][2]) { |
65 |
|
|
case 'w': /* width */ |
66 |
|
|
targetw = atof(argv[++i]); |
67 |
|
|
break; |
68 |
|
|
case 'h': /* height */ |
69 |
|
|
targeth = atof(argv[++i]); |
70 |
|
|
break; |
71 |
|
|
default: |
72 |
|
|
goto badopt; |
73 |
|
|
} |
74 |
|
|
break; |
75 |
|
|
case 'a': |
76 |
|
|
if (!strcmp(argv[i], "-alpha")) |
77 |
|
|
setscan(alpha, argv[++i]); |
78 |
|
|
else |
79 |
|
|
switch (argv[i][2]) { |
80 |
|
|
case 'd': /* ambient divisions */ |
81 |
|
|
strcat(rtcom, " -ad "); |
82 |
|
|
strcat(rtcom, argv[++i]); |
83 |
|
|
break; |
84 |
|
|
case 'r': /* ambient radius */ |
85 |
|
|
strcat(rtcom, " -ar "); |
86 |
|
|
strcat(rtcom, argv[++i]); |
87 |
|
|
break; |
88 |
|
|
default: |
89 |
|
|
goto badopt; |
90 |
|
|
} |
91 |
|
|
break; |
92 |
|
|
case 'b': |
93 |
|
|
if (!strcmp(argv[i], "-beta")) |
94 |
|
|
setscan(beta, argv[++i]); |
95 |
|
|
else |
96 |
|
|
goto badopt; |
97 |
|
|
break; |
98 |
|
|
case 'g': |
99 |
|
|
if (!strcmp(argv[i], "-gamma")) |
100 |
|
|
setscan(gamma, argv[++i]); |
101 |
|
|
else |
102 |
|
|
goto badopt; |
103 |
|
|
break; |
104 |
|
|
case 'x': /* x resolution */ |
105 |
|
|
xres = atoi(argv[++i]); |
106 |
|
|
break; |
107 |
|
|
case 'y': /* y resolution */ |
108 |
|
|
yres = atoi(argv[++i]); |
109 |
|
|
break; |
110 |
|
|
case 'l': |
111 |
|
|
switch (argv[i][2]) { |
112 |
|
|
case 'w': /* limit weight */ |
113 |
|
|
strcat(rtcom, " -lw "); |
114 |
|
|
strcat(rtcom, argv[++i]); |
115 |
|
|
break; |
116 |
|
|
case 'r': /* limit recursion */ |
117 |
|
|
strcat(rtcom, " -lr "); |
118 |
|
|
strcat(rtcom, argv[++i]); |
119 |
|
|
break; |
120 |
|
|
default: |
121 |
|
|
goto badopt; |
122 |
|
|
} |
123 |
|
|
break; |
124 |
|
|
default:; |
125 |
|
|
badopt: |
126 |
|
|
fprintf(stderr, "%s: bad option: %s\n", |
127 |
|
|
argv[0], argv[i]); |
128 |
|
|
quit(1); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
if (i >= argc) { |
132 |
|
|
fprintf(stderr, "%s: missing target file\n", argv[0]); |
133 |
|
|
quit(1); |
134 |
|
|
} else if (i < argc-1) { |
135 |
|
|
fprintf(stderr, "%s: too many target files\n", argv[0]); |
136 |
|
|
quit(1); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
target = argv[i]; |
140 |
|
|
sourcetemp = mktemp("/usr/tmp/soXXXXXX"); |
141 |
|
|
octreetemp = mktemp("/usr/tmp/ocXXXXXX"); |
142 |
|
|
strcat(strcat(otcom, " "), target); |
143 |
|
|
strcat(strcat(otcom, " "), sourcetemp); |
144 |
|
|
strcat(strcat(otcom, " > "), octreetemp); |
145 |
|
|
strcat(strcat(rtcom, " "), octreetemp); |
146 |
|
|
|
147 |
|
|
doscan(); |
148 |
|
|
|
149 |
|
|
quit(0); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
|
153 |
|
|
quit(code) /* unlink temp files and exit */ |
154 |
|
|
int code; |
155 |
|
|
{ |
156 |
|
|
int i; |
157 |
|
|
|
158 |
|
|
unlink(sourcetemp); |
159 |
|
|
unlink(octreetemp); |
160 |
|
|
exit(code); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
|
164 |
|
|
setscan(ang, arg) /* set up scan according to arg */ |
165 |
|
|
register ANGLE *ang; |
166 |
|
|
register char *arg; |
167 |
|
|
{ |
168 |
|
|
int start = 0, finish = -1, step = 1; |
169 |
|
|
|
170 |
|
|
for ( ; ; ) { |
171 |
|
|
switch (*arg) { |
172 |
|
|
case '\0': |
173 |
|
|
case ',': |
174 |
|
|
while (start <= finish) { |
175 |
|
|
*ang++ = start; |
176 |
|
|
start += step; |
177 |
|
|
} |
178 |
|
|
if (*arg) { |
179 |
|
|
arg++; |
180 |
|
|
continue; |
181 |
|
|
} else { |
182 |
|
|
*ang = AEND; |
183 |
|
|
return; |
184 |
|
|
} |
185 |
|
|
case '0': |
186 |
|
|
case '1': |
187 |
|
|
case '2': |
188 |
|
|
case '3': |
189 |
|
|
case '4': |
190 |
|
|
case '5': |
191 |
|
|
case '6': |
192 |
|
|
case '7': |
193 |
|
|
case '8': |
194 |
|
|
case '9': |
195 |
|
|
start = atoi(arg); |
196 |
|
|
finish = start; |
197 |
|
|
step = 1; |
198 |
|
|
break; |
199 |
|
|
case '-': |
200 |
|
|
finish = atoi(++arg); |
201 |
|
|
break; |
202 |
|
|
case ':': |
203 |
|
|
step = atoi(++arg); |
204 |
|
|
break; |
205 |
|
|
default: |
206 |
|
|
fprintf(stderr, "Scan specification error\n"); |
207 |
|
|
quit(1); |
208 |
|
|
} |
209 |
|
|
while (isdigit(*arg)) |
210 |
|
|
arg++; |
211 |
|
|
} |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
|
215 |
|
|
doscan() /* do scan for target */ |
216 |
|
|
{ |
217 |
|
|
FILE *fopen(), *scanstart(); |
218 |
|
|
double readsample(); |
219 |
|
|
FILE *fp; |
220 |
|
|
ANGLE *a, *b, *g; |
221 |
|
|
|
222 |
|
|
printf("Alpha\tBeta\tGamma\tDistribution for \"%s\"\n", target); |
223 |
|
|
for (g = gamma; *g != AEND; g++) { |
224 |
|
|
fp = scanstart(*g); |
225 |
|
|
for (b = beta; *b != AEND; b++) /* read data */ |
226 |
|
|
for (a = alpha; *a != AEND; a++) |
227 |
|
|
printf("%d\t%d\t%d\t%f\n", |
228 |
|
|
*a, *b, *g, readsample(fp)); |
229 |
|
|
scanend(fp); |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
FILE * |
235 |
|
|
scanstart(g) /* open scanner pipeline */ |
236 |
|
|
ANGLE g; |
237 |
|
|
{ |
238 |
|
|
char *fgets(); |
239 |
|
|
int p1[2], p2[2]; |
240 |
|
|
int pid; |
241 |
|
|
FILE *fp; |
242 |
|
|
char buf[128]; |
243 |
|
|
|
244 |
|
|
makesource(g); /* make source */ |
245 |
|
|
if (system(otcom)) /* generate new octree */ |
246 |
|
|
goto err; |
247 |
|
|
|
248 |
|
|
if (pipe(p1) < 0) /* get pipe1 */ |
249 |
|
|
goto err; |
250 |
|
|
if ((pid = fork()) == 0) { /* if child1 */ |
251 |
|
|
close(p1[0]); |
252 |
|
|
if (p1[1] != 1) { /* connect stdout to pipe1 */ |
253 |
|
|
dup2(p1[1], 1); |
254 |
|
|
close(p1[1]); |
255 |
|
|
} |
256 |
|
|
if (pipe(p2) < 0) /* get pipe2 */ |
257 |
|
|
goto err; |
258 |
|
|
if ((pid = fork()) == 0) { /* if child2 */ |
259 |
|
|
close(p2[0]); |
260 |
|
|
if ((fp = fdopen(p2[1], "w")) == NULL) |
261 |
|
|
goto err; |
262 |
|
|
sendsamples(fp); /* send our samples to pipe2 */ |
263 |
|
|
fclose(fp); |
264 |
|
|
_exit(0); |
265 |
|
|
} |
266 |
|
|
if (pid == -1) |
267 |
|
|
goto err; |
268 |
|
|
close(p2[1]); |
269 |
|
|
|
270 |
|
|
if (p2[0] != 0) { /* for child1 pipe2 to stdin */ |
271 |
|
|
dup2(p2[0], 0); |
272 |
|
|
close(p2[0]); |
273 |
|
|
} |
274 |
|
|
execl("/bin/sh", "sh", "-c", rtcom, 0); /* run ray tracer */ |
275 |
|
|
goto err; |
276 |
|
|
} |
277 |
|
|
if (pid == -1) |
278 |
|
|
goto err; |
279 |
|
|
close(p1[1]); |
280 |
|
|
if ((fp = fdopen(p1[0], "r")) == NULL) /* parent reads pipe1 */ |
281 |
|
|
goto err; |
282 |
|
|
while (fgets(buf, sizeof(buf), fp) != NULL && buf[0] != '\n') |
283 |
|
|
/* discard header */; |
284 |
|
|
return(fp); |
285 |
|
|
err: |
286 |
|
|
fprintf(stderr, "System error in scanstart\n"); |
287 |
|
|
quit(1); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
|
291 |
|
|
scanend(fp) /* done with scanner input */ |
292 |
|
|
FILE *fp; |
293 |
|
|
{ |
294 |
|
|
fclose(fp); |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
|
298 |
|
|
makesource(g) /* make a source (output normalized) */ |
299 |
|
|
ANGLE g; |
300 |
|
|
{ |
301 |
|
|
FILE *fp, *fopen(); |
302 |
|
|
double srcdir[3], sin(), cos(); |
303 |
|
|
|
304 |
|
|
srcdir[0] = sin(atorad(g)); |
305 |
|
|
srcdir[1] = 0.0; |
306 |
|
|
srcdir[2] = -cos(atorad(g)); |
307 |
|
|
if ((fp = fopen(sourcetemp, "w")) == NULL) { |
308 |
|
|
fprintf(stderr, "%s: cannot create\n", sourcetemp); |
309 |
|
|
quit(1); |
310 |
|
|
} |
311 |
|
|
fprintf(fp, "0 0 0 1\n"); |
312 |
|
|
fprintf(fp, "\nvoid light scanner_light\n"); |
313 |
|
|
fprintf(fp, "0\n0\n3 3283 3283 3283\n"); |
314 |
|
|
fprintf(fp, "\nscanner_light source scanner_source\n"); |
315 |
|
|
fprintf(fp, "0\n0\n4 %f %f %f 2\n", srcdir[0], srcdir[1], srcdir[2]); |
316 |
|
|
fclose(fp); |
317 |
|
|
} |
318 |
|
|
|
319 |
|
|
|
320 |
|
|
sendsamples(fp) /* send our samples to fp */ |
321 |
|
|
FILE *fp; |
322 |
|
|
{ |
323 |
|
|
ANGLE *a, *b; |
324 |
|
|
|
325 |
|
|
for (b = beta; *b != AEND; b++) |
326 |
|
|
for (a = alpha; *a != AEND; a++) |
327 |
|
|
writesample(fp, *a, *b); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
|
331 |
|
|
writesample(fp, a, b) /* write out sample ray grid */ |
332 |
|
|
FILE *fp; |
333 |
|
|
ANGLE a, b; |
334 |
|
|
{ |
335 |
|
|
double sin(), cos(); |
336 |
|
|
float sp[6]; |
337 |
|
|
int i, j; |
338 |
|
|
|
339 |
|
|
sp[3] = -sin(atorad(b)-(PI/2.0)) * sin(atorad(a)); |
340 |
|
|
sp[4] = -cos(atorad(a)); |
341 |
|
|
sp[5] = -sin(atorad(b)) * sin(atorad(a)); |
342 |
|
|
|
343 |
|
|
for (j = 0; j < yres; j++) |
344 |
|
|
for (i = 0; i < xres; i++) { |
345 |
|
|
sp[0] = -100.0*sp[3] + |
346 |
|
|
(i - xres/2.0 + frandom())*targetw/xres; |
347 |
|
|
sp[1] = -100.0*sp[4] + |
348 |
|
|
(j - yres/2.0 + frandom())*targeth/yres; |
349 |
|
|
sp[2] = -100.0*sp[5]; |
350 |
|
|
fwrite(sp, sizeof(*sp), 6, fp); |
351 |
|
|
if (ferror(fp)) { |
352 |
|
|
fprintf(stderr, "Data write error\n"); |
353 |
|
|
quit(1); |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
|
359 |
|
|
double |
360 |
|
|
readsample(fp) /* read in sample ray grid */ |
361 |
|
|
FILE *fp; |
362 |
|
|
{ |
363 |
|
|
double sum; |
364 |
|
|
float col[3]; |
365 |
|
|
int i; |
366 |
|
|
|
367 |
|
|
sum = 0.0; |
368 |
|
|
i = xres*yres; |
369 |
|
|
while (i--) { |
370 |
|
|
if (fread(col, sizeof(*col), 3, fp) != 3) { |
371 |
|
|
fprintf(stderr, "Data read error\n"); |
372 |
|
|
quit(1); |
373 |
|
|
} |
374 |
|
|
sum += col[1]; |
375 |
|
|
} |
376 |
|
|
return(sum / (xres*yres)); |
377 |
|
|
} |