hist.c

Go to the documentation of this file.
00001 /*                          H I S T . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1990-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup bu_hist */
00023 /*@{*/
00024 /** @file hist.c
00025  *
00026  *@brief
00027  *  General purpose histogram handling routines.
00028  *
00029  *  The macro RT_HISTOGRAM_TALLY is used to record items that
00030  *  live in a single "bin", while the subroutine rt_hist_range()
00031  *  is used to record items that may extend across multiple "bin"s.
00032  *
00033  *  @author  Michael John Muuss
00034  *
00035  *  @par Source -
00036  *  @n  SECAD/VLD Computing Consortium, Bldg 394
00037  *  @n  The U. S. Army Ballistic Research Laboratory
00038  *  @n  Aberdeen Proving Ground, Maryland  21005-5066
00039  *
00040  */
00041 
00042 #ifndef lint
00043 static const char RCShist[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/hist.c,v 14.10 2006/08/31 23:16:38 lbutler Exp $ (BRL)";
00044 #endif
00045 
00046 #include "common.h"
00047 
00048 
00049 
00050 #include <stdio.h>
00051 #include <math.h>
00052 #ifdef HAVE_STRING_H
00053 #include <string.h>
00054 #endif
00055 #include "machine.h"
00056 #include "bu.h"
00057 
00058 /**
00059  *                      B U _ H I S T _ F R E E
00060  */
00061 void
00062 bu_hist_free(struct bu_hist *histp)
00063 {
00064         if( histp && histp->magic == 0 )  return;
00065         if( histp->magic == -1 ) return;
00066         BU_CK_HIST(histp);
00067         if( histp->hg_bins )
00068                 bu_free( (char *)histp->hg_bins, "old bu_hist bins");
00069         histp->hg_bins = (long *)0;
00070         histp->hg_nbins = 0;
00071         histp->magic = -1;      /* sanity */
00072 }
00073 
00074 /**
00075  *                      B U _ H I S T _ I N I T
00076  *
00077  *  Initialize a bu_hist structure.
00078  *  It is expected that the structure is junk upon entry.
00079  */
00080 void
00081 bu_hist_init(struct bu_hist *histp, fastf_t min, fastf_t max, unsigned int nbins)
00082 {
00083 
00084         if( max <= min )  max = min+1;
00085         if( nbins < 1 )  {
00086                 nbins = 1;      /* nbins=1 makes for a nice 2-bin binary histogram */
00087         } else if( nbins > 10000 )  {
00088                 nbins = 10000;  /* This is a lot of lines to print out */
00089         }
00090 
00091         histp->hg_min = floor(min);
00092         histp->hg_max = ceil(max);
00093         histp->hg_nbins = nbins;
00094 
00095         histp->hg_clumpsize = ((max-min)/nbins);
00096         if( histp->hg_clumpsize <= 0 )  histp->hg_clumpsize = 1;
00097 
00098         histp->hg_nsamples = 0L;
00099         histp->hg_bins = (long *)bu_calloc( nbins+1, sizeof(long), "bu_hist bins");
00100         histp->magic = BU_HIST_MAGIC;
00101 }
00102 
00103 /**
00104  *                      B U _ H I S T _ R A N G E
00105  */
00106 void
00107 bu_hist_range(register struct bu_hist *hp, fastf_t low, fastf_t high)
00108 {
00109         long            a;
00110         long            b;
00111         register int    i;
00112 
00113         BU_CK_HIST(hp);
00114         if( low <= hp->hg_min )
00115                 a = 0;
00116         else
00117                 a = (low - hp->hg_min) / hp->hg_clumpsize;
00118         if( high >= hp->hg_max )
00119                 b = hp->hg_nbins-1;
00120         else
00121                 b = (high - hp->hg_min) / hp->hg_clumpsize;
00122         if( b >= hp->hg_nbins )  b = hp->hg_nbins-1;
00123 
00124         for( i=a; i <= b; i++ )  {
00125                 hp->hg_bins[i]++;
00126         }
00127         hp->hg_nsamples++;
00128 }
00129 
00130 /**
00131  *                      B U _ H I S T _ P R _ S U P P R E S S
00132  *
00133  *  Allows caller control over zero-suppression feature.
00134  */
00135 void
00136 bu_hist_pr_suppress(register const struct bu_hist *histp, const char *title, int zero_suppress)
00137 {
00138         register int    i;
00139         long            maxcount;
00140         static const char       marks[] = "################################################################";
00141 #define NMARKS  50
00142         char            buf[256];
00143         int             percent;
00144         unsigned int    mark_count;
00145         double          val;
00146         int             nbins;
00147 
00148         BU_CK_HIST(histp);
00149 
00150         /* Find entry with highest count */
00151         maxcount = 0L;
00152         for( i=0; i<=histp->hg_nbins; i++ )  {
00153                 if( histp->hg_bins[i] > maxcount )
00154                         maxcount = histp->hg_bins[i];
00155         }
00156         if( maxcount <= 0 )  maxcount = 1;
00157 
00158         nbins = histp->hg_nbins;
00159         if( zero_suppress )  {
00160                 /* Supress trailing bins with zero counts.  nbins s/b >= 1 */
00161                 for( ; nbins >= 1; nbins-- )
00162                         if(histp->hg_bins[nbins] > 0)  break;
00163         }
00164 
00165         /* 12345678 12345678 123 .... */
00166         bu_log("\nHistogram of %s\nmin=%g, max=%g, nbins=%d, clumpsize=%g\n%d samples collected, highest count was %d\n\n Value      Count Rel%%|  Bar Graph\n",
00167                 title,
00168                 histp->hg_min, histp->hg_max,
00169                 histp->hg_nbins, histp->hg_clumpsize,
00170                 histp->hg_nsamples, maxcount );
00171 
00172         /* Print each bin. */
00173         i = 0;
00174         if( zero_suppress )  {
00175                 /* Leading bins with zero counts are supressed. */
00176                 for( ; i <= nbins; i++ )  {
00177                         if(histp->hg_bins[i] > 0)  break;
00178                 }
00179         }
00180         for( ; i <= nbins; i++ )  {
00181                 percent = (int)(((double)histp->hg_bins[i])*100.0/maxcount);
00182                 mark_count = percent*NMARKS/100;
00183                 if( mark_count <= 0 && histp->hg_bins[i] > 0 )
00184                         mark_count = 1;
00185                 if( mark_count > NMARKS )  {
00186                         bu_log("mark_count = %d, NMARKS=%d, hg_bins[%d]=%d, maxcount\n",
00187                                 mark_count, NMARKS, i, histp->hg_bins[i], maxcount);
00188                         bu_bomb("bu_hist_pr() bogus mark_count\n");
00189                 }
00190                 if( mark_count <= 0 )  {
00191                         buf[0] = '\0';
00192                 } else {
00193                         bcopy( marks, buf, mark_count );
00194                         buf[mark_count] = '\0';
00195                 }
00196                 val = histp->hg_min + i*histp->hg_clumpsize;
00197                 bu_log("%8g %8d %3d |%s\n",
00198                         val,
00199                         histp->hg_bins[i], percent, buf );
00200         }
00201 }
00202 
00203 /**
00204  *                      B U _ H I S T _ P R
00205  *
00206  *  The original interface.
00207  */
00208 void
00209 bu_hist_pr(register const struct bu_hist *histp, const char *title)
00210 {
00211         bu_hist_pr_suppress( histp, title, 1 );
00212 }
00213 
00214 /*@}*/
00215 
00216 /*
00217  * Local Variables:
00218  * mode: C
00219  * tab-width: 8
00220  * c-basic-offset: 4
00221  * indent-tabs-mode: t
00222  * End:
00223  * ex: shiftwidth=4 tabstop=8
00224  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6