Point Cloud Library (PCL)  1.11.1
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 template <typename PointT, typename Function>
18 constexpr static bool is_functor_for_filter_v =
20  Function,
22  pcl::index_t>;
23 
24 /**
25  * \brief Filter point clouds and indices based on a functor passed in the ctor
26  * \ingroup filters
27  */
28 template <typename PointT, typename Functor>
29 class FunctorFilter : public FilterIndices<PointT> {
32 
33 public:
34  using FunctorT = Functor;
35  // using in type would complicate signature
36  static_assert(is_functor_for_filter_v<PointT, FunctorT>,
37  "Functor signature must be similar to `bool(const PointCloud<PointT>&, "
38  "index_t)`");
39 
40 protected:
42  using Base::filter_name_;
43  using Base::negative_;
45  using PCLBase::indices_;
46  using PCLBase::input_;
47 
48  // need to hold a value because functors can only be copy or move constructed
50 
51 public:
52  /** \brief Constructor.
53  * \param[in] extract_removed_indices Set to true if you want to be able to
54  * extract the indices of points being removed (default = false).
55  */
56  FunctorFilter(FunctorT functor, bool extract_removed_indices = false)
57  : Base(extract_removed_indices), functor_(std::move(functor))
58  {
59  filter_name_ = "functor_filter";
60  }
61 
62  const FunctorT&
63  getFunctor() const noexcept
64  {
65  return functor_;
66  }
67 
68  FunctorT&
69  getFunctor() noexcept
70  {
71  return functor_;
72  }
73 
74  /**
75  * \brief Filtered results are indexed by an indices array.
76  * \param[out] indices The resultant indices.
77  */
78  void
79  applyFilter(Indices& indices) override
80  {
81  indices.clear();
82  indices.reserve(input_->size());
84  removed_indices_->clear();
85  removed_indices_->reserve(input_->size());
86  }
87 
88  for (const auto index : *indices_) {
89  // functor returns true for points that should be selected
90  if (negative_ != functor_(*input_, index)) {
91  indices.push_back(index);
92  }
93  else if (extract_removed_indices_) {
94  removed_indices_->push_back(index);
95  }
96  }
97  }
98 };
99 } // namespace experimental
100 } // namespace pcl
pcl
Definition: convolution.h:46
pcl::experimental::FunctorFilter::FunctorFilter
FunctorFilter(FunctorT functor, bool extract_removed_indices=false)
Constructor.
Definition: functor_filter.h:56
pcl::PCLBase::input_
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
pcl::experimental::FunctorFilter::FunctorT
Functor FunctorT
Definition: functor_filter.h:34
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:73
pcl::experimental::FunctorFilter::functor_
FunctorT functor_
Definition: functor_filter.h:49
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:120
pcl::experimental::FunctorFilter::getFunctor
FunctorT & getFunctor() noexcept
Definition: functor_filter.h:69
pcl::Functor
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:648
pcl::Filter::removed_indices_
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:158
pcl::experimental::FunctorFilter::getFunctor
const FunctorT & getFunctor() const noexcept
Definition: functor_filter.h:63
pcl::FilterIndices
FilterIndices represents the base class for filters that are about binary point removal.
Definition: filter_indices.h:75
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:141
pcl::experimental::FunctorFilter
Filter point clouds and indices based on a functor passed in the ctor.
Definition: functor_filter.h:29
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition: filter.h:161
pcl::PCLBase::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: pcl_base.h:153
pcl::remove_cvref_t
std::remove_cv_t< std::remove_reference_t< T > > remove_cvref_t
Definition: type_traits.h:290
pcl::experimental::is_functor_for_filter_v
constexpr static bool is_functor_for_filter_v
Definition: functor_filter.h:18
pcl::FilterIndices::negative_
bool negative_
False = normal filter behavior (default), true = inverted behavior.
Definition: filter_indices.h:168
pcl::is_invocable_r_v
constexpr bool is_invocable_r_v
Definition: type_traits.h:277
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:164
pcl::experimental::FunctorFilter::applyFilter
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
Definition: functor_filter.h:79