polygons_display.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
37 #include <color_util/convert.h>
39 #include <vector>
40 
41 
42 
43 namespace robot_nav_rviz_plugins
44 {
46 {
48  outline_color_property_ = new rviz::ColorProperty("Outline Color", QColor(79, 98, 142), "Color to draw the polygon.",
49  this, SLOT(updateProperties()));
50 
51  color_mode_property_ = new rviz::EnumProperty("Fill Color Mode", "Single Color",
52  "Color scheme for coloring each polygon", this, SLOT(updateStyle()));
53  color_mode_property_->addOption("Single Color", static_cast<int>(FillColorMode::SINGLE));
54  color_mode_property_->addOption("From Message", static_cast<int>(FillColorMode::FROM_MSG));
55  color_mode_property_->addOption("Unique", static_cast<int>(FillColorMode::UNIQUE));
56 
57  filler_color_property_ = new rviz::ColorProperty("Fill Color", QColor(22, 41, 85), "Color to fill the polygon.",
58  this, SLOT(updateProperties()));
59  filler_alpha_property_ = new rviz::FloatProperty("Alpha", 0.8, "Amount of transparency to apply to the filler.",
60  this, SLOT(updateProperties()));
63 
64  zoffset_property_ = new rviz::FloatProperty("Z-Offset", 0.0, "Offset in the Z direction.", this,
65  SLOT(updateProperties()));
66 
67  for (const auto& color : color_util::getNamedColors())
68  {
69  if (color.a == 0.0) continue;
70  unique_colors_.push_back(color_util::toMsg(color));
71  }
72 }
73 
75 {
76  for (auto& outline_object : outline_objects_)
77  {
78  delete outline_object;
79  }
80  for (auto filler_object : filler_objects_)
81  {
82  delete filler_object;
83  }
84 }
85 
87 {
89  resetOutlines();
90  resetFillers();
91 }
92 
94 {
95  for (auto& outline_object : outline_objects_)
96  {
97  outline_object->reset();
98  }
99 }
100 
102 {
103  for (auto filler_object : filler_objects_)
104  {
105  filler_object->reset();
106  }
107 }
108 
110 {
112  {
114  }
115  else
116  {
118  }
119 
121  {
123 
124  FillColorMode coloring = getFillColorMode();
125  if (coloring == FillColorMode::SINGLE)
126  {
129  }
130  else
131  {
134  }
135  }
136  else
137  {
141  }
143 }
144 
145 void PolygonsDisplay::processMessage(const nav_2d_msgs::Polygon2DCollection::ConstPtr& msg)
146 {
147  for (const auto& polygon : msg->polygons)
148  {
149  if (!validateFloats(polygon))
150  {
151  setStatus(rviz::StatusProperty::Error, "Topic", "Message contained invalid floating point values (nans or infs)");
152  return;
153  }
154  }
155 
156  Ogre::Vector3 position;
157  Ogre::Quaternion orientation;
158  if (!context_->getFrameManager()->getTransform(msg->header, position, orientation))
159  {
160  ROS_DEBUG("Error transforming from frame '%s' to frame '%s'",
161  msg->header.frame_id.c_str(), qPrintable(fixed_frame_));
162  }
163 
164  scene_node_->setPosition(position);
165  scene_node_->setOrientation(orientation);
166 
167  // Save data internally
168  saved_polygons_ = *msg;
169  saved_outlines_.clear();
170  for (const auto& complex : msg->polygons)
171  {
172  saved_outlines_.push_back(complex.outer);
173  for (const auto& inner : complex.inner)
174  {
175  saved_outlines_.push_back(inner);
176  }
177  }
178 
179  // Resize object vectors
180  const unsigned int num_outlines = saved_outlines_.size();
181  while (outline_objects_.size() > num_outlines)
182  {
183  delete outline_objects_.back();
184  outline_objects_.pop_back();
185  }
186  while (outline_objects_.size() < num_outlines)
187  {
189  }
190 
191  const unsigned int num_fillers = msg->polygons.size();
192  while (filler_objects_.size() > num_fillers)
193  {
194  delete filler_objects_.back();
195  filler_objects_.pop_back();
196  }
197  while (filler_objects_.size() < num_fillers)
198  {
200  }
201 
203 }
204 
206 {
207  double z_offset = zoffset_property_->getFloat();
208  bool empty = saved_polygons_.polygons.empty();
209 
210  resetOutlines();
211  if (mode_property_->shouldDrawOutlines() && !empty)
212  {
213  Ogre::ColourValue outline_color = rviz::qtToOgre(outline_color_property_->getColor());
214  for (unsigned int i = 0; i < saved_outlines_.size(); ++i)
215  {
216  outline_objects_[i]->setPolygon(saved_outlines_[i], outline_color, z_offset);
217  }
218  }
219 
220  if (!mode_property_->shouldDrawFiller() || empty)
221  {
222  resetFillers();
223  }
224  else
225  {
226  FillColorMode coloring = getFillColorMode();
227  std_msgs::ColorRGBA default_color; // transparent by default
228  if (coloring == FillColorMode::SINGLE)
229  {
231  }
232 
233  for (unsigned int i = 0; i < saved_polygons_.polygons.size(); ++i)
234  {
235  std_msgs::ColorRGBA color;
236  if (coloring == FillColorMode::UNIQUE)
237  {
238  color = unique_colors_[i % unique_colors_.size()];
239  }
240  else if (coloring == FillColorMode::FROM_MSG && i < saved_polygons_.colors.size())
241  {
242  color = saved_polygons_.colors[i];
243  }
244  else
245  {
246  color = default_color;
247  }
248  filler_objects_[i]->setPolygon(saved_polygons_.polygons[i], color, z_offset);
249  }
250  }
251  queueRender();
252 }
253 
254 } // namespace robot_nav_rviz_plugins
255 
256 
virtual QColor getColor() const
rviz::FloatProperty * filler_alpha_property_
std::vector< nav_2d_msgs::Polygon2D > saved_outlines_
void setMin(float min)
rviz::ColorProperty * outline_color_property_
Displays a nav_2d_msgs::Polygon2DCollection message in Rviz.
void setMax(float max)
std_msgs::ColorRGBA toMsg(const color_util::ColorRGBA &rgba)
DisplayContext * context_
std::vector< PolygonFill * > filler_objects_
Constructs a manual ogre object to display the polygon area as a triangle mesh.
Definition: polygon_parts.h:91
virtual float getFloat() const
Constructs a manual ogre object to display the polygon outline as a line strip.
Definition: polygon_parts.h:65
std_msgs::ColorRGBA getColor(rviz::ColorProperty *color_property, rviz::FloatProperty *alpha_property=nullptr)
Given a Color Property and an optional Float property, return a ROS Color message.
const std::string & getName() const
Ogre::SceneNode * scene_node_
std::vector< std_msgs::ColorRGBA > unique_colors_
Ogre::ColourValue qtToOgre(const QColor &c)
QString fixed_frame_
std::vector< PolygonOutline * > outline_objects_
const std::vector< ColorRGBA24 > & getNamedColors()
virtual void addOption(const QString &option, int value=0)
Several reusable pieces for displaying polygons.
void queueRender()
nav_2d_msgs::Polygon2DCollection saved_polygons_
virtual FrameManager * getFrameManager() const =0
PolygonDisplayModeProperty * mode_property_
Ogre::SceneManager * scene_manager_
bool getTransform(const Header &header, Ogre::Vector3 &position, Ogre::Quaternion &orientation)
Wrapper for EnumProperty + enum for whether to display the outline, area, or both.
rviz::ColorProperty * filler_color_property_
bool validateFloats(const nav_grid::NavGridInfo &info)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
void processMessage(const nav_2d_msgs::Polygon2DCollection::ConstPtr &msg) override
#define ROS_DEBUG(...)


robot_nav_rviz_plugins
Author(s):
autogenerated on Sun Jan 10 2021 04:08:58