Point Cloud Library (PCL)  1.9.1-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 #ifdef HAVE_QHULL
43 
44 // PCL includes
45 #include <pcl/pcl_config.h>
46 #include <pcl/surface/reconstruction.h>
47 #include <pcl/ModelCoefficients.h>
48 #include <pcl/PolygonMesh.h>
49 
50 namespace pcl
51 {
52  /** \brief Sort 2D points in a vector structure
53  * \param p1 the first point
54  * \param p2 the second point
55  * \ingroup surface
56  */
57  inline bool
58  comparePoints2D (const std::pair<int, Eigen::Vector4f> & p1, const std::pair<int, Eigen::Vector4f> & p2)
59  {
60  double angle1 = atan2 (p1.second[1], p1.second[0]) + M_PI;
61  double angle2 = atan2 (p2.second[1], p2.second[0]) + M_PI;
62  return (angle1 > angle2);
63  }
64 
65  ////////////////////////////////////////////////////////////////////////////////////////////
66  /** \brief @b ConvexHull using libqhull library.
67  * \author Aitor Aldoma, Alex Trevor
68  * \ingroup surface
69  */
70  template<typename PointInT>
71  class ConvexHull : public MeshConstruction<PointInT>
72  {
73  protected:
78 
79  public:
80  typedef boost::shared_ptr<ConvexHull<PointInT> > Ptr;
81  typedef boost::shared_ptr<const ConvexHull<PointInT> > ConstPtr;
82 
84 
86  typedef typename PointCloud::Ptr PointCloudPtr;
88 
89  /** \brief Empty constructor. */
91  projection_angle_thresh_ (cos (0.174532925) ), qhull_flags ("qhull "),
92  x_axis_ (1.0, 0.0, 0.0), y_axis_ (0.0, 1.0, 0.0), z_axis_ (0.0, 0.0, 1.0)
93  {
94  };
95 
96  /** \brief Empty destructor */
97  virtual ~ConvexHull () {}
98 
99  /** \brief Compute a convex hull for all points given.
100  *
101  * \note In 2D case (i.e. if the input points belong to one plane)
102  * the \a polygons vector will have a single item, whereas in 3D
103  * case it will contain one item for each hull facet.
104  *
105  * \param[out] points the resultant points lying on the convex hull.
106  * \param[out] polygons the resultant convex hull polygons, as a set of
107  * vertices. The Vertices structure contains an array of point indices.
108  */
109  void
110  reconstruct (PointCloud &points,
111  std::vector<pcl::Vertices> &polygons);
112 
113  /** \brief Compute a convex hull for all points given.
114  * \param[out] points the resultant points lying on the convex hull.
115  */
116  void
117  reconstruct (PointCloud &points);
118 
119  /** \brief If set to true, the qhull library is called to compute the total area and volume of the convex hull.
120  * NOTE: When this option is activated, the qhull library produces output to the console.
121  * \param[in] value whether to compute the area and the volume, default is false
122  */
123  void
124  setComputeAreaVolume (bool value)
125  {
126  compute_area_ = value;
127  if (compute_area_)
128  qhull_flags = std::string ("qhull FA");
129  else
130  qhull_flags = std::string ("qhull ");
131  }
132 
133  /** \brief Returns the total area of the convex hull. */
134  double
135  getTotalArea () const
136  {
137  return (total_area_);
138  }
139 
140  /** \brief Returns the total volume of the convex hull. Only valid for 3-dimensional sets.
141  * For 2D-sets volume is zero.
142  */
143  double
144  getTotalVolume () const
145  {
146  return (total_volume_);
147  }
148 
149  /** \brief Sets the dimension on the input data, 2D or 3D.
150  * \param[in] dimension The dimension of the input data. If not set, this will be determined automatically.
151  */
152  void
153  setDimension (int dimension)
154  {
155  if ((dimension == 2) || (dimension == 3))
156  dimension_ = dimension;
157  else
158  PCL_ERROR ("[pcl::%s::setDimension] Invalid input dimension specified!\n", getClassName ().c_str ());
159  }
160 
161  /** \brief Returns the dimensionality (2 or 3) of the calculated hull. */
162  inline int
163  getDimension () const
164  {
165  return (dimension_);
166  }
167 
168  /** \brief Retrieve the indices of the input point cloud that for the convex hull.
169  *
170  * \note Should only be called after reconstruction was performed.
171  * \param[out] hull_point_indices The indices of the points forming the point cloud
172  */
173  void
174  getHullPointIndices (pcl::PointIndices &hull_point_indices) const;
175 
176  protected:
177  /** \brief The actual reconstruction method.
178  *
179  * \param[out] points the resultant points lying on the convex hull
180  * \param[out] polygons the resultant convex hull polygons, as a set of
181  * vertices. The Vertices structure contains an array of point indices.
182  * \param[in] fill_polygon_data true if polygons should be filled, false otherwise
183  */
184  void
185  performReconstruction (PointCloud &points,
186  std::vector<pcl::Vertices> &polygons,
187  bool fill_polygon_data = false);
188 
189  /** \brief The reconstruction method for 2D data. Does not require dimension to be set.
190  *
191  * \param[out] points the resultant points lying on the convex hull
192  * \param[out] polygons the resultant convex hull polygons, as a set of
193  * vertices. The Vertices structure contains an array of point indices.
194  * \param[in] fill_polygon_data true if polygons should be filled, false otherwise
195  */
196  void
197  performReconstruction2D (PointCloud &points,
198  std::vector<pcl::Vertices> &polygons,
199  bool fill_polygon_data = false);
200 
201  /** \brief The reconstruction method for 3D data. Does not require dimension to be set.
202  *
203  * \param[out] points the resultant points lying on the convex hull
204  * \param[out] polygons the resultant convex hull polygons, as a set of
205  * vertices. The Vertices structure contains an array of point indices.
206  * \param[in] fill_polygon_data true if polygons should be filled, false otherwise
207  */
208  void
209  performReconstruction3D (PointCloud &points,
210  std::vector<pcl::Vertices> &polygons,
211  bool fill_polygon_data = false);
212 
213  /** \brief A reconstruction method that returns a polygonmesh.
214  *
215  * \param[out] output a PolygonMesh representing the convex hull of the input data.
216  */
217  virtual void
219 
220  /** \brief A reconstruction method that returns the polygon of the convex hull.
221  *
222  * \param[out] polygons the polygon(s) representing the convex hull of the input data.
223  */
224  virtual void
225  performReconstruction (std::vector<pcl::Vertices> &polygons);
226 
227  /** \brief Automatically determines the dimension of input data - 2D or 3D. */
228  void
230 
231  /** \brief Class get name method. */
232  std::string
233  getClassName () const
234  {
235  return ("ConvexHull");
236  }
237 
238  /* \brief True if we should compute the area and volume of the convex hull. */
240 
241  /* \brief The area of the convex hull. */
242  double total_area_;
243 
244  /* \brief The volume of the convex hull (only for 3D hulls, zero for 2D). */
246 
247  /** \brief The dimensionality of the concave hull (2D or 3D). */
249 
250  /** \brief How close can a 2D plane's normal be to an axis to make projection problematic. */
252 
253  /** \brief Option flag string to be used calling qhull. */
254  std::string qhull_flags;
255 
256  /* \brief x-axis - for checking valid projections. */
257  const Eigen::Vector3d x_axis_;
258 
259  /* \brief y-axis - for checking valid projections. */
260  const Eigen::Vector3d y_axis_;
261 
262  /* \brief z-axis - for checking valid projections. */
263  const Eigen::Vector3d z_axis_;
264 
265  /* \brief vector containing the point cloud indices of the convex hull points. */
267 
268  public:
269  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
270  };
271 }
272 
273 #ifdef PCL_NO_PRECOMPILE
274 #include <pcl/surface/impl/convex_hull.hpp>
275 #endif
276 
277 #endif
int dimension_
The dimensionality of the concave hull (2D or 3D).
Definition: convex_hull.h:248
pcl::PointIndices hull_indices_
Definition: convex_hull.h:266
ConvexHull using libqhull library.
Definition: convex_hull.h:71
std::string qhull_flags
Option flag string to be used calling qhull.
Definition: convex_hull.h:254
void performReconstruction(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The actual reconstruction method.
boost::shared_ptr< ConvexHull< PointInT > > Ptr
Definition: convex_hull.h:80
double getTotalVolume() const
Returns the total volume of the convex hull.
Definition: convex_hull.h:144
const Eigen::Vector3d z_axis_
Definition: convex_hull.h:263
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
PointCloud::ConstPtr PointCloudConstPtr
Definition: convex_hull.h:87
std::string getClassName() const
Class get name method.
Definition: convex_hull.h:233
ConvexHull()
Empty constructor.
Definition: convex_hull.h:90
void calculateInputDimension()
Automatically determines the dimension of input data - 2D or 3D.
Definition: convex_hull.hpp:57
PointCloud::Ptr PointCloudPtr
Definition: convex_hull.h:86
boost::shared_ptr< PointCloud< PointInT > > Ptr
Definition: point_cloud.h:427
int getDimension() const
Returns the dimensionality (2 or 3) of the calculated hull.
Definition: convex_hull.h:163
void performReconstruction3D(PointCloud &points, std::vector< pcl::Vertices > &polygons, bool fill_polygon_data=false)
The reconstruction method for 3D data.
PCL base class.
Definition: pcl_base.h:68
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
boost::shared_ptr< const PointCloud< PointInT > > ConstPtr
Definition: point_cloud.h:428
void getHullPointIndices(pcl::PointIndices &hull_point_indices) const
Retrieve the indices of the input point cloud that for the convex hull.
virtual ~ConvexHull()
Empty destructor.
Definition: convex_hull.h:97
MeshConstruction represents a base surface reconstruction class.
pcl::PointCloud< PointInT > PointCloud
Definition: convex_hull.h:85
double total_volume_
Definition: convex_hull.h:245
double total_area_
Definition: convex_hull.h:242
void reconstruct(PointCloud &points, std::vector< pcl::Vertices > &polygons)
Compute a convex hull for all points given.
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:124
double projection_angle_thresh_
How close can a 2D plane&#39;s normal be to an axis to make projection problematic.
Definition: convex_hull.h:251
const Eigen::Vector3d x_axis_
Definition: convex_hull.h:257
void setDimension(int dimension)
Sets the dimension on the input data, 2D or 3D.
Definition: convex_hull.h:153
double getTotalArea() const
Returns the total area of the convex hull.
Definition: convex_hull.h:135
const Eigen::Vector3d y_axis_
Definition: convex_hull.h:260
boost::shared_ptr< const ConvexHull< PointInT > > ConstPtr
Definition: convex_hull.h:81
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:58