ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 1.11
Committed: Tue Aug 13 14:43:49 1991 UTC (32 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +2 -2 lines
Log Message:
fixed format of defaults printing

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