40 #ifndef PCL_FILTERS_IMPL_FAST_BILATERAL_OMP_HPP_
41 #define PCL_FILTERS_IMPL_FAST_BILATERAL_OMP_HPP_
43 #include <pcl/filters/fast_bilateral_omp.h>
44 #include <pcl/common/io.h>
47 template <
typename Po
intT>
void
52 threads_ = omp_get_num_procs();
57 threads_ = nr_threads;
61 template <
typename Po
intT>
void
64 if (!input_->isOrganized ())
66 PCL_ERROR (
"[pcl::FastBilateralFilterOMP] Input cloud needs to be organized.\n");
71 float base_max = -std::numeric_limits<float>::max (),
72 base_min = std::numeric_limits<float>::max ();
73 bool found_finite =
false;
74 for (
const auto& pt: output)
76 if (std::isfinite(pt.z))
78 base_max = std::max<float>(pt.z, base_max);
79 base_min = std::min<float>(pt.z, base_min);
85 PCL_WARN (
"[pcl::FastBilateralFilterOMP] Given an empty cloud. Doing nothing.\n");
88 #pragma omp parallel for \
90 shared(base_min, base_max, output) \
92 for (
long int i = 0; i < static_cast<long int> (output.
size ()); ++i)
93 if (!std::isfinite (output.
at(i).z))
94 output.
at(i).z = base_max;
96 const float base_delta = base_max - base_min;
98 const std::size_t padding_xy = 2;
99 const std::size_t padding_z = 2;
101 const std::size_t small_width =
static_cast<std::size_t
> (
static_cast<float> (input_->width - 1) / sigma_s_) + 1 + 2 * padding_xy;
102 const std::size_t small_height =
static_cast<std::size_t
> (
static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;
103 const std::size_t small_depth =
static_cast<std::size_t
> (base_delta / sigma_r_) + 1 + 2 * padding_z;
105 Array3D data (small_width, small_height, small_depth);
106 #if OPENMP_LEGACY_CONST_DATA_SHARING_RULE
107 #pragma omp parallel for \
109 shared(base_min, data, output) \
110 num_threads(threads_)
112 #pragma omp parallel for \
114 shared(base_min, data, output, small_height, small_width) \
115 num_threads(threads_)
117 for (
long int i = 0; i < static_cast<long int> (small_width * small_height); ++i)
119 auto small_x =
static_cast<std::size_t
> (i % small_width);
120 auto small_y =
static_cast<std::size_t
> (i / small_width);
121 auto start_x =
static_cast<std::size_t
>(
122 std::max ((
static_cast<float> (small_x) -
static_cast<float> (padding_xy) - 0.5f) * sigma_s_ + 1, 0.f));
123 auto end_x =
static_cast<std::size_t
>(
124 std::max ((
static_cast<float> (small_x) -
static_cast<float> (padding_xy) + 0.5f) * sigma_s_ + 1, 0.f));
125 auto start_y =
static_cast<std::size_t
>(
126 std::max ((
static_cast<float> (small_y) -
static_cast<float> (padding_xy) - 0.5f) * sigma_s_ + 1, 0.f));
127 auto end_y =
static_cast<std::size_t
>(
128 std::max ((
static_cast<float> (small_y) -
static_cast<float> (padding_xy) + 0.5f) * sigma_s_ + 1, 0.f));
129 for (std::size_t x = start_x; x < end_x && x < input_->width; ++x)
131 for (std::size_t y = start_y; y < end_y && y < input_->height; ++y)
133 const float z = output (x,y).z - base_min;
134 const std::size_t small_z =
static_cast<std::size_t
> (
static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;
135 Eigen::Vector2f& d = data (small_x, small_y, small_z);
136 d[0] += output (x,y).z;
142 std::vector<long int> offset (3);
143 offset[0] = &(data (1,0,0)) - &(data (0,0,0));
144 offset[1] = &(data (0,1,0)) - &(data (0,0,0));
145 offset[2] = &(data (0,0,1)) - &(data (0,0,0));
147 Array3D buffer (small_width, small_height, small_depth);
149 for (std::size_t dim = 0; dim < 3; ++dim)
151 for (std::size_t n_iter = 0; n_iter < 2; ++n_iter)
153 Array3D* current_buffer = (n_iter % 2 == 1 ? &buffer : &data);
154 Array3D* current_data =(n_iter % 2 == 1 ? &data : &buffer);
155 #if OPENMP_LEGACY_CONST_DATA_SHARING_RULE
156 #pragma omp parallel for \
158 shared(current_buffer, current_data, dim, offset) \
159 num_threads(threads_)
161 #pragma omp parallel for \
163 shared(current_buffer, current_data, dim, offset, small_depth, small_height, small_width) \
164 num_threads(threads_)
166 for(
long int i = 0; i < static_cast<long int> ((small_width - 2)*(small_height - 2)); ++i)
168 auto x =
static_cast<std::size_t
> (i % (small_width - 2) + 1);
169 auto y =
static_cast<std::size_t
> (i / (small_width - 2) + 1);
170 const long int off = offset[dim];
171 Eigen::Vector2f* d_ptr = &(current_data->operator() (x,y,1));
172 Eigen::Vector2f* b_ptr = &(current_buffer->operator() (x,y,1));
174 for(std::size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)
175 *d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;
185 for (
auto d = data.begin (); d != data.end (); ++d)
186 *d /= ((*d)[0] != 0) ? (*d)[1] : 1;
188 #pragma omp parallel for \
190 shared(base_min, data, output) \
191 num_threads(threads_)
192 for (
long int i = 0; i < static_cast<long int> (input_->size ()); ++i)
194 auto x =
static_cast<std::size_t
> (i % input_->width);
195 auto y =
static_cast<std::size_t
> (i / input_->width);
196 const float z = output (x,y).z - base_min;
197 const Eigen::Vector2f D = data.trilinear_interpolation (
static_cast<float> (x) / sigma_s_ + padding_xy,
198 static_cast<float> (y) / sigma_s_ + padding_xy,
199 z / sigma_r_ + padding_z);
200 output(x,y).z = D[0];
205 #pragma omp parallel for \
207 shared(base_min, data, output) \
208 num_threads(threads_)
209 for (
long i = 0; i < static_cast<long int> (input_->size ()); ++i)
211 auto x =
static_cast<std::size_t
> (i % input_->width);
212 auto y =
static_cast<std::size_t
> (i / input_->width);
213 const float z = output (x,y).z - base_min;
214 const Eigen::Vector2f D = data.trilinear_interpolation (
static_cast<float> (x) / sigma_s_ + padding_xy,
215 static_cast<float> (y) / sigma_s_ + padding_xy,
216 z / sigma_r_ + padding_z);
217 output (x,y).z = D[0] / D[1];
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.
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
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.