ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum.c
(Generate patch)

Comparing ray/src/gen/mkillum.c (file contents):
Revision 1.1 by greg, Tue Jul 23 12:00:29 1991 UTC vs.
Revision 2.14 by schorsch, Thu Jun 26 00:58:09 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines