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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines