contrib/brl/bbas/bgui/bgui_bargraph_clipon_tableau.cxx
Go to the documentation of this file.
00001 // This is brl/bbas/bgui/bgui_bargraph_clipon_tableau.cxx
00002 #include "bgui_bargraph_clipon_tableau.h"
00003 //:
00004 // \file
00005 // \author  Joe Mundy
00006 #include <vgui/vgui.h>
00007 #include <vgui/vgui_gl.h>
00008 #include <vgui/vgui_easy2D_tableau.h>
00009 
00010 //========================================================================
00011 //: Constructor
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   // Draw a square around the bargraph_clipon
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   // set up color values
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 //: Destructor.
00050 bgui_bargraph_clipon_tableau::~bgui_bargraph_clipon_tableau()
00051 {
00052   this->clear();
00053 }
00054 
00055 // Update the plot data.  Choice of fixed scale or variable scale
00056 // If fixed = 0 then the plot is automatically scaled. If fixed is
00057 // not false then scale is used. That is,
00058 // the data is multiplied by scale*graph_height_
00059 void bgui_bargraph_clipon_tableau::update(vcl_vector<float> const& bars,
00060                                           const bool fixed,
00061                                           const float scale)
00062 {
00063   //if not fixed then scale max to graph_height_
00064   unsigned int nbars = bars.size();
00065   bars_.resize(nbars);
00066   if (!nbars)
00067   {
00068     vcl_cout << "In bgui_bargraph_clipon_tableau::update(..) - no data\n";
00069     return;
00070   }
00071   if (!fixed)
00072   {
00073     float max = bars[0];
00074     for (unsigned int i=1; i<nbars; ++i)
00075       if (max < bars[i]) max = bars[i];
00076 
00077     for (unsigned int i=0; i<nbars; ++i)
00078       bars_[i] = graph_height_ - (bars[i]/max)*graph_height_;
00079   }
00080   else
00081     for (unsigned int i=0; i<nbars; ++i)
00082       bars_[i] = graph_height_- bars[i]*graph_height_*scale;
00083 
00084   //reconcile the number of plot bars
00085   float bar_width = graph_width_/nbars;
00086   if (bar_width>5*nominal_bar_width_)
00087     bar_width = 5*nominal_bar_width_;
00088   if (bar_width<1)
00089     bar_width = 1;
00090 
00091   //The first n_plot_bars data values will be plotted the
00092   //others are dropped
00093   this->clear();
00094   easy_->set_line_width(bar_width);
00095   for (unsigned int i = 0; i<nbars; i++)
00096   {
00097     float x0 = i*bar_width + left_offset_ + 1, x1 = x0;
00098     float y0 = top_offset_+graph_height_, y1 = top_offset_ + bars_[i];
00099     unsigned int color_index = 2;
00100     if (color_index_.size()>=nbars)
00101     {
00102       unsigned int temp = color_index_[i];
00103       if (temp<=7)
00104         color_index = temp;
00105     }
00106     vcl_vector<float> c = color_values_[color_index];
00107     easy_->set_foreground(c[0], c[1], c[2]);
00108     vgui_soview2D_lineseg* l = easy_->add_line(x0, y0, x1, y1);
00109     bar_plot_.push_back(l);
00110   }
00111   easy_->post_redraw();
00112 }
00113 
00114 void bgui_bargraph_clipon_tableau::set_color_vector(vcl_vector<unsigned char> const& colors)
00115 {
00116   color_index_.clear();
00117   color_index_ = colors;
00118 }
00119 
00120 void bgui_bargraph_clipon_tableau::clear()
00121 {
00122   for (vcl_vector<vgui_soview2D_lineseg*>::iterator sit = bar_plot_.begin();
00123        sit != bar_plot_.end();++sit)
00124   {
00125     easy_->remove(*sit);
00126     delete *sit;
00127   }
00128   bar_plot_.clear();
00129   easy_->post_redraw();
00130 }