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.39 by greg, Sat Oct 13 20:15:43 2012 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  "rtprocess.h" /* win_popen() */
12 + #include  "mkillum.h"
13 +
14                                  /* default parameters */
15 < #define  SAMPDENS       128             /* points per projected steradian */
15 > #define  SAMPDENS       48              /* points per projected steradian */
16   #define  NSAMPS         32              /* samples per point */
17   #define  DFLMAT         "illum_mat"     /* material name */
18 + #define  DFLDAT         "illum"         /* data file name */
19                                  /* 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 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
25   struct illum_args  thisillum = {        /* our illum and default values */
26                  0,
27 +                0.,
28                  DFLMAT,
29 <                DFLMAT,
29 >                DFLDAT,
30                  0,
31                  VOIDID,
32 +                SAMPDENS,
33                  NSAMPS,
34 +                0.,
35          };
36  
43 int     sampdens = SAMPDENS;    /* sample point density */
37   char    matcheck[MAXSTR];       /* current material to include or exclude */
38   int     matselect = S_ALL;      /* selection criterion */
39  
47 FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
48
40   int     gargc;                  /* global argc */
41   char    **gargv;                /* global argv */
51 #define  progname       gargv[0]
42  
43   int     doneheader = 0;         /* printed header yet? */
44   #define  checkhead()    if (!doneheader++) printhead(gargc,gargv)
45  
46   int     warnings = 1;           /* print warnings? */
47  
48 < extern char     *fgetline(), *fgetword(), *sskip(),
49 <                *atos(), *iskip(), *fskip();
50 < extern FILE     *popen();
48 > void init(char *octnm, int np);
49 > void filter(register FILE       *infp, char     *name);
50 > void xoptions(char      *s, char        *nm);
51 > void printopts(void);
52 > void printhead(register int  ac, register char  **av);
53 > void xobject(FILE  *fp, char  *nm);
54  
55  
56 < main(argc, argv)                /* compute illum distributions using rtrace */
57 < int     argc;
58 < char    *argv[];
56 > int
57 > main(           /* compute illum distributions using rtrace */
58 >        int     argc,
59 >        char    *argv[]
60 > )
61   {
62 <        extern char     *getenv(), *getpath();
63 <        char    *rtpath;
62 >        int     nprocs = 1;
63 >        FILE    *fp;
64 >        int     rval;
65          register int    i;
66                                  /* set global arguments */
67 <        gargc = argc; gargv = argv;
68 <                                /* set up rtrace command */
69 <        if (argc < 2)
70 <                error(USER, "too few arguments");
71 <        for (i = 1; i < argc-1; i++) {
72 <                rtargv[rtargc++] = argv[i];
73 <                if (argv[i][0] == '-' && argv[i][1] == 'w')
74 <                        warnings = !warnings;
75 <        }
76 <        for (i = 0; myrtopts[i] != NULL; i++)
77 <                rtargv[rtargc++] = myrtopts[i];
78 <        rtargv[rtargc++] = argv[argc-1];
79 <        rtargv[rtargc] = NULL;
84 <                                /* 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);
67 >        gargv = argv;
68 >        progname = gargv[0];
69 >                                /* set up rendering defaults */
70 >        dstrsrc = 0.5;
71 >        directrelay = 3;
72 >        ambounce = 2;
73 >                                /* get options from command line */
74 >        for (i = 1; i < argc; i++) {
75 >                while ((rval = expandarg(&argc, &argv, i)) > 0)
76 >                        ;
77 >                if (rval < 0) {
78 >                        sprintf(errmsg, "cannot expand '%s'", argv[i]);
79 >                        error(SYSTEM, errmsg);
80                  }
81 <                execv(rtpath, rtargv);
82 <                perror(rtpath);
83 <                exit(1);
81 >                if (argv[i][0] != '-')
82 >                        break;
83 >                if (!strcmp(argv[i], "-w")) {
84 >                        warnings = 0;
85 >                        continue;
86 >                }
87 >                if (!strcmp(argv[i], "-n")) {
88 >                        nprocs = atoi(argv[++i]);
89 >                        if (nprocs <= 0)
90 >                                error(USER, "illegal number of processes");
91 >                        continue;
92 >                }
93 >                if (!strcmp(argv[i], "-defaults")) {
94 >                        printopts();
95 >                        print_rdefaults();
96 >                        quit(0);
97 >                }
98 >                rval = getrenderopt(argc-i, argv+i);
99 >                if (rval < 0) {
100 >                        sprintf(errmsg, "bad render option at '%s'", argv[i]);
101 >                        error(USER, errmsg);
102 >                }
103 >                i += rval;
104          }
105 +        gargc = ++i;
106 +                                /* add "mandatory" render options */
107 +        do_irrad = 0;
108 +        if (gargc > argc || argv[gargc-1][0] == '-')
109 +                error(USER, "missing octree argument");
110                                  /* else initialize and run our calculation */
111 <        init();
112 <        filter(stdin, "standard input");
111 >        init(argv[gargc-1], nprocs);
112 >        if (gargc < argc) {
113 >                if (gargc == argc-1 || argv[gargc][0] != '<' || argv[gargc][1])
114 >                        error(USER, "use '< file1 file2 ..' for multiple inputs");
115 >                for (i = gargc+1; i < argc; i++) {
116 >                        if ((fp = fopen(argv[i], "r")) == NULL) {
117 >                                sprintf(errmsg,
118 >                                "cannot open scene file \"%s\"", argv[i]);
119 >                                error(SYSTEM, errmsg);
120 >                        }
121 >                        filter(fp, argv[i]);
122 >                        fclose(fp);
123 >                }
124 >        } else
125 >                filter(stdin, "standard input");
126          quit(0);
127 +        return 0; /* pro forma return */
128   }
129  
130  
131 < init()                          /* start rtrace and set up buffers */
131 > void
132 > init(char *octnm, int np)               /* start rendering process(es) */
133   {
134 <        int     maxbytes;
135 <
136 <        maxbytes = open_process(rt.pd, rtargv);
137 <        if (maxbytes == 0) {
138 <                eputs(rtargv[0]);
139 <                eputs(": command not found\n");
140 <                exit(1);
141 <        }
142 <        if (maxbytes < 0)
143 <                error(SYSTEM, "cannot start rtrace process");
144 <        rt.bsiz = maxbytes/(6*sizeof(float));
145 <        rt.buf = (float *)malloc(rt.bsiz*(6*sizeof(float)));
146 <        if (rt.buf == NULL)
118 <                error(SYSTEM, "out of memory in init");
119 <        rt.bsiz--;                      /* allow for flush ray */
120 <        rt.nrays = 0;
134 >                                        /* set up signal handling */
135 >        signal(SIGINT, quit);
136 > #ifdef SIGHUP
137 >        signal(SIGHUP, quit);
138 > #endif
139 > #ifdef SIGTERM
140 >        signal(SIGTERM, quit);
141 > #endif
142 > #ifdef SIGPIPE
143 >        signal(SIGPIPE, quit);
144 > #endif
145 >                                        /* start rendering process(es) */
146 >        ray_pinit(octnm, np);
147   }
148  
149  
150 < quit(status)                    /* exit with status */
151 < int  status;
150 > void
151 > eputs(                          /* put string to stderr */
152 >        register char  *s
153 > )
154   {
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 {
155          static int  midline = 0;
156  
157          if (!*s) return;
# Line 152 | Line 164 | register char  *s;
164   }
165  
166  
167 + void
168   wputs(s)                        /* print warning if enabled */
169   char  *s;
170   {
# Line 160 | Line 173 | char  *s;
173   }
174  
175  
176 < filter(infp, name)              /* process stream */
177 < register FILE   *infp;
178 < char    *name;
176 > void
177 > quit(ec)                        /* make sure exit is called */
178 > int     ec;
179   {
180 +        if (ray_pnprocs > 0)    /* close children if any */
181 +                ray_pclose(0);          
182 +        exit(ec);
183 + }
184 +
185 +
186 + void
187 + filter(         /* process stream */
188 +        register FILE   *infp,
189 +        char    *name
190 + )
191 + {
192          char    buf[512];
193          FILE    *pfp;
194          register int    c;
# Line 192 | Line 217 | char   *name;
217   }
218  
219  
220 < xoptions(s, nm)                 /* process options in string s */
221 < char    *s;
222 < char    *nm;
220 > void
221 > xoptions(                       /* process options in string s */
222 >        char    *s,
223 >        char    *nm
224 > )
225   {
226          extern FILE     *freopen();
227          char    buf[64];
228 +        int     negax;
229          int     nerrs = 0;
230          register char   *cp;
231  
# Line 211 | Line 239 | char   *nm;
239                  case ' ':
240                  case '\t':
241                  case '\n':
242 +                case '\r':
243 +                case '\f':
244                          cp++;
245                          continue;
246                  case 'm':                       /* material name */
247                          if (*++cp != '=')
248                                  break;
249 <                        if (!*++cp)
249 >                        if (!*++cp || isspace(*cp))
250                                  break;
251                          atos(thisillum.matname, MAXSTR, cp);
252                          cp = sskip(cp);
# Line 228 | Line 258 | char   *nm;
258                  case 'f':                       /* data file name */
259                          if (*++cp != '=')
260                                  break;
261 <                        if (!*++cp) {
261 >                        if (!*++cp || isspace(*cp)) {
262 >                                strcpy(thisillum.datafile,thisillum.matname);
263 >                                thisillum.dfnum = 0;
264                                  thisillum.flags &= ~IL_DATCLB;
265                                  continue;
266                          }
# Line 239 | Line 271 | char   *nm;
271                          continue;
272                  case 'i':                       /* include material */
273                  case 'e':                       /* exclude material */
274 <                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
243 <                        if (*++cp != '=')
274 >                        if (cp[1] != '=')
275                                  break;
276 <                        atos(matcheck, MAXSTR, ++cp);
276 >                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
277 >                        cp += 2;
278 >                        atos(matcheck, MAXSTR, cp);
279                          cp = sskip(cp);
280                          continue;
281                  case 'a':                       /* use everything */
# Line 258 | Line 291 | char   *nm;
291                                  break;
292                          switch (*++cp) {
293                          case 'a':                       /* average */
294 <                                thisillum.flags |= IL_COLAVG;
295 <                                thisillum.flags &= ~IL_COLDST;
294 >                                thisillum.flags = (thisillum.flags|IL_COLAVG)
295 >                                                        & ~IL_COLDST;
296                                  break;
297                          case 'd':                       /* distribution */
298 <                                thisillum.flags |= IL_COLDST;
266 <                                thisillum.flags &= ~IL_COLAVG;
298 >                                thisillum.flags |= (IL_COLDST|IL_COLAVG);
299                                  break;
300                          case 'n':                       /* none */
301                                  thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
# Line 273 | Line 305 | char   *nm;
305                          }
306                          cp = sskip(cp);
307                          continue;
308 <                case 'd':                       /* point sample density */
308 >                case 'd':                       /* sample density */
309                          if (*++cp != '=')
310                                  break;
311 <                        if (!isintd(++cp, " \t\n"))
312 <                                break;
313 <                        sampdens = atoi(cp);
311 >                        if (!*++cp || isspace(*cp))
312 >                                continue;
313 >                        if (isintd(cp, " \t\n\r")) {
314 >                                thisillum.sampdens = atoi(cp);
315 >                        } else {
316 >                                error(WARNING, "direct BSDF input unsupported");
317 >                                goto opterr;
318 >                        }
319                          cp = sskip(cp);
320                          continue;
321 <                case 's':                       /* point super-samples */
321 >                case 's':                       /* surface super-samples */
322                          if (*++cp != '=')
323                                  break;
324 <                        if (!isintd(++cp, " \t\n"))
324 >                        if (!isintd(++cp, " \t\n\r"))
325                                  break;
326                          thisillum.nsamps = atoi(cp);
327                          cp = sskip(cp);
328                          continue;
329 +                case 'l':                       /* light sources */
330 +                        cp++;
331 +                        if (*cp == '+')
332 +                                thisillum.flags |= IL_LIGHT;
333 +                        else if (*cp == '-')
334 +                                thisillum.flags &= ~IL_LIGHT;
335 +                        else
336 +                                break;
337 +                        cp++;
338 +                        continue;
339 +                case 'b':                       /* brightness */
340 +                        if (*++cp != '=')
341 +                                break;
342 +                        if (!isfltd(++cp, " \t\n\r"))
343 +                                break;
344 +                        thisillum.minbrt = atof(cp);
345 +                        if (thisillum.minbrt < 0.)
346 +                                thisillum.minbrt = 0.;
347 +                        cp = sskip(cp);
348 +                        continue;
349                  case 'o':                       /* output file */
350                          if (*++cp != '=')
351                                  break;
352 <                        if (!*++cp)
352 >                        if (!*++cp || isspace(*cp))
353                                  break;
354                          atos(buf, sizeof(buf), cp);
355                          cp = sskip(cp);
# Line 303 | Line 360 | char   *nm;
360                          }
361                          doneheader = 0;
362                          continue;
363 +                case 't':                       /* object thickness */
364 +                        if (*++cp != '=')
365 +                                break;
366 +                        if (!isfltd(++cp, " \t\n\r"))
367 +                                break;
368 +                        thisillum.thick = atof(cp);
369 +                        if (thisillum.thick < .0)
370 +                                thisillum.thick = .0;
371 +                        cp = sskip(cp);
372 +                        continue;
373 +                case '!':                       /* processed file! */
374 +                        sprintf(errmsg, "(%s): already processed!", nm);
375 +                        error(WARNING, errmsg);
376 +                        matselect = S_NONE;
377 +                        return;
378                  }
379          opterr:                                 /* skip faulty option */
380 <                cp = sskip(cp);
380 >                while (*cp && !isspace(*cp))
381 >                        cp++;
382                  nerrs++;
383          }
384                                                  /* print header? */
# Line 320 | Line 393 | char   *nm;
393                                  progname, nerrs);
394          }
395                                                  /* print pure comment */
396 <        putchar('#'); putchar(' '); fputs(s+2, stdout);
396 >        printf("# %s", s+2);
397   }
398  
399 + void
400 + printopts(void)                 /* print out option default values */
401 + {
402 +        printf("m=%-15s\t\t# material name\n", thisillum.matname);
403 +        printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
404 +        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 +        printf("d=%d\t\t\t\t# density of directions\n", thisillum.sampdens);
416 +        printf("s=%d\t\t\t\t# samples per direction\n", thisillum.nsamps);
417 +        printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
418 +        printf("t=%f\t\t\t# object thickness\n", thisillum.thick);
419 + }
420  
421 < printhead(ac, av)                       /* print out header */
422 < register int  ac;
423 < register char  **av;
421 >
422 > void
423 > printhead(                      /* print out header */
424 >        register int  ac,
425 >        register char  **av
426 > )
427   {
428          putchar('#');
429          while (ac-- > 0) {
430                  putchar(' ');
431                  fputs(*av++, stdout);
432          }
433 <        putchar('\n');
433 >        fputs("\n#@mkillum !\n", stdout);
434   }
435  
436  
437 < xobject(fp, nm)                         /* translate an object from fp */
438 < FILE  *fp;
439 < char  *nm;
437 > void
438 > xobject(                                /* translate an object from fp */
439 >        FILE  *fp,
440 >        char  *nm
441 > )
442   {
443          OBJREC  thisobj;
444          char  str[MAXSTR];
# Line 350 | Line 449 | char  *nm;
449          if (fgetword(str, MAXSTR, fp) == NULL)
450                  goto readerr;
451                                          /* is it an alias? */
452 <        if (!strcmp(str, ALIASID)) {
452 >        if (!strcmp(str, ALIASKEY)) {
453                  if (fgetword(str, MAXSTR, fp) == NULL)
454                          goto readerr;
455 <                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
455 >                printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
456                  if (fgetword(str, MAXSTR, fp) == NULL)
457                          goto readerr;
458 <                printf(" %s\n", str);
458 >                printf("\t%s\n", str);
459                  return;
460          }
461 <        thisobj.omod = OVOID;
461 >        thisobj.omod = OVOID;           /* unused field */
462          if ((thisobj.otype = otype(str)) < 0) {
463                  sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
464                  error(USER, errmsg);
# Line 376 | Line 475 | char  *nm;
475                  doit = 0;
476                  break;
477          case S_ALL:
478 <                doit = issurface(thisobj.otype);
478 >                doit = 1;
479                  break;
480          case S_ELEM:
481                  doit = !strcmp(thisillum.altmat, matcheck);
# Line 385 | Line 484 | char  *nm;
484                  doit = strcmp(thisillum.altmat, matcheck);
485                  break;
486          }
487 <        if (doit)                               /* make sure we can do it */
487 >        doit = doit && issurface(thisobj.otype);
488 >                                                /* print header? */
489 >        checkhead();
490 >                                                /* process object */
491 >        if (doit)
492                  switch (thisobj.otype) {
390                case OBJ_SPHERE:
493                  case OBJ_FACE:
494 +                        my_face(&thisobj, &thisillum, nm);
495 +                        break;
496 +                case OBJ_SPHERE:
497 +                        my_sphere(&thisobj, &thisillum, nm);
498 +                        break;
499                  case OBJ_RING:
500 +                        my_ring(&thisobj, &thisillum, nm);
501                          break;
502                  default:
503 <                        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;
503 >                        my_default(&thisobj, &thisillum, nm);
504                          break;
505                  }
403                                                /* print header? */
404        checkhead();
405                                                /* process object */
406        if (doit)
407                mkillum(&thisobj, &thisillum, &rt);
506          else
507                  printobj(thisillum.altmat, &thisobj);
508                                                  /* free arguments */
509          freefargs(&thisobj.oargs);
510          return;
511   readerr:
512 <        sprintf(errmsg, "(%s): error reading scene", nm);
512 >        sprintf(errmsg, "(%s): error reading input", nm);
513          error(USER, errmsg);
514   }
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