ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 1.3
Committed: Tue Jul 23 17:18:21 1991 UTC (32 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +64 -42 lines
Log Message:
added input file capability

File Contents

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