ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.10
Committed: Mon Oct 20 16:37:14 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.9: +2 -1 lines
Log Message:
added -ld- to rtrace options list

File Contents

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