mapviz_plugin.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #ifndef MAPVIZ_MAPVIZ_PLUGIN_H_
31 #define MAPVIZ_MAPVIZ_PLUGIN_H_
32 
33 // C++ standard libraries
34 #include <string>
35 
36 #include <boost/make_shared.hpp>
37 #include <boost/shared_ptr.hpp>
38 
39 // QT libraries
40 #include <QWidget>
41 #include <QGLWidget>
42 #include <QObject>
43 
44 // ROS libraries
45 #include <ros/ros.h>
46 #include <tf/transform_datatypes.h>
50 
51 #include <mapviz/widgets.h>
52 
53 #include "stopwatch.h"
54 
55 namespace mapviz
56 {
57  class MapvizPlugin : public QObject
58  {
59  Q_OBJECT;
60  public:
61  virtual ~MapvizPlugin() {}
62 
63  virtual bool Initialize(
66  QGLWidget* canvas)
67  {
68  tf_ = tf_listener;
69  tf_manager_ = tf_manager;
70  return Initialize(canvas);
71  }
72 
73  virtual void Shutdown() = 0;
74 
75  virtual void ClearHistory() {}
76 
81  virtual void Draw(double x, double y, double scale) = 0;
82 
87  virtual void Paint(QPainter* painter, double x, double y, double scale) {};
88 
89  void SetName(const std::string& name) { name_ = name; }
90 
91  std::string Name() const { return name_; }
92 
93  void SetType(const std::string& type) { type_ = type; }
94 
95  std::string Type() const { return type_; }
96 
97  int DrawOrder() const { return draw_order_; }
98 
99  void SetDrawOrder(int order)
100  {
101  if (draw_order_ != order)
102  {
103  draw_order_ = order;
105  }
106  }
107 
108  virtual void SetNode(const ros::NodeHandle& node)
109  {
110  node_ = node;
111  }
112 
113  void DrawPlugin(double x, double y, double scale)
114  {
115  if (visible_ && initialized_)
116  {
118  Transform();
120 
121  meas_draw_.start();
122  Draw(x, y, scale);
123  meas_draw_.stop();
124  }
125  }
126 
127  void PaintPlugin(QPainter* painter, double x, double y, double scale)
128  {
129  if (visible_ && initialized_)
130  {
132  Transform();
134 
135  meas_paint_.start();
136  Paint(painter, x, y, scale);
137  meas_paint_.start();
138  }
139  }
140 
141  void SetTargetFrame(std::string frame_id)
142  {
143  if (frame_id != target_frame_)
144  {
146 
148  Transform();
150 
152  }
153  }
154 
155  bool Visible() const { return visible_; }
156 
157  void SetVisible(bool visible)
158  {
159  if (visible_ != visible)
160  {
161  visible_ = visible;
162  Q_EMIT VisibleChanged(visible_);
163  }
164  }
165 
167  {
168  return GetTransform(source_frame_, stamp, transform);
169  }
170 
171  bool GetTransform(const std::string& source, const ros::Time& stamp, swri_transform_util::Transform& transform)
172  {
173  if (!initialized_)
174  return false;
175 
176  ros::Duration elapsed = ros::Time::now() - stamp;
177 
178  if (stamp != ros::Time() && elapsed > tf_->getCacheLength())
179  {
180  return false;
181  }
182 
183  if (tf_manager_->GetTransform(target_frame_, source, stamp, transform))
184  {
185  return true;
186  }
187  else if (elapsed.toSec() < 0.1)
188  {
189  // If the stamped transform failed because it is too recent, find the
190  // most recent transform in the cache instead.
191  if (tf_manager_->GetTransform(target_frame_, source, ros::Time(), transform))
192  {
193  return true;
194  }
195  }
196 
197  return false;
198  }
199 
200  virtual void Transform() = 0;
201 
202  virtual void LoadConfig(const YAML::Node& load, const std::string& path) = 0;
203  virtual void SaveConfig(YAML::Emitter& emitter, const std::string& path) = 0;
204 
205  virtual QWidget* GetConfigWidget(QWidget* parent) { return NULL; }
206 
207  virtual void PrintError(const std::string& message) = 0;
208  virtual void PrintInfo(const std::string& message) = 0;
209  virtual void PrintWarning(const std::string& message) = 0;
210 
211  void SetIcon(IconWidget* icon) { icon_ = icon; }
212 
214  {
215  std::string header = type_ + " (" + name_ + ")";
216  meas_transform_.printInfo(header + " Transform()");
217  meas_paint_.printInfo(header + " Paint()");
218  meas_draw_.printInfo(header + " Draw()");
219  }
220 
221  static void PrintErrorHelper(QLabel *status_label, const std::string& message, double throttle = 0.0);
222  static void PrintInfoHelper(QLabel *status_label, const std::string& message, double throttle = 0.0);
223  static void PrintWarningHelper(QLabel *status_label, const std::string& message, double throttle = 0.0);
224 
225  public Q_SLOTS:
226  virtual void DrawIcon() {}
227 
232  virtual bool SupportsPainting()
233  {
234  return false;
235  }
236 
237  Q_SIGNALS:
238  void DrawOrderChanged(int draw_order);
239  void SizeChanged();
240  void TargetFrameChanged(const std::string& target_frame);
241  void UseLatestTransformsChanged(bool use_latest_transforms);
242  void VisibleChanged(bool visible);
243 
244 
245  protected:
247  bool visible_;
248 
249  QGLWidget* canvas_;
251 
253 
256 
257  std::string target_frame_;
258  std::string source_frame_;
259  std::string type_;
260  std::string name_;
261 
263 
264  virtual bool Initialize(QGLWidget* canvas) = 0;
265 
267  initialized_(false),
268  visible_(true),
269  canvas_(NULL),
270  icon_(NULL),
271  tf_(),
272  target_frame_(""),
273  source_frame_(""),
274  draw_order_(0) {}
275 
276  private:
277  // Collect basic profiling info to know how much time each plugin
278  // spends in Transform(), Paint(), and Draw().
282  };
284 
285  // Implementation
286 
287  inline void MapvizPlugin::PrintErrorHelper(QLabel *status_label, const std::string &message,
288  double throttle)
289  {
290  if (message == status_label->text().toStdString())
291  {
292  return;
293  }
294 
295  if( throttle > 0.0){
296  ROS_ERROR_THROTTLE(throttle, "Error: %s", message.c_str());
297  }
298  else{
299  ROS_ERROR("Error: %s", message.c_str());
300  }
301  QPalette p(status_label->palette());
302  p.setColor(QPalette::Text, Qt::red);
303  status_label->setPalette(p);
304  status_label->setText(message.c_str());
305  }
306 
307  inline void MapvizPlugin::PrintInfoHelper(QLabel *status_label, const std::string &message,
308  double throttle)
309  {
310  if (message == status_label->text().toStdString())
311  {
312  return;
313  }
314 
315  if( throttle > 0.0){
316  ROS_INFO_THROTTLE(throttle, "%s", message.c_str());
317  }
318  else{
319  ROS_INFO("%s", message.c_str());
320  }
321  QPalette p(status_label->palette());
322  p.setColor(QPalette::Text, Qt::darkGreen);
323  status_label->setPalette(p);
324  status_label->setText(message.c_str());
325  }
326 
327  inline void MapvizPlugin::PrintWarningHelper(QLabel *status_label, const std::string &message,
328  double throttle)
329  {
330  if (message == status_label->text().toStdString())
331  {
332  return;
333  }
334 
335  if( throttle > 0.0){
336  ROS_WARN_THROTTLE(throttle, "%s", message.c_str());
337  }
338  else{
339  ROS_WARN("%s", message.c_str());
340  }
341  QPalette p(status_label->palette());
342  p.setColor(QPalette::Text, Qt::darkYellow);
343  status_label->setPalette(p);
344  status_label->setText(message.c_str());
345  }
346 
347 }
348 #endif // MAPVIZ_MAPVIZ_PLUGIN_H_
349 
mapviz::Stopwatch::start
void start()
Definition: stopwatch.h:51
mapviz::MapvizPlugin::SetVisible
void SetVisible(bool visible)
Definition: mapviz_plugin.h:157
ROS_ERROR_THROTTLE
#define ROS_ERROR_THROTTLE(period,...)
mapviz::MapvizPlugin::Transform
virtual void Transform()=0
mapviz::MapvizPlugin::tf_
boost::shared_ptr< tf::TransformListener > tf_
Definition: mapviz_plugin.h:254
mapviz::MapvizPluginPtr
boost::shared_ptr< MapvizPlugin > MapvizPluginPtr
Definition: mapviz_plugin.h:283
mapviz::Stopwatch
Definition: stopwatch.h:41
mapviz::MapvizPlugin::visible_
bool visible_
Definition: mapviz_plugin.h:247
mapviz
Definition: color_button.h:36
mapviz::MapvizPlugin::SupportsPainting
virtual bool SupportsPainting()
Definition: mapviz_plugin.h:232
mapviz::MapvizPlugin::SetNode
virtual void SetNode(const ros::NodeHandle &node)
Definition: mapviz_plugin.h:108
NULL
#define NULL
boost::shared_ptr< tf::TransformListener >
mapviz::MapvizPlugin::LoadConfig
virtual void LoadConfig(const YAML::Node &load, const std::string &path)=0
mapviz::MapvizPlugin::SizeChanged
void SizeChanged()
mapviz::MapvizPlugin::PrintWarning
virtual void PrintWarning(const std::string &message)=0
ROS_WARN_THROTTLE
#define ROS_WARN_THROTTLE(period,...)
ros.h
mapviz::MapvizPlugin::PrintErrorHelper
static void PrintErrorHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
Definition: mapviz_plugin.h:287
mapviz::MapvizPlugin::SetType
void SetType(const std::string &type)
Definition: mapviz_plugin.h:93
transform.h
frame_id
std::string frame_id
mapviz::MapvizPlugin::Type
std::string Type() const
Definition: mapviz_plugin.h:95
mapviz::IconWidget
Definition: widgets.h:139
mapviz::Stopwatch::stop
void stop()
Definition: stopwatch.h:59
mapviz::MapvizPlugin::PrintError
virtual void PrintError(const std::string &message)=0
mapviz::MapvizPlugin::Name
std::string Name() const
Definition: mapviz_plugin.h:91
mapviz::MapvizPlugin::SetTargetFrame
void SetTargetFrame(std::string frame_id)
Definition: mapviz_plugin.h:141
mapviz::MapvizPlugin::~MapvizPlugin
virtual ~MapvizPlugin()
Definition: mapviz_plugin.h:61
mapviz::MapvizPlugin::GetTransform
bool GetTransform(const std::string &source, const ros::Time &stamp, swri_transform_util::Transform &transform)
Definition: mapviz_plugin.h:171
yaml_util.h
mapviz::MapvizPlugin::DrawOrder
int DrawOrder() const
Definition: mapviz_plugin.h:97
mapviz::MapvizPlugin::DrawPlugin
void DrawPlugin(double x, double y, double scale)
Definition: mapviz_plugin.h:113
mapviz::MapvizPlugin::type_
std::string type_
Definition: mapviz_plugin.h:259
mapviz::MapvizPlugin::meas_transform_
Stopwatch meas_transform_
Definition: mapviz_plugin.h:279
mapviz::MapvizPlugin::meas_paint_
Stopwatch meas_paint_
Definition: mapviz_plugin.h:280
mapviz::MapvizPlugin::Draw
virtual void Draw(double x, double y, double scale)=0
widgets.h
mapviz::MapvizPlugin::Shutdown
virtual void Shutdown()=0
transform_manager.h
mapviz::MapvizPlugin::MapvizPlugin
MapvizPlugin()
Definition: mapviz_plugin.h:266
swri_transform_util::Transform
mapviz::MapvizPlugin::PrintWarningHelper
static void PrintWarningHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
Definition: mapviz_plugin.h:327
mapviz::MapvizPlugin::PrintMeasurements
void PrintMeasurements()
Definition: mapviz_plugin.h:213
ROS_WARN
#define ROS_WARN(...)
mapviz::MapvizPlugin::draw_order_
int draw_order_
Definition: mapviz_plugin.h:262
mapviz::MapvizPlugin::SetDrawOrder
void SetDrawOrder(int order)
Definition: mapviz_plugin.h:99
mapviz::MapvizPlugin::name_
std::string name_
Definition: mapviz_plugin.h:260
mapviz::MapvizPlugin::meas_draw_
Stopwatch meas_draw_
Definition: mapviz_plugin.h:281
mapviz::MapvizPlugin::PrintInfoHelper
static void PrintInfoHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
Definition: mapviz_plugin.h:307
mapviz::MapvizPlugin::Initialize
virtual bool Initialize(boost::shared_ptr< tf::TransformListener > tf_listener, swri_transform_util::TransformManagerPtr tf_manager, QGLWidget *canvas)
Definition: mapviz_plugin.h:63
mapviz::MapvizPlugin::GetConfigWidget
virtual QWidget * GetConfigWidget(QWidget *parent)
Definition: mapviz_plugin.h:205
mapviz::MapvizPlugin::ClearHistory
virtual void ClearHistory()
Definition: mapviz_plugin.h:75
mapviz::MapvizPlugin::canvas_
QGLWidget * canvas_
Definition: mapviz_plugin.h:249
mapviz::MapvizPlugin::UseLatestTransformsChanged
void UseLatestTransformsChanged(bool use_latest_transforms)
mapviz::MapvizPlugin::VisibleChanged
void VisibleChanged(bool visible)
mapviz::MapvizPlugin::SetIcon
void SetIcon(IconWidget *icon)
Definition: mapviz_plugin.h:211
transform_datatypes.h
ros::Time
mapviz::MapvizPlugin::SaveConfig
virtual void SaveConfig(YAML::Emitter &emitter, const std::string &path)=0
mapviz::MapvizPlugin::PrintInfo
virtual void PrintInfo(const std::string &message)=0
mapviz::MapvizPlugin::DrawOrderChanged
void DrawOrderChanged(int draw_order)
mapviz::MapvizPlugin::target_frame_
std::string target_frame_
Definition: mapviz_plugin.h:257
mapviz::MapvizPlugin::TargetFrameChanged
void TargetFrameChanged(const std::string &target_frame)
ROS_ERROR
#define ROS_ERROR(...)
mapviz::MapvizPlugin::tf_manager_
swri_transform_util::TransformManagerPtr tf_manager_
Definition: mapviz_plugin.h:255
mapviz::MapvizPlugin
Definition: mapviz_plugin.h:57
mapviz::MapvizPlugin::Visible
bool Visible() const
Definition: mapviz_plugin.h:155
mapviz::MapvizPlugin::Paint
virtual void Paint(QPainter *painter, double x, double y, double scale)
Definition: mapviz_plugin.h:87
mapviz::MapvizPlugin::initialized_
bool initialized_
Definition: mapviz_plugin.h:246
DurationBase< Duration >::toSec
double toSec() const
mapviz::MapvizPlugin::SetName
void SetName(const std::string &name)
Definition: mapviz_plugin.h:89
header
const std::string header
mapviz::MapvizPlugin::source_frame_
std::string source_frame_
Definition: mapviz_plugin.h:258
ROS_INFO
#define ROS_INFO(...)
mapviz::MapvizPlugin::icon_
IconWidget * icon_
Definition: mapviz_plugin.h:250
mapviz::MapvizPlugin::node_
ros::NodeHandle node_
Definition: mapviz_plugin.h:252
ros::Duration
mapviz::MapvizPlugin::PaintPlugin
void PaintPlugin(QPainter *painter, double x, double y, double scale)
Definition: mapviz_plugin.h:127
name
string name
mapviz::MapvizPlugin::GetTransform
bool GetTransform(const ros::Time &stamp, swri_transform_util::Transform &transform)
Definition: mapviz_plugin.h:166
mapviz::Stopwatch::printInfo
void printInfo(const std::string &name) const
Definition: stopwatch.h:87
ros::NodeHandle
stopwatch.h
ros::Time::now
static Time now()
ROS_INFO_THROTTLE
#define ROS_INFO_THROTTLE(period,...)
mapviz::MapvizPlugin::DrawIcon
virtual void DrawIcon()
Definition: mapviz_plugin.h:226


mapviz
Author(s): Marc Alban
autogenerated on Wed Jan 17 2024 03:27:46