point_drawing_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 #include <vector>
33 #include <list>
34 
35 #include <QDialog>
36 #include <QGLWidget>
37 #include <QPalette>
38 #include <QPainter>
39 
40 #include <opencv2/core/core.hpp>
41 
44 
45 namespace mapviz_plugins
46 {
48  : arrow_size_(25),
49  draw_style_(LINES),
50  position_tolerance_(0.0),
51  buffer_size_(0),
52  covariance_checked_(false),
53  new_lap_(true),
54  lap_checked_(false),
55  buffer_holder_(false),
56  scale_(1.0),
57  static_arrow_sizes_(false),
58  got_begin_(false)
59  {
60  }
61 
63  {
64  points_.clear();
65  }
66 
68  {
69  if (icon_)
70  {
71  QPixmap icon(16, 16);
72  icon.fill(Qt::transparent);
73 
74  QPainter painter(&icon);
75  painter.setRenderHint(QPainter::Antialiasing, true);
76 
77  QPen pen(color_);
78 
79  if (draw_style_ == POINTS)
80  {
81  pen.setWidth(7);
82  pen.setCapStyle(Qt::RoundCap);
83  painter.setPen(pen);
84  painter.drawPoint(8, 8);
85  }
86  else if (draw_style_ == LINES)
87  {
88  pen.setWidth(3);
89  pen.setCapStyle(Qt::FlatCap);
90  painter.setPen(pen);
91  painter.drawLine(1, 14, 14, 1);
92  }
93  else if (draw_style_ == ARROWS)
94  {
95  pen.setWidth(2);
96  pen.setCapStyle(Qt::SquareCap);
97  painter.setPen(pen);
98  painter.drawLine(2, 13, 13, 2);
99  painter.drawLine(13, 2, 13, 8);
100  painter.drawLine(13, 2, 7, 2);
101  }
102 
103  icon_->SetPixmap(icon);
104  }
105  }
106 
108  {
109  arrow_size_ = arrowSize;
110  }
111 
113  {
114  if (style == "lines")
115  {
116  draw_style_ = LINES;
117  }
118  else if (style == "points")
119  {
121  }
122  else if (style == "arrows")
123  {
125  }
126 
127  DrawIcon();
128  }
129 
131  {
132  static_arrow_sizes_ = isChecked;
133  }
134 
136  {
137  position_tolerance_ = value;
138  }
139 
141  {
142  buffer_size_ = value;
143 
144  if (buffer_size_ > 0)
145  {
146  while (static_cast<int>(points_.size()) > buffer_size_)
147  {
148  points_.pop_front();
149  }
150  }
151  }
152 
154  {
155  scale_ = scale;
156  bool transformed = true;
157  if (lap_checked_)
158  {
159  CollectLaps();
160 
161  if (draw_style_ == ARROWS)
162  {
163  transformed &= DrawLapsArrows();
164  }
165  else
166  {
167  transformed &= DrawLaps();
168  }
169  }
170  else if (buffer_size_ == INT_MAX)
171  {
173  laps_.clear();
174  got_begin_ = false;
175  }
176  if (draw_style_ == ARROWS)
177  {
178  transformed &= DrawArrows();
179  }
180  else
181  {
182  transformed &= DrawLines();
183  }
184 
185  return transformed;
186  }
187 
189  {
190  if (!got_begin_)
191  {
193  points_.clear();
195  buffer_size_ = INT_MAX;
196  got_begin_ = true;
197  }
199  if (((std::fabs(check.x()) <= 3) && (std::fabs(check.y()) <= 3)) &&
200  !new_lap_)
201  {
202  new_lap_ = true;
203  if (points_.size() > 0)
204  {
205  laps_.push_back(points_);
206  laps_[0].pop_back();
207  points_.clear();
208  points_.push_back(cur_point_);
209  }
210  }
211 
212  if (((std::fabs(check.x()) > 25) && (std::fabs(check.y()) > 25)) &&
213  new_lap_)
214  {
215  new_lap_ = false;
216  }
217  }
218 
220  {
221  bool success = cur_point_.transformed;
222  glColor4d(color_.redF(), color_.greenF(), color_.blueF(), 1.0);
223  if (draw_style_ == LINES)
224  {
225  glLineWidth(3);
226  glBegin(GL_LINE_STRIP);
227  }
228  else
229  {
230  glPointSize(6);
231  glBegin(GL_POINTS);
232  }
233 
234  std::list<StampedPoint>::iterator it = points_.begin();
235  for (; it != points_.end(); ++it)
236  {
237  success &= it->transformed;
238  if (it->transformed)
239  {
240  glVertex2d(it->transformed_point.getX(), it->transformed_point.getY());
241  }
242  }
243 
245  {
246  glVertex2d(cur_point_.transformed_point.getX(),
248  }
249 
250  glEnd();
251 
252  return success;
253  }
254 
256  {
257  if (it.transformed)
258  {
259  glVertex2d(it.transformed_point.getX(),
260  it.transformed_point.getY());
261 
262  glVertex2d(it.transformed_arrow_point.getX(),
264 
265  glVertex2d(it.transformed_arrow_point.getX(),
267  glVertex2d(it.transformed_arrow_left.getX(),
269 
270  glVertex2d(it.transformed_arrow_point.getX(),
272  glVertex2d(it.transformed_arrow_right.getX(),
274  return true;
275  }
276  return false;
277  }
278 
280  {
281  bool success = true;
282  glLineWidth(2);
283  glBegin(GL_LINES);
284  glColor4d(color_.redF(), color_.greenF(), color_.blueF(), 0.5);
285  std::list<StampedPoint>::iterator it = points_.begin();
286  for (; it != points_.end(); ++it)
287  {
288  success &= DrawArrow(*it);
289  }
290 
291  success &= DrawArrow(cur_point_);
292 
293  glEnd();
294 
295  return success;
296  }
297 
298  void PointDrawingPlugin::SetColor(const QColor& color)
299  {
300  if (color != color_)
301  {
302  color_ = color;
303  DrawIcon();
304  }
305  }
306 
308  {
310  if (GetTransform(point.source_frame, point.stamp, transform))
311  {
312  point.transformed_point = transform * point.point;
313 
314  tf::Transform orientation(tf::Transform(transform.GetOrientation()) *
315  point.orientation);
316 
317  double size = static_cast<double>(arrow_size_);
319  {
320  size *= scale_;
321  }
322  else
323  {
324  size /= 10.0;
325  }
326  double arrow_width = size / 5.0;
327  double head_length = size * 0.75;
328 
330  point.transformed_point + orientation * tf::Point(size, 0.0, 0.0);
331  point.transformed_arrow_left =
332  point.transformed_point + orientation * tf::Point(head_length, -arrow_width, 0.0);
334  point.transformed_point + orientation * tf::Point(head_length, arrow_width, 0.0);
335 
337  {
338  for (uint32_t i = 0; i < point.cov_points.size(); i++)
339  {
340  point.transformed_cov_points[i] = transform * point.cov_points[i];
341  }
342  }
343 
344  point.transformed = true;
345  return true;
346  }
347 
348  point.transformed = false;
349  return false;
350  }
351 
353  {
354  bool transformed = false;
355 
356  std::list<StampedPoint>::iterator points_it = points_.begin();
357  for (; points_it != points_.end(); ++points_it)
358  {
359  transformed = transformed | TransformPoint(*points_it);
360  }
361 
362  transformed = transformed | TransformPoint(cur_point_);
363  if (laps_.size() > 0)
364  {
365  for (size_t i = 0; i < laps_.size(); i++)
366  {
367  std::list<StampedPoint>::iterator lap_it = laps_[i].begin();
368  for (; lap_it != laps_[i].end(); ++lap_it)
369  {
370  transformed = transformed | TransformPoint(*lap_it);
371  }
372  }
373  }
374  if (!points_.empty() && !transformed)
375  {
376  PrintError("No transform between " + cur_point_.source_frame + " and " +
377  target_frame_);
378  }
379  }
380 
382  {
383  bool transformed = points_.size() != 0;
384  glColor4d(color_.redF(), color_.greenF(), color_.blueF(), 0.5);
385  glLineWidth(3);
386  QColor base_color = color_;
387  if (laps_.size() != 0)
388  {
389  for (size_t i = 0; i < laps_.size(); i++)
390  {
391  UpdateColor(base_color, static_cast<int>(i));
392  if (draw_style_ == LINES)
393  {
394  glLineWidth(3);
395  glBegin(GL_LINE_STRIP);
396  }
397  else
398  {
399  glPointSize(6);
400  glBegin(GL_POINTS);
401  }
402 
403  std::list<StampedPoint>::iterator it = laps_[i].begin();
404  for (; it != laps_[i].end(); it++)
405  {
406  if (it->transformed)
407  {
408  glVertex2d(it->transformed_point.getX(),
409  it->transformed_point.getY());
410  }
411  }
412  glEnd();
413  }
414  }
415 
416  if (draw_style_ == LINES)
417  {
418  glLineWidth(3);
419  glBegin(GL_LINE_STRIP);
420  }
421  else
422  {
423  glPointSize(6);
424  glBegin(GL_POINTS);
425  }
426 
427  glColor4d(base_color.redF(), base_color.greenF(), base_color.blueF(), 0.5);
428 
429  if (points_.size() > 0)
430  {
431  std::list<StampedPoint>::iterator it = points_.begin();
432  for (; it != points_.end(); ++it)
433  {
434  transformed &= it->transformed;
435  if (it->transformed)
436  {
437  glVertex2d(it->transformed_point.getX(),
438  it->transformed_point.getY());
439  }
440  }
441  }
442 
443  glEnd();
444  return transformed;
445  }
446 
447  void PointDrawingPlugin::UpdateColor(QColor base_color, int i)
448  {
449  int hue = static_cast<int>(color_.hue() + (i + 1.0) * 10.0 * M_PI);
450  if (hue > 360)
451  {
452  hue %= 360;
453  }
454  int sat = color_.saturation();
455  int v = color_.value();
456  base_color.setHsv(hue, sat, v);
457  glColor4d(base_color.redF(), base_color.greenF(), base_color.blueF(),
458  0.5);
459  }
460 
462  {
463  bool success = laps_.size() != 0 && points_.size() != 0;
464  glColor4d(color_.redF(), color_.greenF(), color_.blueF(), 0.5);
465  glLineWidth(2);
466  QColor base_color = color_;
467  if (laps_.size() != 0)
468  {
469  for (size_t i = 0; i < laps_.size(); i++)
470  {
471  UpdateColor(base_color, static_cast<int>(i));
472  std::list<StampedPoint>::iterator it = laps_[i].begin();
473  for (; it != laps_[i].end(); ++it)
474  {
475  glBegin(GL_LINE_STRIP);
476  success &= DrawArrow(*it);
477  glEnd();
478  }
479  }
480  glEnd();
481 
482  int hue = static_cast<int>(color_.hue() + laps_.size() * 10.0 * M_PI);
483  int sat = color_.saturation();
484  int v = color_.value();
485  base_color.setHsv(hue, sat, v);
486  glColor4d(base_color.redF(), base_color.greenF(), base_color.blueF(),
487  0.5);
488  }
489 
490  if (points_.size() > 0)
491  {
492  std::list<StampedPoint>::iterator it = points_.begin();
493  for (; it != points_.end(); ++it)
494  {
495  glBegin(GL_LINE_STRIP);
496  success &= DrawArrow(*it);
497  glEnd();
498  }
499  }
500 
501  return success;
502  }
503 }
bool GetTransform(const ros::Time &stamp, swri_transform_util::Transform &transform, bool use_latest_transforms=true)
ROSCPP_DECL bool check()
IconWidget * icon_
void transform(Route &route, const swri_transform_util::Transform &transform, const std::string &target_frame)
virtual void PositionToleranceChanged(double value)
virtual void SetArrowSize(int arrowSize)
virtual void UpdateColor(QColor base_color, int i)
tf::Quaternion GetOrientation() const
TFSIMD_FORCE_INLINE const tfScalar & getY() const
std::string target_frame_
tf::Vector3 Point
void SetPixmap(QPixmap pixmap)
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::vector< std::list< StampedPoint > > laps_
virtual void SetColor(const QColor &color)
virtual void SetStaticArrowSizes(bool isChecked)
virtual bool DrawArrow(const StampedPoint &point)
TFSIMD_FORCE_INLINE const tfScalar & getX() const
virtual void SetDrawStyle(QString style)
virtual bool TransformPoint(StampedPoint &point)
virtual void PrintError(const std::string &message)=0


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