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, 10 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

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.25 static const char RCSid[] = "$Id: mkillum.c,v 2.24 2005/05/25 04:44:25 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Make illum sources for optimizing rendering process
6     */
7    
8 greg 1.3 #include <signal.h>
9 schorsch 2.13 #include <ctype.h>
10 greg 1.3
11 greg 2.18 #include "platform.h"
12 greg 2.15 #include "mkillum.h"
13 schorsch 2.19 #include "random.h"
14 greg 1.1
15     /* default parameters */
16 greg 1.8 #define SAMPDENS 48 /* points per projected steradian */
17 greg 1.1 #define NSAMPS 32 /* samples per point */
18     #define DFLMAT "illum_mat" /* material name */
19 greg 1.10 #define DFLDAT "illum" /* data file name */
20 greg 1.1 /* 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 greg 2.5 char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-",
28 greg 2.17 "-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", };
29 greg 1.4 int rtargc = 14;
30 greg 1.1 /* overriding rtrace options */
31 gregl 2.10 char *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-",
32     "-fff", "-y", "0", NULL };
33 greg 1.1
34 greg 2.20 struct rtproc rt0; /* head of rtrace process list */
35 greg 1.1
36 greg 1.2 struct illum_args thisillum = { /* our illum and default values */
37     0,
38     DFLMAT,
39 greg 1.10 DFLDAT,
40 greg 1.2 0,
41     VOIDID,
42 greg 1.3 SAMPDENS,
43 greg 1.2 NSAMPS,
44 greg 1.12 0.,
45 greg 1.2 };
46 greg 1.1
47     char matcheck[MAXSTR]; /* current material to include or exclude */
48     int matselect = S_ALL; /* selection criterion */
49    
50 greg 1.2 FUN ofun[NUMOTYPE] = INIT_OTYPE; /* object types */
51 greg 1.1
52 greg 2.20 char persistfn[] = "pfXXXXXX"; /* persist file name */
53    
54 greg 1.1 int gargc; /* global argc */
55     char **gargv; /* global argv */
56     #define progname gargv[0]
57    
58     int doneheader = 0; /* printed header yet? */
59 greg 1.2 #define checkhead() if (!doneheader++) printhead(gargc,gargv)
60 greg 1.1
61 greg 1.2 int warnings = 1; /* print warnings? */
62    
63 greg 2.23 int done_rprocs(struct rtproc *rtp);
64 greg 2.20 void init(int np);
65 schorsch 2.19 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 greg 1.1
71 schorsch 2.19
72     int
73     main( /* compute illum distributions using rtrace */
74     int argc,
75     char *argv[]
76     )
77 greg 1.1 {
78 greg 2.20 int nprocs = 1;
79 greg 1.1 char *rtpath;
80 greg 1.3 FILE *fp;
81 greg 1.1 register int i;
82     /* set global arguments */
83 greg 1.3 gargv = argv;
84 greg 2.20 /* 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 greg 1.1 /* set up rtrace command */
93 greg 2.20 for ( ; i < argc; i++) {
94 greg 2.6 if (argv[i][0] == '<' && argv[i][1] == '\0')
95 greg 1.3 break;
96 greg 1.1 rtargv[rtargc++] = argv[i];
97 greg 1.2 if (argv[i][0] == '-' && argv[i][1] == 'w')
98 greg 2.6 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 greg 1.2 }
116 greg 2.3 gargc = i;
117 greg 2.20 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 greg 1.1 for (i = 0; myrtopts[i] != NULL; i++)
125     rtargv[rtargc++] = myrtopts[i];
126 greg 2.20 /* finally, put back final argument */
127 greg 1.3 rtargv[rtargc++] = argv[gargc-1];
128 greg 1.1 rtargv[rtargc] = NULL;
129 greg 2.20 if (!nprocs) { /* just asking for defaults? */
130 greg 1.9 printopts(); fflush(stdout);
131 greg 1.1 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 greg 2.20 perror(rtpath); /* execv() should not return */
139 greg 1.1 exit(1);
140     }
141 greg 2.3 if (gargc < 2 || argv[gargc-1][0] == '-')
142     error(USER, "missing octree argument");
143 greg 1.1 /* else initialize and run our calculation */
144 greg 2.20 init(nprocs);
145 greg 1.3 if (gargc+1 < argc)
146     for (i = gargc+1; i < argc; i++) {
147 greg 1.5 if ((fp = fopen(argv[i], "r")) == NULL) {
148 greg 1.3 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 greg 2.20 quit(0);
158 greg 1.1 }
159    
160 greg 2.20 static void
161 greg 2.21 killpersist(void) /* kill persistent rtrace process */
162 greg 2.20 {
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 greg 1.1
173 greg 2.23 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 greg 2.11 void
192 greg 2.20 quit(int status) /* exit with status */
193 greg 1.3 {
194     int rtstat;
195    
196 greg 2.20 if (rt0.next != NULL) /* terminate persistent rtrace */
197     killpersist();
198     /* clean up rtrace process(es) */
199 greg 2.23 rtstat = done_rprocs(&rt0);
200     if (status == 0)
201     status = rtstat;
202 greg 1.3 exit(status);
203     }
204    
205 schorsch 2.19 void
206 greg 2.20 init(int np) /* start rtrace and set up buffers */
207 greg 1.1 {
208 greg 2.20 struct rtproc *rtp;
209     int i;
210 greg 1.1 int maxbytes;
211 greg 1.3 /* set up object functions */
212     ofun[OBJ_FACE].funp = o_face;
213 greg 1.4 ofun[OBJ_SPHERE].funp = o_sphere;
214 greg 1.3 ofun[OBJ_RING].funp = o_ring;
215     /* set up signal handling */
216 greg 2.25 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 greg 1.3 signal(SIGPIPE, quit);
225 schorsch 2.13 #endif
226 greg 2.20 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 greg 2.24 if (np > 1)
246     sleep(2); /* wait for persist file */
247 greg 2.20 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 greg 1.1 }
252 greg 2.20 rtp->next = NULL;
253 greg 1.6 /* set up urand */
254 greg 2.7 initurand(16384);
255 greg 1.1 }
256    
257    
258 greg 2.11 void
259 schorsch 2.19 eputs( /* put string to stderr */
260     register char *s
261     )
262 greg 1.1 {
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 greg 2.11 void
276 greg 1.2 wputs(s) /* print warning if enabled */
277     char *s;
278     {
279     if (warnings)
280     eputs(s);
281     }
282    
283    
284 schorsch 2.19 void
285     filter( /* process stream */
286     register FILE *infp,
287     char *name
288     )
289 greg 1.1 {
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 greg 1.2 xoptions(buf, name);
301 greg 1.1 } 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 greg 1.2 filter(pfp, buf);
309 greg 1.1 pclose(pfp);
310     } else { /* object */
311     ungetc(c, infp);
312     xobject(infp, name);
313     }
314     }
315     }
316    
317    
318 schorsch 2.19 void
319     xoptions( /* process options in string s */
320     char *s,
321     char *nm
322     )
323 greg 1.1 {
324     extern FILE *freopen();
325     char buf[64];
326     int nerrs = 0;
327     register char *cp;
328    
329 greg 1.2 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
330     fputs(s, stdout); /* not for us */
331 greg 1.1 return;
332 greg 1.2 }
333 greg 1.1 cp = s+10;
334     while (*cp) {
335     switch (*cp) {
336     case ' ':
337     case '\t':
338     case '\n':
339 greg 2.11 case '\r':
340     case '\f':
341 greg 1.1 cp++;
342     continue;
343     case 'm': /* material name */
344 greg 1.2 if (*++cp != '=')
345 greg 1.1 break;
346 greg 2.8 if (!*++cp || isspace(*cp))
347 greg 1.2 break;
348 greg 1.1 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 greg 1.2 if (*++cp != '=')
357 greg 1.1 break;
358 greg 2.8 if (!*++cp || isspace(*cp)) {
359 greg 1.6 strcpy(thisillum.datafile,thisillum.matname);
360 greg 1.10 thisillum.dfnum = 0;
361 greg 1.1 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 greg 1.6 if (cp[1] != '=')
372 greg 1.1 break;
373 greg 1.7 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
374     cp += 2;
375     atos(matcheck, MAXSTR, cp);
376 greg 1.1 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 greg 1.2 if (*++cp != '=')
388 greg 1.1 break;
389 greg 1.2 switch (*++cp) {
390 greg 1.1 case 'a': /* average */
391 greg 1.6 thisillum.flags = (thisillum.flags|IL_COLAVG)
392     & ~IL_COLDST;
393 greg 1.1 break;
394     case 'd': /* distribution */
395 greg 1.5 thisillum.flags |= (IL_COLDST|IL_COLAVG);
396 greg 1.1 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 greg 1.2 if (*++cp != '=')
407 greg 1.1 break;
408 greg 2.11 if (!isintd(++cp, " \t\n\r"))
409 greg 1.1 break;
410 greg 1.3 thisillum.sampdens = atoi(cp);
411 greg 1.1 cp = sskip(cp);
412     continue;
413     case 's': /* point super-samples */
414 greg 1.2 if (*++cp != '=')
415 greg 1.1 break;
416 greg 2.11 if (!isintd(++cp, " \t\n\r"))
417 greg 1.1 break;
418     thisillum.nsamps = atoi(cp);
419     cp = sskip(cp);
420     continue;
421 greg 1.3 case 'l': /* light sources */
422 greg 1.4 cp++;
423     if (*cp == '+')
424 greg 1.3 thisillum.flags |= IL_LIGHT;
425 greg 1.4 else if (*cp == '-')
426     thisillum.flags &= ~IL_LIGHT;
427 greg 1.3 else
428 greg 1.4 break;
429     cp++;
430 greg 1.3 continue;
431 greg 1.12 case 'b': /* brightness */
432     if (*++cp != '=')
433     break;
434 greg 2.11 if (!isfltd(++cp, " \t\n\r"))
435 greg 1.12 break;
436     thisillum.minbrt = atof(cp);
437 greg 1.13 if (thisillum.minbrt < 0.)
438     thisillum.minbrt = 0.;
439 greg 1.12 cp = sskip(cp);
440     continue;
441 greg 1.1 case 'o': /* output file */
442 greg 1.2 if (*++cp != '=')
443 greg 1.1 break;
444 greg 2.8 if (!*++cp || isspace(*cp))
445 greg 1.2 break;
446 greg 1.1 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 greg 1.6 case '!': /* processed file! */
456 greg 1.7 sprintf(errmsg, "(%s): already processed!", nm);
457 greg 1.6 error(WARNING, errmsg);
458 greg 1.7 matselect = S_NONE;
459 greg 1.6 return;
460 greg 1.1 }
461     opterr: /* skip faulty option */
462 greg 2.9 while (*cp && !isspace(*cp))
463     cp++;
464 greg 1.1 nerrs++;
465     }
466 greg 1.2 /* print header? */
467     checkhead();
468     /* issue warnings? */
469 greg 1.1 if (nerrs) {
470     sprintf(errmsg, "(%s): %d error(s) in option line:",
471     nm, nerrs);
472     error(WARNING, errmsg);
473 greg 1.2 wputs(s);
474     printf("# %s: the following option line has %d error(s):\n",
475     progname, nerrs);
476 greg 1.1 }
477 greg 1.2 /* print pure comment */
478 greg 1.4 printf("# %s", s+2);
479 greg 1.9 }
480    
481 schorsch 2.19 void
482     printopts(void) /* print out option default values */
483 greg 1.9 {
484 greg 1.11 printf("m=%-15s\t\t# material name\n", thisillum.matname);
485     printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
486 greg 2.2 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 greg 1.9 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 greg 1.12 printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
500 greg 1.1 }
501 greg 1.2
502    
503 schorsch 2.19 void
504     printhead( /* print out header */
505     register int ac,
506     register char **av
507     )
508 greg 1.2 {
509     putchar('#');
510     while (ac-- > 0) {
511     putchar(' ');
512     fputs(*av++, stdout);
513     }
514 greg 1.6 fputs("\n#@mkillum !\n", stdout);
515 greg 1.2 }
516    
517    
518 schorsch 2.19 void
519     xobject( /* translate an object from fp */
520     FILE *fp,
521     char *nm
522     )
523 greg 1.2 {
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 greg 2.12 if (!strcmp(str, ALIASKEY)) {
534 greg 1.2 if (fgetword(str, MAXSTR, fp) == NULL)
535     goto readerr;
536 greg 2.12 printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
537 greg 1.2 if (fgetword(str, MAXSTR, fp) == NULL)
538     goto readerr;
539 greg 1.4 printf("\t%s\n", str);
540 greg 1.2 return;
541     }
542 greg 1.4 thisobj.omod = OVOID; /* unused field */
543 greg 1.2 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 greg 1.4 doit = 1;
560 greg 1.2 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 greg 1.4 doit = doit && issurface(thisobj.otype);
569 greg 1.2 /* print header? */
570     checkhead();
571     /* process object */
572     if (doit)
573 greg 2.20 (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
574 greg 1.2 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     }