00001
00002 #include <blort/TomGine/tgPlot2D.h>
00003 #include <stdio.h>
00004
00005 #ifdef WIN32
00006 #include <GL/glew.h>
00007 #include <GL/gl.h>
00008 #include <GL/glu.h>
00009 #else
00010 #include <GL/gl.h>
00011 #include <GL/glu.h>
00012 #include <GL/glext.h>
00013 #endif
00014
00015 using namespace TomGine;
00016
00017 tgPlot2D::tgPlot2D(int x, int y, unsigned w, unsigned h)
00018 {
00019 this->x = x;
00020 this->y = y;
00021 this->w = w;
00022 this->h = h;
00023
00024 m_buffer_size = w;
00025
00026 x_min = 0.0f;
00027 x_max = 1.0f;
00028 y_min = 0.0f;
00029 y_max = 1.0f;
00030 x_scale = w;
00031 y_scale = h;
00032
00033 }
00034
00035 tgPlot2D::~tgPlot2D()
00036 {
00037
00038 }
00039
00040 float tgPlot2D::findmax(const std::vector<float> &x) const
00041 {
00042 if(x.empty())
00043 return 0.0f;
00044
00045 float fmax=0.0f;
00046 for(unsigned i=0; i<x.size(); i++)
00047 if(fmax<x[i])
00048 fmax=x[i];
00049
00050 return fmax;
00051 }
00052
00053 float tgPlot2D::findmin(const std::vector<float> &x) const
00054 {
00055 if(x.empty())
00056 return 0.0f;
00057
00058 float fmin=x[0];
00059 for(unsigned i=1; i<x.size(); i++)
00060 if(x[i]<fmin)
00061 fmin=x[i];
00062
00063 return fmin;
00064 }
00065
00066 void tgPlot2D::updateBuffer(float y1, float y2, float y3, float y4)
00067 {
00068 m_timer.Update();
00069
00070 float t = float(m_timer.GetApplicationTime());
00071
00072
00073
00074
00075 if(!m_buffer_x.empty()){
00076 float x = m_buffer_x[m_buffer_x.size()-1];
00077 float d = floor((t - x) * x_scale);
00078 if(d < float(w)/(m_buffer_size))
00079 return;
00080 }
00081
00082 float x_max = findmax(m_buffer_x);
00083 float x_min = findmin(m_buffer_x);
00084 float x_range = x_max - x_min;
00085
00086 if(x_range > (this->x_max - this->x_min) || m_buffer_x.size() >= m_buffer_size){
00087
00088 m_buffer_x.clear();
00089 m_buffer_y_1.clear();
00090 m_buffer_y_2.clear();
00091 m_buffer_y_3.clear();
00092 m_buffer_y_4.clear();
00093
00094 m_timer.Reset();
00095 t = 0.0f;
00096 }
00097
00098 m_buffer_x.push_back(t);
00099 m_buffer_y_1.push_back(y1);
00100 m_buffer_y_2.push_back(y2);
00101 m_buffer_y_3.push_back(y3);
00102 m_buffer_y_4.push_back(y4);
00103 }
00104
00105 void tgPlot2D::axis(float x_min, float x_max, float y_min, float y_max)
00106 {
00107 this->x_min = x_min;
00108 this->x_max = x_max;
00109 this->y_min = y_min;
00110 this->y_max = y_max;
00111
00112 this->x_scale = float(w)/(x_max-x_min);
00113 this->y_scale = float(h)/(y_max-y_min);
00114 }
00115
00116
00117 void tgPlot2D::draw()
00118 {
00119 float fx = float(x);
00120 float fy = float(y);
00121 float fw = float(w);
00122 float fh = float(h);;
00123
00124
00125 glLineWidth(1.0f);
00126 glColor3f(0.5f, 0.5f, 0.5f);
00127 glBegin(GL_LINES);
00128 float num_lines_h = 10.0f;
00129 float num_lines_v = 10.0f;
00130 for(unsigned i=1; i<=num_lines_h; i++)
00131 {
00132 glVertex3f(fx, fy + (fh/num_lines_h)*i, 0.0);
00133 glVertex3f(fx+fw, fy + (fh/num_lines_h)*i, 0.0);
00134 }
00135 for(unsigned i=1; i<=num_lines_v; i++)
00136 {
00137 glVertex3f(fx + (fw/num_lines_v)*i, fy, 0.0);
00138 glVertex3f(fx + (fw/num_lines_v)*i, fy+fh, 0.0);
00139 }
00140 glEnd();
00141
00142
00143 glColor3f(1,1,1);
00144 for(unsigned i=0; i<=num_lines_h; i++)
00145 {
00146 char b[8];
00147 sprintf(b,"%.2f", y_min + ((y_max-y_min)/num_lines_h) * i);
00148 font.Print(b, 14, int(fx-40), int(fy + (fh/num_lines_h)*i-5));
00149 }
00150 for(unsigned i=0; i<=num_lines_v; i++)
00151 {
00152 char b[8];
00153 sprintf(b,"%.1f", x_min + ((x_max-x_min)/num_lines_v) * i);
00154 font.Print(b, 14, int(fx + (fw/num_lines_v)*i-10), int(fy-20));
00155 }
00156
00157
00158 glLineWidth(2.0f);
00159 glColor3f(1.0f, 1.0f, 1.0f);
00160 glBegin(GL_LINES);
00161 glVertex3f(fx, fy, 0.0);
00162 glVertex3f(fx+fw, fy, 0.0);
00163 glVertex3f(fx, fy, 0.0);
00164 glVertex3f(fx, fy+fh, 0.0);
00165 glEnd();
00166 }
00167
00168 void tgPlot2D::plot(const std::vector<float> &x, const std::vector<float> &y) const
00169 {
00170
00171 if(x.empty() || y.empty())
00172 return;
00173
00174 unsigned m = std::min(x.size(), y.size());
00175
00176 glBegin(GL_LINE_STRIP);
00177 for(unsigned i=0; i<m; i++){
00178 glVertex3f( this->x+(x[i]-this->x_min)*this->x_scale,
00179 this->y+(y[i]-this->y_min)*this->y_scale,
00180 0.0f);
00181 }
00182 glEnd();
00183 }
00184
00185 void tgPlot2D::push(float y1)
00186 {
00187
00188
00189
00190 updateBuffer(y1);
00191
00192 draw();
00193 glColor3f(1.0f, 1.0f, 0.0f);
00194 plot(m_buffer_x, m_buffer_y_1);
00195 }
00196
00197 void tgPlot2D::push(float y1, float y2)
00198 {
00199
00200
00201
00202
00203
00204 updateBuffer(y1, y2);
00205
00206 draw();
00207 glColor3f(1.0f, 1.0f, 0.0f);
00208 plot(m_buffer_x, m_buffer_y_1);
00209 glColor3f(1.0f, 0.0f, 1.0f);
00210 plot(m_buffer_x, m_buffer_y_2);
00211 }
00212
00213 void tgPlot2D::push(float y1, float y2, float y3)
00214 {
00215
00216
00217
00218
00219
00220
00221
00222 updateBuffer(y1, y2, y3);
00223
00224 draw();
00225 glColor3f(1.0f, 1.0f, 0.0f);
00226 plot(m_buffer_x, m_buffer_y_1);
00227 glColor3f(1.0f, 0.0f, 1.0f);
00228 plot(m_buffer_x, m_buffer_y_2);
00229 glColor3f(0.0f, 1.0f, 1.0f);
00230 plot(m_buffer_x, m_buffer_y_3);
00231 }
00232
00233 void tgPlot2D::push(float y1, float y2, float y3, float y4)
00234 {
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 updateBuffer(y1, y2, y3, y4);
00245
00246 draw();
00247 glColor3f(1.0f, 1.0f, 0.0f);
00248 plot(m_buffer_x, m_buffer_y_1);
00249 glColor3f(1.0f, 0.0f, 1.0f);
00250 plot(m_buffer_x, m_buffer_y_2);
00251 glColor3f(0.0f, 1.0f, 1.0f);
00252 plot(m_buffer_x, m_buffer_y_3);
00253 glColor3f(0.0f, 1.0f, 0.0f);
00254 plot(m_buffer_x, m_buffer_y_4);
00255 }