ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/scanner.c
Revision: 1.5
Committed: Fri Mar 26 23:34:23 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1, rad3R6
Changes since 1.4: +61 -41 lines
Log Message:
Continued ANSIfication.

File Contents

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