40 #include <pcl/pcl_config.h>
43 #ifndef PCL_SURFACE_IMPL_CONVEX_HULL_H_
44 #define PCL_SURFACE_IMPL_CONVEX_HULL_H_
46 #include <pcl/surface/convex_hull.h>
48 #include <pcl/common/eigen.h>
49 #include <pcl/common/transforms.h>
50 #include <pcl/common/io.h>
53 #include <pcl/surface/qhull.h>
56 template <
typename Po
intInT>
void
59 PCL_DEBUG (
"[pcl::%s::calculateInputDimension] WARNING: Input dimension not specified. Automatically determining input dimension.\n", getClassName ().c_str ());
60 Eigen::Vector4d xyz_centroid;
62 EIGEN_ALIGN16 Eigen::Matrix3d covariance_matrix = Eigen::Matrix3d::Zero ();
65 EIGEN_ALIGN16 Eigen::Vector3d eigen_values;
68 if (std::abs (eigen_values[0]) < std::numeric_limits<double>::epsilon () || std::abs (eigen_values[0] / eigen_values[2]) < 1.0e-3)
75 template <
typename Po
intInT>
void
80 bool xy_proj_safe =
true;
81 bool yz_proj_safe =
true;
82 bool xz_proj_safe =
true;
85 PointInT p0 = (*input_)[(*indices_)[0]];
86 PointInT p1 = (*input_)[(*indices_)[indices_->size () - 1]];
87 PointInT p2 = (*input_)[(*indices_)[indices_->size () / 2]];
88 Eigen::Array4f dy1dy2 = (p1.getArray4fMap () - p0.getArray4fMap ()) / (p2.getArray4fMap () - p0.getArray4fMap ());
89 while (!( (dy1dy2[0] != dy1dy2[1]) || (dy1dy2[2] != dy1dy2[1]) ) )
91 p0 = (*input_)[(*indices_)[rand () % indices_->size ()]];
92 p1 = (*input_)[(*indices_)[rand () % indices_->size ()]];
93 p2 = (*input_)[(*indices_)[rand () % indices_->size ()]];
94 dy1dy2 = (p1.getArray4fMap () - p0.getArray4fMap ()) / (p2.getArray4fMap () - p0.getArray4fMap ());
98 normal_calc_cloud.
resize (3);
99 normal_calc_cloud[0] = p0;
100 normal_calc_cloud[1] = p1;
101 normal_calc_cloud[2] = p2;
103 Eigen::Vector4d normal_calc_centroid;
104 Eigen::Matrix3d normal_calc_covariance;
109 Eigen::Vector3d::Scalar eigen_value;
110 Eigen::Vector3d plane_params;
111 pcl::eigen33 (normal_calc_covariance, eigen_value, plane_params);
112 float theta_x = std::abs (
static_cast<float> (plane_params.dot (x_axis_)));
113 float theta_y = std::abs (
static_cast<float> (plane_params.dot (y_axis_)));
114 float theta_z = std::abs (
static_cast<float> (plane_params.dot (z_axis_)));
118 if (theta_z > projection_angle_thresh_)
120 xz_proj_safe =
false;
121 yz_proj_safe =
false;
123 if (theta_x > projection_angle_thresh_)
125 xz_proj_safe =
false;
126 xy_proj_safe =
false;
128 if (theta_y > projection_angle_thresh_)
130 xy_proj_safe =
false;
131 yz_proj_safe =
false;
135 boolT ismalloc = True;
137 FILE *outfile =
nullptr;
143 const char* flags = qhull_flags.c_str ();
145 FILE *errfile = stderr;
148 coordT *points =
reinterpret_cast<coordT*
> (calloc (indices_->size () * dimension, sizeof (coordT)));
154 for (std::size_t i = 0; i < indices_->size (); ++i, j+=dimension)
156 points[j + 0] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].x);
157 points[j + 1] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].y);
160 else if (yz_proj_safe)
162 for (std::size_t i = 0; i < indices_->size (); ++i, j+=dimension)
164 points[j + 0] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].y);
165 points[j + 1] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].z);
168 else if (xz_proj_safe)
170 for (std::size_t i = 0; i < indices_->size (); ++i, j+=dimension)
172 points[j + 0] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].x);
173 points[j + 1] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].z);
179 PCL_ERROR (
"[pcl::%s::performReconstruction2D] Invalid input!\n", getClassName ().c_str ());
185 qh_zero(qh, errfile);
188 int exitcode = qh_new_qhull (qh, dimension,
static_cast<int> (indices_->size ()), points, ismalloc,
const_cast<char*
> (flags), outfile, errfile);
191 qh_prepare_output(qh);
195 if (exitcode != 0 || qh->num_vertices == 0)
197 PCL_ERROR (
"[pcl::%s::performReconstrution2D] ERROR: qhull was unable to compute a convex hull for the given point cloud (%lu)!\n", getClassName ().c_str (), indices_->size ());
203 qh_freeqhull (qh, !qh_ALL);
204 int curlong, totlong;
205 qh_memfreeshort (qh, &curlong, &totlong);
213 total_area_ = qh->totvol;
217 int num_vertices = qh->num_vertices;
220 hull.
resize(num_vertices, PointInT{});
229 hull[i] = (*input_)[(*indices_)[qh_pointid (qh, vertex->point)]];
230 idx_points[i].first = qh_pointid (qh, vertex->point);
235 Eigen::Vector4f centroid;
239 for (std::size_t j = 0; j < hull.
size (); j++)
241 idx_points[j].second[0] = hull[j].x - centroid[0];
242 idx_points[j].second[1] = hull[j].y - centroid[1];
245 else if (yz_proj_safe)
247 for (std::size_t j = 0; j < hull.
size (); j++)
249 idx_points[j].second[0] = hull[j].y - centroid[1];
250 idx_points[j].second[1] = hull[j].z - centroid[2];
253 else if (xz_proj_safe)
255 for (std::size_t j = 0; j < hull.
size (); j++)
257 idx_points[j].second[0] = hull[j].x - centroid[0];
258 idx_points[j].second[1] = hull[j].z - centroid[2];
264 polygons[0].vertices.resize (hull.
size ());
266 hull_indices_.header = input_->header;
267 hull_indices_.indices.clear ();
268 hull_indices_.indices.reserve (hull.
size ());
270 for (
int j = 0; j < static_cast<int> (hull.
size ()); j++)
272 hull_indices_.indices.push_back ((*indices_)[idx_points[j].first]);
273 hull[j] = (*input_)[(*indices_)[idx_points[j].first]];
274 polygons[0].vertices[j] =
static_cast<unsigned int> (j);
277 qh_freeqhull (qh, !qh_ALL);
278 int curlong, totlong;
279 qh_memfreeshort (qh, &curlong, &totlong);
288 #pragma GCC diagnostic ignored "-Wold-style-cast"
291 template <
typename Po
intInT>
void
293 PointCloud &hull, std::vector<pcl::Vertices> &polygons,
bool fill_polygon_data)
298 boolT ismalloc = True;
300 FILE *outfile =
nullptr;
306 const char *flags = qhull_flags.c_str ();
308 FILE *errfile = stderr;
311 coordT *points =
reinterpret_cast<coordT*
> (calloc (indices_->size () * dimension, sizeof (coordT)));
314 for (std::size_t i = 0; i < indices_->size (); ++i, j+=dimension)
316 points[j + 0] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].x);
317 points[j + 1] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].y);
318 points[j + 2] =
static_cast<coordT
> ((*input_)[(*indices_)[i]].z);
324 qh_zero(qh, errfile);
327 int exitcode = qh_new_qhull (qh, dimension,
static_cast<int> (indices_->size ()), points, ismalloc,
const_cast<char*
> (flags), outfile, errfile);
330 qh_prepare_output(qh);
336 PCL_ERROR(
"[pcl::%s::performReconstrution3D] ERROR: qhull was unable to compute a "
337 "convex hull for the given point cloud (%zu)!\n",
338 getClassName().c_str(),
339 static_cast<std::size_t
>(input_->size()));
345 qh_freeqhull (qh, !qh_ALL);
346 int curlong, totlong;
347 qh_memfreeshort (qh, &curlong, &totlong);
354 int num_facets = qh->num_facets;
356 int num_vertices = qh->num_vertices;
357 hull.
resize (num_vertices);
362 unsigned int max_vertex_id = 0;
365 if (vertex->id + 1 > max_vertex_id)
366 max_vertex_id = vertex->id + 1;
370 std::vector<int> qhid_to_pcidx (max_vertex_id);
372 hull_indices_.header = input_->header;
373 hull_indices_.indices.clear ();
374 hull_indices_.indices.reserve (num_vertices);
379 hull_indices_.indices.push_back ((*indices_)[qh_pointid (qh, vertex->point)]);
380 hull[i] = (*input_)[hull_indices_.indices.back ()];
382 qhid_to_pcidx[vertex->id] = i;
388 total_area_ = qh->totarea;
389 total_volume_ = qh->totvol;
392 if (fill_polygon_data)
394 polygons.
resize (num_facets);
400 polygons[dd].vertices.resize (3);
403 int vertex_n, vertex_i;
404 FOREACHvertex_i_ (qh, (*facet).vertices)
406 polygons[dd].vertices[vertex_i] = qhid_to_pcidx[vertex->id];
411 qh_freeqhull (qh, !qh_ALL);
412 int curlong, totlong;
413 qh_memfreeshort (qh, &curlong, &totlong);
420 #pragma GCC diagnostic warning "-Wold-style-cast"
424 template <
typename Po
intInT>
void
426 bool fill_polygon_data)
429 calculateInputDimension ();
431 performReconstruction2D (hull, polygons, fill_polygon_data);
432 else if (dimension_ == 3)
433 performReconstruction3D (hull, polygons, fill_polygon_data);
435 PCL_ERROR (
"[pcl::%s::performReconstruction] Error: invalid input dimension requested: %d\n",getClassName ().c_str (),dimension_);
439 template <
typename Po
intInT>
void
442 points.
header = input_->header;
443 if (!initCompute () || input_->points.empty () || indices_->empty ())
450 std::vector<pcl::Vertices> polygons;
451 performReconstruction (points, polygons,
false);
462 template <
typename Po
intInT>
void
467 performReconstruction (hull_points, output.
polygons,
true);
474 template <
typename Po
intInT>
void
478 performReconstruction (hull_points, polygons,
true);
482 template <
typename Po
intInT>
void
485 points.
header = input_->header;
486 if (!initCompute () || input_->points.empty () || indices_->empty ())
493 performReconstruction (points, polygons,
true);
502 template <
typename Po
intInT>
void
505 hull_point_indices = hull_indices_;
508 #define PCL_INSTANTIATE_ConvexHull(T) template class PCL_EXPORTS pcl::ConvexHull<T>;
void calculateInputDimension()
Automatically determines the dimension of input data - 2D or 3D.
void performReconstruction2D(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The reconstruction method for 2D data.
void getHullPointIndices(pcl::PointIndices &hull_point_indices) const
Retrieve the indices of the input point cloud that for the convex hull.
void performReconstruction(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The actual reconstruction method.
void reconstruct(PointCloud &points, std::vector< pcl::Vertices > &polygons)
Compute a convex hull for all points given.
void performReconstruction3D(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The reconstruction method for 3D data.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
void resize(std::size_t count)
Resizes the container to contain count elements.
std::uint32_t width
The point cloud width (if organized as an image-structure).
pcl::PCLHeader header
The point cloud header.
std::uint32_t height
The point cloud height (if organized as an image-structure).
void clear()
Removes all points in a cloud and sets the width and height to 0.
Define standard C methods and C++ classes that are common to all methods.
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.
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...
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 comparePoints2D(const std::pair< int, Eigen::Vector4f > &p1, const std::pair< int, Eigen::Vector4f > &p2)
Sort 2D points in a vector structure.
PCL_EXPORTS bool isVerbosityLevelEnabled(VERBOSITY_LEVEL severity)
is verbosity level enabled?
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
std::vector< T, Eigen::aligned_allocator< T > > AlignedVector
Type used for aligned vector of Eigen objects in PCL.
std::vector< ::pcl::Vertices > polygons
::pcl::PCLPointCloud2 cloud