ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/makedist.c
Revision: 2.2
Committed: Thu Dec 19 14:56:34 1991 UTC (32 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +0 -1 lines
Log Message:
eliminated atof declaration for NeXT

File Contents

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