ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.6
Committed: Tue Sep 21 17:09:59 1993 UTC (30 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +18 -2 lines
Log Message:
fixed detection of -w+, etc.

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 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 greg 2.4 char *myrtopts[] = { "-I-", "-i-", "-ov", "-h-", "-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 extern char *fgetline(), *fgetword(), *sskip(),
63 greg 1.6 *atos(), *iskip(), *fskip(), *strcpy();
64 greg 1.1 extern FILE *popen();
65    
66    
67     main(argc, argv) /* compute illum distributions using rtrace */
68     int argc;
69     char *argv[];
70     {
71     extern char *getenv(), *getpath();
72     char *rtpath;
73 greg 1.3 FILE *fp;
74 greg 1.1 register int i;
75     /* set global arguments */
76 greg 1.3 gargv = argv;
77 greg 1.1 /* set up rtrace command */
78 greg 1.3 for (i = 1; i < argc; i++) {
79 greg 2.6 if (argv[i][0] == '<' && argv[i][1] == '\0')
80 greg 1.3 break;
81 greg 1.1 rtargv[rtargc++] = argv[i];
82 greg 1.2 if (argv[i][0] == '-' && argv[i][1] == 'w')
83 greg 2.6 switch (argv[i][2]) {
84     case '\0':
85     warnings = !warnings;
86     break;
87     case '+':
88     case 'T': case 't':
89     case 'Y': case 'y':
90     case '1':
91     warnings = 1;
92     break;
93     case '-':
94     case 'F': case 'f':
95     case 'N': case 'n':
96     case '0':
97     warnings = 0;
98     break;
99     }
100 greg 1.2 }
101 greg 2.3 gargc = i;
102 greg 1.3 rtargc--;
103 greg 1.1 for (i = 0; myrtopts[i] != NULL; i++)
104     rtargv[rtargc++] = myrtopts[i];
105 greg 1.3 rtargv[rtargc++] = argv[gargc-1];
106 greg 1.1 rtargv[rtargc] = NULL;
107     /* just asking for defaults? */
108 greg 1.3 if (!strcmp(argv[gargc-1], "-defaults")) {
109 greg 1.9 printopts(); fflush(stdout);
110 greg 1.1 rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
111     if (rtpath == NULL) {
112     eputs(rtargv[0]);
113     eputs(": command not found\n");
114     exit(1);
115     }
116     execv(rtpath, rtargv);
117     perror(rtpath);
118     exit(1);
119     }
120 greg 2.3 if (gargc < 2 || argv[gargc-1][0] == '-')
121     error(USER, "missing octree argument");
122 greg 1.1 /* else initialize and run our calculation */
123     init();
124 greg 1.3 if (gargc+1 < argc)
125     for (i = gargc+1; i < argc; i++) {
126 greg 1.5 if ((fp = fopen(argv[i], "r")) == NULL) {
127 greg 1.3 sprintf(errmsg,
128     "cannot open scene file \"%s\"", argv[i]);
129     error(SYSTEM, errmsg);
130     }
131     filter(fp, argv[i]);
132     fclose(fp);
133     }
134     else
135     filter(stdin, "standard input");
136 greg 1.2 quit(0);
137 greg 1.1 }
138    
139    
140 greg 1.3 quit(status) /* exit with status */
141     int status;
142     {
143     int rtstat;
144    
145     rtstat = close_process(rt.pd);
146     if (status == 0)
147     if (rtstat < 0)
148     error(WARNING,
149     "unknown return status from rtrace process");
150     else
151     status = rtstat;
152     exit(status);
153     }
154    
155    
156 greg 1.1 init() /* start rtrace and set up buffers */
157     {
158 greg 1.4 extern int o_face(), o_sphere(), o_ring();
159 greg 1.1 int maxbytes;
160 greg 1.3 /* set up object functions */
161     ofun[OBJ_FACE].funp = o_face;
162 greg 1.4 ofun[OBJ_SPHERE].funp = o_sphere;
163 greg 1.3 ofun[OBJ_RING].funp = o_ring;
164     /* set up signal handling */
165     signal(SIGPIPE, quit);
166     /* start rtrace process */
167 greg 1.14 errno = 0;
168 greg 1.2 maxbytes = open_process(rt.pd, rtargv);
169 greg 1.1 if (maxbytes == 0) {
170     eputs(rtargv[0]);
171     eputs(": command not found\n");
172     exit(1);
173     }
174     if (maxbytes < 0)
175     error(SYSTEM, "cannot start rtrace process");
176 greg 1.2 rt.bsiz = maxbytes/(6*sizeof(float));
177 greg 1.4 rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
178     rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
179     if (rt.buf == NULL || rt.dest == NULL)
180 greg 1.1 error(SYSTEM, "out of memory in init");
181 greg 1.2 rt.nrays = 0;
182 greg 1.6 /* set up urand */
183     initurand(2048);
184 greg 1.1 }
185    
186    
187     eputs(s) /* put string to stderr */
188     register char *s;
189     {
190     static int midline = 0;
191    
192     if (!*s) return;
193     if (!midline) {
194     fputs(progname, stderr);
195     fputs(": ", stderr);
196     }
197     fputs(s, stderr);
198     midline = s[strlen(s)-1] != '\n';
199     }
200    
201    
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     cp++;
262     continue;
263     case 'm': /* material name */
264 greg 1.2 if (*++cp != '=')
265 greg 1.1 break;
266 greg 1.2 if (!*++cp)
267     break;
268 greg 1.1 atos(thisillum.matname, MAXSTR, cp);
269     cp = sskip(cp);
270     if (!(thisillum.flags & IL_DATCLB)) {
271     strcpy(thisillum.datafile, thisillum.matname);
272     thisillum.dfnum = 0;
273     }
274     continue;
275     case 'f': /* data file name */
276 greg 1.2 if (*++cp != '=')
277 greg 1.1 break;
278 greg 1.2 if (!*++cp) {
279 greg 1.6 strcpy(thisillum.datafile,thisillum.matname);
280 greg 1.10 thisillum.dfnum = 0;
281 greg 1.1 thisillum.flags &= ~IL_DATCLB;
282     continue;
283     }
284     atos(thisillum.datafile, MAXSTR, cp);
285     cp = sskip(cp);
286     thisillum.dfnum = 0;
287     thisillum.flags |= IL_DATCLB;
288     continue;
289     case 'i': /* include material */
290     case 'e': /* exclude material */
291 greg 1.6 if (cp[1] != '=')
292 greg 1.1 break;
293 greg 1.7 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
294     cp += 2;
295     atos(matcheck, MAXSTR, cp);
296 greg 1.1 cp = sskip(cp);
297     continue;
298     case 'a': /* use everything */
299     cp = sskip(cp);
300     matselect = S_ALL;
301     continue;
302     case 'n': /* use nothing (passive) */
303     cp = sskip(cp);
304     matselect = S_NONE;
305     continue;
306     case 'c': /* color calculation */
307 greg 1.2 if (*++cp != '=')
308 greg 1.1 break;
309 greg 1.2 switch (*++cp) {
310 greg 1.1 case 'a': /* average */
311 greg 1.6 thisillum.flags = (thisillum.flags|IL_COLAVG)
312     & ~IL_COLDST;
313 greg 1.1 break;
314     case 'd': /* distribution */
315 greg 1.5 thisillum.flags |= (IL_COLDST|IL_COLAVG);
316 greg 1.1 break;
317     case 'n': /* none */
318     thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
319     break;
320     default:
321     goto opterr;
322     }
323     cp = sskip(cp);
324     continue;
325     case 'd': /* point sample density */
326 greg 1.2 if (*++cp != '=')
327 greg 1.1 break;
328 greg 1.2 if (!isintd(++cp, " \t\n"))
329 greg 1.1 break;
330 greg 1.3 thisillum.sampdens = atoi(cp);
331 greg 1.1 cp = sskip(cp);
332     continue;
333     case 's': /* point super-samples */
334 greg 1.2 if (*++cp != '=')
335 greg 1.1 break;
336 greg 1.2 if (!isintd(++cp, " \t\n"))
337 greg 1.1 break;
338     thisillum.nsamps = atoi(cp);
339     cp = sskip(cp);
340     continue;
341 greg 1.3 case 'l': /* light sources */
342 greg 1.4 cp++;
343     if (*cp == '+')
344 greg 1.3 thisillum.flags |= IL_LIGHT;
345 greg 1.4 else if (*cp == '-')
346     thisillum.flags &= ~IL_LIGHT;
347 greg 1.3 else
348 greg 1.4 break;
349     cp++;
350 greg 1.3 continue;
351 greg 1.12 case 'b': /* brightness */
352     if (*++cp != '=')
353     break;
354     if (!isfltd(++cp, " \t\n"))
355     break;
356     thisillum.minbrt = atof(cp);
357 greg 1.13 if (thisillum.minbrt < 0.)
358     thisillum.minbrt = 0.;
359 greg 1.12 cp = sskip(cp);
360     continue;
361 greg 1.1 case 'o': /* output file */
362 greg 1.2 if (*++cp != '=')
363 greg 1.1 break;
364 greg 1.2 if (!*++cp)
365     break;
366 greg 1.1 atos(buf, sizeof(buf), cp);
367     cp = sskip(cp);
368     if (freopen(buf, "w", stdout) == NULL) {
369     sprintf(errmsg,
370     "cannot open output file \"%s\"", buf);
371     error(SYSTEM, errmsg);
372     }
373     doneheader = 0;
374     continue;
375 greg 1.6 case '!': /* processed file! */
376 greg 1.7 sprintf(errmsg, "(%s): already processed!", nm);
377 greg 1.6 error(WARNING, errmsg);
378 greg 1.7 matselect = S_NONE;
379 greg 1.6 return;
380 greg 1.1 }
381     opterr: /* skip faulty option */
382     cp = sskip(cp);
383     nerrs++;
384     }
385 greg 1.2 /* print header? */
386     checkhead();
387     /* issue warnings? */
388 greg 1.1 if (nerrs) {
389     sprintf(errmsg, "(%s): %d error(s) in option line:",
390     nm, nerrs);
391     error(WARNING, errmsg);
392 greg 1.2 wputs(s);
393     printf("# %s: the following option line has %d error(s):\n",
394     progname, nerrs);
395 greg 1.1 }
396 greg 1.2 /* print pure comment */
397 greg 1.4 printf("# %s", s+2);
398 greg 1.9 }
399    
400    
401     printopts() /* print out option default values */
402     {
403 greg 1.11 printf("m=%-15s\t\t# material name\n", thisillum.matname);
404     printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
405 greg 2.2 if (thisillum.flags & IL_COLAVG)
406     if (thisillum.flags & IL_COLDST)
407     printf("c=d\t\t\t\t# color distribution\n");
408     else
409     printf("c=a\t\t\t\t# color average\n");
410     else
411     printf("c=n\t\t\t\t# color none\n");
412     if (thisillum.flags & IL_LIGHT)
413     printf("l+\t\t\t\t# light type on\n");
414     else
415     printf("l-\t\t\t\t# light type off\n");
416 greg 1.9 printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
417     printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
418 greg 1.12 printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
419 greg 1.1 }
420 greg 1.2
421    
422     printhead(ac, av) /* print out header */
423     register int ac;
424     register char **av;
425     {
426     putchar('#');
427     while (ac-- > 0) {
428     putchar(' ');
429     fputs(*av++, stdout);
430     }
431 greg 1.6 fputs("\n#@mkillum !\n", stdout);
432 greg 1.2 }
433    
434    
435     xobject(fp, nm) /* translate an object from fp */
436     FILE *fp;
437     char *nm;
438     {
439     OBJREC thisobj;
440     char str[MAXSTR];
441     int doit;
442     /* read the object */
443     if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
444     goto readerr;
445     if (fgetword(str, MAXSTR, fp) == NULL)
446     goto readerr;
447     /* is it an alias? */
448     if (!strcmp(str, ALIASID)) {
449     if (fgetword(str, MAXSTR, fp) == NULL)
450     goto readerr;
451     printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
452     if (fgetword(str, MAXSTR, fp) == NULL)
453     goto readerr;
454 greg 1.4 printf("\t%s\n", str);
455 greg 1.2 return;
456     }
457 greg 1.4 thisobj.omod = OVOID; /* unused field */
458 greg 1.2 if ((thisobj.otype = otype(str)) < 0) {
459     sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
460     error(USER, errmsg);
461     }
462     if (fgetword(str, MAXSTR, fp) == NULL)
463     goto readerr;
464     thisobj.oname = str;
465     if (readfargs(&thisobj.oargs, fp) != 1)
466     goto readerr;
467     thisobj.os = NULL;
468     /* check for translation */
469     switch (matselect) {
470     case S_NONE:
471     doit = 0;
472     break;
473     case S_ALL:
474 greg 1.4 doit = 1;
475 greg 1.2 break;
476     case S_ELEM:
477     doit = !strcmp(thisillum.altmat, matcheck);
478     break;
479     case S_COMPL:
480     doit = strcmp(thisillum.altmat, matcheck);
481     break;
482     }
483 greg 1.4 doit = doit && issurface(thisobj.otype);
484 greg 1.2 /* print header? */
485     checkhead();
486     /* process object */
487     if (doit)
488 greg 1.4 (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
489 greg 1.2 else
490     printobj(thisillum.altmat, &thisobj);
491     /* free arguments */
492     freefargs(&thisobj.oargs);
493     return;
494     readerr:
495     sprintf(errmsg, "(%s): error reading scene", nm);
496     error(USER, errmsg);
497     }