Point Cloud Library (PCL)  1.11.0
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/common/eigen.h>
43 #include <pcl/point_cloud.h>
44 #include <pcl/exceptions.h>
45 #include <pcl/pcl_base.h>
46 
47 namespace pcl
48 {
49  namespace filters
50  {
51  /** Convolution is a mathematical operation on two functions f and g,
52  * producing a third function that is typically viewed as a modified
53  * version of one of the original functions.
54  * see http://en.wikipedia.org/wiki/Convolution.
55  *
56  * The class provides rows, column and separate convolving operations
57  * of a point cloud.
58  * Columns and separate convolution is only allowed on organised
59  * point clouds.
60  *
61  * When convolving, computing the rows and cols elements at 1/2 kernel
62  * width distance from the borders is not defined. We allow for 3
63  * policies:
64  * - Ignoring: elements at special locations are filled with zero
65  * (default behaviour)
66  * - Mirroring: the missing rows or columns are obtained through mirroring
67  * - Duplicating: the missing rows or columns are obtained through
68  * duplicating
69  *
70  * \author Nizar Sallem
71  * \ingroup filters
72  */
73 
74  template <typename PointIn, typename PointOut>
76  {
77  public:
82  using Ptr = shared_ptr< Convolution<PointIn, PointOut> >;
83  using ConstPtr = shared_ptr< const Convolution<PointIn, PointOut> >;
84 
85 
86  /// The borders policy available
88  {
92  };
93  /// Constructor
94  Convolution ();
95  /// Empty destructor
97  /** \brief Provide a pointer to the input dataset
98  * \param cloud the const boost shared pointer to a PointCloud message
99  * \remark Will perform a deep copy
100  */
101  inline void
102  setInputCloud (const PointCloudInConstPtr& cloud) { input_ = cloud; }
103  /** Set convolving kernel
104  * \param[in] kernel convolving element
105  */
106  inline void
107  setKernel (const Eigen::ArrayXf& kernel) { kernel_ = kernel; }
108  /// Set the borders policy
109  void
110  setBordersPolicy (int policy) { borders_policy_ = policy; }
111  /// Get the borders policy
112  int
113  getBordersPolicy () { return (borders_policy_); }
114  /** \remark this is critical so please read it carefully.
115  * In 3D the next point in (u,v) coordinate can be really far so a distance
116  * threshold is used to keep us from ghost points.
117  * The value you set here is strongly related to the sensor. A good value for
118  * kinect data is 0.001. Default is std::numeric<float>::infinity ()
119  * \param[in] threshold maximum allowed distance between 2 juxtaposed points
120  */
121  inline void
122  setDistanceThreshold (const float& threshold) { distance_threshold_ = threshold; }
123  /// \return the distance threshold
124  inline const float &
125  getDistanceThreshold () const { return (distance_threshold_); }
126  /** \brief Initialize the scheduler and set the number of threads to use.
127  * \param nr_threads the number of hardware threads to use (0 sets the value back to automatic)
128  */
129  inline void
130  setNumberOfThreads (unsigned int nr_threads = 0) { threads_ = nr_threads; }
131  /** Convolve a float image rows by a given kernel.
132  * \param[out] output the convolved cloud
133  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
134  * output.cols () < input.cols () then output is resized to input sizes.
135  */
136  inline void
137  convolveRows (PointCloudOut& output);
138  /** Convolve a float image columns by a given kernel.
139  * \param[out] output the convolved image
140  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
141  * output.cols () < input.cols () then output is resized to input sizes.
142  */
143  inline void
144  convolveCols (PointCloudOut& output);
145  /** Convolve point cloud with an horizontal kernel along rows
146  * then vertical kernel along columns : convolve separately.
147  * \param[in] h_kernel kernel for convolving rows
148  * \param[in] v_kernel kernel for convolving columns
149  * \param[out] output the convolved cloud
150  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
151  * output.cols () < input.cols () then output is resized to input sizes.
152  */
153  inline void
154  convolve (const Eigen::ArrayXf& h_kernel, const Eigen::ArrayXf& v_kernel, PointCloudOut& output);
155  /** Convolve point cloud with same kernel along rows and columns separately.
156  * \param[out] output the convolved cloud
157  * \note if output doesn't fit in input i.e. output.rows () < input.rows () or
158  * output.cols () < input.cols () then output is resized to input sizes.
159  */
160  inline void
161  convolve (PointCloudOut& output);
162 
163  protected:
164  /// \brief convolve rows and ignore borders
165  void
166  convolve_rows (PointCloudOut& output);
167  /// \brief convolve cols and ignore borders
168  void
169  convolve_cols (PointCloudOut& output);
170  /// \brief convolve rows and mirror borders
171  void
173  /// \brief convolve cols and mirror borders
174  void
176  /// \brief convolve rows and duplicate borders
177  void
179  /// \brief convolve cols and duplicate borders
180  void
182  /** init compute is an internal method called before computation
183  * \param[in] output
184  * \throw pcl::InitFailedException
185  */
186  void
187  initCompute (PointCloudOut& output);
188  private:
189  /** \return the result of convolution of point at (\ai, \aj)
190  * \note no test on finity is performed
191  */
192  inline PointOut
193  convolveOneRowDense (int i, int j);
194  /** \return the result of convolution of point at (\ai, \aj)
195  * \note no test on finity is performed
196  */
197  inline PointOut
198  convolveOneColDense (int i, int j);
199  /** \return the result of convolution of point at (\ai, \aj)
200  * \note only finite points within \a distance_threshold_ are accounted
201  */
202  inline PointOut
203  convolveOneRowNonDense (int i, int j);
204  /** \return the result of convolution of point at (\ai, \aj)
205  * \note only finite points within \a distance_threshold_ are accounted
206  */
207  inline PointOut
208  convolveOneColNonDense (int i, int j);
209 
210  /// Border policy
211  int borders_policy_;
212  /// Threshold distance between adjacent points
213  float distance_threshold_;
214  /// Pointer to the input cloud
215  PointCloudInConstPtr input_;
216  /// convolution kernel
217  Eigen::ArrayXf kernel_;
218  /// half kernel size
219  int half_width_;
220  /// kernel size - 1
221  int kernel_width_;
222  protected:
223  /** \brief The number of threads the scheduler should use. */
224  unsigned int threads_;
225 
226  void
227  makeInfinite (PointOut& p)
228  {
229  p.x = p.y = p.z = std::numeric_limits<float>::quiet_NaN ();
230  }
231  };
232  }
233 }
234 
235 #include <pcl/filters/impl/convolution.hpp>
pcl::filters::Convolution::BORDERS_POLICY_DUPLICATE
@ BORDERS_POLICY_DUPLICATE
Definition: convolution.h:91
pcl::filters::Convolution::convolveCols
void convolveCols(PointCloudOut &output)
Convolve a float image columns by a given kernel.
Definition: convolution.hpp:115
pcl
Definition: convolution.h:46
pcl::filters::Convolution::convolve
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...
Definition: convolution.hpp:135
pcl::filters::Convolution::~Convolution
~Convolution()
Empty destructor.
Definition: convolution.h:96
pcl::filters::Convolution::convolve_cols
void convolve_cols(PointCloudOut &output)
convolve cols and ignore borders
Definition: convolution.hpp:421
pcl::filters::Convolution::Ptr
shared_ptr< Convolution< PointIn, PointOut > > Ptr
Definition: convolution.h:82
pcl::filters::Convolution::convolve_rows_mirror
void convolve_rows_mirror(PointCloudOut &output)
convolve rows and mirror borders
Definition: convolution.hpp:374
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:52
pcl::filters::Convolution::setInputCloud
void setInputCloud(const PointCloudInConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: convolution.h:102
pcl::filters::Convolution::PointCloudOut
pcl::PointCloud< PointOut > PointCloudOut
Definition: convolution.h:81
pcl::filters::Convolution::initCompute
void initCompute(PointCloudOut &output)
init compute is an internal method called before computation
Definition: convolution.hpp:64
pcl::filters::Convolution
Convolution is a mathematical operation on two functions f and g, producing a third function that is ...
Definition: convolution.h:76
pcl::filters::Convolution::convolve_rows
void convolve_rows(PointCloudOut &output)
convolve rows and ignore borders
Definition: convolution.hpp:281
pcl::filters::Convolution::makeInfinite
void makeInfinite(PointOut &p)
Definition: convolution.h:227
pcl::filters::Convolution::setDistanceThreshold
void setDistanceThreshold(const float &threshold)
Definition: convolution.h:122
pcl::filters::Convolution::setBordersPolicy
void setBordersPolicy(int policy)
Set the borders policy.
Definition: convolution.h:110
pcl::filters::Convolution::BORDERS_POLICY
BORDERS_POLICY
The borders policy available.
Definition: convolution.h:88
pcl::filters::Convolution::Convolution
Convolution()
Constructor.
Definition: convolution.hpp:54
pcl::filters::Convolution::convolve_cols_mirror
void convolve_cols_mirror(PointCloudOut &output)
convolve cols and mirror borders
Definition: convolution.hpp:514
pcl::filters::Convolution::convolve_rows_duplicate
void convolve_rows_duplicate(PointCloudOut &output)
convolve rows and duplicate borders
Definition: convolution.hpp:327
pcl::filters::Convolution::convolveRows
void convolveRows(PointCloudOut &output)
Convolve a float image rows by a given kernel.
Definition: convolution.hpp:95
pcl::filters::Convolution::PointCloudInPtr
typename PointCloudIn::Ptr PointCloudInPtr
Definition: convolution.h:79
pcl::filters::Convolution::BORDERS_POLICY_MIRROR
@ BORDERS_POLICY_MIRROR
Definition: convolution.h:90
pcl::filters::Convolution::getBordersPolicy
int getBordersPolicy()
Get the borders policy.
Definition: convolution.h:113
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
pcl::filters::Convolution::setNumberOfThreads
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
Definition: convolution.h:130
pcl::filters::Convolution::PointCloudInConstPtr
typename PointCloudIn::ConstPtr PointCloudInConstPtr
Definition: convolution.h:80
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
pcl::filters::Convolution::convolve_cols_duplicate
void convolve_cols_duplicate(PointCloudOut &output)
convolve cols and duplicate borders
Definition: convolution.hpp:467
pcl::filters::Convolution::BORDERS_POLICY_IGNORE
@ BORDERS_POLICY_IGNORE
Definition: convolution.h:89
pcl::filters::Convolution::setKernel
void setKernel(const Eigen::ArrayXf &kernel)
Set convolving kernel.
Definition: convolution.h:107
pcl::filters::Convolution::ConstPtr
shared_ptr< const Convolution< PointIn, PointOut > > ConstPtr
Definition: convolution.h:83
pcl::kernel
Definition: kernel.h:46
pcl::filters::Convolution::threads_
unsigned int threads_
The number of threads the scheduler should use.
Definition: convolution.h:224
pcl::filters::Convolution::getDistanceThreshold
const float & getDistanceThreshold() const
Definition: convolution.h:125