9 #pragma message("warning: Task-based OpenMP parallelism causes run-time memory errors with Eigen matrices.")
10 #pragma message("warning: Thus, OpenMP-based multi-threading for KdTree construction is disabled on MSVC.")
23 template <
typename KdTree,
typename Po
intCloud>
26 std::iota(kdtree.indices.begin(), kdtree.indices.end(), 0);
28 std::atomic_uint64_t node_count = 0;
32 #pragma omp parallel num_threads(num_threads)
34 #pragma omp single nowait
35 { kdtree.root =
create_node(kdtree, node_count, points, kdtree.indices.begin(), kdtree.indices.begin(), kdtree.indices.end()); }
38 kdtree.root =
create_node(kdtree, node_count, points, kdtree.indices.begin(), kdtree.indices.begin(), kdtree.indices.end());
41 kdtree.nodes.resize(node_count);
49 template <
typename Po
intCloud,
typename KdTree,
typename IndexConstIterator>
52 std::atomic_uint64_t& node_count,
54 IndexConstIterator global_first,
55 IndexConstIterator first,
56 IndexConstIterator last)
const {
57 const size_t N = std::distance(first, last);
59 auto& node = kdtree.nodes[node_index];
64 node.node_type.lr.first = std::distance(global_first, first);
65 node.node_type.lr.last = std::distance(global_first, last);
71 using Projection =
typename KdTree::Projection;
73 const auto median_itr = first + N / 2;
74 std::nth_element(first, median_itr, last, [&](
size_t i,
size_t j) {
return proj(
traits::point(points, i)) < proj(
traits::point(points, j)); });
77 node.node_type.sub.proj = proj;
78 node.node_type.sub.thresh = proj(
traits::point(points, *median_itr));
82 #pragma omp task default(shared) if (N > 512)
83 node.left =
create_node(kdtree, node_count, points, global_first, first, median_itr);
84 #pragma omp task default(shared) if (N > 512)
85 node.right =
create_node(kdtree, node_count, points, global_first, median_itr, last);
88 node.left =
create_node(kdtree, node_count, points, global_first, first, median_itr);
89 node.right =
create_node(kdtree, node_count, points, global_first, median_itr, last);
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
Definition: flat_container.hpp:12
std::uint32_t NodeIndexType
Definition: kdtree.hpp:52
Kd-tree builder with OpenMP.
Definition: kdtree_omp.hpp:16
int max_leaf_size
Maximum number of points in a leaf node.
Definition: kdtree_omp.hpp:97
KdTreeBuilderOMP(int num_threads=4)
Constructor.
Definition: kdtree_omp.hpp:20
int num_threads
Number of threads.
Definition: kdtree_omp.hpp:96
void build_tree(KdTree &kdtree, const PointCloud &points) const
Build KdTree.
Definition: kdtree_omp.hpp:24
NodeIndexType create_node(KdTree &kdtree, std::atomic_uint64_t &node_count, const PointCloud &points, IndexConstIterator global_first, IndexConstIterator first, IndexConstIterator last) const
Create a Kd-tree node from the given point indices.
Definition: kdtree_omp.hpp:50
ProjectionSetting projection_setting
Projection setting.
Definition: kdtree_omp.hpp:98
"Safe" KdTree that holds the ownership of the input points.
Definition: kdtree.hpp:245
Point cloud.
Definition: point_cloud.hpp:15
Parameters to control the projection axis search.
Definition: projection.hpp:12