Point Cloud Library (PCL)  1.7.0
simple_buffer_visualizer.h
1 #ifndef PCL_VISUALIZATION_SIMPLE_BUFF_H
2 #define PCL_VISUALIZATION_SIMPLE_BUFF_H
3 
4 #include <pcl/visualization/histogram_visualizer.h>
5 
6 //#include <pcl/visualization/impl/simple_buffer_visualizer.hpp>
7 
8 
9 namespace pcl
10 {
11  namespace visualization
12  {
13  /** \brief PCL simple buffer visualizer main class.
14  * \note The idea is to offer a simple visualizer that stores and display the last X values as a curve.
15  * \note The class is based on PCLHistogramVisualizer and pcl::VFHSignature308 for display.
16  * \note Therefore, the number of values is limited to [2-308].
17  * \author Raphael Favier
18  * \ingroup visualization
19  */
20  class PCL_EXPORTS PCLSimpleBufferVisualizer
21  {
22  public:
23  /** \brief PCL simple buffer visualizer visualizer default constructor. */
25  {
26  histo_ = new PCLHistogramVisualizer ();
27  nb_values_ = 308;
28 
29  // init values buffer
30  initValuesAndVisualization();
31  }
32 
33  /** \brief PCL simple buffer visualizer visualizer constructor.
34  * \param[in] nb_values the number of values stored in the buffer [2 - 308]
35  */
36  PCLSimpleBufferVisualizer (const int nb_values)
37  {
38  histo_ = new PCLHistogramVisualizer ();
39  nb_values_ = nb_values;
40 
41  if(nb_values_ > 308)
42  {
43  PCL_WARN("Maximum number of values can only be 308 (%d given). Setting back to 308. \n");
44  nb_values_ = 308;
45  }
46 
47  if(nb_values_ <= 1)
48  {
49  PCL_WARN("Number of values must be at least 2 (%d given). Setting it to default (308). \n");
50  nb_values_ = 308;
51  }
52 
53  // init values buffer
54  initValuesAndVisualization();
55  }
56 
57  /** \brief force display of values.
58  * \param[in] time - How long (in ms) should the visualization loop be allowed to run
59  */
60  void
61  displayValues (const int time = 1)
62  {
63  // load values into cloud
64  updateValuesToDisplay();
65 
66  // check if we need to automatically handle the backgroud color
67  if(control_background_color_)
68  {
69  if(values_.back() < lowest_threshold_)
70  {
71  histo_->setBackgroundColor(255.0, 140.0, 0.0);
72  }
73  else
74  {
75  histo_->setBackgroundColor(255.0, 255.0, 255.0);
76  }
77  }
78 
79  // add cloud to the visualizer
80  histo_->updateFeatureHistogram(cloud_, nb_values_);
81 
82  // check if we need to handle the Y scale ourselves
83  if (handle_y_scale_)
84  {
85  histo_->setGlobalYRange(min_, max_);
86  }
87 
88  // spin once
89  spinOnce(time);
90  }
91 
92  /** \brief add a new value at the end of the buffer.
93  * \param[in] val the float value to add.
94  */
95  void
96  addValue (const float val)
97  {
98  // remove front value
99  values_.pop_front();
100 
101  // push new value in the back
102  values_.push_back(val);
103 
104  // udapte min_ and max_ values
105  if (val > max_)
106  max_ = val;
107 
108  if (val < min_)
109  min_ = val;
110  }
111 
112  /** \brief spinOnce method.
113  * \param[in] time - How long (in ms) should the visualization loop be allowed to run
114  */
115  void
116  spinOnce (const int time = 1)
117  {
118  histo_->spinOnce(time);
119  }
120 
121  /** \brief spin method. */
122  void
123  spin ()
124  {
125  histo_->spin();
126  }
127 
128  /** \brief set background color handling mode.
129  * \note The point here is to change the background to orange when the latest value is under a threshold.
130  * \param[in] value if true, automatic mode is enabled. Else, background will be white
131  * \param[in] threshold value that triggers the background to turn orange if the latest value is lower
132  * \note This functionality does not work yet at time of commit (see http://dev.pointclouds.org/issues/829)
133  */
134  void
135  setAutomaticBackgroundColorControl (const bool value = true, const float threshold = 0.0f)
136  {
137  control_background_color_ = value;
138 
139  // if the user sets it back to false, we make sure to reset the bckgrd color to white
140  if(value == false)
141  histo_->setBackgroundColor(255.0, 255.0, 255.0);
142 
143  lowest_threshold_ = threshold;
144  }
145 
146  /** \brief set Y scale policy.
147  * \note If set to true, the minimal and maximal Y values are kept forever.
148  * \note If set to false, the Y scale is automatically adjusted to the current values (default).
149  * \param[in] value boolean that enable or disable this policy
150  */
151  void
152  setManuallyManageYScale (const bool value = false)
153  {
154  handle_y_scale_ = value;
155  }
156 
157  private:
158  /** \brief initialize ther buffer that stores the values to zero.
159  * \note The size is set by private member nb_values_ which is in the range [2-308].
160  */
161  void
162  initValuesAndVisualization ()
163  {
164  cloud_.points.resize(1);
165 
166  PCL_WARN("Setting buffer size to %d entries.\n", nb_values_);
167  values_.resize(nb_values_);
168 
169  // add the cloud to the histogram viewer
170  histo_->addFeatureHistogram(cloud_, nb_values_);
171 
172  // init GUI-related variables
173  initGUIValues();
174  }
175 
176  /** \brief pushes the values contained inside the buffer to the cloud used for visualization. */
177  void
178  updateValuesToDisplay ()
179  {
180  for(int i = 0 ; i < nb_values_ ; ++i)
181  {
182  cloud_.points[0].histogram[i] = values_[i];
183  }
184  }
185 
186  /** \brief initialize private variables linked to the GUI */
187  void
188  initGUIValues ()
189  {
190  control_background_color_ = false;
191  lowest_threshold_ = 0.0f;
192 
193  handle_y_scale_ = false;
194 
195  min_ = -1.0f; // numeric_limits<float>::max( );
196  max_ = 1.0f; // numeric_limits<float>::min( );
197  }
198 
199  /** \brief visualizer object */
200  PCLHistogramVisualizer *histo_;
201 
202  /** \brief cloud used for visualization */
204 
205  /** \brief buffer of values */
206  std::deque<float> values_;
207 
208  /** \brief number of values stored in the buffer
209  * \note ([2-308])
210  */
211  int nb_values_;
212 
213  /** \brief boolean used to know if we need to change the backgroud color in case of low values. */
214  bool control_background_color_;
215 
216  /** \brief threshold to turn the background orange if latest value is lower. */
217  float lowest_threshold_;
218 
219  /** \brief boolean used to know if we need to change the backgroud color in case of low values. True means we do it ourselves. */
220  bool handle_y_scale_;
221 
222  /** \brief float tracking the minimal and maximal values ever observed. */
223  float min_, max_;
224  };
225  }
226 }
227 
228 #endif // PCL_VISUALIZATION_SIMPLE_BUFF_H