ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/makedist.c
Revision: 1.2
Committed: Thu Mar 14 17:38:24 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +1 -1 lines
Log Message:
set setscan.h into its own file

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