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.8 by gregl, Mon Nov 10 18:05:24 1997 UTC vs.
Revision 3.17 by gregl, Mon Dec 8 18:51:15 1997 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 + #include "random.h"
13   #include "paths.h"
14 + #include <signal.h>
15   #include <sys/types.h>
16 + #include <sys/stat.h>
17  
18                          /* the following must be consistent with rholo.h */
19   int     NVARS = NRHVARS;                /* total number of variables */
# Line 23 | Line 26 | char   froot[MAXPATH];         /* root file name */
26  
27   int     nowarn = 0;             /* turn warnings off? */
28  
26 double  expval = 1.;            /* global exposure value */
27
29   int     ncprocs = 0;            /* desired number of compute processes */
30  
31   char    *outdev = NULL;         /* output device name */
32  
33 + int     readinp = 0;            /* read commands from stdin */
34 +
35   time_t  starttime;              /* time we got started */
36   time_t  endtime;                /* time we should end by */
37   time_t  reporttime;             /* time for next report */
# Line 38 | Line 41 | long   maxdisk;                /* maximum file space (bytes) */
41   int     rtargc = 1;             /* rtrace command */
42   char    *rtargv[128] = {"rtrace", NULL};
43  
44 + int     orig_mode = -1;         /* original file mode (-1 if unchanged) */
45 +
46   long    nraysdone = 0L;         /* number of rays done */
47   long    npacksdone = 0L;        /* number of packets done */
48  
49   PACKET  *freepacks;             /* available packets */
50  
51 + char  *sigerr[NSIG];            /* signal error messages */
52 +
53   extern time_t   time();
54  
55  
# Line 55 | Line 62 | char   *argv[];
62          int     force = 0;
63                                                  /* mark start time */
64          starttime = time(NULL);
65 +        initurand(10240);                       /* initialize urand */
66          progname = argv[0];                     /* get arguments */
67          for (i = 1; i < argc && argv[i][0] == '-'; i++)
68                  switch (argv[i][1]) {
# Line 64 | 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 107 | Line 118 | char   *argv[];
118                  creatholo(&hdg);
119          } else {                                /* else load holodeck */
120                  loadholo();
121 <                if (vdef(RIF))                          /* load RIF if any */
121 >                                                        /* check settings */
122 >                checkvalues();
123 >                                                        /* load RIF if any */
124 >                if (vdef(RIF))
125                          getradfile(vval(RIF));
126 +                                                        /* set defaults */
127 +                setdefaults(NULL);
128          }
129                                                  /* initialize */
130          initrholo();
131 <                                                /* run */
131 >                                                /* main loop */
132          while (rholo())
133                  ;
134                                                  /* done */
# Line 125 | Line 141 | userr:
141   }
142  
143  
144 + onsig(signo)                            /* fatal signal */
145 + int  signo;
146 + {
147 +        static int  gotsig = 0;
148 +
149 +        if (gotsig++)                   /* two signals and we're gone! */
150 +                _exit(signo);
151 +
152 +        alarm(30);                      /* allow 30 seconds to clean up */
153 +        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
154 +        eputs("signal - ");
155 +        eputs(sigerr[signo]);
156 +        eputs("\n");
157 +        quit(3);
158 + }
159 +
160 +
161 + sigdie(signo, msg)                      /* set fatal signal */
162 + int  signo;
163 + char  *msg;
164 + {
165 +        if (signal(signo, onsig) == SIG_IGN)
166 +                signal(signo, SIG_IGN);
167 +        sigerr[signo] = msg;
168 + }
169 +
170 +
171 + int
172 + resfmode(fd, mod)               /* restrict open file access mode */
173 + int     fd, mod;
174 + {
175 +        struct stat     stbuf;
176 +                                        /* get original mode */
177 +        if (fstat(fd, &stbuf) < 0)
178 +                error(SYSTEM, "cannot stat open holodeck file");
179 +        mod &= stbuf.st_mode;           /* always more restrictive */
180 +        if (mod == stbuf.st_mode)
181 +                return(-1);             /* already set */
182 +                                        /* else change it */
183 +        if (fchmod(fd, mod) < 0) {
184 +                error(WARNING, "cannot change holodeck file access mode");
185 +                return(-1);
186 +        }
187 +        return(stbuf.st_mode);          /* return original mode */
188 + }
189 +
190 +
191   initrholo()                     /* get our holodeck running */
192   {
193          extern int      global_packet();
194          register int    i;
195 +                                                /* close holodeck on exec() */
196 +        fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
197  
198          if (outdev != NULL)                     /* open output device */
199                  disp_open(outdev);
# Line 138 | Line 203 | initrholo()                    /* get our holodeck running */
203          if (!vdef(DISKSPACE))
204                  maxdisk = 0;
205          else
206 <                maxdisk = 1024.*1024.*vflt(DISKSPACE));
206 >                maxdisk = 1024.*1024.*vflt(DISKSPACE);
207                                                  /* record end time */
208          if (!vdef(TIME) || vflt(TIME) <= FTINY)
209                  endtime = 0;
# Line 184 | Line 249 | initrholo()                    /* get our holodeck running */
249                          freepacks[i].next = &freepacks[i+1];
250                  }
251          }
252 +                                        /* set up signal handling */
253 +        sigdie(SIGINT, "Interrupt");
254 +        sigdie(SIGHUP, "Hangup");
255 +        sigdie(SIGTERM, "Terminate");
256 +        sigdie(SIGPIPE, "Broken pipe");
257 +        sigdie(SIGALRM, "Alarm clock");
258 + #ifdef  SIGXCPU
259 +        sigdie(SIGXCPU, "CPU limit exceeded");
260 +        sigdie(SIGXFSZ, "File size exceeded");
261 + #endif
262 +                                                /* protect holodeck file */
263 +        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
264          return;
265   memerr:
266          error(SYSTEM, "out of memory in initrholo");
# Line 205 | Line 282 | rholo()                                /* holodeck main loop */
282          if (ncprocs <= 0)
283                  return(1);
284                                          /* check file size */
285 <        if (maxdisk > 0 && hdfiluse(hdlist[0]->fd,0)+hdmemuse(0) >= maxdisk) {
285 >        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
286                  error(WARNING, "file limit exceeded");
287                  return(0);
288          }
# Line 268 | Line 345 | register HDGRID        *gp;
345                  sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
346                  error(WARNING, errmsg);
347          }
271        if (sscanf(vval(SECTION),
272                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
273                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
274                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
275                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
276                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
277                badvalue(SECTION);
278        maxlen = 0.;
279        for (i = 0; i < 3; i++)
280                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
281                        maxlen = len[i];
282        if (!vdef(GRID)) {
283                sprintf(buf, "%.4f", maxlen/8.);
284                vval(GRID) = savqstr(buf);
285                vdef(GRID)++;
286        }
287        if ((d = vflt(GRID)) <= FTINY)
288                badvalue(GRID);
289        for (i = 0; i < 3; i++)
290                gp->grid[i] = len[i]/d + (1.-FTINY);
291        if (!vdef(EXPOSURE)) {
292                sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
293                error(USER, errmsg);
294        }
295        expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
296                        pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
348          if (!vdef(OCTREE)) {
349                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
350                          error(SYSTEM, "out of memory");
# Line 304 | Line 355 | register HDGRID        *gp;
355                  vval(OBSTRUCTIONS) = "T";
356                  vdef(OBSTRUCTIONS)++;
357          }
358 +        if (!vdef(VDIST)) {
359 +                vval(VDIST) = "F";
360 +                vdef(VDIST)++;
361 +        }
362          if (!vdef(OCCUPANCY)) {
363                  vval(OCCUPANCY) = "U";
364                  vdef(OCCUPANCY)++;
# Line 311 | Line 366 | register HDGRID        *gp;
366                                  /* append rendering options */
367          if (vdef(RENDER))
368                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
369 +        
370 +        if (gp == NULL)         /* already initialized? */
371 +                return;
372 +                                /* set grid parameters */
373 +        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
374 +        if (sscanf(vval(SECTION),
375 +                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
376 +                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
377 +                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
378 +                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
379 +                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
380 +                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
381 +                badvalue(SECTION);
382 +        maxlen = 0.;
383 +        for (i = 0; i < 3; i++)
384 +                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
385 +                        maxlen = len[i];
386 +        if (!vdef(GRID)) {
387 +                sprintf(buf, "%.4f", maxlen/8.);
388 +                vval(GRID) = savqstr(buf);
389 +                vdef(GRID)++;
390 +        }
391 +        if ((d = vflt(GRID)) <= FTINY)
392 +                badvalue(GRID);
393 +        for (i = 0; i < 3; i++)
394 +                if (gp->grid[i] <= 0)
395 +                        gp->grid[i] = len[i]/d + (1.-FTINY);
396   }
397  
398  
399   creatholo(gp)                   /* create a holodeck output file */
400   HDGRID  *gp;
401   {
402 +        extern char     VersionID[];
403          long    endloc = 0;
404 +        int     fd;
405          FILE    *fp;
406                                          /* open & truncate file */
407          if ((fp = fopen(hdkfile, "w+")) == NULL) {
# Line 326 | Line 410 | HDGRID *gp;
410          }
411                                          /* write information header */
412          newheader("RADIANCE", fp);
413 +        fprintf(fp, "SOFTWARE= %s\n", VersionID);
414          printvars(fp);
415          fputformat(HOLOFMT, fp);
416          fputc('\n', fp);
417          putw(HOLOMAGIC, fp);            /* put magic number & terminus */
418          fwrite(&endloc, sizeof(long), 1, fp);
419 <        fflush(fp);                     /* flush buffer */
420 <        hdinit(fileno(fp), gp);         /* allocate and initialize index */
421 <                        /* we're dropping fp here.... */
419 >        fd = dup(fileno(fp));
420 >        fclose(fp);                     /* flush and close stdio stream */
421 >        hdinit(fd, gp);                 /* allocate and initialize index */
422   }
423  
424  
# Line 363 | Line 448 | char   *s;
448  
449   loadholo()                      /* start loading a holodeck from fname */
450   {
451 +        extern long     ftell();
452          FILE    *fp;
453 <        long    endloc;
453 >        int     fd;
454 >        long    fpos;
455                                          /* open holodeck file */
456          if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
457                  sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
# Line 379 | Line 466 | loadholo()                     /* start loading a holodeck from fname */
466                                  hdkfile);
467                  error(USER, errmsg);
468          }
469 <        fread(&endloc, sizeof(long), 1, fp);
470 <        if (endloc != 0)
469 >        fread(&fpos, sizeof(long), 1, fp);
470 >        if (fpos != 0)
471                  error(WARNING, "ignoring multiple sections in holodeck file");
472 <        fseek(fp, 0L, 1);                       /* align system file pointer */
473 <        hdinit(fileno(fp), NULL);               /* allocate and load index */
474 <                        /* we're dropping fp here.... */
472 >        fpos = ftell(fp);                       /* get stdio position */
473 >        fd = dup(fileno(fp));
474 >        fclose(fp);                             /* done with stdio */
475 >        lseek(fd, fpos, 0);                     /* align system file pointer */
476 >        hdinit(fd, NULL);                       /* allocate and load index */
477   }
478  
479  
480   done_packets(pl)                /* handle finished packets */
481   PACKET  *pl;
482   {
483 <        static int      nunflushed = 0;
483 >        static int      n2flush = 0;
484          register PACKET *p;
485  
486          while (pl != NULL) {
# Line 402 | Line 491 | PACKET *pl;
491                                  p->nr*sizeof(RAYVAL));
492                          if (outdev != NULL)     /* display it */
493                                  disp_packet((PACKHEAD *)p);
494 <                        else
495 <                                nunflushed += p->nr;
494 >                        if (hdcachesize <= 0)   /* manual flushing */
495 >                                n2flush += p->nr;
496                          nraysdone += p->nr;
497                          npacksdone++;
498                  }
# Line 411 | Line 500 | PACKET *pl;
500                  p->next = freepacks;
501                  freepacks = p;
502          }
503 <        if (nunflushed >= 256*RPACKSIZ) {
503 >        if (n2flush > 512*RPACKSIZ*ncprocs) {
504                  hdflush(NULL);                  /* flush holodeck buffers */
505 <                nunflushed = 0;
505 >                n2flush = 0;
506          }
507   }
508  
# Line 421 | Line 510 | PACKET *pl;
510   getradfile(rfargs)              /* run rad and get needed variables */
511   char    *rfargs;
512   {
513 <        static short    mvar[] = {VIEW,OCTREE,EXPOSURE,-1};
513 >        static short    mvar[] = {OCTREE,-1};
514          static char     tf1[] = TEMPLATE;
515          char    tf2[64];
516          char    combuf[256];
# Line 520 | Line 609 | int    ec;
609   {
610          int     status = 0;
611  
523        if (outdev != NULL)             /* close display */
524                disp_close();
612          if (hdlist[0] != NULL) {        /* flush holodeck */
613                  if (ncprocs > 0) {
614                          done_packets(flush_queue());
# Line 530 | Line 617 | int    ec;
617                          if (vdef(REPORT)) {
618                                  long    fsiz, fuse;
619                                  report(0);
620 <                                fsiz = lseek(hdlist[0]->fd, 0L, 2);
620 >                                fsiz = hdfilen(hdlist[0]->fd);
621                                  fuse = hdfiluse(hdlist[0]->fd, 1);
622                                  fprintf(stderr,
623                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
# Line 540 | Line 627 | int    ec;
627                  } else
628                          hdflush(NULL);
629          }
630 +        if (orig_mode >= 0)             /* reset holodeck access mode */
631 +                fchmod(hdlist[0]->fd, orig_mode);
632 +        if (outdev != NULL)             /* close display */
633 +                disp_close();
634          exit(ec ? ec : status);         /* exit */
635   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines