small_gicp
downsampling_omp.hpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright 2024 Kenji Koide
2 // SPDX-License-Identifier: MIT
3 #pragma once
4 
5 #include <atomic>
6 #include <memory>
7 #include <iostream>
8 
13 
14 namespace small_gicp {
15 
25 template <typename InputPointCloud, typename OutputPointCloud = InputPointCloud>
26 std::shared_ptr<OutputPointCloud> voxelgrid_sampling_omp(const InputPointCloud& points, double leaf_size, int num_threads = 4) {
27  if (traits::size(points) == 0) {
28  return std::make_shared<OutputPointCloud>();
29  }
30 
31  const double inv_leaf_size = 1.0 / leaf_size;
32 
33  constexpr std::uint64_t invalid_coord = std::numeric_limits<std::uint64_t>::max();
34  constexpr int coord_bit_size = 21; // Bits to represent each voxel coordinate (pack 21x3 = 63bits in 64bit int)
35  constexpr size_t coord_bit_mask = (1 << 21) - 1; // Bit mask
36  constexpr int coord_offset = 1 << (coord_bit_size - 1); // Coordinate offset to make values positive
37 
38  std::vector<std::pair<std::uint64_t, size_t>> coord_pt(traits::size(points));
39 #pragma omp parallel for num_threads(num_threads) schedule(guided, 32)
40  for (std::int64_t i = 0; i < traits::size(points); i++) {
41  const Eigen::Array4i coord = fast_floor(traits::point(points, i) * inv_leaf_size) + coord_offset;
42  if ((coord < 0).any() || (coord > coord_bit_mask).any()) {
43  std::cerr << "warning: voxel coord is out of range!!" << std::endl;
44  coord_pt[i] = {invalid_coord, i};
45  continue;
46  }
47  // Compute voxel coord bits (0|1bit, z|21bit, y|21bit, x|21bit)
48  const std::uint64_t bits = //
49  (static_cast<std::uint64_t>(coord[0] & coord_bit_mask) << (coord_bit_size * 0)) | //
50  (static_cast<std::uint64_t>(coord[1] & coord_bit_mask) << (coord_bit_size * 1)) | //
51  (static_cast<std::uint64_t>(coord[2] & coord_bit_mask) << (coord_bit_size * 2));
52  coord_pt[i] = {bits, i};
53  }
54 
55  // Sort by voxel coords
56  quick_sort_omp(coord_pt.begin(), coord_pt.end(), [](const auto& lhs, const auto& rhs) { return lhs.first < rhs.first; }, num_threads);
57 
58  auto downsampled = std::make_shared<OutputPointCloud>();
59  traits::resize(*downsampled, traits::size(points));
60 
61  // Take block-wise sum
62  const int block_size = 1024;
63  std::atomic_uint64_t num_points = 0;
64 
65 #pragma omp parallel for num_threads(num_threads) schedule(guided, 4)
66  for (std::int64_t block_begin = 0; block_begin < traits::size(points); block_begin += block_size) {
67  std::vector<Eigen::Vector4d> sub_points;
68  sub_points.reserve(block_size);
69 
70  const size_t block_end = std::min<size_t>(traits::size(points), block_begin + block_size);
71 
72  Eigen::Vector4d sum_pt = traits::point(points, coord_pt[block_begin].second);
73  for (size_t i = block_begin + 1; i != block_end; i++) {
74  if (coord_pt[i].first == invalid_coord) {
75  continue;
76  }
77 
78  if (coord_pt[i - 1].first != coord_pt[i].first) {
79  sub_points.emplace_back(sum_pt / sum_pt.w());
80  sum_pt.setZero();
81  }
82  sum_pt += traits::point(points, coord_pt[i].second);
83  }
84  sub_points.emplace_back(sum_pt / sum_pt.w());
85 
86  const size_t point_index_begin = num_points.fetch_add(sub_points.size());
87  for (size_t i = 0; i < sub_points.size(); i++) {
88  traits::set_point(*downsampled, point_index_begin + i, sub_points[i]);
89  }
90  }
91 
92  traits::resize(*downsampled, num_points);
93 
94  return downsampled;
95 }
96 
97 } // namespace small_gicp
size_t size(const T &points)
Get the number of points.
Definition: traits.hpp:16
auto point(const T &points, size_t i)
Get i-th point. 4D vector is used to take advantage of SIMD intrinsics. The last element must be fill...
Definition: traits.hpp:40
void set_point(T &points, size_t i, const Eigen::Vector4d &pt)
Set i-th point. (x, y, z, 1)
Definition: traits.hpp:64
void resize(T &points, size_t n)
Resize the point cloud (this function should resize all attributes)
Definition: traits.hpp:58
Definition: flat_container.hpp:12
void quick_sort_omp(RandomAccessIterator first, RandomAccessIterator last, const Compare &comp, int num_threads)
Quick sort with OpenMP parallelism.
Definition: sort_omp.hpp:95
Eigen::Array4i fast_floor(const Eigen::Array4d &pt)
Fast floor (https://stackoverflow.com/questions/824118/why-is-floor-so-slow).
Definition: fast_floor.hpp:12
std::shared_ptr< OutputPointCloud > voxelgrid_sampling_omp(const InputPointCloud &points, double leaf_size, int num_threads=4)
Voxel grid downsampling with OpenMP backend.
Definition: downsampling_omp.hpp:26