| 1 | gregl | 3.1 | #ifndef lint | 
| 2 | greg | 3.58 | static const char       RCSid[] = "$Id: rholo.c,v 3.57 2003/06/20 00:25:49 greg Exp $"; | 
| 3 | gregl | 3.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | * Radiance holodeck generation controller | 
| 6 |  |  | */ | 
| 7 |  |  |  | 
| 8 |  |  | #include "rholo.h" | 
| 9 | gregl | 3.14 | #include "random.h" | 
| 10 | gregl | 3.12 | #include <signal.h> | 
| 11 |  |  | #include <sys/stat.h> | 
| 12 | gregl | 3.1 |  | 
| 13 | gwlarson | 3.44 | #ifndef FRAGWARN | 
| 14 |  |  | #define FRAGWARN        20              /* fragmentation for warning (%) */ | 
| 15 |  |  | #endif | 
| 16 | gregl | 3.33 | #ifndef MAXQTIME | 
| 17 |  |  | #define MAXQTIME        5               /* target maximum seconds in queue */ | 
| 18 |  |  | #endif | 
| 19 | gwlarson | 3.49 | /* manual cache flushing frequency */ | 
| 20 |  |  | #ifndef RTFLUSH | 
| 21 |  |  | #if MAXQTIME | 
| 22 |  |  | #define RTFLUSH         (300/MAXQTIME*totqlen)  /* <= 5 minutes */ | 
| 23 |  |  | #else | 
| 24 |  |  | #define RTFLUSH         (50*totqlen)            /* just guess */ | 
| 25 |  |  | #endif | 
| 26 |  |  | #endif | 
| 27 | gregl | 3.1 | /* the following must be consistent with rholo.h */ | 
| 28 |  |  | int     NVARS = NRHVARS;                /* total number of variables */ | 
| 29 |  |  |  | 
| 30 |  |  | VARIABLE        vv[] = RHVINIT;         /* variable-value pairs */ | 
| 31 |  |  |  | 
| 32 |  |  | char    *progname;              /* our program name */ | 
| 33 |  |  | char    *hdkfile;               /* holodeck file name */ | 
| 34 | gregl | 3.22 | char    froot[256];             /* root file name */ | 
| 35 | gregl | 3.1 |  | 
| 36 |  |  | int     ncprocs = 0;            /* desired number of compute processes */ | 
| 37 |  |  |  | 
| 38 |  |  | char    *outdev = NULL;         /* output device name */ | 
| 39 |  |  |  | 
| 40 | gregl | 3.17 | int     readinp = 0;            /* read commands from stdin */ | 
| 41 |  |  |  | 
| 42 | gwlarson | 3.51 | int     force = 0;              /* allow overwrite of holodeck (-1 == read-only) */ | 
| 43 | gregl | 3.20 |  | 
| 44 | gregl | 3.1 | time_t  starttime;              /* time we got started */ | 
| 45 |  |  | time_t  endtime;                /* time we should end by */ | 
| 46 |  |  | time_t  reporttime;             /* time for next report */ | 
| 47 |  |  |  | 
| 48 | greg | 3.54 | off_t   maxdisk;                /* maximum file space (bytes) */ | 
| 49 | gregl | 3.8 |  | 
| 50 | gregl | 3.1 | int     rtargc = 1;             /* rtrace command */ | 
| 51 |  |  | char    *rtargv[128] = {"rtrace", NULL}; | 
| 52 |  |  |  | 
| 53 | gregl | 3.12 | int     orig_mode = -1;         /* original file mode (-1 if unchanged) */ | 
| 54 |  |  |  | 
| 55 | gregl | 3.1 | long    nraysdone = 0L;         /* number of rays done */ | 
| 56 |  |  | long    npacksdone = 0L;        /* number of packets done */ | 
| 57 |  |  |  | 
| 58 |  |  | PACKET  *freepacks;             /* available packets */ | 
| 59 | gwlarson | 3.49 | int     totqlen;                /* maximum queue length (number of packets) */ | 
| 60 | gregl | 3.1 |  | 
| 61 | gregl | 3.12 | char  *sigerr[NSIG];            /* signal error messages */ | 
| 62 |  |  |  | 
| 63 | gwlarson | 3.40 | extern int      nowarn;         /* turn warnings off? */ | 
| 64 |  |  |  | 
| 65 | gregl | 3.1 | extern time_t   time(); | 
| 66 |  |  |  | 
| 67 |  |  |  | 
| 68 |  |  | main(argc, argv) | 
| 69 |  |  | int     argc; | 
| 70 |  |  | char    *argv[]; | 
| 71 |  |  | { | 
| 72 |  |  | int     i; | 
| 73 | gregl | 3.33 |  | 
| 74 | gregl | 3.18 | initurand(16384);                       /* initialize urand */ | 
| 75 | gregl | 3.1 | progname = argv[0];                     /* get arguments */ | 
| 76 |  |  | for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 77 |  |  | switch (argv[i][1]) { | 
| 78 |  |  | case 'w':                       /* turn off warnings */ | 
| 79 |  |  | nowarn++; | 
| 80 |  |  | break; | 
| 81 |  |  | case 'f':                       /* force overwrite */ | 
| 82 | gwlarson | 3.51 | force = 1; | 
| 83 | gregl | 3.1 | break; | 
| 84 | gwlarson | 3.51 | case 'r':                       /* read-only mode */ | 
| 85 |  |  | force = -1; | 
| 86 |  |  | break; | 
| 87 | gregl | 3.17 | case 'i':                       /* read input from stdin */ | 
| 88 |  |  | readinp++; | 
| 89 |  |  | break; | 
| 90 | gregl | 3.1 | case 'n':                       /* compute processes */ | 
| 91 |  |  | if (i >= argc-2) | 
| 92 |  |  | goto userr; | 
| 93 |  |  | ncprocs = atoi(argv[++i]); | 
| 94 |  |  | break; | 
| 95 |  |  | case 'o':                       /* output display */ | 
| 96 |  |  | if (i >= argc-2) | 
| 97 |  |  | goto userr; | 
| 98 |  |  | outdev = argv[++i]; | 
| 99 |  |  | break; | 
| 100 |  |  | default: | 
| 101 |  |  | goto userr; | 
| 102 |  |  | } | 
| 103 |  |  | /* get root file name */ | 
| 104 | gregl | 3.32 | if (i >= argc) | 
| 105 |  |  | goto userr; | 
| 106 | gregl | 3.1 | rootname(froot, hdkfile=argv[i++]); | 
| 107 | gregl | 3.27 | /* load variables? */ | 
| 108 |  |  | if (i < argc) | 
| 109 |  |  | if (argv[i][0] != '-' && argv[i][0] != '+') | 
| 110 |  |  | loadvars(argv[i]);      /* load variables from file */ | 
| 111 |  |  |  | 
| 112 |  |  | if (i >= argc || argv[i][0] == '+') | 
| 113 |  |  | loadholo();                     /* load existing holodeck */ | 
| 114 |  |  |  | 
| 115 |  |  | while (++i < argc)                      /* get command line settings */ | 
| 116 |  |  | if (setvariable(argv[i], matchvar) < 0) { | 
| 117 |  |  | sprintf(errmsg, "unknown variable: %s", argv[i]); | 
| 118 |  |  | error(USER, errmsg); | 
| 119 |  |  | } | 
| 120 |  |  | /* check settings */ | 
| 121 |  |  | checkvalues(); | 
| 122 | gregl | 3.36 | /* load rad input file */ | 
| 123 |  |  | getradfile(); | 
| 124 | gregl | 3.27 |  | 
| 125 | gregl | 3.28 | if (hdlist[0] == NULL) {                /* create new holodeck */ | 
| 126 | gregl | 3.29 | HDGRID  hdg[HDMAX]; | 
| 127 | gregl | 3.1 | /* set defaults */ | 
| 128 | gregl | 3.29 | setdefaults(hdg); | 
| 129 | gwlarson | 3.51 | /* check read-only */ | 
| 130 |  |  | if (force < 0) | 
| 131 |  |  | error(USER, "cannot create read-only holodeck"); | 
| 132 | gregl | 3.1 | /* holodeck exists? */ | 
| 133 |  |  | if (!force && access(hdkfile, R_OK|W_OK) == 0) | 
| 134 |  |  | error(USER, | 
| 135 |  |  | "holodeck file exists -- use -f to overwrite"); | 
| 136 |  |  | /* create holodeck */ | 
| 137 | gregl | 3.29 | creatholo(hdg); | 
| 138 | gregl | 3.27 | } else                                  /* else just set defaults */ | 
| 139 | gregl | 3.15 | setdefaults(NULL); | 
| 140 | gregl | 3.1 | /* initialize */ | 
| 141 |  |  | initrholo(); | 
| 142 | gregl | 3.15 | /* main loop */ | 
| 143 | gregl | 3.1 | while (rholo()) | 
| 144 |  |  | ; | 
| 145 |  |  | /* done */ | 
| 146 |  |  | quit(0); | 
| 147 |  |  | userr: | 
| 148 |  |  | fprintf(stderr, | 
| 149 | gwlarson | 3.51 | "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", | 
| 150 | gregl | 3.1 | progname); | 
| 151 |  |  | quit(1); | 
| 152 |  |  | } | 
| 153 |  |  |  | 
| 154 |  |  |  | 
| 155 | schorsch | 3.55 | void | 
| 156 | gregl | 3.12 | onsig(signo)                            /* fatal signal */ | 
| 157 |  |  | int  signo; | 
| 158 |  |  | { | 
| 159 |  |  | static int  gotsig = 0; | 
| 160 |  |  |  | 
| 161 | gwlarson | 3.47 | if (gotsig > 1)                 /* we're going as fast as we can! */ | 
| 162 |  |  | return; | 
| 163 |  |  | if (gotsig++) {                 /* two signals and we split */ | 
| 164 |  |  | hdsync(NULL, 0);        /* don't leave w/o saying goodbye */ | 
| 165 | gregl | 3.12 | _exit(signo); | 
| 166 | gwlarson | 3.47 | } | 
| 167 |  |  | alarm(300);                     /* allow 5 minutes to clean up */ | 
| 168 | gregl | 3.12 | eputs("signal - "); | 
| 169 |  |  | eputs(sigerr[signo]); | 
| 170 |  |  | eputs("\n"); | 
| 171 |  |  | quit(3); | 
| 172 |  |  | } | 
| 173 |  |  |  | 
| 174 |  |  |  | 
| 175 |  |  | sigdie(signo, msg)                      /* set fatal signal */ | 
| 176 |  |  | int  signo; | 
| 177 |  |  | char  *msg; | 
| 178 |  |  | { | 
| 179 |  |  | if (signal(signo, onsig) == SIG_IGN) | 
| 180 |  |  | signal(signo, SIG_IGN); | 
| 181 |  |  | sigerr[signo] = msg; | 
| 182 |  |  | } | 
| 183 |  |  |  | 
| 184 |  |  |  | 
| 185 |  |  | int | 
| 186 |  |  | resfmode(fd, mod)               /* restrict open file access mode */ | 
| 187 |  |  | int     fd, mod; | 
| 188 |  |  | { | 
| 189 |  |  | struct stat     stbuf; | 
| 190 |  |  | /* get original mode */ | 
| 191 |  |  | if (fstat(fd, &stbuf) < 0) | 
| 192 |  |  | error(SYSTEM, "cannot stat open holodeck file"); | 
| 193 |  |  | mod &= stbuf.st_mode;           /* always more restrictive */ | 
| 194 | gregl | 3.35 | if (mod == (stbuf.st_mode & 0777)) | 
| 195 | gregl | 3.12 | return(-1);             /* already set */ | 
| 196 |  |  | /* else change it */ | 
| 197 |  |  | if (fchmod(fd, mod) < 0) { | 
| 198 |  |  | error(WARNING, "cannot change holodeck file access mode"); | 
| 199 |  |  | return(-1); | 
| 200 |  |  | } | 
| 201 |  |  | return(stbuf.st_mode);          /* return original mode */ | 
| 202 |  |  | } | 
| 203 |  |  |  | 
| 204 |  |  |  | 
| 205 | gregl | 3.1 | initrholo()                     /* get our holodeck running */ | 
| 206 |  |  | { | 
| 207 |  |  | extern int      global_packet(); | 
| 208 |  |  | register int    i; | 
| 209 | gregl | 3.17 | /* close holodeck on exec() */ | 
| 210 |  |  | fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); | 
| 211 | gregl | 3.4 |  | 
| 212 |  |  | if (outdev != NULL)                     /* open output device */ | 
| 213 |  |  | disp_open(outdev); | 
| 214 | gregl | 3.1 | else if (ncprocs > 0)                   /* else use global ray feed */ | 
| 215 |  |  | init_global(); | 
| 216 | gregl | 3.8 | /* record disk space limit */ | 
| 217 |  |  | if (!vdef(DISKSPACE)) | 
| 218 | greg | 3.58 | maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024; | 
| 219 | gregl | 3.8 | else | 
| 220 | gregl | 3.9 | maxdisk = 1024.*1024.*vflt(DISKSPACE); | 
| 221 | gregl | 3.1 | /* set up memory cache */ | 
| 222 | gregl | 3.5 | if (outdev == NULL) | 
| 223 | gwlarson | 3.50 | hdcachesize = 0;                /* manual flushing */ | 
| 224 | gregl | 3.5 | else if (vdef(CACHE)) | 
| 225 |  |  | hdcachesize = 1024.*1024.*vflt(CACHE); | 
| 226 | gregl | 3.1 | /* open report file */ | 
| 227 |  |  | if (vdef(REPORT)) { | 
| 228 |  |  | register char   *s = sskip2(vval(REPORT), 1); | 
| 229 |  |  | if (*s && freopen(s, "a", stderr) == NULL) | 
| 230 |  |  | quit(2); | 
| 231 |  |  | } | 
| 232 | gregl | 3.33 | /* mark the starting time */ | 
| 233 |  |  | starttime = time(NULL); | 
| 234 |  |  | /* compute end time */ | 
| 235 |  |  | if (!vdef(TIME) || vflt(TIME) <= FTINY) | 
| 236 |  |  | endtime = 0; | 
| 237 |  |  | else | 
| 238 |  |  | endtime = starttime + vflt(TIME)*3600. + .5; | 
| 239 | gregl | 3.1 | /* start rtrace */ | 
| 240 |  |  | if (ncprocs > 0) { | 
| 241 | gwlarson | 3.38 | totqlen = i = start_rtrace(); | 
| 242 | gregl | 3.1 | if (i < 1) | 
| 243 | gregl | 3.20 | error(USER, "cannot start rtrace process(es)"); | 
| 244 | gregl | 3.1 | if (vdef(REPORT)) {             /* make first report */ | 
| 245 |  |  | printargs(rtargc, rtargv, stderr); | 
| 246 |  |  | report(0); | 
| 247 |  |  | } | 
| 248 |  |  | /* allocate packets */ | 
| 249 |  |  | freepacks = (PACKET *)bmalloc(i*sizeof(PACKET)); | 
| 250 |  |  | if (freepacks == NULL) | 
| 251 |  |  | goto memerr; | 
| 252 |  |  | freepacks[--i].nr = 0; | 
| 253 |  |  | freepacks[i].next = NULL; | 
| 254 | gregl | 3.25 | if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { | 
| 255 | gregl | 3.1 | freepacks[i].offset = (float *)bmalloc( | 
| 256 |  |  | RPACKSIZ*sizeof(float)*(i+1) ); | 
| 257 |  |  | if (freepacks[i].offset == NULL) | 
| 258 |  |  | goto memerr; | 
| 259 |  |  | } else | 
| 260 |  |  | freepacks[i].offset = NULL; | 
| 261 |  |  | while (i--) { | 
| 262 |  |  | freepacks[i].nr = 0; | 
| 263 |  |  | freepacks[i].offset = freepacks[i+1].offset == NULL ? | 
| 264 |  |  | NULL : freepacks[i+1].offset+RPACKSIZ ; | 
| 265 |  |  | freepacks[i].next = &freepacks[i+1]; | 
| 266 |  |  | } | 
| 267 |  |  | } | 
| 268 | gregl | 3.12 | /* set up signal handling */ | 
| 269 |  |  | sigdie(SIGINT, "Interrupt"); | 
| 270 |  |  | sigdie(SIGHUP, "Hangup"); | 
| 271 |  |  | sigdie(SIGTERM, "Terminate"); | 
| 272 |  |  | sigdie(SIGPIPE, "Broken pipe"); | 
| 273 |  |  | sigdie(SIGALRM, "Alarm clock"); | 
| 274 |  |  | #ifdef  SIGXCPU | 
| 275 |  |  | sigdie(SIGXCPU, "CPU limit exceeded"); | 
| 276 |  |  | sigdie(SIGXFSZ, "File size exceeded"); | 
| 277 |  |  | #endif | 
| 278 | gwlarson | 3.50 | /* protect holodeck file */ | 
| 279 | greg | 3.52 | orig_mode = resfmode(hdlist[0]->fd, ncprocs>0&force>=0 ? 0 : 0444); | 
| 280 | gregl | 3.1 | return; | 
| 281 |  |  | memerr: | 
| 282 |  |  | error(SYSTEM, "out of memory in initrholo"); | 
| 283 |  |  | } | 
| 284 |  |  |  | 
| 285 |  |  |  | 
| 286 |  |  | rholo()                         /* holodeck main loop */ | 
| 287 |  |  | { | 
| 288 | gwlarson | 3.44 | static long     nextfragwarn = 100*(1L<<20); | 
| 289 | gregl | 3.30 | static int      idle = 0; | 
| 290 | gregl | 3.1 | PACKET  *pl = NULL, *plend; | 
| 291 | greg | 3.54 | off_t   fsiz; | 
| 292 | gregl | 3.33 | int     pksiz; | 
| 293 | gregl | 3.1 | register PACKET *p; | 
| 294 |  |  | time_t  t; | 
| 295 | gregl | 3.30 | /* check display */ | 
| 296 |  |  | if (nprocs <= 0) | 
| 297 |  |  | idle = 1; | 
| 298 |  |  | if (outdev != NULL) { | 
| 299 | gregl | 3.4 | if (!disp_check(idle)) | 
| 300 | gregl | 3.30 | return(0);      /* quit request */ | 
| 301 |  |  | if (nprocs <= 0) | 
| 302 |  |  | return(1); | 
| 303 |  |  | } else if (idle) | 
| 304 |  |  | return(0);              /* all done */ | 
| 305 | gwlarson | 3.44 | fsiz = hdfilen(hdlist[0]->fd);  /* check file size */ | 
| 306 |  |  | if (maxdisk > 0 && fsiz >= maxdisk) { | 
| 307 | gregl | 3.8 | error(WARNING, "file limit exceeded"); | 
| 308 | gregl | 3.21 | done_rtrace(); | 
| 309 |  |  | return(1);      /* comes back */ | 
| 310 | gregl | 3.8 | } | 
| 311 | gwlarson | 3.44 | #if FRAGWARN | 
| 312 |  |  | if (fsiz >= nextfragwarn && | 
| 313 |  |  | (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) { | 
| 314 |  |  | sprintf(errmsg, "holodeck file fragmentation is %.0f%%", | 
| 315 |  |  | 100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz); | 
| 316 |  |  | error(WARNING, errmsg); | 
| 317 |  |  | nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */ | 
| 318 |  |  | } | 
| 319 |  |  | #endif | 
| 320 | gregl | 3.33 | t = time(NULL);                 /* check time */ | 
| 321 | gregl | 3.8 | if (endtime > 0 && t >= endtime) { | 
| 322 |  |  | error(WARNING, "time limit exceeded"); | 
| 323 | gregl | 3.21 | done_rtrace(); | 
| 324 |  |  | return(1);      /* comes back */ | 
| 325 | gregl | 3.8 | } | 
| 326 | gregl | 3.3 | if (reporttime > 0 && t >= reporttime) | 
| 327 | gregl | 3.1 | report(t); | 
| 328 | gregl | 3.33 | /* figure out good packet size */ | 
| 329 | gwlarson | 3.48 | pksiz = RPACKSIZ; | 
| 330 | gregl | 3.33 | #if MAXQTIME | 
| 331 | gwlarson | 3.48 | if (!chunkycmp) { | 
| 332 |  |  | pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); | 
| 333 |  |  | if (pksiz < 1) pksiz = 1; | 
| 334 |  |  | else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ; | 
| 335 |  |  | } | 
| 336 | gregl | 3.33 | #endif | 
| 337 | gregl | 3.28 | idle = 0;                       /* get packets to process */ | 
| 338 | gregl | 3.1 | while (freepacks != NULL) { | 
| 339 |  |  | p = freepacks; freepacks = p->next; p->next = NULL; | 
| 340 | gregl | 3.33 | if (!next_packet(p, pksiz)) { | 
| 341 | gregl | 3.1 | p->next = freepacks; freepacks = p; | 
| 342 | gregl | 3.21 | idle = 1; | 
| 343 | gregl | 3.1 | break; | 
| 344 |  |  | } | 
| 345 |  |  | if (pl == NULL) pl = p; | 
| 346 |  |  | else plend->next = p; | 
| 347 |  |  | plend = p; | 
| 348 |  |  | } | 
| 349 | gregl | 3.21 | /* process packets */ | 
| 350 | gregl | 3.1 | done_packets(do_packets(pl)); | 
| 351 |  |  | return(1);                      /* and continue */ | 
| 352 |  |  | } | 
| 353 |  |  |  | 
| 354 |  |  |  | 
| 355 |  |  | setdefaults(gp)                 /* set default values */ | 
| 356 |  |  | register HDGRID *gp; | 
| 357 |  |  | { | 
| 358 |  |  | extern char     *atos(); | 
| 359 |  |  | register int    i; | 
| 360 | gregl | 3.29 | int     n; | 
| 361 | gregl | 3.37 | double  len[3], d; | 
| 362 | gregl | 3.1 | char    buf[64]; | 
| 363 |  |  |  | 
| 364 |  |  | if (!vdef(SECTION)) { | 
| 365 |  |  | sprintf(errmsg, "%s must be defined", vnam(SECTION)); | 
| 366 |  |  | error(USER, errmsg); | 
| 367 |  |  | } | 
| 368 |  |  | if (!vdef(OCTREE)) { | 
| 369 |  |  | if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) | 
| 370 |  |  | error(SYSTEM, "out of memory"); | 
| 371 |  |  | sprintf(vval(OCTREE), "%s.oct", froot); | 
| 372 |  |  | vdef(OCTREE)++; | 
| 373 | gregl | 3.14 | } | 
| 374 |  |  | if (!vdef(VDIST)) { | 
| 375 |  |  | vval(VDIST) = "F"; | 
| 376 |  |  | vdef(VDIST)++; | 
| 377 | gregl | 3.1 | } | 
| 378 |  |  | /* append rendering options */ | 
| 379 |  |  | if (vdef(RENDER)) | 
| 380 |  |  | rtargc += wordstring(rtargv+rtargc, vval(RENDER)); | 
| 381 | gregl | 3.15 |  | 
| 382 |  |  | if (gp == NULL)         /* already initialized? */ | 
| 383 |  |  | return; | 
| 384 |  |  | /* set grid parameters */ | 
| 385 | gregl | 3.29 | for (n = 0; n < vdef(SECTION); n++, gp++) { | 
| 386 |  |  | gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; | 
| 387 |  |  | if (sscanf(nvalue(SECTION, n), | 
| 388 | gregl | 3.16 | "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", | 
| 389 | gregl | 3.29 | &gp->orig[0], &gp->orig[1], &gp->orig[2], | 
| 390 |  |  | &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], | 
| 391 |  |  | &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], | 
| 392 |  |  | &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], | 
| 393 |  |  | &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) | 
| 394 |  |  | badvalue(SECTION); | 
| 395 |  |  | for (i = 0; i < 3; i++) | 
| 396 | gregl | 3.37 | len[i] = VLEN(gp->xv[i]); | 
| 397 |  |  | if (!vdef(GRID)) { | 
| 398 | gwlarson | 3.43 | d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] + | 
| 399 | gregl | 3.37 | len[2]*len[2] + 4*len[1]*len[2]) | 
| 400 |  |  | + len[1]*len[1]*len[2]*(len[2] + 4*len[0]) | 
| 401 |  |  | + 4*len[0]*len[1]*len[2]*len[2] ); | 
| 402 |  |  | d = sqrt(sqrt(d)); | 
| 403 |  |  | } else if ((d = vflt(GRID)) <= FTINY) | 
| 404 | gregl | 3.29 | badvalue(GRID); | 
| 405 |  |  | for (i = 0; i < 3; i++) | 
| 406 |  |  | if (gp->grid[i] <= 0) | 
| 407 |  |  | gp->grid[i] = len[i]/d + (1.-FTINY); | 
| 408 | gregl | 3.15 | } | 
| 409 | gregl | 3.1 | } | 
| 410 |  |  |  | 
| 411 |  |  |  | 
| 412 |  |  | creatholo(gp)                   /* create a holodeck output file */ | 
| 413 |  |  | HDGRID  *gp; | 
| 414 |  |  | { | 
| 415 | gregl | 3.12 | extern char     VersionID[]; | 
| 416 | greg | 3.57 | int32   lastloc, nextloc; | 
| 417 | gregl | 3.29 | int     n; | 
| 418 | gregl | 3.10 | int     fd; | 
| 419 | gregl | 3.1 | FILE    *fp; | 
| 420 |  |  | /* open & truncate file */ | 
| 421 |  |  | if ((fp = fopen(hdkfile, "w+")) == NULL) { | 
| 422 |  |  | sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile); | 
| 423 |  |  | error(SYSTEM, errmsg); | 
| 424 |  |  | } | 
| 425 |  |  | /* write information header */ | 
| 426 |  |  | newheader("RADIANCE", fp); | 
| 427 | gregl | 3.12 | fprintf(fp, "SOFTWARE= %s\n", VersionID); | 
| 428 | gregl | 3.1 | printvars(fp); | 
| 429 |  |  | fputformat(HOLOFMT, fp); | 
| 430 |  |  | fputc('\n', fp); | 
| 431 | gregl | 3.29 | putw(HOLOMAGIC, fp);            /* put magic number */ | 
| 432 | gregl | 3.10 | fd = dup(fileno(fp)); | 
| 433 |  |  | fclose(fp);                     /* flush and close stdio stream */ | 
| 434 | greg | 3.56 | lastloc = lseek(fd, (off_t)0, 2); | 
| 435 | gregl | 3.29 | for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */ | 
| 436 |  |  | nextloc = 0L; | 
| 437 |  |  | write(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 438 |  |  | hdinit(fd, gp);                 /* writes beam index */ | 
| 439 |  |  | if (!n) | 
| 440 |  |  | break; | 
| 441 |  |  | nextloc = hdfilen(fd);          /* write section pointer */ | 
| 442 | greg | 3.52 | if (lseek(fd, (off_t)lastloc, 0) < 0) | 
| 443 | gregl | 3.29 | error(SYSTEM, | 
| 444 |  |  | "cannot seek on holodeck file in creatholo"); | 
| 445 |  |  | write(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 446 | greg | 3.52 | lseek(fd, (off_t)(lastloc=nextloc), 0); | 
| 447 | gregl | 3.29 | } | 
| 448 | gregl | 3.1 | } | 
| 449 |  |  |  | 
| 450 |  |  |  | 
| 451 | gwlarson | 3.42 | int | 
| 452 | gregl | 3.1 | headline(s)                     /* process information header line */ | 
| 453 |  |  | char    *s; | 
| 454 |  |  | { | 
| 455 |  |  | extern char     FMTSTR[]; | 
| 456 |  |  | register char   *cp; | 
| 457 |  |  | char    fmt[32]; | 
| 458 |  |  |  | 
| 459 | gregl | 3.2 | if (formatval(fmt, s)) { | 
| 460 | gregl | 3.1 | if (strcmp(fmt, HOLOFMT)) { | 
| 461 |  |  | sprintf(errmsg, "%s file \"%s\" has %s%s", | 
| 462 |  |  | HOLOFMT, hdkfile, FMTSTR, fmt); | 
| 463 |  |  | error(USER, errmsg); | 
| 464 |  |  | } | 
| 465 | gwlarson | 3.42 | return(0); | 
| 466 | gregl | 3.1 | } | 
| 467 |  |  | for (cp = s; *cp; cp++)         /* take off any comments */ | 
| 468 |  |  | if (*cp == '#') { | 
| 469 |  |  | *cp = '\0'; | 
| 470 |  |  | break; | 
| 471 |  |  | } | 
| 472 |  |  | setvariable(s, matchvar);       /* don't flag errors */ | 
| 473 | gwlarson | 3.42 | return(0); | 
| 474 | gregl | 3.1 | } | 
| 475 |  |  |  | 
| 476 |  |  |  | 
| 477 |  |  | loadholo()                      /* start loading a holodeck from fname */ | 
| 478 |  |  | { | 
| 479 |  |  | FILE    *fp; | 
| 480 | gregl | 3.10 | int     fd; | 
| 481 | gregl | 3.29 | int     n; | 
| 482 | greg | 3.57 | int32   nextloc; | 
| 483 | gwlarson | 3.51 |  | 
| 484 |  |  | if (ncprocs > 0 & force >= 0) | 
| 485 |  |  | fp = fopen(hdkfile, "r+"); | 
| 486 |  |  | else | 
| 487 |  |  | fp = NULL; | 
| 488 |  |  | if (fp == NULL) { | 
| 489 |  |  | if ((fp = fopen(hdkfile, "r")) == NULL) { | 
| 490 |  |  | sprintf(errmsg, "cannot open \"%s\"", hdkfile); | 
| 491 |  |  | error(SYSTEM, errmsg); | 
| 492 |  |  | } | 
| 493 |  |  | if (ncprocs > 0) { | 
| 494 |  |  | sprintf(errmsg, | 
| 495 |  |  | "\"%s\" opened read-only; new rays will be discarded", | 
| 496 |  |  | hdkfile); | 
| 497 |  |  | error(WARNING, errmsg); | 
| 498 |  |  | force = -1; | 
| 499 |  |  | } | 
| 500 | gregl | 3.1 | } | 
| 501 |  |  | /* load variables from header */ | 
| 502 |  |  | getheader(fp, headline, NULL); | 
| 503 |  |  | /* check magic number */ | 
| 504 |  |  | if (getw(fp) != HOLOMAGIC) { | 
| 505 |  |  | sprintf(errmsg, "bad magic number in holodeck file \"%s\"", | 
| 506 |  |  | hdkfile); | 
| 507 |  |  | error(USER, errmsg); | 
| 508 |  |  | } | 
| 509 | gregl | 3.29 | nextloc = ftell(fp);                    /* get stdio position */ | 
| 510 | gregl | 3.10 | fd = dup(fileno(fp)); | 
| 511 |  |  | fclose(fp);                             /* done with stdio */ | 
| 512 | gregl | 3.29 | for (n = 0; nextloc > 0L; n++) {        /* initialize each section */ | 
| 513 | greg | 3.52 | lseek(fd, (off_t)nextloc, 0); | 
| 514 | gregl | 3.29 | read(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 515 |  |  | hdinit(fd, NULL); | 
| 516 |  |  | } | 
| 517 |  |  | if (n != vdef(SECTION)) { | 
| 518 |  |  | sprintf(errmsg, "number of sections does not match %s setting", | 
| 519 |  |  | vnam(SECTION)); | 
| 520 |  |  | error(WARNING, errmsg); | 
| 521 |  |  | } | 
| 522 | gregl | 3.1 | } | 
| 523 |  |  |  | 
| 524 |  |  |  | 
| 525 |  |  | done_packets(pl)                /* handle finished packets */ | 
| 526 |  |  | PACKET  *pl; | 
| 527 |  |  | { | 
| 528 | gregl | 3.13 | static int      n2flush = 0; | 
| 529 | gregl | 3.1 | register PACKET *p; | 
| 530 |  |  |  | 
| 531 |  |  | while (pl != NULL) { | 
| 532 |  |  | p = pl; pl = p->next; p->next = NULL; | 
| 533 |  |  | if (p->nr > 0) {                /* add to holodeck */ | 
| 534 | greg | 3.53 | bcopy((void *)p->ra, | 
| 535 |  |  | (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr), | 
| 536 | gregl | 3.1 | p->nr*sizeof(RAYVAL)); | 
| 537 |  |  | if (outdev != NULL)     /* display it */ | 
| 538 | gregl | 3.7 | disp_packet((PACKHEAD *)p); | 
| 539 | gwlarson | 3.49 | if (hdcachesize <= 0) | 
| 540 | gregl | 3.33 | n2flush++; | 
| 541 | gregl | 3.6 | nraysdone += p->nr; | 
| 542 |  |  | npacksdone++; | 
| 543 | gregl | 3.33 | p->nr = 0; | 
| 544 | gregl | 3.1 | } | 
| 545 | gregl | 3.33 | p->next = freepacks;            /* push onto free list */ | 
| 546 | gregl | 3.1 | freepacks = p; | 
| 547 | gregl | 3.5 | } | 
| 548 | gwlarson | 3.50 | if (n2flush >= RTFLUSH) { | 
| 549 | gwlarson | 3.46 | if (outdev != NULL) | 
| 550 |  |  | hdsync(NULL, 1); | 
| 551 | gwlarson | 3.41 | else | 
| 552 | gwlarson | 3.46 | hdflush(NULL); | 
| 553 | gregl | 3.13 | n2flush = 0; | 
| 554 | gregl | 3.1 | } | 
| 555 |  |  | } | 
| 556 |  |  |  | 
| 557 |  |  |  | 
| 558 |  |  | rootname(rn, fn)                /* remove tail from end of fn */ | 
| 559 |  |  | register char   *rn, *fn; | 
| 560 |  |  | { | 
| 561 |  |  | char    *tp, *dp; | 
| 562 |  |  |  | 
| 563 |  |  | for (tp = NULL, dp = rn; *rn = *fn++; rn++) | 
| 564 | gregl | 3.22 | if (*rn == '/') | 
| 565 | gregl | 3.1 | dp = rn; | 
| 566 |  |  | else if (*rn == '.') | 
| 567 |  |  | tp = rn; | 
| 568 |  |  | if (tp != NULL && tp > dp) | 
| 569 |  |  | *tp = '\0'; | 
| 570 |  |  | } | 
| 571 |  |  |  | 
| 572 |  |  |  | 
| 573 |  |  | badvalue(vc)                    /* report bad variable value and exit */ | 
| 574 |  |  | int     vc; | 
| 575 |  |  | { | 
| 576 |  |  | sprintf(errmsg, "bad value for variable '%s'", vnam(vc)); | 
| 577 |  |  | error(USER, errmsg); | 
| 578 |  |  | } | 
| 579 |  |  |  | 
| 580 |  |  |  | 
| 581 | greg | 3.52 | void | 
| 582 | gregl | 3.1 | eputs(s)                        /* put error message to stderr */ | 
| 583 |  |  | register char  *s; | 
| 584 |  |  | { | 
| 585 |  |  | static int  midline = 0; | 
| 586 |  |  |  | 
| 587 |  |  | if (!*s) | 
| 588 |  |  | return; | 
| 589 |  |  | if (!midline++) {       /* prepend line with program name */ | 
| 590 |  |  | fputs(progname, stderr); | 
| 591 |  |  | fputs(": ", stderr); | 
| 592 |  |  | } | 
| 593 |  |  | fputs(s, stderr); | 
| 594 |  |  | if (s[strlen(s)-1] == '\n') { | 
| 595 |  |  | fflush(stderr); | 
| 596 |  |  | midline = 0; | 
| 597 |  |  | } | 
| 598 |  |  | } | 
| 599 |  |  |  | 
| 600 |  |  |  | 
| 601 | greg | 3.52 | void | 
| 602 | gregl | 3.1 | quit(ec)                        /* exit program gracefully */ | 
| 603 |  |  | int     ec; | 
| 604 |  |  | { | 
| 605 |  |  | int     status = 0; | 
| 606 |  |  |  | 
| 607 | gregl | 3.24 | if (hdlist[0] != NULL) {        /* close holodeck */ | 
| 608 | gregl | 3.21 | if (nprocs > 0) | 
| 609 | gregl | 3.24 | status = done_rtrace();         /* calls hdsync() */ | 
| 610 | gwlarson | 3.51 | if (ncprocs > 0 & force >= 0 && vdef(REPORT)) { | 
| 611 | greg | 3.54 | off_t   fsiz, fuse; | 
| 612 | gregl | 3.19 | fsiz = hdfilen(hdlist[0]->fd); | 
| 613 |  |  | fuse = hdfiluse(hdlist[0]->fd, 1); | 
| 614 |  |  | fprintf(stderr, | 
| 615 | gregl | 3.3 | "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", | 
| 616 | gregl | 3.19 | hdkfile, fsiz/(1024.*1024.), | 
| 617 |  |  | 100.*(fsiz-fuse)/fsiz); | 
| 618 |  |  | } | 
| 619 | gregl | 3.1 | } | 
| 620 | gregl | 3.12 | if (orig_mode >= 0)             /* reset holodeck access mode */ | 
| 621 |  |  | fchmod(hdlist[0]->fd, orig_mode); | 
| 622 | gregl | 3.11 | if (outdev != NULL)             /* close display */ | 
| 623 |  |  | disp_close(); | 
| 624 | gregl | 3.1 | exit(ec ? ec : status);         /* exit */ | 
| 625 |  |  | } |