Point Cloud Library (PCL)  1.11.1-dev
functor_filter.h
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2020-, Open Perception
6  *
7  * All rights reserved
8  */
9 
10 #pragma once
11 
12 #include <pcl/filters/filter_indices.h>
13 #include <pcl/type_traits.h> // for is_invocable
14 
15 namespace pcl {
16 namespace experimental {
17 /**
18  * \brief Checks if the function object meets the usage in `FunctorFilter` class
19  * \details `Function` needs to be callable with a const reference to a PointCloud
20  * and an index value. The return type should be implicitly convertible to a boolean
21  */
22 template <typename PointT, typename Function>
23 constexpr static bool is_function_object_for_filter_v =
24  is_invocable_r_v<bool, Function, const PointCloud<PointT>&, index_t>;
25 
26 namespace advanced {
27 /**
28  * \brief Filter point clouds and indices based on a function object passed in the ctor
29  * \details The function object can be anything (lambda, std::function, invocable class,
30  * etc.) that can be moved into the class. Additionally, it must satisfy the condition
31  * `is_function_object_for_filter_v`
32  * \ingroup filters
33  */
34 template <typename PointT, typename FunctionObject>
35 class FunctorFilter : public FilterIndices<PointT> {
37  using PCL_Base = PCLBase<PointT>;
38 
39 public:
40  using FunctionObjectT = FunctionObject;
41  // using in type would complicate signature
42  static_assert(is_function_object_for_filter_v<PointT, FunctionObjectT>,
43  "Function object signature must be similar to `bool(const "
44  "PointCloud<PointT>&, index_t)`");
45 
46 protected:
48  using Base::filter_name_;
49  using Base::negative_;
51  using PCL_Base::indices_;
52  using PCL_Base::input_;
53 
54  // need to hold a value because lambdas can only be copy or move constructed in C++14
56 
57 public:
58  /** \brief Constructor.
59  * \param[in] extract_removed_indices Set to true if you want to be able to
60  * extract the indices of points being removed (default = false).
61  */
62  FunctorFilter(FunctionObjectT function_object, bool extract_removed_indices = false)
63  : Base(extract_removed_indices), functionObject_(std::move(function_object))
64  {
65  filter_name_ = "functor_filter";
66  }
67 
68  const FunctionObjectT&
69  getFunctionObject() const noexcept
70  {
71  return functionObject_;
72  }
73 
75  getFunctionObject() noexcept
76  {
77  return functionObject_;
78  }
79 
80  /**
81  * \brief Filtered results are indexed by an indices array.
82  * \param[out] indices The resultant indices.
83  */
84  void
85  applyFilter(Indices& indices) override
86  {
87  indices.clear();
88  indices.reserve(indices_->size());
90  removed_indices_->clear();
91  removed_indices_->reserve(indices_->size());
92  }
93 
94  for (const auto index : *indices_) {
95  // function object returns true for points that should be selected
96  if (negative_ != functionObject_(*input_, index)) {
97  indices.push_back(index);
98  }
99  else if (extract_removed_indices_) {
100  removed_indices_->push_back(index);
101  }
102  }
103  }
104 };
105 } // namespace advanced
106 
107 template <class PointT>
108 using FilterFunction = std::function<bool(const PointCloud<PointT>&, index_t)>;
109 
110 template <class PointT>
112 } // namespace experimental
113 } // namespace pcl
pcl
Definition: convolution.h:46
pcl::experimental::advanced::FunctorFilter::applyFilter
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
Definition: functor_filter.h:85
pcl::PCLBase::input_
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:147
pcl::experimental::FilterFunction
std::function< bool(const PointCloud< PointT > &, index_t)> FilterFunction
Definition: functor_filter.h:108
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:69
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::index_t
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:110
pcl::experimental::advanced::FunctorFilter
Filter point clouds and indices based on a function object passed in the ctor.
Definition: functor_filter.h:35
pcl::experimental::advanced::FunctorFilter::functionObject_
FunctionObjectT functionObject_
Definition: functor_filter.h:55
pcl::experimental::is_function_object_for_filter_v
constexpr static bool is_function_object_for_filter_v
Checks if the function object meets the usage in FunctorFilter class.
Definition: functor_filter.h:23
pcl::Filter::removed_indices_
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:155
pcl::FilterIndices
FilterIndices represents the base class for filters that are about binary point removal.
Definition: filter_indices.h:74
pcl::experimental::advanced::FunctorFilter::getFunctionObject
FunctionObjectT & getFunctionObject() noexcept
Definition: functor_filter.h:75
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::experimental::advanced::FunctorFilter::getFunctionObject
const FunctionObjectT & getFunctionObject() const noexcept
Definition: functor_filter.h:69
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition: filter.h:158
pcl::PCLBase::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: pcl_base.h:150
pcl::experimental::advanced::FunctorFilter::FunctionObjectT
FunctionObject FunctionObjectT
Definition: functor_filter.h:40
pcl::FilterIndices::negative_
bool negative_
False = normal filter behavior (default), true = inverted behavior.
Definition: filter_indices.h:168
pcl::experimental::advanced::FunctorFilter::FunctorFilter
FunctorFilter(FunctionObjectT function_object, bool extract_removed_indices=false)
Constructor.
Definition: functor_filter.h:62
pcl::Filter::extract_removed_indices_
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition: filter.h:161