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