PointCloudVisualization.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::PointCloudVisualization
17  * @author Peter Kaiser ( peter dot kaiser at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
26 
27 #include <thread>
28 
29 namespace visionx
30 {
32  providerName(providerName),
33  component(component)
34  {
36  task->start();
37  }
38 
40  {
41  stop();
42  }
43 
45  {
46  ARMARX_INFO << "Starting PointCloudProvider handler for '" << providerName << "'";
47 
48  while (!task->isStopped())
49  {
51  {
52  continue;
53  }
54 
55  ARMARX_INFO << "Receiving point cloud from provider '" << providerName << "'";
57 
58  std::this_thread::sleep_for(std::chrono::milliseconds(1));
59  }
60 
61  ARMARX_INFO << "Stopping PointCloudProvider handler for '" << providerName << "'";
62  }
63 
65  {
66  task->stop();
67  }
68 
69 
70  template<typename PointT>
71  void PointCloudVisualization::processPointCloud(const std::string& providerName)
72  {
73  typename pcl::PointCloud<PointT>::Ptr pc(new pcl::PointCloud<PointT>());
74  if (!getPointClouds(providerName, pc))
75  {
76  ARMARX_WARNING << "Unable to get point cloud data";
77  return;
78  }
79 
80  ARMARX_INFO << "Visualizing point cloud from provider '" << providerName << "'";
81 
82  {
83  std::unique_lock lock(pointCloudMutex);
84 
85  visualizations[providerName].clear();
86 
87  DebugDrawerPointCloudPtr dd_pc_plain(new armarx::DebugDrawer24BitColoredPointCloud);
89 
90  visualizations[providerName][visionx::ePoints] = dd_pc_plain;
91 
92 
93  if (pointCloudFormats[providerName]->type == visionx::eColoredPoints || pointCloudFormats[providerName]->type == visionx::eColoredLabeledPoints)
94  {
95  DebugDrawerPointCloudPtr dd_pc_colors(new armarx::DebugDrawer24BitColoredPointCloud);
97  visualizations[providerName][visionx::eColoredPoints] = dd_pc_colors;
98  }
99 
100  if (pointCloudFormats[providerName]->type == visionx::eLabeledPoints || pointCloudFormats[providerName]->type == visionx::eColoredLabeledPoints)
101  {
102  DebugDrawerPointCloudPtr dd_pc_labels(new armarx::DebugDrawer24BitColoredPointCloud);
104  visualizations[providerName][visionx::eLabeledPoints] = dd_pc_labels;
105  }
106  }
107 
108  updateVisualization(providerName);
109  }
110 
111  void PointCloudVisualization::processPointCloudProvider(const std::string& providerName)
112  {
113  switch (pointCloudFormats.at(providerName)->type)
114  {
115  case visionx::ePoints:
116  processPointCloud<pcl::PointXYZ>(providerName);
117  break;
118 
119  case visionx::eColoredPoints:
120  processPointCloud<pcl::PointXYZRGBA>(providerName);
121  break;
122 
123  case visionx::eLabeledPoints:
124  processPointCloud<pcl::PointXYZL>(providerName);
125  break;
126 
127  case visionx::eColoredLabeledPoints:
128  processPointCloud<pcl::PointXYZRGBL>(providerName);
129  break;
130 
131  default:
132  ARMARX_WARNING << "Could not visualize point cloud: Unknown format";
133  break;
134  }
135  }
136 
137  bool PointCloudVisualization::waitForNewPointCloud(const std::string& providerName)
138  {
139  return waitForPointClouds(providerName, 10000);
140  }
141 
142  PointCloudProviderVisualizationInfoList PointCloudVisualization::getAvailableProviders(const Ice::Current& c)
143  {
144  PointCloudProviderVisualizationInfoList result;
145 
146  for (auto& provider : providerNames)
147  {
148  PointCloudProviderVisualizationInfo p;
149  p.name = provider;
150  p.type = pointCloudFormats[provider]->type;
151  result.push_back(p);
152  }
153 
154  return result;
155  }
156 
157  void PointCloudVisualization::enablePointCloudVisualization(const std::string& providerName, PointContentType type, bool enable, const Ice::Current& c)
158  {
159  {
160  std::unique_lock lock(pointCloudMutex);
161 
162  enabledFlags[providerName] = enable;
163  visualizationTypes[providerName] = type;
164 
165  if (enable && (visualizations.find(providerName) == visualizations.end() || visualizations.at(providerName).find(type) == visualizations.at(providerName).end()))
166  {
167  ARMARX_WARNING << "Enabling visualization for " << providerName << " with type " << pointCloudTypeToText(type) << ", which has not been created yet (no point cloud reported so far?)";
168  }
169  }
170 
171  updateVisualization(providerName);
172  }
173 
174  PointCloudVisualizationInfo PointCloudVisualization::getCurrentPointCloudVisualizationInfo(const std::string& providerName, const Ice::Current& c)
175  {
176  return PointCloudVisualizationInfo{visualizationTypes[providerName], enabledFlags[providerName]};
177  }
178 
180  {
181  offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
182 
183  providerNames = armarx::Split(getProperty<std::string>("PointCloudProviders").getValue(), ",", true);
184 
185  for (auto& providerName : providerNames)
186  {
187  usingPointCloudProvider(providerName);
188  }
189 
190  std::vector<std::string> initiallyDisabled = armarx::Split(getProperty<std::string>("InitiallyDisabledVisualizations").getValue(), ",", true);
191  for (auto& providerName : providerNames)
192  {
193  enabledFlags[providerName] = (std::find(initiallyDisabled.begin(), initiallyDisabled.end(), providerName) == initiallyDisabled.end());
194  }
195  }
196 
198  {
199  debugDrawerTopicPrx = getTopic<armarx::DebugDrawerInterfacePrx>(getProperty<std::string>("DebugDrawerTopicName").getValue());
200  if (!debugDrawerTopicPrx)
201  {
202  ARMARX_ERROR << "Failed to obtain debug drawer proxy";
203  return;
204  }
205 
206  pointCloudFormats.clear();
208  providerHandlers.clear();
209  visualizationTypes.clear();
210  visualizations.clear();
211 
212  for (auto& providerName : providerNames)
213  {
214  pointCloudFormats[providerName] = getPointCloudFormat(providerName);
215  debugDrawerVisualizationIteration[providerName] = 0;
217  }
218 
219  clearAllLayers();
220  }
221 
223  {
224  for (auto& handler : providerHandlers)
225  {
226  handler.second->stop();
227  }
228 
229  providerHandlers.clear();
230  }
231 
233  {
234  }
235 
237  {
238  // Empty, as we need to process point clouds coming from multiple providers
239  }
240 
242  {
244  }
245 
246  void PointCloudVisualization::updateVisualization(const std::string& providerName)
247  {
248  std::string oldLayerName, currentLayerName, entityName;
249 
250  try
251  {
252  std::unique_lock lock(pointCloudMutex);
253 
254  int visualizationIteration = debugDrawerVisualizationIteration.at(providerName);
255  debugDrawerVisualizationIteration[providerName]++;
256 
257  oldLayerName = getName() + "_" + providerName + "_" + std::to_string((visualizationIteration + 1) % 2);
258  currentLayerName = getName() + "_" + providerName + "_" + std::to_string(visualizationIteration % 2);
259  entityName = "PointCloud_" + std::to_string(visualizationIteration % 2);
260 
261  if (enabledFlags.at(providerName))
262  {
263  auto it = visualizationTypes.find(providerName);
264  PointContentType visualizationType = (it != visualizationTypes.end()) ? it->second : pointCloudTextToType(getProperty<std::string>("DefaultVisualizationType").getValue());
265 
266  if (visualizations.at(providerName).find(visualizationType) == visualizations.at(providerName).end())
267  {
268  visualizationType = visionx::ePoints;
269  }
270 
271  visualizationTypes[providerName] = visualizationType;
272 
273  ARMARX_INFO << "Visualizing " << providerName << " as " << pointCloudTypeToText(visualizationType);
274  debugDrawerTopicPrx->begin_set24BitColoredPointCloudVisu(currentLayerName, entityName, *visualizations.at(providerName).at(visualizationType));
275  }
276  }
277  catch (const std::exception& e)
278  {
279  ARMARX_WARNING << "Requesting visualization for " << providerName << ", but no point cloud arrived yet";
280  }
281 
282  // Wait 200ms to let the point cloud be drawn before we clear the old layer
283  usleep(200000);
284 
285  debugDrawerTopicPrx->clearLayer(oldLayerName);
286  }
287 
289  {
290  for (auto& providerName : providerNames)
291  {
292  debugDrawerTopicPrx->clearLayer(getName() + "_" + providerName + "_0");
293  debugDrawerTopicPrx->clearLayer(getName() + "_" + providerName + "_1");
294  }
295  }
296 
297  std::string PointCloudVisualization::pointCloudTypeToText(PointContentType type)
298  {
299  switch (type)
300  {
301  case visionx::ePoints:
302  return "Plain";
303 
304  case visionx::eColoredPoints:
305  return "Colors";
306 
307  case visionx::eLabeledPoints:
308  return "Labels";
309 
310  default:
311  return "Unknown";
312  }
313  }
314 
315  PointContentType PointCloudVisualization::pointCloudTextToType(std::string text)
316  {
317  std::transform(text.begin(), text.end(), text.begin(), ::tolower);
318  if (text == "plain")
319  {
320  return visionx::ePoints;
321  }
322  else if (text == "colors")
323  {
324  return visionx::eColoredPoints;
325  }
326  else if (text == "labels")
327  {
328  return visionx::eLabeledPoints;
329  }
330  return visionx::ePoints;
331  }
332 }
333 
visionx::PointCloudVisualization::visualizations
std::map< std::string, PointCloudVisualizationCollection > visualizations
Definition: PointCloudVisualization.h:163
visionx::PointCloudVisualization::processPointCloud
void processPointCloud(const std::string &providerName)
Definition: PointCloudVisualization.cpp:71
visionx::PointCloudProcessor::getPointCloudFormat
MetaPointCloudFormatPtr getPointCloudFormat(std::string providerName)
Definition: PointCloudProcessor.cpp:506
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::PointCloudVisualization::debugDrawerVisualizationIteration
std::map< std::string, int > debugDrawerVisualizationIteration
Definition: PointCloudVisualization.h:165
visionx::DebugDrawerPointCloudPtr
std::shared_ptr< armarx::DebugDrawer24BitColoredPointCloud > DebugDrawerPointCloudPtr
Definition: PointCloudVisualization.h:79
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
visionx::PointCloudVisualizationHandler::processPointCloudProvider
void processPointCloudProvider()
Definition: PointCloudVisualization.cpp:44
visionx::tools::eConvertAsColors
@ eConvertAsColors
Definition: PointCloudConversions.h:105
visionx::PointCloudVisualization
Brief description of class PointCloudVisualization.
Definition: PointCloudVisualization.h:93
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
visionx::PointCloudVisualization::processPointCloudProvider
void processPointCloudProvider(const std::string &providerName)
Definition: PointCloudVisualization.cpp:111
visionx::PointCloudVisualizationHandler::providerName
std::string providerName
Definition: PointCloudVisualization.h:53
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
visionx::PointCloudVisualization::pointCloudTypeToText
std::string pointCloudTypeToText(visionx::PointContentType type)
Definition: PointCloudVisualization.cpp:297
visionx::PointCloudVisualization::onConnectPointCloudProcessor
void onConnectPointCloudProcessor() override
Definition: PointCloudVisualization.cpp:197
visionx::PointCloudVisualization::pointCloudMutex
std::mutex pointCloudMutex
Definition: PointCloudVisualization.h:167
visionx::PointCloudVisualizationHandler::PointCloudVisualizationHandler
PointCloudVisualizationHandler(PointCloudVisualization *component, const std::string &providerName)
Definition: PointCloudVisualization.cpp:31
visionx::PointCloudVisualizationHandler::task
armarx::RunningTask< PointCloudVisualizationHandler >::pointer_type task
Definition: PointCloudVisualization.h:55
visionx::PointCloudVisualization::getAvailableProviders
PointCloudProviderVisualizationInfoList getAvailableProviders(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PointCloudVisualization.cpp:142
visionx::PointCloudVisualization::pointCloudTextToType
visionx::PointContentType pointCloudTextToType(std::string text)
Definition: PointCloudVisualization.cpp:315
visionx::tools::eConvertAsPoints
@ eConvertAsPoints
Definition: PointCloudConversions.h:104
visionx::PointCloudProcessor::waitForPointClouds
bool waitForPointClouds(int milliseconds=1000)
Wait for new PointClouds.
Definition: PointCloudProcessor.cpp:433
visionx::PointCloudVisualization::enabledFlags
std::map< std::string, bool > enabledFlags
Definition: PointCloudVisualization.h:161
visionx::PointCloudVisualizationHandlerPtr
std::shared_ptr< PointCloudVisualizationHandler > PointCloudVisualizationHandlerPtr
Definition: PointCloudVisualization.h:58
PointCloudVisualization.h
visionx::PointCloudVisualizationHandler
Definition: PointCloudVisualization.h:43
visionx::PointCloudVisualization::updateVisualization
void updateVisualization(const std::string &providerName)
Definition: PointCloudVisualization.cpp:246
visionx::PointCloudVisualization::onExitPointCloudProcessor
void onExitPointCloudProcessor() override
Definition: PointCloudVisualization.cpp:232
visionx::PointCloudVisualization::clearAllLayers
void clearAllLayers()
Definition: PointCloudVisualization.cpp:288
visionx::tools::convertFromPCLToDebugDrawer
void convertFromPCLToDebugDrawer(const pcl::PointCloud< pcl::PointXYZRGBA >::Ptr &sourcePtr, armarx::DebugDrawer24BitColoredPointCloud &target, PCLToDebugDrawerConversionMode mode, float pointSize)
Definition: PointCloudConversions.cpp:530
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
visionx::tools::eConvertAsLabels
@ eConvertAsLabels
Definition: PointCloudConversions.h:106
visionx::PointCloudVisualizationPropertyDefinitions
Definition: PointCloudVisualization.h:65
visionx::PointCloudVisualizationHandler::~PointCloudVisualizationHandler
~PointCloudVisualizationHandler()
Definition: PointCloudVisualization.cpp:39
visionx::PointCloudProcessor::getPointClouds
int getPointClouds(const PointCloudPtrT &pointCloudPtr)
Poll PointClouds from provider.
Definition: PointCloudProcessor.h:373
visionx::PointCloudVisualization::onInitPointCloudProcessor
void onInitPointCloudProcessor() override
Definition: PointCloudVisualization.cpp:179
visionx::PointCloudVisualization::enablePointCloudVisualization
void enablePointCloudVisualization(const std::string &providerName, visionx::PointContentType type, bool enable, const Ice::Current &c=Ice::emptyCurrent) override
Definition: PointCloudVisualization.cpp:157
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
visionx::PointCloudVisualizationHandler::stop
void stop()
Definition: PointCloudVisualization.cpp:64
visionx::PointCloudVisualization::waitForNewPointCloud
bool waitForNewPointCloud(const std::string &providerName)
Definition: PointCloudVisualization.cpp:137
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
PointCloudConversions.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
visionx::PointCloudVisualizationHandler::component
PointCloudVisualization * component
Definition: PointCloudVisualization.h:54
visionx::PointCloudVisualization::debugDrawerTopicPrx
armarx::DebugDrawerInterfacePrx debugDrawerTopicPrx
Definition: PointCloudVisualization.h:155
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:290
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::PointCloudVisualization::getCurrentPointCloudVisualizationInfo
PointCloudVisualizationInfo getCurrentPointCloudVisualizationInfo(const std::string &providerName, const Ice::Current &c=Ice::emptyCurrent) override
Definition: PointCloudVisualization.cpp:174
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
visionx::PointCloudProcessor::usingPointCloudProvider
void usingPointCloudProvider(std::string providerName)
Registers a delayed topic subscription and a delayed provider proxy retrieval which will be available...
Definition: PointCloudProcessor.cpp:261
visionx::PointCloudVisualization::providerHandlers
std::map< std::string, PointCloudVisualizationHandlerPtr > providerHandlers
Definition: PointCloudVisualization.h:159
visionx::PointCloudVisualization::pointCloudFormats
std::map< std::string, visionx::MetaPointCloudFormatPtr > pointCloudFormats
Definition: PointCloudVisualization.h:158
visionx::PointCloudVisualization::providerNames
std::vector< std::string > providerNames
Definition: PointCloudVisualization.h:157
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
visionx::PointCloudVisualization::process
void process() override
Definition: PointCloudVisualization.cpp:236
pc
Introduction Thank you for taking interest in our work and downloading this software This library implements the algorithm described in the paper R R R Klein Efficient RANSAC for Point Cloud Shape in Computer Graphics Blackwell June If you use this software you should cite the aforementioned paper in any resulting publication Please send comments or bug reports to Ruwen Roland BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE Example usage This section shows how to use the library to detect the shapes in a point cloud PointCloud pc
Definition: ReadMe.txt:68
visionx::PointCloudVisualization::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: PointCloudVisualization.cpp:241
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
visionx::PointCloudVisualization::visualizationTypes
std::map< std::string, visionx::PointContentType > visualizationTypes
Definition: PointCloudVisualization.h:160
visionx::PointCloudVisualization::onDisconnectPointCloudProcessor
void onDisconnectPointCloudProcessor() override
Definition: PointCloudVisualization.cpp:222