Back to index

radiance  4R0+20100331
rhinfo.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: rhinfo.c,v 3.11 2004/09/09 01:06:19 greg Exp $";
00003 #endif
00004 /*
00005  * Get general information on holodeck file
00006  */
00007 
00008 #include <stdio.h>
00009 
00010 #include "platform.h"
00011 #include "resolu.h"
00012 #include "holo.h"
00013 
00014 #ifndef NHBINS
00015 #define NHBINS              12     /* number of histogram bins to use for stats */
00016 #endif
00017 
00018 char   *progname;           /* global argv[0] */
00019 
00020 long   beamtot, samptot;    /* total beams and samples */
00021 
00022 static void gethdinfo(char  *fname, FILE  *fout);
00023 static void psectstats(HOLO *hp, FILE     *fp);
00024 
00025 
00026 int
00027 main(
00028        int    argc,
00029        char   *argv[]
00030 )
00031 {
00032        progname = argv[0];
00033        if (argc != 2)
00034               goto userr;
00035        gethdinfo(argv[1], stdout);
00036        quit(0);
00037 userr:
00038        fprintf(stderr, "Usage: %s input.hdk\n", progname);
00039        exit(1);
00040 }
00041 
00042 
00043 static void
00044 gethdinfo(           /* get information on holodeck */
00045        char   *fname,
00046        FILE   *fout
00047 )
00048 {
00049        FILE   *fp;
00050        HOLO   *hdsect;
00051        int    fd;
00052        int32  nextloc;
00053        int    n;
00054                                    /* open holodeck file */
00055        if ((fp = fopen(fname, "r")) == NULL) {
00056               sprintf(errmsg, "cannot open \"%s\"", fname);
00057               error(SYSTEM, errmsg);
00058        }
00059                                    /* check header and magic number */
00060        if (checkheader(fp, HOLOFMT, fout) < 0 || getw(fp) != HOLOMAGIC) {
00061               sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
00062               error(USER, errmsg);
00063        }
00064        fd = dup(fileno(fp));                     /* dup file handle */
00065        nextloc = ftell(fp);               /* get stdio position */
00066        fclose(fp);                        /* done with stdio */
00067        for (n = 0; nextloc > 0L; n++) {   /* get the section(s) */
00068               lseek(fd, (off_t)nextloc, SEEK_SET);
00069               read(fd, (char *)&nextloc, sizeof(nextloc));
00070               fprintf(fout, "Section %d:\n", n);
00071               hdsect = hdinit(fd, NULL);  /* load section directory */
00072               psectstats(hdsect, fout);   /* print section statistics */
00073        }
00074        nextloc = hdfilen(fd);                    /* print global statistics */
00075        fputs("=====================================================\n", fout);
00076        fprintf(fout, "Total samples/beams: %ld/%ld (%.2f samples/beam)\n",
00077                      samptot, beamtot, (double)samptot/beamtot);
00078        fprintf(fout, "%.1f Mbyte file, %.1f%% fragmentation\n",
00079                      nextloc/(1024.*1024.),
00080                      100.*(nextloc-hdfiluse(fd))/nextloc);
00081                                           /* don't bother with cleanup */
00082 #if 0
00083        hddone(NULL);                      /* free sections */
00084        close(fd);                         /* done with the holodeck */
00085 #endif
00086 }
00087 
00088 
00089 static void
00090 psectstats(          /* print statistical information for section */
00091        register HOLO *hp,
00092        FILE   *fp
00093 )
00094 {
00095        int    scount[NHBINS];
00096        int    minsamp = 10000, maxsamp = 0;
00097        FVECT  vt;
00098        double sqrtmaxp;
00099        int    bmin, bmax, cnt;
00100        register int  i;
00101 
00102        fcross(vt, hp->xv[0], hp->xv[1]);
00103        fprintf(fp, "\tWorld volume:            %g\n", fabs(DOT(vt,hp->xv[2])));
00104        fprintf(fp, "\tGrid resolution:         %d x %d x %d\n",
00105                      hp->grid[0], hp->grid[1], hp->grid[2]);
00106        fprintf(fp, "\tNumber of beams:         %ld\n", (long)nbeams(hp));
00107        beamtot += nbeams(hp);
00108        fprintf(fp, "\tNumber of ray samples:   %ld\n", (long)biglob(hp)->nrd);
00109        samptot += biglob(hp)->nrd;
00110        if (biglob(hp)->nrd <= 0)
00111               return;                            /* no samples to stat! */
00112        for (i = nbeams(hp); i > 0; i--) {
00113               if (hp->bi[i].nrd < minsamp)
00114                      minsamp = hp->bi[i].nrd;
00115               if (hp->bi[i].nrd > maxsamp)
00116                      maxsamp = hp->bi[i].nrd;
00117        }
00118        sqrtmaxp = sqrt(maxsamp+1.0);
00119        for (i = NHBINS; i--; )
00120               scount[i] = 0;
00121        for (i = nbeams(hp); i > 0; i--)
00122               scount[(int)(NHBINS*sqrt((double)hp->bi[i].nrd)/sqrtmaxp)]++;
00123        for (cnt = 0, i = 0; i < NHBINS && cnt<<1 < nbeams(hp); i++)
00124               cnt += scount[i];
00125        fprintf(fp, "\tSamples per beam:        [min,med,max]= [%d,%.0f,%d]\n",
00126                      minsamp,
00127                      (i-.5)*(i-.5)*(maxsamp+1)/(NHBINS*NHBINS),
00128                      maxsamp);
00129        fprintf(fp, "\tHistogram: [minsamp,maxsamp)= #beams\n");
00130        bmax = 0;
00131        for (i = 0; i < NHBINS; i++) {
00132               bmin = bmax;
00133               bmax = (i+1)*(i+1)*(maxsamp+1)/(NHBINS*NHBINS);
00134               fprintf(fp, "\t\t[%d,%d)= %d\n", bmin, bmax, scount[i]);
00135        }
00136 }