00001
00002 #include <bgui/bgui_bargraph_clipon_tableau.h>
00003
00004
00005
00006 #include <vgui/vgui.h>
00007 #include <vgui/vgui_gl.h>
00008 #include <vgui/vgui_easy2D_tableau.h>
00009
00010
00011
00012 bgui_bargraph_clipon_tableau::
00013 bgui_bargraph_clipon_tableau(vgui_easy2D_tableau_sptr const& easy)
00014 : left_offset_(10), top_offset_(10),
00015 graph_width_(256), graph_height_(200), nominal_bar_width_(5)
00016 {
00017 easy_ = easy;
00018 easy_->set_foreground(1.0, 1.0, 0);
00019 easy_->set_line_width(3.0);
00020
00021 vcl_vector<float> x_corners, y_corners;
00022 x_corners.push_back(left_offset_);
00023 x_corners.push_back(left_offset_+graph_width_);
00024 x_corners.push_back(left_offset_+graph_width_);
00025 x_corners.push_back(left_offset_);
00026 y_corners.push_back(top_offset_);
00027 y_corners.push_back(top_offset_);
00028 y_corners.push_back(top_offset_+graph_height_);
00029 y_corners.push_back(top_offset_+graph_height_);
00030 easy_->add_polygon(4, &x_corners[0], &y_corners[0]);
00031
00032 vcl_vector<float> c0(3), c1(3), c2(3), c3(3), c4(3);
00033 vcl_vector<float> c5(3), c6(3), c7(3);
00034 c0[0]=0.0; c0[1]=0.0; c0[2]=1.0;
00035 c1[0]=0.0; c1[1]=1.0; c1[2]=0.0;
00036 c2[0]=0.0; c2[1]=1.0; c2[2]=1.0;
00037 c3[0]=1.0; c3[1]=0.0; c3[2]=0.0;
00038 c4[0]=1.0; c4[1]=0.0; c4[2]=1.0;
00039 c5[0]=1.0; c5[1]=1.0; c5[2]=0.0;
00040 c6[0]=1.0; c6[1]=1.0; c6[2]=1.0;
00041 c7[0]=0.75; c7[1]=0.0; c7[2]=0.25;
00042 color_values_.push_back(c0); color_values_.push_back(c1);
00043 color_values_.push_back(c2); color_values_.push_back(c3);
00044 color_values_.push_back(c4); color_values_.push_back(c5);
00045 color_values_.push_back(c6); color_values_.push_back(c7);
00046 }
00047
00048
00049
00050 bgui_bargraph_clipon_tableau::~bgui_bargraph_clipon_tableau()
00051 {
00052 this->clear();
00053 }
00054
00055
00056
00057
00058
00059 void bgui_bargraph_clipon_tableau::update(vcl_vector<float> const& bars,
00060 const bool fixed,
00061 const float scale)
00062 {
00063
00064 unsigned int nbars = bars.size();
00065 bars_.resize(nbars);
00066 if(!nbars)
00067 {
00068 vcl_cout << "In bgui_bargraph_clipon_tableau::update(..) -"
00069 << " no data\n";
00070 return;
00071 }
00072 if(!fixed)
00073 {
00074 float max = bars[0];
00075 for (unsigned int i=1; i<nbars; ++i)
00076 if (max < bars[i]) max = bars[i];
00077
00078 for (unsigned int i=0; i<nbars; ++i)
00079 bars_[i] = graph_height_ - (bars[i]/max)*graph_height_;
00080 }
00081 else
00082 for (unsigned int i=0; i<nbars; ++i)
00083 bars_[i] = graph_height_- bars[i]*graph_height_*scale;
00084
00085
00086 float bar_width = graph_width_/nbars;
00087 if(bar_width>5*nominal_bar_width_)
00088 bar_width = 5*nominal_bar_width_;
00089 if(bar_width<1)
00090 bar_width = 1;
00091
00092
00093
00094 this->clear();
00095 easy_->set_line_width(bar_width);
00096 for(unsigned int i = 0; i<nbars; i++)
00097 {
00098 float x0 = i*bar_width + left_offset_ + 1, x1 = x0;
00099 float y0 = top_offset_+graph_height_, y1 = top_offset_ + bars_[i];
00100 unsigned int color_index = 2;
00101 if(color_index_.size()>=nbars)
00102 {
00103 unsigned int temp = color_index_[i];
00104 if(temp<=7)
00105 color_index = temp;
00106 }
00107 vcl_vector<float> c = color_values_[color_index];
00108 easy_->set_foreground(c[0], c[1], c[2]);
00109 vgui_soview2D_lineseg* l = easy_->add_line(x0, y0, x1, y1);
00110 bar_plot_.push_back(l);
00111 }
00112 easy_->post_redraw();
00113 }
00114
00115 void bgui_bargraph_clipon_tableau::set_color_vector(vcl_vector<unsigned char> const& colors)
00116 {
00117 color_index_.clear();
00118 color_index_ = colors;
00119 }
00120
00121 void bgui_bargraph_clipon_tableau::clear()
00122 {
00123 for(vcl_vector<vgui_soview2D_lineseg*>::iterator sit = bar_plot_.begin();
00124 sit != bar_plot_.end();++sit)
00125 {
00126 easy_->remove(*sit);
00127 delete *sit;
00128 }
00129 bar_plot_.clear();
00130 easy_->post_redraw();
00131 }