ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.27
Committed: Wed Jun 7 17:52:04 2006 UTC (17 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R8
Changes since 2.26: +2 -1 lines
Log Message:
Eliminated some compiler warnings.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.27 static const char RCSid[] = "$Id: mkillum.c,v 2.26 2005/09/19 11:30:10 schorsch 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 schorsch 2.27 return 0; /* pro forma return */
159 greg 1.1 }
160    
161 schorsch 2.26
162     #ifndef SIGALRM
163     #define SIGALRM SIGTERM
164     #endif
165 greg 2.20 static void
166 greg 2.21 killpersist(void) /* kill persistent rtrace process */
167 greg 2.20 {
168     FILE *fp = fopen(persistfn, "r");
169     int pid;
170    
171     if (fp == NULL)
172     return;
173     if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
174     unlink(persistfn);
175     fclose(fp);
176     }
177 greg 1.1
178 schorsch 2.26
179 greg 2.23 int
180     done_rprocs(struct rtproc *rtp)
181     {
182     int st0, st1 = 0;
183    
184     if (rtp->next != NULL) { /* close last opened first! */
185     st1 = done_rprocs(rtp->next);
186     free((void *)rtp->next);
187     rtp->next = NULL;
188     }
189     st0 = close_process(&rtp->pd);
190     if (st0 < 0)
191     error(WARNING, "unknown return status from rtrace process");
192     else if (st0 > 0)
193     return(st0);
194     return(st1);
195     }
196    
197 greg 2.11 void
198 greg 2.20 quit(int status) /* exit with status */
199 greg 1.3 {
200     int rtstat;
201    
202 greg 2.20 if (rt0.next != NULL) /* terminate persistent rtrace */
203     killpersist();
204     /* clean up rtrace process(es) */
205 greg 2.23 rtstat = done_rprocs(&rt0);
206     if (status == 0)
207     status = rtstat;
208 greg 1.3 exit(status);
209     }
210    
211 schorsch 2.19 void
212 greg 2.20 init(int np) /* start rtrace and set up buffers */
213 greg 1.1 {
214 greg 2.20 struct rtproc *rtp;
215     int i;
216 greg 1.1 int maxbytes;
217 greg 1.3 /* set up object functions */
218     ofun[OBJ_FACE].funp = o_face;
219 greg 1.4 ofun[OBJ_SPHERE].funp = o_sphere;
220 greg 1.3 ofun[OBJ_RING].funp = o_ring;
221     /* set up signal handling */
222 greg 2.25 signal(SIGINT, quit);
223     #ifdef SIGHUP
224     signal(SIGHUP, quit);
225     #endif
226     #ifdef SIGTERM
227     signal(SIGTERM, quit);
228     #endif
229     #ifdef SIGPIPE
230 greg 1.3 signal(SIGPIPE, quit);
231 schorsch 2.13 #endif
232 greg 2.20 rtp = &rt0; /* start rtrace process(es) */
233     for (i = 0; i++ < np; ) {
234     errno = 0;
235     maxbytes = open_process(&rtp->pd, rtargv);
236     if (maxbytes == 0) {
237     eputs(rtargv[0]);
238     eputs(": command not found\n");
239     exit(1);
240     }
241     if (maxbytes < 0)
242     error(SYSTEM, "cannot start rtrace process");
243     rtp->bsiz = maxbytes/(6*sizeof(float));
244     rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
245     rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
246     if (rtp->buf == NULL || rtp->dest == NULL)
247     error(SYSTEM, "out of memory in init");
248     rtp->nrays = 0;
249     if (i == np) /* last process? */
250     break;
251 greg 2.24 if (np > 1)
252     sleep(2); /* wait for persist file */
253 greg 2.20 rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
254     if (rtp->next == NULL)
255     error(SYSTEM, "out of memory in init");
256     rtp = rtp->next;
257 greg 1.1 }
258 greg 2.20 rtp->next = NULL;
259 greg 1.6 /* set up urand */
260 greg 2.7 initurand(16384);
261 greg 1.1 }
262    
263    
264 greg 2.11 void
265 schorsch 2.19 eputs( /* put string to stderr */
266     register char *s
267     )
268 greg 1.1 {
269     static int midline = 0;
270    
271     if (!*s) return;
272     if (!midline) {
273     fputs(progname, stderr);
274     fputs(": ", stderr);
275     }
276     fputs(s, stderr);
277     midline = s[strlen(s)-1] != '\n';
278     }
279    
280    
281 greg 2.11 void
282 greg 1.2 wputs(s) /* print warning if enabled */
283     char *s;
284     {
285     if (warnings)
286     eputs(s);
287     }
288    
289    
290 schorsch 2.19 void
291     filter( /* process stream */
292     register FILE *infp,
293     char *name
294     )
295 greg 1.1 {
296     char buf[512];
297     FILE *pfp;
298     register int c;
299    
300     while ((c = getc(infp)) != EOF) {
301     if (isspace(c))
302     continue;
303     if (c == '#') { /* comment/options */
304     buf[0] = c;
305     fgets(buf+1, sizeof(buf)-1, infp);
306 greg 1.2 xoptions(buf, name);
307 greg 1.1 } else if (c == '!') { /* command */
308     buf[0] = c;
309     fgetline(buf+1, sizeof(buf)-1, infp);
310     if ((pfp = popen(buf+1, "r")) == NULL) {
311     sprintf(errmsg, "cannot execute \"%s\"", buf);
312     error(SYSTEM, errmsg);
313     }
314 greg 1.2 filter(pfp, buf);
315 greg 1.1 pclose(pfp);
316     } else { /* object */
317     ungetc(c, infp);
318     xobject(infp, name);
319     }
320     }
321     }
322    
323    
324 schorsch 2.19 void
325     xoptions( /* process options in string s */
326     char *s,
327     char *nm
328     )
329 greg 1.1 {
330     extern FILE *freopen();
331     char buf[64];
332     int nerrs = 0;
333     register char *cp;
334    
335 greg 1.2 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
336     fputs(s, stdout); /* not for us */
337 greg 1.1 return;
338 greg 1.2 }
339 greg 1.1 cp = s+10;
340     while (*cp) {
341     switch (*cp) {
342     case ' ':
343     case '\t':
344     case '\n':
345 greg 2.11 case '\r':
346     case '\f':
347 greg 1.1 cp++;
348     continue;
349     case 'm': /* material name */
350 greg 1.2 if (*++cp != '=')
351 greg 1.1 break;
352 greg 2.8 if (!*++cp || isspace(*cp))
353 greg 1.2 break;
354 greg 1.1 atos(thisillum.matname, MAXSTR, cp);
355     cp = sskip(cp);
356     if (!(thisillum.flags & IL_DATCLB)) {
357     strcpy(thisillum.datafile, thisillum.matname);
358     thisillum.dfnum = 0;
359     }
360     continue;
361     case 'f': /* data file name */
362 greg 1.2 if (*++cp != '=')
363 greg 1.1 break;
364 greg 2.8 if (!*++cp || isspace(*cp)) {
365 greg 1.6 strcpy(thisillum.datafile,thisillum.matname);
366 greg 1.10 thisillum.dfnum = 0;
367 greg 1.1 thisillum.flags &= ~IL_DATCLB;
368     continue;
369     }
370     atos(thisillum.datafile, MAXSTR, cp);
371     cp = sskip(cp);
372     thisillum.dfnum = 0;
373     thisillum.flags |= IL_DATCLB;
374     continue;
375     case 'i': /* include material */
376     case 'e': /* exclude material */
377 greg 1.6 if (cp[1] != '=')
378 greg 1.1 break;
379 greg 1.7 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
380     cp += 2;
381     atos(matcheck, MAXSTR, cp);
382 greg 1.1 cp = sskip(cp);
383     continue;
384     case 'a': /* use everything */
385     cp = sskip(cp);
386     matselect = S_ALL;
387     continue;
388     case 'n': /* use nothing (passive) */
389     cp = sskip(cp);
390     matselect = S_NONE;
391     continue;
392     case 'c': /* color calculation */
393 greg 1.2 if (*++cp != '=')
394 greg 1.1 break;
395 greg 1.2 switch (*++cp) {
396 greg 1.1 case 'a': /* average */
397 greg 1.6 thisillum.flags = (thisillum.flags|IL_COLAVG)
398     & ~IL_COLDST;
399 greg 1.1 break;
400     case 'd': /* distribution */
401 greg 1.5 thisillum.flags |= (IL_COLDST|IL_COLAVG);
402 greg 1.1 break;
403     case 'n': /* none */
404     thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
405     break;
406     default:
407     goto opterr;
408     }
409     cp = sskip(cp);
410     continue;
411     case 'd': /* point sample density */
412 greg 1.2 if (*++cp != '=')
413 greg 1.1 break;
414 greg 2.11 if (!isintd(++cp, " \t\n\r"))
415 greg 1.1 break;
416 greg 1.3 thisillum.sampdens = atoi(cp);
417 greg 1.1 cp = sskip(cp);
418     continue;
419     case 's': /* point super-samples */
420 greg 1.2 if (*++cp != '=')
421 greg 1.1 break;
422 greg 2.11 if (!isintd(++cp, " \t\n\r"))
423 greg 1.1 break;
424     thisillum.nsamps = atoi(cp);
425     cp = sskip(cp);
426     continue;
427 greg 1.3 case 'l': /* light sources */
428 greg 1.4 cp++;
429     if (*cp == '+')
430 greg 1.3 thisillum.flags |= IL_LIGHT;
431 greg 1.4 else if (*cp == '-')
432     thisillum.flags &= ~IL_LIGHT;
433 greg 1.3 else
434 greg 1.4 break;
435     cp++;
436 greg 1.3 continue;
437 greg 1.12 case 'b': /* brightness */
438     if (*++cp != '=')
439     break;
440 greg 2.11 if (!isfltd(++cp, " \t\n\r"))
441 greg 1.12 break;
442     thisillum.minbrt = atof(cp);
443 greg 1.13 if (thisillum.minbrt < 0.)
444     thisillum.minbrt = 0.;
445 greg 1.12 cp = sskip(cp);
446     continue;
447 greg 1.1 case 'o': /* output file */
448 greg 1.2 if (*++cp != '=')
449 greg 1.1 break;
450 greg 2.8 if (!*++cp || isspace(*cp))
451 greg 1.2 break;
452 greg 1.1 atos(buf, sizeof(buf), cp);
453     cp = sskip(cp);
454     if (freopen(buf, "w", stdout) == NULL) {
455     sprintf(errmsg,
456     "cannot open output file \"%s\"", buf);
457     error(SYSTEM, errmsg);
458     }
459     doneheader = 0;
460     continue;
461 greg 1.6 case '!': /* processed file! */
462 greg 1.7 sprintf(errmsg, "(%s): already processed!", nm);
463 greg 1.6 error(WARNING, errmsg);
464 greg 1.7 matselect = S_NONE;
465 greg 1.6 return;
466 greg 1.1 }
467     opterr: /* skip faulty option */
468 greg 2.9 while (*cp && !isspace(*cp))
469     cp++;
470 greg 1.1 nerrs++;
471     }
472 greg 1.2 /* print header? */
473     checkhead();
474     /* issue warnings? */
475 greg 1.1 if (nerrs) {
476     sprintf(errmsg, "(%s): %d error(s) in option line:",
477     nm, nerrs);
478     error(WARNING, errmsg);
479 greg 1.2 wputs(s);
480     printf("# %s: the following option line has %d error(s):\n",
481     progname, nerrs);
482 greg 1.1 }
483 greg 1.2 /* print pure comment */
484 greg 1.4 printf("# %s", s+2);
485 greg 1.9 }
486    
487 schorsch 2.19 void
488     printopts(void) /* print out option default values */
489 greg 1.9 {
490 greg 1.11 printf("m=%-15s\t\t# material name\n", thisillum.matname);
491     printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
492 greg 2.2 if (thisillum.flags & IL_COLAVG)
493     if (thisillum.flags & IL_COLDST)
494     printf("c=d\t\t\t\t# color distribution\n");
495     else
496     printf("c=a\t\t\t\t# color average\n");
497     else
498     printf("c=n\t\t\t\t# color none\n");
499     if (thisillum.flags & IL_LIGHT)
500     printf("l+\t\t\t\t# light type on\n");
501     else
502     printf("l-\t\t\t\t# light type off\n");
503 greg 1.9 printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
504     printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
505 greg 1.12 printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
506 greg 1.1 }
507 greg 1.2
508    
509 schorsch 2.19 void
510     printhead( /* print out header */
511     register int ac,
512     register char **av
513     )
514 greg 1.2 {
515     putchar('#');
516     while (ac-- > 0) {
517     putchar(' ');
518     fputs(*av++, stdout);
519     }
520 greg 1.6 fputs("\n#@mkillum !\n", stdout);
521 greg 1.2 }
522    
523    
524 schorsch 2.19 void
525     xobject( /* translate an object from fp */
526     FILE *fp,
527     char *nm
528     )
529 greg 1.2 {
530     OBJREC thisobj;
531     char str[MAXSTR];
532     int doit;
533     /* read the object */
534     if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
535     goto readerr;
536     if (fgetword(str, MAXSTR, fp) == NULL)
537     goto readerr;
538     /* is it an alias? */
539 greg 2.12 if (!strcmp(str, ALIASKEY)) {
540 greg 1.2 if (fgetword(str, MAXSTR, fp) == NULL)
541     goto readerr;
542 greg 2.12 printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
543 greg 1.2 if (fgetword(str, MAXSTR, fp) == NULL)
544     goto readerr;
545 greg 1.4 printf("\t%s\n", str);
546 greg 1.2 return;
547     }
548 greg 1.4 thisobj.omod = OVOID; /* unused field */
549 greg 1.2 if ((thisobj.otype = otype(str)) < 0) {
550     sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
551     error(USER, errmsg);
552     }
553     if (fgetword(str, MAXSTR, fp) == NULL)
554     goto readerr;
555     thisobj.oname = str;
556     if (readfargs(&thisobj.oargs, fp) != 1)
557     goto readerr;
558     thisobj.os = NULL;
559     /* check for translation */
560     switch (matselect) {
561     case S_NONE:
562     doit = 0;
563     break;
564     case S_ALL:
565 greg 1.4 doit = 1;
566 greg 1.2 break;
567     case S_ELEM:
568     doit = !strcmp(thisillum.altmat, matcheck);
569     break;
570     case S_COMPL:
571     doit = strcmp(thisillum.altmat, matcheck);
572     break;
573     }
574 greg 1.4 doit = doit && issurface(thisobj.otype);
575 greg 1.2 /* print header? */
576     checkhead();
577     /* process object */
578     if (doit)
579 greg 2.20 (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
580 greg 1.2 else
581     printobj(thisillum.altmat, &thisobj);
582     /* free arguments */
583     freefargs(&thisobj.oargs);
584     return;
585     readerr:
586     sprintf(errmsg, "(%s): error reading scene", nm);
587     error(USER, errmsg);
588     }