PointCloudUtility.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package VisionX::ArmarXObjects::PointCloudUtility
19 * @author Tim Niklas Freudenstein ( uidnv at student dot kit dot edu )
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#include "PointCloudUtility.h"
26
28
29
30using namespace armarx;
31
32void
33PointCloudUtility::fusePointclouds(std::string file1, std::string file2, std::string out)
34{
35 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pc1 = loadPointCloud(file1);
36 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pc2 = loadPointCloud(file2);
37 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pc1Aligned =
38 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr(new pcl::PointCloud<pcl::PointXYZRGBA>);
39 pcl::ApproximateVoxelGrid<pcl::PointXYZRGBA> filter;
40
41 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr temp1 =
42 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr(new pcl::PointCloud<pcl::PointXYZRGBA>);
43 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr temp2 =
44 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr(new pcl::PointCloud<pcl::PointXYZRGBA>);
45
46 // filter.setLeafSize(0.01, 0.01, 0.01);
47 // filter.setInputCloud(pc1);
48 // filter.filter(*temp1);
49 // filter.setInputCloud(pc2);
50 // filter.filter(*temp2);
51
52
53 pcl::IterativeClosestPoint<pcl::PointXYZRGBA, pcl::PointXYZRGBA> aligner;
54 aligner.setEuclideanFitnessEpsilon(1e-20);
55 aligner.setTransformationEpsilon(1e-20);
56 aligner.setMaximumIterations(10000000);
57 aligner.setUseReciprocalCorrespondences(true);
58 aligner.setInputSource(pc1);
59 aligner.setInputTarget(pc2);
60 aligner.align(*pc1Aligned);
61 pcl::PointCloud<pcl::PointXYZRGBA> pcout = *pc2 + *pc1Aligned;
62 ARMARX_VERBOSE << aligner.getFitnessScore();
63 pcl::io::savePCDFileASCII(out, pcout);
64}
65
66void
67PointCloudUtility::moveOrigin(std::string file, std::string out, Eigen::Vector3f translation)
68{
69 Eigen::Matrix4f transform = Eigen::Matrix4f::Identity();
70 transform.block<3, 1>(0, 3) = translation;
71 this->transformPointcloud(file, out, transform);
72}
73
74void
75PointCloudUtility::rotatePointCloud(std::string file, std::string out, Eigen::Matrix3f rotation)
76{
77 Eigen::Matrix4f transform = Eigen::Matrix4f::Identity();
78 transform.block<3, 3>(0, 0) = rotation;
79 this->transformPointcloud(file, out, transform);
80}
81
82void
83PointCloudUtility::transformPointcloud(std::string file, std::string out, Eigen::Matrix4f pose)
84{
85 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pc = loadPointCloud(file);
86 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr transformed_cloud(
87 new pcl::PointCloud<pcl::PointXYZRGBA>());
88 pcl::transformPointCloud<pcl::PointXYZRGBA>(*pc, *transformed_cloud, pose);
89
90 pcl::io::savePCDFileASCII(out, *transformed_cloud);
91}
92
93void
94PointCloudUtility::showResult(std::string file)
95{
96 pcl::visualization::PCLVisualizer viewer;
97 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pc = loadPointCloud(file);
98 viewer.addPointCloud(pc);
99 viewer.addCoordinateSystem(1.0);
100 viewer.spin();
101 while (!viewer.wasStopped())
102 {
103 }
104}
105
106void
107PointCloudUtility::process()
108{
109 std::string file1 = getProperty<std::string>("PointCloud1").getValue();
110 std::string file2 = getProperty<std::string>("PointCloud2").getValue();
111 std::string out = getProperty<std::string>("ResultFileName").getValue();
112 bool merge = getProperty<bool>("Merge").getValue();
113 bool transform = getProperty<bool>("Transform").getValue();
114 bool view = getProperty<bool>("Show").getValue();
115 std::string temp = "temp.pcd";
116 ARMARX_VERBOSE << "Starting";
117 if (merge)
118 {
119
120 fusePointclouds(file1, file2, temp);
121 ARMARX_VERBOSE << "Merged";
122 }
123 else
124 {
125 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pc = loadPointCloud(file1);
126 pcl::io::savePCDFileASCII(temp, *pc);
127 }
128 if (transform)
129 {
130 Eigen::Vector3f tVector;
131 tVector << getProperty<float>("X").getValue(), getProperty<float>("Y").getValue(),
132 getProperty<float>("Z").getValue();
133 Eigen::Vector3f rot;
134 rot << getProperty<float>("Roll").getValue(), getProperty<float>("Pitch").getValue(),
135 getProperty<float>("Yaw").getValue();
136 Eigen::Matrix4f transpose;
137 VirtualRobot::MathTools::posrpy2eigen4f(tVector, rot, transpose);
138 transformPointcloud(temp, out, transpose);
139 ARMARX_VERBOSE << "Transformed.";
140 }
141 else
142 {
143 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr pc = loadPointCloud(temp);
144 pcl::io::savePCDFileASCII(out, *pc);
145 }
146 if (view)
147 {
148 showResult(out);
149 }
150 //auto manager = getArmarXManager();
151 //auto name = getName();
152 //std::thread {[manager, name]{manager->removeObjectBlocking(name);}}.detach();
153}
154
155void
157{
158 processorTask = new RunningTask<PointCloudUtility>(this, &PointCloudUtility::process);
159}
160
161/**
162 * @brief PointCloudHandLocalizer::loadPointCloud loads a point cloud file from
163 * the ArmarXDataPath. Returns nullptr if failed.
164 * @param filename the name of the pointcloud file
165 * @return a pointer to said pointcload
166 */
167pcl::PointCloud<pcl::PointXYZRGBA>::Ptr
168PointCloudUtility::loadPointCloud(std::string filename)
169{
170
171 pcl::PointCloud<pcl::PointXYZRGBA>::Ptr model(new pcl::PointCloud<pcl::PointXYZRGBA>());
172 if (!ArmarXDataPath::getAbsolutePath(filename, filename))
173 {
174 ARMARX_ERROR << "Could not find point cloud file in ArmarXDataPath: " << filename;
175 return nullptr;
176 }
177 else if (pcl::io::loadPCDFile<pcl::PointXYZRGBA>(filename.c_str(), *model) == -1)
178 {
179 ARMARX_WARNING << "unable to load point cloud from file " << filename;
180 return nullptr;
181 }
182 else
183 {
184 return model;
185 }
186}
187
188void
192
193void
197
198void
202
209
210std::string
212{
213 return "PointCloudUtility";
214}
215
216std::string
218{
219 return GetDefaultName();
220}
221
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
Brief description of class PointCloudUtility.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
static std::string GetDefaultName()
std::string getDefaultName() const override
Retrieve default name of component.
#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
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
SceneBounds merge(const std::vector< SceneBounds > &sceneBounds)
Definition util.cpp:183
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::vector< T > > transpose(const std::vector< std::vector< T > > &src, Thrower thrower)
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:351
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.