ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 1.5
Committed: Wed Jul 24 16:48:22 1991 UTC (32 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +2 -3 lines
Log Message:
first version of mkillum

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", "-di+",
29 "-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", };
30 int rtargc = 14;
31 /* overriding rtrace options */
32 char *myrtopts[] = { "-I-", "-i-", "-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_sphere(), o_ring();
139 int maxbytes;
140 /* set up object functions */
141 ofun[OBJ_FACE].funp = o_face;
142 ofun[OBJ_SPHERE].funp = o_sphere;
143 ofun[OBJ_RING].funp = o_ring;
144 /* set up signal handling */
145 signal(SIGPIPE, quit);
146 /* start rtrace process */
147 maxbytes = open_process(rt.pd, rtargv);
148 if (maxbytes == 0) {
149 eputs(rtargv[0]);
150 eputs(": command not found\n");
151 exit(1);
152 }
153 if (maxbytes < 0)
154 error(SYSTEM, "cannot start rtrace process");
155 rt.bsiz = maxbytes/(6*sizeof(float));
156 rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
157 rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
158 if (rt.buf == NULL || rt.dest == NULL)
159 error(SYSTEM, "out of memory in init");
160 rt.nrays = 0;
161 }
162
163
164 eputs(s) /* put string to stderr */
165 register char *s;
166 {
167 static int midline = 0;
168
169 if (!*s) return;
170 if (!midline) {
171 fputs(progname, stderr);
172 fputs(": ", stderr);
173 }
174 fputs(s, stderr);
175 midline = s[strlen(s)-1] != '\n';
176 }
177
178
179 wputs(s) /* print warning if enabled */
180 char *s;
181 {
182 if (warnings)
183 eputs(s);
184 }
185
186
187 filter(infp, name) /* process stream */
188 register FILE *infp;
189 char *name;
190 {
191 char buf[512];
192 FILE *pfp;
193 register int c;
194
195 while ((c = getc(infp)) != EOF) {
196 if (isspace(c))
197 continue;
198 if (c == '#') { /* comment/options */
199 buf[0] = c;
200 fgets(buf+1, sizeof(buf)-1, infp);
201 xoptions(buf, name);
202 } else if (c == '!') { /* command */
203 buf[0] = c;
204 fgetline(buf+1, sizeof(buf)-1, infp);
205 if ((pfp = popen(buf+1, "r")) == NULL) {
206 sprintf(errmsg, "cannot execute \"%s\"", buf);
207 error(SYSTEM, errmsg);
208 }
209 filter(pfp, buf);
210 pclose(pfp);
211 } else { /* object */
212 ungetc(c, infp);
213 xobject(infp, name);
214 }
215 }
216 }
217
218
219 xoptions(s, nm) /* process options in string s */
220 char *s;
221 char *nm;
222 {
223 extern FILE *freopen();
224 char buf[64];
225 int nerrs = 0;
226 register char *cp;
227
228 if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
229 fputs(s, stdout); /* not for us */
230 return;
231 }
232 cp = s+10;
233 while (*cp) {
234 switch (*cp) {
235 case ' ':
236 case '\t':
237 case '\n':
238 cp++;
239 continue;
240 case 'm': /* material name */
241 if (*++cp != '=')
242 break;
243 if (!*++cp)
244 break;
245 atos(thisillum.matname, MAXSTR, cp);
246 cp = sskip(cp);
247 if (!(thisillum.flags & IL_DATCLB)) {
248 strcpy(thisillum.datafile, thisillum.matname);
249 thisillum.dfnum = 0;
250 }
251 continue;
252 case 'f': /* data file name */
253 if (*++cp != '=')
254 break;
255 if (!*++cp) {
256 thisillum.flags &= ~IL_DATCLB;
257 continue;
258 }
259 atos(thisillum.datafile, MAXSTR, cp);
260 cp = sskip(cp);
261 thisillum.dfnum = 0;
262 thisillum.flags |= IL_DATCLB;
263 continue;
264 case 'i': /* include material */
265 case 'e': /* exclude material */
266 matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
267 if (*++cp != '=')
268 break;
269 atos(matcheck, MAXSTR, ++cp);
270 cp = sskip(cp);
271 continue;
272 case 'a': /* use everything */
273 cp = sskip(cp);
274 matselect = S_ALL;
275 continue;
276 case 'n': /* use nothing (passive) */
277 cp = sskip(cp);
278 matselect = S_NONE;
279 continue;
280 case 'c': /* color calculation */
281 if (*++cp != '=')
282 break;
283 switch (*++cp) {
284 case 'a': /* average */
285 thisillum.flags |= IL_COLAVG;
286 thisillum.flags &= ~IL_COLDST;
287 break;
288 case 'd': /* distribution */
289 thisillum.flags |= (IL_COLDST|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 cp++;
317 if (*cp == '+')
318 thisillum.flags |= IL_LIGHT;
319 else if (*cp == '-')
320 thisillum.flags &= ~IL_LIGHT;
321 else
322 break;
323 cp++;
324 continue;
325 case 'o': /* output file */
326 if (*++cp != '=')
327 break;
328 if (!*++cp)
329 break;
330 atos(buf, sizeof(buf), cp);
331 cp = sskip(cp);
332 if (freopen(buf, "w", stdout) == NULL) {
333 sprintf(errmsg,
334 "cannot open output file \"%s\"", buf);
335 error(SYSTEM, errmsg);
336 }
337 doneheader = 0;
338 continue;
339 }
340 opterr: /* skip faulty option */
341 cp = sskip(cp);
342 nerrs++;
343 }
344 /* print header? */
345 checkhead();
346 /* issue warnings? */
347 if (nerrs) {
348 sprintf(errmsg, "(%s): %d error(s) in option line:",
349 nm, nerrs);
350 error(WARNING, errmsg);
351 wputs(s);
352 printf("# %s: the following option line has %d error(s):\n",
353 progname, nerrs);
354 }
355 /* print pure comment */
356 printf("# %s", s+2);
357 }
358
359
360 printhead(ac, av) /* print out header */
361 register int ac;
362 register char **av;
363 {
364 putchar('#');
365 while (ac-- > 0) {
366 putchar(' ');
367 fputs(*av++, stdout);
368 }
369 putchar('\n');
370 }
371
372
373 xobject(fp, nm) /* translate an object from fp */
374 FILE *fp;
375 char *nm;
376 {
377 OBJREC thisobj;
378 char str[MAXSTR];
379 int doit;
380 /* read the object */
381 if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
382 goto readerr;
383 if (fgetword(str, MAXSTR, fp) == NULL)
384 goto readerr;
385 /* is it an alias? */
386 if (!strcmp(str, ALIASID)) {
387 if (fgetword(str, MAXSTR, fp) == NULL)
388 goto readerr;
389 printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
390 if (fgetword(str, MAXSTR, fp) == NULL)
391 goto readerr;
392 printf("\t%s\n", str);
393 return;
394 }
395 thisobj.omod = OVOID; /* unused field */
396 if ((thisobj.otype = otype(str)) < 0) {
397 sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
398 error(USER, errmsg);
399 }
400 if (fgetword(str, MAXSTR, fp) == NULL)
401 goto readerr;
402 thisobj.oname = str;
403 if (readfargs(&thisobj.oargs, fp) != 1)
404 goto readerr;
405 thisobj.os = NULL;
406 /* check for translation */
407 switch (matselect) {
408 case S_NONE:
409 doit = 0;
410 break;
411 case S_ALL:
412 doit = 1;
413 break;
414 case S_ELEM:
415 doit = !strcmp(thisillum.altmat, matcheck);
416 break;
417 case S_COMPL:
418 doit = strcmp(thisillum.altmat, matcheck);
419 break;
420 }
421 doit = doit && issurface(thisobj.otype);
422 /* print header? */
423 checkhead();
424 /* process object */
425 if (doit)
426 (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
427 else
428 printobj(thisillum.altmat, &thisobj);
429 /* free arguments */
430 freefargs(&thisobj.oargs);
431 return;
432 readerr:
433 sprintf(errmsg, "(%s): error reading scene", nm);
434 error(USER, errmsg);
435 }