Point Cloud Library (PCL)  1.11.1
fast_bilateral_omp.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  * Copyright (c) 2004, Sylvain Paris and Francois Sillion
7 
8  * All rights reserved.
9 
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: fast_bilateral_omp.hpp 8381 2013-01-02 23:12:44Z sdmiller $
38  *
39  */
40 #ifndef PCL_FILTERS_IMPL_FAST_BILATERAL_OMP_HPP_
41 #define PCL_FILTERS_IMPL_FAST_BILATERAL_OMP_HPP_
42 
43 #include <pcl/filters/fast_bilateral_omp.h>
44 #include <pcl/common/io.h>
45 #include <cassert>
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointT> void
50 {
51  if (nr_threads == 0)
52 #ifdef _OPENMP
53  threads_ = omp_get_num_procs();
54 #else
55  threads_ = 1;
56 #endif
57  else
58  threads_ = nr_threads;
59 }
60 
61 //////////////////////////////////////////////////////////////////////////////////////////////
62 template <typename PointT> void
64 {
65  if (!input_->isOrganized ())
66  {
67  PCL_ERROR ("[pcl::FastBilateralFilterOMP] Input cloud needs to be organized.\n");
68  return;
69  }
70 
71  copyPointCloud (*input_, output);
72  float base_max = -std::numeric_limits<float>::max (),
73  base_min = std::numeric_limits<float>::max ();
74  bool found_finite = false;
75  for (const auto& pt: output)
76  {
77  if (std::isfinite(pt.z))
78  {
79  base_max = std::max<float>(pt.z, base_max);
80  base_min = std::min<float>(pt.z, base_min);
81  found_finite = true;
82  }
83  }
84  if (!found_finite)
85  {
86  PCL_WARN ("[pcl::FastBilateralFilterOMP] Given an empty cloud. Doing nothing.\n");
87  return;
88  }
89 #pragma omp parallel for \
90  default(none) \
91  shared(base_min, base_max, output) \
92  num_threads(threads_)
93  for (long int i = 0; i < static_cast<long int> (output.size ()); ++i)
94  if (!std::isfinite (output.at(i).z))
95  output.at(i).z = base_max;
96 
97  const float base_delta = base_max - base_min;
98 
99  const std::size_t padding_xy = 2;
100  const std::size_t padding_z = 2;
101 
102  const std::size_t small_width = static_cast<std::size_t> (static_cast<float> (input_->width - 1) / sigma_s_) + 1 + 2 * padding_xy;
103  const std::size_t small_height = static_cast<std::size_t> (static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;
104  const std::size_t small_depth = static_cast<std::size_t> (base_delta / sigma_r_) + 1 + 2 * padding_z;
105 
106  Array3D data (small_width, small_height, small_depth);
107 #if OPENMP_LEGACY_CONST_DATA_SHARING_RULE
108 #pragma omp parallel for \
109  default(none) \
110  shared(base_min, data, output) \
111  num_threads(threads_)
112 #else
113 #pragma omp parallel for \
114  default(none) \
115  shared(base_min, data, output, small_height, small_width) \
116  num_threads(threads_)
117 #endif
118  for (long int i = 0; i < static_cast<long int> (small_width * small_height); ++i)
119  {
120  std::size_t small_x = static_cast<std::size_t> (i % small_width);
121  std::size_t small_y = static_cast<std::size_t> (i / small_width);
122  std::size_t start_x = static_cast<std::size_t>(
123  std::max ((static_cast<float> (small_x) - static_cast<float> (padding_xy) - 0.5f) * sigma_s_ + 1, 0.f));
124  std::size_t end_x = static_cast<std::size_t>(
125  std::max ((static_cast<float> (small_x) - static_cast<float> (padding_xy) + 0.5f) * sigma_s_ + 1, 0.f));
126  std::size_t start_y = static_cast<std::size_t>(
127  std::max ((static_cast<float> (small_y) - static_cast<float> (padding_xy) - 0.5f) * sigma_s_ + 1, 0.f));
128  std::size_t end_y = static_cast<std::size_t>(
129  std::max ((static_cast<float> (small_y) - static_cast<float> (padding_xy) + 0.5f) * sigma_s_ + 1, 0.f));
130  for (std::size_t x = start_x; x < end_x && x < input_->width; ++x)
131  {
132  for (std::size_t y = start_y; y < end_y && y < input_->height; ++y)
133  {
134  const float z = output (x,y).z - base_min;
135  const std::size_t small_z = static_cast<std::size_t> (static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;
136  Eigen::Vector2f& d = data (small_x, small_y, small_z);
137  d[0] += output (x,y).z;
138  d[1] += 1.0f;
139  }
140  }
141  }
142 
143  std::vector<long int> offset (3);
144  offset[0] = &(data (1,0,0)) - &(data (0,0,0));
145  offset[1] = &(data (0,1,0)) - &(data (0,0,0));
146  offset[2] = &(data (0,0,1)) - &(data (0,0,0));
147 
148  Array3D buffer (small_width, small_height, small_depth);
149 
150  for (std::size_t dim = 0; dim < 3; ++dim)
151  {
152  for (std::size_t n_iter = 0; n_iter < 2; ++n_iter)
153  {
154  Array3D* current_buffer = (n_iter % 2 == 1 ? &buffer : &data);
155  Array3D* current_data =(n_iter % 2 == 1 ? &data : &buffer);
156 #if OPENMP_LEGACY_CONST_DATA_SHARING_RULE
157 #pragma omp parallel for \
158  default(none) \
159  shared(current_buffer, current_data, dim, offset) \
160  num_threads(threads_)
161 #else
162 #pragma omp parallel for \
163  default(none) \
164  shared(current_buffer, current_data, dim, offset, small_depth, small_height, small_width) \
165  num_threads(threads_)
166 #endif
167  for(long int i = 0; i < static_cast<long int> ((small_width - 2)*(small_height - 2)); ++i)
168  {
169  std::size_t x = static_cast<std::size_t> (i % (small_width - 2) + 1);
170  std::size_t y = static_cast<std::size_t> (i / (small_width - 2) + 1);
171  const long int off = offset[dim];
172  Eigen::Vector2f* d_ptr = &(current_data->operator() (x,y,1));
173  Eigen::Vector2f* b_ptr = &(current_buffer->operator() (x,y,1));
174 
175  for(std::size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)
176  *d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;
177  }
178  }
179  }
180  // Note: this works because there are an even number of iterations.
181  // If there were an odd number, we would need to end with a:
182  // std::swap (data, buffer);
183 
184  if (early_division_)
185  {
186  for (std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >::iterator d = data.begin (); d != data.end (); ++d)
187  *d /= ((*d)[0] != 0) ? (*d)[1] : 1;
188 
189 #pragma omp parallel for \
190  default(none) \
191  shared(base_min, data, output) \
192  num_threads(threads_)
193  for (long int i = 0; i < static_cast<long int> (input_->size ()); ++i)
194  {
195  std::size_t x = static_cast<std::size_t> (i % input_->width);
196  std::size_t y = static_cast<std::size_t> (i / input_->width);
197  const float z = output (x,y).z - base_min;
198  const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,
199  static_cast<float> (y) / sigma_s_ + padding_xy,
200  z / sigma_r_ + padding_z);
201  output(x,y).z = D[0];
202  }
203  }
204  else
205  {
206 #pragma omp parallel for \
207  default(none) \
208  shared(base_min, data, output) \
209  num_threads(threads_)
210  for (long i = 0; i < static_cast<long int> (input_->size ()); ++i)
211  {
212  std::size_t x = static_cast<std::size_t> (i % input_->width);
213  std::size_t y = static_cast<std::size_t> (i / input_->width);
214  const float z = output (x,y).z - base_min;
215  const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,
216  static_cast<float> (y) / sigma_s_ + padding_xy,
217  z / sigma_r_ + padding_z);
218  output (x,y).z = D[0] / D[1];
219  }
220  }
221 }
222 
223 
224 
225 #endif /* PCL_FILTERS_IMPL_FAST_BILATERAL_OMP_HPP_ */
226 
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
void applyFilter(PointCloud &output) override
Filter the input data and store the results into output.
typename FastBilateralFilter< PointT >::Array3D Array3D
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:181
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
Definition: point_cloud.h:284
std::size_t size() const
Definition: point_cloud.h:459
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.
Definition: io.hpp:121