ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.25
Committed: Thu May 26 14:46:21 2005 UTC (18 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1
Changes since 2.24: +9 -2 lines
Log Message:
Additional signal handling

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: mkillum.c,v 2.24 2005/05/25 04:44:25 greg Exp $";
3 #endif
4 /*
5 * Make illum sources for optimizing rendering process
6 */
7
8 #include <signal.h>
9 #include <ctype.h>
10
11 #include "platform.h"
12 #include "mkillum.h"
13 #include "random.h"
14
15 /* default parameters */
16 #define SAMPDENS 48 /* points per projected steradian */
17 #define NSAMPS 32 /* samples per point */
18 #define DFLMAT "illum_mat" /* material name */
19 #define DFLDAT "illum" /* data file name */
20 /* selection options */
21 #define S_NONE 0 /* select none */
22 #define S_ELEM 1 /* select specified element */
23 #define S_COMPL 2 /* select all but element */
24 #define S_ALL 3 /* select all */
25
26 /* rtrace command and defaults */
27 char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-",
28 "-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", };
29 int rtargc = 14;
30 /* overriding rtrace options */
31 char *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-",
32 "-fff", "-y", "0", NULL };
33
34 struct rtproc rt0; /* head of rtrace process list */
35
36 struct illum_args thisillum = { /* our illum and default values */
37 0,
38 DFLMAT,
39 DFLDAT,
40 0,
41 VOIDID,
42 SAMPDENS,
43 NSAMPS,
44 0.,
45 };
46
47 char matcheck[MAXSTR]; /* current material to include or exclude */
48 int matselect = S_ALL; /* selection criterion */
49
50 FUN ofun[NUMOTYPE] = INIT_OTYPE; /* object types */
51
52 char persistfn[] = "pfXXXXXX"; /* persist file name */
53
54 int gargc; /* global argc */
55 char **gargv; /* global argv */
56 #define progname gargv[0]
57
58 int doneheader = 0; /* printed header yet? */
59 #define checkhead() if (!doneheader++) printhead(gargc,gargv)
60
61 int warnings = 1; /* print warnings? */
62
63 int done_rprocs(struct rtproc *rtp);
64 void init(int np);
65 void filter(register FILE *infp, char *name);
66 void xoptions(char *s, char *nm);
67 void printopts(void);
68 void printhead(register int ac, register char **av);
69 void xobject(FILE *fp, char *nm);
70
71
72 int
73 main( /* compute illum distributions using rtrace */
74 int argc,
75 char *argv[]
76 )
77 {
78 int nprocs = 1;
79 char *rtpath;
80 FILE *fp;
81 register int i;
82 /* set global arguments */
83 gargv = argv;
84 /* check for -n option */
85 if (!strcmp(argv[1], "-n")) {
86 nprocs = atoi(argv[2]);
87 if (nprocs <= 0)
88 error(USER, "illegal number of processes");
89 i = 3;
90 } else
91 i = 1;
92 /* set up rtrace command */
93 for ( ; i < argc; i++) {
94 if (argv[i][0] == '<' && argv[i][1] == '\0')
95 break;
96 rtargv[rtargc++] = argv[i];
97 if (argv[i][0] == '-' && argv[i][1] == 'w')
98 switch (argv[i][2]) {
99 case '\0':
100 warnings = !warnings;
101 break;
102 case '+':
103 case 'T': case 't':
104 case 'Y': case 'y':
105 case '1':
106 warnings = 1;
107 break;
108 case '-':
109 case 'F': case 'f':
110 case 'N': case 'n':
111 case '0':
112 warnings = 0;
113 break;
114 }
115 }
116 gargc = i;
117 if (!strcmp(rtargv[--rtargc], "-defaults"))
118 nprocs = 0;
119 if (nprocs > 1) { /* add persist file if parallel invocation */
120 rtargv[rtargc++] = "-PP";
121 rtargv[rtargc++] = mktemp(persistfn);
122 }
123 /* add "mandatory" rtrace options */
124 for (i = 0; myrtopts[i] != NULL; i++)
125 rtargv[rtargc++] = myrtopts[i];
126 /* finally, put back final argument */
127 rtargv[rtargc++] = argv[gargc-1];
128 rtargv[rtargc] = NULL;
129 if (!nprocs) { /* just asking for defaults? */
130 printopts(); fflush(stdout);
131 rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
132 if (rtpath == NULL) {
133 eputs(rtargv[0]);
134 eputs(": command not found\n");
135 exit(1);
136 }
137 execv(rtpath, rtargv);
138 perror(rtpath); /* execv() should not return */
139 exit(1);
140 }
141 if (gargc < 2 || argv[gargc-1][0] == '-')
142 error(USER, "missing octree argument");
143 /* else initialize and run our calculation */
144 init(nprocs);
145 if (gargc+1 < argc)
146 for (i = gargc+1; i < argc; i++) {
147 if ((fp = fopen(argv[i], "r")) == NULL) {
148 sprintf(errmsg,
149 "cannot open scene file \"%s\"", argv[i]);
150 error(SYSTEM, errmsg);
151 }
152 filter(fp, argv[i]);
153 fclose(fp);
154 }
155 else
156 filter(stdin, "standard input");
157 quit(0);
158 }
159
160 static void
161 killpersist(void) /* kill persistent rtrace process */
162 {
163 FILE *fp = fopen(persistfn, "r");
164 int pid;
165
166 if (fp == NULL)
167 return;
168 if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
169 unlink(persistfn);
170 fclose(fp);
171 }
172
173 int
174 done_rprocs(struct rtproc *rtp)
175 {
176 int st0, st1 = 0;
177
178 if (rtp->next != NULL) { /* close last opened first! */
179 st1 = done_rprocs(rtp->next);
180 free((void *)rtp->next);
181 rtp->next = NULL;
182 }
183 st0 = close_process(&rtp->pd);
184 if (st0 < 0)
185 error(WARNING, "unknown return status from rtrace process");
186 else if (st0 > 0)
187 return(st0);
188 return(st1);
189 }
190
191 void
192 quit(int status) /* exit with status */
193 {
194 int rtstat;
195
196 if (rt0.next != NULL) /* terminate persistent rtrace */
197 killpersist();
198 /* clean up rtrace process(es) */
199 rtstat = done_rprocs(&rt0);
200 if (status == 0)
201 status = rtstat;
202 exit(status);
203 }
204
205 void
206 init(int np) /* start rtrace and set up buffers */
207 {
208 struct rtproc *rtp;
209 int i;
210 int maxbytes;
211 /* set up object functions */
212 ofun[OBJ_FACE].funp = o_face;
213 ofun[OBJ_SPHERE].funp = o_sphere;
214 ofun[OBJ_RING].funp = o_ring;
215 /* set up signal handling */
216 signal(SIGINT, quit);
217 #ifdef SIGHUP
218 signal(SIGHUP, quit);
219 #endif
220 #ifdef SIGTERM
221 signal(SIGTERM, quit);
222 #endif
223 #ifdef SIGPIPE
224 signal(SIGPIPE, quit);
225 #endif
226 rtp = &rt0; /* start rtrace process(es) */
227 for (i = 0; i++ < np; ) {
228 errno = 0;
229 maxbytes = open_process(&rtp->pd, rtargv);
230 if (maxbytes == 0) {
231 eputs(rtargv[0]);
232 eputs(": command not found\n");
233 exit(1);
234 }
235 if (maxbytes < 0)
236 error(SYSTEM, "cannot start rtrace process");
237 rtp->bsiz = maxbytes/(6*sizeof(float));
238 rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
239 rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
240 if (rtp->buf == NULL || rtp->dest == NULL)
241 error(SYSTEM, "out of memory in init");
242 rtp->nrays = 0;
243 if (i == np) /* last process? */
244 break;
245 if (np > 1)
246 sleep(2); /* wait for persist file */
247 rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
248 if (rtp->next == NULL)
249 error(SYSTEM, "out of memory in init");
250 rtp = rtp->next;
251 }
252 rtp->next = NULL;
253 /* set up urand */
254 initurand(16384);
255 }
256
257
258 void
259 eputs( /* put string to stderr */
260 register char *s
261 )
262 {
263 static int midline = 0;
264
265 if (!*s) return;
266 if (!midline) {
267 fputs(progname, stderr);
268 fputs(": ", stderr);
269 }
270 fputs(s, stderr);
271 midline = s[strlen(s)-1] != '\n';
272 }
273
274
275 void
276 wputs(s) /* print warning if enabled */
277 char *s;
278 {
279 if (warnings)
280 eputs(s);
281 }
282
283
284 void
285 filter( /* process stream */
286 register FILE *infp,
287 char *name
288 )
289 {
290 char buf[512];
291 FILE *pfp;
292 register int c;
293
294 while ((c = getc(infp)) != EOF) {
295 if (isspace(c))
296 continue;
297 if (c == '#') { /* comment/options */
298 buf[0] = c;
299 fgets(buf+1, sizeof(buf)-1, infp);
300 xoptions(buf, name);
301 } else if (c == '!') { /* command */
302 buf[0] = c;
303 fgetline(buf+1, sizeof(buf)-1, infp);
304 if ((pfp = popen(buf+1, "r")) == NULL) {
305 sprintf(errmsg, "cannot execute \"%s\"", buf);
306 error(SYSTEM, errmsg);
307 }
308 filter(pfp, buf);
309 pclose(pfp);
310 } else { /* object */
311 ungetc(c, infp);
312 xobject(infp, name);
313 }
314 }
315 }
316
317
318 void
319 xoptions( /* process options in string s */
320 char *s,
321 char *nm
322 )
323 {
324 extern FILE *freopen();
325 char buf[64];
326 int nerrs = 0;
327 register char *cp;
328
329 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
330 fputs(s, stdout); /* not for us */
331 return;
332 }
333 cp = s+10;
334 while (*cp) {
335 switch (*cp) {
336 case ' ':
337 case '\t':
338 case '\n':
339 case '\r':
340 case '\f':
341 cp++;
342 continue;
343 case 'm': /* material name */
344 if (*++cp != '=')
345 break;
346 if (!*++cp || isspace(*cp))
347 break;
348 atos(thisillum.matname, MAXSTR, cp);
349 cp = sskip(cp);
350 if (!(thisillum.flags & IL_DATCLB)) {
351 strcpy(thisillum.datafile, thisillum.matname);
352 thisillum.dfnum = 0;
353 }
354 continue;
355 case 'f': /* data file name */
356 if (*++cp != '=')
357 break;
358 if (!*++cp || isspace(*cp)) {
359 strcpy(thisillum.datafile,thisillum.matname);
360 thisillum.dfnum = 0;
361 thisillum.flags &= ~IL_DATCLB;
362 continue;
363 }
364 atos(thisillum.datafile, MAXSTR, cp);
365 cp = sskip(cp);
366 thisillum.dfnum = 0;
367 thisillum.flags |= IL_DATCLB;
368 continue;
369 case 'i': /* include material */
370 case 'e': /* exclude material */
371 if (cp[1] != '=')
372 break;
373 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
374 cp += 2;
375 atos(matcheck, MAXSTR, cp);
376 cp = sskip(cp);
377 continue;
378 case 'a': /* use everything */
379 cp = sskip(cp);
380 matselect = S_ALL;
381 continue;
382 case 'n': /* use nothing (passive) */
383 cp = sskip(cp);
384 matselect = S_NONE;
385 continue;
386 case 'c': /* color calculation */
387 if (*++cp != '=')
388 break;
389 switch (*++cp) {
390 case 'a': /* average */
391 thisillum.flags = (thisillum.flags|IL_COLAVG)
392 & ~IL_COLDST;
393 break;
394 case 'd': /* distribution */
395 thisillum.flags |= (IL_COLDST|IL_COLAVG);
396 break;
397 case 'n': /* none */
398 thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
399 break;
400 default:
401 goto opterr;
402 }
403 cp = sskip(cp);
404 continue;
405 case 'd': /* point sample density */
406 if (*++cp != '=')
407 break;
408 if (!isintd(++cp, " \t\n\r"))
409 break;
410 thisillum.sampdens = atoi(cp);
411 cp = sskip(cp);
412 continue;
413 case 's': /* point super-samples */
414 if (*++cp != '=')
415 break;
416 if (!isintd(++cp, " \t\n\r"))
417 break;
418 thisillum.nsamps = atoi(cp);
419 cp = sskip(cp);
420 continue;
421 case 'l': /* light sources */
422 cp++;
423 if (*cp == '+')
424 thisillum.flags |= IL_LIGHT;
425 else if (*cp == '-')
426 thisillum.flags &= ~IL_LIGHT;
427 else
428 break;
429 cp++;
430 continue;
431 case 'b': /* brightness */
432 if (*++cp != '=')
433 break;
434 if (!isfltd(++cp, " \t\n\r"))
435 break;
436 thisillum.minbrt = atof(cp);
437 if (thisillum.minbrt < 0.)
438 thisillum.minbrt = 0.;
439 cp = sskip(cp);
440 continue;
441 case 'o': /* output file */
442 if (*++cp != '=')
443 break;
444 if (!*++cp || isspace(*cp))
445 break;
446 atos(buf, sizeof(buf), cp);
447 cp = sskip(cp);
448 if (freopen(buf, "w", stdout) == NULL) {
449 sprintf(errmsg,
450 "cannot open output file \"%s\"", buf);
451 error(SYSTEM, errmsg);
452 }
453 doneheader = 0;
454 continue;
455 case '!': /* processed file! */
456 sprintf(errmsg, "(%s): already processed!", nm);
457 error(WARNING, errmsg);
458 matselect = S_NONE;
459 return;
460 }
461 opterr: /* skip faulty option */
462 while (*cp && !isspace(*cp))
463 cp++;
464 nerrs++;
465 }
466 /* print header? */
467 checkhead();
468 /* issue warnings? */
469 if (nerrs) {
470 sprintf(errmsg, "(%s): %d error(s) in option line:",
471 nm, nerrs);
472 error(WARNING, errmsg);
473 wputs(s);
474 printf("# %s: the following option line has %d error(s):\n",
475 progname, nerrs);
476 }
477 /* print pure comment */
478 printf("# %s", s+2);
479 }
480
481 void
482 printopts(void) /* print out option default values */
483 {
484 printf("m=%-15s\t\t# material name\n", thisillum.matname);
485 printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
486 if (thisillum.flags & IL_COLAVG)
487 if (thisillum.flags & IL_COLDST)
488 printf("c=d\t\t\t\t# color distribution\n");
489 else
490 printf("c=a\t\t\t\t# color average\n");
491 else
492 printf("c=n\t\t\t\t# color none\n");
493 if (thisillum.flags & IL_LIGHT)
494 printf("l+\t\t\t\t# light type on\n");
495 else
496 printf("l-\t\t\t\t# light type off\n");
497 printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
498 printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
499 printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
500 }
501
502
503 void
504 printhead( /* print out header */
505 register int ac,
506 register char **av
507 )
508 {
509 putchar('#');
510 while (ac-- > 0) {
511 putchar(' ');
512 fputs(*av++, stdout);
513 }
514 fputs("\n#@mkillum !\n", stdout);
515 }
516
517
518 void
519 xobject( /* translate an object from fp */
520 FILE *fp,
521 char *nm
522 )
523 {
524 OBJREC thisobj;
525 char str[MAXSTR];
526 int doit;
527 /* read the object */
528 if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
529 goto readerr;
530 if (fgetword(str, MAXSTR, fp) == NULL)
531 goto readerr;
532 /* is it an alias? */
533 if (!strcmp(str, ALIASKEY)) {
534 if (fgetword(str, MAXSTR, fp) == NULL)
535 goto readerr;
536 printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
537 if (fgetword(str, MAXSTR, fp) == NULL)
538 goto readerr;
539 printf("\t%s\n", str);
540 return;
541 }
542 thisobj.omod = OVOID; /* unused field */
543 if ((thisobj.otype = otype(str)) < 0) {
544 sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
545 error(USER, errmsg);
546 }
547 if (fgetword(str, MAXSTR, fp) == NULL)
548 goto readerr;
549 thisobj.oname = str;
550 if (readfargs(&thisobj.oargs, fp) != 1)
551 goto readerr;
552 thisobj.os = NULL;
553 /* check for translation */
554 switch (matselect) {
555 case S_NONE:
556 doit = 0;
557 break;
558 case S_ALL:
559 doit = 1;
560 break;
561 case S_ELEM:
562 doit = !strcmp(thisillum.altmat, matcheck);
563 break;
564 case S_COMPL:
565 doit = strcmp(thisillum.altmat, matcheck);
566 break;
567 }
568 doit = doit && issurface(thisobj.otype);
569 /* print header? */
570 checkhead();
571 /* process object */
572 if (doit)
573 (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
574 else
575 printobj(thisillum.altmat, &thisobj);
576 /* free arguments */
577 freefargs(&thisobj.oargs);
578 return;
579 readerr:
580 sprintf(errmsg, "(%s): error reading scene", nm);
581 error(USER, errmsg);
582 }