Point Cloud Library (PCL)  1.14.0-dev
convex_hull.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/memory.h>
43 #include <pcl/pcl_macros.h>
44 #include <pcl/pcl_config.h>
45 #ifdef HAVE_QHULL
46 
47 // PCL includes
48 #include <pcl/surface/reconstruction.h>
49 #include <pcl/PolygonMesh.h>
50 
51 namespace pcl
52 {
53  /** \brief Sort 2D points in a vector structure
54  * \param p1 the first point
55  * \param p2 the second point
56  * \ingroup surface
57  */
58  inline bool
59  comparePoints2D (const std::pair<int, Eigen::Vector4f> & p1, const std::pair<int, Eigen::Vector4f> & p2)
60  {
61  double angle1 = std::atan2 (p1.second[1], p1.second[0]) + M_PI;
62  double angle2 = std::atan2 (p2.second[1], p2.second[0]) + M_PI;
63  return (angle1 > angle2);
64  }
65 
66  ////////////////////////////////////////////////////////////////////////////////////////////
67  /** \brief @b ConvexHull using libqhull library.
68  * \author Aitor Aldoma, Alex Trevor
69  * \ingroup surface
70  */
71  template<typename PointInT>
72  class ConvexHull : public MeshConstruction<PointInT>
73  {
74  protected:
79 
80  public:
81  using Ptr = shared_ptr<ConvexHull<PointInT> >;
82  using ConstPtr = shared_ptr<const ConvexHull<PointInT> >;
83 
85 
87  using PointCloudPtr = typename PointCloud::Ptr;
89 
90  /** \brief Empty constructor. */
91  ConvexHull() = default;
92 
93  /** \brief Empty destructor */
94  ~ConvexHull () override = default;
95 
96  /** \brief Compute a convex hull for all points given.
97  *
98  * \note In 2D case (i.e. if the input points belong to one plane)
99  * the \a polygons vector will have a single item, whereas in 3D
100  * case it will contain one item for each hull facet.
101  *
102  * \param[out] points the resultant points lying on the convex hull.
103  * \param[out] polygons the resultant convex hull polygons, as a set of
104  * vertices. The Vertices structure contains an array of point indices.
105  */
106  void
107  reconstruct (PointCloud &points,
108  std::vector<pcl::Vertices> &polygons);
109 
110  /** \brief Compute a convex hull for all points given.
111  * \param[out] points the resultant points lying on the convex hull.
112  */
113  void
114  reconstruct (PointCloud &points);
115 
116  /** \brief If set to true, the qhull library is called to compute the total area and volume of the convex hull.
117  * NOTE: When this option is activated, the qhull library produces output to the console.
118  * \param[in] value whether to compute the area and the volume, default is false
119  */
120  void
121  setComputeAreaVolume (bool value)
122  {
123  compute_area_ = value;
124  if (compute_area_)
125  qhull_flags = std::string ("qhull FA");
126  else
127  qhull_flags = std::string ("qhull ");
128  }
129 
130  /** \brief Returns the total area of the convex hull. */
131  double
132  getTotalArea () const
133  {
134  return (total_area_);
135  }
136 
137  /** \brief Returns the total volume of the convex hull. Only valid for 3-dimensional sets.
138  * For 2D-sets volume is zero.
139  */
140  double
141  getTotalVolume () const
142  {
143  return (total_volume_);
144  }
145 
146  /** \brief Sets the dimension on the input data, 2D or 3D.
147  * \param[in] dimension The dimension of the input data. If not set, this will be determined automatically.
148  */
149  void
150  setDimension (int dimension)
151  {
152  if ((dimension == 2) || (dimension == 3))
153  dimension_ = dimension;
154  else
155  PCL_ERROR ("[pcl::%s::setDimension] Invalid input dimension specified!\n", getClassName ().c_str ());
156  }
157 
158  /** \brief Returns the dimensionality (2 or 3) of the calculated hull. */
159  inline int
160  getDimension () const
161  {
162  return (dimension_);
163  }
164 
165  /** \brief Retrieve the indices of the input point cloud that for the convex hull.
166  *
167  * \note Should only be called after reconstruction was performed.
168  * \param[out] hull_point_indices The indices of the points forming the point cloud
169  */
170  void
171  getHullPointIndices (pcl::PointIndices &hull_point_indices) const;
172 
173  protected:
174  /** \brief The actual reconstruction method.
175  *
176  * \param[out] points the resultant points lying on the convex hull
177  * \param[out] polygons the resultant convex hull polygons, as a set of
178  * vertices. The Vertices structure contains an array of point indices.
179  * \param[in] fill_polygon_data true if polygons should be filled, false otherwise
180  */
181  void
183  std::vector<pcl::Vertices> &polygons,
184  bool fill_polygon_data = false);
185 
186  /** \brief The reconstruction method for 2D data. Does not require dimension to be set.
187  *
188  * \param[out] points the resultant points lying on the convex hull
189  * \param[out] polygons the resultant convex hull polygons, as a set of
190  * vertices. The Vertices structure contains an array of point indices.
191  * \param[in] fill_polygon_data true if polygons should be filled, false otherwise
192  */
193  void
195  std::vector<pcl::Vertices> &polygons,
196  bool fill_polygon_data = false);
197 
198  /** \brief The reconstruction method for 3D data. Does not require dimension to be set.
199  *
200  * \param[out] points the resultant points lying on the convex hull
201  * \param[out] polygons the resultant convex hull polygons, as a set of
202  * vertices. The Vertices structure contains an array of point indices.
203  * \param[in] fill_polygon_data true if polygons should be filled, false otherwise
204  */
205  void
207  std::vector<pcl::Vertices> &polygons,
208  bool fill_polygon_data = false);
209 
210  /** \brief A reconstruction method that returns a polygonmesh.
211  *
212  * \param[out] output a PolygonMesh representing the convex hull of the input data.
213  */
214  void
215  performReconstruction (PolygonMesh &output) override;
216 
217  /** \brief A reconstruction method that returns the polygon of the convex hull.
218  *
219  * \param[out] polygons the polygon(s) representing the convex hull of the input data.
220  */
221  void
222  performReconstruction (std::vector<pcl::Vertices> &polygons) override;
223 
224  /** \brief Automatically determines the dimension of input data - 2D or 3D. */
225  void
227 
228  /** \brief Class get name method. */
229  std::string
230  getClassName () const override
231  {
232  return ("ConvexHull");
233  }
234 
235  /* \brief True if we should compute the area and volume of the convex hull. */
236  bool compute_area_{false};
237 
238  /* \brief The area of the convex hull. */
239  double total_area_{0.0};
240 
241  /* \brief The volume of the convex hull (only for 3D hulls, zero for 2D). */
242  double total_volume_{0.0};
243 
244  /** \brief The dimensionality of the concave hull (2D or 3D). */
245  int dimension_{0};
246 
247  /** \brief How close can a 2D plane's normal be to an axis to make projection problematic. */
248  double projection_angle_thresh_{std::cos (0.174532925)};
249 
250  /** \brief Option flag string to be used calling qhull. */
251  std::string qhull_flags{"qhull "};
252 
253  /* \brief x-axis - for checking valid projections. */
254  const Eigen::Vector3d x_axis_{1.0, 0.0, 0.0};
255 
256  /* \brief y-axis - for checking valid projections. */
257  const Eigen::Vector3d y_axis_{0.0, 1.0, 0.0};
258 
259  /* \brief z-axis - for checking valid projections. */
260  const Eigen::Vector3d z_axis_{0.0, 0.0, 1.0};
261 
262  /* \brief vector containing the point cloud indices of the convex hull points. */
264 
265  public:
267  };
268 }
269 
270 #ifdef PCL_NO_PRECOMPILE
271 #include <pcl/surface/impl/convex_hull.hpp>
272 #endif
273 
274 #endif
ConvexHull using libqhull library.
Definition: convex_hull.h:73
double total_volume_
Definition: convex_hull.h:242
double getTotalArea() const
Returns the total area of the convex hull.
Definition: convex_hull.h:132
shared_ptr< const ConvexHull< PointInT > > ConstPtr
Definition: convex_hull.h:82
std::string getClassName() const override
Class get name method.
Definition: convex_hull.h:230
void setComputeAreaVolume(bool value)
If set to true, the qhull library is called to compute the total area and volume of the convex hull.
Definition: convex_hull.h:121
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: convex_hull.h:88
void calculateInputDimension()
Automatically determines the dimension of input data - 2D or 3D.
Definition: convex_hull.hpp:57
void performReconstruction2D(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The reconstruction method for 2D data.
Definition: convex_hull.hpp:76
~ConvexHull() override=default
Empty destructor.
void setDimension(int dimension)
Sets the dimension on the input data, 2D or 3D.
Definition: convex_hull.h:150
ConvexHull()=default
Empty constructor.
const Eigen::Vector3d z_axis_
Definition: convex_hull.h:260
double getTotalVolume() const
Returns the total volume of the convex hull.
Definition: convex_hull.h:141
std::string qhull_flags
Option flag string to be used calling qhull.
Definition: convex_hull.h:251
void getHullPointIndices(pcl::PointIndices &hull_point_indices) const
Retrieve the indices of the input point cloud that for the convex hull.
pcl::PointIndices hull_indices_
Definition: convex_hull.h:263
shared_ptr< ConvexHull< PointInT > > Ptr
Definition: convex_hull.h:81
void performReconstruction(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The actual reconstruction method.
int getDimension() const
Returns the dimensionality (2 or 3) of the calculated hull.
Definition: convex_hull.h:160
double projection_angle_thresh_
How close can a 2D plane's normal be to an axis to make projection problematic.
Definition: convex_hull.h:248
double total_area_
Definition: convex_hull.h:239
typename PointCloud::Ptr PointCloudPtr
Definition: convex_hull.h:87
void reconstruct(PointCloud &points, std::vector< pcl::Vertices > &polygons)
Compute a convex hull for all points given.
const Eigen::Vector3d y_axis_
Definition: convex_hull.h:257
const Eigen::Vector3d x_axis_
Definition: convex_hull.h:254
void performReconstruction3D(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The reconstruction method for 3D data.
int dimension_
The dimensionality of the concave hull (2D or 3D).
Definition: convex_hull.h:245
MeshConstruction represents a base surface reconstruction class.
PCL base class.
Definition: pcl_base.h:70
shared_ptr< PointCloud< PointInT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointInT > > ConstPtr
Definition: point_cloud.h:414
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
bool comparePoints2D(const std::pair< int, Eigen::Vector4f > &p1, const std::pair< int, Eigen::Vector4f > &p2)
Sort 2D points in a vector structure.
Definition: convex_hull.h:59
Defines functions, macros and traits for allocating and using memory.
Defines all the PCL and non-PCL macros used.
#define M_PI
Definition: pcl_macros.h:201