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.14 by gregl, Mon Dec 1 16:34:21 1997 UTC vs.
Revision 3.26 by gregl, Sat Dec 13 10:38:26 1997 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10  
11   #include "rholo.h"
12   #include "random.h"
13 #include "paths.h"
13   #include <signal.h>
14   #include <sys/types.h>
15   #include <sys/stat.h>
# Line 22 | 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  
29 double  expval = 1.;            /* global exposure value */
30
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 59 | Line 60 | char   *argv[];
60   {
61          HDGRID  hdg;
62          int     i;
62        int     force = 0;
63                                                  /* mark start time */
64          starttime = time(NULL);
65 <        initurand(10240);                       /* initialize urand */
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 72 | 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 85 | Line 88 | char   *argv[];
88                  default:
89                          goto userr;
90                  }
88                                                /* do we have a job? */
89        if (outdev == NULL && ncprocs <= 0)
90                goto userr;
91                                                  /* get root file name */
92          rootname(froot, hdkfile=argv[i++]);
93                                                  /* load... */
# Line 103 | Line 103 | char   *argv[];
103                                                          /* check settings */
104                  checkvalues();
105                                                          /* load RIF if any */
106 <                if (vdef(RIF))
107 <                        getradfile(vval(RIF));
106 >                getradfile();
107                                                          /* set defaults */
108                  setdefaults(&hdg);
109                                                          /* holodeck exists? */
# Line 115 | 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 184 | 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 198 | 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 214 | 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 225 | 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 269 | 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 290 | 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;
300 <                                        /* are we out of stuff to do? */
301 <        if (idle && outdev == NULL)
302 <                return(0);
303 <                                        /* else process packets */
309 >                                        /* process packets */
310          done_packets(do_packets(pl));
311          return(1);                      /* and continue */
312   }
313  
314  
309 report(t)                       /* report progress so far */
310 time_t  t;
311 {
312        if (t == 0)
313                t = time(NULL);
314        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
315                        progname, npacksdone, nraysdone, (t-starttime)/3600.);
316        fflush(stderr);
317        if (vdef(REPORT))
318                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
319 }
320
321
315   setdefaults(gp)                 /* set default values */
316   register HDGRID *gp;
317   {
# Line 335 | Line 328 | register HDGRID        *gp;
328                  sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
329                  error(WARNING, errmsg);
330          }
338        if (sscanf(vval(SECTION),
339                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
340                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
341                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
342                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
343                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
344                badvalue(SECTION);
345        maxlen = 0.;
346        for (i = 0; i < 3; i++)
347                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
348                        maxlen = len[i];
349        if (!vdef(GRID)) {
350                sprintf(buf, "%.4f", maxlen/8.);
351                vval(GRID) = savqstr(buf);
352                vdef(GRID)++;
353        }
354        if ((d = vflt(GRID)) <= FTINY)
355                badvalue(GRID);
356        for (i = 0; i < 3; i++)
357                gp->grid[i] = len[i]/d + (1.-FTINY);
358        if (!vdef(EXPOSURE)) {
359                sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
360                error(USER, errmsg);
361        }
362        expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
363                        pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
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          }
370        if (!vdef(OBSTRUCTIONS)) {
371                vval(OBSTRUCTIONS) = "T";
372                vdef(OBSTRUCTIONS)++;
373        }
337          if (!vdef(VDIST)) {
338                  vval(VDIST) = "F";
339                  vdef(VDIST)++;
# Line 382 | Line 345 | register HDGRID        *gp;
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 %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],
359 +                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
360 +                badvalue(SECTION);
361 +        maxlen = 0.;
362 +        for (i = 0; i < 3; i++)
363 +                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
364 +                        maxlen = len[i];
365 +        if (!vdef(GRID)) {
366 +                sprintf(buf, "%.4f", maxlen/8.);
367 +                vval(GRID) = savqstr(buf);
368 +                vdef(GRID)++;
369 +        }
370 +        if ((d = vflt(GRID)) <= FTINY)
371 +                badvalue(GRID);
372 +        for (i = 0; i < 3; i++)
373 +                if (gp->grid[i] <= 0)
374 +                        gp->grid[i] = len[i]/d + (1.-FTINY);
375   }
376  
377  
# Line 489 | 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  
499 getradfile(rfargs)              /* run rad and get needed variables */
500 char    *rfargs;
501 {
502        static short    mvar[] = {OCTREE,EXPOSURE,-1};
503        static char     tf1[] = TEMPLATE;
504        char    tf2[64];
505        char    combuf[256];
506        char    *pippt;
507        register int    i;
508        register char   *cp;
509                                        /* create rad command */
510        mktemp(tf1);
511        sprintf(tf2, "%s.rif", tf1);
512        sprintf(combuf,
513                "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
514                        rfargs, tf1);
515        cp = combuf;
516        while (*cp){
517                if (*cp == '|') pippt = cp;
518                cp++;
519        }                               /* match unset variables */
520        for (i = 0; mvar[i] >= 0; i++)
521                if (!vdef(mvar[i])) {
522                        *cp++ = '|';
523                        strcpy(cp, vnam(mvar[i]));
524                        while (*cp) cp++;
525                        pippt = NULL;
526                }
527        if (pippt != NULL)
528                strcpy(pippt, "> /dev/null");   /* nothing to match */
529        else
530                sprintf(cp, ")[ \t]*=' > %s", tf2);
531        if (system(combuf)) {
532                error(SYSTEM, "cannot execute rad command");
533                unlink(tf2);                    /* clean up */
534                unlink(tf1);
535                quit(1);
536        }
537        if (pippt == NULL) {
538                loadvars(tf2);                  /* load variables */
539                unlink(tf2);
540        }
541        rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
542        unlink(tf1);                    /* clean up */
543 }
544
545
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 598 | 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);
609 <                                fsiz = hdfilen(hdlist[0]->fd);
610 <                                fuse = hdfiluse(hdlist[0]->fd, 1);
611 <                                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 <                        }
616 <                } else
617 <                        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