ClusterLabelDisplay.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Robot Operating System code by the University of Osnabrück
5  * Copyright (c) 2015, University of Osnabrück
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  * 1. Redistributions of source code must retain the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  *
21  * 3. Neither the name of the copyright holder nor the names of its
22  * contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
30  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
33  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  *
38  *
39  *
40  * ClusterLabelDisplay.cpp
41  *
42  *
43  * authors:
44  *
45  * Kristin Schmidt <krschmidt@uni-osnabrueck.de>
46  * Jan Philipp Vogtherr <jvogtherr@uni-osnabrueck.de>
47  */
48 
49 #include <ClusterLabelDisplay.hpp>
50 #include <ClusterLabelVisual.hpp>
51 #include <ClusterLabelTool.hpp>
52 
59 
60 namespace rviz_map_plugin
61 {
62 Ogre::ColourValue getRainbowColor(float value)
63 {
64  float r = 0.0f;
65  float g = 0.0f;
66  float b = 0.0f;
67 
68  value = std::min(value, 1.0f);
69  value = std::max(value, 0.0f);
70 
71  float h = value * 5.0f + 1.0f;
72  int i = floor(h);
73  float f = h - i;
74  if (!(i & 1))
75  f = 1 - f; // if i is even
76  float n = 1 - f;
77 
78  if (i <= 1)
79  r = n, g = 0, b = 1;
80  else if (i == 2)
81  r = 0, g = n, b = 1;
82  else if (i == 3)
83  r = 0, g = 1, b = n;
84  else if (i == 4)
85  r = n, g = 1, b = 0;
86  else if (i >= 5)
87  r = 1, g = n, b = 0;
88 
89  return Ogre::ColourValue(r, g, b, 1.0f);
90 }
91 
93 {
95  new rviz::EnumProperty("Active label", "__NEW__", "Current active label. Can be edited with Cluster Label Tool",
96  this, SLOT(changeVisual()), this);
97  m_alphaProperty = new rviz::FloatProperty("Transparency", 1.0f,
98  "Transparency of the Labeled Cluster Visualization. 0.0 is fully "
99  "transparent, 1.0 fully opaque",
100  this, SLOT(updateColors()), this);
101  m_alphaProperty->setMin(0.0f);
102  m_alphaProperty->setMax(1.0f);
103 
104  m_colorsProperty = new rviz::Property("Colors", "", "colors", this, SLOT(updateColors()), this);
107  new rviz::FloatProperty("Brush Size", 1.0f, "Brush Size", this, SLOT(updateSphereSize()), this);
108  m_phantomVisualProperty = new rviz::BoolProperty("Show Phantom", false,
109  "Show a transparent silhouette of the whole mesh to help with "
110  "labeling",
111  this, SLOT(updatePhantomVisual()), this);
112 
113  setStatus(rviz::StatusProperty::Error, "Display", "Cant be used without Map3D plugin");
114 }
115 
117 {
118 }
119 
120 // =====================================================================================================================
121 // Public Q_SLOTS
122 
123 std::shared_ptr<Geometry> ClusterLabelDisplay::getGeometry()
124 {
125  if (!m_geometry)
126  {
127  ROS_ERROR("Label Display: Geometry requested, but none available!");
128  }
129  return m_geometry;
130 }
131 
132 void ClusterLabelDisplay::setData(shared_ptr<Geometry> geometry, vector<Cluster> clusters)
133 {
134  if (has_data)
135  {
136  ROS_WARN("Label Display: already has data, but setData() was called again!");
137  }
138 
139  // Copy data
140  m_geometry = geometry;
141  m_clusterList = clusters;
142  m_clusterList.insert(m_clusterList.begin(), Cluster("__NEW__", vector<uint32_t>()));
143 
144  // Set flag
145  ROS_INFO("Label Display: received data");
146  has_data = true;
147 
148  // Draw visuals
149  if (isEnabled())
150  updateMap();
151 
152  setStatus(rviz::StatusProperty::Ok, "Display", "");
153 }
154 
155 // =====================================================================================================================
156 // Callbacks
157 
159 {
160  // Look for an existing label tool or create a new one
162 }
163 
165 {
166  updateMap();
167 }
168 
170 {
171  m_visuals.clear();
172  m_phantomVisual.reset();
173  m_tool->resetVisual();
174 }
175 
176 // =====================================================================================================================
177 // Callbacks triggered from UI events (mostly)
178 
180 {
181  if (m_activeVisualProperty->getStdString().empty())
182  {
183  ROS_ERROR("Label Display: Should change visual but no visual selected!");
184  return;
185  }
186 
187  ROS_INFO_STREAM("Label Display: Changed active visual to '" << m_activeVisualProperty->getStdString() << "'");
188 
190 
191  // Active visual has changed, notify label tool that it has to refresh its pointer on the active visual
192  notifyLabelTool();
193 }
194 
196 {
197  ROS_INFO("Label Display: Update");
198 
199  if (!has_data)
200  {
201  ROS_WARN("Label Display: No data available! Can't show map");
202  return;
203  }
204 
205  // Reset the visual of the label tool so that it can be deleted
206  m_tool->resetVisual();
207 
208  // Now create the visuals for the loaded clusters
210 
211  // Fill options for dropdown menu containing the cluster names
213 
214  // Create a phantom visual if it is enabled
216 
217  // Notify label tool for changes. The label tool should now destroy its visual and get a new one from this obj
218  notifyLabelTool();
219 
220  // Apply the default colors to the visuals
221  updateColors();
222 
223  // Update the tool's assigned display (to this display)
224  m_tool->setDisplay(this);
225 
226  // All good
228 }
229 
231 {
232  for (int i = 0; i < m_colorProperties.size(); i++)
233  {
234  auto colorProp = m_colorProperties[i];
235  m_visuals[i]->setColor(colorProp->getOgreColor(), m_alphaProperty->getFloat());
236  }
237 }
238 
240 {
242 }
243 
245 {
247  {
248  m_phantomVisual.reset(nullptr);
249  }
250  else if (!m_phantomVisual)
251  {
253  }
254 }
255 
257 {
258  // Clear options
261  m_colorProperties.clear();
262 
263  for (int i = 0; i < m_clusterList.size(); i++)
264  {
265  // Add cluster labels to dropdown menu
266  m_activeVisualProperty->addOption(QString::fromStdString(m_clusterList[i].name), i);
267 
268  // Add color options
269  Ogre::ColourValue rainbowColor = getRainbowColor((((float)i + 1) / m_clusterList.size()));
270  m_colorProperties.emplace_back(new rviz::ColorProperty(
271  QString::fromStdString(m_clusterList[i].name),
272  QColor(rainbowColor.r * 255, rainbowColor.g * 255, rainbowColor.b * 255),
273  QString::fromStdString(m_clusterList[i].name), m_colorsProperty, SLOT(updateColors()), this));
274  }
275 }
276 
277 // =====================================================================================================================
278 // Visuals logic
279 
281 {
282  // Destroy all current visuals
283  if (!m_visuals.empty())
284  {
285  m_visuals.clear();
286  }
287 
288  // Create a visual for each entry in the cluster list
289  float colorIndex = 0.0; // index for coloring
290  for (int i = 0; i < m_clusterList.size(); i++)
291  {
292  std::stringstream ss;
293  ss << "ClusterLabelVisual_" << i;
294 
295  auto visual = std::make_shared<ClusterLabelVisual>(context_, ss.str(), m_geometry);
296  ROS_DEBUG_STREAM("Label Display: Create visual for label '" << m_clusterList[i].name << "'");
297  visual->setFacesInCluster(m_clusterList[i].faces);
298  visual->setColor(getRainbowColor((++colorIndex / m_clusterList.size())), m_alphaProperty->getFloat());
299  m_visuals.push_back(visual);
300  }
301 }
302 
304 {
305  m_phantomVisual.reset(new ClusterLabelVisual(context_, "ClusterLabelPhantomVisual", m_geometry));
306  vector<uint32_t> allFacesVector;
307  for (uint32_t i = 0; i < m_geometry->faces.size(); i++)
308  {
309  allFacesVector.push_back(i);
310  }
311  m_phantomVisual->setFacesInCluster(allFacesVector);
312  m_phantomVisual->setColor(Ogre::ColourValue(0.2, 0.3, 0.2), 0.1);
313 }
314 
315 // =====================================================================================================================
316 // Label tool
317 
319 {
320  // Check if the cluster label tool is already opened
321  rviz::ToolManager* toolManager = context_->getToolManager();
322  QStringList toolClasses = toolManager->getToolClasses();
323  bool foundTool = false;
324  for (int i = 0; i < toolClasses.size(); i++)
325  {
326  if (toolClasses.at(i).contains("ClusterLabel"))
327  {
328  m_tool = static_cast<ClusterLabelTool*>(toolManager->getTool(i));
329  foundTool = true;
330  break;
331  }
332  }
333 
334  if (!foundTool)
335  {
336  m_tool = static_cast<ClusterLabelTool*>(context_->getToolManager()->addTool("rviz_map_plugin/ClusterLabel"));
337  }
338 }
339 
341 {
343 }
344 
345 void ClusterLabelDisplay::addLabel(std::string label, std::vector<uint32_t> faces)
346 {
347  ROS_INFO_STREAM("Cluster Label Display: add label '" << label << "'");
348 
349  Q_EMIT signalAddLabel(Cluster(label, faces));
350 }
351 
352 } // End namespace rviz_map_plugin
353 
rviz_map_plugin::ClusterLabelDisplay::onInitialize
void onInitialize()
RViz callback on initialize.
Definition: ClusterLabelDisplay.cpp:158
rviz::ToolManager::getToolClasses
QStringList getToolClasses()
rviz::BoolProperty::getBool
virtual bool getBool() const
rviz_map_plugin::ClusterLabelTool
Tool for selecting faces.
Definition: ClusterLabelTool.hpp:125
rviz::EnumProperty::getOptionInt
virtual int getOptionInt()
rviz_map_plugin::ClusterLabelDisplay::onDisable
void onDisable()
RViz callback on disable.
Definition: ClusterLabelDisplay.cpp:169
rviz::Display::isEnabled
bool isEnabled() const
rviz::EnumProperty::clearOptions
virtual void clearOptions()
rviz_map_plugin::ClusterLabelDisplay::has_data
bool has_data
A variable that will be set to true, once the initial data has arrived.
Definition: ClusterLabelDisplay.hpp:290
rviz::ToolManager
rviz::StatusProperty::Error
Error
rviz_map_plugin::ClusterLabelDisplay::~ClusterLabelDisplay
~ClusterLabelDisplay()
Destructor.
Definition: ClusterLabelDisplay.cpp:116
rviz_map_plugin::ClusterLabelDisplay::onEnable
void onEnable()
RViz callback on enable.
Definition: ClusterLabelDisplay.cpp:164
rviz_map_plugin::getRainbowColor
Ogre::ColourValue getRainbowColor(float value)
Definition: ClusterLabelDisplay.cpp:62
rviz::BoolProperty
rviz::FloatProperty::setMax
void setMax(float max)
int_property.h
rviz_map_plugin::ClusterLabelDisplay::getGeometry
shared_ptr< Geometry > getGeometry()
Getter for the current geometry.
Definition: ClusterLabelDisplay.cpp:123
ClusterLabelDisplay.hpp
enum_property.h
rviz_map_plugin::ClusterLabelDisplay::m_activeVisualId
uint32_t m_activeVisualId
ID of the current active visual.
Definition: ClusterLabelDisplay.hpp:257
rviz_map_plugin::ClusterLabelDisplay::m_sphereSizeProperty
rviz::FloatProperty * m_sphereSizeProperty
Property to set the brushsize of the sphere brush of the label tool from this package.
Definition: ClusterLabelDisplay.hpp:281
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
rviz_map_plugin::ClusterLabelDisplay::m_tool
ClusterLabelTool * m_tool
Label tool.
Definition: ClusterLabelDisplay.hpp:266
rviz::FloatProperty::setMin
void setMin(float min)
float_property.h
f
f
rviz::ColorProperty
rviz::Display
rviz::EnumProperty
rviz_map_plugin::ClusterLabelTool::resetVisual
void resetVisual()
Resets the current visual.
Definition: ClusterLabelTool.cpp:732
rviz::FloatProperty
rviz::Property
ClusterLabelVisual.hpp
class_list_macros.h
rviz_map_plugin::ClusterLabelDisplay
Display class for the map plugin.
Definition: ClusterLabelDisplay.hpp:136
rviz::Display::setStatus
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
rviz_map_plugin::ClusterLabelDisplay::fillPropertyOptions
void fillPropertyOptions()
Dynamically fills the dropdown menus of those properties.
Definition: ClusterLabelDisplay.cpp:256
rviz_map_plugin::ClusterLabelDisplay::signalAddLabel
void signalAddLabel(Cluster cluster)
This signal is used for delegating new label data to the master display.
bool_property.h
rviz::ToolManager::getTool
Tool * getTool(int index)
rviz_map_plugin::ClusterLabelDisplay::notifyLabelTool
void notifyLabelTool()
Refreshes the tool's current visual.
Definition: ClusterLabelDisplay.cpp:340
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rviz_map_plugin::ClusterLabelTool::setSphereSize
void setSphereSize(float size)
Adjust the sphere size for the brush tool.
Definition: ClusterLabelTool.cpp:247
rviz::FloatProperty::getFloat
virtual float getFloat() const
rviz::EnumProperty::addOption
virtual void addOption(const QString &option, int value=0)
rviz_map_plugin::ClusterLabelDisplay::m_colorsProperty
rviz::Property * m_colorsProperty
Property for selecting colors (menu)
Definition: ClusterLabelDisplay.hpp:275
rviz_map_plugin::ClusterLabelDisplay::addLabel
void addLabel(string label, vector< uint32_t > faces)
The tool will call this function and emit the signal below to the master display to create the label.
Definition: ClusterLabelDisplay.cpp:345
rviz_map_plugin::ClusterLabelDisplay::m_visuals
vector< shared_ptr< ClusterLabelVisual > > m_visuals
Visuals.
Definition: ClusterLabelDisplay.hpp:254
rviz::StringProperty::getStdString
std::string getStdString()
rviz_map_plugin::ClusterLabelDisplay::updateSphereSize
void updateSphereSize()
Updates the sphere size for the brush tool.
Definition: ClusterLabelDisplay.cpp:239
rviz::StatusProperty::Ok
Ok
rviz::DisplayContext::getToolManager
virtual ToolManager * getToolManager() const=0
rviz_map_plugin::ClusterLabelDisplay::updateMap
void updateMap()
Update the map, based on newly loaded data since the last update.
Definition: ClusterLabelDisplay.cpp:195
rviz_map_plugin::ClusterLabelDisplay::createVisualsFromClusterList
void createVisualsFromClusterList()
Create visuals for each cluster in the list.
Definition: ClusterLabelDisplay.cpp:280
rviz_map_plugin::ClusterLabelDisplay::setData
void setData(shared_ptr< Geometry > geometry, vector< Cluster > clusters)
Setter for the geometry and cluster data.
Definition: ClusterLabelDisplay.cpp:132
ROS_WARN
#define ROS_WARN(...)
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
rviz_map_plugin::ClusterLabelDisplay::ClusterLabelDisplay
ClusterLabelDisplay()
Constructor.
Definition: ClusterLabelDisplay.cpp:92
rviz_map_plugin::ClusterLabelDisplay::m_phantomVisual
unique_ptr< ClusterLabelVisual > m_phantomVisual
Additional visual to help with labeling without a TexturedMesh.
Definition: ClusterLabelDisplay.hpp:260
rviz::ToolManager::addTool
Tool * addTool(const QString &tool_class_lookup_name)
rviz_map_plugin::ClusterLabelDisplay::m_phantomVisualProperty
rviz::BoolProperty * m_phantomVisualProperty
Property to hide or show a phantom visual.
Definition: ClusterLabelDisplay.hpp:284
ClusterLabelTool.hpp
rviz_map_plugin::ClusterLabelVisual
Visual to show a labeled cluster.
Definition: ClusterLabelVisual.hpp:80
rviz_map_plugin::ClusterLabelDisplay::m_activeVisualProperty
rviz::EnumProperty * m_activeVisualProperty
Property for the current active visual.
Definition: ClusterLabelDisplay.hpp:269
rviz_map_plugin::ClusterLabelTool::setDisplay
void setDisplay(ClusterLabelDisplay *display)
Connects this tool with a given display.
Definition: ClusterLabelTool.cpp:261
rviz_map_plugin::ClusterLabelDisplay::updateColors
void updateColors()
Updates the colors, based on newly loaded data since the last update.
Definition: ClusterLabelDisplay.cpp:230
rviz::Display::context_
DisplayContext * context_
rviz::Property::setReadOnly
virtual void setReadOnly(bool read_only)
rviz_map_plugin::ClusterLabelDisplay::createPhantomVisual
void createPhantomVisual()
Creates a phantom visual.
Definition: ClusterLabelDisplay.cpp:303
rviz_map_plugin::ClusterLabelTool::setVisual
void setVisual(std::shared_ptr< ClusterLabelVisual > visual)
Connects this tool with a given visual.
Definition: ClusterLabelTool.cpp:231
ROS_ERROR
#define ROS_ERROR(...)
rviz_map_plugin
Definition: ClusterLabelDisplay.hpp:120
rviz_map_plugin::ClusterLabelDisplay::updatePhantomVisual
void updatePhantomVisual()
Updates the phantom visual, based on newly loaded data since the last update.
Definition: ClusterLabelDisplay.cpp:244
rviz_map_plugin::ClusterLabelDisplay::m_colorProperties
std::vector< rviz::ColorProperty * > m_colorProperties
Properties for selecting colors (menu-items)
Definition: ClusterLabelDisplay.hpp:278
string_property.h
rviz_map_plugin::ClusterLabelDisplay::m_clusterList
vector< Cluster > m_clusterList
Cluster data.
Definition: ClusterLabelDisplay.hpp:263
ROS_INFO
#define ROS_INFO(...)
rviz::Property::removeChildren
virtual void removeChildren(int start_index=0, int count=-1)
rviz_map_plugin::ClusterLabelDisplay::m_geometry
shared_ptr< Geometry > m_geometry
Geometry.
Definition: ClusterLabelDisplay.hpp:251
rviz_map_plugin::Cluster
Struct for clusters.
Definition: Types.hpp:87
rviz_map_plugin::ClusterLabelDisplay::m_alphaProperty
rviz::FloatProperty * m_alphaProperty
Property to set transparency.
Definition: ClusterLabelDisplay.hpp:272
color_property.h
rviz_map_plugin::ClusterLabelDisplay::changeVisual
void changeVisual()
Slot for changing the visual to the selected visual from the dropdown menu.
Definition: ClusterLabelDisplay.cpp:179
rviz_map_plugin::ClusterLabelDisplay::initializeLabelTool
void initializeLabelTool()
Programmatically create an instance of the label tool from this package.
Definition: ClusterLabelDisplay.cpp:318


rviz_map_plugin
Author(s): Sebastian Pütz , Kristin Schmidt , Jan Philipp Vogtherr , Malte kleine Piening
autogenerated on Sun Jan 21 2024 04:06:25