ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
(Generate patch)

Comparing ray/src/hd/rholo.c (file contents):
Revision 3.13 by gregl, Wed Nov 26 20:13:55 1997 UTC vs.
Revision 3.26 by gregl, Sat Dec 13 10:38:26 1997 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 < #include "paths.h"
12 > #include "random.h"
13   #include <signal.h>
14   #include <sys/types.h>
15   #include <sys/stat.h>
# Line 21 | Line 21 | VARIABLE       vv[] = RHVINIT;         /* variable-value pairs */
21  
22   char    *progname;              /* our program name */
23   char    *hdkfile;               /* holodeck file name */
24 < char    froot[MAXPATH];         /* root file name */
24 > char    froot[256];             /* root file name */
25  
26   int     nowarn = 0;             /* turn warnings off? */
27  
28 double  expval = 1.;            /* global exposure value */
29
28   int     ncprocs = 0;            /* desired number of compute processes */
29  
30   char    *outdev = NULL;         /* output device name */
31  
32 + int     readinp = 0;            /* read commands from stdin */
33 +
34 + int     force = 0;              /* allow overwrite of holodeck */
35 +
36   time_t  starttime;              /* time we got started */
37   time_t  endtime;                /* time we should end by */
38   time_t  reporttime;             /* time for next report */
# Line 58 | Line 60 | char   *argv[];
60   {
61          HDGRID  hdg;
62          int     i;
61        int     force = 0;
63                                                  /* mark start time */
64          starttime = time(NULL);
65 +        initurand(16384);                       /* initialize urand */
66          progname = argv[0];                     /* get arguments */
67          for (i = 1; i < argc && argv[i][0] == '-'; i++)
68                  switch (argv[i][1]) {
# Line 70 | Line 72 | char   *argv[];
72                  case 'f':                       /* force overwrite */
73                          force++;
74                          break;
75 +                case 'i':                       /* read input from stdin */
76 +                        readinp++;
77 +                        break;
78                  case 'n':                       /* compute processes */
79                          if (i >= argc-2)
80                                  goto userr;
# Line 83 | Line 88 | char   *argv[];
88                  default:
89                          goto userr;
90                  }
86                                                /* do we have a job? */
87        if (outdev == NULL && ncprocs <= 0)
88                goto userr;
91                                                  /* get root file name */
92          rootname(froot, hdkfile=argv[i++]);
93                                                  /* load... */
# Line 101 | Line 103 | char   *argv[];
103                                                          /* check settings */
104                  checkvalues();
105                                                          /* load RIF if any */
106 <                if (vdef(RIF))
105 <                        getradfile(vval(RIF));
106 >                getradfile();
107                                                          /* set defaults */
108                  setdefaults(&hdg);
109                                                          /* holodeck exists? */
# Line 113 | Line 114 | char   *argv[];
114                  creatholo(&hdg);
115          } else {                                /* else load holodeck */
116                  loadholo();
117 <                if (vdef(RIF))                          /* load RIF if any */
118 <                        getradfile(vval(RIF));
117 >                                                        /* check settings */
118 >                checkvalues();
119 >                                                        /* load RIF if any */
120 >                getradfile();
121 >                                                        /* set defaults */
122 >                setdefaults(NULL);
123          }
124                                                  /* initialize */
125          initrholo();
126 <                                                /* run */
126 >                                                /* main loop */
127          while (rholo())
128                  ;
129                                                  /* done */
130          quit(0);
131   userr:
132          fprintf(stderr,
133 < "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
133 > "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
134                          progname);
135          quit(1);
136   }
# Line 182 | Line 187 | initrholo()                    /* get our holodeck running */
187   {
188          extern int      global_packet();
189          register int    i;
190 +                                                /* close holodeck on exec() */
191 +        fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
192  
193          if (outdev != NULL)                     /* open output device */
194                  disp_open(outdev);
# Line 196 | Line 203 | initrholo()                    /* get our holodeck running */
203          if (!vdef(TIME) || vflt(TIME) <= FTINY)
204                  endtime = 0;
205          else
206 <                endtime = starttime + vflt(TIME)*3600.;
206 >                endtime = starttime + vflt(TIME)*3600. + .5;
207                                                  /* set up memory cache */
208          if (outdev == NULL)
209                  hdcachesize = 0;        /* manual flushing */
# Line 212 | Line 219 | initrholo()                    /* get our holodeck running */
219          if (ncprocs > 0) {
220                  i = start_rtrace();
221                  if (i < 1)
222 <                        error(USER, "cannot start rtrace process");
222 >                        error(USER, "cannot start rtrace process(es)");
223                  if (vdef(REPORT)) {             /* make first report */
224                          printargs(rtargc, rtargv, stderr);
225                          report(0);
# Line 223 | Line 230 | initrholo()                    /* get our holodeck running */
230                          goto memerr;
231                  freepacks[--i].nr = 0;
232                  freepacks[i].next = NULL;
233 <                if (!vbool(OBSTRUCTIONS)) {
233 >                if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
234                          freepacks[i].offset = (float *)bmalloc(
235                                          RPACKSIZ*sizeof(float)*(i+1) );
236                          if (freepacks[i].offset == NULL)
# Line 267 | Line 274 | rholo()                                /* holodeck main loop */
274                  if (!disp_check(idle))
275                          return(0);
276                                          /* display only? */
277 <        if (ncprocs <= 0)
278 <                return(1);
277 >        if (nprocs <= 0)
278 >                return(outdev != NULL);
279                                          /* check file size */
280          if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
281                  error(WARNING, "file limit exceeded");
282 <                return(0);
282 >                done_rtrace();
283 >                idle = 1;
284 >                return(1);      /* comes back */
285          }
286                                          /* check time */
287          if (endtime > 0 || reporttime > 0)
288                  t = time(NULL);
289          if (endtime > 0 && t >= endtime) {
290                  error(WARNING, "time limit exceeded");
291 <                return(0);
291 >                done_rtrace();
292 >                idle = 1;
293 >                return(1);      /* comes back */
294          }
295          if (reporttime > 0 && t >= reporttime)
296                  report(t);
# Line 288 | Line 299 | rholo()                                /* holodeck main loop */
299                  p = freepacks; freepacks = p->next; p->next = NULL;
300                  if (!next_packet(p)) {
301                          p->next = freepacks; freepacks = p;
302 +                        idle = 1;
303                          break;
304                  }
305                  if (pl == NULL) pl = p;
306                  else plend->next = p;
307                  plend = p;
308          }
309 <        idle = pl == NULL && freepacks != NULL;
298 <                                        /* are we out of stuff to do? */
299 <        if (idle && outdev == NULL)
300 <                return(0);
301 <                                        /* else process packets */
309 >                                        /* process packets */
310          done_packets(do_packets(pl));
311          return(1);                      /* and continue */
312   }
313  
314  
307 report(t)                       /* report progress so far */
308 time_t  t;
309 {
310        if (t == 0)
311                t = time(NULL);
312        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
313                        progname, npacksdone, nraysdone, (t-starttime)/3600.);
314        fflush(stderr);
315        if (vdef(REPORT))
316                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
317 }
318
319
315   setdefaults(gp)                 /* set default values */
316   register HDGRID *gp;
317   {
# Line 333 | Line 328 | register HDGRID        *gp;
328                  sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
329                  error(WARNING, errmsg);
330          }
331 +        if (!vdef(OCTREE)) {
332 +                if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
333 +                        error(SYSTEM, "out of memory");
334 +                sprintf(vval(OCTREE), "%s.oct", froot);
335 +                vdef(OCTREE)++;
336 +        }
337 +        if (!vdef(VDIST)) {
338 +                vval(VDIST) = "F";
339 +                vdef(VDIST)++;
340 +        }
341 +        if (!vdef(OCCUPANCY)) {
342 +                vval(OCCUPANCY) = "U";
343 +                vdef(OCCUPANCY)++;
344 +        }
345 +                                /* append rendering options */
346 +        if (vdef(RENDER))
347 +                rtargc += wordstring(rtargv+rtargc, vval(RENDER));
348 +        
349 +        if (gp == NULL)         /* already initialized? */
350 +                return;
351 +                                /* set grid parameters */
352 +        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
353          if (sscanf(vval(SECTION),
354 <                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
354 >                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
355                          &gp->orig[0], &gp->orig[1], &gp->orig[2],
356                          &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
357                          &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
358 <                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
358 >                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
359 >                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
360                  badvalue(SECTION);
361          maxlen = 0.;
362          for (i = 0; i < 3; i++)
# Line 352 | Line 370 | register HDGRID        *gp;
370          if ((d = vflt(GRID)) <= FTINY)
371                  badvalue(GRID);
372          for (i = 0; i < 3; i++)
373 <                gp->grid[i] = len[i]/d + (1.-FTINY);
374 <        if (!vdef(EXPOSURE)) {
357 <                sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
358 <                error(USER, errmsg);
359 <        }
360 <        expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
361 <                        pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
362 <        if (!vdef(OCTREE)) {
363 <                if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
364 <                        error(SYSTEM, "out of memory");
365 <                sprintf(vval(OCTREE), "%s.oct", froot);
366 <                vdef(OCTREE)++;
367 <        }
368 <        if (!vdef(OBSTRUCTIONS)) {
369 <                vval(OBSTRUCTIONS) = "T";
370 <                vdef(OBSTRUCTIONS)++;
371 <        }
372 <        if (!vdef(OCCUPANCY)) {
373 <                vval(OCCUPANCY) = "U";
374 <                vdef(OCCUPANCY)++;
375 <        }
376 <                                /* append rendering options */
377 <        if (vdef(RENDER))
378 <                rtargc += wordstring(rtargv+rtargc, vval(RENDER));
373 >                if (gp->grid[i] <= 0)
374 >                        gp->grid[i] = len[i]/d + (1.-FTINY);
375   }
376  
377  
# Line 483 | Line 479 | PACKET *pl;
479                  p->next = freepacks;
480                  freepacks = p;
481          }
482 <        if (n2flush > 512*RPACKSIZ*ncprocs) {
482 >        if (n2flush > 512*RPACKSIZ*nprocs) {
483                  hdflush(NULL);                  /* flush holodeck buffers */
484                  n2flush = 0;
485          }
486   }
487  
488  
493 getradfile(rfargs)              /* run rad and get needed variables */
494 char    *rfargs;
495 {
496        static short    mvar[] = {OCTREE,EXPOSURE,-1};
497        static char     tf1[] = TEMPLATE;
498        char    tf2[64];
499        char    combuf[256];
500        char    *pippt;
501        register int    i;
502        register char   *cp;
503                                        /* create rad command */
504        mktemp(tf1);
505        sprintf(tf2, "%s.rif", tf1);
506        sprintf(combuf,
507                "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
508                        rfargs, tf1);
509        cp = combuf;
510        while (*cp){
511                if (*cp == '|') pippt = cp;
512                cp++;
513        }                               /* match unset variables */
514        for (i = 0; mvar[i] >= 0; i++)
515                if (!vdef(mvar[i])) {
516                        *cp++ = '|';
517                        strcpy(cp, vnam(mvar[i]));
518                        while (*cp) cp++;
519                        pippt = NULL;
520                }
521        if (pippt != NULL)
522                strcpy(pippt, "> /dev/null");   /* nothing to match */
523        else
524                sprintf(cp, ")[ \t]*=' > %s", tf2);
525        if (system(combuf)) {
526                error(SYSTEM, "cannot execute rad command");
527                unlink(tf2);                    /* clean up */
528                unlink(tf1);
529                quit(1);
530        }
531        if (pippt == NULL) {
532                loadvars(tf2);                  /* load variables */
533                unlink(tf2);
534        }
535        rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
536        unlink(tf1);                    /* clean up */
537 }
538
539
489   rootname(rn, fn)                /* remove tail from end of fn */
490   register char   *rn, *fn;
491   {
492          char    *tp, *dp;
493  
494          for (tp = NULL, dp = rn; *rn = *fn++; rn++)
495 <                if (ISDIRSEP(*rn))
495 >                if (*rn == '/')
496                          dp = rn;
497                  else if (*rn == '.')
498                          tp = rn;
# Line 592 | Line 541 | int    ec;
541   {
542          int     status = 0;
543  
544 <        if (hdlist[0] != NULL) {        /* flush holodeck */
545 <                if (ncprocs > 0) {
546 <                        done_packets(flush_queue());
547 <                        status = end_rtrace();  /* close rtrace */
548 <                        hdflush(NULL);
549 <                        if (vdef(REPORT)) {
550 <                                long    fsiz, fuse;
551 <                                report(0);
603 <                                fsiz = hdfilen(hdlist[0]->fd);
604 <                                fuse = hdfiluse(hdlist[0]->fd, 1);
605 <                                fprintf(stderr,
544 >        if (hdlist[0] != NULL) {        /* close holodeck */
545 >                if (nprocs > 0)
546 >                        status = done_rtrace();         /* calls hdsync() */
547 >                if (ncprocs > 0 && vdef(REPORT)) {
548 >                        long    fsiz, fuse;
549 >                        fsiz = hdfilen(hdlist[0]->fd);
550 >                        fuse = hdfiluse(hdlist[0]->fd, 1);
551 >                        fprintf(stderr,
552                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
553 <                                                hdkfile, fsiz/(1024.*1024.),
554 <                                                100.*(fsiz-fuse)/fsiz);
555 <                        }
610 <                } else
611 <                        hdflush(NULL);
553 >                                        hdkfile, fsiz/(1024.*1024.),
554 >                                        100.*(fsiz-fuse)/fsiz);
555 >                }
556          }
557          if (orig_mode >= 0)             /* reset holodeck access mode */
558                  fchmod(hdlist[0]->fd, orig_mode);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines