Back to index

radiance  4R0+20100331
Defines | Functions | Variables
rhoptimize.c File Reference
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include "platform.h"
#include "rterror.h"
#include "resolu.h"
#include "rtprocess.h"
#include "holo.h"

Go to the source code of this file.

Defines

#define BKBSIZE   256 /* beam clump size (kilobytes) */

Functions

static long rhinitcopy (int hfd[2], char *infn, char *outfn)
static int nuniq (RAYVAL *rva, int n)
static int bpcmp (const void *b1p, const void *b2p)
static int xferclump (HOLO *hp, int *bq, int nb)
static void copysect (int ifd, int ofd)
int main (int argc, char *argv[])
static int nuniq (register RAYVAL *rva, int n)
void eputs (char *s)
void quit (int code)

Variables

static const char RCSid [] = "$Id: rhoptimize.c,v 3.18 2004/01/01 11:21:55 schorsch Exp $"
char * progname
char tempfile [128]
int dupchecking = 0
static BEAMIbeamdir
static HOLOhout

Define Documentation

#define BKBSIZE   256 /* beam clump size (kilobytes) */

Definition at line 21 of file rhoptimize.c.


Function Documentation

static int bpcmp ( const void *  b1p,
const void *  b2p 
) [static]

Definition at line 186 of file rhoptimize.c.

{
       register off_t       pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo;

       if (pdif < 0L) return(-1);
       return(pdif > 0L);
}

Here is the caller graph for this function:

static void copysect ( int  ifd,
int  ofd 
) [static]

Definition at line 226 of file rhoptimize.c.

{
       HOLO   *hinp;
                                   /* load input section directory */
       hinp = hdinit(ifd, NULL);
                                   /* create output section directory */
       hout = hdinit(ofd, (HDGRID *)hinp);
                                   /* clump the beams */
       clumpbeams(hinp, 0, BKBSIZE*1024, xferclump);
                                   /* clean up */
       hddone(hinp);
       hddone(hout);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void eputs ( char *  s)

Definition at line 245 of file rhoptimize.c.

{
       static int  midline = 0;

       if (!*s)
              return;
       if (!midline++) {    /* prepend line with program name */
              fputs(progname, stderr);
              fputs(": ", stderr);
       }
       fputs(s, stderr);
       if (s[strlen(s)-1] == '\n') {
              fflush(stderr);
              midline = 0;
       }
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 36 of file rhoptimize.c.

{
       char   *inpname, *outname;
       int    hdfd[2];
       long   nextipos, lastopos, thisopos;

       progname = argv[0];
       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[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 = strrchr(tempfile, '/')) != NULL)
                     outname++;
              else
                     outname = tempfile;
              sprintf(outname, "rho%d.hdk", getpid());
              outname = tempfile;
       }
                                   /* copy holodeck file header */
       nextipos = rhinitcopy(hdfd, inpname, outname);
       lastopos = 0L;                     /* copy sections one by one */
       while (nextipos != 0L) {
                                   /* set input position; get next */
              lseek(hdfd[0], (off_t)nextipos, SEEK_SET);
              read(hdfd[0], (char *)&nextipos, sizeof(nextipos));
                                   /* get output position; set last */
              thisopos = lseek(hdfd[1], (off_t)0, SEEK_END);
              if (lastopos > 0L) {
                     lseek(hdfd[1], (off_t)lastopos, SEEK_SET);
                     write(hdfd[1], (char *)&thisopos, sizeof(thisopos));
                     lseek(hdfd[1], (off_t)0, SEEK_END);
              }
              lastopos = thisopos;
              thisopos = 0L;              /* write place holder */
              write(hdfd[1], (char *)&thisopos, sizeof(thisopos));
                                   /* copy holodeck section */
              copysect(hdfd[0], hdfd[1]);
       }
                                   /* clean up */
       close(hdfd[0]);
       close(hdfd[1]);
       if (outname == tempfile && rename(outname, inpname) < 0) {
              sprintf(errmsg, "cannot rename \"%s\" to \"%s\"",
                            outname, inpname);
              error(SYSTEM, errmsg);
       }
       return 0;
}

Here is the call graph for this function:

static int nuniq ( RAYVAL rva,
int  n 
) [static]

Here is the caller graph for this function:

static int nuniq ( register RAYVAL rva,
int  n 
) [static]

Definition at line 158 of file rhoptimize.c.

{
       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);
}
void quit ( int  code)

Definition at line 265 of file rhoptimize.c.

{
       if (tempfile[0])
              unlink(tempfile);
       exit(code);
}
static long rhinitcopy ( int  hfd[2],
char *  infn,
char *  outfn 
) [static]

Definition at line 106 of file rhoptimize.c.

{
       FILE   *infp, *outfp;
       long   ifpos;
                                   /* open files for i/o */
       if ((infp = fopen(infn, "r")) == NULL) {
              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);
       }
                                   /* set up signal handling */
       if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN);
       if (signal(SIGHUP, quit) == SIG_IGN) signal(SIGHUP, SIG_IGN);
       if (signal(SIGTERM, quit) == SIG_IGN) signal(SIGTERM, SIG_IGN);
#ifdef SIGXCPU
       if (signal(SIGXCPU, quit) == SIG_IGN) signal(SIGXCPU, SIG_IGN);
       if (signal(SIGXFSZ, quit) == SIG_IGN) signal(SIGXFSZ, SIG_IGN);
#endif
                                   /* copy and verify header */
       if (checkheader(infp, HOLOFMT, outfp) < 0 || getw(infp) != HOLOMAGIC)
              error(USER, "input not in holodeck format");
       fputformat(HOLOFMT, outfp);
       fputc('\n', outfp);
       putw(HOLOMAGIC, outfp);
                                   /* get descriptors and free stdio */
       if ((hfd[0] = dup(fileno(infp))) < 0 ||
                     (hfd[1] = dup(fileno(outfp))) < 0)
              error(SYSTEM, "dup call failed in rhinitcopy");
       ifpos = ftell(infp);
       fclose(infp);
       if (fclose(outfp) == EOF)
              error(SYSTEM, "file flushing error in rhinitcopy");
                                   /* check cache size */
       if (BKBSIZE*1024*1.5 > hdcachesize)
              hdcachesize = BKBSIZE*1024*1.5;
                                   /* return input position */
       return(ifpos);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int xferclump ( HOLO hp,
int *  bq,
int  nb 
) [static]

Definition at line 200 of file rhoptimize.c.

{
       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);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

BEAMI* beamdir [static]

Definition at line 183 of file rhoptimize.c.

int dupchecking = 0

Definition at line 26 of file rhoptimize.c.

HOLO* hout [static]

Definition at line 197 of file rhoptimize.c.

char* progname

Definition at line 24 of file rhoptimize.c.

const char RCSid[] = "$Id: rhoptimize.c,v 3.18 2004/01/01 11:21:55 schorsch Exp $" [static]

Definition at line 2 of file rhoptimize.c.

char tempfile[128]

Definition at line 25 of file rhoptimize.c.