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