Point Cloud Library (PCL)  1.14.1-dev
convolution.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, 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  */
37 
38 #pragma once
39 
40 #include <pcl/2d/convolution.h>
41 
42 namespace pcl {
43 
44 template <typename PointT>
45 void
47 {
48  int input_row = 0;
49  int input_col = 0;
50  // default boundary option : zero padding
51  output = *input_;
52 
53  int iw = static_cast<int>(input_->width), ih = static_cast<int>(input_->height),
54  kw = static_cast<int>(kernel_.width), kh = static_cast<int>(kernel_.height);
55  switch (boundary_options_) {
56  default:
57  case BOUNDARY_OPTION_CLAMP: {
58  for (int i = 0; i < ih; i++) {
59  for (int j = 0; j < iw; j++) {
60  float intensity = 0;
61  for (int k = 0; k < kh; k++) {
62  for (int l = 0; l < kw; l++) {
63  int ikkh = i + k - kh / 2, jlkw = j + l - kw / 2;
64  if (ikkh < 0)
65  input_row = 0;
66  else if (ikkh >= ih)
67  input_row = ih - 1;
68  else
69  input_row = ikkh;
70 
71  if (jlkw < 0)
72  input_col = 0;
73  else if (jlkw >= iw)
74  input_col = iw - 1;
75  else
76  input_col = jlkw;
77 
78  intensity +=
79  kernel_(l, k).intensity * (*input_)(input_col, input_row).intensity;
80  }
81  }
82  output(j, i).intensity = intensity;
83  }
84  }
85  break;
86  }
87 
88  case BOUNDARY_OPTION_MIRROR: {
89  for (int i = 0; i < ih; i++) {
90  for (int j = 0; j < iw; j++) {
91  float intensity = 0;
92  for (int k = 0; k < kh; k++) {
93  for (int l = 0; l < kw; l++) {
94  int ikkh = i + k - kh / 2, jlkw = j + l - kw / 2;
95  if (ikkh < 0)
96  input_row = -ikkh - 1;
97  else if (ikkh >= ih)
98  input_row = 2 * ih - 1 - ikkh;
99  else
100  input_row = ikkh;
101 
102  if (jlkw < 0)
103  input_col = -jlkw - 1;
104  else if (jlkw >= iw)
105  input_col = 2 * iw - 1 - jlkw;
106  else
107  input_col = jlkw;
108 
109  intensity +=
110  kernel_(l, k).intensity * ((*input_)(input_col, input_row).intensity);
111  }
112  }
113  output(j, i).intensity = intensity;
114  }
115  }
116  break;
117  }
118 
119  case BOUNDARY_OPTION_ZERO_PADDING: {
120  for (int i = 0; i < ih; i++) {
121  for (int j = 0; j < iw; j++) {
122  float intensity = 0;
123  for (int k = 0; k < kh; k++) {
124  for (int l = 0; l < kw; l++) {
125  int ikkh = i + k - kh / 2, jlkw = j + l - kw / 2;
126  if (ikkh < 0 || ikkh >= ih || jlkw < 0 || jlkw >= iw)
127  continue;
128  intensity += kernel_(l, k).intensity * ((*input_)(jlkw, ikkh).intensity);
129  }
130  }
131  output(j, i).intensity = intensity;
132  }
133  }
134  break;
135  }
136  } // switch
137 }
138 } // namespace pcl
void filter(pcl::PointCloud< PointT > &output)
Performs 2D convolution of the input point cloud with the kernel.
Definition: convolution.hpp:46
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173