--- ray/src/hd/rhcopy.c 1997/12/18 09:33:42 3.1 +++ ray/src/hd/rhcopy.c 2003/07/21 22:30:18 3.18 @@ -1,20 +1,22 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhcopy.c,v 3.18 2003/07/21 22:30:18 schorsch Exp $"; #endif - /* * Copy data into a holodeck file */ #include "holo.h" #include "view.h" -#include "resolu.h" +#ifndef BKBSIZE +#define BKBSIZE 256 /* beam clump size (kilobytes) */ +#endif + +int checkdepth = 1; /* check depth (!-d option)? */ +int checkrepeats = 0; /* check for repeats (-u option)? */ int frompicz; /* input from pictures & depth-buffers? */ int noutsects; /* number of output sections */ -int obstructions = -1; /* interior obstructions allowed? */ +char obstr, unobstr; /* flag pointer values */ char *progname; /* global argv[0] */ @@ -26,66 +28,89 @@ char *argv[]; int i; progname = argv[0]; - if (argc < 4) + frompicz = -1; + for (i = 2; i < argc && argv[i][0] == '-'; i++) + switch (argv[i][1]) { + case 'u': + checkrepeats = 1; + break; + case 'd': + checkdepth = 0; + break; + case 'h': + frompicz = 0; + break; + case 'p': + frompicz = 1; + break; + default: + goto userr; + } + if (i >= argc || frompicz < 0) goto userr; - if (!strcmp(argv[2], "-h")) - frompicz = 0; - else if (!strcmp(argv[2], "-pz")) - frompicz = 1; - else + if (frompicz && (argc-i)%2) goto userr; - if (frompicz && (argc-3)%2) - goto userr; noutsects = openholo(argv[1], 1); - if (frompicz) - for (i = 3; i < argc; i += 2) + if (frompicz) { + for ( ; i < argc; i += 2) addpicz(argv[i], argv[i+1]); - else - for (i = 3; i < argc; i++) + } else { + if (BKBSIZE*1024*1.5 > hdcachesize) + hdcachesize = BKBSIZE*1024*1.5; + for ( ; i < argc; i++) addholo(argv[i]); + } quit(0); userr: - fprintf(stderr, "Usage: %s output.hdk -h inp1.hdk ..\n", progname); - fprintf(stderr, " Or: %s output.hdk -pz inp1.pic inp1.zbf ..\n", + fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n", progname); + fprintf(stderr, " Or: %s output.hdk [-u][-d] -p inp1.pic inp1.zbf ..\n", + progname); exit(1); } -holheadline(s, bf) /* check holodeck header line */ +#define H_BADF 01 +#define H_OBST 02 +#define H_OBSF 04 + +int +holheadline(s, hf) /* check holodeck header line */ register char *s; -int *bf; +int *hf; { char fmt[32]; if (formatval(fmt, s)) { - *bf = strcmp(fmt, HOLOFMT); - return; + if (strcmp(fmt, HOLOFMT)) + *hf |= H_BADF; + else + *hf &= ~H_BADF; + return(0); } if (!strncmp(s, "OBSTRUCTIONS=", 13)) { s += 13; while (*s == ' ') s++; if (*s == 't' | *s == 'T') - obstructions = 1; + *hf |= H_OBST; else if (*s == 'f' | *s == 'F') - obstructions = 0; + *hf |= H_OBSF; else error(WARNING, "bad OBSTRUCTIONS value in holodeck"); - return; + return(0); } + return(0); } - int openholo(fname, append) /* open existing holodeck file for i/o */ char *fname; int append; { - extern long ftell(); FILE *fp; int fd; - int badfmt = 0; - int4 nextloc; + int hflags = 0; + long nextloc; int n; /* open holodeck file */ if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) { @@ -94,11 +119,8 @@ int append; error(SYSTEM, errmsg); } /* check header and magic number */ - if (append) - badfmt |= getheader(fp, holheadline, &badfmt) < 0; - else - badfmt = checkheader(fp, HOLOFMT, NULL) < 0; - if (badfmt || getw(fp) != HOLOMAGIC) { + if (getheader(fp, holheadline, (char *)&hflags) < 0 || + hflags&H_BADF || getw(fp) != HOLOMAGIC) { sprintf(errmsg, "file \"%s\" not in holodeck format", fname); error(USER, errmsg); } @@ -106,24 +128,31 @@ int append; nextloc = ftell(fp); /* get stdio position */ fclose(fp); /* done with stdio */ for (n = 0; nextloc > 0L; n++) { /* initialize each section */ - lseek(fd, (long)nextloc, 0); + lseek(fd, (off_t)nextloc, 0); read(fd, (char *)&nextloc, sizeof(nextloc)); - hdinit(fd, NULL); + hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr : + hflags&H_OBSF ? &unobstr : (char *)NULL; } return(n); } +#undef H_BADF +#undef H_OBST +#undef H_OBSF + addray(ro, rd, d, cv) /* add a ray to our output holodeck */ FVECT ro, rd; double d; COLR cv; { - int sn; + int sn, bi, n; register HOLO *hp; GCOORD gc[2]; BYTE rr[2][2]; + BEAM *bp; double d0, d1; + unsigned dc; register RAYVAL *rv; /* check each output section */ for (sn = noutsects; sn--; ) { @@ -131,48 +160,94 @@ COLR cv; d0 = hdinter(gc, rr, &d1, hp, ro, rd); if (d <= d0 || d1 < -0.001) continue; /* missed section */ - if (obstructions > 0 && d0 < -0.001) - continue; /* ray starts too late */ - if (!obstructions && d < 0.999*d1) - continue; /* ray ends too soon */ - /* should we check for duplicates? */ - rv = hdnewrays(hp, hdbindex(hp, gc), 1); + if (checkdepth) { /* check depth */ + if (hp->priv == &obstr && d0 < -0.001) + continue; /* ray starts too late */ + if (hp->priv == &unobstr && d < 0.999*d1) + continue; /* ray ends too soon */ + } + dc = hdcode(hp, d-d0); + bi = hdbindex(hp, gc); /* check for duplicates */ + if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) { + for (n = bp->nrm, rv = hdbray(bp); n--; rv++) + if ((hp->priv != NULL || rv->d == dc) && + rv->r[0][0] == rr[0][0] && + rv->r[0][1] == rr[0][1] && + rv->r[1][0] == rr[1][0] && + rv->r[1][1] == rr[1][1]) + break; + if (n >= 0) + continue; /* found a matching ray */ + } + rv = hdnewrays(hp, bi, 1); + rv->d = dc; rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1]; rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1]; copycolr(rv->v, cv); - rv->d = hdcode(hp, d-d0); } } -addholo(hdf) /* add a holodeck file */ -char *hdf; +static BEAMI *beamdir; + +static int +bpcmp(b1p, b2p) /* compare beam positions on disk */ +int *b1p, *b2p; { - int fd; - register HOLO *hp; - register BEAM *bp; + register off_t pdif = beamdir[*b1p].fo - beamdir[*b2p].fo; + + if (pdif > 0L) return(1); + if (pdif < 0L) return(-1); + return(0); +} + +static int +addclump(hp, bq, nb) /* transfer the given clump and free */ +HOLO *hp; +int *bq, nb; +{ GCOORD gc[2]; FVECT ro, rd; double d; - int i, j; + int i; register int k; + register BEAM *bp; + /* sort based on file position */ + beamdir = hp->bi; + qsort((char *)bq, nb, sizeof(*bq), bpcmp); + /* transfer each beam */ + for (i = 0; i < nb; i++) { + bp = hdgetbeam(hp, bq[i]); + hdbcoord(gc, hp, bq[i]); + /* add each ray to output */ + for (k = bp->nrm; k--; ) { + d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r); + if (hp->priv == &unobstr) + VSUM(ro, ro, rd, d); + else + d = 0.; + d = hddepth(hp, hdbray(bp)[k].d) - d; + addray(ro, rd, d, hdbray(bp)[k].v); + } + hdfreebeam(hp, bq[i]); /* free the beam */ + } + return(0); +} - openholo(hdf, 0); /* open the holodeck for reading */ +addholo(hdf) /* add a holodeck file */ +char *hdf; +{ + int fd; + /* open the holodeck for reading */ + openholo(hdf, 0); fd = hdlist[noutsects]->fd; /* remember the file handle */ - while ((hp = hdlist[noutsects]) != NULL) { /* load each section */ - for (j = nbeams(hp); j > 0; j--) /* load each beam */ - if ((bp = hdgetbeam(hp, j)) != NULL) { - hdbcoord(gc, hp, j); - for (k = bp->nrm; k--; ) { - hdray(ro, rd, hp, gc, hdbray(bp)[k].r); - d = hddepth(hp, hdbray(bp)[k].d); - addray(ro, rd, d, hdbray(bp)[k].v); - } - hdfreebeam(hp, j); /* free the beam */ - } - hddone(hp); /* free the section */ + while (hdlist[noutsects] != NULL) { /* load each section */ + /* clump the beams */ + clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump); + hddone(hdlist[noutsects]); /* free the section */ } - close(fd); /* close the file */ + close(fd); /* close input file */ + hdflush(NULL); /* flush output */ } @@ -185,6 +260,7 @@ struct phead { }; +int picheadline(s, ph) /* process picture header line */ char *s; struct phead *ph; @@ -193,20 +269,21 @@ struct phead *ph; if (formatval(fmt, s)) { ph->badfmt = strcmp(fmt, COLRFMT); - return; + return(0); } if (isprims(s)) { ph->altprims++; /* don't want to deal with this */ - return; + return(0); } if (isexpos(s)) { ph->expos *= exposval(s); - return; + return(0); } if (isview(s)) { ph->gotview += sscanview(&ph->vw, s); - return; + return(0); } + return(0); } @@ -221,7 +298,7 @@ char *pcf, *zbf; int eshft; double emult; RESOLU prs; - FLOAT vl[2]; + RREAL vl[2]; FVECT ro, rd; double aftd; COLOR ctmp; @@ -237,10 +314,10 @@ char *pcf, *zbf; error(SYSTEM, pcf); } /* load picture header */ - copystruct(&phd.vw, &stdview); + phd.vw = stdview; phd.expos = 1.0; phd.badfmt = phd.gotview = phd.altprims = 0; - if (getheader(pfp, picheadline, &phd) < 0 || + if (getheader(pfp, picheadline, (char *)&phd) < 0 || phd.badfmt || !fgetsresolu(&prs, pfp)) { sprintf(errmsg, "bad format for picture file \"%s\"", pcf); error(USER, errmsg); @@ -305,14 +382,17 @@ char *pcf, *zbf; addray(ro, rd, (double)zscn[i], cscn[i]); } } + /* write output and free beams */ + hdflush(NULL); /* clean up */ - free((char *)cscn); - free((char *)zscn); + free((void *)cscn); + free((void *)zscn); fclose(pfp); close(zfd); } +void eputs(s) /* put error message to stderr */ register char *s; { @@ -332,6 +412,7 @@ register char *s; } +void quit(code) /* exit the program gracefully */ int code; {