40 #include <pcl/pcl_config.h>
43 #ifndef PCL_SURFACE_IMPL_CONCAVE_HULL_H_
44 #define PCL_SURFACE_IMPL_CONCAVE_HULL_H_
47 #include <pcl/surface/concave_hull.h>
48 #include <pcl/common/common.h>
49 #include <pcl/common/eigen.h>
50 #include <pcl/common/centroid.h>
51 #include <pcl/common/transforms.h>
52 #include <pcl/common/io.h>
55 #include <pcl/surface/qhull.h>
58 template <
typename Po
intInT>
void
61 output.
header = input_->header;
64 PCL_ERROR (
"[pcl::%s::reconstruct] Alpha parameter must be set to a positive number!\n", getClassName ().c_str ());
76 std::vector<pcl::Vertices> polygons;
77 performReconstruction (output, polygons);
79 output.
width =
static_cast<std::uint32_t
> (output.
points.size ());
87 template <
typename Po
intInT>
void
90 output.
header = input_->header;
93 PCL_ERROR (
"[pcl::%s::reconstruct] Alpha parameter must be set to a positive number!\n", getClassName ().c_str ());
105 performReconstruction (output, polygons);
107 output.
width =
static_cast<std::uint32_t
> (output.
points.size ());
115 #pragma GCC diagnostic ignored "-Wold-style-cast"
118 template <
typename Po
intInT>
void
121 Eigen::Vector4d xyz_centroid;
123 EIGEN_ALIGN16 Eigen::Matrix3d covariance_matrix = Eigen::Matrix3d::Zero ();
127 for (
int i = 0; i < 3; ++i)
128 for (
int j = 0; j < 3; ++j)
129 if (!std::isfinite (covariance_matrix.coeffRef (i, j)))
132 EIGEN_ALIGN16 Eigen::Vector3d eigen_values;
133 EIGEN_ALIGN16 Eigen::Matrix3d eigen_vectors;
134 pcl::eigen33 (covariance_matrix, eigen_vectors, eigen_values);
136 Eigen::Affine3d transform1;
137 transform1.setIdentity ();
142 PCL_DEBUG (
"[pcl::%s] WARNING: Input dimension not specified. Automatically determining input dimension.\n", getClassName ().c_str ());
143 if (std::abs (eigen_values[0]) < std::numeric_limits<double>::epsilon () || std::abs (eigen_values[0] / eigen_values[2]) < 1.0e-3)
154 transform1 (2, 0) = eigen_vectors (0, 0);
155 transform1 (2, 1) = eigen_vectors (1, 0);
156 transform1 (2, 2) = eigen_vectors (2, 0);
158 transform1 (1, 0) = eigen_vectors (0, 1);
159 transform1 (1, 1) = eigen_vectors (1, 1);
160 transform1 (1, 2) = eigen_vectors (2, 1);
161 transform1 (0, 0) = eigen_vectors (0, 2);
162 transform1 (0, 1) = eigen_vectors (1, 2);
163 transform1 (0, 2) = eigen_vectors (2, 2);
167 transform1.setIdentity ();
175 boolT ismalloc = True;
177 char flags[] =
"qhull d QJ";
179 FILE *outfile =
nullptr;
181 FILE *errfile = stderr;
186 coordT *points =
reinterpret_cast<coordT*
> (calloc (cloud_transformed.
points.size () * dim_,
sizeof(coordT)));
188 for (std::size_t i = 0; i < cloud_transformed.
points.size (); ++i)
190 points[i * dim_ + 0] =
static_cast<coordT
> (cloud_transformed.
points[i].x);
191 points[i * dim_ + 1] =
static_cast<coordT
> (cloud_transformed.
points[i].y);
194 points[i * dim_ + 2] =
static_cast<coordT
> (cloud_transformed.
points[i].z);
198 exitcode = qh_new_qhull (dim_, static_cast<int> (cloud_transformed.
points.size ()), points, ismalloc, flags, outfile, errfile);
202 PCL_ERROR (
"[pcl::%s::performReconstrution] ERROR: qhull was unable to compute a concave hull for the given point cloud (%lu)!\n", getClassName ().c_str (), cloud_transformed.
points.size ());
207 bool NaNvalues =
false;
208 for (std::size_t i = 0; i < cloud_transformed.
size (); ++i)
210 if (!std::isfinite (cloud_transformed.
points[i].x) ||
211 !std::isfinite (cloud_transformed.
points[i].y) ||
212 !std::isfinite (cloud_transformed.
points[i].z))
220 PCL_ERROR (
"[pcl::%s::performReconstruction] ERROR: point cloud contains NaN values, consider running pcl::PassThrough filter first to remove NaNs!\n", getClassName ().c_str ());
223 alpha_shape.
points.resize (0);
227 qh_freeqhull (!qh_ALL);
228 int curlong, totlong;
229 qh_memfreeshort (&curlong, &totlong);
234 qh_setvoronoi_all ();
236 int num_vertices = qh num_vertices;
237 alpha_shape.
points.resize (num_vertices);
241 int max_vertex_id = 0;
244 if (vertex->id + 1 > unsigned (max_vertex_id))
245 max_vertex_id = vertex->id + 1;
251 std::vector<int> qhid_to_pcidx (max_vertex_id);
253 int num_facets = qh num_facets;
257 setT *triangles_set = qh_settemp (4 * num_facets);
258 if (voronoi_centers_)
259 voronoi_centers_->points.resize (num_facets);
265 if (!facet->upperdelaunay)
267 vertexT *anyVertex =
static_cast<vertexT*
> (facet->vertices->e[0].p);
268 double *center = facet->center;
269 double r = qh_pointdist (anyVertex->point,center,dim_);
271 if (voronoi_centers_)
273 voronoi_centers_->points[non_upper].x =
static_cast<float> (facet->center[0]);
274 voronoi_centers_->points[non_upper].y =
static_cast<float> (facet->center[1]);
275 voronoi_centers_->points[non_upper].z =
static_cast<float> (facet->center[2]);
283 qh_makeridges (facet);
285 facet->visitid = qh visit_id;
286 ridgeT *ridge, **ridgep;
287 FOREACHridge_ (facet->ridges)
289 facetT *neighb = otherfacet_ (ridge, facet);
290 if ((neighb->visitid != qh visit_id))
291 qh_setappend (&triangles_set, ridge);
298 facet->visitid = qh visit_id;
299 qh_makeridges (facet);
300 ridgeT *ridge, **ridgep;
301 FOREACHridge_ (facet->ridges)
304 neighb = otherfacet_ (ridge, facet);
305 if ((neighb->visitid != qh visit_id))
310 a.x =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[0].p))->point[0]);
311 a.y =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[0].p))->point[1]);
312 a.z =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[0].p))->point[2]);
313 b.x =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[1].p))->point[0]);
314 b.y =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[1].p))->point[1]);
315 b.z =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[1].p))->point[2]);
316 c.x =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[2].p))->point[0]);
317 c.y =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[2].p))->point[1]);
318 c.z =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[2].p))->point[2]);
322 qh_setappend (&triangles_set, ridge);
329 if (voronoi_centers_)
330 voronoi_centers_->points.resize (non_upper);
334 int num_good_triangles = 0;
335 ridgeT *ridge, **ridgep;
336 FOREACHridge_ (triangles_set)
338 if (ridge->bottom->upperdelaunay || ridge->top->upperdelaunay || !ridge->top->good || !ridge->bottom->good)
339 num_good_triangles++;
342 polygons.resize (num_good_triangles);
345 std::vector<bool> added_vertices (max_vertex_id,
false);
348 FOREACHridge_ (triangles_set)
350 if (ridge->bottom->upperdelaunay || ridge->top->upperdelaunay || !ridge->top->good || !ridge->bottom->good)
352 polygons[triangles].vertices.resize (3);
353 int vertex_n, vertex_i;
354 FOREACHvertex_i_ ((*ridge).vertices)
356 if (!added_vertices[vertex->id])
358 alpha_shape.
points[vertices].x =
static_cast<float> (vertex->point[0]);
359 alpha_shape.
points[vertices].y =
static_cast<float> (vertex->point[1]);
360 alpha_shape.
points[vertices].z =
static_cast<float> (vertex->point[2]);
362 qhid_to_pcidx[vertex->id] = vertices;
363 added_vertices[vertex->id] =
true;
367 polygons[triangles].vertices[vertex_i] = qhid_to_pcidx[vertex->id];
375 alpha_shape.
points.resize (vertices);
376 alpha_shape.
width =
static_cast<std::uint32_t
> (alpha_shape.
points.size ());
383 setT *edges_set = qh_settemp (3 * num_facets);
384 if (voronoi_centers_)
385 voronoi_centers_->points.resize (num_facets);
391 if (!facet->upperdelaunay)
395 vertexT *anyVertex =
static_cast<vertexT*
>(facet->vertices->e[0].p);
396 double r = (sqrt ((anyVertex->point[0] - facet->center[0]) *
397 (anyVertex->point[0] - facet->center[0]) +
398 (anyVertex->point[1] - facet->center[1]) *
399 (anyVertex->point[1] - facet->center[1])));
403 qh_makeridges (facet);
406 ridgeT *ridge, **ridgep;
407 FOREACHridge_ (facet->ridges)
408 qh_setappend (&edges_set, ridge);
410 if (voronoi_centers_)
412 voronoi_centers_->points[dd].x =
static_cast<float> (facet->center[0]);
413 voronoi_centers_->points[dd].y =
static_cast<float> (facet->center[1]);
414 voronoi_centers_->points[dd].z = 0.0f;
425 std::vector<bool> added_vertices (max_vertex_id,
false);
426 std::map<int, std::vector<int> > edges;
428 ridgeT *ridge, **ridgep;
429 FOREACHridge_ (edges_set)
431 if (ridge->bottom->upperdelaunay || ridge->top->upperdelaunay || !ridge->top->good || !ridge->bottom->good)
433 int vertex_n, vertex_i;
434 int vertices_in_ridge=0;
435 std::vector<int> pcd_indices;
436 pcd_indices.resize (2);
438 FOREACHvertex_i_ ((*ridge).vertices)
440 if (!added_vertices[vertex->id])
442 alpha_shape.
points[vertices].x =
static_cast<float> (vertex->point[0]);
443 alpha_shape.
points[vertices].y =
static_cast<float> (vertex->point[1]);
446 alpha_shape.
points[vertices].z =
static_cast<float> (vertex->point[2]);
448 alpha_shape.
points[vertices].z = 0;
450 qhid_to_pcidx[vertex->id] = vertices;
451 added_vertices[vertex->id] =
true;
452 pcd_indices[vertices_in_ridge] = vertices;
457 pcd_indices[vertices_in_ridge] = qhid_to_pcidx[vertex->id];
464 edges[pcd_indices[0]].push_back (pcd_indices[1]);
465 edges[pcd_indices[1]].push_back (pcd_indices[0]);
469 alpha_shape.
points.resize (vertices);
472 alpha_shape_sorted.
points.resize (vertices);
475 std::map<int, std::vector<int> >::iterator curr = edges.begin ();
477 std::vector<bool> used (vertices,
false);
478 std::vector<int> pcd_idx_start_polygons;
479 pcd_idx_start_polygons.push_back (0);
483 while (!edges.empty ())
485 alpha_shape_sorted.
points[sorted_idx] = alpha_shape.
points[(*curr).first];
487 for (
const int &i : (*curr).second)
497 used[(*curr).first] =
true;
506 curr = edges.find (next);
507 if (curr == edges.end ())
510 curr = edges.begin ();
511 pcd_idx_start_polygons.push_back (sorted_idx);
515 pcd_idx_start_polygons.push_back (sorted_idx);
519 polygons.reserve (pcd_idx_start_polygons.size () - 1);
521 for (std::size_t poly_id = 0; poly_id < pcd_idx_start_polygons.size () - 1; poly_id++)
524 if (pcd_idx_start_polygons[poly_id + 1] - pcd_idx_start_polygons[poly_id] >= 3)
527 vertices.
vertices.resize (pcd_idx_start_polygons[poly_id + 1] - pcd_idx_start_polygons[poly_id]);
529 for (
int j = pcd_idx_start_polygons[poly_id]; j < pcd_idx_start_polygons[poly_id + 1]; ++j)
530 vertices.
vertices[j - pcd_idx_start_polygons[poly_id]] = static_cast<std::uint32_t> (j);
532 polygons.push_back (vertices);
536 if (voronoi_centers_)
537 voronoi_centers_->points.resize (dd);
540 qh_freeqhull (!qh_ALL);
541 int curlong, totlong;
542 qh_memfreeshort (&curlong, &totlong);
544 Eigen::Affine3d transInverse = transform1.inverse ();
546 xyz_centroid[0] = - xyz_centroid[0];
547 xyz_centroid[1] = - xyz_centroid[1];
548 xyz_centroid[2] = - xyz_centroid[2];
552 if (voronoi_centers_)
558 if (keep_information_)
564 std::vector<int> neighbor;
565 std::vector<float> distances;
567 distances.resize (1);
570 hull_indices_.header = input_->header;
571 hull_indices_.indices.clear ();
572 hull_indices_.indices.reserve (alpha_shape.
points.size ());
574 for (std::size_t i = 0; i < alpha_shape.
points.size (); i++)
577 hull_indices_.indices.push_back (neighbor[0]);
585 #pragma GCC diagnostic warning "-Wold-style-cast"
589 template <
typename Po
intInT>
void
594 performReconstruction (hull_points, output.
polygons);
601 template <
typename Po
intInT>
void
605 performReconstruction (hull_points, polygons);
609 template <
typename Po
intInT>
void
612 hull_point_indices = hull_indices_;
615 #define PCL_INSTANTIATE_ConcaveHull(T) template class PCL_EXPORTS pcl::ConcaveHull<T>;
617 #endif // PCL_SURFACE_IMPL_CONCAVE_HULL_H_
KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures.
std::vector< std::uint32_t > vertices
std::uint32_t width
The point cloud width (if organized as an image-structure).
void getHullPointIndices(pcl::PointIndices &hull_point_indices) const
Retrieve the indices of the input point cloud that for the convex hull.
Describes a set of vertices in a polygon mesh, by basically storing an array of indices.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform, bool copy_all_fields)
Apply an affine transform defined by an Eigen Transform.
void demeanPointCloud(ConstCloudIterator< PointT > &cloud_iterator, const Eigen::Matrix< Scalar, 4, 1 > ¢roid, pcl::PointCloud< PointT > &cloud_out, int npts=0)
Subtract a centroid from a point cloud and return the de-meaned representation.
void reconstruct(PointCloud &points, std::vector< pcl::Vertices > &polygons)
Compute a concave hull for all points given.
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
std::vector< ::pcl::Vertices > polygons
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
::pcl::PCLPointCloud2 cloud
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud object to a PCLPointCloud2 binary data blob.
double getCircumcircleRadius(const PointT &pa, const PointT &pb, const PointT &pc)
Compute the radius of a circumscribed circle for a triangle formed of three points pa...
std::uint32_t height
The point cloud height (if organized as an image-structure).
int nearestKSearch(const PointT &point, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const override
Search for k-nearest neighbors for the given query point.
void performReconstruction(PointCloud &points, std::vector< pcl::Vertices > &polygons)
The actual reconstruction method.
unsigned int computeCovarianceMatrixNormalized(const pcl::PointCloud< PointT > &cloud, const Eigen::Matrix< Scalar, 4, 1 > ¢roid, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix)
Compute normalized the 3x3 covariance matrix of a given set of points.
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > ¢roid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
pcl::PCLHeader header
The point cloud header.