Point Cloud Library (PCL)  1.8.1-dev
crop_hull.h
1  /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2011, 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 the copyright holder(s) 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  */
37 
38 #ifndef PCL_FILTERS_CROP_HULL_H_
39 #define PCL_FILTERS_CROP_HULL_H_
40 
41 #include <pcl/point_types.h>
42 #include <pcl/Vertices.h>
43 #include <pcl/filters/filter_indices.h>
44 
45 namespace pcl
46 {
47  /** \brief Filter points that lie inside or outside a 3D closed surface or 2D
48  * closed polygon, as generated by the ConvexHull or ConcaveHull classes.
49  * \author James Crosby
50  * \ingroup filters
51  */
52  template<typename PointT>
53  class CropHull: public FilterIndices<PointT>
54  {
58 
59  typedef typename Filter<PointT>::PointCloud PointCloud;
60  typedef typename PointCloud::Ptr PointCloudPtr;
62 
63  public:
64 
65  typedef boost::shared_ptr< CropHull<PointT> > Ptr;
66  typedef boost::shared_ptr< const CropHull<PointT> > ConstPtr;
67 
68  /** \brief Empty Constructor. */
69  CropHull () :
70  hull_polygons_(),
71  hull_cloud_(),
72  dim_(3),
73  crop_outside_(true)
74  {
75  filter_name_ = "CropHull";
76  }
77 
78  /** \brief Set the vertices of the hull used to filter points.
79  * \param[in] polygons Vector of polygons (Vertices structures) forming
80  * the hull used for filtering points.
81  */
82  inline void
83  setHullIndices (const std::vector<Vertices>& polygons)
84  {
85  hull_polygons_ = polygons;
86  }
87 
88  /** \brief Get the vertices of the hull used to filter points.
89  */
90  std::vector<Vertices>
91  getHullIndices () const
92  {
93  return (hull_polygons_);
94  }
95 
96  /** \brief Set the point cloud that the hull indices refer to
97  * \param[in] points the point cloud that the hull indices refer to
98  */
99  inline void
101  {
102  hull_cloud_ = points;
103  }
104 
105  /** \brief Get the point cloud that the hull indices refer to. */
106  PointCloudPtr
107  getHullCloud () const
108  {
109  return (hull_cloud_);
110  }
111 
112  /** \brief Set the dimensionality of the hull to be used.
113  * This should be set to correspond to the dimensionality of the
114  * convex/concave hull produced by the pcl::ConvexHull and
115  * pcl::ConcaveHull classes.
116  * \param[in] dim Dimensionailty of the hull used to filter points.
117  */
118  inline void
119  setDim (int dim)
120  {
121  dim_ = dim;
122  }
123 
124  /** \brief Remove points outside the hull (default), or those inside the hull.
125  * \param[in] crop_outside If true, the filter will remove points
126  * outside the hull. If false, those inside will be removed.
127  */
128  inline void
129  setCropOutside(bool crop_outside)
130  {
131  crop_outside_ = crop_outside;
132  }
133 
134  protected:
135  /** \brief Filter the input points using the 2D or 3D polygon hull.
136  * \param[out] output The set of points that passed the filter
137  */
138  void
139  applyFilter (PointCloud &output);
140 
141  /** \brief Filter the input points using the 2D or 3D polygon hull.
142  * \param[out] indices the indices of the set of points that passed the filter.
143  */
144  void
145  applyFilter (std::vector<int> &indices);
146 
147  private:
148  /** \brief Return the size of the hull point cloud in line with coordinate axes.
149  * This is used to choose the 2D projection to use when cropping to a 2d
150  * polygon.
151  */
152  Eigen::Vector3f
153  getHullCloudRange ();
154 
155  /** \brief Apply the two-dimensional hull filter.
156  * All points are assumed to lie in the same plane as the 2D hull, an
157  * axis-aligned 2D coordinate system using the two dimensions specified
158  * (PlaneDim1, PlaneDim2) is used for calculations.
159  * \param[out] output The set of points that pass the 2D polygon filter.
160  */
161  template<unsigned PlaneDim1, unsigned PlaneDim2> void
162  applyFilter2D (PointCloud &output);
163 
164  /** \brief Apply the two-dimensional hull filter.
165  * All points are assumed to lie in the same plane as the 2D hull, an
166  * axis-aligned 2D coordinate system using the two dimensions specified
167  * (PlaneDim1, PlaneDim2) is used for calculations.
168  * \param[out] indices The indices of the set of points that pass the
169  * 2D polygon filter.
170  */
171  template<unsigned PlaneDim1, unsigned PlaneDim2> void
172  applyFilter2D (std::vector<int> &indices);
173 
174  /** \brief Apply the three-dimensional hull filter.
175  * Polygon-ray crossings are used for three rays cast from each point
176  * being tested, and a majority vote of the resulting
177  * polygon-crossings is used to decide whether the point lies inside
178  * or outside the hull.
179  * \param[out] output The set of points that pass the 3D polygon hull
180  * filter.
181  */
182  void
183  applyFilter3D (PointCloud &output);
184 
185  /** \brief Apply the three-dimensional hull filter.
186  * Polygon-ray crossings are used for three rays cast from each point
187  * being tested, and a majority vote of the resulting
188  * polygon-crossings is used to decide whether the point lies inside
189  * or outside the hull.
190  * \param[out] indices The indices of the set of points that pass the 3D
191  * polygon hull filter.
192  */
193  void
194  applyFilter3D (std::vector<int> &indices);
195 
196  /** \brief Test an individual point against a 2D polygon.
197  * PlaneDim1 and PlaneDim2 specify the x/y/z coordinate axes to use.
198  * \param[in] point Point to test against the polygon.
199  * \param[in] verts Vertex indices of polygon.
200  * \param[in] cloud Cloud from which the vertex indices are drawn.
201  */
202  template<unsigned PlaneDim1, unsigned PlaneDim2> inline static bool
203  isPointIn2DPolyWithVertIndices (const PointT& point,
204  const Vertices& verts,
205  const PointCloud& cloud);
206 
207  /** \brief Does a ray cast from a point intersect with an arbitrary
208  * triangle in 3D?
209  * See: http://softsurfer.com/Archive/algorithm_0105/algorithm_0105.htm#intersect_RayTriangle()
210  * \param[in] point Point from which the ray is cast.
211  * \param[in] ray Vector in direction of ray.
212  * \param[in] verts Indices of vertices making the polygon.
213  * \param[in] cloud Cloud from which the vertex indices are drawn.
214  */
215  inline static bool
216  rayTriangleIntersect (const PointT& point,
217  const Eigen::Vector3f& ray,
218  const Vertices& verts,
219  const PointCloud& cloud);
220 
221 
222  /** \brief The vertices of the hull used to filter points. */
223  std::vector<pcl::Vertices> hull_polygons_;
224 
225  /** \brief The point cloud that the hull indices refer to. */
226  PointCloudPtr hull_cloud_;
227 
228  /** \brief The dimensionality of the hull to be used. */
229  int dim_;
230 
231  /** \brief If true, the filter will remove points outside the hull. If
232  * false, those inside will be removed.
233  */
234  bool crop_outside_;
235  };
236 
237 } // namespace pcl
238 
239 #ifdef PCL_NO_PRECOMPILE
240 #include <pcl/filters/impl/crop_hull.hpp>
241 #endif
242 
243 #endif // ifndef PCL_FILTERS_CROP_HULL_H_
void applyFilter(PointCloud &output)
Filter the input points using the 2D or 3D polygon hull.
Definition: crop_hull.hpp:45
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:73
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
Filter points that lie inside or outside a 3D closed surface or 2D closed polygon, as generated by the ConvexHull or ConcaveHull classes.
Definition: crop_hull.h:53
void setHullCloud(PointCloudPtr points)
Set the point cloud that the hull indices refer to.
Definition: crop_hull.h:100
std::vector< Vertices > getHullIndices() const
Get the vertices of the hull used to filter points.
Definition: crop_hull.h:91
Describes a set of vertices in a polygon mesh, by basically storing an array of indices.
Definition: Vertices.h:14
FilterIndices represents the base class for filters that are about binary point removal.
PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:72
Filter represents the base filter class.
Definition: filter.h:84
void setCropOutside(bool crop_outside)
Remove points outside the hull (default), or those inside the hull.
Definition: crop_hull.h:129
void setDim(int dim)
Set the dimensionality of the hull to be used.
Definition: crop_hull.h:119
void setHullIndices(const std::vector< Vertices > &polygons)
Set the vertices of the hull used to filter points.
Definition: crop_hull.h:83
boost::shared_ptr< const CropHull< PointT > > ConstPtr
Definition: crop_hull.h:66
boost::shared_ptr< CropHull< PointT > > Ptr
Definition: crop_hull.h:65
std::string filter_name_
The filter name.
Definition: filter.h:166
A point structure representing Euclidean xyz coordinates, and the RGB color.
CropHull()
Empty Constructor.
Definition: crop_hull.h:69
PointCloudPtr getHullCloud() const
Get the point cloud that the hull indices refer to.
Definition: crop_hull.h:107