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
25#include <thread>
26
28
29namespace visionx
30{
40
45
46 void
48 {
49 ARMARX_INFO << "Starting PointCloudProvider handler for '" << providerName << "'";
50
51 while (!task->isStopped())
52 {
53 if (!component->waitForNewPointCloud(providerName))
54 {
55 continue;
56 }
57
58 ARMARX_INFO << "Receiving point cloud from provider '" << providerName << "'";
59 component->processPointCloudProvider(providerName);
60
61 std::this_thread::sleep_for(std::chrono::milliseconds(1));
62 }
63
64 ARMARX_INFO << "Stopping PointCloudProvider handler for '" << providerName << "'";
65 }
66
67 void
72
73 template <typename PointT>
74 void
75 PointCloudVisualization::processPointCloud(const std::string& providerName)
76 {
77 typename pcl::PointCloud<PointT>::Ptr pc(new pcl::PointCloud<PointT>());
78 if (!getPointClouds(providerName, pc))
79 {
80 ARMARX_WARNING << "Unable to get point cloud data";
81 return;
82 }
83
84 ARMARX_INFO << "Visualizing point cloud from provider '" << providerName << "'";
85
86 {
87 std::unique_lock lock(pointCloudMutex);
88
89 visualizations[providerName].clear();
90
91 DebugDrawerPointCloudPtr dd_pc_plain(new armarx::DebugDrawer24BitColoredPointCloud);
93 pc, *dd_pc_plain, visionx::tools::eConvertAsPoints);
94
95 visualizations[providerName][visionx::ePoints] = dd_pc_plain;
96
97
98 if (pointCloudFormats[providerName]->type == visionx::eColoredPoints ||
99 pointCloudFormats[providerName]->type == visionx::eColoredLabeledPoints)
100 {
101 DebugDrawerPointCloudPtr dd_pc_colors(
102 new armarx::DebugDrawer24BitColoredPointCloud);
104 pc, *dd_pc_colors, visionx::tools::eConvertAsColors);
105 visualizations[providerName][visionx::eColoredPoints] = dd_pc_colors;
106 }
107
108 if (pointCloudFormats[providerName]->type == visionx::eLabeledPoints ||
109 pointCloudFormats[providerName]->type == visionx::eColoredLabeledPoints)
110 {
111 DebugDrawerPointCloudPtr dd_pc_labels(
112 new armarx::DebugDrawer24BitColoredPointCloud);
114 pc, *dd_pc_labels, visionx::tools::eConvertAsLabels);
115 visualizations[providerName][visionx::eLabeledPoints] = dd_pc_labels;
116 }
117 }
118
119 updateVisualization(providerName);
120 }
121
122 void
124 {
125 switch (pointCloudFormats.at(providerName)->type)
126 {
127 case visionx::ePoints:
129 break;
130
131 case visionx::eColoredPoints:
133 break;
134
135 case visionx::eLabeledPoints:
137 break;
138
139 case visionx::eColoredLabeledPoints:
141 break;
142
143 default:
144 ARMARX_WARNING << "Could not visualize point cloud: Unknown format";
145 break;
146 }
147 }
148
149 bool
150 PointCloudVisualization::waitForNewPointCloud(const std::string& providerName)
151 {
152 return waitForPointClouds(providerName, 10000);
153 }
154
155 PointCloudProviderVisualizationInfoList
157 {
158 PointCloudProviderVisualizationInfoList result;
159
160 for (auto& provider : providerNames)
161 {
162 PointCloudProviderVisualizationInfo p;
163 p.name = provider;
164 p.type = pointCloudFormats[provider]->type;
165 result.push_back(p);
166 }
167
168 return result;
169 }
170
171 void
173 PointContentType type,
174 bool enable,
175 const Ice::Current& c)
176 {
177 {
178 std::unique_lock lock(pointCloudMutex);
179
180 enabledFlags[providerName] = enable;
181 visualizationTypes[providerName] = type;
182
183 if (enable && (visualizations.find(providerName) == visualizations.end() ||
184 visualizations.at(providerName).find(type) ==
185 visualizations.at(providerName).end()))
186 {
188 << "Enabling visualization for " << providerName << " with type "
189 << pointCloudTypeToText(type)
190 << ", which has not been created yet (no point cloud reported so far?)";
191 }
192 }
193
194 updateVisualization(providerName);
195 }
196
197 PointCloudVisualizationInfo
199 const Ice::Current& c)
200 {
201 return PointCloudVisualizationInfo{visualizationTypes[providerName],
202 enabledFlags[providerName]};
203 }
204
205 void
207 {
208 offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
209
211 armarx::Split(getProperty<std::string>("PointCloudProviders").getValue(), ",", true);
212
213 for (auto& providerName : providerNames)
214 {
215 usingPointCloudProvider(providerName);
216 }
217
218 std::vector<std::string> initiallyDisabled = armarx::Split(
219 getProperty<std::string>("InitiallyDisabledVisualizations").getValue(), ",", true);
220 for (auto& providerName : providerNames)
221 {
222 enabledFlags[providerName] =
223 (std::find(initiallyDisabled.begin(), initiallyDisabled.end(), providerName) ==
224 initiallyDisabled.end());
225 }
226 }
227
228 void
230 {
232 getProperty<std::string>("DebugDrawerTopicName").getValue());
234 {
235 ARMARX_ERROR << "Failed to obtain debug drawer proxy";
236 return;
237 }
238
239 pointCloudFormats.clear();
241 providerHandlers.clear();
242 visualizationTypes.clear();
243 visualizations.clear();
244
245 for (auto& providerName : providerNames)
246 {
247 pointCloudFormats[providerName] = getPointCloudFormat(providerName);
248 debugDrawerVisualizationIteration[providerName] = 0;
250 new PointCloudVisualizationHandler(this, providerName));
251 }
252
254 }
255
256 void
258 {
259 for (auto& handler : providerHandlers)
260 {
261 handler.second->stop();
262 }
263
264 providerHandlers.clear();
265 }
266
267 void
271
272 void
274 {
275 // Empty, as we need to process point clouds coming from multiple providers
276 }
277
284
285 void
286 PointCloudVisualization::updateVisualization(const std::string& providerName)
287 {
288 std::string oldLayerName, currentLayerName, entityName;
289
290 try
291 {
292 std::unique_lock lock(pointCloudMutex);
293
294 int visualizationIteration = debugDrawerVisualizationIteration.at(providerName);
295 debugDrawerVisualizationIteration[providerName]++;
296
297 oldLayerName = getName() + "_" + providerName + "_" +
298 std::to_string((visualizationIteration + 1) % 2);
299 currentLayerName =
300 getName() + "_" + providerName + "_" + std::to_string(visualizationIteration % 2);
301 entityName = "PointCloud_" + std::to_string(visualizationIteration % 2);
302
303 if (enabledFlags.at(providerName))
304 {
305 auto it = visualizationTypes.find(providerName);
306 PointContentType visualizationType =
307 (it != visualizationTypes.end())
308 ? it->second
310 getProperty<std::string>("DefaultVisualizationType").getValue());
311
312 if (visualizations.at(providerName).find(visualizationType) ==
313 visualizations.at(providerName).end())
314 {
315 visualizationType = visionx::ePoints;
316 }
317
318 visualizationTypes[providerName] = visualizationType;
319
320 ARMARX_INFO << "Visualizing " << providerName << " as "
321 << pointCloudTypeToText(visualizationType);
322 debugDrawerTopicPrx->begin_set24BitColoredPointCloudVisu(
323 currentLayerName,
324 entityName,
325 *visualizations.at(providerName).at(visualizationType));
326 }
327 }
328 catch (const std::exception& e)
329 {
330 ARMARX_WARNING << "Requesting visualization for " << providerName
331 << ", but no point cloud arrived yet";
332 }
333
334 // Wait 200ms to let the point cloud be drawn before we clear the old layer
335 usleep(200000);
336
337 debugDrawerTopicPrx->clearLayer(oldLayerName);
338 }
339
340 void
342 {
343 for (auto& providerName : providerNames)
344 {
345 debugDrawerTopicPrx->clearLayer(getName() + "_" + providerName + "_0");
346 debugDrawerTopicPrx->clearLayer(getName() + "_" + providerName + "_1");
347 }
348 }
349
350 std::string
352 {
353 switch (type)
354 {
355 case visionx::ePoints:
356 return "Plain";
357
358 case visionx::eColoredPoints:
359 return "Colors";
360
361 case visionx::eLabeledPoints:
362 return "Labels";
363
364 default:
365 return "Unknown";
366 }
367 }
368
369 PointContentType
371 {
372 std::transform(text.begin(), text.end(), text.begin(), ::tolower);
373 if (text == "plain")
374 {
375 return visionx::ePoints;
376 }
377 else if (text == "colors")
378 {
379 return visionx::eColoredPoints;
380 }
381 else if (text == "labels")
382 {
383 return visionx::eLabeledPoints;
384 }
385 return visionx::ePoints;
386 }
387} // namespace visionx
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)
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