ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
Revision: 2.12
Committed: Tue Mar 11 19:29:05 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.11: +3 -3 lines
Log Message:
Changed alias handling to allow tracking, fixed freeobjects() and do_irrad bugs

File Contents

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