| 51 |  | } RAYPAR; | 
| 52 |  |  | 
| 53 |  | static int openholo(char *fname, int append); | 
| 54 | < | static void addray(RAYPAR *rp); | 
| 54 | > | static int addray(RAYPAR *rp); | 
| 55 |  | static int readval(RREAL *v, int n, FILE *fp); | 
| 56 |  | static void readrays(FILE *fp); | 
| 57 |  | static int writeval(RREAL *v, int n, FILE *fp); | 
| 228 |  | return(n); | 
| 229 |  | } | 
| 230 |  |  | 
| 231 | < | void | 
| 231 | > | int | 
| 232 |  | addray(         /* add a ray to our output holodeck */ | 
| 233 |  | RAYPAR *rp | 
| 234 |  | ) | 
| 241 |  | double  d0, d1; | 
| 242 |  | unsigned        dc; | 
| 243 |  | RAYVAL  *rv; | 
| 244 | + | int     nsects = 0; | 
| 245 |  | /* check each output section */ | 
| 246 |  | for (sn = nholosects; sn--; ) { | 
| 247 |  | hp = hdlist[sn]; | 
| 272 |  | rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1]; | 
| 273 |  | rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1]; | 
| 274 |  | copycolr(rv->v, rp->cv); | 
| 275 | + | ++nsects; | 
| 276 |  | } | 
| 277 | + | return nsects; | 
| 278 |  | } | 
| 279 |  |  | 
| 280 |  | /* Read n-vector from file stream */ | 
| 328 |  | static void | 
| 329 |  | readrays(FILE *fp) | 
| 330 |  | { | 
| 331 | + | unsigned long   nread=0, ngood=0; | 
| 332 | + |  | 
| 333 |  | if (iofmt != 'a') | 
| 334 |  | SET_FILE_BINARY(fp); | 
| 335 |  | #ifdef getc_unlocked | 
| 384 |  | if (!flags)     /* got nothing, so may be normal EOF */ | 
| 385 |  | return; | 
| 386 |  | } | 
| 387 | + | ++nread; | 
| 388 |  | if (flags & (BAD_DIR|BAD_LEN)) | 
| 389 |  | continue;       /* just a bad ray is all -- skip */ | 
| 390 |  | if (!(flags & GOT_VAL)) | 
| 402 |  | } else | 
| 403 |  | goto missingData; | 
| 404 |  | } | 
| 405 | < | addray(&ryp);           /* add our ray to holodeck */ | 
| 405 | > | ngood += (addray(&ryp) > 0);    /* add our ray to holodeck */ | 
| 406 |  | } | 
| 407 |  | return; | 
| 408 |  | missingData: | 
| 409 | < | sprintf(errmsg, "insufficient data in -i%s", rspec); | 
| 409 | > | sprintf(errmsg, "insufficient data or read error with -i%s after %lu rays read (%lu used)", | 
| 410 | > | rspec, nread, ngood); | 
| 411 |  | error(USER, errmsg); | 
| 412 |  | } | 
| 413 |  |  | 
| 485 |  | return 0;                       /* write error? */ | 
| 486 |  | } | 
| 487 |  |  | 
| 488 | + | static BEAMI    *beamdir; | 
| 489 | + |  | 
| 490 | + | static int | 
| 491 | + | bpcmp(                  /* compare beam positions on disk */ | 
| 492 | + | const void      *b1p, | 
| 493 | + | const void      *b2p | 
| 494 | + | ) | 
| 495 | + | { | 
| 496 | + | off_t   pdif = beamdir[*(int *)b1p].fo - beamdir[*(int *)b2p].fo; | 
| 497 | + |  | 
| 498 | + | if (pdif > 0L) return(1); | 
| 499 | + | if (pdif < 0L) return(-1); | 
| 500 | + | return(0); | 
| 501 | + | } | 
| 502 | + |  | 
| 503 |  | /* Write all rays from holodeck to stream */ | 
| 504 |  | static void | 
| 505 |  | writerays(FILE *fp) | 
| 520 |  | #endif | 
| 521 |  | for (sn = 0; sn < nholosects; sn++) {   /* write each holodeck section */ | 
| 522 |  | HOLO    *hp = hdlist[sn]; | 
| 523 | < | for (bi = nbeams(hp); bi > 0; bi--) { | 
| 524 | < | BEAM    *bp = hdgetbeam(hp, bi); | 
| 523 | > | int     nb = nbeams(hp);        /* sort beams by file location */ | 
| 524 | > | int     *bq = (int *)malloc(nb*sizeof(int)); | 
| 525 | > | if (!bq) | 
| 526 | > | error(SYSTEM, "out of memory in writerays()"); | 
| 527 | > | for (bi = nb; bi--; ) bq[bi] = bi+1; | 
| 528 | > | beamdir = hp->bi; | 
| 529 | > | qsort(bq, nb, sizeof(*bq), bpcmp); | 
| 530 | > | for (bi = 0; bi < nb; bi++) { | 
| 531 | > | BEAM    *bp = hdgetbeam(hp, bq[bi]); | 
| 532 |  | if (!bp)                /* empty beam? */ | 
| 533 |  | continue; | 
| 534 | < | hdbcoord(gc, hp, bi);   /* else write rays */ | 
| 534 | > | hdbcoord(gc, hp, bq[bi]); | 
| 535 |  | rv = hdbray(bp); | 
| 536 |  | for (k = bp->nrm; k--; rv++) { | 
| 537 | + | RREAL   hitd = hddepth(hp, rv->d); | 
| 538 |  | ryp.d = hdray(ryp.ro, ryp.rd, hp, gc, rv->r); | 
| 539 |  | if (*(int *)hp->priv & H_OBSF) | 
| 540 |  | VSUM(ryp.ro, ryp.ro, ryp.rd, ryp.d); | 
| 541 | < | else | 
| 542 | < | ryp.d = 0.; | 
| 543 | < | ryp.d = hddepth(hp, rv->d) - ryp.d; | 
| 541 | > | else if (*(int *)hp->priv & H_OBST) | 
| 542 | > | ryp.d = 0; | 
| 543 | > | else if (hitd < ryp.d) { | 
| 544 | > | ryp.d = 0.97*hitd; | 
| 545 | > | VSUM(ryp.ro, ryp.ro, ryp.rd, ryp.d); | 
| 546 | > | } else | 
| 547 | > | ryp.d = 0; | 
| 548 | > | ryp.d = hitd - ryp.d; | 
| 549 |  | copycolr(ryp.cv, rv->v); | 
| 550 | < | if (!write_ray(&ryp, fp)) | 
| 550 | > | if (!write_ray(&ryp, fp)) { | 
| 551 | > | free(bq); | 
| 552 |  | goto writError; | 
| 553 | + | } | 
| 554 |  | } | 
| 555 | < | hdfreebeam(hp, bi); | 
| 555 | > | hdfreebeam(hp, bq[bi]); | 
| 556 |  | } | 
| 557 | + | free(bq); | 
| 558 |  | } | 
| 559 |  | if (fflush(fp) != EOF) | 
| 560 |  | return; | 
| 562 |  | error(SYSTEM, "error writing holodeck rays"); | 
| 563 |  | } | 
| 564 |  |  | 
| 527 | – | static BEAMI    *beamdir; | 
| 528 | – |  | 
| 565 |  | static int | 
| 530 | – | bpcmp(                  /* compare beam positions on disk */ | 
| 531 | – | const void      *b1p, | 
| 532 | – | const void      *b2p | 
| 533 | – | ) | 
| 534 | – | { | 
| 535 | – | off_t   pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo; | 
| 536 | – |  | 
| 537 | – | if (pdif > 0L) return(1); | 
| 538 | – | if (pdif < 0L) return(-1); | 
| 539 | – | return(0); | 
| 540 | – | } | 
| 541 | – |  | 
| 542 | – | static int | 
| 566 |  | addclump(               /* transfer the given clump and free */ | 
| 567 |  | HOLO    *hp, | 
| 568 |  | int     *bq, | 
| 577 |  | BEAM    *bp; | 
| 578 |  | /* sort based on file position */ | 
| 579 |  | beamdir = hp->bi; | 
| 580 | < | qsort((char *)bq, nb, sizeof(*bq), bpcmp); | 
| 580 | > | qsort(bq, nb, sizeof(*bq), bpcmp); | 
| 581 |  | /* transfer each beam */ | 
| 582 |  | for (i = 0; i < nb; i++) { | 
| 583 |  | bp = hdgetbeam(hp, bq[i]); | 
| 759 |  |  | 
| 760 |  | void | 
| 761 |  | eputs(                  /* put error message to stderr */ | 
| 762 | < | char  *s | 
| 762 | > | const char  *s | 
| 763 |  | ) | 
| 764 |  | { | 
| 765 |  | static int  midline = 0; |