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.7 by greg, Thu Jul 25 12:52:16 1991 UTC vs.
Revision 2.22 by schorsch, Sat Oct 23 18:55:52 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(), *strcpy();
66 < extern FILE     *popen();
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          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 <        for (i = 1; i < argc; i++) {
94 <                if (argv[i][0] == '<' && !argv[i][1])
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 <        if ((gargc = i) < 2)
117 <                error(USER, "too few arguments");
118 <        rtargc--;
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 +                                /* finally, put back final argument */
127          rtargv[rtargc++] = argv[gargc-1];
128          rtargv[rtargc] = NULL;
129 <                                /* just asking for defaults? */
130 <        if (!strcmp(argv[gargc-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 96 | 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();
144 >        init(nprocs);
145          if (gargc+1 < argc)
146                  for (i = gargc+1; i < argc; i++) {
147                          if ((fp = fopen(argv[i], "r")) == NULL) {
# Line 116 | Line 157 | char   *argv[];
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 < quit(status)                    /* exit with status */
167 < int  status;
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 > void
174 > quit(int status)                        /* exit with status */
175   {
176 +        struct rtproc   *rtp;
177          int     rtstat;
178  
179 <        rtstat = close_process(rt.pd);
180 <        if (status == 0)
181 <                if (rtstat < 0)
182 <                        error(WARNING,
183 <                        "unknown return status from rtrace process");
184 <                else
185 <                        status = rtstat;
179 >        if (rt0.next != NULL)           /* terminate persistent rtrace */
180 >                killpersist();
181 >                                        /* clean up rtrace process(es) */
182 >        for (rtp = &rt0; rtp != NULL; rtp = rtp->next) {
183 >                rtstat = close_process(&rtp->pd);
184 >                if (status == 0) {
185 >                        if (rtstat < 0)
186 >                                error(WARNING,
187 >                                "unknown return status from rtrace process");
188 >                        else
189 >                                status = rtstat;
190 >                }
191 >        }
192          exit(status);
193   }
194  
195 <
196 < init()                          /* start rtrace and set up buffers */
195 > void
196 > init(int np)                            /* start rtrace and set up buffers */
197   {
198 <        extern int  o_face(), o_sphere(), o_ring();
198 >        struct rtproc   *rtp;
199 >        int     i;
200          int     maxbytes;
201                                          /* set up object functions */
202          ofun[OBJ_FACE].funp = o_face;
203          ofun[OBJ_SPHERE].funp = o_sphere;
204          ofun[OBJ_RING].funp = o_ring;
205                                          /* set up signal handling */
206 + #ifdef SIGPIPE /* not present on Windows */
207          signal(SIGPIPE, quit);
208 <                                        /* start rtrace process */
209 <        maxbytes = open_process(rt.pd, rtargv);
210 <        if (maxbytes == 0) {
211 <                eputs(rtargv[0]);
212 <                eputs(": command not found\n");
213 <                exit(1);
208 > #endif
209 >        rtp = &rt0;                     /* start rtrace process(es) */
210 >        for (i = 0; i++ < np; ) {
211 >                errno = 0;
212 >                maxbytes = open_process(&rtp->pd, rtargv);
213 >                if (maxbytes == 0) {
214 >                        eputs(rtargv[0]);
215 >                        eputs(": command not found\n");
216 >                        exit(1);
217 >                }
218 >                if (maxbytes < 0)
219 >                        error(SYSTEM, "cannot start rtrace process");
220 >                if (!i && np > 1)
221 >                        sleep(2);       /* wait for persist file */
222 >                rtp->bsiz = maxbytes/(6*sizeof(float));
223 >                rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
224 >                rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
225 >                if (rtp->buf == NULL || rtp->dest == NULL)
226 >                        error(SYSTEM, "out of memory in init");
227 >                rtp->nrays = 0;
228 >                if (i == np)            /* last process? */
229 >                        break;
230 >                rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
231 >                if (rtp->next == NULL)
232 >                        error(SYSTEM, "out of memory in init");
233 >                rtp = rtp->next;
234          }
235 <        if (maxbytes < 0)
154 <                error(SYSTEM, "cannot start rtrace process");
155 <        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;
235 >        rtp->next = NULL;
236                                          /* set up urand */
237 <        initurand(2048);
237 >        initurand(16384);
238   }
239  
240  
241 < eputs(s)                                /* put string to stderr */
242 < register char  *s;
241 > void
242 > eputs(                          /* put string to stderr */
243 >        register char  *s
244 > )
245   {
246          static int  midline = 0;
247  
# Line 178 | Line 255 | register char  *s;
255   }
256  
257  
258 + void
259   wputs(s)                        /* print warning if enabled */
260   char  *s;
261   {
# Line 186 | Line 264 | char  *s;
264   }
265  
266  
267 < filter(infp, name)              /* process stream */
268 < register FILE   *infp;
269 < char    *name;
267 > void
268 > filter(         /* process stream */
269 >        register FILE   *infp,
270 >        char    *name
271 > )
272   {
273          char    buf[512];
274          FILE    *pfp;
# Line 218 | Line 298 | char   *name;
298   }
299  
300  
301 < xoptions(s, nm)                 /* process options in string s */
302 < char    *s;
303 < char    *nm;
301 > void
302 > xoptions(                       /* process options in string s */
303 >        char    *s,
304 >        char    *nm
305 > )
306   {
307          extern FILE     *freopen();
308          char    buf[64];
# Line 237 | Line 319 | char   *nm;
319                  case ' ':
320                  case '\t':
321                  case '\n':
322 +                case '\r':
323 +                case '\f':
324                          cp++;
325                          continue;
326                  case 'm':                       /* material name */
327                          if (*++cp != '=')
328                                  break;
329 <                        if (!*++cp)
329 >                        if (!*++cp || isspace(*cp))
330                                  break;
331                          atos(thisillum.matname, MAXSTR, cp);
332                          cp = sskip(cp);
# Line 254 | Line 338 | char   *nm;
338                  case 'f':                       /* data file name */
339                          if (*++cp != '=')
340                                  break;
341 <                        if (!*++cp) {
341 >                        if (!*++cp || isspace(*cp)) {
342                                  strcpy(thisillum.datafile,thisillum.matname);
343 +                                thisillum.dfnum = 0;
344                                  thisillum.flags &= ~IL_DATCLB;
345                                  continue;
346                          }
# Line 303 | Line 388 | char   *nm;
388                  case 'd':                       /* point sample density */
389                          if (*++cp != '=')
390                                  break;
391 <                        if (!isintd(++cp, " \t\n"))
391 >                        if (!isintd(++cp, " \t\n\r"))
392                                  break;
393                          thisillum.sampdens = atoi(cp);
394                          cp = sskip(cp);
# Line 311 | Line 396 | char   *nm;
396                  case 's':                       /* point super-samples */
397                          if (*++cp != '=')
398                                  break;
399 <                        if (!isintd(++cp, " \t\n"))
399 >                        if (!isintd(++cp, " \t\n\r"))
400                                  break;
401                          thisillum.nsamps = atoi(cp);
402                          cp = sskip(cp);
# Line 326 | Line 411 | char   *nm;
411                                  break;
412                          cp++;
413                          continue;
414 +                case 'b':                       /* brightness */
415 +                        if (*++cp != '=')
416 +                                break;
417 +                        if (!isfltd(++cp, " \t\n\r"))
418 +                                break;
419 +                        thisillum.minbrt = atof(cp);
420 +                        if (thisillum.minbrt < 0.)
421 +                                thisillum.minbrt = 0.;
422 +                        cp = sskip(cp);
423 +                        continue;
424                  case 'o':                       /* output file */
425                          if (*++cp != '=')
426                                  break;
427 <                        if (!*++cp)
427 >                        if (!*++cp || isspace(*cp))
428                                  break;
429                          atos(buf, sizeof(buf), cp);
430                          cp = sskip(cp);
# Line 347 | Line 442 | char   *nm;
442                          return;
443                  }
444          opterr:                                 /* skip faulty option */
445 <                cp = sskip(cp);
445 >                while (*cp && !isspace(*cp))
446 >                        cp++;
447                  nerrs++;
448          }
449                                                  /* print header? */
# Line 365 | Line 461 | char   *nm;
461          printf("# %s", s+2);
462   }
463  
464 + void
465 + printopts(void)                 /* print out option default values */
466 + {
467 +        printf("m=%-15s\t\t# material name\n", thisillum.matname);
468 +        printf("f=%-15s\t\t# data file name\n", thisillum.datafile);
469 +        if (thisillum.flags & IL_COLAVG)
470 +                if (thisillum.flags & IL_COLDST)
471 +                        printf("c=d\t\t\t\t# color distribution\n");
472 +                else
473 +                        printf("c=a\t\t\t\t# color average\n");
474 +        else
475 +                printf("c=n\t\t\t\t# color none\n");
476 +        if (thisillum.flags & IL_LIGHT)
477 +                printf("l+\t\t\t\t# light type on\n");
478 +        else
479 +                printf("l-\t\t\t\t# light type off\n");
480 +        printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
481 +        printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
482 +        printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
483 + }
484  
485 < printhead(ac, av)                       /* print out header */
486 < register int  ac;
487 < register char  **av;
485 >
486 > void
487 > printhead(                      /* print out header */
488 >        register int  ac,
489 >        register char  **av
490 > )
491   {
492          putchar('#');
493          while (ac-- > 0) {
# Line 379 | Line 498 | register char  **av;
498   }
499  
500  
501 < xobject(fp, nm)                         /* translate an object from fp */
502 < FILE  *fp;
503 < char  *nm;
501 > void
502 > xobject(                                /* translate an object from fp */
503 >        FILE  *fp,
504 >        char  *nm
505 > )
506   {
507          OBJREC  thisobj;
508          char  str[MAXSTR];
# Line 392 | Line 513 | char  *nm;
513          if (fgetword(str, MAXSTR, fp) == NULL)
514                  goto readerr;
515                                          /* is it an alias? */
516 <        if (!strcmp(str, ALIASID)) {
516 >        if (!strcmp(str, ALIASKEY)) {
517                  if (fgetword(str, MAXSTR, fp) == NULL)
518                          goto readerr;
519 <                printf("\n%s %s %s", thisillum.altmat, ALIASID, str);
519 >                printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str);
520                  if (fgetword(str, MAXSTR, fp) == NULL)
521                          goto readerr;
522                  printf("\t%s\n", str);
# Line 432 | Line 553 | char  *nm;
553          checkhead();
554                                                  /* process object */
555          if (doit)
556 <                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm);
556 >                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
557          else
558                  printobj(thisillum.altmat, &thisobj);
559                                                  /* free arguments */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines