Point Cloud Library (PCL)  1.15.1-dev
auto.hpp
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2025-, Open Perception Inc.
6 *
7 * All rights reserved
8 */
9 
10 #ifndef PCL_SEARCH_AUTO_IMPL_HPP_
11 #define PCL_SEARCH_AUTO_IMPL_HPP_
12 
13 #include <pcl/common/utils.h> // for ignore
14 #include <pcl/search/auto.h>
15 #include <pcl/search/brute_force.h>
16 #include <pcl/search/kdtree.h>
17 #include <pcl/search/kdtree_nanoflann.h>
18 #include <pcl/search/octree.h>
19 #include <pcl/search/organized.h>
20 
21 template<typename PointT>
23  pcl::search::Search<PointT> * searcher = nullptr;
24  if constexpr (pcl::traits::has_xyz_v<PointT>) {
25  if (cloud->isOrganized ()) {
26  searcher = new pcl::search::OrganizedNeighbor<PointT> (sorted_results);
27  if(searcher->setInputCloud (cloud, indices)) { // may return false if OrganizedNeighbor cannot work with the cloud, then use another search method instead
28  return searcher;
29  }
30  delete searcher;
31  }
32  }
33 
34 #if PCL_HAS_NANOFLANN
35  // we get the number of search dimensions as a compile-time-constant via NR_DIMS. NR_DIMS may be -1 if it is not possible to determine the dimensions at compile-time (only at run-time), however then searching may be slower. If NR_DIMS is not -1, it must be the same as the return value of getNumberOfDimensions().
36  searcher = new pcl::search::KdTreeNanoflann<PointT, pcl::DefaultPointRepresentation<PointT>::NR_DIMS> (sorted_results, (purpose == pcl::search::Purpose::one_knn_search ? 10 : 20));
37  if(searcher->setInputCloud (cloud, indices)) {
38  return searcher;
39  }
40  delete searcher;
41 #else
42  pcl::utils::ignore(purpose);
43 #endif
44 
45 #if PCL_HAS_FLANN
46  searcher = new pcl::search::KdTree<PointT> (sorted_results);
47  if(searcher->setInputCloud (cloud, indices)) {
48  return searcher;
49  }
50  delete searcher;
51 #endif
52 
53  if constexpr (pcl::traits::has_xyz_v<PointT>) {
54  searcher = new pcl::search::Octree<PointT> (0.01); // TODO a better heuristic to choose octree resolution?
55  searcher->setSortedResults (sorted_results);
56  if(searcher->setInputCloud (cloud, indices)) {
57  return searcher;
58  }
59  delete searcher;
60  }
61 
62  // If nothing else works, and the point type has xyz coordinates, use brute force method
63  if constexpr (pcl::traits::has_xyz_v<PointT>) {
64  searcher = new pcl::search::BruteForce<PointT> (sorted_results);
65  searcher->setInputCloud (cloud, indices);
66  return searcher;
67  }
68  PCL_ERROR("[pcl::search::autoSelectMethod] No suitable method found. Make sure you have nanoflann and/or FLANN installed.\n");
69  return nullptr;
70 }
71 
72 #define PCL_INSTANTIATE_AutoSelectMethod(T) template PCL_EXPORTS pcl::search::Search<T> * pcl::search::autoSelectMethod<T>(const typename pcl::PointCloud<T>::ConstPtr& cloud, const pcl::IndicesConstPtr& indices, bool sorted_results, pcl::search::Purpose purpose);
73 
74 #endif //#ifndef PCL_SEARCH_AUTO_IMPL_HPP_
bool isOrganized() const
Return whether a dataset is organized (e.g., arranged in a structured grid).
Definition: point_cloud.h:311
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:415
Implementation of a simple brute force search algorithm.
Definition: brute_force.h:52
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:62
search::Octree is a wrapper class which implements nearest neighbor search operations based on the pc...
Definition: octree.h:69
OrganizedNeighbor is a class for optimized nearest neighbor search in organized projectable point clo...
Definition: organized.h:66
Generic search class.
Definition: search.h:75
virtual bool setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr())
Pass the input dataset that the search will be performed on.
Definition: search.hpp:75
virtual void setSortedResults(bool sorted)
sets whether the results should be sorted (ascending in the distance) or not
Definition: search.hpp:61
@ one_knn_search
The search method will mainly be used for nearestKSearch where k is 1.
pcl::search::Search< PointT > * autoSelectMethod(const typename pcl::PointCloud< PointT >::ConstPtr &cloud, bool sorted_results, Purpose purpose=Purpose::undefined)
Automatically select the fastest search method for the given point cloud.
Definition: auto.h:31
void ignore(const T &...)
Utility function to eliminate unused variable warnings.
Definition: utils.h:62
shared_ptr< const Indices > IndicesConstPtr
Definition: pcl_base.h:59