ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 1.12
Committed: Fri Aug 23 12:30:29 1991 UTC (32 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +10 -0 lines
Log Message:
added b variable for minimum brightness

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 1.4 char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-di+",
30     "-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", };
31     int rtargc = 14;
32 greg 1.1 /* overriding rtrace options */
33 greg 1.4 char *myrtopts[] = { "-I-", "-i-", "-ov", "-h-", "-fff", 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     if (argv[i][0] == '<' && !argv[i][1])
80     break;
81 greg 1.1 rtargv[rtargc++] = argv[i];
82 greg 1.2 if (argv[i][0] == '-' && argv[i][1] == 'w')
83     warnings = !warnings;
84     }
85 greg 1.3 if ((gargc = i) < 2)
86     error(USER, "too few arguments");
87     rtargc--;
88 greg 1.1 for (i = 0; myrtopts[i] != NULL; i++)
89     rtargv[rtargc++] = myrtopts[i];
90 greg 1.3 rtargv[rtargc++] = argv[gargc-1];
91 greg 1.1 rtargv[rtargc] = NULL;
92     /* just asking for defaults? */
93 greg 1.3 if (!strcmp(argv[gargc-1], "-defaults")) {
94 greg 1.9 printopts(); fflush(stdout);
95 greg 1.1 rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
96     if (rtpath == NULL) {
97     eputs(rtargv[0]);
98     eputs(": command not found\n");
99     exit(1);
100     }
101     execv(rtpath, rtargv);
102     perror(rtpath);
103     exit(1);
104     }
105     /* else initialize and run our calculation */
106     init();
107 greg 1.3 if (gargc+1 < argc)
108     for (i = gargc+1; i < argc; i++) {
109 greg 1.5 if ((fp = fopen(argv[i], "r")) == NULL) {
110 greg 1.3 sprintf(errmsg,
111     "cannot open scene file \"%s\"", argv[i]);
112     error(SYSTEM, errmsg);
113     }
114     filter(fp, argv[i]);
115     fclose(fp);
116     }
117     else
118     filter(stdin, "standard input");
119 greg 1.2 quit(0);
120 greg 1.1 }
121    
122    
123 greg 1.3 quit(status) /* exit with status */
124     int status;
125     {
126     int rtstat;
127    
128     rtstat = close_process(rt.pd);
129     if (status == 0)
130     if (rtstat < 0)
131     error(WARNING,
132     "unknown return status from rtrace process");
133     else
134     status = rtstat;
135     exit(status);
136     }
137    
138    
139 greg 1.1 init() /* start rtrace and set up buffers */
140     {
141 greg 1.4 extern int o_face(), o_sphere(), o_ring();
142 greg 1.1 int maxbytes;
143 greg 1.3 /* set up object functions */
144     ofun[OBJ_FACE].funp = o_face;
145 greg 1.4 ofun[OBJ_SPHERE].funp = o_sphere;
146 greg 1.3 ofun[OBJ_RING].funp = o_ring;
147     /* set up signal handling */
148     signal(SIGPIPE, quit);
149     /* start rtrace process */
150 greg 1.2 maxbytes = open_process(rt.pd, rtargv);
151 greg 1.1 if (maxbytes == 0) {
152     eputs(rtargv[0]);
153     eputs(": command not found\n");
154     exit(1);
155     }
156     if (maxbytes < 0)
157     error(SYSTEM, "cannot start rtrace process");
158 greg 1.2 rt.bsiz = maxbytes/(6*sizeof(float));
159 greg 1.4 rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
160     rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
161     if (rt.buf == NULL || rt.dest == NULL)
162 greg 1.1 error(SYSTEM, "out of memory in init");
163 greg 1.2 rt.nrays = 0;
164 greg 1.6 /* set up urand */
165     initurand(2048);
166 greg 1.1 }
167    
168    
169     eputs(s) /* put string to stderr */
170     register char *s;
171     {
172     static int midline = 0;
173    
174     if (!*s) return;
175     if (!midline) {
176     fputs(progname, stderr);
177     fputs(": ", stderr);
178     }
179     fputs(s, stderr);
180     midline = s[strlen(s)-1] != '\n';
181     }
182    
183    
184 greg 1.2 wputs(s) /* print warning if enabled */
185     char *s;
186     {
187     if (warnings)
188     eputs(s);
189     }
190    
191    
192     filter(infp, name) /* process stream */
193 greg 1.1 register FILE *infp;
194     char *name;
195     {
196     char buf[512];
197     FILE *pfp;
198     register int c;
199    
200     while ((c = getc(infp)) != EOF) {
201     if (isspace(c))
202     continue;
203     if (c == '#') { /* comment/options */
204     buf[0] = c;
205     fgets(buf+1, sizeof(buf)-1, infp);
206 greg 1.2 xoptions(buf, name);
207 greg 1.1 } else if (c == '!') { /* command */
208     buf[0] = c;
209     fgetline(buf+1, sizeof(buf)-1, infp);
210     if ((pfp = popen(buf+1, "r")) == NULL) {
211     sprintf(errmsg, "cannot execute \"%s\"", buf);
212     error(SYSTEM, errmsg);
213     }
214 greg 1.2 filter(pfp, buf);
215 greg 1.1 pclose(pfp);
216     } else { /* object */
217     ungetc(c, infp);
218     xobject(infp, name);
219     }
220     }
221     }
222    
223    
224 greg 1.2 xoptions(s, nm) /* process options in string s */
225 greg 1.1 char *s;
226     char *nm;
227     {
228     extern FILE *freopen();
229     char buf[64];
230     int nerrs = 0;
231     register char *cp;
232    
233 greg 1.2 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
234     fputs(s, stdout); /* not for us */
235 greg 1.1 return;
236 greg 1.2 }
237 greg 1.1 cp = s+10;
238     while (*cp) {
239     switch (*cp) {
240     case ' ':
241     case '\t':
242     case '\n':
243     cp++;
244     continue;
245     case 'm': /* material name */
246 greg 1.2 if (*++cp != '=')
247 greg 1.1 break;
248 greg 1.2 if (!*++cp)
249     break;
250 greg 1.1 atos(thisillum.matname, MAXSTR, cp);
251     cp = sskip(cp);
252     if (!(thisillum.flags & IL_DATCLB)) {
253     strcpy(thisillum.datafile, thisillum.matname);
254     thisillum.dfnum = 0;
255     }
256     continue;
257     case 'f': /* data file name */
258 greg 1.2 if (*++cp != '=')
259 greg 1.1 break;
260 greg 1.2 if (!*++cp) {
261 greg 1.6 strcpy(thisillum.datafile,thisillum.matname);
262 greg 1.10 thisillum.dfnum = 0;
263 greg 1.1 thisillum.flags &= ~IL_DATCLB;
264     continue;
265     }
266     atos(thisillum.datafile, MAXSTR, cp);
267     cp = sskip(cp);
268     thisillum.dfnum = 0;
269     thisillum.flags |= IL_DATCLB;
270     continue;
271     case 'i': /* include material */
272     case 'e': /* exclude material */
273 greg 1.6 if (cp[1] != '=')
274 greg 1.1 break;
275 greg 1.7 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
276     cp += 2;
277     atos(matcheck, MAXSTR, cp);
278 greg 1.1 cp = sskip(cp);
279     continue;
280     case 'a': /* use everything */
281     cp = sskip(cp);
282     matselect = S_ALL;
283     continue;
284     case 'n': /* use nothing (passive) */
285     cp = sskip(cp);
286     matselect = S_NONE;
287     continue;
288     case 'c': /* color calculation */
289 greg 1.2 if (*++cp != '=')
290 greg 1.1 break;
291 greg 1.2 switch (*++cp) {
292 greg 1.1 case 'a': /* average */
293 greg 1.6 thisillum.flags = (thisillum.flags|IL_COLAVG)
294     & ~IL_COLDST;
295 greg 1.1 break;
296     case 'd': /* distribution */
297 greg 1.5 thisillum.flags |= (IL_COLDST|IL_COLAVG);
298 greg 1.1 break;
299     case 'n': /* none */
300     thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
301     break;
302     default:
303     goto opterr;
304     }
305     cp = sskip(cp);
306     continue;
307     case 'd': /* point sample density */
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 greg 1.3 thisillum.sampdens = atoi(cp);
313 greg 1.1 cp = sskip(cp);
314     continue;
315     case 's': /* point super-samples */
316 greg 1.2 if (*++cp != '=')
317 greg 1.1 break;
318 greg 1.2 if (!isintd(++cp, " \t\n"))
319 greg 1.1 break;
320     thisillum.nsamps = atoi(cp);
321     cp = sskip(cp);
322     continue;
323 greg 1.3 case 'l': /* light sources */
324 greg 1.4 cp++;
325     if (*cp == '+')
326 greg 1.3 thisillum.flags |= IL_LIGHT;
327 greg 1.4 else if (*cp == '-')
328     thisillum.flags &= ~IL_LIGHT;
329 greg 1.3 else
330 greg 1.4 break;
331     cp++;
332 greg 1.3 continue;
333 greg 1.12 case 'b': /* brightness */
334     if (*++cp != '=')
335     break;
336     if (!isfltd(++cp, " \t\n"))
337     break;
338     thisillum.minbrt = atof(cp);
339     cp = sskip(cp);
340     continue;
341 greg 1.1 case 'o': /* output file */
342 greg 1.2 if (*++cp != '=')
343 greg 1.1 break;
344 greg 1.2 if (!*++cp)
345     break;
346 greg 1.1 atos(buf, sizeof(buf), cp);
347     cp = sskip(cp);
348     if (freopen(buf, "w", stdout) == NULL) {
349     sprintf(errmsg,
350     "cannot open output file \"%s\"", buf);
351     error(SYSTEM, errmsg);
352     }
353     doneheader = 0;
354     continue;
355 greg 1.6 case '!': /* processed file! */
356 greg 1.7 sprintf(errmsg, "(%s): already processed!", nm);
357 greg 1.6 error(WARNING, errmsg);
358 greg 1.7 matselect = S_NONE;
359 greg 1.6 return;
360 greg 1.1 }
361     opterr: /* skip faulty option */
362     cp = sskip(cp);
363     nerrs++;
364     }
365 greg 1.2 /* print header? */
366     checkhead();
367     /* issue warnings? */
368 greg 1.1 if (nerrs) {
369     sprintf(errmsg, "(%s): %d error(s) in option line:",
370     nm, nerrs);
371     error(WARNING, errmsg);
372 greg 1.2 wputs(s);
373     printf("# %s: the following option line has %d error(s):\n",
374     progname, nerrs);
375 greg 1.1 }
376 greg 1.2 /* print pure comment */
377 greg 1.4 printf("# %s", s+2);
378 greg 1.9 }
379    
380    
381     printopts() /* print out option default values */
382     {
383 greg 1.11 printf("m=%-15s\t\t# material name\n", thisillum.matname);
384     printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
385 greg 1.9 printf("c=n\t\t\t\t# color none\n");
386     printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
387     printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
388 greg 1.12 printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
389 greg 1.1 }
390 greg 1.2
391    
392     printhead(ac, av) /* print out header */
393     register int ac;
394     register char **av;
395     {
396     putchar('#');
397     while (ac-- > 0) {
398     putchar(' ');
399     fputs(*av++, stdout);
400     }
401 greg 1.6 fputs("\n#@mkillum !\n", stdout);
402 greg 1.2 }
403    
404    
405     xobject(fp, nm) /* translate an object from fp */
406     FILE *fp;
407     char *nm;
408     {
409     OBJREC thisobj;
410     char str[MAXSTR];
411     int doit;
412     /* read the object */
413     if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
414     goto readerr;
415     if (fgetword(str, MAXSTR, fp) == NULL)
416     goto readerr;
417     /* is it an alias? */
418     if (!strcmp(str, ALIASID)) {
419     if (fgetword(str, MAXSTR, fp) == NULL)
420     goto readerr;
421     printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
422     if (fgetword(str, MAXSTR, fp) == NULL)
423     goto readerr;
424 greg 1.4 printf("\t%s\n", str);
425 greg 1.2 return;
426     }
427 greg 1.4 thisobj.omod = OVOID; /* unused field */
428 greg 1.2 if ((thisobj.otype = otype(str)) < 0) {
429     sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
430     error(USER, errmsg);
431     }
432     if (fgetword(str, MAXSTR, fp) == NULL)
433     goto readerr;
434     thisobj.oname = str;
435     if (readfargs(&thisobj.oargs, fp) != 1)
436     goto readerr;
437     thisobj.os = NULL;
438     /* check for translation */
439     switch (matselect) {
440     case S_NONE:
441     doit = 0;
442     break;
443     case S_ALL:
444 greg 1.4 doit = 1;
445 greg 1.2 break;
446     case S_ELEM:
447     doit = !strcmp(thisillum.altmat, matcheck);
448     break;
449     case S_COMPL:
450     doit = strcmp(thisillum.altmat, matcheck);
451     break;
452     }
453 greg 1.4 doit = doit && issurface(thisobj.otype);
454 greg 1.2 /* print header? */
455     checkhead();
456     /* process object */
457     if (doit)
458 greg 1.4 (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
459 greg 1.2 else
460     printobj(thisillum.altmat, &thisobj);
461     /* free arguments */
462     freefargs(&thisobj.oargs);
463     return;
464     readerr:
465     sprintf(errmsg, "(%s): error reading scene", nm);
466     error(USER, errmsg);
467     }