ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/makedist.c
Revision: 2.1
Committed: Tue Nov 12 17:19:04 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +0 -0 lines
Log Message:
changed revision number for 2.0 release

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