ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/makedist.c
Revision: 2.3
Committed: Mon Aug 2 14:35:13 1993 UTC (30 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +3 -0 lines
Log Message:
Added conditional declaration of atof()

File Contents

# User Rev Content
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     * makedist.c - program to make a source distribution.
9     *
10     * 8/18/86
11     *
12     * Program uses angular coordinates as follows:
13     *
14     * alpha - degrees measured from x1 axis.
15     * beta - degress of projection into x2x3 plane,
16     * measured from x2 axis towards x3 axis.
17     *
18     * Default axes are (x1,x2,x3) = (x,y,z).
19     */
20    
21     #include <stdio.h>
22    
23     #include "random.h"
24    
25 greg 1.2 #include "setscan.h"
26 greg 1.1
27 greg 1.3 #ifndef BSD
28     #define vfork fork
29     #endif
30 greg 1.1
31     #define FTINY 1e-7
32    
33     #define PI 3.14159265358979324
34    
35     #define atorad(a) ((PI/180.0) * (a))
36    
37     char *rtargv[128] = {"rtrace", "-h"};
38     int rtargc = 2;
39    
40     #define passarg(s) (rtargv[rtargc++] = s)
41    
42 greg 2.3 #ifdef DCL_ATOF
43     extern double atof();
44     #endif
45 greg 1.1 /* default angles */
46     ANGLE alpha[181] = {10, 25, 40, 55, 70, 85, AEND};
47     ANGLE beta[361] = {0,30,60,90,120,150,180,210,240,270,300,330,AEND};
48    
49     /* coordinate system */
50     double axis[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
51    
52     double targetw = 1.0; /* target width */
53     double targeth = 1.0; /* target height */
54     double targetd = 1.0; /* target distance */
55     double targetp[3] = {0,0,0}; /* target center */
56     int xres = 16; /* x sample resolution */
57     int yres = 16; /* y sample resolution */
58    
59     int userformat = 1; /* output 1=nice,0=plain,-1=raw */
60     int header = 1; /* print header? */
61    
62     char *progname; /* program name */
63    
64    
65     main(argc, argv)
66     int argc;
67     char *argv[];
68     {
69     int i;
70    
71     progname = argv[0];
72    
73     for (i = 1; i < argc && argv[i][0] == '-'; i++)
74     switch (argv[i][1]) {
75     case 't': /* target */
76     switch (argv[i][2]) {
77     case 'w': /* width */
78     targetw = atof(argv[++i]);
79     break;
80     case 'h': /* height */
81     targeth = atof(argv[++i]);
82     break;
83     case 'd': /* distance */
84     targetd = atof(argv[++i]);
85     break;
86     case 'c': /* center */
87     targetp[0] = atof(argv[++i]);
88     targetp[1] = atof(argv[++i]);
89     targetp[2] = atof(argv[++i]);
90     break;
91     default:
92     goto badopt;
93     }
94     break;
95     case 'a':
96     if (!strcmp(argv[i], "-alpha")) {
97     if (setscan(alpha, argv[++i]) < 0) {
98     fprintf(stderr,
99     "%s: bad alpha spec: %s\n",
100     progname, argv[i]);
101     exit(1);
102     }
103     } else
104     switch (argv[i][2]) {
105     case 'd': /* ambient divisions */
106     case 's': /* ambient samples */
107     case 'r': /* ambient resolution */
108     case 'a': /* ambient accuracy */
109     case 'b': /* ambient bounces */
110     case 'i': /* include */
111     case 'e': /* exclude */
112     case 'f': /* file */
113     passarg(argv[i]);
114     passarg(argv[++i]);
115     break;
116     case 'v': /* ambient value */
117     passarg(argv[i]);
118     passarg(argv[++i]);
119     passarg(argv[++i]);
120     passarg(argv[++i]);
121     break;
122     default:
123     goto badopt;
124     }
125     break;
126     case 'b':
127     if (!strcmp(argv[i], "-beta")) {
128     if (setscan(beta, argv[++i]) < 0) {
129     fprintf(stderr,
130     "%s: bad beta spec: %s\n",
131     progname, argv[i]);
132     exit(1);
133     }
134     } else
135     goto badopt;
136     break;
137     case 'x':
138     switch (argv[i][2]) {
139     case '\0': /* x resolution */
140     xres = atoi(argv[++i]);
141     break;
142     case '1':
143     case '2': /* axis spec */
144     case '3':
145     axis[argv[i][2]-'1'][0] = atof(argv[i+1]);
146     axis[argv[i][2]-'1'][1] = atof(argv[i+2]);
147     axis[argv[i][2]-'1'][2] = atof(argv[i+3]);
148     i += 3;
149     break;
150     default:
151     goto badopt;
152     }
153     break;
154     case 'y': /* y resolution */
155     yres = atoi(argv[++i]);
156     break;
157     case 'l':
158     switch (argv[i][2]) {
159     case 'w': /* limit weight */
160     case 'r': /* limit recursion */
161     passarg(argv[i]);
162     passarg(argv[++i]);
163     break;
164     default:
165     goto badopt;
166     }
167     break;
168     case 'u': /* user friendly format */
169     userformat = 1;
170     break;
171     case 'o': /* custom format */
172     passarg(argv[i]);
173     userformat = -1;
174     break;
175     case 'd':
176     switch (argv[i][2]) {
177     case '\0': /* data format */
178     userformat = 0;
179     break;
180     case 'j': /* direct jitter */
181     case 't': /* direct threshold */
182     case 'c': /* direct certainty */
183     passarg(argv[i]);
184     passarg(argv[++i]);
185     break;
186     default:
187     goto badopt;
188     }
189     break;
190     case 'h': /* no header */
191     header = 0;
192     break;
193     default:;
194     badopt:
195     fprintf(stderr, "%s: bad option: %s\n",
196     progname, argv[i]);
197     exit(1);
198     }
199    
200     if (userformat >= 0) { /* we cook */
201     passarg("-ov");
202     passarg("-fff");
203     } else /* raw */
204     passarg("-ffa");
205    
206     if (i != argc-1) {
207     fprintf(stderr, "%s: single octree required\n", progname);
208     exit(1);
209     }
210    
211     passarg(argv[i]);
212     passarg(NULL);
213    
214     fixaxes();
215    
216     if (header)
217     if (userformat > 0)
218     printf("Alpha\tBeta\tRadiance\n");
219     else {
220     printargs(argc, argv, stdout);
221     putchar('\n');
222     }
223    
224     if (doscan() == -1)
225     exit(1);
226    
227     exit(0);
228     }
229    
230    
231     printargs(ac, av, fp) /* print arguments to a file */
232     int ac;
233     char **av;
234     FILE *fp;
235     {
236     while (ac-- > 0) {
237     fputs(*av++, fp);
238     putc(' ', fp);
239     }
240     putc('\n', fp);
241     }
242    
243    
244     fixaxes() /* make sure axes are OK */
245     {
246     extern double normalize(), fdot();
247     double d;
248     register int i;
249    
250     for (i = 0; i < 3; i++)
251     if (normalize(axis[i]) == 0.0)
252     goto axerr;
253    
254     for (i = 0; i < 3; i++) {
255     d = fdot(axis[i], axis[(i+1)%3]);
256     if (d > FTINY || d < -FTINY)
257     goto axerr;
258     }
259     return;
260     axerr:
261     fprintf(stderr, "%s: bad axis specification\n", progname);
262     exit(1);
263     }
264    
265    
266     doscan() /* do scan for target */
267     {
268     FILE *fopen(), *scanstart();
269     double readsample();
270     FILE *fp;
271     ANGLE *a, *b;
272    
273     fp = scanstart(); /* returns in child if not raw */
274     for (a = alpha; *a != AEND; a++) /* read data */
275     for (b = beta; *b != AEND; b++)
276     if (userformat > 0)
277     printf("%d\t%d\t%e\n", *a, *b, readsample(fp));
278     else
279     printf("%e\n", readsample(fp));
280     return(scanend(fp));
281     }
282    
283    
284     FILE *
285     scanstart() /* open scanner pipeline */
286     {
287     int status;
288     char *fgets();
289     int p1[2], p2[2];
290     int pid;
291     FILE *fp;
292    
293     fflush(stdout); /* empty output buffer */
294     if (pipe(p1) < 0) /* get pipe1 */
295     goto err;
296     if ((pid = fork()) == 0) { /* if child1 */
297     close(p1[1]);
298     if (p1[0] != 0) { /* connect pipe1 to stdin */
299     dup2(p1[0], 0);
300     close(p1[0]);
301     }
302     if (userformat >= 0) { /* if processing output */
303     if (pipe(p2) < 0) /* get pipe2 */
304     goto err;
305     if ((pid = vfork()) == 0) { /* if child2 */
306     close(p2[0]);
307     if (p2[1] != 1) { /* pipe2 to stdout */
308     dup2(p2[1], 1);
309     close(p2[1]);
310     }
311     execvp(rtargv[0], rtargv); /* rtrace */
312     goto err;
313     }
314     if (pid == -1)
315     goto err;
316     close(p2[1]);
317     if ((fp = fdopen(p2[0], "r")) == NULL)
318     goto err;
319     return(fp);
320     } else { /* if running raw */
321     execvp(rtargv[0], rtargv); /* rtrace */
322     goto err;
323     }
324     }
325     if (pid == -1)
326     goto err;
327     close(p1[0]);
328     if ((fp = fdopen(p1[1], "w")) == NULL)
329     goto err;
330     sendsamples(fp); /* parent writes output to pipe1 */
331     fclose(fp);
332     if (wait(&status) != -1) /* wait for child1 */
333     _exit(status);
334     err:
335     perror(progname);
336     _exit(1);
337     }
338    
339    
340     scanend(fp) /* done with scanner input */
341     FILE *fp;
342     {
343     int status;
344    
345     fclose(fp);
346     if (wait(&status) == -1) { /* wait for child2 */
347     perror(progname);
348     exit(1);
349     }
350     return(status ? -1 : 0);
351     }
352    
353    
354     sendsamples(fp) /* send our samples to fp */
355     FILE *fp;
356     {
357     ANGLE *a, *b;
358    
359     for (a = alpha; *a != AEND; a++)
360     for (b = beta; *b != AEND; b++)
361     writesample(fp, *a, *b);
362     }
363    
364    
365     writesample(fp, a, b) /* write out sample ray grid */
366     FILE *fp;
367     ANGLE a, b;
368     {
369     double sin(), cos();
370     float sp[6];
371     double dv[3];
372     double xpos, ypos;
373     int i, j, k;
374     /* get direction in their system */
375     dv[0] = -cos(atorad(a));
376     dv[1] = dv[2] = -sin(atorad(a));
377     dv[1] *= cos(atorad(b));
378     dv[2] *= sin(atorad(b));
379     /* do sample grid in our system */
380     for (j = 0; j < yres; j++) {
381     for (i = 0; i < xres; i++) {
382     xpos = ((i + frandom())/xres - 0.5) * targetw;
383     ypos = ((j + frandom())/yres - 0.5) * targeth;
384     for (k = 0; k < 3; k++) {
385     sp[k] = targetp[k];
386     sp[k] += xpos*axis[2][k];
387     sp[k] += ypos*axis[1][k];
388     sp[k+3] = dv[0]*axis[0][k] +
389     dv[1]*axis[1][k] +
390     dv[2]*axis[2][k];
391     sp[k] -= sp[k+3]*targetd;
392     }
393     if (fwrite(sp, sizeof(*sp), 6, fp) != 6) {
394     fprintf(stderr, "%s: data write error\n",
395     progname);
396     _exit(1);
397     }
398     }
399     }
400     }
401    
402    
403     double
404     readsample(fp) /* read in sample ray grid */
405     FILE *fp;
406     {
407     float col[3];
408     double sum;
409     int i;
410    
411     sum = 0.0;
412     i = xres*yres;
413     while (i--) {
414     if (fread(col, sizeof(*col), 3, fp) != 3) {
415     fprintf(stderr, "%s: data read error\n", progname);
416     exit(1);
417     }
418     sum += .3*col[0] + .59*col[1] + .11*col[2];
419     }
420     return(sum / (xres*yres));
421     }