Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
convex_hull.h
Go to the documentation of this file.
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: convex_hull.h 5026 2012-03-12 02:51:44Z rusu $
37  *
38  */
39 
40 #include <pcl/pcl_config.h>
41 #ifdef HAVE_QHULL
42 
43 #ifndef PCL_CONVEX_HULL_2D_H_
44 #define PCL_CONVEX_HULL_2D_H_
45 
46 // PCL includes
48 #include <pcl/ModelCoefficients.h>
49 #include <pcl/PolygonMesh.h>
50 
51 namespace pcl
52 {
58  inline bool
59  comparePoints2D (const std::pair<int, Eigen::Vector4f> & p1, const std::pair<int, Eigen::Vector4f> & p2)
60  {
61  double angle1 = atan2 (p1.second[1], p1.second[0]) + M_PI;
62  double angle2 = atan2 (p2.second[1], p2.second[0]) + M_PI;
63  return (angle1 > angle2);
64  }
65 
67 
71  template<typename PointInT>
72  class ConvexHull : public MeshConstruction<PointInT>
73  {
74  protected:
75  using PCLBase<PointInT>::input_;
76  using PCLBase<PointInT>::indices_;
77  using PCLBase<PointInT>::initCompute;
78  using PCLBase<PointInT>::deinitCompute;
79 
80  public:
81  using MeshConstruction<PointInT>::reconstruct;
82 
84  typedef typename PointCloud::Ptr PointCloudPtr;
86 
88  ConvexHull () : compute_area_ (false), total_area_ (0), total_volume_ (0), dimension_ (0),
89  projection_angle_thresh_ (cos (0.174532925) ), qhull_flags ("qhull "),
90  x_axis_ (1.0, 0.0, 0.0), y_axis_ (0.0, 1.0, 0.0), z_axis_ (0.0, 0.0, 1.0)
91  {
92  };
93 
100  void
101  reconstruct (PointCloud &points,
102  std::vector<pcl::Vertices> &polygons);
103 
107  void
108  reconstruct (PointCloud &output);
109 
114  void
115  setComputeAreaVolume (bool value)
116  {
117  compute_area_ = value;
118  if (compute_area_)
119  qhull_flags = std::string ("qhull FA");
120  else
121  qhull_flags = std::string ("qhull ");
122  }
123 
125  double
126  getTotalArea () const
127  {
128  return (total_area_);
129  }
130 
134  double
135  getTotalVolume () const
136  {
137  return (total_volume_);
138  }
139 
143  void
144  setDimension (int dimension)
145  {
146  if ((dimension == 2) || (dimension == 3))
147  dimension_ = dimension;
148  else
149  PCL_ERROR ("[pcl::%s::setDimension] Invalid input dimension specified!\n", getClassName ().c_str ());
150  }
151 
153  inline int
154  getDimension () const
155  {
156  return (dimension_);
157  }
158 
159  protected:
167  void
168  performReconstruction (PointCloud &points,
169  std::vector<pcl::Vertices> &polygons,
170  bool fill_polygon_data = false);
171 
179  void
180  performReconstruction2D (PointCloud &points,
181  std::vector<pcl::Vertices> &polygons,
182  bool fill_polygon_data = false);
183 
191  void
192  performReconstruction3D (PointCloud &points,
193  std::vector<pcl::Vertices> &polygons,
194  bool fill_polygon_data = false);
195 
200  virtual void
201  performReconstruction (PolygonMesh &output);
202 
207  virtual void
208  performReconstruction (std::vector<pcl::Vertices> &polygons);
209 
211  void
212  calculateInputDimension ();
213 
215  std::string
216  getClassName () const
217  {
218  return ("ConvexHull");
219  }
220 
221  /* \brief True if we should compute the area and volume of the convex hull. */
222  bool compute_area_;
223 
224  /* \brief The area of the convex hull. */
225  double total_area_;
226 
227  /* \brief The volume of the convex hull (only for 3D hulls, zero for 2D). */
228  double total_volume_;
229 
231  int dimension_;
232 
234  double projection_angle_thresh_;
235 
237  std::string qhull_flags;
238 
239  /* \brief x-axis - for checking valid projections. */
240  const Eigen::Vector3f x_axis_;
241 
242  /* \brief y-axis - for checking valid projections. */
243  const Eigen::Vector3f y_axis_;
244 
245  /* \brief z-axis - for checking valid projections. */
246  const Eigen::Vector3f z_axis_;
247 
248  public:
249  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
250  };
251 }
252 
253 #endif //#ifndef PCL_CONVEX_HULL_2D_H_
254 #endif