Skip to content

Commit f19e4af

Browse files
authored
[search] Add support for nanoflann (faster alternative to pcl::search::KdTree) (#6250)
* Add support for nanoflann * Fix and additional test
1 parent 39669c0 commit f19e4af

File tree

9 files changed

+1615
-2
lines changed

9 files changed

+1615
-2
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ else()
361361
endif()
362362
endif()
363363

364+
find_package(nanoflann 1.4.2 QUIET)
365+
if(TARGET nanoflann::nanoflann)
366+
set(PCL_HAS_NANOFLANN 1)
367+
message(STATUS "Found nanoflann")
368+
else()
369+
message(STATUS "nanoflann was not found, so some PCL classes will not be available.")
370+
endif()
371+
364372
# libusb
365373
option(WITH_LIBUSB "Build USB RGBD-Camera drivers" TRUE)
366374
if(WITH_LIBUSB)

pcl_config.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949

5050
#cmakedefine HAVE_DAVIDSDK 1
5151

52+
#ifndef PCL_HAS_NANOFLANN
53+
#cmakedefine PCL_HAS_NANOFLANN 1
54+
#endif
55+
5256
// SSE macros
5357
#cmakedefine HAVE_POSIX_MEMALIGN
5458
#cmakedefine HAVE_MM_MALLOC

search/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(srcs
2222
set(incs
2323
"include/pcl/${SUBSYS_NAME}/search.h"
2424
"include/pcl/${SUBSYS_NAME}/kdtree.h"
25+
"include/pcl/${SUBSYS_NAME}/kdtree_nanoflann.h"
2526
"include/pcl/${SUBSYS_NAME}/brute_force.h"
2627
"include/pcl/${SUBSYS_NAME}/organized.h"
2728
"include/pcl/${SUBSYS_NAME}/octree.h"

search/include/pcl/search/impl/kdtree.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,21 @@ pcl::search::KdTree<PointT,Tree>::KdTree (bool sorted)
4848
{
4949
}
5050

51+
template <typename PointT, class Tree>
52+
pcl::search::KdTree<PointT,Tree>::KdTree (const std::string& name, bool sorted)
53+
: pcl::search::Search<PointT> (name, sorted)
54+
{
55+
}
56+
5157
///////////////////////////////////////////////////////////////////////////////////////////
5258
template <typename PointT, class Tree> void
5359
pcl::search::KdTree<PointT,Tree>::setPointRepresentation (
5460
const PointRepresentationConstPtr &point_representation)
5561
{
56-
tree_->setPointRepresentation (point_representation);
62+
if(tree_)
63+
tree_->setPointRepresentation (point_representation);
64+
else
65+
PCL_ERROR("Calling setPointRepresentation on KdTreeNanoflann that has been cast to KdTree is not possible in this PCL version. Call setPointRepresentation directly on the KdTreeNanoflann object.\n");
5766
}
5867

5968
///////////////////////////////////////////////////////////////////////////////////////////
@@ -68,7 +77,10 @@ pcl::search::KdTree<PointT,Tree>::setSortedResults (bool sorted_results)
6877
template <typename PointT, class Tree> void
6978
pcl::search::KdTree<PointT,Tree>::setEpsilon (float eps)
7079
{
71-
tree_->setEpsilon (eps);
80+
if(tree_)
81+
tree_->setEpsilon (eps);
82+
else
83+
PCL_ERROR("Calling setEpsilon on KdTreeNanoflann that has been cast to KdTree is not possible in this PCL version. Call setEpsilon directly on the KdTreeNanoflann object.\n");
7284
}
7385

7486
///////////////////////////////////////////////////////////////////////////////////////////

search/include/pcl/search/kdtree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ namespace pcl
161161
std::vector<float> &k_sqr_distances,
162162
unsigned int max_nn = 0) const override;
163163
protected:
164+
/** \brief This leaves the internal tree_ object uninitialized! */
165+
KdTree (const std::string& name, bool sorted);
164166
/** \brief A pointer to the internal KdTree object. */
165167
KdTreePtr tree_;
166168
};

0 commit comments

Comments
 (0)