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.4 by greg, Wed Jul 24 12:22:03 1991 UTC vs.
Revision 2.23 by greg, Thu Oct 28 00:50:47 2004 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  
11 #include  "mkillum.h"
12
8   #include  <signal.h>
14
9   #include  <ctype.h>
10  
11 + #include  "platform.h"
12 + #include  "rtprocess.h"
13 + #include  "mkillum.h"
14 + #include  "random.h"
15 +
16                                  /* default parameters */
17 < #define  SAMPDENS       128             /* points per projected steradian */
17 > #define  SAMPDENS       48              /* points per projected steradian */
18   #define  NSAMPS         32              /* samples per point */
19   #define  DFLMAT         "illum_mat"     /* material name */
20 + #define  DFLDAT         "illum"         /* data file name */
21                                  /* selection options */
22   #define  S_NONE         0               /* select none */
23   #define  S_ELEM         1               /* select specified element */
# Line 25 | Line 25 | static char SCCSid[] = "$SunId$ LBL";
25   #define  S_ALL          3               /* select all */
26  
27                                  /* rtrace command and defaults */
28 < char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-di+",
29 <                "-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", };
28 > char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-",
29 >                "-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", };
30   int  rtargc = 14;
31                                  /* overriding rtrace options */
32 < char  *myrtopts[] = { "-I-", "-i-", "-ov", "-h-", "-fff", NULL };
32 > char  *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-",
33 >                        "-fff", "-y", "0", NULL };
34  
35 < struct rtproc   rt;             /* our rtrace process */
35 > struct rtproc   rt0;            /* head of rtrace process list */
36  
37   struct illum_args  thisillum = {        /* our illum and default values */
38                  0,
39                  DFLMAT,
40 <                DFLMAT,
40 >                DFLDAT,
41                  0,
42                  VOIDID,
43                  SAMPDENS,
44                  NSAMPS,
45 +                0.,
46          };
47  
48   char    matcheck[MAXSTR];       /* current material to include or exclude */
# Line 48 | Line 50 | int    matselect = S_ALL;      /* selection criterion */
50  
51   FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
52  
53 + char    persistfn[] = "pfXXXXXX";       /* persist file name */
54 +
55   int     gargc;                  /* global argc */
56   char    **gargv;                /* global argv */
57   #define  progname       gargv[0]
# Line 57 | Line 61 | int    doneheader = 0;         /* printed header yet? */
61  
62   int     warnings = 1;           /* print warnings? */
63  
64 < extern char     *fgetline(), *fgetword(), *sskip(),
65 <                *atos(), *iskip(), *fskip();
66 < extern FILE     *popen();
64 > int done_rprocs(struct rtproc *rtp);
65 > void init(int np);
66 > void filter(register FILE       *infp, char     *name);
67 > void xoptions(char      *s, char        *nm);
68 > void printopts(void);
69 > void printhead(register int  ac, register char  **av);
70 > void xobject(FILE  *fp, char  *nm);
71  
72  
73 < main(argc, argv)                /* compute illum distributions using rtrace */
74 < int     argc;
75 < char    *argv[];
73 > int
74 > main(           /* compute illum distributions using rtrace */
75 >        int     argc,
76 >        char    *argv[]
77 > )
78   {
79 <        extern char     *getenv(), *getpath();
79 >        int     nprocs = 1;
80          char    *rtpath;
81          FILE    *fp;
82          register int    i;
83                                  /* set global arguments */
84          gargv = argv;
85 +                                /* check for -n option */
86 +        if (!strcmp(argv[1], "-n")) {
87 +                nprocs = atoi(argv[2]);
88 +                if (nprocs <= 0)
89 +                        error(USER, "illegal number of processes");
90 +                i = 3;
91 +        } else
92 +                i = 1;
93                                  /* set up rtrace command */
94 <        for (i = 1; i < argc; i++) {
95 <                if (argv[i][0] == '<' && !argv[i][1])
94 >        for ( ; i < argc; i++) {
95 >                if (argv[i][0] == '<' && argv[i][1] == '\0')
96                          break;
97                  rtargv[rtargc++] = argv[i];
98                  if (argv[i][0] == '-' && argv[i][1] == 'w')
99 <                        warnings = !warnings;
99 >                        switch (argv[i][2]) {
100 >                        case '\0':
101 >                                warnings = !warnings;
102 >                                break;
103 >                        case '+':
104 >                        case 'T': case 't':
105 >                        case 'Y': case 'y':
106 >                        case '1':
107 >                                warnings = 1;
108 >                                break;
109 >                        case '-':
110 >                        case 'F': case 'f':
111 >                        case 'N': case 'n':
112 >                        case '0':
113 >                                warnings = 0;
114 >                                break;
115 >                        }
116          }
117 <        if ((gargc = i) < 2)
118 <                error(USER, "too few arguments");
119 <        rtargc--;
117 >        gargc = i;
118 >        if (!strcmp(rtargv[--rtargc], "-defaults"))
119 >                nprocs = 0;
120 >        if (nprocs > 1) {       /* add persist file if parallel invocation */
121 >                rtargv[rtargc++] = "-PP";
122 >                rtargv[rtargc++] = mktemp(persistfn);
123 >        }
124 >                                /* add "mandatory" rtrace options */
125          for (i = 0; myrtopts[i] != NULL; i++)
126                  rtargv[rtargc++] = myrtopts[i];
127 +                                /* finally, put back final argument */
128          rtargv[rtargc++] = argv[gargc-1];
129          rtargv[rtargc] = NULL;
130 <                                /* just asking for defaults? */
131 <        if (!strcmp(argv[gargc-1], "-defaults")) {
130 >        if (!nprocs) {          /* just asking for defaults? */
131 >                printopts(); fflush(stdout);
132                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
133                  if (rtpath == NULL) {
134                          eputs(rtargv[0]);
# Line 96 | Line 136 | char   *argv[];
136                          exit(1);
137                  }
138                  execv(rtpath, rtargv);
139 <                perror(rtpath);
139 >                perror(rtpath); /* execv() should not return */
140                  exit(1);
141          }
142 +        if (gargc < 2 || argv[gargc-1][0] == '-')
143 +                error(USER, "missing octree argument");
144                                  /* else initialize and run our calculation */
145 <        init();
145 >        init(nprocs);
146          if (gargc+1 < argc)
147                  for (i = gargc+1; i < argc; i++) {
148 <                        if ((fp = fopen(argv[i], "r")) == NULL {
148 >                        if ((fp = fopen(argv[i], "r")) == NULL) {
149                                  sprintf(errmsg,
150                                  "cannot open scene file \"%s\"", argv[i]);
151                                  error(SYSTEM, errmsg);
# Line 116 | Line 158 | char   *argv[];
158          quit(0);
159   }
160  
161 + static void
162 + killpersist(void)                       /* kill persistent rtrace process */
163 + {
164 +        FILE    *fp = fopen(persistfn, "r");
165 +        int     pid;
166  
167 < quit(status)                    /* exit with status */
168 < int  status;
167 >        if (fp == NULL)
168 >                return;
169 >        if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
170 >                unlink(persistfn);
171 >        fclose(fp);
172 > }
173 >
174 > int
175 > done_rprocs(struct rtproc *rtp)
176   {
177 +        int     st0, st1 = 0;
178 +
179 +        if (rtp->next != NULL) {        /* close last opened first! */
180 +                st1 = done_rprocs(rtp->next);
181 +                free((void *)rtp->next);
182 +                rtp->next = NULL;
183 +        }
184 +        st0 = close_process(&rtp->pd);
185 +        if (st0 < 0)
186 +                error(WARNING, "unknown return status from rtrace process");
187 +        else if (st0 > 0)
188 +                return(st0);
189 +        return(st1);
190 + }
191 +
192 + void
193 + quit(int status)                        /* exit with status */
194 + {
195          int     rtstat;
196  
197 <        rtstat = close_process(rt.pd);
197 >        if (rt0.next != NULL)           /* terminate persistent rtrace */
198 >                killpersist();
199 >                                        /* clean up rtrace process(es) */
200 >        rtstat = done_rprocs(&rt0);
201          if (status == 0)
202 <                if (rtstat < 0)
128 <                        error(WARNING,
129 <                        "unknown return status from rtrace process");
130 <                else
131 <                        status = rtstat;
202 >                status = rtstat;
203          exit(status);
204   }
205  
206 <
207 < init()                          /* start rtrace and set up buffers */
206 > void
207 > init(int np)                            /* start rtrace and set up buffers */
208   {
209 <        extern int  o_face(), o_sphere(), o_ring();
209 >        struct rtproc   *rtp;
210 >        int     i;
211          int     maxbytes;
212                                          /* set up object functions */
213          ofun[OBJ_FACE].funp = o_face;
214          ofun[OBJ_SPHERE].funp = o_sphere;
215          ofun[OBJ_RING].funp = o_ring;
216                                          /* set up signal handling */
217 + #ifdef SIGPIPE /* not present on Windows */
218          signal(SIGPIPE, quit);
219 <                                        /* start rtrace process */
220 <        maxbytes = open_process(rt.pd, rtargv);
221 <        if (maxbytes == 0) {
222 <                eputs(rtargv[0]);
223 <                eputs(": command not found\n");
224 <                exit(1);
219 > #endif
220 >        rtp = &rt0;                     /* start rtrace process(es) */
221 >        for (i = 0; i++ < np; ) {
222 >                errno = 0;
223 >                maxbytes = open_process(&rtp->pd, rtargv);
224 >                if (maxbytes == 0) {
225 >                        eputs(rtargv[0]);
226 >                        eputs(": command not found\n");
227 >                        exit(1);
228 >                }
229 >                if (maxbytes < 0)
230 >                        error(SYSTEM, "cannot start rtrace process");
231 >                if (!i && np > 1)
232 >                        sleep(2);       /* wait for persist file */
233 >                rtp->bsiz = maxbytes/(6*sizeof(float));
234 >                rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
235 >                rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
236 >                if (rtp->buf == NULL || rtp->dest == NULL)
237 >                        error(SYSTEM, "out of memory in init");
238 >                rtp->nrays = 0;
239 >                if (i == np)            /* last process? */
240 >                        break;
241 >                rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
242 >                if (rtp->next == NULL)
243 >                        error(SYSTEM, "out of memory in init");
244 >                rtp = rtp->next;
245          }
246 <        if (maxbytes < 0)
247 <                error(SYSTEM, "cannot start rtrace process");
248 <        rt.bsiz = maxbytes/(6*sizeof(float));
156 <        rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--);
157 <        rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz);
158 <        if (rt.buf == NULL || rt.dest == NULL)
159 <                error(SYSTEM, "out of memory in init");
160 <        rt.nrays = 0;
246 >        rtp->next = NULL;
247 >                                        /* set up urand */
248 >        initurand(16384);
249   }
250  
251  
252 < eputs(s)                                /* put string to stderr */
253 < register char  *s;
252 > void
253 > eputs(                          /* put string to stderr */
254 >        register char  *s
255 > )
256   {
257          static int  midline = 0;
258  
# Line 176 | Line 266 | register char  *s;
266   }
267  
268  
269 + void
270   wputs(s)                        /* print warning if enabled */
271   char  *s;
272   {
# Line 184 | Line 275 | char  *s;
275   }
276  
277  
278 < filter(infp, name)              /* process stream */
279 < register FILE   *infp;
280 < char    *name;
278 > void
279 > filter(         /* process stream */
280 >        register FILE   *infp,
281 >        char    *name
282 > )
283   {
284          char    buf[512];
285          FILE    *pfp;
# Line 216 | Line 309 | char   *name;
309   }
310  
311  
312 < xoptions(s, nm)                 /* process options in string s */
313 < char    *s;
314 < char    *nm;
312 > void
313 > xoptions(                       /* process options in string s */
314 >        char    *s,
315 >        char    *nm
316 > )
317   {
318          extern FILE     *freopen();
319          char    buf[64];
# Line 235 | Line 330 | char   *nm;
330                  case ' ':
331                  case '\t':
332                  case '\n':
333 +                case '\r':
334 +                case '\f':
335                          cp++;
336                          continue;
337                  case 'm':                       /* material name */
338                          if (*++cp != '=')
339                                  break;
340 <                        if (!*++cp)
340 >                        if (!*++cp || isspace(*cp))
341                                  break;
342                          atos(thisillum.matname, MAXSTR, cp);
343                          cp = sskip(cp);
# Line 252 | Line 349 | char   *nm;
349                  case 'f':                       /* data file name */
350                          if (*++cp != '=')
351                                  break;
352 <                        if (!*++cp) {
352 >                        if (!*++cp || isspace(*cp)) {
353 >                                strcpy(thisillum.datafile,thisillum.matname);
354 >                                thisillum.dfnum = 0;
355                                  thisillum.flags &= ~IL_DATCLB;
356                                  continue;
357                          }
# Line 263 | Line 362 | char   *nm;
362                          continue;
363                  case 'i':                       /* include material */
364                  case 'e':                       /* exclude material */
365 <                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
267 <                        if (*++cp != '=')
365 >                        if (cp[1] != '=')
366                                  break;
367 <                        atos(matcheck, MAXSTR, ++cp);
367 >                        matselect = (*cp == 'i') ? S_ELEM : S_COMPL;
368 >                        cp += 2;
369 >                        atos(matcheck, MAXSTR, cp);
370                          cp = sskip(cp);
371                          continue;
372                  case 'a':                       /* use everything */
# Line 282 | Line 382 | char   *nm;
382                                  break;
383                          switch (*++cp) {
384                          case 'a':                       /* average */
385 <                                thisillum.flags |= IL_COLAVG;
386 <                                thisillum.flags &= ~IL_COLDST;
385 >                                thisillum.flags = (thisillum.flags|IL_COLAVG)
386 >                                                        & ~IL_COLDST;
387                                  break;
388                          case 'd':                       /* distribution */
389 <                                thisillum.flags |= IL_COLDST;
290 <                                thisillum.flags &= ~IL_COLAVG;
389 >                                thisillum.flags |= (IL_COLDST|IL_COLAVG);
390                                  break;
391                          case 'n':                       /* none */
392                                  thisillum.flags &= ~(IL_COLAVG|IL_COLDST);
# Line 300 | Line 399 | char   *nm;
399                  case 'd':                       /* point sample density */
400                          if (*++cp != '=')
401                                  break;
402 <                        if (!isintd(++cp, " \t\n"))
402 >                        if (!isintd(++cp, " \t\n\r"))
403                                  break;
404                          thisillum.sampdens = atoi(cp);
405                          cp = sskip(cp);
# Line 308 | Line 407 | char   *nm;
407                  case 's':                       /* point super-samples */
408                          if (*++cp != '=')
409                                  break;
410 <                        if (!isintd(++cp, " \t\n"))
410 >                        if (!isintd(++cp, " \t\n\r"))
411                                  break;
412                          thisillum.nsamps = atoi(cp);
413                          cp = sskip(cp);
# Line 323 | Line 422 | char   *nm;
422                                  break;
423                          cp++;
424                          continue;
425 +                case 'b':                       /* brightness */
426 +                        if (*++cp != '=')
427 +                                break;
428 +                        if (!isfltd(++cp, " \t\n\r"))
429 +                                break;
430 +                        thisillum.minbrt = atof(cp);
431 +                        if (thisillum.minbrt < 0.)
432 +                                thisillum.minbrt = 0.;
433 +                        cp = sskip(cp);
434 +                        continue;
435                  case 'o':                       /* output file */
436                          if (*++cp != '=')
437                                  break;
438 <                        if (!*++cp)
438 >                        if (!*++cp || isspace(*cp))
439                                  break;
440                          atos(buf, sizeof(buf), cp);
441                          cp = sskip(cp);
# Line 337 | Line 446 | char   *nm;
446                          }
447                          doneheader = 0;
448                          continue;
449 +                case '!':                       /* processed file! */
450 +                        sprintf(errmsg, "(%s): already processed!", nm);
451 +                        error(WARNING, errmsg);
452 +                        matselect = S_NONE;
453 +                        return;
454                  }
455          opterr:                                 /* skip faulty option */
456 <                cp = sskip(cp);
456 >                while (*cp && !isspace(*cp))
457 >                        cp++;
458                  nerrs++;
459          }
460                                                  /* print header? */
# Line 357 | Line 472 | char   *nm;
472          printf("# %s", s+2);
473   }
474  
475 + void
476 + printopts(void)                 /* print out option default values */
477 + {
478 +        printf("m=%-15s\t\t# material name\n", thisillum.matname);
479 +        printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
480 +        if (thisillum.flags & IL_COLAVG)
481 +                if (thisillum.flags & IL_COLDST)
482 +                        printf("c=d\t\t\t\t# color distribution\n");
483 +                else
484 +                        printf("c=a\t\t\t\t# color average\n");
485 +        else
486 +                printf("c=n\t\t\t\t# color none\n");
487 +        if (thisillum.flags & IL_LIGHT)
488 +                printf("l+\t\t\t\t# light type on\n");
489 +        else
490 +                printf("l-\t\t\t\t# light type off\n");
491 +        printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
492 +        printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
493 +        printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
494 + }
495  
496 < printhead(ac, av)                       /* print out header */
497 < register int  ac;
498 < register char  **av;
496 >
497 > void
498 > printhead(                      /* print out header */
499 >        register int  ac,
500 >        register char  **av
501 > )
502   {
503          putchar('#');
504          while (ac-- > 0) {
505                  putchar(' ');
506                  fputs(*av++, stdout);
507          }
508 <        putchar('\n');
508 >        fputs("\n#@mkillum !\n", stdout);
509   }
510  
511  
512 < xobject(fp, nm)                         /* translate an object from fp */
513 < FILE  *fp;
514 < char  *nm;
512 > void
513 > xobject(                                /* translate an object from fp */
514 >        FILE  *fp,
515 >        char  *nm
516 > )
517   {
518          OBJREC  thisobj;
519          char  str[MAXSTR];
# Line 384 | Line 524 | char  *nm;
524          if (fgetword(str, MAXSTR, fp) == NULL)
525                  goto readerr;
526                                          /* is it an alias? */
527 <        if (!strcmp(str, ALIASID)) {
527 >        if (!strcmp(str, ALIASKEY)) {
528                  if (fgetword(str, MAXSTR, fp) == NULL)
529                          goto readerr;
530 <                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
530 >                printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
531                  if (fgetword(str, MAXSTR, fp) == NULL)
532                          goto readerr;
533                  printf("\t%s\n", str);
# Line 424 | Line 564 | char  *nm;
564          checkhead();
565                                                  /* process object */
566          if (doit)
567 <                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
567 >                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
568          else
569                  printobj(thisillum.altmat, &thisobj);
570                                                  /* free arguments */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines