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
30
31namespace visionx
32{
42
47
48 void
50 {
51 ARMARX_INFO << "Starting PointCloudProvider handler for '" << providerName << "'";
52
53 while (!task->isStopped())
54 {
55 if (!component->waitForNewPointCloud(providerName))
56 {
57 continue;
58 }
59
60 ARMARX_INFO << "Receiving point cloud from provider '" << providerName << "'";
61 component->processPointCloudProvider(providerName);
62
63 std::this_thread::sleep_for(std::chrono::milliseconds(1));
64 }
65
66 ARMARX_INFO << "Stopping PointCloudProvider handler for '" << providerName << "'";
67 }
68
69 void
74
75 template <typename PointT>
76 void
77 PointCloudVisualization::processPointCloud(const std::string& providerName)
78 {
79 typename pcl::PointCloud<PointT>::Ptr pc(new pcl::PointCloud<PointT>());
80 if (!getPointClouds(providerName, pc))
81 {
82 ARMARX_WARNING << "Unable to get point cloud data";
83 return;
84 }
85
86 ARMARX_INFO << "Visualizing point cloud from provider '" << providerName << "'";
87
88 {
89 std::unique_lock lock(pointCloudMutex);
90
91 visualizations[providerName].clear();
92
93 DebugDrawerPointCloudPtr dd_pc_plain(new armarx::DebugDrawer24BitColoredPointCloud);
95 pc, *dd_pc_plain, visionx::tools::eConvertAsPoints);
96
97 visualizations[providerName][visionx::ePoints] = dd_pc_plain;
98
99
100 if (pointCloudFormats[providerName]->type == visionx::eColoredPoints ||
101 pointCloudFormats[providerName]->type == visionx::eColoredLabeledPoints)
102 {
103 DebugDrawerPointCloudPtr dd_pc_colors(
104 new armarx::DebugDrawer24BitColoredPointCloud);
106 pc, *dd_pc_colors, visionx::tools::eConvertAsColors);
107 visualizations[providerName][visionx::eColoredPoints] = dd_pc_colors;
108 }
109
110 if (pointCloudFormats[providerName]->type == visionx::eLabeledPoints ||
111 pointCloudFormats[providerName]->type == visionx::eColoredLabeledPoints)
112 {
113 DebugDrawerPointCloudPtr dd_pc_labels(
114 new armarx::DebugDrawer24BitColoredPointCloud);
116 pc, *dd_pc_labels, visionx::tools::eConvertAsLabels);
117 visualizations[providerName][visionx::eLabeledPoints] = dd_pc_labels;
118 }
119 }
120
121 updateVisualization(providerName);
122 }
123
124 void
126 {
127 switch (pointCloudFormats.at(providerName)->type)
128 {
129 case visionx::ePoints:
131 break;
132
133 case visionx::eColoredPoints:
135 break;
136
137 case visionx::eLabeledPoints:
139 break;
140
141 case visionx::eColoredLabeledPoints:
143 break;
144
145 default:
146 ARMARX_WARNING << "Could not visualize point cloud: Unknown format";
147 break;
148 }
149 }
150
151 bool
152 PointCloudVisualization::waitForNewPointCloud(const std::string& providerName)
153 {
154 return waitForPointClouds(providerName, 10000);
155 }
156
157 PointCloudProviderVisualizationInfoList
159 {
160 PointCloudProviderVisualizationInfoList result;
161
162 for (auto& provider : providerNames)
163 {
164 PointCloudProviderVisualizationInfo p;
165 p.name = provider;
166 p.type = pointCloudFormats[provider]->type;
167 result.push_back(p);
168 }
169
170 return result;
171 }
172
173 void
175 PointContentType type,
176 bool enable,
177 const Ice::Current& c)
178 {
179 {
180 std::unique_lock lock(pointCloudMutex);
181
182 enabledFlags[providerName] = enable;
183 visualizationTypes[providerName] = type;
184
185 if (enable && (visualizations.find(providerName) == visualizations.end() ||
186 visualizations.at(providerName).find(type) ==
187 visualizations.at(providerName).end()))
188 {
190 << "Enabling visualization for " << providerName << " with type "
191 << pointCloudTypeToText(type)
192 << ", which has not been created yet (no point cloud reported so far?)";
193 }
194 }
195
196 updateVisualization(providerName);
197 }
198
199 PointCloudVisualizationInfo
201 const Ice::Current& c)
202 {
203 return PointCloudVisualizationInfo{visualizationTypes[providerName],
204 enabledFlags[providerName]};
205 }
206
207 void
209 {
210 offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
211
213 armarx::Split(getProperty<std::string>("PointCloudProviders").getValue(), ",", true);
214
215 for (auto& providerName : providerNames)
216 {
217 usingPointCloudProvider(providerName);
218 }
219
220 std::vector<std::string> initiallyDisabled = armarx::Split(
221 getProperty<std::string>("InitiallyDisabledVisualizations").getValue(), ",", true);
222 for (auto& providerName : providerNames)
223 {
224 enabledFlags[providerName] =
225 (std::find(initiallyDisabled.begin(), initiallyDisabled.end(), providerName) ==
226 initiallyDisabled.end());
227 }
228 }
229
230 void
232 {
234 getProperty<std::string>("DebugDrawerTopicName").getValue());
236 {
237 ARMARX_ERROR << "Failed to obtain debug drawer proxy";
238 return;
239 }
240
241 pointCloudFormats.clear();
243 providerHandlers.clear();
244 visualizationTypes.clear();
245 visualizations.clear();
246
247 for (auto& providerName : providerNames)
248 {
249 pointCloudFormats[providerName] = getPointCloudFormat(providerName);
250 debugDrawerVisualizationIteration[providerName] = 0;
252 new PointCloudVisualizationHandler(this, providerName));
253 }
254
256 }
257
258 void
260 {
261 for (auto& handler : providerHandlers)
262 {
263 handler.second->stop();
264 }
265
266 providerHandlers.clear();
267 }
268
269 void
273
274 void
276 {
277 // Empty, as we need to process point clouds coming from multiple providers
278 }
279
286
287 void
288 PointCloudVisualization::updateVisualization(const std::string& providerName)
289 {
290 std::string oldLayerName, currentLayerName, entityName;
291
292 try
293 {
294 std::unique_lock lock(pointCloudMutex);
295
296 int visualizationIteration = debugDrawerVisualizationIteration.at(providerName);
297 debugDrawerVisualizationIteration[providerName]++;
298
299 oldLayerName = getName() + "_" + providerName + "_" +
300 std::to_string((visualizationIteration + 1) % 2);
301 currentLayerName =
302 getName() + "_" + providerName + "_" + std::to_string(visualizationIteration % 2);
303 entityName = "PointCloud_" + std::to_string(visualizationIteration % 2);
304
305 if (enabledFlags.at(providerName))
306 {
307 auto it = visualizationTypes.find(providerName);
308 PointContentType visualizationType =
309 (it != visualizationTypes.end())
310 ? it->second
312 getProperty<std::string>("DefaultVisualizationType").getValue());
313
314 if (visualizations.at(providerName).find(visualizationType) ==
315 visualizations.at(providerName).end())
316 {
317 visualizationType = visionx::ePoints;
318 }
319
320 visualizationTypes[providerName] = visualizationType;
321
322 ARMARX_INFO << "Visualizing " << providerName << " as "
323 << pointCloudTypeToText(visualizationType);
324 debugDrawerTopicPrx->begin_set24BitColoredPointCloudVisu(
325 currentLayerName,
326 entityName,
327 *visualizations.at(providerName).at(visualizationType));
328 }
329 }
330 catch (const std::exception& e)
331 {
332 ARMARX_WARNING << "Requesting visualization for " << providerName
333 << ", but no point cloud arrived yet";
334 }
335
336 // Wait 200ms to let the point cloud be drawn before we clear the old layer
337 usleep(200000);
338
339 debugDrawerTopicPrx->clearLayer(oldLayerName);
340 }
341
342 void
344 {
345 for (auto& providerName : providerNames)
346 {
347 debugDrawerTopicPrx->clearLayer(getName() + "_" + providerName + "_0");
348 debugDrawerTopicPrx->clearLayer(getName() + "_" + providerName + "_1");
349 }
350 }
351
352 std::string
354 {
355 switch (type)
356 {
357 case visionx::ePoints:
358 return "Plain";
359
360 case visionx::eColoredPoints:
361 return "Colors";
362
363 case visionx::eLabeledPoints:
364 return "Labels";
365
366 default:
367 return "Unknown";
368 }
369 }
370
371 PointContentType
373 {
374 std::transform(text.begin(), text.end(), text.begin(), ::tolower);
375 if (text == "plain")
376 {
377 return visionx::ePoints;
378 }
379 else if (text == "colors")
380 {
381 return visionx::eColoredPoints;
382 }
383 else if (text == "labels")
384 {
385 return visionx::eLabeledPoints;
386 }
387 return visionx::ePoints;
388 }
389
390std::string
392{
393 return "PointCloudVisualization";
394}
395
396std::string
401
402
404} // namespace visionx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
std::string getName() const
Retrieve name of object.
int getPointClouds(const PointCloudPtrT &pointCloudPtr)
Poll PointClouds from provider.
MetaPointCloudFormatPtr getPointCloudFormat(std::string providerName)
void usingPointCloudProvider(std::string providerName)
Registers a delayed topic subscription and a delayed provider proxy retrieval which will be available...
bool waitForPointClouds(int milliseconds=1000)
Wait for new PointClouds.
armarx::RunningTask< PointCloudVisualizationHandler >::pointer_type task
PointCloudVisualizationHandler(PointCloudVisualization *component, const std::string &providerName)
Brief description of class PointCloudVisualization.
std::map< std::string, visionx::MetaPointCloudFormatPtr > pointCloudFormats
bool waitForNewPointCloud(const std::string &providerName)
std::map< std::string, PointCloudVisualizationCollection > visualizations
std::string pointCloudTypeToText(visionx::PointContentType type)
std::map< std::string, PointCloudVisualizationHandlerPtr > providerHandlers
PointCloudProviderVisualizationInfoList getAvailableProviders(const Ice::Current &c=Ice::emptyCurrent) override
std::vector< std::string > providerNames
std::map< std::string, int > debugDrawerVisualizationIteration
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void enablePointCloudVisualization(const std::string &providerName, visionx::PointContentType type, bool enable, const Ice::Current &c=Ice::emptyCurrent) override
std::map< std::string, bool > enabledFlags
void updateVisualization(const std::string &providerName)
armarx::DebugDrawerInterfacePrx debugDrawerTopicPrx
visionx::PointContentType pointCloudTextToType(std::string text)
std::map< std::string, visionx::PointContentType > visualizationTypes
void processPointCloudProvider(const std::string &providerName)
void processPointCloud(const std::string &providerName)
std::string getDefaultName() const override
Retrieve default name of component.
PointCloudVisualizationInfo getCurrentPointCloudVisualizationInfo(const std::string &providerName, const Ice::Current &c=Ice::emptyCurrent) override
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void convertFromPCLToDebugDrawer(const pcl::PointCloud< pcl::PointXYZRGBA >::Ptr &sourcePtr, armarx::DebugDrawer24BitColoredPointCloud &target, PCLToDebugDrawerConversionMode mode, float pointSize)
ArmarX headers.
std::shared_ptr< armarx::DebugDrawer24BitColoredPointCloud > DebugDrawerPointCloudPtr
std::shared_ptr< PointCloudVisualizationHandler > PointCloudVisualizationHandlerPtr