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 1.3 by greg, Tue Jul 23 17:18:21 1991 UTC

# Line 8 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   * Make illum sources for optimizing rendering process
9   */
10  
11 < #include  "standard.h"
11 > #include  "mkillum.h"
12  
13 < #include  "object.h"
13 > #include  <signal.h>
14  
15 < #include  "otypes.h"
15 > #include  <ctype.h>
16  
17                                  /* default parameters */
18   #define  SAMPDENS       128             /* points per projected steradian */
# Line 31 | Line 31 | int  rtargc = 13;
31                                  /* overriding rtrace options */
32   char  *myrtopts[] = { "-I-", "-i-", "-di+", "-ov", "-h-", "-fff", 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 */
50 < } 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 >                DFLMAT,
40 >                0,
41 >                VOIDID,
42 >                SAMPDENS,
43 >                NSAMPS,
44 >        };
45  
59 int     sampdens = SAMPDENS;    /* sample point density */
46   char    matcheck[MAXSTR];       /* current material to include or exclude */
47   int     matselect = S_ALL;      /* selection criterion */
48  
49 < 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 */
49 > FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
50  
51   int     gargc;                  /* global argc */
52   char    **gargv;                /* global argv */
53   #define  progname       gargv[0]
54  
55   int     doneheader = 0;         /* printed header yet? */
56 + #define  checkhead()    if (!doneheader++) printhead(gargc,gargv)
57  
58 + int     warnings = 1;           /* print warnings? */
59 +
60   extern char     *fgetline(), *fgetword(), *sskip(),
61                  *atos(), *iskip(), *fskip();
62   extern FILE     *popen();
# Line 82 | Line 68 | char   *argv[];
68   {
69          extern char     *getenv(), *getpath();
70          char    *rtpath;
71 +        FILE    *fp;
72          register int    i;
73                                  /* set global arguments */
74 <        gargc = argc; gargv = argv;
74 >        gargv = argv;
75                                  /* set up rtrace command */
76 <        if (argc < 2)
77 <                error(USER, "too few arguments");
78 <        for (i = 1; i < argc-1; i++)
76 >        for (i = 1; i < argc; i++) {
77 >                if (argv[i][0] == '<' && !argv[i][1])
78 >                        break;
79                  rtargv[rtargc++] = argv[i];
80 +                if (argv[i][0] == '-' && argv[i][1] == 'w')
81 +                        warnings = !warnings;
82 +        }
83 +        if ((gargc = i) < 2)
84 +                error(USER, "too few arguments");
85 +        rtargc--;
86          for (i = 0; myrtopts[i] != NULL; i++)
87                  rtargv[rtargc++] = myrtopts[i];
88 <        rtargv[rtargc++] = argv[argc-1];
88 >        rtargv[rtargc++] = argv[gargc-1];
89          rtargv[rtargc] = NULL;
90                                  /* just asking for defaults? */
91 <        if (!strcmp(argv[argc-1], "-defaults")) {
91 >        if (!strcmp(argv[gargc-1], "-defaults")) {
92                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
93                  if (rtpath == NULL) {
94                          eputs(rtargv[0]);
# Line 108 | Line 101 | char   *argv[];
101          }
102                                  /* else initialize and run our calculation */
103          init();
104 <        mkillum(stdin, "standard input");
105 <        exit(cleanup());        /* cleanup returns rtrace status */
104 >        if (gargc+1 < argc)
105 >                for (i = gargc+1; i < argc; i++) {
106 >                        if ((fp = fopen(argv[i], "r")) == NULL {
107 >                                sprintf(errmsg,
108 >                                "cannot open scene file \"%s\"", argv[i]);
109 >                                error(SYSTEM, errmsg);
110 >                        }
111 >                        filter(fp, argv[i]);
112 >                        fclose(fp);
113 >                }
114 >        else
115 >                filter(stdin, "standard input");
116 >        quit(0);
117   }
118  
119  
120 + quit(status)                    /* exit with status */
121 + int  status;
122 + {
123 +        int     rtstat;
124 +
125 +        rtstat = close_process(rt.pd);
126 +        if (status == 0)
127 +                if (rtstat < 0)
128 +                        error(WARNING,
129 +                        "unknown return status from rtrace process");
130 +                else
131 +                        status = rtstat;
132 +        exit(status);
133 + }
134 +
135 +
136   init()                          /* start rtrace and set up buffers */
137   {
138 +        extern int  o_face(), o_ring();
139          int     maxbytes;
140 <
141 <        maxbytes = open_process(rt_pd, rtargv);
140 >                                        /* set up object functions */
141 >        ofun[OBJ_FACE].funp = o_face;
142 >        ofun[OBJ_RING].funp = o_ring;
143 >                                        /* set up signal handling */
144 >        signal(SIGPIPE, quit);
145 >                                        /* start rtrace process */
146 >        maxbytes = open_process(rt.pd, rtargv);
147          if (maxbytes == 0) {
148                  eputs(rtargv[0]);
149                  eputs(": command not found\n");
# Line 125 | Line 151 | init()                         /* start rtrace and set up buffers */
151          }
152          if (maxbytes < 0)
153                  error(SYSTEM, "cannot start rtrace process");
154 <        rt_bsiz = maxbytes/(6*sizeof(float));
155 <        rt_buf = (float *)malloc(rt_bsiz*(6*sizeof(float)));
156 <        if (rt_buf == NULL)
154 >        rt.bsiz = maxbytes/(6*sizeof(float));
155 >        rt.buf = (float *)malloc(rt.bsiz*(6*sizeof(float)));
156 >        if (rt.buf == NULL)
157                  error(SYSTEM, "out of memory in init");
158 <        rt_bsiz--;                      /* allow for flush ray */
159 <        rt_nrays = 0;
158 >        rt.bsiz--;                      /* allow for flush ray */
159 >        rt.nrays = 0;
160   }
161  
162  
137 int
138 cleanup()                       /* close rtrace process and return status */
139 {
140        int     status;
141
142        free((char *)rt_buf);
143        rt_bsiz = 0;
144        status = close_process(rt_pd);
145        if (status < 0)
146                return(0);              /* unknown status */
147        return(status);
148 }
149
150
163   eputs(s)                                /* put string to stderr */
164   register char  *s;
165   {
# Line 163 | Line 175 | register char  *s;
175   }
176  
177  
178 < mkillum(infp, name)             /* process stream */
178 > wputs(s)                        /* print warning if enabled */
179 > char  *s;
180 > {
181 >        if (warnings)
182 >                eputs(s);
183 > }
184 >
185 >
186 > filter(infp, name)              /* process stream */
187   register FILE   *infp;
188   char    *name;
189   {
# Line 177 | Line 197 | char   *name;
197                  if (c == '#') {                         /* comment/options */
198                          buf[0] = c;
199                          fgets(buf+1, sizeof(buf)-1, infp);
200 <                        fputs(buf, stdout);
181 <                        getoptions(buf, name);
200 >                        xoptions(buf, name);
201                  } else if (c == '!') {                  /* command */
202                          buf[0] = c;
203                          fgetline(buf+1, sizeof(buf)-1, infp);
# Line 186 | Line 205 | char   *name;
205                                  sprintf(errmsg, "cannot execute \"%s\"", buf);
206                                  error(SYSTEM, errmsg);
207                          }
208 <                        mkillum(pfp, buf);
208 >                        filter(pfp, buf);
209                          pclose(pfp);
210                  } else {                                /* object */
211                          ungetc(c, infp);
# Line 196 | Line 215 | char   *name;
215   }
216  
217  
218 < getoptions(s, nm)               /* get options from string s */
218 > xoptions(s, nm)                 /* process options in string s */
219   char    *s;
220   char    *nm;
221   {
# Line 205 | Line 224 | char   *nm;
224          int     nerrs = 0;
225          register char   *cp;
226  
227 <        if (strncmp(s, "#@mkillum", 9) || !isspace(s[9]))
227 >        if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
228 >                fputs(s, stdout);               /* not for us */
229                  return;
230 +        }
231          cp = s+10;
232          while (*cp) {
233                  switch (*cp) {
# Line 216 | Line 237 | char   *nm;
237                          cp++;
238                          continue;
239                  case 'm':                       /* material name */
240 <                        cp++;
220 <                        if (*cp != '=')
240 >                        if (*++cp != '=')
241                                  break;
242 <                        cp++;
242 >                        if (!*++cp)
243 >                                break;
244                          atos(thisillum.matname, MAXSTR, cp);
245                          cp = sskip(cp);
246                          if (!(thisillum.flags & IL_DATCLB)) {
# Line 228 | Line 249 | char   *nm;
249                          }
250                          continue;
251                  case 'f':                       /* data file name */
252 <                        cp++;
232 <                        if (*cp != '=')
252 >                        if (*++cp != '=')
253                                  break;
254 <                        cp++;
235 <                        if (*cp == '\0') {
254 >                        if (!*++cp) {
255                                  thisillum.flags &= ~IL_DATCLB;
256                                  continue;
257                          }
# Line 243 | Line 262 | char   *nm;
262                          continue;
263                  case 'i':                       /* include material */
264                  case 'e':                       /* exclude material */
265 <                        matselect = (*cp++ == 'i') ? S_ELEM : S_COMPL;
266 <                        if (*cp != '=')
265 >                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
266 >                        if (*++cp != '=')
267                                  break;
268 <                        cp++;
250 <                        atos(matcheck, MAXSTR, cp);
268 >                        atos(matcheck, MAXSTR, ++cp);
269                          cp = sskip(cp);
270                          continue;
271                  case 'a':                       /* use everything */
# Line 259 | Line 277 | char   *nm;
277                          matselect = S_NONE;
278                          continue;
279                  case 'c':                       /* color calculation */
280 <                        cp++;
263 <                        if (*cp != '=')
280 >                        if (*++cp != '=')
281                                  break;
282 <                        cp++;
266 <                        switch (*cp) {
282 >                        switch (*++cp) {
283                          case 'a':                       /* average */
284                                  thisillum.flags |= IL_COLAVG;
285                                  thisillum.flags &= ~IL_COLDST;
# Line 281 | Line 297 | char   *nm;
297                          cp = sskip(cp);
298                          continue;
299                  case 'd':                       /* point sample density */
300 <                        cp++;
285 <                        if (*cp != '=')
300 >                        if (*++cp != '=')
301                                  break;
302 <                        cp++;
288 <                        if (!isintd(cp, " \t\n"))
302 >                        if (!isintd(++cp, " \t\n"))
303                                  break;
304 <                        sampdens = atoi(cp);
304 >                        thisillum.sampdens = atoi(cp);
305                          cp = sskip(cp);
306                          continue;
307                  case 's':                       /* point super-samples */
308 <                        cp++;
295 <                        if (*cp != '=')
308 >                        if (*++cp != '=')
309                                  break;
310 <                        cp++;
298 <                        if (!isintd(cp, " \t\n"))
310 >                        if (!isintd(++cp, " \t\n"))
311                                  break;
312                          thisillum.nsamps = atoi(cp);
313                          cp = sskip(cp);
314                          continue;
315 +                case 'l':                       /* light sources */
316 +                        if (*++cp != '+' && *cp != '-')
317 +                                break;
318 +                        if (*cp++ == '+')
319 +                                thisillum.flags |= IL_LIGHT;
320 +                        else
321 +                                thisillum.flags &= ~IL_LIGHT;
322 +                        continue;
323                  case 'o':                       /* output file */
324 <                        cp++;
305 <                        if (*cp != '=')
324 >                        if (*++cp != '=')
325                                  break;
326 <                        cp++;
326 >                        if (!*++cp)
327 >                                break;
328                          atos(buf, sizeof(buf), cp);
329                          cp = sskip(cp);
330                          if (freopen(buf, "w", stdout) == NULL) {
# Line 319 | Line 339 | char   *nm;
339                  cp = sskip(cp);
340                  nerrs++;
341          }
342 +                                                /* print header? */
343 +        checkhead();
344 +                                                /* issue warnings? */
345          if (nerrs) {
346                  sprintf(errmsg, "(%s): %d error(s) in option line:",
347                                  nm, nerrs);
348                  error(WARNING, errmsg);
349 <                eputs(s);
349 >                wputs(s);
350 >                printf("# %s: the following option line has %d error(s):\n",
351 >                                progname, nerrs);
352          }
353 +                                                /* print pure comment */
354 +        putchar('#'); putchar(' '); fputs(s+2, stdout);
355   }
356 +
357 +
358 + printhead(ac, av)                       /* print out header */
359 + register int  ac;
360 + register char  **av;
361 + {
362 +        putchar('#');
363 +        while (ac-- > 0) {
364 +                putchar(' ');
365 +                fputs(*av++, stdout);
366 +        }
367 +        putchar('\n');
368 + }
369 +
370 +
371 + xobject(fp, nm)                         /* translate an object from fp */
372 + FILE  *fp;
373 + char  *nm;
374 + {
375 +        OBJREC  thisobj;
376 +        char  str[MAXSTR];
377 +        int  doit;
378 +                                        /* read the object */
379 +        if (fgetword(thisillum.altmat, MAXSTR, fp) == NULL)
380 +                goto readerr;
381 +        if (fgetword(str, MAXSTR, fp) == NULL)
382 +                goto readerr;
383 +                                        /* is it an alias? */
384 +        if (!strcmp(str, ALIASID)) {
385 +                if (fgetword(str, MAXSTR, fp) == NULL)
386 +                        goto readerr;
387 +                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
388 +                if (fgetword(str, MAXSTR, fp) == NULL)
389 +                        goto readerr;
390 +                printf(" %s\n", str);
391 +                return;
392 +        }
393 +        thisobj.omod = OVOID;
394 +        if ((thisobj.otype = otype(str)) < 0) {
395 +                sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
396 +                error(USER, errmsg);
397 +        }
398 +        if (fgetword(str, MAXSTR, fp) == NULL)
399 +                goto readerr;
400 +        thisobj.oname = str;
401 +        if (readfargs(&thisobj.oargs, fp) != 1)
402 +                goto readerr;
403 +        thisobj.os = NULL;
404 +                                        /* check for translation */
405 +        switch (matselect) {
406 +        case S_NONE:
407 +                doit = 0;
408 +                break;
409 +        case S_ALL:
410 +                doit = issurface(thisobj.otype);
411 +                break;
412 +        case S_ELEM:
413 +                doit = !strcmp(thisillum.altmat, matcheck);
414 +                break;
415 +        case S_COMPL:
416 +                doit = strcmp(thisillum.altmat, matcheck);
417 +                break;
418 +        }
419 +        if (doit && ofun[thisobj.otype].funp == o_default) {
420 +                sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"",
421 +                                nm, ofun[thisobj.otype].funame, thisobj.oname);
422 +                error(WARNING, errmsg);
423 +                doit = 0;
424 +        }
425 +                                                /* print header? */
426 +        checkhead();
427 +                                                /* process object */
428 +        if (doit)
429 +                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt);
430 +        else
431 +                printobj(thisillum.altmat, &thisobj);
432 +                                                /* free arguments */
433 +        freefargs(&thisobj.oargs);
434 +        return;
435 + readerr:
436 +        sprintf(errmsg, "(%s): error reading scene", nm);
437 +        error(USER, errmsg);
438 + }
439 +
440 +
441 + int
442 + otype(ofname)                   /* get object function number from its name */
443 + char  *ofname;
444 + {
445 +        register int  i;
446 +
447 +        for (i = 0; i < NUMOTYPE; i++)
448 +                if (!strcmp(ofun[i].funame, ofname))
449 +                        return(i);
450 +
451 +        return(-1);             /* not found */
452 + }
453 +
454 +
455 + o_default() {}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines