Loading...
Searching...
No Matches
functor_filter.h
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2020-, Open Perception
*
* All rights reserved
*/
#pragma once
#include <pcl/filters/filter_indices.h>
#include <pcl/type_traits.h> // for is_invocable
namespace pcl {
namespace experimental {
/**
* \brief Checks if the function object meets the usage in `FunctorFilter` class
* \details `Function` needs to be callable with a const reference to a PointCloud
* and an index value. The return type should be implicitly convertible to a boolean
*/
template <typename PointT, typename Function>
constexpr static bool is_function_object_for_filter_v =
is_invocable_r_v<bool, Function, const PointCloud<PointT>&, index_t>;
namespace advanced {
/**
* \brief Filter point clouds and indices based on a function object passed in the ctor
* \details The function object can be anything (lambda, std::function, invocable class,
* etc.) that can be moved into the class. Additionally, it must satisfy the condition
* `is_function_object_for_filter_v`
* \ingroup filters
*/
template <typename PointT, typename FunctionObject>
class FunctorFilter : public FilterIndices<PointT> {
using Base = FilterIndices<PointT>;
using PCL_Base = PCLBase<PointT>;
public:
using FunctionObjectT = FunctionObject;
// using in type would complicate signature
static_assert(is_function_object_for_filter_v<PointT, FunctionObjectT>,
"Function object signature must be similar to `bool(const "
"PointCloud<PointT>&, index_t)`");
protected:
using Base::extract_removed_indices_;
using Base::filter_name_;
using Base::negative_;
using Base::removed_indices_;
using PCL_Base::indices_;
using PCL_Base::input_;
private:
// need to hold a value because lambdas can only be copy or move constructed in C++14
FunctionObjectT functionObject_;
public:
/** \brief Constructor.
* \param[in] function_object Object of effective type `FilterFunction` in order to
* filter out the indices for which it returns false
* \param[in] extract_removed_indices Set to true if you want to be able to
* extract the indices of points being removed (default = false).
*/
FunctorFilter(FunctionObjectT function_object, bool extract_removed_indices = false)
: Base(extract_removed_indices), functionObject_(std::move(function_object))
{
filter_name_ = "functor_filter";
}
const FunctionObjectT&
getFunctionObject() const noexcept
{
return functionObject_;
}
FunctionObjectT&
getFunctionObject() noexcept
{
return functionObject_;
}
/**
* \brief Filtered results are indexed by an indices array.
* \param[out] indices The resultant indices.
*/
void
applyFilter(Indices& indices) override
{
indices.clear();
indices.reserve(indices_->size());
if (extract_removed_indices_) {
removed_indices_->clear();
removed_indices_->reserve(indices_->size());
}
for (const auto index : *indices_) {
// function object returns true for points that should be selected
if (negative_ != functionObject_(*input_, index)) {
indices.push_back(index);
}
else if (extract_removed_indices_) {
removed_indices_->push_back(index);
}
}
}
protected:
/**
* \brief ctor to be used by derived classes with member function as FilterFunction
* \param[in] extract_removed_indices Set to true if you want to be able to
* extract the indices of points being removed (default = false).
* \note The class would be ill-defined until `setFunctionObject` has been called
* Do not call any filter routine until then
*/
FunctorFilter(bool extract_removed_indices = false) : Base(extract_removed_indices)
{
filter_name_ = "functor_filter";
}
/**
* \brief utility function for derived class
* \param[in] function_object Object of effective type `FilterFunction` in order to
* filter out the indices for which it returns false
*/
void
setFunctionObject(FunctionObjectT function_object) const noexcept
{
functionObject_ = std::move(function_object);
}
};
} // namespace advanced
template <class PointT>
using FilterFunction = std::function<bool(const PointCloud<PointT>&, index_t)>;
template <class PointT>
using FunctionFilter = advanced::FunctorFilter<PointT, FilterFunction<PointT>>;
} // namespace experimental
} // namespace pcl
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
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition filter.h:158
pcl::Filter::removed_indices_
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition filter.h:155
pcl::FilterIndices::FilterIndices
FilterIndices(bool extract_removed_indices=false)
Constructor.
Definition filter_indices.h:87
pcl::FilterIndices::negative_
bool negative_
False = normal filter behavior (default), true = inverted behavior.
Definition filter_indices.h:167
pcl::PCLBase::input_
PointCloudConstPtr input_
The input point cloud dataset.
Definition pcl_base.h:147
pcl::PCLBase::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition pcl_base.h:150
pcl::PCLBase::PCLBase
PCLBase()
Empty constructor.
Definition pcl_base.hpp:46
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition point_cloud.h:174
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::applyFilter
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
Definition functor_filter.h:88
pcl::experimental::advanced::FunctorFilter< PointT, FilterFunction< PointT > >::input_
PointCloudConstPtr input_
pcl::experimental::advanced::FunctorFilter::FunctorFilter
FunctorFilter(FunctionObjectT function_object, bool extract_removed_indices=false)
Constructor.
Definition functor_filter.h:65
pcl::experimental::advanced::FunctorFilter::getFunctionObject
FunctionObjectT & getFunctionObject() noexcept
Definition functor_filter.h:78
pcl::experimental::advanced::FunctorFilter::FunctorFilter
FunctorFilter(bool extract_removed_indices=false)
ctor to be used by derived classes with member function as FilterFunction
Definition functor_filter.h:116
pcl::experimental::advanced::FunctorFilter< PointT, FilterFunction< PointT > >::negative_
bool negative_
pcl::experimental::advanced::FunctorFilter::getFunctionObject
const FunctionObjectT & getFunctionObject() const noexcept
Definition functor_filter.h:72
pcl::experimental::advanced::FunctorFilter< PointT, FilterFunction< PointT > >::indices_
IndicesPtr indices_
pcl::experimental::advanced::FunctorFilter::setFunctionObject
void setFunctionObject(FunctionObjectT function_object) const noexcept
utility function for derived class
Definition functor_filter.h:127
pcl::experimental::advanced::FunctorFilter::FunctionObjectT
FunctionObject FunctionObjectT
Definition functor_filter.h:40
pcl::experimental::advanced
Definition functor_filter.h:26
pcl::experimental
Definition functor_filter.h:16
pcl::experimental::FunctionFilter
advanced::FunctorFilter< PointT, FilterFunction< PointT > > FunctionFilter
Definition functor_filter.h:138
pcl::experimental::FilterFunction
std::function< bool(const PointCloud< PointT > &, index_t)> FilterFunction
Definition functor_filter.h:135
pcl::experimental::is_function_object_for_filter_v
static constexpr bool is_function_object_for_filter_v
Checks if the function object meets the usage in FunctorFilter class.
Definition functor_filter.h:23
pcl
Definition convolution.h:46
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:112
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
pcl::is_invocable_r_v
constexpr bool is_invocable_r_v
Definition type_traits.h:285