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, 9 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

# Content
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 #include "mkillum.h"
12
13 #include <signal.h>
14
15 #include <ctype.h>
16
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 struct rtproc rt; /* our rtrace process */
35
36 struct illum_args thisillum = { /* our illum and default values */
37 0,
38 DFLMAT,
39 DFLMAT,
40 0,
41 VOIDID,
42 SAMPDENS,
43 NSAMPS,
44 };
45
46 char matcheck[MAXSTR]; /* current material to include or exclude */
47 int matselect = S_ALL; /* selection criterion */
48
49 FUN ofun[NUMOTYPE] = INIT_OTYPE; /* object types */
50
51 int gargc; /* global argc */
52 char **gargv; /* global argv */
53 #define progname gargv[0]
54
55 int doneheader = 0; /* printed header yet? */
56 #define checkhead() if (!doneheader++) printhead(gargc,gargv)
57
58 int warnings = 1; /* print warnings? */
59
60 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 FILE *fp;
72 register int i;
73 /* set global arguments */
74 gargv = argv;
75 /* set up rtrace command */
76 for (i = 1; i < argc; i++) {
77 if (argv[i][0] == '<' && !argv[i][1])
78 break;
79 rtargv[rtargc++] = argv[i];
80 if (argv[i][0] == '-' && argv[i][1] == 'w')
81 warnings = !warnings;
82 }
83 if ((gargc = i) < 2)
84 error(USER, "too few arguments");
85 rtargc--;
86 for (i = 0; myrtopts[i] != NULL; i++)
87 rtargv[rtargc++] = myrtopts[i];
88 rtargv[rtargc++] = argv[gargc-1];
89 rtargv[rtargc] = NULL;
90 /* just asking for defaults? */
91 if (!strcmp(argv[gargc-1], "-defaults")) {
92 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 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 quit(0);
117 }
118
119
120 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 init() /* start rtrace and set up buffers */
137 {
138 extern int o_face(), o_ring();
139 int maxbytes;
140 /* 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 maxbytes = open_process(rt.pd, rtargv);
147 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 rt.bsiz = maxbytes/(6*sizeof(float));
155 rt.buf = (float *)malloc(rt.bsiz*(6*sizeof(float)));
156 if (rt.buf == NULL)
157 error(SYSTEM, "out of memory in init");
158 rt.bsiz--; /* allow for flush ray */
159 rt.nrays = 0;
160 }
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 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 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 xoptions(buf, name);
201 } 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 filter(pfp, buf);
209 pclose(pfp);
210 } else { /* object */
211 ungetc(c, infp);
212 xobject(infp, name);
213 }
214 }
215 }
216
217
218 xoptions(s, nm) /* process options in string s */
219 char *s;
220 char *nm;
221 {
222 extern FILE *freopen();
223 char buf[64];
224 int nerrs = 0;
225 register char *cp;
226
227 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
228 fputs(s, stdout); /* not for us */
229 return;
230 }
231 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 if (*++cp != '=')
241 break;
242 if (!*++cp)
243 break;
244 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 if (*++cp != '=')
253 break;
254 if (!*++cp) {
255 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 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
266 if (*++cp != '=')
267 break;
268 atos(matcheck, MAXSTR, ++cp);
269 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 if (*++cp != '=')
281 break;
282 switch (*++cp) {
283 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 if (*++cp != '=')
301 break;
302 if (!isintd(++cp, " \t\n"))
303 break;
304 thisillum.sampdens = atoi(cp);
305 cp = sskip(cp);
306 continue;
307 case 's': /* point super-samples */
308 if (*++cp != '=')
309 break;
310 if (!isintd(++cp, " \t\n"))
311 break;
312 thisillum.nsamps = atoi(cp);
313 cp = sskip(cp);
314 continue;
315 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 case 'o': /* output file */
324 if (*++cp != '=')
325 break;
326 if (!*++cp)
327 break;
328 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 /* print header? */
343 checkhead();
344 /* issue warnings? */
345 if (nerrs) {
346 sprintf(errmsg, "(%s): %d error(s) in option line:",
347 nm, nerrs);
348 error(WARNING, errmsg);
349 wputs(s);
350 printf("# %s: the following option line has %d error(s):\n",
351 progname, nerrs);
352 }
353 /* print pure comment */
354 putchar('#'); putchar(' '); fputs(s+2, stdout);
355 }
356
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 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 /* print header? */
426 checkhead();
427 /* process object */
428 if (doit)
429 (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt);
430 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() {}