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