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.2 by greg, Tue Jul 23 16:13:28 1991 UTC vs.
Revision 2.30 by greg, Tue Sep 18 19:51:07 2007 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  "mkillum.h"
12 <
8 > #include  <signal.h>
9   #include  <ctype.h>
10  
11 + #include  "platform.h"
12 + #include  "mkillum.h"
13 + #include  "random.h"
14 +
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 */
23   #define  S_COMPL        2               /* select all but element */
24   #define  S_ALL          3               /* select all */
25  
25                                /* rtrace command and defaults */
26 char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-ab", "2",
27                "-ad", "256", "-as", "128", "-aa", ".15" };
28 int  rtargc = 13;
29                                /* overriding rtrace options */
30 char  *myrtopts[] = { "-I-", "-i-", "-di+", "-ov", "-h-", "-fff", NULL };
31
32 struct rtproc   rt;             /* our rtrace process */
33
26   struct illum_args  thisillum = {        /* our illum and default values */
27                  0,
28 +                UDzpos,
29                  DFLMAT,
30 <                DFLMAT,
30 >                DFLDAT,
31                  0,
32                  VOIDID,
33 +                SAMPDENS,
34                  NSAMPS,
35 +                NULL,
36 +                0.,
37          };
38  
43 int     sampdens = SAMPDENS;    /* sample point density */
39   char    matcheck[MAXSTR];       /* current material to include or exclude */
40   int     matselect = S_ALL;      /* selection criterion */
41  
47 FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
48
42   int     gargc;                  /* global argc */
43   char    **gargv;                /* global argv */
51 #define  progname       gargv[0]
44  
45   int     doneheader = 0;         /* printed header yet? */
46   #define  checkhead()    if (!doneheader++) printhead(gargc,gargv)
47  
48   int     warnings = 1;           /* print warnings? */
49  
50 < extern char     *fgetline(), *fgetword(), *sskip(),
51 <                *atos(), *iskip(), *fskip();
52 < extern FILE     *popen();
50 > void init(char *octnm, int np);
51 > void filter(register FILE       *infp, char     *name);
52 > void xoptions(char      *s, char        *nm);
53 > void printopts(void);
54 > void printhead(register int  ac, register char  **av);
55 > void xobject(FILE  *fp, char  *nm);
56  
57  
58 < main(argc, argv)                /* compute illum distributions using rtrace */
59 < int     argc;
60 < char    *argv[];
58 > int
59 > main(           /* compute illum distributions using rtrace */
60 >        int     argc,
61 >        char    *argv[]
62 > )
63   {
64 <        extern char     *getenv(), *getpath();
65 <        char    *rtpath;
64 >        int     nprocs = 1;
65 >        FILE    *fp;
66 >        int     rval;
67          register int    i;
68                                  /* set global arguments */
69 <        gargc = argc; gargv = argv;
70 <                                /* set up rtrace command */
71 <        if (argc < 2)
72 <                error(USER, "too few arguments");
73 <        for (i = 1; i < argc-1; i++) {
74 <                rtargv[rtargc++] = argv[i];
75 <                if (argv[i][0] == '-' && argv[i][1] == 'w')
76 <                        warnings = !warnings;
77 <        }
78 <        for (i = 0; myrtopts[i] != NULL; i++)
79 <                rtargv[rtargc++] = myrtopts[i];
80 <        rtargv[rtargc++] = argv[argc-1];
81 <        rtargv[rtargc] = NULL;
82 <                                /* just asking for defaults? */
85 <        if (!strcmp(argv[argc-1], "-defaults")) {
86 <                rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
87 <                if (rtpath == NULL) {
88 <                        eputs(rtargv[0]);
89 <                        eputs(": command not found\n");
90 <                        exit(1);
69 >        gargv = argv;
70 >        progname = gargv[0];
71 >                                /* set up rendering defaults */
72 >        dstrsrc = 0.25;
73 >        directrelay = 3;
74 >        directvis = 0;
75 >        ambounce = 2;
76 >                                /* get options from command line */
77 >        for (i = 1; i < argc; i++) {
78 >                while ((rval = expandarg(&argc, &argv, i)) > 0)
79 >                        ;
80 >                if (rval < 0) {
81 >                        sprintf(errmsg, "cannot expand '%s'", argv[i]);
82 >                        error(SYSTEM, errmsg);
83                  }
84 <                execv(rtpath, rtargv);
85 <                perror(rtpath);
86 <                exit(1);
84 >                if (argv[i][0] != '-')
85 >                        break;
86 >                if (!strcmp(argv[i], "-w")) {
87 >                        warnings = 0;
88 >                        continue;
89 >                }
90 >                if (!strcmp(argv[i], "-n")) {
91 >                        nprocs = atoi(argv[++i]);
92 >                        if (nprocs <= 0)
93 >                                error(USER, "illegal number of processes");
94 >                        continue;
95 >                }
96 >                if (!strcmp(argv[i], "-defaults")) {
97 >                        printopts();
98 >                        print_rdefaults();
99 >                        quit(0);
100 >                }
101 >                rval = getrenderopt(argc-i, argv+i);
102 >                if (rval < 0) {
103 >                        sprintf(errmsg, "bad render option at '%s'", argv[i]);
104 >                        error(USER, errmsg);
105 >                }
106 >                i += rval;
107          }
108 +        gargc = ++i;
109 +                                /* add "mandatory" render options */
110 +        do_irrad = 0;
111 +        if (gargc > argc || argv[gargc-1][0] == '-')
112 +                error(USER, "missing octree argument");
113                                  /* else initialize and run our calculation */
114 <        init();
115 <        filter(stdin, "standard input");
114 >        init(argv[gargc-1], nprocs);
115 >        if (gargc < argc) {
116 >                if (gargc == argc-1 || argv[gargc][0] != '<' || argv[gargc][1])
117 >                        error(USER, "use '< file1 file2 ..' for multiple inputs");
118 >                for (i = gargc+1; i < argc; i++) {
119 >                        if ((fp = fopen(argv[i], "r")) == NULL) {
120 >                                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          quit(0);
130 +        return 0; /* pro forma return */
131   }
132  
133  
134 < init()                          /* start rtrace and set up buffers */
134 > void
135 > init(char *octnm, int np)               /* start rendering process(es) */
136   {
137 <        int     maxbytes;
138 <
139 <        maxbytes = open_process(rt.pd, rtargv);
140 <        if (maxbytes == 0) {
141 <                eputs(rtargv[0]);
142 <                eputs(": command not found\n");
143 <                exit(1);
144 <        }
145 <        if (maxbytes < 0)
146 <                error(SYSTEM, "cannot start rtrace process");
147 <        rt.bsiz = maxbytes/(6*sizeof(float));
148 <        rt.buf = (float *)malloc(rt.bsiz*(6*sizeof(float)));
149 <        if (rt.buf == NULL)
118 <                error(SYSTEM, "out of memory in init");
119 <        rt.bsiz--;                      /* allow for flush ray */
120 <        rt.nrays = 0;
137 >                                        /* set up signal handling */
138 >        signal(SIGINT, quit);
139 > #ifdef SIGHUP
140 >        signal(SIGHUP, quit);
141 > #endif
142 > #ifdef SIGTERM
143 >        signal(SIGTERM, quit);
144 > #endif
145 > #ifdef SIGPIPE
146 >        signal(SIGPIPE, quit);
147 > #endif
148 >                                        /* start rendering process(es) */
149 >        ray_pinit(octnm, np);
150   }
151  
152  
153 < quit(status)                    /* exit with status */
154 < int  status;
153 > void
154 > eputs(                          /* put string to stderr */
155 >        register char  *s
156 > )
157   {
127        int     rtstat;
128
129        rtstat = close_process(rt.pd);
130        if (status == 0)
131                if (rtstat < 0)
132                        error(WARNING,
133                        "unknown return status from rtrace process");
134                else
135                        status = rtstat;
136        exit(status);
137 }
138
139
140 eputs(s)                                /* put string to stderr */
141 register char  *s;
142 {
158          static int  midline = 0;
159  
160          if (!*s) return;
# Line 152 | Line 167 | register char  *s;
167   }
168  
169  
170 + void
171   wputs(s)                        /* print warning if enabled */
172   char  *s;
173   {
# Line 160 | Line 176 | char  *s;
176   }
177  
178  
179 < filter(infp, name)              /* process stream */
180 < register FILE   *infp;
181 < char    *name;
179 > void
180 > filter(         /* process stream */
181 >        register FILE   *infp,
182 >        char    *name
183 > )
184   {
185          char    buf[512];
186          FILE    *pfp;
# Line 192 | Line 210 | char   *name;
210   }
211  
212  
213 < xoptions(s, nm)                 /* process options in string s */
214 < char    *s;
215 < char    *nm;
213 > void
214 > xoptions(                       /* process options in string s */
215 >        char    *s,
216 >        char    *nm
217 > )
218   {
219          extern FILE     *freopen();
220          char    buf[64];
221 +        int     negax;
222          int     nerrs = 0;
223          register char   *cp;
224  
# Line 211 | Line 232 | char   *nm;
232                  case ' ':
233                  case '\t':
234                  case '\n':
235 +                case '\r':
236 +                case '\f':
237                          cp++;
238                          continue;
239                  case 'm':                       /* material name */
240                          if (*++cp != '=')
241                                  break;
242 <                        if (!*++cp)
242 >                        if (!*++cp || isspace(*cp))
243                                  break;
244                          atos(thisillum.matname, MAXSTR, cp);
245                          cp = sskip(cp);
# Line 228 | Line 251 | char   *nm;
251                  case 'f':                       /* data file name */
252                          if (*++cp != '=')
253                                  break;
254 <                        if (!*++cp) {
254 >                        if (!*++cp || isspace(*cp)) {
255 >                                strcpy(thisillum.datafile,thisillum.matname);
256 >                                thisillum.dfnum = 0;
257                                  thisillum.flags &= ~IL_DATCLB;
258                                  continue;
259                          }
# Line 239 | Line 264 | char   *nm;
264                          continue;
265                  case 'i':                       /* include material */
266                  case 'e':                       /* exclude material */
267 <                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
243 <                        if (*++cp != '=')
267 >                        if (cp[1] != '=')
268                                  break;
269 <                        atos(matcheck, MAXSTR, ++cp);
269 >                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
270 >                        cp += 2;
271 >                        atos(matcheck, MAXSTR, cp);
272                          cp = sskip(cp);
273                          continue;
274                  case 'a':                       /* use everything */
# Line 258 | Line 284 | char   *nm;
284                                  break;
285                          switch (*++cp) {
286                          case 'a':                       /* average */
287 <                                thisillum.flags |= IL_COLAVG;
288 <                                thisillum.flags &= ~IL_COLDST;
287 >                                thisillum.flags = (thisillum.flags|IL_COLAVG)
288 >                                                        & ~IL_COLDST;
289                                  break;
290                          case 'd':                       /* distribution */
291 <                                thisillum.flags |= IL_COLDST;
266 <                                thisillum.flags &= ~IL_COLAVG;
291 >                                thisillum.flags |= (IL_COLDST|IL_COLAVG);
292                                  break;
293                          case 'n':                       /* none */
294                                  thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
# Line 273 | Line 298 | char   *nm;
298                          }
299                          cp = sskip(cp);
300                          continue;
301 <                case 'd':                       /* point sample density */
301 >                case 'd':                       /* sample density / BSDF data */
302                          if (*++cp != '=')
303                                  break;
304 <                        if (!isintd(++cp, " \t\n"))
305 <                                break;
306 <                        sampdens = atoi(cp);
304 >                        if (thisillum.sd != NULL) {
305 >                                free_BSDF(thisillum.sd);
306 >                                thisillum.sd = NULL;
307 >                        }
308 >                        if (!*++cp || isspace(*cp))
309 >                                continue;
310 >                        if (isintd(++cp, " \t\n\r")) {
311 >                                thisillum.sampdens = atoi(cp);
312 >                        } else {
313 >                                atos(buf, sizeof(buf), cp);
314 >                                thisillum.sd = load_BSDF(buf);
315 >                                if (thisillum.sd == NULL)
316 >                                        break;
317 >                        }
318                          cp = sskip(cp);
319                          continue;
320 <                case 's':                       /* point super-samples */
320 >                case 's':                       /* surface super-samples */
321                          if (*++cp != '=')
322                                  break;
323 <                        if (!isintd(++cp, " \t\n"))
323 >                        if (!isintd(++cp, " \t\n\r"))
324                                  break;
325                          thisillum.nsamps = atoi(cp);
326                          cp = sskip(cp);
327                          continue;
328 +                case 'l':                       /* light sources */
329 +                        cp++;
330 +                        if (*cp == '+')
331 +                                thisillum.flags |= IL_LIGHT;
332 +                        else if (*cp == '-')
333 +                                thisillum.flags &= ~IL_LIGHT;
334 +                        else
335 +                                break;
336 +                        cp++;
337 +                        continue;
338 +                case 'b':                       /* brightness */
339 +                        if (*++cp != '=')
340 +                                break;
341 +                        if (!isfltd(++cp, " \t\n\r"))
342 +                                break;
343 +                        thisillum.minbrt = atof(cp);
344 +                        if (thisillum.minbrt < 0.)
345 +                                thisillum.minbrt = 0.;
346 +                        cp = sskip(cp);
347 +                        continue;
348                  case 'o':                       /* output file */
349                          if (*++cp != '=')
350                                  break;
351 <                        if (!*++cp)
351 >                        if (!*++cp || isspace(*cp))
352                                  break;
353                          atos(buf, sizeof(buf), cp);
354                          cp = sskip(cp);
# Line 303 | Line 359 | char   *nm;
359                          }
360                          doneheader = 0;
361                          continue;
362 +                case 'u':                       /* up direction */
363 +                        if (*++cp != '=')
364 +                                break;
365 +                        if (!*++cp || isspace(*cp)) {
366 +                                thisillum.udir = UDunknown;
367 +                                continue;
368 +                        }
369 +                        negax = 0;
370 +                        if (*cp == '+')
371 +                                cp++;
372 +                        else if (*cp == '-') {
373 +                                negax++;
374 +                                cp++;
375 +                        }
376 +                        switch (*cp++) {
377 +                        case 'x':
378 +                        case 'X':
379 +                                thisillum.udir = negax ? UDxneg : UDxpos;
380 +                                break;
381 +                        case 'y':
382 +                        case 'Y':
383 +                                thisillum.udir = negax ? UDyneg : UDypos;
384 +                                break;
385 +                        case 'z':
386 +                        case 'Z':
387 +                                thisillum.udir = negax ? UDxneg : UDxpos;
388 +                                break;
389 +                        default:
390 +                                thisillum.udir = UDunknown;
391 +                                break;
392 +                        }
393 +                        if (thisillum.udir == UDunknown || !isspace(*cp))
394 +                                break;
395 +                        continue;
396 +                case '!':                       /* processed file! */
397 +                        sprintf(errmsg, "(%s): already processed!", nm);
398 +                        error(WARNING, errmsg);
399 +                        matselect = S_NONE;
400 +                        return;
401                  }
402          opterr:                                 /* skip faulty option */
403 <                cp = sskip(cp);
403 >                while (*cp && !isspace(*cp))
404 >                        cp++;
405                  nerrs++;
406          }
407                                                  /* print header? */
# Line 320 | Line 416 | char   *nm;
416                                  progname, nerrs);
417          }
418                                                  /* print pure comment */
419 <        putchar('#'); putchar(' '); fputs(s+2, stdout);
419 >        printf("# %s", s+2);
420   }
421  
422 + void
423 + printopts(void)                 /* print out option default values */
424 + {
425 +        printf("m=%-15s\t\t# material name\n", thisillum.matname);
426 +        printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
427 +        if (thisillum.flags & IL_COLAVG)
428 +                if (thisillum.flags & IL_COLDST)
429 +                        printf("c=d\t\t\t\t# color distribution\n");
430 +                else
431 +                        printf("c=a\t\t\t\t# color average\n");
432 +        else
433 +                printf("c=n\t\t\t\t# color none\n");
434 +        if (thisillum.flags & IL_LIGHT)
435 +                printf("l+\t\t\t\t# light type on\n");
436 +        else
437 +                printf("l-\t\t\t\t# light type off\n");
438 +        printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
439 +        printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
440 +        printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
441 +        switch (thisillum.udir) {
442 +        case UDzneg:
443 +                fputs("u=-Z\t\t\t\t# up is negative Z\n", stdout);
444 +                break;
445 +        case UDyneg:
446 +                fputs("u=-Y\t\t\t\t# up is negative Y\n", stdout);
447 +                break;
448 +        case UDxneg:
449 +                fputs("u=-X\t\t\t\t# up is negative X\n", stdout);
450 +                break;
451 +        case UDxpos:
452 +                fputs("u=+X\t\t\t\t# up is positive X\n", stdout);
453 +                break;
454 +        case UDypos:
455 +                fputs("u=+Y\t\t\t\t# up is positive Y\n", stdout);
456 +                break;
457 +        case UDzpos:
458 +                fputs("u=+Z\t\t\t\t# up is positive Z\n", stdout);
459 +                break;
460 +        case UDunknown:
461 +                break;
462 +        }
463 + }
464  
465 < printhead(ac, av)                       /* print out header */
466 < register int  ac;
467 < register char  **av;
465 >
466 > void
467 > printhead(                      /* print out header */
468 >        register int  ac,
469 >        register char  **av
470 > )
471   {
472          putchar('#');
473          while (ac-- > 0) {
474                  putchar(' ');
475                  fputs(*av++, stdout);
476          }
477 <        putchar('\n');
477 >        fputs("\n#@mkillum !\n", stdout);
478   }
479  
480  
481 < xobject(fp, nm)                         /* translate an object from fp */
482 < FILE  *fp;
483 < char  *nm;
481 > void
482 > xobject(                                /* translate an object from fp */
483 >        FILE  *fp,
484 >        char  *nm
485 > )
486   {
487          OBJREC  thisobj;
488          char  str[MAXSTR];
# Line 350 | Line 493 | char  *nm;
493          if (fgetword(str, MAXSTR, fp) == NULL)
494                  goto readerr;
495                                          /* is it an alias? */
496 <        if (!strcmp(str, ALIASID)) {
496 >        if (!strcmp(str, ALIASKEY)) {
497                  if (fgetword(str, MAXSTR, fp) == NULL)
498                          goto readerr;
499 <                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
499 >                printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
500                  if (fgetword(str, MAXSTR, fp) == NULL)
501                          goto readerr;
502 <                printf(" %s\n", str);
502 >                printf("\t%s\n", str);
503                  return;
504          }
505 <        thisobj.omod = OVOID;
505 >        thisobj.omod = OVOID;           /* unused field */
506          if ((thisobj.otype = otype(str)) < 0) {
507                  sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
508                  error(USER, errmsg);
# Line 376 | Line 519 | char  *nm;
519                  doit = 0;
520                  break;
521          case S_ALL:
522 <                doit = issurface(thisobj.otype);
522 >                doit = 1;
523                  break;
524          case S_ELEM:
525                  doit = !strcmp(thisillum.altmat, matcheck);
# Line 385 | Line 528 | char  *nm;
528                  doit = strcmp(thisillum.altmat, matcheck);
529                  break;
530          }
531 <        if (doit)                               /* make sure we can do it */
531 >        doit = doit && issurface(thisobj.otype);
532 >                                                /* print header? */
533 >        checkhead();
534 >                                                /* process object */
535 >        if (doit)
536                  switch (thisobj.otype) {
390                case OBJ_SPHERE:
537                  case OBJ_FACE:
538 +                        my_face(&thisobj, &thisillum, nm);
539 +                        break;
540 +                case OBJ_SPHERE:
541 +                        my_sphere(&thisobj, &thisillum, nm);
542 +                        break;
543                  case OBJ_RING:
544 +                        my_ring(&thisobj, &thisillum, nm);
545                          break;
546                  default:
547 <                        sprintf(errmsg,
396 <                                "(%s): cannot make illum for %s \"%s\"",
397 <                                        nm, ofun[thisobj.otype].funame,
398 <                                        thisobj.oname);
399 <                        error(WARNING, errmsg);
400 <                        doit = 0;
547 >                        my_default(&thisobj, &thisillum, nm);
548                          break;
549                  }
403                                                /* print header? */
404        checkhead();
405                                                /* process object */
406        if (doit)
407                mkillum(&thisobj, &thisillum, &rt);
550          else
551                  printobj(thisillum.altmat, &thisobj);
552                                                  /* free arguments */
553          freefargs(&thisobj.oargs);
554          return;
555   readerr:
556 <        sprintf(errmsg, "(%s): error reading scene", nm);
556 >        sprintf(errmsg, "(%s): error reading input", nm);
557          error(USER, errmsg);
558   }
417
418
419 int
420 otype(ofname)                   /* get object function number from its name */
421 char  *ofname;
422 {
423        register int  i;
424
425        for (i = 0; i < NUMOTYPE; i++)
426                if (!strcmp(ofun[i].funame, ofname))
427                        return(i);
428
429        return(-1);             /* not found */
430 }
431
432
433 o_default() {}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines