28#ifndef UTIL3D_MAPPING_HPP_
29#define UTIL3D_MAPPING_HPP_
31#include <rtabmap/core/util3d_filtering.h>
32#include <rtabmap/core/util3d.h>
33#include <pcl/common/common.h>
34#include <pcl/common/centroid.h>
35#include <pcl/common/io.h>
40template<
typename Po
intT>
42 const typename pcl::PointCloud<PointT> & cloud)
44 typename pcl::PointCloud<PointT>::Ptr output(
new pcl::PointCloud<PointT>);
46 for(
unsigned int i=0; i<output->size(); ++i)
53template<
typename Po
intT>
54void segmentObstaclesFromGround(
55 const typename pcl::PointCloud<PointT>::Ptr & cloud,
56 const typename pcl::IndicesPtr & indices,
57 pcl::IndicesPtr & ground,
58 pcl::IndicesPtr & obstacles,
60 float groundNormalAngle,
63 bool segmentFlatObstacles,
64 float maxGroundHeight,
65 pcl::IndicesPtr * flatObstacles,
66 const Eigen::Vector4f & viewPoint,
67 float groundNormalsUp)
69 ground.reset(
new std::vector<int>);
70 obstacles.reset(
new std::vector<int>);
73 flatObstacles->reset(
new std::vector<int>);
83 Eigen::Vector4f(0,0,1,0),
88 if(flatSurfaces->size() &&
89 (segmentFlatObstacles || maxGroundHeight != 0.0f || minClusterSize>1))
91 int biggestFlatSurfaceIndex;
97 std::numeric_limits<int>::max(),
98 &biggestFlatSurfaceIndex);
101 if(clusteredFlatSurfaces.size())
103 Eigen::Vector4f biggestSurfaceMin,biggestSurfaceMax;
104 if(maxGroundHeight != 0.0f)
108 biggestFlatSurfaceIndex = -1;
109 for(
size_t i=0;i<clusteredFlatSurfaces.size();++i)
111 Eigen::Vector4f min,max;
112 pcl::getMinMax3D(*cloud, *clusteredFlatSurfaces.at(i), min, max);
113 if(min[2]<maxGroundHeight && clusteredFlatSurfaces.at(i)->size() > points)
115 points = clusteredFlatSurfaces.at(i)->size();
116 biggestFlatSurfaceIndex = i;
117 biggestSurfaceMin = min;
118 biggestSurfaceMax = max;
124 pcl::getMinMax3D(*cloud, *clusteredFlatSurfaces.at(biggestFlatSurfaceIndex), biggestSurfaceMin, biggestSurfaceMax);
126 if(biggestFlatSurfaceIndex>=0)
128 ground = clusteredFlatSurfaces.at(biggestFlatSurfaceIndex);
131 if(!ground->empty() && (maxGroundHeight == 0.0f || biggestSurfaceMin[2] < maxGroundHeight))
133 for(
unsigned int i=0; i<clusteredFlatSurfaces.size(); ++i)
135 if((
int)i!=biggestFlatSurfaceIndex)
137 Eigen::Vector4f centroid(0,0,0,1);
138 pcl::compute3DCentroid(*cloud, *clusteredFlatSurfaces.at(i), centroid);
139 if((maxGroundHeight!=0.0f && centroid[2] <= maxGroundHeight) ||
140 centroid[2] <= biggestSurfaceMax[2] ||
141 (maxGroundHeight==0.0f && !segmentFlatObstacles))
145 else if(flatObstacles)
155 ground.reset(
new std::vector<int>);
158 *flatObstacles = flatSurfaces;
165 ground = flatSurfaces;
168 if(ground->size() != cloud->size())
171 pcl::IndicesPtr notObstacles = ground;
180 if(maxGroundHeight != 0.0f)
186 if(otherStuffIndices->size())
201template<
typename Po
intT>
202void segmentObstaclesFromGround(
203 const typename pcl::PointCloud<PointT>::Ptr & cloud,
204 pcl::IndicesPtr & ground,
205 pcl::IndicesPtr & obstacles,
207 float groundNormalAngle,
210 bool segmentFlatObstacles,
211 float maxGroundHeight,
212 pcl::IndicesPtr * flatObstacles,
213 const Eigen::Vector4f & viewPoint,
214 float groundNormalsUp)
216 pcl::IndicesPtr indices(
new std::vector<int>);
217 segmentObstaclesFromGround<PointT>(
226 segmentFlatObstacles,
233template<
typename Po
intT>
235 const typename pcl::PointCloud<PointT>::Ptr & cloud,
236 const pcl::IndicesPtr & groundIndices,
237 const pcl::IndicesPtr & obstaclesIndices,
242 typename pcl::PointCloud<PointT>::Ptr groundCloud(
new pcl::PointCloud<PointT>);
243 typename pcl::PointCloud<PointT>::Ptr obstaclesCloud(
new pcl::PointCloud<PointT>);
245 if(groundIndices->size())
247 pcl::copyPointCloud(*cloud, *groundIndices, *groundCloud);
250 if(obstaclesIndices->size())
252 pcl::copyPointCloud(*cloud, *obstaclesIndices, *obstaclesCloud);
255 occupancy2DFromGroundObstacles<PointT>(
263template<
typename Po
intT>
265 const typename pcl::PointCloud<PointT>::Ptr & groundCloud,
266 const typename pcl::PointCloud<PointT>::Ptr & obstaclesCloud,
272 if(groundCloud->size())
275 typename pcl::PointCloud<PointT>::Ptr groundCloudProjected;
280 ground = cv::Mat(1, (
int)groundCloudProjected->size(), CV_32FC2);
281 for(
unsigned int i=0;i<groundCloudProjected->size(); ++i)
283 cv::Vec2f * ptr = ground.ptr<cv::Vec2f>();
284 ptr[i][0] = groundCloudProjected->at(i).x;
285 ptr[i][1] = groundCloudProjected->at(i).y;
289 obstacles = cv::Mat();
290 if(obstaclesCloud->size())
293 typename pcl::PointCloud<PointT>::Ptr obstaclesCloudProjected;
296 obstaclesCloudProjected =
util3d::voxelize(obstaclesCloudProjected, cellSize);
298 obstacles = cv::Mat(1, (
int)obstaclesCloudProjected->size(), CV_32FC2);
299 for(
unsigned int i=0;i<obstaclesCloudProjected->size(); ++i)
301 cv::Vec2f * ptr = obstacles.ptr<cv::Vec2f>();
302 ptr[i][0] = obstaclesCloudProjected->at(i).x;
303 ptr[i][1] = obstaclesCloudProjected->at(i).y;
308template<
typename Po
intT>
310 const typename pcl::PointCloud<PointT>::Ptr & cloud,
311 const pcl::IndicesPtr & indices,
315 float groundNormalAngle,
317 bool segmentFlatObstacles,
318 float maxGroundHeight)
320 if(cloud->size() == 0)
324 pcl::IndicesPtr groundIndices, obstaclesIndices;
326 segmentObstaclesFromGround<PointT>(
335 segmentFlatObstacles,
338 occupancy2DFromGroundObstacles<PointT>(
347template<
typename Po
intT>
349 const typename pcl::PointCloud<PointT>::Ptr & cloud,
353 float groundNormalAngle,
355 bool segmentFlatObstacles,
356 float maxGroundHeight)
358 pcl::IndicesPtr indices(
new std::vector<int>);
359 occupancy2DFromCloud3D<PointT>(cloud, indices, ground, obstacles, cellSize, groundNormalAngle, minClusterSize, segmentFlatObstacles, maxGroundHeight);
pcl::IndicesPtr RTABMAP_CORE_EXPORT passThrough(const pcl::PointCloud< pcl::PointXYZ >::Ptr &cloud, const pcl::IndicesPtr &indices, const std::string &axis, float min, float max, bool negative=false)
Performs pass-through filtering on a point cloud of type pcl::PointXYZ and returns filtered indices.
pcl::IndicesPtr RTABMAP_CORE_EXPORT normalFiltering(const pcl::PointCloud< pcl::PointXYZ >::Ptr &cloud, float angleMax, const Eigen::Vector4f &normal, int normalKSearch, const Eigen::Vector4f &viewpoint, float groundNormalsUp=0.0f)
Point normal filtering for point cloud of type pcl::PointXYZ.
pcl::PointCloud< pcl::PointXYZ >::Ptr RTABMAP_CORE_EXPORT voxelize(const pcl::PointCloud< pcl::PointXYZ >::Ptr &cloud, const pcl::IndicesPtr &indices, float voxelSize)
Performs voxel grid downsampling on a point cloud of type pcl::PointXYZ on provided indices.
pcl::PointCloud< PointT >::Ptr projectCloudOnXYPlane(const typename pcl::PointCloud< PointT > &cloud)
Projects a point cloud onto the XY plane by setting all Z coordinates to zero.
void occupancy2DFromCloud3D(const typename pcl::PointCloud< PointT >::Ptr &cloud, const pcl::IndicesPtr &indices, cv::Mat &ground, cv::Mat &obstacles, float cellSize, float groundNormalAngle, int minClusterSize, bool segmentFlatObstacles, float maxGroundHeight)
Generates 2D ground and obstacle occupancy data from a 3D point cloud.
void occupancy2DFromGroundObstacles(const typename pcl::PointCloud< PointT >::Ptr &cloud, const pcl::IndicesPtr &groundIndices, const pcl::IndicesPtr &obstaclesIndices, cv::Mat &ground, cv::Mat &obstacles, float cellSize)
Projects 3D ground and obstacle point clouds onto the 2D XY plane and voxelizes them into 2D occupanc...
pcl::IndicesPtr RTABMAP_CORE_EXPORT concatenate(const std::vector< pcl::IndicesPtr > &indices)
Concatenates multiple sets of indices into a single index vector.