00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
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;
00072 }
00073
00074
00075
00076
00077
00078
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;
00087 } else if( nbins > 10000 ) {
00088 nbins = 10000;
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
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
00132
00133
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
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
00161 for( ; nbins >= 1; nbins-- )
00162 if(histp->hg_bins[nbins] > 0) break;
00163 }
00164
00165
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
00173 i = 0;
00174 if( zero_suppress ) {
00175
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
00205
00206
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
00218
00219
00220
00221
00222
00223
00224