8 #ifndef CORELIB_INCLUDE_RTABMAP_CORE_IMPL_UTIL3D_SURFACE_HPP_
9 #define CORELIB_INCLUDE_RTABMAP_CORE_IMPL_UTIL3D_SURFACE_HPP_
11 #include <pcl/search/kdtree.h>
12 #include <pcl/conversions.h>
19 template<
typename po
intT>
21 const typename pcl::PointCloud<pointT> & cloud,
22 const std::vector<pcl::Vertices> & polygons,
23 const pcl::PointXYZ & viewPoint)
25 std::vector<pcl::Vertices> output(polygons.size());
26 for(
unsigned int i=0;
i<polygons.size(); ++
i)
28 pcl::Vertices polygon = polygons[
i];
29 Eigen::Vector3f
v1 = cloud.at(polygon.vertices[1]).getVector3fMap() - cloud.at(polygon.vertices[0]).getVector3fMap();
30 Eigen::Vector3f
v2 = cloud.at(polygon.vertices[2]).getVector3fMap() - cloud.at(polygon.vertices[0]).getVector3fMap();
31 Eigen::Vector3f
n = (
v1.cross(
v2)).normalized();
33 Eigen::Vector3f
p = Eigen::Vector3f(viewPoint.x, viewPoint.y, viewPoint.z) - cloud.at(polygon.vertices[1]).getVector3fMap();
39 int tmp = polygon.vertices[0];
40 polygon.vertices[0] = polygon.vertices[2];
41 polygon.vertices[2] = tmp;
49 template<
typename po
intRGBT>
51 pcl::PolygonMeshPtr & mesh,
52 float meshDecimationFactor,
54 const typename pcl::PointCloud<pointRGBT>::Ptr & cloud,
55 float transferColorRadius,
62 bool hasNormals =
false;
63 bool hasColors =
false;
64 for(
unsigned int i=0;
i<mesh->cloud.fields.size(); ++
i)
66 if(mesh->cloud.fields[
i].name.compare(
"normal_x") == 0)
70 else if(mesh->cloud.fields[
i].name.compare(
"rgb") == 0)
76 if(maximumPolygons > 0)
78 double factor = 1.0-double(maximumPolygons)/double(mesh->polygons.size());
79 if(
factor > meshDecimationFactor)
81 meshDecimationFactor =
factor;
84 if(meshDecimationFactor > 0.0)
86 unsigned int count = mesh->polygons.size();
87 if(progressState) progressState->
callback(
uFormat(
"Mesh decimation (factor=%f) from %d polygons...",meshDecimationFactor, (
int)
count));
90 if(progressState) progressState->
callback(
uFormat(
"Mesh decimated (factor=%f) from %d to %d polygons", meshDecimationFactor, (
int)
count, (
int)mesh->polygons.size()));
91 if(count < mesh->polygons.size())
93 if(progressState) progressState->
callback(
uFormat(
"Decimated mesh has more polygons than before!"));
101 transferColorRadius >= 0.0)
103 if(progressState) progressState->
callback(
uFormat(
"Transferring color from point cloud to mesh..."));
106 typename pcl::search::KdTree<pointRGBT>::Ptr
tree (
new pcl::search::KdTree<pointRGBT>(
true));
107 tree->setInputCloud(cloud);
108 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr coloredCloud(
new pcl::PointCloud<pcl::PointXYZRGBNormal>);
109 pcl::fromPCLPointCloud2(mesh->cloud, *coloredCloud);
110 std::vector<bool> coloredPts(coloredCloud->size());
111 for(
unsigned int i=0;
i<coloredCloud->size(); ++
i)
114 std::vector<float> kDistances;
116 pt.x = coloredCloud->at(
i).x;
117 pt.y = coloredCloud->at(
i).y;
118 pt.z = coloredCloud->at(
i).z;
119 if(transferColorRadius > 0.0)
121 tree->radiusSearch(pt, transferColorRadius,
kIndices, kDistances);
141 coloredCloud->at(
i).r = r/
kIndices.size();
145 coloredPts.at(
i) =
true;
150 coloredCloud->at(
i).r = coloredCloud->at(
i).g = coloredCloud->at(
i).b = 255;
151 coloredPts.at(
i) =
false;
155 pcl::toPCLPointCloud2(*coloredCloud, mesh->cloud);
162 std::vector<pcl::Vertices> filteredPolygons(mesh->polygons.size());
164 for(
unsigned int i=0;
i<mesh->polygons.size(); ++
i)
166 bool coloredPolygon =
true;
167 for(
unsigned int j=0;
j<mesh->polygons[
i].vertices.size(); ++
j)
169 if(!coloredPts.at(mesh->polygons[
i].vertices[
j]))
171 coloredPolygon =
false;
177 filteredPolygons[oi++] = mesh->polygons[
i];
180 filteredPolygons.resize(oi);
181 mesh->polygons = filteredPolygons;
187 if(progressState) progressState->
callback(
uFormat(
"Filter small polygon clusters..."));
190 std::vector<std::set<int> > neighbors;
191 std::vector<std::set<int> > vertexToPolygons;
193 mesh->cloud.height*mesh->cloud.width,
198 minClusterSize<0?0:minClusterSize);
200 std::vector<pcl::Vertices> filteredPolygons(mesh->polygons.size());
201 if(minClusterSize < 0)
204 std::list<std::list<int> >
::iterator biggestClusterIndex = clusters.end();
205 unsigned int biggestClusterSize = 0;
208 if(
iter->size() > biggestClusterSize)
210 biggestClusterIndex =
iter;
211 biggestClusterSize =
iter->size();
214 if(biggestClusterIndex != clusters.end())
217 for(std::list<int>::iterator jter=biggestClusterIndex->begin(); jter!=biggestClusterIndex->end(); ++jter)
219 filteredPolygons[oi++] = mesh->polygons.at(*jter);
221 filteredPolygons.resize(oi);
229 for(std::list<int>::iterator jter=
iter->begin(); jter!=
iter->end(); ++jter)
231 filteredPolygons[oi++] = mesh->polygons.at(*jter);
234 filteredPolygons.resize(oi);
237 int before = (
int)mesh->polygons.size();
238 mesh->polygons = filteredPolygons;
240 if(progressState) progressState->
callback(
uFormat(
"Filtered %d polygons.", before-(
int)mesh->polygons.size()));
249 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud (
new pcl::PointCloud<pcl::PointXYZRGBNormal>);
250 pcl::fromPCLPointCloud2(mesh->cloud, *cloud);
252 Eigen::Vector3f
normal(1,0,0);
253 for(
unsigned int i=0;
i<mesh->polygons.size(); ++
i)
255 pcl::Vertices &
v = mesh->polygons[
i];
260 cloud->at(
v.vertices[1]).x - cloud->at(
v.vertices[0]).x,
261 cloud->at(
v.vertices[1]).y - cloud->at(
v.vertices[0]).y,
262 cloud->at(
v.vertices[1]).z - cloud->at(
v.vertices[0]).z);
263 int last =
v.vertices.size()-1;
265 cloud->at(
v.vertices[
last]).x - cloud->at(
v.vertices[0]).x,
266 cloud->at(
v.vertices[
last]).y - cloud->at(
v.vertices[0]).y,
267 cloud->at(
v.vertices[
last]).z - cloud->at(
v.vertices[0]).z);
272 for(
unsigned int j=0;
j<
v.vertices.size(); ++
j)
274 cloud->at(
v.vertices[
j]).normal_x =
normal[0];
275 cloud->at(
v.vertices[
j]).normal_y =
normal[1];
276 cloud->at(
v.vertices[
j]).normal_z =
normal[2];
279 pcl::toPCLPointCloud2 (*cloud, mesh->cloud);
283 pcl::PointCloud<pcl::PointNormal>::Ptr cloud (
new pcl::PointCloud<pcl::PointNormal>);
284 pcl::fromPCLPointCloud2(mesh->cloud, *cloud);
286 for(
unsigned int i=0;
i<mesh->polygons.size(); ++
i)
288 pcl::Vertices &
v = mesh->polygons[
i];
291 cloud->at(
v.vertices[1]).x - cloud->at(
v.vertices[0]).x,
292 cloud->at(
v.vertices[1]).y - cloud->at(
v.vertices[0]).y,
293 cloud->at(
v.vertices[1]).z - cloud->at(
v.vertices[0]).z);
294 int last =
v.vertices.size()-1;
296 cloud->at(
v.vertices[
last]).x - cloud->at(
v.vertices[0]).x,
297 cloud->at(
v.vertices[
last]).y - cloud->at(
v.vertices[0]).y,
298 cloud->at(
v.vertices[
last]).z - cloud->at(
v.vertices[0]).z);
299 Eigen::Vector3f
normal = v0.cross(
v1);
302 for(
unsigned int j=0;
j<
v.vertices.size(); ++
j)
304 cloud->at(
v.vertices[
j]).normal_x =
normal[0];
305 cloud->at(
v.vertices[
j]).normal_y =
normal[1];
306 cloud->at(
v.vertices[
j]).normal_z =
normal[2];
309 pcl::toPCLPointCloud2 (*cloud, mesh->cloud);
314 template<
typename Po
intT>
316 const Eigen::Vector3f & origin,
317 const Eigen::Vector3f & dir,
318 const typename pcl::PointCloud<PointT> & cloud,
319 const std::vector<pcl::Vertices> & polygons,
320 bool ignoreBackFaces,
322 Eigen::Vector3f & normal,
325 bool intersect =
false;
327 for (
size_t i = 0;
i < polygons.size(); ++
i)
329 const pcl::Vertices & vert = polygons.at(
i);
330 UASSERT(vert.vertices.size()==3);
334 cloud.at(vert.vertices.at(0)).getVector3fMap(),
335 cloud.at(vert.vertices.at(1)).getVector3fMap(),
336 cloud.at(vert.vertices.at(2)).getVector3fMap(),
d,
n) &&
338 (!ignoreBackFaces ||
n.dot(dir)<0))