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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines