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