ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.29
Committed: Fri Sep 14 21:29:08 2007 UTC (16 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +2 -2 lines
Log Message:
Fixed -defaults option

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.29 static const char RCSid[] = "$Id: mkillum.c,v 2.28 2007/09/13 06:31:21 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 greg 1.2 struct illum_args thisillum = { /* our illum and default values */
27     0,
28     DFLMAT,
29 greg 1.10 DFLDAT,
30 greg 1.2 0,
31     VOIDID,
32 greg 1.3 SAMPDENS,
33 greg 1.2 NSAMPS,
34 greg 1.12 0.,
35 greg 1.2 };
36 greg 1.1
37     char matcheck[MAXSTR]; /* current material to include or exclude */
38     int matselect = S_ALL; /* selection criterion */
39    
40     int gargc; /* global argc */
41     char **gargv; /* global argv */
42    
43     int doneheader = 0; /* printed header yet? */
44 greg 1.2 #define checkhead() if (!doneheader++) printhead(gargc,gargv)
45 greg 1.1
46 greg 1.2 int warnings = 1; /* print warnings? */
47    
48 greg 2.28 void init(char *octnm, int np);
49 schorsch 2.19 void filter(register FILE *infp, char *name);
50     void xoptions(char *s, char *nm);
51     void printopts(void);
52     void printhead(register int ac, register char **av);
53     void xobject(FILE *fp, char *nm);
54 greg 1.1
55 schorsch 2.19
56     int
57     main( /* compute illum distributions using rtrace */
58     int argc,
59     char *argv[]
60     )
61 greg 1.1 {
62 greg 2.20 int nprocs = 1;
63 greg 1.3 FILE *fp;
64 greg 2.28 int rval;
65 greg 1.1 register int i;
66     /* set global arguments */
67 greg 1.3 gargv = argv;
68 greg 2.28 progname = gargv[0];
69     /* set up rendering defaults */
70     dstrsrc = 0.25;
71     directrelay = 3;
72     directvis = 0;
73     ambounce = 2;
74     /* get options from command line */
75 greg 2.29 for (i = 1; i < argc; i++) {
76 greg 2.28 while ((rval = expandarg(&argc, &argv, i)) > 0)
77     ;
78     if (rval < 0) {
79     sprintf(errmsg, "cannot expand '%s'", argv[i]);
80     error(SYSTEM, errmsg);
81     }
82     if (argv[i][0] != '-')
83 greg 1.3 break;
84 greg 2.28 if (!strcmp(argv[i], "-w")) {
85     warnings = 0;
86     continue;
87     }
88     if (!strcmp(argv[i], "-n")) {
89     nprocs = atoi(argv[++i]);
90     if (nprocs <= 0)
91     error(USER, "illegal number of processes");
92     continue;
93     }
94     if (!strcmp(argv[i], "-defaults")) {
95     printopts();
96     print_rdefaults();
97     quit(0);
98     }
99     rval = getrenderopt(argc-i, argv+i);
100     if (rval < 0) {
101     sprintf(errmsg, "bad render option at '%s'", argv[i]);
102     error(USER, errmsg);
103 greg 1.1 }
104 greg 2.28 i += rval;
105 greg 1.1 }
106 greg 2.28 gargc = ++i;
107     /* add "mandatory" render options */
108     do_irrad = 0;
109     if (gargc > argc || argv[gargc-1][0] == '-')
110 greg 2.3 error(USER, "missing octree argument");
111 greg 1.1 /* else initialize and run our calculation */
112 greg 2.28 init(argv[gargc-1], nprocs);
113     if (gargc < argc) {
114     if (gargc == argc-1 || argv[gargc][0] != '<' || argv[gargc][1])
115     error(USER, "Use '< file1 file2 ..' for multiple inputs");
116 greg 1.3 for (i = gargc+1; i < argc; i++) {
117 greg 1.5 if ((fp = fopen(argv[i], "r")) == NULL) {
118 greg 1.3 sprintf(errmsg,
119     "cannot open scene file \"%s\"", argv[i]);
120     error(SYSTEM, errmsg);
121     }
122     filter(fp, argv[i]);
123     fclose(fp);
124     }
125 greg 2.28 } else
126 greg 1.3 filter(stdin, "standard input");
127 greg 2.20 quit(0);
128 schorsch 2.27 return 0; /* pro forma return */
129 greg 1.1 }
130    
131 schorsch 2.26
132 greg 2.11 void
133 greg 2.28 init(char *octnm, int np) /* start rendering process(es) */
134 greg 1.3 {
135     /* set up signal handling */
136 greg 2.25 signal(SIGINT, quit);
137     #ifdef SIGHUP
138     signal(SIGHUP, quit);
139     #endif
140     #ifdef SIGTERM
141     signal(SIGTERM, quit);
142     #endif
143     #ifdef SIGPIPE
144 greg 1.3 signal(SIGPIPE, quit);
145 schorsch 2.13 #endif
146 greg 2.28 /* start rendering process(es) */
147     ray_pinit(octnm, np);
148 greg 1.1 }
149    
150    
151 greg 2.11 void
152 schorsch 2.19 eputs( /* put string to stderr */
153     register char *s
154     )
155 greg 1.1 {
156     static int midline = 0;
157    
158     if (!*s) return;
159     if (!midline) {
160     fputs(progname, stderr);
161     fputs(": ", stderr);
162     }
163     fputs(s, stderr);
164     midline = s[strlen(s)-1] != '\n';
165     }
166    
167    
168 greg 2.11 void
169 greg 1.2 wputs(s) /* print warning if enabled */
170     char *s;
171     {
172     if (warnings)
173     eputs(s);
174     }
175    
176    
177 schorsch 2.19 void
178     filter( /* process stream */
179     register FILE *infp,
180     char *name
181     )
182 greg 1.1 {
183     char buf[512];
184     FILE *pfp;
185     register int c;
186    
187     while ((c = getc(infp)) != EOF) {
188     if (isspace(c))
189     continue;
190     if (c == '#') { /* comment/options */
191     buf[0] = c;
192     fgets(buf+1, sizeof(buf)-1, infp);
193 greg 1.2 xoptions(buf, name);
194 greg 1.1 } else if (c == '!') { /* command */
195     buf[0] = c;
196     fgetline(buf+1, sizeof(buf)-1, infp);
197     if ((pfp = popen(buf+1, "r")) == NULL) {
198     sprintf(errmsg, "cannot execute \"%s\"", buf);
199     error(SYSTEM, errmsg);
200     }
201 greg 1.2 filter(pfp, buf);
202 greg 1.1 pclose(pfp);
203     } else { /* object */
204     ungetc(c, infp);
205     xobject(infp, name);
206     }
207     }
208     }
209    
210    
211 schorsch 2.19 void
212     xoptions( /* process options in string s */
213     char *s,
214     char *nm
215     )
216 greg 1.1 {
217     extern FILE *freopen();
218     char buf[64];
219     int nerrs = 0;
220     register char *cp;
221    
222 greg 1.2 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
223     fputs(s, stdout); /* not for us */
224 greg 1.1 return;
225 greg 1.2 }
226 greg 1.1 cp = s+10;
227     while (*cp) {
228     switch (*cp) {
229     case ' ':
230     case '\t':
231     case '\n':
232 greg 2.11 case '\r':
233     case '\f':
234 greg 1.1 cp++;
235     continue;
236     case 'm': /* material name */
237 greg 1.2 if (*++cp != '=')
238 greg 1.1 break;
239 greg 2.8 if (!*++cp || isspace(*cp))
240 greg 1.2 break;
241 greg 1.1 atos(thisillum.matname, MAXSTR, cp);
242     cp = sskip(cp);
243     if (!(thisillum.flags & IL_DATCLB)) {
244     strcpy(thisillum.datafile, thisillum.matname);
245     thisillum.dfnum = 0;
246     }
247     continue;
248     case 'f': /* data file name */
249 greg 1.2 if (*++cp != '=')
250 greg 1.1 break;
251 greg 2.8 if (!*++cp || isspace(*cp)) {
252 greg 1.6 strcpy(thisillum.datafile,thisillum.matname);
253 greg 1.10 thisillum.dfnum = 0;
254 greg 1.1 thisillum.flags &= ~IL_DATCLB;
255     continue;
256     }
257     atos(thisillum.datafile, MAXSTR, cp);
258     cp = sskip(cp);
259     thisillum.dfnum = 0;
260     thisillum.flags |= IL_DATCLB;
261     continue;
262     case 'i': /* include material */
263     case 'e': /* exclude material */
264 greg 1.6 if (cp[1] != '=')
265 greg 1.1 break;
266 greg 1.7 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
267     cp += 2;
268     atos(matcheck, MAXSTR, cp);
269 greg 1.1 cp = sskip(cp);
270     continue;
271     case 'a': /* use everything */
272     cp = sskip(cp);
273     matselect = S_ALL;
274     continue;
275     case 'n': /* use nothing (passive) */
276     cp = sskip(cp);
277     matselect = S_NONE;
278     continue;
279     case 'c': /* color calculation */
280 greg 1.2 if (*++cp != '=')
281 greg 1.1 break;
282 greg 1.2 switch (*++cp) {
283 greg 1.1 case 'a': /* average */
284 greg 1.6 thisillum.flags = (thisillum.flags|IL_COLAVG)
285     & ~IL_COLDST;
286 greg 1.1 break;
287     case 'd': /* distribution */
288 greg 1.5 thisillum.flags |= (IL_COLDST|IL_COLAVG);
289 greg 1.1 break;
290     case 'n': /* none */
291     thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
292     break;
293     default:
294     goto opterr;
295     }
296     cp = sskip(cp);
297     continue;
298     case 'd': /* point sample density */
299 greg 1.2 if (*++cp != '=')
300 greg 1.1 break;
301 greg 2.11 if (!isintd(++cp, " \t\n\r"))
302 greg 1.1 break;
303 greg 1.3 thisillum.sampdens = atoi(cp);
304 greg 1.1 cp = sskip(cp);
305     continue;
306     case 's': /* point super-samples */
307 greg 1.2 if (*++cp != '=')
308 greg 1.1 break;
309 greg 2.11 if (!isintd(++cp, " \t\n\r"))
310 greg 1.1 break;
311     thisillum.nsamps = atoi(cp);
312     cp = sskip(cp);
313     continue;
314 greg 1.3 case 'l': /* light sources */
315 greg 1.4 cp++;
316     if (*cp == '+')
317 greg 1.3 thisillum.flags |= IL_LIGHT;
318 greg 1.4 else if (*cp == '-')
319     thisillum.flags &= ~IL_LIGHT;
320 greg 1.3 else
321 greg 1.4 break;
322     cp++;
323 greg 1.3 continue;
324 greg 1.12 case 'b': /* brightness */
325     if (*++cp != '=')
326     break;
327 greg 2.11 if (!isfltd(++cp, " \t\n\r"))
328 greg 1.12 break;
329     thisillum.minbrt = atof(cp);
330 greg 1.13 if (thisillum.minbrt < 0.)
331     thisillum.minbrt = 0.;
332 greg 1.12 cp = sskip(cp);
333     continue;
334 greg 1.1 case 'o': /* output file */
335 greg 1.2 if (*++cp != '=')
336 greg 1.1 break;
337 greg 2.8 if (!*++cp || isspace(*cp))
338 greg 1.2 break;
339 greg 1.1 atos(buf, sizeof(buf), cp);
340     cp = sskip(cp);
341     if (freopen(buf, "w", stdout) == NULL) {
342     sprintf(errmsg,
343     "cannot open output file \"%s\"", buf);
344     error(SYSTEM, errmsg);
345     }
346     doneheader = 0;
347     continue;
348 greg 1.6 case '!': /* processed file! */
349 greg 1.7 sprintf(errmsg, "(%s): already processed!", nm);
350 greg 1.6 error(WARNING, errmsg);
351 greg 1.7 matselect = S_NONE;
352 greg 1.6 return;
353 greg 1.1 }
354     opterr: /* skip faulty option */
355 greg 2.9 while (*cp && !isspace(*cp))
356     cp++;
357 greg 1.1 nerrs++;
358     }
359 greg 1.2 /* print header? */
360     checkhead();
361     /* issue warnings? */
362 greg 1.1 if (nerrs) {
363     sprintf(errmsg, "(%s): %d error(s) in option line:",
364     nm, nerrs);
365     error(WARNING, errmsg);
366 greg 1.2 wputs(s);
367     printf("# %s: the following option line has %d error(s):\n",
368     progname, nerrs);
369 greg 1.1 }
370 greg 1.2 /* print pure comment */
371 greg 1.4 printf("# %s", s+2);
372 greg 1.9 }
373    
374 schorsch 2.19 void
375     printopts(void) /* print out option default values */
376 greg 1.9 {
377 greg 1.11 printf("m=%-15s\t\t# material name\n", thisillum.matname);
378     printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
379 greg 2.2 if (thisillum.flags & IL_COLAVG)
380     if (thisillum.flags & IL_COLDST)
381     printf("c=d\t\t\t\t# color distribution\n");
382     else
383     printf("c=a\t\t\t\t# color average\n");
384     else
385     printf("c=n\t\t\t\t# color none\n");
386     if (thisillum.flags & IL_LIGHT)
387     printf("l+\t\t\t\t# light type on\n");
388     else
389     printf("l-\t\t\t\t# light type off\n");
390 greg 1.9 printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
391     printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
392 greg 1.12 printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
393 greg 1.1 }
394 greg 1.2
395    
396 schorsch 2.19 void
397     printhead( /* print out header */
398     register int ac,
399     register char **av
400     )
401 greg 1.2 {
402     putchar('#');
403     while (ac-- > 0) {
404     putchar(' ');
405     fputs(*av++, stdout);
406     }
407 greg 1.6 fputs("\n#@mkillum !\n", stdout);
408 greg 1.2 }
409    
410    
411 schorsch 2.19 void
412     xobject( /* translate an object from fp */
413     FILE *fp,
414     char *nm
415     )
416 greg 1.2 {
417     OBJREC thisobj;
418     char str[MAXSTR];
419     int doit;
420     /* read the object */
421     if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
422     goto readerr;
423     if (fgetword(str, MAXSTR, fp) == NULL)
424     goto readerr;
425     /* is it an alias? */
426 greg 2.12 if (!strcmp(str, ALIASKEY)) {
427 greg 1.2 if (fgetword(str, MAXSTR, fp) == NULL)
428     goto readerr;
429 greg 2.12 printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
430 greg 1.2 if (fgetword(str, MAXSTR, fp) == NULL)
431     goto readerr;
432 greg 1.4 printf("\t%s\n", str);
433 greg 1.2 return;
434     }
435 greg 1.4 thisobj.omod = OVOID; /* unused field */
436 greg 1.2 if ((thisobj.otype = otype(str)) < 0) {
437     sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
438     error(USER, errmsg);
439     }
440     if (fgetword(str, MAXSTR, fp) == NULL)
441     goto readerr;
442     thisobj.oname = str;
443     if (readfargs(&thisobj.oargs, fp) != 1)
444     goto readerr;
445     thisobj.os = NULL;
446     /* check for translation */
447     switch (matselect) {
448     case S_NONE:
449     doit = 0;
450     break;
451     case S_ALL:
452 greg 1.4 doit = 1;
453 greg 1.2 break;
454     case S_ELEM:
455     doit = !strcmp(thisillum.altmat, matcheck);
456     break;
457     case S_COMPL:
458     doit = strcmp(thisillum.altmat, matcheck);
459     break;
460     }
461 greg 1.4 doit = doit && issurface(thisobj.otype);
462 greg 1.2 /* print header? */
463     checkhead();
464     /* process object */
465     if (doit)
466 greg 2.28 switch (thisobj.otype) {
467     case OBJ_FACE:
468     my_face(&thisobj, &thisillum, nm);
469     break;
470     case OBJ_SPHERE:
471     my_sphere(&thisobj, &thisillum, nm);
472     break;
473     case OBJ_RING:
474     my_ring(&thisobj, &thisillum, nm);
475     break;
476     default:
477     my_default(&thisobj, &thisillum, nm);
478     break;
479     }
480 greg 1.2 else
481     printobj(thisillum.altmat, &thisobj);
482     /* free arguments */
483     freefargs(&thisobj.oargs);
484     return;
485     readerr:
486 greg 2.28 sprintf(errmsg, "(%s): error reading input", nm);
487 greg 1.2 error(USER, errmsg);
488     }