MergedLabeledPointCloud.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 MA_RainerKartmann::ArmarXObjects::PointCloudMerger
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2019
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <pcl/common/io.h>
26 
28 
30 
31 #include "segments.h"
32 
33 
34 namespace visionx
35 {
36 
38 
39 
41  const std::string& name,
42  pcl::PointCloud<PointL>::ConstPtr inputCloud,
43  bool isLabeled)
44  {
45  // Todo: replace with real exception.
46  ARMARX_CHECK(inputCloud) << "Passed input cloud must not be null (name: " << name << ").";
47 
48  if (isLabeled)
49  {
50  inputLabeledClouds[name] = inputCloud;
51  changed = true;
52  }
53  else
54  {
55  pcl::PointCloud<PointT>::Ptr unlabeled(new pcl::PointCloud<PointT>);
56  pcl::copyPointCloud(*inputCloud, *unlabeled);
57  setInputPointCloud(name, unlabeled);
58  }
59  }
60 
62  const std::string& name,
63  pcl::PointCloud<PointL>::ConstPtr inputCloud,
64  visionx::PointContentType originalType)
65  {
66  setInputPointCloud(name, inputCloud, visionx::tools::isLabeled(originalType));
67  }
68 
70  const std::string& name, pcl::PointCloud<PointT>::ConstPtr inputCloud)
71  {
72  // Todo: replace with real exception.
73  ARMARX_CHECK(inputCloud) << "Passed input cloud must not be null (name: " << name << ").";
74 
75  inputUnlabeledClouds[name] = inputCloud;
76  changed = true;
77  // Remove potential converted clouds.
78  inputLabeledClouds.erase(name);
79  }
80 
81 
82  auto MergedLabeledPointCloud::getResultPointCloud() -> pcl::PointCloud<PointL>::Ptr
83  {
84  // Rebuild result cloud if necessary.
85  if (changed)
86  {
87  // Relabel unlabeled point clouds if necessary.
88  if (!inputUnlabeledClouds.empty())
89  {
90  // Get used labels and label unlabeled clouds.
91  labelUnlabeledClouds(getUsedLabels());
92  }
93 
94  mergeLabeledClouds();
95  changed = false;
96  }
97 
98  return resultCloud;
99  }
100 
101 
102  auto MergedLabeledPointCloud::getUsedLabels() const -> std::set<Label>
103  {
104  // Get used labels.
105  std::set<Label> usedLabels;
106  for (const auto& [name, cloud] : inputLabeledClouds)
107  {
108  for (const auto& point : cloud->points)
109  {
110  usedLabels.insert(point.label);
111  }
112  }
113  return usedLabels;
114  }
115 
116 
117  void MergedLabeledPointCloud::labelUnlabeledClouds(const std::set<Label>& usedLabels)
118  {
119  // Start assigning new labels.
120  Label label = 1;
121  for (const auto& [name, unlabeled] : inputUnlabeledClouds)
122  {
123  // Find unused label.
124  while (usedLabels.count(label) > 0)
125  {
126  ++label;
127  }
128 
129  // Construct labelled cloud.
130  pcl::PointCloud<PointL>::Ptr labeled(new pcl::PointCloud<PointL>);
131  pcl::copyPointCloud(*unlabeled, *labeled);
132  for (auto& point : labeled->points)
133  {
134  point.label = label;
135  }
136 
137  // Store labeled cloud (overwriting old relabeled clouds with the same name).
138  inputLabeledClouds[name] = labeled;
139  }
140  // Remove old unlabeled clouds.
141  inputUnlabeledClouds.clear();
142  }
143 
144 
145  void MergedLabeledPointCloud::mergeLabeledClouds()
146  {
147  // Build result point cloud.
148  resultCloud->clear();
149  uint64_t timestamp = 0; // Use highest timestamp for result cloud.
150  for (const auto& [name, cloud] : inputLabeledClouds)
151  {
152  ARMARX_CHECK(cloud);
153  if (cloud)
154  {
155  (*resultCloud) += *cloud;
156  }
157  timestamp = std::max(timestamp, cloud->header.stamp);
158  }
159  resultCloud->header.stamp = timestamp;
160  }
161 
162 }
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::RemoteGui::Client::Label
Definition: Widgets.h:31
visionx::tools::isLabeled
bool isLabeled(PointContentType type)
Definition: PointCloudConversions.cpp:108
visionx::MergedLabeledPointCloud::Label
uint32_t Label
Definition: MergedLabeledPointCloud.h:49
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
MergedLabeledPointCloud.h
segments.h
visionx::MergedLabeledPointCloud::MergedLabeledPointCloud
MergedLabeledPointCloud()
Constructor.
visionx::MergedLabeledPointCloud::setInputPointCloud
void setInputPointCloud(const std::string &name, pcl::PointCloud< PointL >::ConstPtr inputCloud, bool isLabeled=true)
Set a (new) labeled or unlabeled input point cloud.
Definition: MergedLabeledPointCloud.cpp:40
max
T max(T t1, T t2)
Definition: gdiam.h:48
ExpressionException.h
PointCloudConversions.h
std
Definition: Application.h:66
set
set(LIBS ArmarXCoreInterfaces ${CMAKE_THREAD_LIBS_INIT} ${dl_LIBRARIES} ${rt_LIBRARIES} ${QT_LIBRARIES} ${Boost_LIBRARIES} BoostAssertionHandler ArmarXCPPUtility SimoxUtility) set(LIB_FILES ArmarXManager.cpp ArmarXMultipleObjectsScheduler.cpp ArmarXObjectScheduler.cpp ManagedIceObject.cpp ManagedIceObjectPlugin.cpp Component.cpp ComponentPlugin.cpp IceGridAdmin.cpp ArmarXObjectObserver.cpp IceManager.cpp PackagePath.cpp RemoteReferenceCount.cpp logging/LoggingUtil.cpp logging/Logging.cpp logging/LogSender.cpp logging/ArmarXLogBuf.cpp system/ArmarXDataPath.cpp system/DynamicLibrary.cpp system/ProcessWatcher.cpp system/FactoryCollectionBase.cpp system/cmake/CMakePackageFinder.cpp system/cmake/CMakePackageFinderCache.cpp system/cmake/ArmarXPackageToolInterface.cpp system/RemoteObjectNode.cpp services/sharedmemory/HardwareId.cpp services/tasks/RunningTask.cpp services/tasks/ThreadList.cpp services/tasks/ThreadPool.cpp services/profiler/Profiler.cpp services/profiler/FileLoggingStrategy.cpp services/profiler/IceLoggingStrategy.cpp application/Application.cpp application/ApplicationOptions.cpp application/ApplicationProcessFacet.cpp application/ApplicationNetworkStats.cpp application/properties/PropertyUser.cpp application/properties/Property.cpp application/properties/PropertyDefinition.cpp application/properties/PropertyDefinitionContainer.cpp application/properties/PropertyDefinitionHelpFormatter.cpp application/properties/PropertyDefinitionConfigFormatter.cpp application/properties/PropertyDefinitionBriefHelpFormatter.cpp application/properties/PropertyDefinitionXmlFormatter.cpp application/properties/PropertyDefinitionDoxygenFormatter.cpp application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.cpp application/properties/PropertyDefinitionContainerBriefHelpFormatter.cpp application/properties/IceProperties.cpp exceptions/Exception.cpp exceptions/local/UnexpectedEnumValueException.cpp util/FileSystemPathBuilder.cpp util/StringHelpers.cpp util/IceReportSkipper.cpp util/Throttler.cpp util/distributed/AMDCallbackCollection.cpp util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.cpp util/distributed/RemoteHandle/RemoteHandle.cpp util/distributed/RemoteHandle/RemoteHandleControlBlock.cpp time/ice_conversions.cpp time/json_conversions.cpp time/CallbackWaitLock.cpp time/Clock.cpp time/ClockType.cpp time/ClockTypeNames.cpp time/CycleUtil.cpp time/DateTime.cpp time/Duration.cpp time/Frequency.cpp time/LocalTimeServer.cpp time/Metronome.cpp time/ScopedStopWatch.cpp time/StopWatch.cpp time/Timer.cpp time/TimeKeeper.cpp time/TimeUtil.cpp csv/CsvWriter.cpp csv/CsvReader.cpp eigen/conversions.cpp eigen/ice_conversions.cpp) set(LIB_HEADERS ArmarXManager.h ArmarXDummyManager.h ArmarXMultipleObjectsScheduler.h ArmarXObjectObserver.h ArmarXObjectScheduler.h ArmarXFwd.h Component.h ComponentPlugin.h ComponentFactories.h CoreObjectFactories.h IceGridAdmin.h IceManager.h IceManagerImpl.h json_conversions.h ManagedIceObject.h ManagedIceObjectPlugin.h ManagedIceObjectImpl.h ManagedIceObjectDependency.h ManagedIceObjectRegistryInterface.h PackagePath.h RemoteReferenceCount.h system/ImportExport.h system/ImportExportComponent.h system/AbstractFactoryMethod.h system/FactoryCollectionBase.h system/Synchronization.h system/ArmarXDataPath.h system/DynamicLibrary.h system/ProcessWatcher.h system/ConditionSynchronization.h system/cmake/CMakePackageFinder.h system/cmake/CMakePackageFinderCache.h system/cmake/FindPackageX.cmake system/cmake/ArmarXPackageToolInterface.h system/RemoteObjectNode.h logging/LoggingUtil.h logging/LogSender.h logging/Logging.h logging/ArmarXLogBuf.h logging/SpamFilterData.h services/tasks/RunningTask.h services/tasks/PeriodicTask.h services/tasks/ThreadList.h services/tasks/TaskUtil.h services/tasks/ThreadPool.h services/sharedmemory/SharedMemoryProvider.h services/sharedmemory/SharedMemoryConsumer.h services/sharedmemory/IceSharedMemoryProvider.h services/sharedmemory/IceSharedMemoryConsumer.h services/sharedmemory/HardwareIdentifierProvider.h services/sharedmemory/HardwareId.h services/sharedmemory/exceptions/SharedMemoryExceptions.h services/profiler/Profiler.h services/profiler/LoggingStrategy.h services/profiler/FileLoggingStrategy.h services/profiler/IceLoggingStrategy.h application/Application.h application/ApplicationOptions.h application/ApplicationProcessFacet.h application/ApplicationNetworkStats.h application/properties/forward_declarations.h application/properties/Properties.h application/properties/Property.h application/properties/PluginEigen.h application/properties/PluginEnumNames.h application/properties/PluginCfgStruct.h application/properties/PluginAll.h application/properties/PropertyUser.h application/properties/PropertyDefinition.h application/properties/PropertyDefinition.hpp application/properties/PropertyDefinitionInterface.h application/properties/PropertyDefinitionContainer.h application/properties/PropertyDefinitionFormatter.h application/properties/PropertyDefinitionContainerFormatter.h application/properties/PropertyDefinitionConfigFormatter.h application/properties/PropertyDefinitionHelpFormatter.h application/properties/PropertyDefinitionBriefHelpFormatter.h application/properties/PropertyDefinitionXmlFormatter.h application/properties/PropertyDefinitionDoxygenFormatter.h application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.h application/properties/PropertyDefinitionContainerBriefHelpFormatter.h application/properties/ProxyPropertyDefinition.h application/properties/IceProperties.h exceptions/Exception.h exceptions/LocalException.h exceptions/local/DynamicLibraryException.h exceptions/local/ExpressionException.h exceptions/local/FileIOException.h exceptions/local/InvalidPropertyValueException.h exceptions/local/MissingRequiredPropertyException.h exceptions/local/PropertyInheritanceCycleException.h exceptions/local/ProxyNotInitializedException.h exceptions/local/UnexpectedEnumValueException.h exceptions/local/UnmappedValueException.h exceptions/local/ValueRangeExceededException.h exceptions/user/NotImplementedYetException.h rapidxml/rapidxml.hpp rapidxml/rapidxml_print.hpp rapidxml/rapidxml_iterators.hpp rapidxml/rapidxml_utils.hpp rapidxml/wrapper/RapidXmlReader.h rapidxml/wrapper/RapidXmlWriter.h rapidxml/wrapper/DefaultRapidXmlReader.h rapidxml/wrapper/MultiNodeRapidXMLReader.h util/IceBlobToObject.h util/ObjectToIceBlob.h util/FileSystemPathBuilder.h util/FiniteStateMachine.h util/StringHelpers.h util/StringHelperTemplates.h util/algorithm.h util/OnScopeExit.h util/Predicates.h util/Preprocessor.h util/PropagateConst.h util/Registrar.h util/TemplateMetaProgramming.h util/TripleBuffer.h util/IceReportSkipper.h util/Throttler.h util/distributed/AMDCallbackCollection.h util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.h util/distributed/RemoteHandle/RemoteHandle.h util/distributed/RemoteHandle/RemoteHandleControlBlock.h util/SimpleStatemachine.h time.h time_minimal.h time/forward_declarations.h time/ice_conversions.h time/json_conversions.h time/CallbackWaitLock.h time/Clock.h time/ClockType.h time/ClockTypeNames.h time/CycleUtil.h time/DateTime.h time/Duration.h time/Frequency.h time/LocalTimeServer.h time/Metronome.h time/ScopedStopWatch.h time/StopWatch.h time/Timer.h time/TimeUtil.h time/TimeKeeper.h csv/CsvWriter.h csv/CsvReader.h eigen/conversions.h eigen/ice_conversions.h ice_conversions.h ice_conversions/ice_conversions_boost_templates.h ice_conversions/ice_conversions_templates.h ice_conversions/ice_conversions_templates.tpp $
Definition: CMakeLists.txt:12
visionx::MergedLabeledPointCloud::getResultPointCloud
pcl::PointCloud< PointL >::Ptr getResultPointCloud()
Get the result point cloud.
Definition: MergedLabeledPointCloud.cpp:82