Point Cloud Library (PCL)  1.14.1-dev
convolution.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/point_cloud.h>
43 
44 namespace pcl
45 {
46  namespace filters
47  {
48  /** Convolution is a mathematical operation on two functions f and g,
49  * producing a third function that is typically viewed as a modified
50  * version of one of the original functions.
51  * see https://en.wikipedia.org/wiki/Convolution.
52  *
53  * The class provides rows, column and separate convolving operations
54  * of a point cloud.
55  * Columns and separate convolution is only allowed on organised
56  * point clouds.
57  *
58  * When convolving, computing the rows and cols elements at 1/2 kernel
59  * width distance from the borders is not defined. We allow for 3
60  * policies:
61  * - Ignoring: elements at special locations are filled with zero
62  * (default behaviour)
63  * - Mirroring: the missing rows or columns are obtained through mirroring
64  * - Duplicating: the missing rows or columns are obtained through
65  * duplicating
66  *
67  * \author Nizar Sallem
68  * \ingroup filters
69  */
70 
71  template <typename PointIn, typename PointOut>
73  {
74  public:
79  using Ptr = shared_ptr< Convolution<PointIn, PointOut> >;
80  using ConstPtr = shared_ptr< const Convolution<PointIn, PointOut> >;
81 
82 
83  /// The borders policy available
85  {
89  };
90  /// Constructor
91  Convolution ();
92  /// Empty destructor
93  ~Convolution () = default;
94  /** \brief Provide a pointer to the input dataset
95  * \param cloud the const boost shared pointer to a PointCloud message
96  * \remark Will perform a deep copy
97  */
98  inline void
99  setInputCloud (const PointCloudInConstPtr& cloud) { input_ = cloud; }
100  /** Set convolving kernel
101  * \param[in] kernel convolving element
102  */
103  inline void
104  setKernel (const Eigen::ArrayXf& kernel) { kernel_ = kernel; }
105  /// Set the borders policy
106  void
107  setBordersPolicy (int policy) { borders_policy_ = policy; }
108  /// Get the borders policy
109  int
110  getBordersPolicy () { return (borders_policy_); }
111  /** \remark this is critical so please read it carefully.
112  * In 3D the next point in (u,v) coordinate can be really far so a distance
113  * threshold is used to keep us from ghost points.
114  * The value you set here is strongly related to the sensor. A good value for
115  * kinect data is 0.001. Default is std::numeric<float>::infinity ()
116  * \param[in] threshold maximum allowed distance between 2 juxtaposed points
117  */
118  inline void
119  setDistanceThreshold (const float& threshold) { distance_threshold_ = threshold; }
120  /// \return the distance threshold
121  inline const float &
122  getDistanceThreshold () const { return (distance_threshold_); }
123  /** \brief Initialize the scheduler and set the number of threads to use.
124  * \param nr_threads the number of hardware threads to use (0 sets the value back to automatic)
125  */
126  inline void
127  setNumberOfThreads (unsigned int nr_threads = 0) { threads_ = nr_threads; }
128  /** Convolve a float image rows by a given kernel.
129  * \param[out] output the convolved cloud
130  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
131  * output.cols () < input.cols () then output is resized to input sizes.
132  */
133  inline void
134  convolveRows (PointCloudOut& output);
135  /** Convolve a float image columns by a given kernel.
136  * \param[out] output the convolved image
137  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
138  * output.cols () < input.cols () then output is resized to input sizes.
139  */
140  inline void
141  convolveCols (PointCloudOut& output);
142  /** Convolve point cloud with an horizontal kernel along rows
143  * then vertical kernel along columns : convolve separately.
144  * \param[in] h_kernel kernel for convolving rows
145  * \param[in] v_kernel kernel for convolving columns
146  * \param[out] output the convolved cloud
147  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
148  * output.cols () < input.cols () then output is resized to input sizes.
149  */
150  inline void
151  convolve (const Eigen::ArrayXf& h_kernel, const Eigen::ArrayXf& v_kernel, PointCloudOut& output);
152  /** Convolve point cloud with same kernel along rows and columns separately.
153  * \param[out] output the convolved cloud
154  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
155  * output.cols () < input.cols () then output is resized to input sizes.
156  */
157  inline void
158  convolve (PointCloudOut& output);
159 
160  protected:
161  /// \brief convolve rows and ignore borders
162  void
163  convolve_rows (PointCloudOut& output);
164  /// \brief convolve cols and ignore borders
165  void
166  convolve_cols (PointCloudOut& output);
167  /// \brief convolve rows and mirror borders
168  void
170  /// \brief convolve cols and mirror borders
171  void
173  /// \brief convolve rows and duplicate borders
174  void
176  /// \brief convolve cols and duplicate borders
177  void
179  /** init compute is an internal method called before computation
180  * \param[in] output
181  * \throw pcl::InitFailedException
182  */
183  void
184  initCompute (PointCloudOut& output);
185  private:
186  /** \return the result of convolution of point at (\ai, \aj)
187  * \note no test on finity is performed
188  */
189  inline PointOut
190  convolveOneRowDense (int i, int j);
191  /** \return the result of convolution of point at (\ai, \aj)
192  * \note no test on finity is performed
193  */
194  inline PointOut
195  convolveOneColDense (int i, int j);
196  /** \return the result of convolution of point at (\ai, \aj)
197  * \note only finite points within \a distance_threshold_ are accounted
198  */
199  inline PointOut
200  convolveOneRowNonDense (int i, int j);
201  /** \return the result of convolution of point at (\ai, \aj)
202  * \note only finite points within \a distance_threshold_ are accounted
203  */
204  inline PointOut
205  convolveOneColNonDense (int i, int j);
206 
207  /// Border policy
208  int borders_policy_;
209  /// Threshold distance between adjacent points
210  float distance_threshold_;
211  /// Pointer to the input cloud
212  PointCloudInConstPtr input_;
213  /// convolution kernel
214  Eigen::ArrayXf kernel_;
215  /// half kernel size
216  int half_width_{};
217  /// kernel size - 1
218  int kernel_width_{};
219  protected:
220  /** \brief The number of threads the scheduler should use. */
221  unsigned int threads_{1};
222 
223  void
224  makeInfinite (PointOut& p)
225  {
226  p.x = p.y = p.z = std::numeric_limits<float>::quiet_NaN ();
227  }
228  };
229  }
230 }
231 
232 #include <pcl/filters/impl/convolution.hpp>
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
Convolution is a mathematical operation on two functions f and g, producing a third function that is ...
Definition: convolution.h:73
const float & getDistanceThreshold() const
Definition: convolution.h:122
typename PointCloudIn::Ptr PointCloudInPtr
Definition: convolution.h:76
void convolveCols(PointCloudOut &output)
Convolve a float image columns by a given kernel.
void setInputCloud(const PointCloudInConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: convolution.h:99
pcl::PointCloud< PointOut > PointCloudOut
Definition: convolution.h:78
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
Definition: convolution.h:127
void convolve_rows_mirror(PointCloudOut &output)
convolve rows and mirror borders
void makeInfinite(PointOut &p)
Definition: convolution.h:224
shared_ptr< Convolution< PointIn, PointOut > > Ptr
Definition: convolution.h:79
typename PointCloudIn::ConstPtr PointCloudInConstPtr
Definition: convolution.h:77
int getBordersPolicy()
Get the borders policy.
Definition: convolution.h:110
~Convolution()=default
Empty destructor.
void convolve_cols_duplicate(PointCloudOut &output)
convolve cols and duplicate borders
void convolve_rows_duplicate(PointCloudOut &output)
convolve rows and duplicate borders
void convolveRows(PointCloudOut &output)
Convolve a float image rows by a given kernel.
Definition: convolution.hpp:92
void convolve_cols(PointCloudOut &output)
convolve cols and ignore borders
void convolve_cols_mirror(PointCloudOut &output)
convolve cols and mirror borders
void convolve_rows(PointCloudOut &output)
convolve rows and ignore borders
void convolve(const Eigen::ArrayXf &h_kernel, const Eigen::ArrayXf &v_kernel, PointCloudOut &output)
Convolve point cloud with an horizontal kernel along rows then vertical kernel along columns : convol...
void setDistanceThreshold(const float &threshold)
Definition: convolution.h:119
shared_ptr< const Convolution< PointIn, PointOut > > ConstPtr
Definition: convolution.h:80
void setBordersPolicy(int policy)
Set the borders policy.
Definition: convolution.h:107
void initCompute(PointCloudOut &output)
init compute is an internal method called before computation
Definition: convolution.hpp:61
void setKernel(const Eigen::ArrayXf &kernel)
Set convolving kernel.
Definition: convolution.h:104
unsigned int threads_
The number of threads the scheduler should use.
Definition: convolution.h:221
BORDERS_POLICY
The borders policy available.
Definition: convolution.h:85