00001 #include "histogrm.h"
00002
00007 #include <glt/viewport.h>
00008 #include <glt/error.h>
00009 #include <glt/glu.h>
00010 #include <glt/rgb.h>
00011
00012 #include <node/fields.h>
00013
00014 #include <cmath>
00015 using namespace std;
00016
00017 GltHistogram::GltHistogram()
00018 :
00019 _min(0.0),
00020 _max(1.0),
00021 _size(0),
00022 _cumulative(true),
00023 _drawLine(true),
00024 _samples(0),
00025 _outlyers(0)
00026 {
00027 color() = yellow;
00028 name() = "histogram";
00029 }
00030
00031 GltHistogram::~GltHistogram()
00032 {
00033 }
00034
00035 GltFieldPtr
00036 GltHistogram::settings()
00037 {
00038 GltFields *root = new GltFields(name());
00039
00040 root->add(visible() ,"display" );
00041 root->add(_drawLine ,"line" );
00042 root->add(color() ,"color" );
00043 root->add(_min ,1.0,"min" );
00044 root->add(_max ,1.0,"max" );
00045 root->add(_size ,"size" );
00046 root->add(_cumulative ,"cumulative" );
00047 root->add(new GltFieldFunc<GltHistogram>(*this,&GltHistogram::reset,"reset",true));
00048
00049 return root;
00050 }
00051
00052 void
00053 GltHistogram::draw() const
00054 {
00055 if (!visible() || _min==_max || _min>_max || !_count.size())
00056 return;
00057
00058 const unsigned int max = maxCount();
00059
00060 GLERROR;
00061
00062 glPushMatrix();
00063 glLoadIdentity();
00064 glMatrixMode(GL_PROJECTION);
00065 glPushMatrix();
00066 glLoadIdentity();
00067
00068
00069
00070
00071 GltViewport viewport(true);
00072 gluOrtho2D(0,_count.size(),0,MAX(max,1));
00073
00074 GLERROR;
00075
00076 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
00077
00078 glDisable(GL_LIGHTING);
00079 glDisable(GL_DEPTH_TEST);
00080 glPushMatrix();
00081 glScaled(0.99,0.99,1.0);
00082
00083 glColor();
00084
00085 if (_drawLine)
00086 {
00087 glTranslatef(0.5,0.0,0.0);
00088 glBegin(GL_LINE_STRIP);
00089 for (uint32 i=0; i<_count.size(); i++)
00090 glVertex2i(i,_count[i]);
00091 glEnd();
00092 }
00093 else
00094 {
00095 glBegin(GL_LINE_STRIP);
00096 glVertex2i(0,0);
00097 for (uint32 i=0; i<_count.size(); i++)
00098 {
00099 glVertex2i(i,_count[i]);
00100 glVertex2i(i+1,_count[i]);
00101 }
00102 glVertex2i(_count.size(),0);
00103 glEnd();
00104 }
00105
00106 glPopMatrix();
00107
00108 glPopAttrib();
00109
00110 glPopMatrix();
00111 glMatrixMode(GL_MODELVIEW);
00112 glPopMatrix();
00113
00114 GLERROR;
00115 }
00116
00117 void
00118 GltHistogram::reset()
00119 {
00120 _size = MAX(_size,0);
00121 _count.clear();
00122 _count.resize(_size);
00123 for (int i=0; i<_size; i++)
00124 _count[i] = 0;
00125 _samples = 0;
00126 _outlyers = 0;
00127 }
00128
00129 void
00130 GltHistogram::add(const double x)
00131 {
00132 add((int) floor(double(x-_min)/double(_max-_min)*double(_count.size())));
00133 }
00134
00135 void
00136 GltHistogram::add(const int n)
00137 {
00138 if (int(_count.size())!=_size)
00139 reset();
00140
00141 _samples++;
00142
00143 if (n>=0 && n<(int)_count.size())
00144 {
00145 _count[n]++;
00146
00147 if (_samples==(unsigned int)~0)
00148 {
00149 _samples >>= 1;
00150 for (uint32 i=0; i<_count.size(); i++)
00151 _count[i] >>= 1;
00152 }
00153 }
00154 else
00155 _outlyers++;
00156 }
00157
00158 bool &GltHistogram::drawLine() { return _drawLine; }
00159 double &GltHistogram::min() { return _min; }
00160 double &GltHistogram::max() { return _max; }
00161 int &GltHistogram::size() { return _size; }
00162 bool &GltHistogram::cumulative() { return _cumulative; }
00163
00164 unsigned int GltHistogram::samples() const { return _samples; }
00165 unsigned int GltHistogram::outlyers() const { return _outlyers; }
00166
00167 unsigned int GltHistogram::minCount() const
00168 {
00169 unsigned int min = ~0;
00170
00171 for (uint32 i=0; i<_count.size(); i++)
00172 min = MIN(min,_count[i]);
00173
00174 return min;
00175 }
00176
00177 unsigned int GltHistogram::maxCount() const
00178 {
00179 unsigned int max = 0;
00180
00181 for (uint32 i=0; i<_count.size(); i++)
00182 max = MAX(max,_count[i]);
00183
00184 return max;
00185 }
00186