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