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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: mkillum.c,v 2.38 2011/10/05 17:20:55 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 "rtprocess.h" /* win_popen() */
12 #include "mkillum.h"
13
14 /* default parameters */
15 #define SAMPDENS 48 /* points per projected steradian */
16 #define NSAMPS 32 /* samples per point */
17 #define DFLMAT "illum_mat" /* material name */
18 #define DFLDAT "illum" /* data file name */
19 /* 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 struct illum_args thisillum = { /* our illum and default values */
26 0,
27 0.,
28 DFLMAT,
29 DFLDAT,
30 0,
31 VOIDID,
32 SAMPDENS,
33 NSAMPS,
34 0.,
35 };
36
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 #define checkhead() if (!doneheader++) printhead(gargc,gargv)
45
46 int warnings = 1; /* print warnings? */
47
48 void init(char *octnm, int np);
49 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
55
56 int
57 main( /* compute illum distributions using rtrace */
58 int argc,
59 char *argv[]
60 )
61 {
62 int nprocs = 1;
63 FILE *fp;
64 int rval;
65 register int i;
66 /* set global arguments */
67 gargv = argv;
68 progname = gargv[0];
69 /* set up rendering defaults */
70 dstrsrc = 0.5;
71 directrelay = 3;
72 ambounce = 2;
73 /* get options from command line */
74 for (i = 1; i < argc; i++) {
75 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 break;
83 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 }
103 i += rval;
104 }
105 gargc = ++i;
106 /* add "mandatory" render options */
107 do_irrad = 0;
108 if (gargc > argc || argv[gargc-1][0] == '-')
109 error(USER, "missing octree argument");
110 /* else initialize and run our calculation */
111 init(argv[gargc-1], nprocs);
112 if (gargc < argc) {
113 if (gargc == argc-1 || argv[gargc][0] != '<' || argv[gargc][1])
114 error(USER, "use '< file1 file2 ..' for multiple inputs");
115 for (i = gargc+1; i < argc; i++) {
116 if ((fp = fopen(argv[i], "r")) == NULL) {
117 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 } else
125 filter(stdin, "standard input");
126 quit(0);
127 return 0; /* pro forma return */
128 }
129
130
131 void
132 init(char *octnm, int np) /* start rendering process(es) */
133 {
134 /* set up signal handling */
135 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 signal(SIGPIPE, quit);
144 #endif
145 /* start rendering process(es) */
146 ray_pinit(octnm, np);
147 }
148
149
150 void
151 eputs( /* put string to stderr */
152 register char *s
153 )
154 {
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 void
168 wputs(s) /* print warning if enabled */
169 char *s;
170 {
171 if (warnings)
172 eputs(s);
173 }
174
175
176 void
177 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 filter( /* process stream */
188 register FILE *infp,
189 char *name
190 )
191 {
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 xoptions(buf, name);
203 } 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 filter(pfp, buf);
211 pclose(pfp);
212 } else { /* object */
213 ungetc(c, infp);
214 xobject(infp, name);
215 }
216 }
217 }
218
219
220 void
221 xoptions( /* process options in string s */
222 char *s,
223 char *nm
224 )
225 {
226 extern FILE *freopen();
227 char buf[64];
228 int negax;
229 int nerrs = 0;
230 register char *cp;
231
232 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
233 fputs(s, stdout); /* not for us */
234 return;
235 }
236 cp = s+10;
237 while (*cp) {
238 switch (*cp) {
239 case ' ':
240 case '\t':
241 case '\n':
242 case '\r':
243 case '\f':
244 cp++;
245 continue;
246 case 'm': /* material name */
247 if (*++cp != '=')
248 break;
249 if (!*++cp || isspace(*cp))
250 break;
251 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 if (*++cp != '=')
260 break;
261 if (!*++cp || isspace(*cp)) {
262 strcpy(thisillum.datafile,thisillum.matname);
263 thisillum.dfnum = 0;
264 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 if (cp[1] != '=')
275 break;
276 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
277 cp += 2;
278 atos(matcheck, MAXSTR, cp);
279 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 if (*++cp != '=')
291 break;
292 switch (*++cp) {
293 case 'a': /* average */
294 thisillum.flags = (thisillum.flags|IL_COLAVG)
295 & ~IL_COLDST;
296 break;
297 case 'd': /* distribution */
298 thisillum.flags |= (IL_COLDST|IL_COLAVG);
299 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 case 'd': /* sample density */
309 if (*++cp != '=')
310 break;
311 if (!*++cp || isspace(*cp))
312 continue;
313 if (isintd(cp, " \t\n\r")) {
314 thisillum.sampdens = atoi(cp);
315 } else {
316 error(WARNING, "direct BSDF input unsupported");
317 goto opterr;
318 }
319 cp = sskip(cp);
320 continue;
321 case 's': /* surface super-samples */
322 if (*++cp != '=')
323 break;
324 if (!isintd(++cp, " \t\n\r"))
325 break;
326 thisillum.nsamps = atoi(cp);
327 cp = sskip(cp);
328 continue;
329 case 'l': /* light sources */
330 cp++;
331 if (*cp == '+')
332 thisillum.flags |= IL_LIGHT;
333 else if (*cp == '-')
334 thisillum.flags &= ~IL_LIGHT;
335 else
336 break;
337 cp++;
338 continue;
339 case 'b': /* brightness */
340 if (*++cp != '=')
341 break;
342 if (!isfltd(++cp, " \t\n\r"))
343 break;
344 thisillum.minbrt = atof(cp);
345 if (thisillum.minbrt < 0.)
346 thisillum.minbrt = 0.;
347 cp = sskip(cp);
348 continue;
349 case 'o': /* output file */
350 if (*++cp != '=')
351 break;
352 if (!*++cp || isspace(*cp))
353 break;
354 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 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 case '!': /* processed file! */
374 sprintf(errmsg, "(%s): already processed!", nm);
375 error(WARNING, errmsg);
376 matselect = S_NONE;
377 return;
378 }
379 opterr: /* skip faulty option */
380 while (*cp && !isspace(*cp))
381 cp++;
382 nerrs++;
383 }
384 /* print header? */
385 checkhead();
386 /* issue warnings? */
387 if (nerrs) {
388 sprintf(errmsg, "(%s): %d error(s) in option line:",
389 nm, nerrs);
390 error(WARNING, errmsg);
391 wputs(s);
392 printf("# %s: the following option line has %d error(s):\n",
393 progname, nerrs);
394 }
395 /* print pure comment */
396 printf("# %s", s+2);
397 }
398
399 void
400 printopts(void) /* print out option default values */
401 {
402 printf("m=%-15s\t\t# material name\n", thisillum.matname);
403 printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
404 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 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 printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
418 printf("t=%f\t\t\t# object thickness\n", thisillum.thick);
419 }
420
421
422 void
423 printhead( /* print out header */
424 register int ac,
425 register char **av
426 )
427 {
428 putchar('#');
429 while (ac-- > 0) {
430 putchar(' ');
431 fputs(*av++, stdout);
432 }
433 fputs("\n#@mkillum !\n", stdout);
434 }
435
436
437 void
438 xobject( /* translate an object from fp */
439 FILE *fp,
440 char *nm
441 )
442 {
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 if (!strcmp(str, ALIASKEY)) {
453 if (fgetword(str, MAXSTR, fp) == NULL)
454 goto readerr;
455 printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
456 if (fgetword(str, MAXSTR, fp) == NULL)
457 goto readerr;
458 printf("\t%s\n", str);
459 return;
460 }
461 thisobj.omod = OVOID; /* unused field */
462 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 doit = 1;
479 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 doit = doit && issurface(thisobj.otype);
488 /* print header? */
489 checkhead();
490 /* process object */
491 if (doit)
492 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 else
507 printobj(thisillum.altmat, &thisobj);
508 /* free arguments */
509 freefargs(&thisobj.oargs);
510 return;
511 readerr:
512 sprintf(errmsg, "(%s): error reading input", nm);
513 error(USER, errmsg);
514 }