pie_chart_display.cpp
Go to the documentation of this file.
1 // -*- mode: c++; -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 #include "pie_chart_display.h"
37 
38 #include <OGRE/OgreMaterialManager.h>
39 #include <OGRE/OgreTextureManager.h>
40 #include <OGRE/OgreTexture.h>
41 #include <OGRE/OgreTechnique.h>
42 #include <OGRE/OgreHardwarePixelBuffer.h>
44 #include <rviz/display_context.h>
45 #include <QPainter>
46 
47 namespace jsk_rviz_plugins
48 {
49 
51  : rviz::Display(), update_required_(false), first_time_(true), data_(0.0)
52  {
54  "Topic", "",
55  ros::message_traits::datatype<std_msgs::Float32>(),
56  "std_msgs::Float32 topic to subscribe to.",
57  this, SLOT( updateTopic() ));
58  size_property_ = new rviz::IntProperty("size", 128,
59  "size of the plotter window",
60  this, SLOT(updateSize()));
61  left_property_ = new rviz::IntProperty("left", 128,
62  "left of the plotter window",
63  this, SLOT(updateLeft()));
64  top_property_ = new rviz::IntProperty("top", 128,
65  "top of the plotter window",
66  this, SLOT(updateTop()));
67  fg_color_property_ = new rviz::ColorProperty("foreground color",
68  QColor(25, 255, 240),
69  "color to draw line",
70  this, SLOT(updateFGColor()));
72  = new rviz::FloatProperty("foreground alpha", 0.7,
73  "alpha belnding value for foreground",
74  this, SLOT(updateFGAlpha()));
76  = new rviz::FloatProperty("foreground alpha 2", 0.4,
77  "alpha belnding value for foreground for indicator",
78  this, SLOT(updateFGAlpha2()));
79  bg_color_property_ = new rviz::ColorProperty("background color",
80  QColor(0, 0, 0),
81  "background color",
82  this, SLOT(updateBGColor()));
84  = new rviz::FloatProperty("backround alpha", 0.0,
85  "alpha belnding value for background",
86  this, SLOT(updateBGAlpha()));
88  = new rviz::IntProperty("text size", 14,
89  "text size",
90  this, SLOT(updateTextSize()));
92  = new rviz::BoolProperty("show caption", true,
93  "show caption",
94  this, SLOT(updateShowCaption()));
96  = new rviz::FloatProperty("max value", 1.0,
97  "max value of pie chart",
98  this, SLOT(updateMaxValue()));
100  = new rviz::FloatProperty("min value", 0.0,
101  "min value of pie chart",
102  this, SLOT(updateMinValue()));
104  = new rviz::BoolProperty("auto color change",
105  false,
106  "change the color automatically",
107  this, SLOT(updateAutoColorChange()));
109  = new rviz::ColorProperty("max color",
110  QColor(255, 0, 0),
111  "only used if auto color change is set to True.",
112  this, SLOT(updateMaxColor()));
113 
115  = new rviz::BoolProperty("clockwise rotate direction",
116  false,
117  "change the rotate direction",
118  this, SLOT(updateClockwiseRotate()));
119  }
120 
122  {
123  if (overlay_->isVisible()) {
124  overlay_->hide();
125  }
126  delete update_topic_property_;
127  delete fg_color_property_;
128  delete bg_color_property_;
129  delete fg_alpha_property_;
130  delete fg_alpha2_property_;
131  delete bg_alpha_property_;
132  delete top_property_;
133  delete left_property_;
134  delete size_property_;
135  delete min_value_property_;
136  delete max_value_property_;
137  delete text_size_property_;
138  delete show_caption_property_;
139  }
140 
142  {
143  static int count = 0;
145  ss << "PieChartDisplayObject" << count++;
146  overlay_.reset(new OverlayObject(ss.str()));
147  onEnable();
148  updateSize();
149  updateLeft();
150  updateTop();
151  updateFGColor();
152  updateBGColor();
153  updateFGAlpha();
154  updateFGAlpha2();
155  updateBGAlpha();
156  updateMinValue();
157  updateMaxValue();
158  updateTextSize();
161  updateMaxColor();
163  overlay_->updateTextureSize(texture_size_, texture_size_ + caption_offset_);
164  overlay_->hide();
165  }
166 
167  void PieChartDisplay::update(float wall_dt, float ros_dt)
168  {
169  if (update_required_) {
170  update_required_ = false;
171  overlay_->updateTextureSize(texture_size_, texture_size_ + caption_offset_);
172  drawPlot(data_);
173  overlay_->setPosition(left_, top_);
174  overlay_->setDimensions(overlay_->getTextureWidth(),
175  overlay_->getTextureHeight());
176  }
177  }
178 
179  void PieChartDisplay::processMessage(const std_msgs::Float32::ConstPtr& msg)
180  {
181  boost::mutex::scoped_lock lock(mutex_);
182 
183  if (!overlay_->isVisible()) {
184  return;
185  }
186  if (data_ != msg->data || first_time_) {
187  first_time_ = false;
188  data_ = msg->data;
189  update_required_ = true;
190  }
191  }
192 
193  void PieChartDisplay::drawPlot(double val)
194  {
195  QColor fg_color(fg_color_);
196 
197  if (auto_color_change_) {
198  double r
199  = std::min(1.0, fabs((val - min_value_) / (max_value_ - min_value_)));
200  if (r > 0.6) {
201  double r2 = (r - 0.6) / 0.4;
202  fg_color.setRed((max_color_.red() - fg_color_.red()) * r2
203  + fg_color_.red());
204  fg_color.setGreen((max_color_.green() - fg_color_.green()) * r2
205  + fg_color_.green());
206  fg_color.setBlue((max_color_.blue() - fg_color_.blue()) * r2
207  + fg_color_.blue());
208  }
209  }
210 
211 
212  QColor fg_color2(fg_color);
213  QColor bg_color(bg_color_);
214  fg_color.setAlpha(fg_alpha_);
215  fg_color2.setAlpha(fg_alpha2_);
216  bg_color.setAlpha(bg_alpha_);
217  int width = overlay_->getTextureWidth();
218  int height = overlay_->getTextureHeight();
219  {
220  ScopedPixelBuffer buffer = overlay_->getBuffer();
221  QImage Hud = buffer.getQImage(*overlay_, bg_color);
222  QPainter painter( &Hud );
223  painter.setRenderHint(QPainter::Antialiasing, true);
224 
225  const int outer_line_width = 5;
226  const int value_line_width = 10;
227  const int value_indicator_line_width = 2;
228  const int value_padding = 5;
229 
230  const int value_aabb_offset
231  = outer_line_width + value_padding + value_line_width / 2;
232 
233  painter.setPen(QPen(fg_color, outer_line_width, Qt::SolidLine));
234 
235  painter.drawEllipse(outer_line_width / 2, outer_line_width / 2,
236  width - outer_line_width ,
237  height - outer_line_width - caption_offset_);
238 
239  painter.setPen(QPen(fg_color2, value_indicator_line_width, Qt::SolidLine));
240  painter.drawEllipse(value_aabb_offset, value_aabb_offset,
241  width - value_aabb_offset * 2,
242  height - value_aabb_offset * 2 - caption_offset_);
243 
244  const double ratio = (val - min_value_) / (max_value_ - min_value_);
245  const double rotate_direction = clockwise_rotate_ ? -1.0 : 1.0;
246  const double ratio_angle = ratio * 360.0 * rotate_direction;
247  const double start_angle_offset = -90;
248  painter.setPen(QPen(fg_color, value_line_width, Qt::SolidLine));
249  painter.drawArc(QRectF(value_aabb_offset, value_aabb_offset,
250  width - value_aabb_offset * 2,
251  height - value_aabb_offset * 2 - caption_offset_),
252  start_angle_offset * 16 ,
253  ratio_angle * 16);
254  QFont font = painter.font();
255  font.setPointSize(text_size_);
256  font.setBold(true);
257  painter.setFont(font);
258  painter.setPen(QPen(fg_color, value_line_width, Qt::SolidLine));
259  std::ostringstream s;
260  s << std::fixed << std::setprecision(2) << val;
261  painter.drawText(0, 0, width, height - caption_offset_,
262  Qt::AlignCenter | Qt::AlignVCenter,
263  s.str().c_str());
264 
265  // caption
266  if (show_caption_) {
267  painter.drawText(0, height - caption_offset_, width, caption_offset_,
268  Qt::AlignCenter | Qt::AlignVCenter,
269  getName());
270  }
271 
272  // done
273  painter.end();
274  // Unlock the pixel buffer
275  }
276  }
277 
278 
280  {
281  std::string topic_name = update_topic_property_->getTopicStd();
282  if (topic_name.length() > 0 && topic_name != "/") {
283  ros::NodeHandle n;
284  sub_ = n.subscribe(topic_name, 1, &PieChartDisplay::processMessage, this);
285  }
286  }
287 
288 
290  {
291  sub_.shutdown();
292  }
293 
295  {
296  subscribe();
297  overlay_->show();
298  first_time_ = true;
299  }
300 
302  {
303  unsubscribe();
304  overlay_->hide();
305  }
306 
308  {
309  boost::mutex::scoped_lock lock(mutex_);
311  }
312 
314  {
316  }
317 
319  {
321  }
322 
324  {
326  }
327 
329  {
331  }
332 
334  {
336  }
337 
339  {
341  }
342 
343 
345  {
347  }
348 
350  {
352  }
353 
355  {
357  }
358 
360  {
361  boost::mutex::scoped_lock lock(mutex_);
363  QFont font;
364  font.setPointSize(text_size_);
365  caption_offset_ = QFontMetrics(font).height();
366 
367  }
368 
370  {
372  }
373 
374 
376  {
377  unsubscribe();
378  subscribe();
379  }
380 
382  {
384  if (auto_color_change_) {
386  }
387  else {
389  }
390  }
391 
393  {
395  }
396 
398  {
400  }
401 
403  {
404  return (top_ < y && top_ + texture_size_ > y &&
405  left_ < x && left_ + texture_size_ > x);
406  }
407 
409  {
410  top_ = y;
411  left_ = x;
412  }
413 
415  {
418  }
419 }
420 
virtual QColor getColor() const
rviz::BoolProperty * auto_color_change_property_
virtual bool setValue(const QVariant &new_value)
virtual QImage getQImage(unsigned int width, unsigned int height)
rviz::ColorProperty * fg_color_property_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
PLUGINLIB_EXPORT_CLASS(jsk_rviz_plugins::PictogramArrayDisplay, rviz::Display)
rviz::FloatProperty * bg_alpha_property_
virtual int getInt() const
rviz::ColorProperty * bg_color_property_
virtual float getFloat() const
virtual bool isInRegion(int x, int y)
rviz::ColorProperty * max_color_property_
width
rviz::FloatProperty * fg_alpha_property_
virtual bool getBool() const
rviz::FloatProperty * min_value_property_
virtual void processMessage(const std_msgs::Float32::ConstPtr &msg)
virtual void setPosition(int x, int y)
virtual void movePosition(int x, int y)
rviz::FloatProperty * max_value_property_
rviz::BoolProperty * clockwise_rotate_property_
rviz::RosTopicProperty * update_topic_property_
rviz::BoolProperty * show_caption_property_
rviz::FloatProperty * fg_alpha2_property_
virtual void update(float wall_dt, float ros_dt)
height
std::string getTopicStd() const
rviz::IntProperty * text_size_property_
virtual QString getName() const


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Sat Mar 20 2021 03:03:18