grid_plugin.cpp
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 
31 
32 // C++ standard libraries
33 #include <cstdio>
34 #include <vector>
35 
36 // QT libraries
37 #include <QGLWidget>
38 #include <QPalette>
39 
41 
42 // Declare plugin
45 
46 namespace mapviz_plugins
47 {
48  GridPlugin::GridPlugin() :
49  config_widget_(new QWidget()),
50  alpha_(1.0),
51  top_left_(0, 0, 0),
52  size_(1),
53  rows_(1),
54  columns_(1),
55  transformed_(false)
56  {
57  ui_.setupUi(config_widget_);
58 
59  ui_.color->setColor(Qt::red);
60 
61  // Set background white
62  QPalette p(config_widget_->palette());
63  p.setColor(QPalette::Background, Qt::white);
64  config_widget_->setPalette(p);
65 
66  // Set status text red
67  QPalette p3(ui_.status->palette());
68  p3.setColor(QPalette::Text, Qt::red);
69  ui_.status->setPalette(p3);
70 
71  QObject::connect(ui_.select_frame, SIGNAL(clicked()), this, SLOT(SelectFrame()));
72  QObject::connect(ui_.frame, SIGNAL(textEdited(const QString&)), this, SLOT(FrameEdited()));
73  QObject::connect(ui_.alpha, SIGNAL(valueChanged(double)), this, SLOT(SetAlpha(double)));
74  QObject::connect(ui_.x, SIGNAL(valueChanged(double)), this, SLOT(SetX(double)));
75  QObject::connect(ui_.y, SIGNAL(valueChanged(double)), this, SLOT(SetY(double)));
76  QObject::connect(ui_.size, SIGNAL(valueChanged(double)), this, SLOT(SetSize(double)));
77  QObject::connect(ui_.rows, SIGNAL(valueChanged(int)), this, SLOT(SetRows(int)));
78  QObject::connect(ui_.columns, SIGNAL(valueChanged(int)), this, SLOT(SetColumns(int)));
79  connect(ui_.color, SIGNAL(colorEdited(const QColor &)), this, SLOT(DrawIcon()));
80  }
81 
83  {
84  Shutdown();
85  }
86 
88  {
89  }
90 
92  {
93  if (icon_)
94  {
95  QPixmap icon(16, 16);
96  icon.fill(Qt::transparent);
97 
98  QPainter painter(&icon);
99  painter.setRenderHint(QPainter::Antialiasing, true);
100 
101  QPen pen(QColor(ui_.color->color()));
102 
103  pen.setWidth(2);
104  pen.setCapStyle(Qt::SquareCap);
105  painter.setPen(pen);
106 
107  painter.drawLine(2, 2, 14, 2);
108  painter.drawLine(2, 2, 2, 14);
109  painter.drawLine(14, 2, 14, 14);
110  painter.drawLine(2, 14, 14, 14);
111  painter.drawLine(8, 2, 8, 14);
112  painter.drawLine(2, 8, 14, 8);
113 
114  icon_->SetPixmap(icon);
115  }
116  }
117 
118  void GridPlugin::SetAlpha(double alpha)
119  {
120  alpha_ = alpha;
121  }
122 
123  void GridPlugin::SetX(double x)
124  {
125  top_left_.setX(x);
126 
127  RecalculateGrid();
128  }
129 
130  void GridPlugin::SetY(double y)
131  {
132  top_left_.setY(y);
133 
134  RecalculateGrid();
135  }
136 
137  void GridPlugin::SetSize(double size)
138  {
139  size_ = size;
140 
141  RecalculateGrid();
142  }
143 
144  void GridPlugin::SetRows(int rows)
145  {
146  rows_ = rows;
147 
148  RecalculateGrid();
149  }
150 
151  void GridPlugin::SetColumns(int columns)
152  {
153  columns_ = columns;
154 
155  RecalculateGrid();
156  }
157 
159  {
160  std::string frame = mapviz::SelectFrameDialog::selectFrame(tf_);
161  if (!frame.empty())
162  {
163  ui_.frame->setText(QString::fromStdString(frame));
164  FrameEdited();
165  }
166  }
167 
169  {
170  source_frame_ = ui_.frame->text().toStdString();
171 
172  initialized_ = true;
173 
174  RecalculateGrid();
175  }
176 
177  void GridPlugin::PrintError(const std::string& message)
178  {
179  PrintErrorHelper(ui_.status, message);
180  }
181 
182  void GridPlugin::PrintInfo(const std::string& message)
183  {
184  PrintInfoHelper(ui_.status, message);
185  }
186 
187  void GridPlugin::PrintWarning(const std::string& message)
188  {
189  PrintWarningHelper(ui_.status, message);
190  }
191 
192  QWidget* GridPlugin::GetConfigWidget(QWidget* parent)
193  {
194  config_widget_->setParent(parent);
195 
196  return config_widget_;
197  }
198 
199  bool GridPlugin::Initialize(QGLWidget* canvas)
200  {
201  canvas_ = canvas;
202 
203  DrawIcon();
204 
205  return true;
206  }
207 
208  void GridPlugin::Draw(double x, double y, double scale)
209  {
210  if (transformed_)
211  {
212  QColor color = ui_.color->color();
213 
214  glLineWidth(3);
215  glColor4d(color.redF(), color.greenF(), color.blueF(), alpha_);
216  glBegin(GL_LINES);
217 
218  std::list<tf::Point>::iterator transformed_left_it = transformed_left_points_.begin();
219  std::list<tf::Point>::iterator transformed_right_it = transformed_right_points_.begin();
220  for (; transformed_left_it != transformed_left_points_.end(); ++transformed_left_it)
221  {
222  glVertex2d(transformed_left_it->getX(), transformed_left_it->getY());
223  glVertex2d(transformed_right_it->getX(), transformed_right_it->getY());
224 
225  ++transformed_right_it;
226  }
227 
228  std::list<tf::Point>::iterator transformed_top_it = transformed_top_points_.begin();
229  std::list<tf::Point>::iterator transformed_bottom_it = transformed_bottom_points_.begin();
230  for (; transformed_top_it != transformed_top_points_.end(); ++transformed_top_it)
231  {
232  glVertex2d(transformed_top_it->getX(), transformed_top_it->getY());
233  glVertex2d(transformed_bottom_it->getX(), transformed_bottom_it->getY());
234 
235  ++transformed_bottom_it;
236  }
237 
238  glEnd();
239 
240  PrintInfo("OK");
241  }
242  }
243 
245  {
246  transformed_ = false;
247 
248  left_points_.clear();
249  right_points_.clear();
250  top_points_.clear();
251  bottom_points_.clear();
252 
253  transformed_left_points_.clear();
255  transformed_top_points_.clear();
257 
258  // Set top and bottom
259  for (int c = 0; c <= columns_; c++)
260  {
261  tf::Point top_point(top_left_.getX() + c * size_, top_left_.getY(), 0);
262  top_points_.push_back(top_point);
263  transformed_top_points_.push_back(transform_ * top_point);
264 
265  tf::Point bottom_point(top_left_.getX() + c * size_, top_left_.getY() + size_ * rows_, 0);
266  bottom_points_.push_back(bottom_point);
267  transformed_bottom_points_.push_back(transform_ * bottom_point);
268  }
269 
270  // Set left and right
271  for (int r = 0; r <= rows_; r++)
272  {
273  tf::Point left_point(top_left_.getX(), top_left_.getY() + r * size_, 0);
274  left_points_.push_back(left_point);
275  transformed_left_points_.push_back(transform_ * left_point);
276 
277  tf::Point right_point(top_left_.getX() + size_ * columns_, top_left_.getY() + r * size_, 0);
278  right_points_.push_back(right_point);
279  transformed_right_points_.push_back(transform_ * right_point);
280  }
281  }
282 
284  {
285  transformed_ = false;
286 
288  {
293 
294  transformed_ = true;
295  }
296  }
297 
298  void GridPlugin::Transform(std::list<tf::Point>& src, std::list<tf::Point>& dst)
299  {
300  std::list<tf::Point>::iterator points_it = src.begin();
301  std::list<tf::Point>::iterator transformed_it = dst.begin();
302  for (; points_it != src.end() && transformed_it != dst.end(); ++points_it)
303  {
304  (*transformed_it) = transform_ * (*points_it);
305 
306  ++transformed_it;
307  }
308  }
309 
310  void GridPlugin::LoadConfig(const YAML::Node& node, const std::string& path)
311  {
312  if (node["color"])
313  {
314  std::string color;
315  node["color"] >> color;
316  ui_.color->setColor(QColor(color.c_str()));
317  }
318 
319  if (node["frame"])
320  {
321  std::string frame;
322  node["frame"] >> frame;
323  ui_.frame->setText(QString::fromStdString(frame));
324  }
325 
326  if (node["x"])
327  {
328  float x = 0;
329  node["x"] >> x;
330  ui_.x->setValue(x);
331  }
332 
333  if (node["y"])
334  {
335  float y = 0;
336  node["y"] >> y;
337  ui_.y->setValue(y);
338  }
339 
340  if (node["alpha"])
341  {
342  node["alpha"] >> alpha_;
343  ui_.alpha->setValue(alpha_);
344  }
345 
346  if (node["size"])
347  {
348  node["size"] >> size_;
349  ui_.size->setValue(size_);
350  }
351 
352  if (node["rows"])
353  {
354  node["rows"] >> rows_;
355  ui_.rows->setValue(rows_);
356  }
357 
358  if (node["columns"])
359  {
360  node["columns"] >> columns_;
361  ui_.columns->setValue(columns_);
362  }
363 
364  FrameEdited();
365  }
366 
367  void GridPlugin::SaveConfig(YAML::Emitter& emitter, const std::string& path)
368  {
369  emitter << YAML::Key << "color" << YAML::Value << ui_.color->color().name().toStdString();
370 
371  emitter << YAML::Key << "alpha" << YAML::Value << alpha_;
372 
373  std::string frame = ui_.frame->text().toStdString();
374  emitter << YAML::Key << "frame" << YAML::Value << frame;
375 
376  emitter << YAML::Key << "x" << YAML::Value << top_left_.getX();
377  emitter << YAML::Key << "y" << YAML::Value << top_left_.getY();
378  emitter << YAML::Key << "size" << YAML::Value << size_;
379  emitter << YAML::Key << "rows" << YAML::Value << rows_;
380  emitter << YAML::Key << "columns" << YAML::Value << columns_;
381  }
382 }
383 
void LoadConfig(const YAML::Node &node, const std::string &path)
bool Initialize(QGLWidget *canvas)
bool GetTransform(const ros::Time &stamp, swri_transform_util::Transform &transform, bool use_latest_transforms=true)
swri_transform_util::Transform transform_
Definition: grid_plugin.h:116
std::list< tf::Point > transformed_left_points_
Definition: grid_plugin.h:113
IconWidget * icon_
void Draw(double x, double y, double scale)
TFSIMD_FORCE_INLINE void setX(tfScalar x)
TFSIMD_FORCE_INLINE void setY(tfScalar y)
static void PrintWarningHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
static void PrintErrorHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
TFSIMD_FORCE_INLINE const tfScalar & getY() const
static std::string selectFrame(boost::shared_ptr< tf::TransformListener > tf_listener, QWidget *parent=0)
TFSIMD_FORCE_INLINE const tfScalar & y() const
static void PrintInfoHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
void PrintError(const std::string &message)
void PrintWarning(const std::string &message)
std::string source_frame_
void SetPixmap(QPixmap pixmap)
std::list< tf::Point > right_points_
Definition: grid_plugin.h:109
std::list< tf::Point > transformed_top_points_
Definition: grid_plugin.h:111
TFSIMD_FORCE_INLINE const tfScalar & x() const
std::list< tf::Point > top_points_
Definition: grid_plugin.h:106
void PrintInfo(const std::string &message)
std::list< tf::Point > transformed_bottom_points_
Definition: grid_plugin.h:112
void SetAlpha(double alpha)
std::list< tf::Point > bottom_points_
Definition: grid_plugin.h:107
QWidget * GetConfigWidget(QWidget *parent)
TFSIMD_FORCE_INLINE const tfScalar & getX() const
std::list< tf::Point > transformed_right_points_
Definition: grid_plugin.h:114
boost::shared_ptr< tf::TransformListener > tf_
std::list< tf::Point > left_points_
Definition: grid_plugin.h:108
void SaveConfig(YAML::Emitter &emitter, const std::string &path)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
void SetColumns(int columns)
void SetSize(double size)


mapviz_plugins
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 19:25:16