ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.39
Committed: Sat Oct 13 20:15:43 2012 UTC (11 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.38: +4 -68 lines
Log Message:
Corrected errors in XML interpreter and genBSDF and removed mkillum BSDF code

File Contents

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