--- ray/src/hd/rhoptimize.c 1998/11/09 17:11:40 3.4 +++ ray/src/hd/rhoptimize.c 2003/10/20 16:01:55 3.16 @@ -1,28 +1,27 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhoptimize.c,v 3.16 2003/10/20 16:01:55 greg Exp $"; #endif - /* * Optimize holodeck for quick access. * * 11/4/98 Greg Ward Larson */ -#include "holo.h" - #include +#include +#include "rtprocess.h" /* getpid() */ +#include "holo.h" +#include "platform.h" + #ifndef BKBSIZE #define BKBSIZE 256 /* beam clump size (kilobytes) */ #endif char *progname; char tempfile[128]; +int dupchecking = 0; -extern char *rindex(); -extern int quit(); extern long rhinitcopy(); @@ -35,20 +34,27 @@ char *argv[]; long nextipos, lastopos, thisopos; progname = argv[0]; - if (argc < 2 | argc > 3) { - fprintf(stderr, "Usage: %s input.hdk [output.hdk]\n", progname); + argv++; argc--; /* duplicate checking flag? */ + if (argc > 1 && !strcmp(argv[0], "-u")) { + dupchecking++; + argv++; argc--; + } + if ((argc < 1) | (argc > 2)) { + fprintf(stderr, "Usage: %s [-u] input.hdk [output.hdk]\n", + progname); exit(1); } - inpname = argv[1]; - if (argc == 3) /* use given output file */ - outname = argv[2]; + inpname = argv[0]; /* get input file */ + argv++; argc--; + if (argc == 1) /* use given output file */ + outname = argv[0]; else { /* else use temporary file */ if (access(inpname, R_OK|W_OK) < 0) { /* check permissions */ sprintf(errmsg, "cannot access \"%s\"", inpname); error(SYSTEM, errmsg); } strcpy(tempfile, inpname); - if ((outname = rindex(tempfile, '/')) != NULL) + if ((outname = strrchr(tempfile, '/')) != NULL) outname++; else outname = tempfile; @@ -60,14 +66,14 @@ char *argv[]; lastopos = 0L; /* copy sections one by one */ while (nextipos != 0L) { /* set input position; get next */ - lseek(hdfd[0], nextipos, 0); + lseek(hdfd[0], (off_t)nextipos, SEEK_SET); read(hdfd[0], (char *)&nextipos, sizeof(nextipos)); /* get output position; set last */ - thisopos = lseek(hdfd[1], 0L, 2); + thisopos = lseek(hdfd[1], (off_t)0, SEEK_END); if (lastopos > 0L) { - lseek(hdfd[1], lastopos, 0); + lseek(hdfd[1], (off_t)lastopos, SEEK_SET); write(hdfd[1], (char *)&thisopos, sizeof(thisopos)); - lseek(hdfd[1], 0L, 2); + lseek(hdfd[1], (off_t)0, SEEK_END); } lastopos = thisopos; thisopos = 0L; /* write place holder */ @@ -99,6 +105,10 @@ char *infn, *outfn; sprintf(errmsg, "cannot open \"%s\" for reading", infn); error(SYSTEM, errmsg); } + if (access(outfn, F_OK) == 0) { + sprintf(errmsg, "output file \"%s\" already exists!", outfn); + error(USER, errmsg); + } if ((outfp = fopen(outfn, "w+")) == NULL) { sprintf(errmsg, "cannot open \"%s\" for writing", outfn); error(SYSTEM, errmsg); @@ -133,16 +143,66 @@ char *infn, *outfn; } -static HOLO *hout; /* output holodeck section */ +int +nuniq(rva, n) /* sort unique rays to front of beam list */ +register RAYVAL *rva; +int n; +{ + register int i, j; + RAYVAL rtmp; + for (j = 0; j < n; j++) + for (i = j+1; i < n; i++) + if ( rva[i].d == rva[j].d && + rva[i].r[0][0]==rva[j].r[0][0] && + rva[i].r[0][1]==rva[j].r[0][1] && + rva[i].r[1][0]==rva[j].r[1][0] && + rva[i].r[1][1]==rva[j].r[1][1] ) { + n--; /* swap duplicate with end */ + rtmp = *(rva+n); + *(rva+n) = *(rva+i); + *(rva+i) = rtmp; + i--; /* recheck one we swapped */ + } + return(n); +} + + +static BEAMI *beamdir; + static int -xferbeam(bp, hb) /* transfer the given beam to hout and free */ -register BEAM *bp; -HDBEAMI *hb; +bpcmp(b1p, b2p) /* compare beam positions on disk */ +int *b1p, *b2p; { - bcopy((char *)hdbray(bp), (char *)hdnewrays(hout,hb->b,bp->nrm), - bp->nrm*sizeof(RAYVAL)); - hdfreebeam(hb->h, hb->b); + register off_t pdif = beamdir[*b1p].fo - beamdir[*b2p].fo; + + if (pdif < 0L) return(-1); + return(pdif > 0L); +} + +static HOLO *hout; + +static int +xferclump(hp, bq, nb) /* transfer the given clump to hout and free */ +HOLO *hp; +int *bq, nb; +{ + register int i; + register BEAM *bp; + int n; + + beamdir = hp->bi; /* sort based on file position */ + qsort((void *)bq, nb, sizeof(*bq), bpcmp); + /* transfer and free each beam */ + for (i = 0; i < nb; i++) { + bp = hdgetbeam(hp, bq[i]); + DCHECK(bp==NULL, CONSISTENCY, "empty beam in xferclump"); + n = dupchecking ? nuniq(hdbray(bp),bp->nrm) : bp->nrm; + memcpy((void *)hdnewrays(hout,bq[i],n),(void *)hdbray(bp), + n*sizeof(RAYVAL)); + hdfreebeam(hp, bq[i]); + } + hdfreebeam(hout, 0); /* write & free clump */ return(0); } @@ -155,13 +215,14 @@ int ifd, ofd; /* create output section directory */ hout = hdinit(ofd, (HDGRID *)hinp); /* clump the beams */ - clumpbeams(hinp, 0, BKBSIZE*1024, xferbeam); + clumpbeams(hinp, 0, BKBSIZE*1024, xferclump); /* clean up */ hddone(hinp); hddone(hout); } +void eputs(s) /* put error message to stderr */ register char *s; { @@ -181,6 +242,7 @@ register char *s; } +void quit(code) /* exit the program gracefully */ int code; {