iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PassThroughFilter.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifdef NRT_HAVE_CLOUD
36 #ifndef INCLUDE_NRT_POINTCLOUD2_FILTER_PASSTHROUGHFILTER_H
37 #define INCLUDE_NRT_POINTCLOUD2_FILTER_PASSTHROUGHFILTER_H
38 
40 
41 namespace nrt
42 {
43  //! A filter that passes through points that satisfy some criterion
44  /*! Pass through filters allow a user defined function to choose which points
45  are allowed to be passed through the filter. In addition, the filter can
46  either invert its output, passing through those that fail a test, or it
47  can choose to replace values that pass some criterion instead of passing them
48  through the filter.
49 
50  A pass through filter does not allow for a call to filter() with no additional arguments.
51  A criterion function, along with an optional value to use in replacement, are required.
52 
53  Criterion functions must take in the field type under consideration and return true
54  if the parameter passes some test, or false if it fails.
55 
56  An example of such a function that passes through geometry data that falls within
57  some range could look like:
58 
59  @code
60  PointCloud2 myCloud;
61  auto filtered = filterPointCloud( myCloud, PassThroughFilter(), [](PointCloud2::Geometry const & p){ return p.x() >= 0.0f && p.x() <= 1.0f );
62  @endcode
63 
64  The above code will filter a point cloud with a pass through filter, letting through any point with an x value in the range [0.0, 1.0].
65 
66  Criterion functions can be as complicated as you wish, so long as they only operate on one point at a time
67  @ingroup pointcloud2filters */
69  {
70  public:
71  //! Create a pass through filter that lets through only certain points
72  /*! @param replaceValue Whether we will replace (true) or remove (false) points
73  @param invertResult Whether we we should invert the result of the pass through function */
74  PassThroughFilter( bool replaceValue = false, bool invertResult = false );
75 
76  //! Filter the input, applying func to every point to see what to do
77  /*! @param input The cloud to filter
78  @param func The filtering function/functor
79  @param replaceValue The value to use for replacement, if enabled
80  @return The filtered point cloud */
81  PointCloud2 filter( PointCloud2 const input,
82  std::function<bool(PointCloud2::Geometry const &)> && func,
83  PointCloud2::Geometry const & replaceValue = PointCloud2::Geometry() );
84 
85  //! Filter the input, applying func to every point in a subset to see what to do
86  /*! @param input The cloud to filter
87  @param indices The subset to operate on
88  @param func The filtering function/functor
89  @param replaceValue The value to use for replacement, if enabled
90  @return The filtered point cloud */
91  PointCloud2 filter( PointCloud2 const input,
92  Indices const indices,
93  std::function<bool(PointCloud2::Geometry const &)> && func,
94  PointCloud2::Geometry const & replaceValue = PointCloud2::Geometry() );
95 
96  //! Filter the input on a specific field, applying func to every point to see what to do
97  /*! @tparam Field The specific field to filter
98  @param input The cloud to filter
99  @param func The filtering function/functor
100  @param replaceValue The value to use for replacement, if enabled
101  @return The filtered point cloud */
102  template <class Field>
103  PointCloud2 filter( PointCloud2 const input,
104  std::function<bool(Field const &)> && func,
105  Field const & replaceValue = Field() );
106 
107  //! Filter the input on a specific field, applying func to every point in a subset to see what to do
108  /*! @tparam Field The specific field to filter
109  @param input The cloud to filter
110  @param indices The subset to filter
111  @param func The filtering function/functor
112  @param replaceValue The value to use for replacement, if enabled
113  @return The filtered point cloud */
114  template <class Field>
115  PointCloud2 filter( PointCloud2 const input,
116  Indices const indices,
117  std::function<bool(Field const &)> && func,
118  Field const & replaceValue = Field() );
119 
120  protected:
121  //! Normal filtering not supported, must specify additional arguments
122  PointCloud2 filter( PointCloud2 const input );
123 
124  //! Normal filtering not supported, must specify additional arguments
125  PointCloud2 filter( PointCloud2 const input, Indices const indices );
126 
127  //! Figures out which indices we'll have to remove, if any
128  /*! @param inputSize the size of the input
129  @param outputEnd end iterator for the output
130  @param func The filtering function
131  @param replaceValue The replacement value
132  @return The indices to remove */
133  Indices filterImpl( size_t const inputSize,
134  PointCloud2::Iterator<> outputEnd,
135  std::function<bool(PointCloud2::Geometry const &)> && func,
136  PointCloud2::Geometry const & replaceValue );
137 
138  //! Figures out which indices we'll have to remove, if any, templated version
139  /*! @tparam The field to filter
140  @param inputSize the size of the input
141  @param outputEnd end iterator for the output
142  @param func The filtering function
143  @param replaceValue The replacement value
144  @return The indices to remove */
145  template <class Field>
146  Indices filterImpl( size_t const inputSize,
148  std::function<bool(Field const &)> && func,
149  Field const & replaceValue );
150 
151  private:
152  bool itsReplaceValue;
153  bool itsInvertResult;
154  }; // class PassThroughFilter
155 
156 } // namespace nrt
157 
159 
160 #endif // INCLUDE_NRT_POINTCLOUD_FILTER_PASSTHROUGHFILTER_H
161 #endif // NRT_HAVE_CLOUD