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>
48 template <
typename Po
intT>
void
53 threads_ = omp_get_num_procs();
58 threads_ = nr_threads;
62 template <
typename Po
intT>
void
65 if (!input_->isOrganized ())
67 PCL_ERROR (
"[pcl::FastBilateralFilterOMP] Input cloud needs to be organized.\n");
72 float base_max = -std::numeric_limits<float>::max (),
73 base_min = std::numeric_limits<float>::max ();
74 bool found_finite =
false;
75 for (std::size_t x = 0; x < output.
width; ++x)
77 for (std::size_t y = 0; y < output.
height; ++y)
79 if (std::isfinite (output (x, y).z))
81 if (base_max < output (x, y).z)
82 base_max = output (x, y).z;
83 if (base_min > output (x, y).z)
84 base_min = output (x, y).z;
91 PCL_WARN (
"[pcl::FastBilateralFilterOMP] Given an empty cloud. Doing nothing.\n");
94 #pragma omp parallel for \
96 shared(base_min, base_max, output) \
98 for (
long int i = 0; i < static_cast<long int> (output.
size ()); ++i)
99 if (!std::isfinite (output.
at(i).z))
100 output.
at(i).z = base_max;
102 const float base_delta = base_max - base_min;
104 const std::size_t padding_xy = 2;
105 const std::size_t padding_z = 2;
107 const std::size_t small_width =
static_cast<std::size_t
> (
static_cast<float> (input_->width - 1) / sigma_s_) + 1 + 2 * padding_xy;
108 const std::size_t small_height =
static_cast<std::size_t
> (
static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;
109 const std::size_t small_depth =
static_cast<std::size_t
> (base_delta / sigma_r_) + 1 + 2 * padding_z;
111 Array3D data (small_width, small_height, small_depth);
112 #if OPENMP_LEGACY_CONST_DATA_SHARING_RULE
113 #pragma omp parallel for \
115 shared(base_min, data, output) \
116 num_threads(threads_)
118 #pragma omp parallel for \
120 shared(base_min, data, output, small_height, small_width) \
121 num_threads(threads_)
123 for (
long int i = 0; i < static_cast<long int> (small_width * small_height); ++i)
125 std::size_t small_x =
static_cast<std::size_t
> (i % small_width);
126 std::size_t small_y =
static_cast<std::size_t
> (i / small_width);
127 std::size_t start_x =
static_cast<std::size_t
>(
128 std::max ((
static_cast<float> (small_x) -
static_cast<float> (padding_xy) - 0.5f) * sigma_s_ + 1, 0.f));
129 std::size_t end_x =
static_cast<std::size_t
>(
130 std::max ((
static_cast<float> (small_x) -
static_cast<float> (padding_xy) + 0.5f) * sigma_s_ + 1, 0.f));
131 std::size_t start_y =
static_cast<std::size_t
>(
132 std::max ((
static_cast<float> (small_y) -
static_cast<float> (padding_xy) - 0.5f) * sigma_s_ + 1, 0.f));
133 std::size_t end_y =
static_cast<std::size_t
>(
134 std::max ((
static_cast<float> (small_y) -
static_cast<float> (padding_xy) + 0.5f) * sigma_s_ + 1, 0.f));
135 for (std::size_t x = start_x; x < end_x && x < input_->width; ++x)
137 for (std::size_t y = start_y; y < end_y && y < input_->height; ++y)
139 const float z = output (x,y).z - base_min;
140 const std::size_t small_z =
static_cast<std::size_t
> (
static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;
141 Eigen::Vector2f& d = data (small_x, small_y, small_z);
142 d[0] += output (x,y).z;
148 std::vector<long int> offset (3);
149 offset[0] = &(data (1,0,0)) - &(data (0,0,0));
150 offset[1] = &(data (0,1,0)) - &(data (0,0,0));
151 offset[2] = &(data (0,0,1)) - &(data (0,0,0));
153 Array3D buffer (small_width, small_height, small_depth);
155 for (std::size_t dim = 0; dim < 3; ++dim)
157 for (std::size_t n_iter = 0; n_iter < 2; ++n_iter)
159 Array3D* current_buffer = (n_iter % 2 == 1 ? &buffer : &data);
160 Array3D* current_data =(n_iter % 2 == 1 ? &data : &buffer);
161 #if OPENMP_LEGACY_CONST_DATA_SHARING_RULE
162 #pragma omp parallel for \
164 shared(current_buffer, current_data, dim, offset) \
165 num_threads(threads_)
167 #pragma omp parallel for \
169 shared(current_buffer, current_data, dim, offset, small_depth, small_height, small_width) \
170 num_threads(threads_)
172 for(
long int i = 0; i < static_cast<long int> ((small_width - 2)*(small_height - 2)); ++i)
174 std::size_t x =
static_cast<std::size_t
> (i % (small_width - 2) + 1);
175 std::size_t y =
static_cast<std::size_t
> (i / (small_width - 2) + 1);
176 const long int off = offset[dim];
177 Eigen::Vector2f* d_ptr = &(current_data->operator() (x,y,1));
178 Eigen::Vector2f* b_ptr = &(current_buffer->operator() (x,y,1));
180 for(std::size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)
181 *d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;
191 for (std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >::iterator d = data.begin (); d != data.end (); ++d)
192 *d /= ((*d)[0] != 0) ? (*d)[1] : 1;
194 #pragma omp parallel for \
196 shared(base_min, data, output) \
197 num_threads(threads_)
198 for (
long int i = 0; i < static_cast<long int> (input_->size ()); ++i)
200 std::size_t x =
static_cast<std::size_t
> (i % input_->width);
201 std::size_t y =
static_cast<std::size_t
> (i / input_->width);
202 const float z = output (x,y).z - base_min;
203 const Eigen::Vector2f D = data.trilinear_interpolation (
static_cast<float> (x) / sigma_s_ + padding_xy,
204 static_cast<float> (y) / sigma_s_ + padding_xy,
205 z / sigma_r_ + padding_z);
206 output(x,y).z = D[0];
211 #pragma omp parallel for \
213 shared(base_min, data, output) \
214 num_threads(threads_)
215 for (
long i = 0; i < static_cast<long int> (input_->size ()); ++i)
217 std::size_t x =
static_cast<std::size_t
> (i % input_->width);
218 std::size_t y =
static_cast<std::size_t
> (i / input_->width);
219 const float z = output (x,y).z - base_min;
220 const Eigen::Vector2f D = data.trilinear_interpolation (
static_cast<float> (x) / sigma_s_ + padding_xy,
221 static_cast<float> (y) / sigma_s_ + padding_xy,
222 z / sigma_r_ + padding_z);
223 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.
std::uint32_t width
The point cloud width (if organized as an image-structure).
std::uint32_t height
The point cloud height (if organized as an image-structure).
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.