Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
functor_filter.h
Go to the documentation of this file.
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
17 #pragma once
18
19 #include <pcl/filters/filter_indices.h>
20 #include <pcl/point_traits.h> // for is_invocable
21 #include <limits>
22 #include "types.h"
23
24 namespace pcl {
25 namespace experimental {
26
32 template <typename PointT, typename Function>
33 constexpr static bool is_function_object_for_filter_v =
34 std::is_invocable_r_v<bool, Function, const PointCloud<PointT>&, index_t>;
35
36 namespace advanced {
44 template <typename PointT, typename FunctionObject>
45 class FunctorFilter : public FilterIndices<PointT> {
47 using PCL_Base = PCLBase<PointT>;
48
49 public:
50 using FunctionObjectT = FunctionObject;
51 // using in type would complicate signature
52 static_assert(is_function_object_for_filter_v<PointT, FunctionObjectT>,
53 "Function object signature must be similar to `bool(const "
54 "PointCloud<PointT>&, index_t)`");
55
56 protected:
57 using Base::extract_removed_indices_;
58 using Base::filter_name_;
59 using Base::negative_;
60 using Base::removed_indices_;
61 using PCL_Base::indices_;
62 using PCL_Base::input_;
63 bool keep_organized_ = false;
64 float user_filter_value_ = std::numeric_limits<float>::quiet_NaN();
65
66 private:
67 // need to hold a value because lambdas can only be copy or move constructed in C++14
69
70 public:
77 FunctorFilter(FunctionObjectT function_object, bool extract_removed_indices = false)
78 : Base(extract_removed_indices), functionObject_(std::move(function_object))
79 {
80 filter_name_ = "functor_filter";
81 }
82
83 const FunctionObjectT&
84 getFunctionObject() const noexcept
85 {
86 return functionObject_;
87 }
88
91 {
92 return functionObject_;
93 }
94
99 void
100 applyFilter(Indices& indices) override
101 {
102 indices.clear();
103 indices.reserve(indices_->size());
104 if (extract_removed_indices_) {
105 removed_indices_->clear();
106 removed_indices_->reserve(indices_->size());
107 }
108
109 for (const auto index : *indices_) {
110 // function object returns true for points that should be selected
111 if (negative_ != functionObject_(*input_, index)) {
112 indices.push_back(index);
113 }
114 else if (extract_removed_indices_) {
115 removed_indices_->push_back(index);
116 }
117 }
118 }
119
120 void applyFilter (PointCloud<PointT> &output) override
121 {
122 Indices indices;
123 if (keep_organized_)
124 {
125 if (!extract_removed_indices_)
126 {
127 PCL_WARN ("[pcl::FilterIndices<PointT>::applyFilter] extract_removed_indices_ was set to 'true' to keep the point cloud organized.\n");
128 extract_removed_indices_ = true;
129 }
130 applyFilter (indices);
131
132 output = *input_;
133
134 // To preserve legacy behavior, only coordinates xyz are filtered.
135 // Copying a PointXYZ initialized with the user_filter_value_ into a generic
136 // PointT, ensures only the xyz coordinates, if they exist at destination,
137 // are overwritten.
139 for (const auto ri : *removed_indices_) // ri = removed index
140 copyPoint(ufv, output[ri]);
141 if (!std::isfinite (user_filter_value_))
142 output.is_dense = false;
143 }
144 else
145 {
146 output.is_dense = true;
147 applyFilter (indices);
148 pcl::copyPointCloud (*input_, indices, output);
149 }
150 }
151
152 protected:
160 FunctorFilter(bool extract_removed_indices = false) : Base(extract_removed_indices)
161 {
162 filter_name_ = "functor_filter";
163 }
164
170 void
171 setFunctionObject(FunctionObjectT function_object) const noexcept
172 {
173 functionObject_ = std::move(function_object);
174 }
175 };
176 } // namespace advanced
177
178 template <class PointT>
179 using FilterFunction = std::function<bool(const PointCloud<PointT>&, index_t)>;
180
181 template <class PointT>
183 } // namespace experimental
184 } // namespace pcl
Filter point clouds and indices based on a function object passed in the ctor.
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
FunctorFilter(FunctionObjectT function_object, bool extract_removed_indices=false)
Constructor.
FunctionObjectT & getFunctionObject() noexcept
void applyFilter(PointCloud< PointT > &output) override
FunctorFilter(bool extract_removed_indices=false)
ctor to be used by derived classes with member function as FilterFunction
const FunctionObjectT & getFunctionObject() const noexcept
void setFunctionObject(FunctionObjectT function_object) const noexcept
utility function for derived class
std::function< bool(const PointCloud< PointT > &, index_t)> FilterFunction
static constexpr bool is_function_object_for_filter_v
Checks if the function object meets the usage in FunctorFilter class.
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:119