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> 49 #include <pcl/common/eigen.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);
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);
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.
size () * dim_,
sizeof(coordT)));
188 for (std::size_t i = 0; i < cloud_transformed.
size (); ++i)
190 points[i * dim_ + 0] =
static_cast<coordT
> (cloud_transformed[i].x);
191 points[i * dim_ + 1] =
static_cast<coordT
> (cloud_transformed[i].y);
194 points[i * dim_ + 2] =
static_cast<coordT
> (cloud_transformed[i].z);
198 exitcode = qh_new_qhull (dim_, static_cast<int> (cloud_transformed.
size ()), points, ismalloc, flags, outfile, errfile);
202 PCL_ERROR(
"[pcl::%s::performReconstrution] ERROR: qhull was unable to compute a " 203 "concave hull for the given point cloud (%zu)!\n",
204 getClassName().c_str(),
205 static_cast<std::size_t>(cloud_transformed.
size()));
210 bool NaNvalues =
false;
211 for (std::size_t i = 0; i < cloud_transformed.
size (); ++i)
213 if (!std::isfinite (cloud_transformed[i].x) ||
214 !std::isfinite (cloud_transformed[i].y) ||
215 !std::isfinite (cloud_transformed[i].z))
223 PCL_ERROR (
"[pcl::%s::performReconstruction] ERROR: point cloud contains NaN values, consider running pcl::PassThrough filter first to remove NaNs!\n", getClassName ().c_str ());
226 alpha_shape.
points.resize (0);
230 qh_freeqhull (!qh_ALL);
231 int curlong, totlong;
232 qh_memfreeshort (&curlong, &totlong);
237 qh_setvoronoi_all ();
239 int num_vertices = qh num_vertices;
240 alpha_shape.
points.resize (num_vertices);
244 int max_vertex_id = 0;
247 if (vertex->id + 1 > unsigned (max_vertex_id))
248 max_vertex_id = vertex->id + 1;
254 std::vector<int> qhid_to_pcidx (max_vertex_id);
256 int num_facets = qh num_facets;
260 setT *triangles_set = qh_settemp (4 * num_facets);
261 if (voronoi_centers_)
262 voronoi_centers_->points.resize (num_facets);
268 if (!facet->upperdelaunay)
270 vertexT *anyVertex =
static_cast<vertexT*
> (facet->vertices->e[0].p);
271 double *center = facet->center;
272 double r = qh_pointdist (anyVertex->point,center,dim_);
274 if (voronoi_centers_)
276 (*voronoi_centers_)[non_upper].x =
static_cast<float> (facet->center[0]);
277 (*voronoi_centers_)[non_upper].y =
static_cast<float> (facet->center[1]);
278 (*voronoi_centers_)[non_upper].z =
static_cast<float> (facet->center[2]);
286 qh_makeridges (facet);
288 facet->visitid = qh visit_id;
289 ridgeT *ridge, **ridgep;
290 FOREACHridge_ (facet->ridges)
292 facetT *neighb = otherfacet_ (ridge, facet);
293 if ((neighb->visitid != qh visit_id))
294 qh_setappend (&triangles_set, ridge);
301 facet->visitid = qh visit_id;
302 qh_makeridges (facet);
303 ridgeT *ridge, **ridgep;
304 FOREACHridge_ (facet->ridges)
307 neighb = otherfacet_ (ridge, facet);
308 if ((neighb->visitid != qh visit_id))
313 a.x =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[0].p))->point[0]);
314 a.y =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[0].p))->point[1]);
315 a.z =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[0].p))->point[2]);
316 b.x =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[1].p))->point[0]);
317 b.y =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[1].p))->point[1]);
318 b.z =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[1].p))->point[2]);
319 c.x =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[2].p))->point[0]);
320 c.y =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[2].p))->point[1]);
321 c.z =
static_cast<float> ((
static_cast<vertexT*
>(ridge->vertices->e[2].p))->point[2]);
325 qh_setappend (&triangles_set, ridge);
332 if (voronoi_centers_)
333 voronoi_centers_->points.resize (non_upper);
337 int num_good_triangles = 0;
338 ridgeT *ridge, **ridgep;
339 FOREACHridge_ (triangles_set)
341 if (ridge->bottom->upperdelaunay || ridge->top->upperdelaunay || !ridge->top->good || !ridge->bottom->good)
342 num_good_triangles++;
345 polygons.resize (num_good_triangles);
348 std::vector<bool> added_vertices (max_vertex_id,
false);
351 FOREACHridge_ (triangles_set)
353 if (ridge->bottom->upperdelaunay || ridge->top->upperdelaunay || !ridge->top->good || !ridge->bottom->good)
355 polygons[triangles].vertices.resize (3);
356 int vertex_n, vertex_i;
357 FOREACHvertex_i_ ((*ridge).vertices)
359 if (!added_vertices[vertex->id])
361 alpha_shape[vertices].x =
static_cast<float> (vertex->point[0]);
362 alpha_shape[vertices].y =
static_cast<float> (vertex->point[1]);
363 alpha_shape[vertices].z =
static_cast<float> (vertex->point[2]);
365 qhid_to_pcidx[vertex->id] = vertices;
366 added_vertices[vertex->id] =
true;
370 polygons[triangles].vertices[vertex_i] = qhid_to_pcidx[vertex->id];
378 alpha_shape.
points.resize (vertices);
386 setT *edges_set = qh_settemp (3 * num_facets);
387 if (voronoi_centers_)
388 voronoi_centers_->points.resize (num_facets);
394 if (!facet->upperdelaunay)
398 vertexT *anyVertex =
static_cast<vertexT*
>(facet->vertices->e[0].p);
399 double r = (sqrt ((anyVertex->point[0] - facet->center[0]) *
400 (anyVertex->point[0] - facet->center[0]) +
401 (anyVertex->point[1] - facet->center[1]) *
402 (anyVertex->point[1] - facet->center[1])));
406 qh_makeridges (facet);
409 ridgeT *ridge, **ridgep;
410 FOREACHridge_ (facet->ridges)
411 qh_setappend (&edges_set, ridge);
413 if (voronoi_centers_)
415 (*voronoi_centers_)[dd].x =
static_cast<float> (facet->center[0]);
416 (*voronoi_centers_)[dd].y =
static_cast<float> (facet->center[1]);
417 (*voronoi_centers_)[dd].z = 0.0f;
428 std::vector<bool> added_vertices (max_vertex_id,
false);
429 std::map<int, std::vector<int> > edges;
431 ridgeT *ridge, **ridgep;
432 FOREACHridge_ (edges_set)
434 if (ridge->bottom->upperdelaunay || ridge->top->upperdelaunay || !ridge->top->good || !ridge->bottom->good)
436 int vertex_n, vertex_i;
437 int vertices_in_ridge=0;
438 std::vector<int> pcd_indices;
439 pcd_indices.resize (2);
441 FOREACHvertex_i_ ((*ridge).vertices)
443 if (!added_vertices[vertex->id])
445 alpha_shape[vertices].x =
static_cast<float> (vertex->point[0]);
446 alpha_shape[vertices].y =
static_cast<float> (vertex->point[1]);
449 alpha_shape[vertices].z =
static_cast<float> (vertex->point[2]);
451 alpha_shape[vertices].z = 0;
453 qhid_to_pcidx[vertex->id] = vertices;
454 added_vertices[vertex->id] =
true;
455 pcd_indices[vertices_in_ridge] = vertices;
460 pcd_indices[vertices_in_ridge] = qhid_to_pcidx[vertex->id];
467 edges[pcd_indices[0]].
push_back (pcd_indices[1]);
468 edges[pcd_indices[1]].push_back (pcd_indices[0]);
472 alpha_shape.
points.resize (vertices);
475 alpha_shape_sorted.
points.resize (vertices);
478 std::map<int, std::vector<int> >::iterator curr = edges.begin ();
480 std::vector<bool> used (vertices,
false);
481 std::vector<int> pcd_idx_start_polygons;
482 pcd_idx_start_polygons.push_back (0);
486 while (!edges.empty ())
488 alpha_shape_sorted[sorted_idx] = alpha_shape[(*curr).first];
490 for (
const int &i : (*curr).second)
500 used[(*curr).first] =
true;
509 curr = edges.find (next);
510 if (curr == edges.end ())
513 curr = edges.begin ();
514 pcd_idx_start_polygons.push_back (sorted_idx);
518 pcd_idx_start_polygons.push_back (sorted_idx);
522 polygons.reserve (pcd_idx_start_polygons.size () - 1);
524 for (std::size_t poly_id = 0; poly_id < pcd_idx_start_polygons.size () - 1; poly_id++)
527 if (pcd_idx_start_polygons[poly_id + 1] - pcd_idx_start_polygons[poly_id] >= 3)
530 vertices.
vertices.resize (pcd_idx_start_polygons[poly_id + 1] - pcd_idx_start_polygons[poly_id]);
532 for (
int j = pcd_idx_start_polygons[poly_id]; j < pcd_idx_start_polygons[poly_id + 1]; ++j)
533 vertices.
vertices[j - pcd_idx_start_polygons[poly_id]] = static_cast<std::uint32_t> (j);
535 polygons.push_back (vertices);
539 if (voronoi_centers_)
540 voronoi_centers_->points.resize (dd);
543 qh_freeqhull (!qh_ALL);
544 int curlong, totlong;
545 qh_memfreeshort (&curlong, &totlong);
547 Eigen::Affine3d transInverse = transform1.inverse ();
549 xyz_centroid[0] = - xyz_centroid[0];
550 xyz_centroid[1] = - xyz_centroid[1];
551 xyz_centroid[2] = - xyz_centroid[2];
555 if (voronoi_centers_)
561 if (keep_information_)
567 std::vector<int> neighbor;
568 std::vector<float> distances;
570 distances.resize (1);
573 hull_indices_.header = input_->header;
574 hull_indices_.indices.clear ();
575 hull_indices_.indices.reserve (alpha_shape.
size ());
577 for (
const auto& point: alpha_shape)
580 hull_indices_.indices.push_back (neighbor[0]);
588 #pragma GCC diagnostic warning "-Wold-style-cast" 592 template <
typename Po
intInT>
void 597 performReconstruction (hull_points, output.
polygons);
604 template <
typename Po
intInT>
void 608 performReconstruction (hull_points, polygons);
612 template <
typename Po
intInT>
void 615 hull_point_indices = hull_indices_;
618 #define PCL_INSTANTIATE_ConcaveHull(T) template class PCL_EXPORTS pcl::ConcaveHull<T>; 620 #endif // PCL_SURFACE_IMPL_CONCAVE_HULL_H_ KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures.
iterator erase(iterator position)
Erase a point in the cloud.
std::vector< std::uint32_t > vertices
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
void push_back(const PointT &pt)
Insert a new point in the cloud, at the end of the container.
Describes a set of vertices in a polygon mesh, by basically storing an array of indices.
std::uint32_t width
The point cloud width (if organized as an image-structure).
Define standard C methods and C++ classes that are common to all methods.
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 getHullPointIndices(pcl::PointIndices &hull_point_indices) const
Retrieve the indices of the input point cloud that for the convex hull.
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
std::uint32_t height
The point cloud height (if organized as an image-structure).
std::vector< ::pcl::Vertices > polygons
pcl::PCLHeader header
The point cloud header.
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<T> 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...
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
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.
Define methods for centroid estimation and covariance matrix calculus.