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