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