ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.14
Committed: Thu Jun 26 00:58:09 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.13: +5 -4 lines
Log Message:
Abstracted process and path handling for Windows.
Renamed FLOAT to RREAL because of conflict on Windows.
Added conditional compiles for some signal handlers.

File Contents

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