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.25 by greg, Thu May 26 14:46:21 2005 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Make illum sources for optimizing rendering process
6   */
7  
8 < #include  "mkillum.h"
12 <
8 > #include  <signal.h>
9   #include  <ctype.h>
10  
11 + #include  "platform.h"
12 + #include  "mkillum.h"
13 + #include  "random.h"
14 +
15                                  /* default parameters */
16 < #define  SAMPDENS       128             /* points per projected steradian */
16 > #define  SAMPDENS       48              /* points per projected steradian */
17   #define  NSAMPS         32              /* samples per point */
18   #define  DFLMAT         "illum_mat"     /* material name */
19 + #define  DFLDAT         "illum"         /* data file name */
20                                  /* selection options */
21   #define  S_NONE         0               /* select none */
22   #define  S_ELEM         1               /* select specified element */
# Line 23 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24   #define  S_ALL          3               /* select all */
25  
26                                  /* rtrace command and defaults */
27 < char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-ab", "2",
28 <                "-ad", "256", "-as", "128", "-aa", ".15" };
29 < int  rtargc = 13;
27 > char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-",
28 >                "-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", };
29 > int  rtargc = 14;
30                                  /* overriding rtrace options */
31 < char  *myrtopts[] = { "-I-", "-i-", "-di+", "-ov", "-h-", "-fff", NULL };
31 > char  *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-",
32 >                        "-fff", "-y", "0", NULL };
33  
34 < struct rtproc   rt;             /* our rtrace process */
34 > struct rtproc   rt0;            /* head of rtrace process list */
35  
36   struct illum_args  thisillum = {        /* our illum and default values */
37                  0,
38                  DFLMAT,
39 <                DFLMAT,
39 >                DFLDAT,
40                  0,
41                  VOIDID,
42 +                SAMPDENS,
43                  NSAMPS,
44 +                0.,
45          };
46  
43 int     sampdens = SAMPDENS;    /* sample point density */
47   char    matcheck[MAXSTR];       /* current material to include or exclude */
48   int     matselect = S_ALL;      /* selection criterion */
49  
50   FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
51  
52 + char    persistfn[] = "pfXXXXXX";       /* persist file name */
53 +
54   int     gargc;                  /* global argc */
55   char    **gargv;                /* global argv */
56   #define  progname       gargv[0]
# Line 55 | Line 60 | int    doneheader = 0;         /* printed header yet? */
60  
61   int     warnings = 1;           /* print warnings? */
62  
63 < extern char     *fgetline(), *fgetword(), *sskip(),
64 <                *atos(), *iskip(), *fskip();
65 < extern FILE     *popen();
63 > int done_rprocs(struct rtproc *rtp);
64 > void init(int np);
65 > void filter(register FILE       *infp, char     *name);
66 > void xoptions(char      *s, char        *nm);
67 > void printopts(void);
68 > void printhead(register int  ac, register char  **av);
69 > void xobject(FILE  *fp, char  *nm);
70  
71  
72 < main(argc, argv)                /* compute illum distributions using rtrace */
73 < int     argc;
74 < char    *argv[];
72 > int
73 > main(           /* compute illum distributions using rtrace */
74 >        int     argc,
75 >        char    *argv[]
76 > )
77   {
78 <        extern char     *getenv(), *getpath();
78 >        int     nprocs = 1;
79          char    *rtpath;
80 +        FILE    *fp;
81          register int    i;
82                                  /* set global arguments */
83 <        gargc = argc; gargv = argv;
83 >        gargv = argv;
84 >                                /* check for -n option */
85 >        if (!strcmp(argv[1], "-n")) {
86 >                nprocs = atoi(argv[2]);
87 >                if (nprocs <= 0)
88 >                        error(USER, "illegal number of processes");
89 >                i = 3;
90 >        } else
91 >                i = 1;
92                                  /* set up rtrace command */
93 <        if (argc < 2)
94 <                error(USER, "too few arguments");
95 <        for (i = 1; i < argc-1; i++) {
93 >        for ( ; i < argc; i++) {
94 >                if (argv[i][0] == '<' && argv[i][1] == '\0')
95 >                        break;
96                  rtargv[rtargc++] = argv[i];
97                  if (argv[i][0] == '-' && argv[i][1] == 'w')
98 <                        warnings = !warnings;
98 >                        switch (argv[i][2]) {
99 >                        case '\0':
100 >                                warnings = !warnings;
101 >                                break;
102 >                        case '+':
103 >                        case 'T': case 't':
104 >                        case 'Y': case 'y':
105 >                        case '1':
106 >                                warnings = 1;
107 >                                break;
108 >                        case '-':
109 >                        case 'F': case 'f':
110 >                        case 'N': case 'n':
111 >                        case '0':
112 >                                warnings = 0;
113 >                                break;
114 >                        }
115          }
116 +        gargc = i;
117 +        if (!strcmp(rtargv[--rtargc], "-defaults"))
118 +                nprocs = 0;
119 +        if (nprocs > 1) {       /* add persist file if parallel invocation */
120 +                rtargv[rtargc++] = "-PP";
121 +                rtargv[rtargc++] = mktemp(persistfn);
122 +        }
123 +                                /* add "mandatory" rtrace options */
124          for (i = 0; myrtopts[i] != NULL; i++)
125                  rtargv[rtargc++] = myrtopts[i];
126 <        rtargv[rtargc++] = argv[argc-1];
126 >                                /* finally, put back final argument */
127 >        rtargv[rtargc++] = argv[gargc-1];
128          rtargv[rtargc] = NULL;
129 <                                /* just asking for defaults? */
130 <        if (!strcmp(argv[argc-1], "-defaults")) {
129 >        if (!nprocs) {          /* just asking for defaults? */
130 >                printopts(); fflush(stdout);
131                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
132                  if (rtpath == NULL) {
133                          eputs(rtargv[0]);
# Line 90 | Line 135 | char   *argv[];
135                          exit(1);
136                  }
137                  execv(rtpath, rtargv);
138 <                perror(rtpath);
138 >                perror(rtpath); /* execv() should not return */
139                  exit(1);
140          }
141 +        if (gargc < 2 || argv[gargc-1][0] == '-')
142 +                error(USER, "missing octree argument");
143                                  /* else initialize and run our calculation */
144 <        init();
145 <        filter(stdin, "standard input");
144 >        init(nprocs);
145 >        if (gargc+1 < argc)
146 >                for (i = gargc+1; i < argc; i++) {
147 >                        if ((fp = fopen(argv[i], "r")) == NULL) {
148 >                                sprintf(errmsg,
149 >                                "cannot open scene file \"%s\"", argv[i]);
150 >                                error(SYSTEM, errmsg);
151 >                        }
152 >                        filter(fp, argv[i]);
153 >                        fclose(fp);
154 >                }
155 >        else
156 >                filter(stdin, "standard input");
157          quit(0);
158   }
159  
160 + static void
161 + killpersist(void)                       /* kill persistent rtrace process */
162 + {
163 +        FILE    *fp = fopen(persistfn, "r");
164 +        int     pid;
165  
166 < init()                          /* start rtrace and set up buffers */
166 >        if (fp == NULL)
167 >                return;
168 >        if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
169 >                unlink(persistfn);
170 >        fclose(fp);
171 > }
172 >
173 > int
174 > done_rprocs(struct rtproc *rtp)
175   {
176 <        int     maxbytes;
176 >        int     st0, st1 = 0;
177  
178 <        maxbytes = open_process(rt.pd, rtargv);
179 <        if (maxbytes == 0) {
180 <                eputs(rtargv[0]);
181 <                eputs(": command not found\n");
111 <                exit(1);
178 >        if (rtp->next != NULL) {        /* close last opened first! */
179 >                st1 = done_rprocs(rtp->next);
180 >                free((void *)rtp->next);
181 >                rtp->next = NULL;
182          }
183 <        if (maxbytes < 0)
184 <                error(SYSTEM, "cannot start rtrace process");
185 <        rt.bsiz = maxbytes/(6*sizeof(float));
186 <        rt.buf = (float *)malloc(rt.bsiz*(6*sizeof(float)));
187 <        if (rt.buf == NULL)
188 <                error(SYSTEM, "out of memory in init");
119 <        rt.bsiz--;                      /* allow for flush ray */
120 <        rt.nrays = 0;
183 >        st0 = close_process(&rtp->pd);
184 >        if (st0 < 0)
185 >                error(WARNING, "unknown return status from rtrace process");
186 >        else if (st0 > 0)
187 >                return(st0);
188 >        return(st1);
189   }
190  
191 <
192 < quit(status)                    /* exit with status */
125 < int  status;
191 > void
192 > quit(int status)                        /* exit with status */
193   {
194          int     rtstat;
195  
196 <        rtstat = close_process(rt.pd);
196 >        if (rt0.next != NULL)           /* terminate persistent rtrace */
197 >                killpersist();
198 >                                        /* clean up rtrace process(es) */
199 >        rtstat = done_rprocs(&rt0);
200          if (status == 0)
201 <                if (rtstat < 0)
132 <                        error(WARNING,
133 <                        "unknown return status from rtrace process");
134 <                else
135 <                        status = rtstat;
201 >                status = rtstat;
202          exit(status);
203   }
204  
205 + void
206 + init(int np)                            /* start rtrace and set up buffers */
207 + {
208 +        struct rtproc   *rtp;
209 +        int     i;
210 +        int     maxbytes;
211 +                                        /* set up object functions */
212 +        ofun[OBJ_FACE].funp = o_face;
213 +        ofun[OBJ_SPHERE].funp = o_sphere;
214 +        ofun[OBJ_RING].funp = o_ring;
215 +                                        /* set up signal handling */
216 +        signal(SIGINT, quit);
217 + #ifdef SIGHUP
218 +        signal(SIGHUP, quit);
219 + #endif
220 + #ifdef SIGTERM
221 +        signal(SIGTERM, quit);
222 + #endif
223 + #ifdef SIGPIPE
224 +        signal(SIGPIPE, quit);
225 + #endif
226 +        rtp = &rt0;                     /* start rtrace process(es) */
227 +        for (i = 0; i++ < np; ) {
228 +                errno = 0;
229 +                maxbytes = open_process(&rtp->pd, rtargv);
230 +                if (maxbytes == 0) {
231 +                        eputs(rtargv[0]);
232 +                        eputs(": command not found\n");
233 +                        exit(1);
234 +                }
235 +                if (maxbytes < 0)
236 +                        error(SYSTEM, "cannot start rtrace process");
237 +                rtp->bsiz = maxbytes/(6*sizeof(float));
238 +                rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
239 +                rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
240 +                if (rtp->buf == NULL || rtp->dest == NULL)
241 +                        error(SYSTEM, "out of memory in init");
242 +                rtp->nrays = 0;
243 +                if (i == np)            /* last process? */
244 +                        break;
245 +                if (np > 1)
246 +                        sleep(2);       /* wait for persist file */
247 +                rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
248 +                if (rtp->next == NULL)
249 +                        error(SYSTEM, "out of memory in init");
250 +                rtp = rtp->next;
251 +        }
252 +        rtp->next = NULL;
253 +                                        /* set up urand */
254 +        initurand(16384);
255 + }
256  
257 < eputs(s)                                /* put string to stderr */
258 < register char  *s;
257 >
258 > void
259 > eputs(                          /* put string to stderr */
260 >        register char  *s
261 > )
262   {
263          static int  midline = 0;
264  
# Line 152 | Line 272 | register char  *s;
272   }
273  
274  
275 + void
276   wputs(s)                        /* print warning if enabled */
277   char  *s;
278   {
# Line 160 | Line 281 | char  *s;
281   }
282  
283  
284 < filter(infp, name)              /* process stream */
285 < register FILE   *infp;
286 < char    *name;
284 > void
285 > filter(         /* process stream */
286 >        register FILE   *infp,
287 >        char    *name
288 > )
289   {
290          char    buf[512];
291          FILE    *pfp;
# Line 192 | Line 315 | char   *name;
315   }
316  
317  
318 < xoptions(s, nm)                 /* process options in string s */
319 < char    *s;
320 < char    *nm;
318 > void
319 > xoptions(                       /* process options in string s */
320 >        char    *s,
321 >        char    *nm
322 > )
323   {
324          extern FILE     *freopen();
325          char    buf[64];
# Line 211 | Line 336 | char   *nm;
336                  case ' ':
337                  case '\t':
338                  case '\n':
339 +                case '\r':
340 +                case '\f':
341                          cp++;
342                          continue;
343                  case 'm':                       /* material name */
344                          if (*++cp != '=')
345                                  break;
346 <                        if (!*++cp)
346 >                        if (!*++cp || isspace(*cp))
347                                  break;
348                          atos(thisillum.matname, MAXSTR, cp);
349                          cp = sskip(cp);
# Line 228 | Line 355 | char   *nm;
355                  case 'f':                       /* data file name */
356                          if (*++cp != '=')
357                                  break;
358 <                        if (!*++cp) {
358 >                        if (!*++cp || isspace(*cp)) {
359 >                                strcpy(thisillum.datafile,thisillum.matname);
360 >                                thisillum.dfnum = 0;
361                                  thisillum.flags &= ~IL_DATCLB;
362                                  continue;
363                          }
# Line 239 | Line 368 | char   *nm;
368                          continue;
369                  case 'i':                       /* include material */
370                  case 'e':                       /* exclude material */
371 <                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
243 <                        if (*++cp != '=')
371 >                        if (cp[1] != '=')
372                                  break;
373 <                        atos(matcheck, MAXSTR, ++cp);
373 >                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
374 >                        cp += 2;
375 >                        atos(matcheck, MAXSTR, cp);
376                          cp = sskip(cp);
377                          continue;
378                  case 'a':                       /* use everything */
# Line 258 | Line 388 | char   *nm;
388                                  break;
389                          switch (*++cp) {
390                          case 'a':                       /* average */
391 <                                thisillum.flags |= IL_COLAVG;
392 <                                thisillum.flags &= ~IL_COLDST;
391 >                                thisillum.flags = (thisillum.flags|IL_COLAVG)
392 >                                                        & ~IL_COLDST;
393                                  break;
394                          case 'd':                       /* distribution */
395 <                                thisillum.flags |= IL_COLDST;
266 <                                thisillum.flags &= ~IL_COLAVG;
395 >                                thisillum.flags |= (IL_COLDST|IL_COLAVG);
396                                  break;
397                          case 'n':                       /* none */
398                                  thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
# Line 276 | Line 405 | char   *nm;
405                  case 'd':                       /* point sample density */
406                          if (*++cp != '=')
407                                  break;
408 <                        if (!isintd(++cp, " \t\n"))
408 >                        if (!isintd(++cp, " \t\n\r"))
409                                  break;
410 <                        sampdens = atoi(cp);
410 >                        thisillum.sampdens = atoi(cp);
411                          cp = sskip(cp);
412                          continue;
413                  case 's':                       /* point super-samples */
414                          if (*++cp != '=')
415                                  break;
416 <                        if (!isintd(++cp, " \t\n"))
416 >                        if (!isintd(++cp, " \t\n\r"))
417                                  break;
418                          thisillum.nsamps = atoi(cp);
419                          cp = sskip(cp);
420                          continue;
421 +                case 'l':                       /* light sources */
422 +                        cp++;
423 +                        if (*cp == '+')
424 +                                thisillum.flags |= IL_LIGHT;
425 +                        else if (*cp == '-')
426 +                                thisillum.flags &= ~IL_LIGHT;
427 +                        else
428 +                                break;
429 +                        cp++;
430 +                        continue;
431 +                case 'b':                       /* brightness */
432 +                        if (*++cp != '=')
433 +                                break;
434 +                        if (!isfltd(++cp, " \t\n\r"))
435 +                                break;
436 +                        thisillum.minbrt = atof(cp);
437 +                        if (thisillum.minbrt < 0.)
438 +                                thisillum.minbrt = 0.;
439 +                        cp = sskip(cp);
440 +                        continue;
441                  case 'o':                       /* output file */
442                          if (*++cp != '=')
443                                  break;
444 <                        if (!*++cp)
444 >                        if (!*++cp || isspace(*cp))
445                                  break;
446                          atos(buf, sizeof(buf), cp);
447                          cp = sskip(cp);
# Line 303 | Line 452 | char   *nm;
452                          }
453                          doneheader = 0;
454                          continue;
455 +                case '!':                       /* processed file! */
456 +                        sprintf(errmsg, "(%s): already processed!", nm);
457 +                        error(WARNING, errmsg);
458 +                        matselect = S_NONE;
459 +                        return;
460                  }
461          opterr:                                 /* skip faulty option */
462 <                cp = sskip(cp);
462 >                while (*cp && !isspace(*cp))
463 >                        cp++;
464                  nerrs++;
465          }
466                                                  /* print header? */
# Line 320 | Line 475 | char   *nm;
475                                  progname, nerrs);
476          }
477                                                  /* print pure comment */
478 <        putchar('#'); putchar(' '); fputs(s+2, stdout);
478 >        printf("# %s", s+2);
479   }
480  
481 + void
482 + printopts(void)                 /* print out option default values */
483 + {
484 +        printf("m=%-15s\t\t# material name\n", thisillum.matname);
485 +        printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
486 +        if (thisillum.flags & IL_COLAVG)
487 +                if (thisillum.flags & IL_COLDST)
488 +                        printf("c=d\t\t\t\t# color distribution\n");
489 +                else
490 +                        printf("c=a\t\t\t\t# color average\n");
491 +        else
492 +                printf("c=n\t\t\t\t# color none\n");
493 +        if (thisillum.flags & IL_LIGHT)
494 +                printf("l+\t\t\t\t# light type on\n");
495 +        else
496 +                printf("l-\t\t\t\t# light type off\n");
497 +        printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
498 +        printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
499 +        printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
500 + }
501  
502 < printhead(ac, av)                       /* print out header */
503 < register int  ac;
504 < register char  **av;
502 >
503 > void
504 > printhead(                      /* print out header */
505 >        register int  ac,
506 >        register char  **av
507 > )
508   {
509          putchar('#');
510          while (ac-- > 0) {
511                  putchar(' ');
512                  fputs(*av++, stdout);
513          }
514 <        putchar('\n');
514 >        fputs("\n#@mkillum !\n", stdout);
515   }
516  
517  
518 < xobject(fp, nm)                         /* translate an object from fp */
519 < FILE  *fp;
520 < char  *nm;
518 > void
519 > xobject(                                /* translate an object from fp */
520 >        FILE  *fp,
521 >        char  *nm
522 > )
523   {
524          OBJREC  thisobj;
525          char  str[MAXSTR];
# Line 350 | Line 530 | char  *nm;
530          if (fgetword(str, MAXSTR, fp) == NULL)
531                  goto readerr;
532                                          /* is it an alias? */
533 <        if (!strcmp(str, ALIASID)) {
533 >        if (!strcmp(str, ALIASKEY)) {
534                  if (fgetword(str, MAXSTR, fp) == NULL)
535                          goto readerr;
536 <                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
536 >                printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
537                  if (fgetword(str, MAXSTR, fp) == NULL)
538                          goto readerr;
539 <                printf(" %s\n", str);
539 >                printf("\t%s\n", str);
540                  return;
541          }
542 <        thisobj.omod = OVOID;
542 >        thisobj.omod = OVOID;           /* unused field */
543          if ((thisobj.otype = otype(str)) < 0) {
544                  sprintf(errmsg, "(%s): unknown type \"%s\"", nm, str);
545                  error(USER, errmsg);
# Line 376 | Line 556 | char  *nm;
556                  doit = 0;
557                  break;
558          case S_ALL:
559 <                doit = issurface(thisobj.otype);
559 >                doit = 1;
560                  break;
561          case S_ELEM:
562                  doit = !strcmp(thisillum.altmat, matcheck);
# Line 385 | Line 565 | char  *nm;
565                  doit = strcmp(thisillum.altmat, matcheck);
566                  break;
567          }
568 <        if (doit)                               /* make sure we can do it */
389 <                switch (thisobj.otype) {
390 <                case OBJ_SPHERE:
391 <                case OBJ_FACE:
392 <                case OBJ_RING:
393 <                        break;
394 <                default:
395 <                        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;
401 <                        break;
402 <                }
568 >        doit = doit && issurface(thisobj.otype);
569                                                  /* print header? */
570          checkhead();
571                                                  /* process object */
572          if (doit)
573 <                mkillum(&thisobj, &thisillum, &rt);
573 >                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
574          else
575                  printobj(thisillum.altmat, &thisobj);
576                                                  /* free arguments */
# Line 414 | Line 580 | readerr:
580          sprintf(errmsg, "(%s): error reading scene", nm);
581          error(USER, errmsg);
582   }
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