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