Skip to content

Add FlannIndex abstract interface and implement NanoFlannIndex subclass - #1744

Merged
matlabbe merged 13 commits into
introlab:masterfrom
MohammadWolf95:feature/nanoflann-interface
Aug 16, 2026
Merged

matlabbe merged 13 commits into
introlab:masterfrom
MohammadWolf95:feature/nanoflann-interface

Conversation

@MohammadWolf95

@MohammadWolf95 MohammadWolf95 commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Hi @matlabbe,

The architectural refactoring is complete! I have moved FlannIndex into an abstract interface and implemented RtFlannIndex and NanoFlannIndex as subclasses according to your suggestions.

I updated RegistrationVis.cpp and VWDictionary.cpp to use the new factory pattern. All 16 tests in test_corelib (including RegistrationVisTest) are now passing perfectly.

In the meantime, I am going to test the matching performance and speed on a Raspberry Pi 5 to see the actual benchmark results on an ARM platform, and I will share the numbers here as soon as I finish.

ML Edits: Two changes for the default behavior of rtabmap:

  1. RegistrationVis now uses nanoflann for 2D reprojected points matching, when tracking features from one frame to another (when a guess is provided with feature matching approach). It gives a slight speed boost for that.
  2. test_flann_index_perf has been added to compare performance of all flann strategies
  3. Vis/CorNNType=9 and Kp/NNStrategy=6 can select nanoflann. Based on performance tests, it is not recommended to use with Kp/NNStrategy for large high dimensional dictionary. One place that may be beneficial is to set it for Vis/CorNNType, though Vis/CorNNType=8 (flann kdtree single) may already be a good contender.
  4. Kp/FlannRebalancingFactor: The meaning of this parameter has slightly changed (description updated), but the value should be backward compatible with old default. At factor=1, it is still disabling auto-rebuilding of the index. However, when factor>1, the index is rebuilt when the ratio of words removed from the index over the total size of the index (including removed words) is over 1 / factor. Before the factor meant that the index is rebuilt when the index doubles in size, independently if words have been removed or not. Based on performance testing (see new test_flann_index_perf test), we can see that it doesn't worth it to rebuild the index when we don't remove words (the cost of rebalancing the index is far more expensive than the decrease in recall caused by not rebalancing the index). However, rebalancing when many words have been removed (when memory management is enabled) is beneficial in terms of memory and computation time.

@MohammadWolf95 MohammadWolf95 mentioned this pull request Aug 11, 2026
@MohammadWolf95

Copy link
Copy Markdown
Contributor Author

I see that the Docker CI build failed because nanoflann is missing on the test server. I will update the CMakeLists.txt and add #ifdef WITH_NANOFLANN macros later to make this dependency completely optional.

I'll push these CMake fixes along with the Raspberry Pi 5 benchmark results as soon as I finish the speed tests!

@matlabbe

Copy link
Copy Markdown
Member

I can do the plumbing for the conditional build/parameters with nanoflann support. I'll have more time in a couple days to look deeper at this.

@MohammadWolf95

MohammadWolf95 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, that would be awesome if you could handle the CMake and conditional build plumbing!

Compiling on the Raspberry Pi 5 is taking quite a while, so I haven't finished those benchmarks yet. However, I have gathered the results on my x86 machine using the rgbd_dataset_freiburg1_desk dataset.

Here are the numbers:

0.23.7-jazzy branch (updated 2 months ago):

  • Without nanoflann: 20.0s
  • With nanoflann: 17.0s

Latest master branch:

  • Without nanoflann: 17.0s
  • With nanoflann: 15.2s

Command used for testing:

rtabmap-rgbd_dataset --camera_info /root/data/rgbd_dataset_freiburg1_desk/rtabmap_calib.yaml \
--output /root/data/rgbd_dataset_freiburg1_desk/rtabmap_rpi5_test.db --Odom/Strategy 1 --Vis/MinInliers 12 \
--Vis/MaxFeatures 800 --Vis/FeatureType 6 --Kp/DetectorStrategy 6 --Rtabmap/DetectionRate 2.0 \
--Vis/DepthAsMask false --Mem/DepthAsMask false --RGBD/OptimizeMaxError 10.0 /root/data/rgbd_dataset_freiburg1_desk

For my RPi5, I currently have a year-old Noetic build set up. Because of this, I can only share the benchmark results for that specific older build on ARM once I have some time to run it. Let me know if those numbers would still be useful to you!

@MohammadWolf95

MohammadWolf95 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor Author

I profiled the fresh master branch on x86 with and without nanoflann

The tests were performed on the TUM RGB-D Freiburg1 Desk dataset using the following configuration:

rtabmap-rgbd_dataset \
  --camera_info /root/data/rgbd_dataset_freiburg1_desk/rtabmap_calib.yaml \
  --output /root/data/rgbd_dataset_freiburg1_desk/rtabmap_rpi5_test.db \
  --Odom/Strategy 1 \
  --Vis/MinInliers 12 \
  --Vis/MaxFeatures 800 \
  --Vis/FeatureType 6 \
  --Kp/DetectorStrategy 6 \
  --Rtabmap/DetectionRate 2.0 \
  --Vis/DepthAsMask false \
  --Mem/DepthAsMask false \
  --RGBD/OptimizeMaxError 10.0 \
  /root/data/rgbd_dataset_freiburg1_desk

Execution time: 3 min

📊 gprof: BEFORE (with rtflann) — (click arrow to expand)
4,720,873,057 (17.40%)  ???:0x00000000000024c0 [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
1,874,330,797 ( 6.91%)  ???:inflate [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
1,170,389,121 ( 4.31%)  ???:void rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::searchLevel<false>(rtflann::ResultSet<float>&, float const*, rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float, int&, int, float, rtflann::Heap<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float> >*, rtflann::DynamicBitset&) const [/usr/local/lib/librtabmap_core.so.0.23.10]
1,145,969,728 ( 4.22%)  ???:0x0000000000014c50 [/usr/lib/x86_64-linux-gnu/libpng16.so.16.43.0]
1,071,022,776 ( 3.95%)  /opt/opencv/modules/features2d/src/orb.cpp:cv::ORB_Impl::detectAndCompute(cv::_InputArray const&, cv::_InputArray const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::_OutputArray const&, bool) [/usr/local/lib/libopencv_features2d.so.4.8.1]
1,011,007,410 ( 3.73%)  ./gmon/../sysdeps/x86_64/_mcount.S:mcount [/usr/lib/x86_64-linux-gnu/libc.so.6]
  842,544,680 ( 3.11%)  ???:adler32_z [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
  703,349,636 ( 2.59%)  /opt/opencv/modules/core/include/opencv2/core/base.hpp:cv::batchDistL1_32f(float const*, float const*, unsigned long, int, int, float*, unsigned char const*)
  659,017,271 ( 2.43%)  ???:void std::__adjust_heap<__gnu_cxx::__normal_iterator<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float>*, std::vector<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float>, std::allocator<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float> > > >, long, rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float>, __gnu_cxx::__ops::_Iter_comp_iter<rtflann::Heap<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float> >::CompareT> >(__gnu_cxx::__normal_iterator<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float>*, std::vector<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float>, std::allocator<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float> > > >, long, long, rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float>, __gnu_cxx::__ops::_Iter_comp_iter<rtflann::Heap<rtflann::BranchStruct<rtflann::KDTreeIndex<rtflann::L2_Simple<float> >::Node*, float> >::CompareT>) [clone .isra.0] [/usr/local/lib/librtabmap_core.so.0.23.10]
  597,963,142 ( 2.20%)  /opt/opencv/modules/core/src/lapack.cpp:void cv::JacobiSVDImpl_<double>(double*, unsigned long, double*, double*, unsigned long, int, int, int, double, double) [/usr/local/lib/libopencv_core.so.4.8.1]
  578,068,883 ( 2.13%)  /opt/opencv/modules/imgproc/src/featureselect.cpp:cv::goodFeaturesToTrack(cv::_InputArray const&, cv::_OutputArray const&, int, double, double, cv::_InputArray const&, cv::_OutputArray const&, int, int, bool, double) [/usr/local/lib/libopencv_imgproc.so.4.8.1]
  481,704,482 ( 1.78%)  ./gmon/./gmon/mcount.c:__mcount_internal [/usr/lib/x86_64-linux-gnu/libc.so.6]
  448,852,669 ( 1.65%)  /opt/opencv/modules/calib3d/src/calibration.cpp:cvProjectPoints2Internal(CvMat const*, CvMat const*, CvMat const*, CvMat const*, CvMat const*, CvMat*, CvMat*, CvMat*, CvMat*, CvMat*, CvMat*, CvMat*, double) [/usr/local/lib/libopencv_calib3d.so.4.8.1]
  362,446,080 ( 1.34%)  /opt/opencv/modules/imgproc/src/box_filter.simd.hpp:cv::opt_AVX2::(anonymous namespace)::RowSum<float, double>::operator()(unsigned char const*, unsigned char*, int, int) [/usr/local/lib/libopencv_imgproc.so.4.8.1]
  350,455,557 ( 1.29%)  ???:crc32_z [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
  321,625,440 ( 1.19%)  ???:0x0000000000021740 [/usr/lib/x86_64-linux-gnu/libpng16.so.16.43.0]
  308,886,970 ( 1.14%)  /opt/opencv/modules/core/src/matrix.cpp:cv::Mat::release() [/usr/local/lib/libopencv_core.so.4.8.1]
  306,945,649 ( 1.13%)  ./malloc/./malloc/malloc.c:_int_malloc [/usr/lib/x86_64-linux-gnu/libc.so.6]
  289,298,400 ( 1.07%)  ???:0x0000000000015fd0 [/usr/lib/x86_64-linux-gnu/libpng16.so.16.43.0]
  267,792,491 ( 0.99%)  ./malloc/./malloc/malloc.c:_int_free [/usr/lib/x86_64-linux-gnu/libc.so.6]

Execution time: 3 min

🚀 gprof: AFTER (with nanoflann) — (click arrow to expand)
5,204,982,203 (19.59%)  ???:0x00000000000024c0 [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
2,066,473,852 ( 7.78%)  ???:inflate [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
1,261,335,808 ( 4.75%)  ???:0x0000000000014c50 [/usr/lib/x86_64-linux-gnu/libpng16.so.16.43.0]
1,208,314,425 ( 4.55%)  /opt/opencv/modules/features2d/src/orb.cpp:cv::ORB_Impl::detectAndCompute(cv::_InputArray const&, cv::_InputArray const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::_OutputArray const&, bool) [/usr/local/lib/libopencv_features2d.so.4.8.1]
  927,379,852 ( 3.49%)  ???:adler32_z [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
  790,999,224 ( 2.98%)  /opt/opencv/modules/core/include/opencv2/core/base.hpp:cv::batchDistL1_32f(float const*, float const*, unsigned long, int, int, float*, unsigned char const*)
  682,540,423 ( 2.57%)  /opt/opencv/modules/core/src/lapack.cpp:void cv::JacobiSVDImpl_<double>(double*, unsigned long, double*, double*, unsigned long, int, int, int, double, double) [/usr/local/lib/libopencv_core.so.4.8.1]
  638,937,923 ( 2.40%)  /opt/opencv/modules/imgproc/src/featureselect.cpp:cv::goodFeaturesToTrack(cv::_InputArray const&, cv::_OutputArray const&, int, double, double, cv::_InputArray const&, cv::_OutputArray const&, int, int, bool, double) [/usr/local/lib/libopencv_imgproc.so.4.8.1]
  513,947,817 ( 1.93%)  /opt/opencv/modules/calib3d/src/calibration.cpp:cvProjectPoints2Internal(CvMat const*, CvMat const*, CvMat const*, CvMat const*, CvMat const*, CvMat*, CvMat*, CvMat*, CvMat*, CvMat*, CvMat*, CvMat*, double) [/usr/local/lib/libopencv_calib3d.so.4.8.1]
  399,180,480 ( 1.50%)  /opt/opencv/modules/imgproc/src/box_filter.simd.hpp:cv::opt_AVX2::(anonymous namespace)::RowSum<float, double>::operator()(unsigned char const*, unsigned char*, int, int) [/usr/local/lib/libopencv_imgproc.so.4.8.1]
  392,524,178 ( 1.48%)  /opt/opencv/modules/core/src/matrix.cpp:cv::Mat::release() [/usr/local/lib/libopencv_core.so.4.8.1]
  389,994,134 ( 1.47%)  ???:crc32_z [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
  354,003,840 ( 1.33%)  ???:0x0000000000021740 [/usr/lib/x86_64-linux-gnu/libpng16.so.16.43.0]
  319,827,195 ( 1.20%)  ./malloc/./malloc/malloc.c:_int_malloc [/usr/lib/x86_64-linux-gnu/libc.so.6]
  318,422,400 ( 1.20%)  ???:0x0000000000015fd0 [/usr/lib/x86_64-linux-gnu/libpng16.so.16.43.0]
  312,831,484 ( 1.18%)  ./malloc/./malloc/malloc.c:_int_free [/usr/lib/x86_64-linux-gnu/libc.so.6]
  292,583,724 ( 1.10%)  ./math/../sysdeps/x86_64/fpu/s_llrintf.S:lrintf [/usr/lib/x86_64-linux-gnu/libm.so.6]
  267,470,850 ( 1.01%)  ???:0x00000000000037b0 [/usr/lib/x86_64-linux-gnu/libz.so.1.3]
  263,165,914 ( 0.99%)  /opt/opencv/modules/core/src/trace.cpp:cv::utils::trace::details::Region::Region(cv::utils::trace::details::Region::LocationStaticStorage const&) [/usr/local/lib/libopencv_core.so.4.8.1]
  242,993,588 ( 0.91%)  /opt/opencv/modules/core/include/opencv2/core/fast_math.hpp:cv::ORB_Impl::detectAndCompute(cv::_InputArray const&, cv::_InputArray const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::_OutputArray const&, bool)
  240,089,674 ( 0.90%)  /opt/opencv/modules/core/src/copy.cpp:cv::Mat::copyTo(cv::_OutputArray const&) const [/usr/local/lib/libopencv_core.so.4.8.1]
  235,133,626 ( 0.88%)  /opt/opencv/modules/core/src/matrix.cpp:cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&) [/usr/local/lib/libopencv_core.so.4.8.1]
  231,874,835 ( 0.87%)  /opt/opencv/modules/imgproc/src/box_filter.simd.hpp:cv::opt_AVX2::(anonymous namespace)::ColumnSum<double, float>::operator()(unsigned char const**, unsigned char*, int, int, int) [/usr/local/lib/libopencv_imgproc.so.4.8.1]
  229,096,452 ( 0.86%)  ./string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms [/usr/lib/x86_64-linux-gnu/libc.so.6]
  226,991,468 ( 0.85%)  ./malloc/./malloc/malloc.c:malloc [/usr/lib/x86_64-linux-gnu/libc.so.6]
  212,812,800 ( 0.80%)  /usr/lib/gcc/x86_64-linux-gnu/13/include/avxintrin.h:cv::calcMinEigenValLine_AVX(float const*, float*, int)
  205,175,670 ( 0.77%)  ./gmon/../sysdeps/x86_64/_mcount.S:mcount [/usr/lib/x86_64-linux-gnu/libc.so.6]
  202,143,499 ( 0.76%)  ???:bool nanoflann::KDTreeBaseClass<nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<float, rtabmap::NanoFlannIndex::PointCloudAdapter, float, unsigned long>, rtabmap::NanoFlannIndex::PointCloudAdapter, 2, unsigned int>, nanoflann::L2_Simple_Adaptor<float, rtabmap::NanoFlannIndex::PointCloudAdapter, float, unsigned long>, rtabmap::NanoFlannIndex::PointCloudAdapter, 2, unsigned int>::searchLevel<nanoflann::RadiusResultSet<float, unsigned int> >(nanoflann::RadiusResultSet<float, unsigned int>&, float const*, nanoflann::KDTreeBaseClass<nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<float, rtabmap::NanoFlannIndex::PointCloudAdapter, float, unsigned long>, rtabmap::NanoFlannIndex::PointCloudAdapter, 2, unsigned int>, nanoflann::L2_Simple_Adaptor<float, rtabmap::NanoFlannIndex::PointCloudAdapter, float, unsigned long>, rtabmap::NanoFlannIndex::PointCloudAdapter, 2, unsigned int>::Node*, float, std::array<float, 2ul>&, float) const'2 [/usr/local/lib/librtabmap_core.so.0.23.10]
  200,450,880 ( 0.75%)  /usr/lib/gcc/x86_64-linux-gnu/13/include/emmintrin.h:cv::cornerEigenValsVecsLine_AVX(float const*, float const*, float*, int)
  176,707,365 ( 0.66%)  /opt/opencv/modules/core/src/matrix.cpp:cv::Mat::operator=(cv::Mat const&) [/usr/local/lib/libopencv_core.so.4.8.1]

Summary of Changes & Profiling Analysis:

  1. Massive Search Speedup (~5.6x improvement):
    As shown in the profiles, the heavy rtflann::KDTreeIndex::searchLevel (4.31%) and its accompanying std::__adjust_heap (2.43%) completely vanished from the top functions. Instead, the newly integrated nanoflann search loop takes only 0.76% of the entire application CPU time:

    202,143,499 ( 0.76%)  ???:bool nanoflann::KDTreeBaseClass<...>::searchLevel<...>(...) const
    
  2. Zero-Allocation Data Adapter:
    Instead of copying coordinates into a temporary matrix via slow element-by-element loops (cv::Mat::at<float>), I implemented a custom rtabmap::NanoFlannIndex::PointCloudAdapter. This allows nanoflann to query RTAB-Map's native point structures directly from memory with zero copying overhead.

  3. Strict Memory Control:
    The malloc and _int_malloc overhead remained strictly identical (~1.20% before and after), which proves that the new nanoflann implementation runs entirely in zero-allocation mode inside the real-time frame loop without putting any pressure on the heap allocator. (The remaining malloc calls are exclusively bounded to libpng image decompression).

This optimization flattens a substantial computational bottleneck in the visual odometry radius search stage, reclaiming critical CPU cycles for edge computing and low-power robotic hardware.

  • Real-World Embedded Benchmarks (Raspberry Pi 5):
    On low-power edge hardware running the 0.21.13-noetic branch, this optimization successfully boosted the pure visual odometry frame rate from 7 FPS to 8 FPS (~14.2% clear speedup). Note that the overall frame rate is now bottlenecked by other stages of the pipeline

…ndex. Added tests. Vendoring nanoflann.h directly in the repo. RegistrationVis now use NANOFLANN_INDEX_KDTREE_SINGLE (instead of FLANN_INDEX_KDTREE_SINGLE) flann index for 2d points matching.
@matlabbe

Copy link
Copy Markdown
Member

While looking at what nanoflann is good at, it cannot be a full replacement of rtflann, in particular it is very slow on high dimension descriptors. It is however faster for 2d points matching for visual odometry.

I added performance tests to compare all flann strategies:

~/workspace/rtabmap/build$ ./bin/test_flann_index_perf 
Running main() from /home/mathieu/workspace/rtabmap/build/_deps/googletest-src/googletest/src/gtest_main.cc
[==========] Running 8 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 8 tests from FlannIndexPerfTest
[ RUN      ] FlannIndexPerfTest.AllBackendsOnPointClouds
[          ] 2D cloud of 100000 points, 10000 queries, knn=2, rebalancing factor=1
[          ]   linear    exhaustive                 build=    0.4 ms knn= 1674.9 ms radius= 1974.4 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=   72.2 ms knn=   54.7 ms radius=   73.3 ms memory= 25464 KB recall= 98.9 %
[          ]   rtflann   kd-tree single             build=   12.7 ms knn=    3.5 ms radius=   14.0 ms memory=  1747 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   11.6 ms knn=    3.6 ms radius=   14.8 ms memory=  2079 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=   22.5 ms knn=    7.3 ms radius=   21.9 ms memory=  9405 KB recall=100.0 %
[          ] 2D cloud of 100000 points, 10000 queries, knn=2, rebalancing factor=2
[          ]   linear    exhaustive                 build=   10.8 ms knn= 1665.8 ms radius= 1954.3 ms memory= 10547 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=   81.3 ms knn=   51.7 ms radius=   67.4 ms memory= 36011 KB recall= 99.0 %
[          ]   rtflann   kd-tree single             build=   23.4 ms knn=    3.6 ms radius=   13.6 ms memory= 12294 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   11.3 ms knn=    3.5 ms radius=   14.5 ms memory=  2079 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=   21.9 ms knn=    7.8 ms radius=   22.1 ms memory=  9405 KB recall=100.0 %
[          ] 3D cloud of 100000 points, 10000 queries, knn=2, rebalancing factor=1
[          ]   linear    exhaustive                 build=    0.0 ms knn= 1859.5 ms radius= 2273.3 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=   71.1 ms knn=   65.3 ms radius=   44.3 ms memory= 25464 KB recall= 94.4 %
[          ]   rtflann   kd-tree single             build=   14.4 ms knn=    5.7 ms radius=    3.7 ms memory=  1744 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   11.9 ms knn=    5.6 ms radius=    3.4 ms memory=  2074 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=   25.6 ms knn=   10.5 ms radius=    5.4 ms memory= 10974 KB recall=100.0 %
[          ] 3D cloud of 100000 points, 10000 queries, knn=2, rebalancing factor=2
[          ]   linear    exhaustive                 build=   10.4 ms knn= 1854.6 ms radius= 2274.4 ms memory= 10547 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=   84.8 ms knn=   63.4 ms radius=   44.7 ms memory= 36011 KB recall= 93.7 %
[          ]   rtflann   kd-tree single             build=   24.8 ms knn=    5.6 ms radius=    3.4 ms memory= 12291 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   12.1 ms knn=    5.6 ms radius=    3.6 ms memory=  2074 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=   25.6 ms knn=   11.5 ms radius=    5.3 ms memory= 10974 KB recall=100.0 %
[       OK ] FlannIndexPerfTest.AllBackendsOnPointClouds (16787 ms)
[ RUN      ] FlannIndexPerfTest.AllBackendsOnFloatDescriptors
[          ] 32D float descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive                 build=    0.0 ms knn=  106.6 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=   82.9 ms knn=    2.3 ms memory= 12732 KB recall= 93.5 %
[          ]   rtflann   kd-tree single             build=   30.9 ms knn=    2.1 ms memory=   969 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   13.4 ms knn=    2.8 ms memory=  1831 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=   45.3 ms knn=    7.4 ms memory=  6260 KB recall=100.0 %
[          ] 64D float descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive                 build=    0.0 ms knn=  214.2 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=  112.9 ms knn=    2.6 ms memory= 12732 KB recall= 92.8 %
[          ]   rtflann   kd-tree single             build=   58.8 ms knn=    7.3 ms memory=   982 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   20.2 ms knn=   18.0 ms memory=  1840 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=   86.7 ms knn=   49.4 ms memory=  6260 KB recall=100.0 %
[          ] 128D float descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive                 build=    0.0 ms knn=  501.4 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=  172.1 ms knn=    3.2 ms memory= 12732 KB recall= 89.2 %
[          ]   rtflann   kd-tree single             build=  117.5 ms knn=   42.3 ms memory=   985 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   33.2 ms knn=  153.8 ms memory=  1833 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=  209.5 ms knn=  418.4 ms memory=  6260 KB recall=100.0 %
[          ] 256D float descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive                 build=    0.0 ms knn= 1064.8 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=  255.8 ms knn=    3.7 ms memory= 12732 KB recall= 86.5 %
[          ]   rtflann   kd-tree single             build=  222.6 ms knn=  270.8 ms memory=   984 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   58.8 ms knn= 1088.3 ms memory=  1845 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=  511.6 ms knn= 1832.2 ms memory=  6260 KB recall=100.0 %
[       OK ] FlannIndexPerfTest.AllBackendsOnFloatDescriptors (7963 ms)
[ RUN      ] FlannIndexPerfTest.AllBackendsOnBinaryDescriptors
[          ] 256 bits binary descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive (hamming)       build=    0.1 ms knn=   67.9 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   LSH                        build=  114.9 ms knn=   24.0 ms memory=   195 KB recall= 86.2 %
[          ] 512 bits binary descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive (hamming)       build=    0.0 ms knn=  112.6 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   LSH                        build=  113.0 ms knn=   28.7 ms memory=   195 KB recall= 90.5 %
[       OK ] FlannIndexPerfTest.AllBackendsOnBinaryDescriptors (559 ms)
[ RUN      ] FlannIndexPerfTest.IncrementalInsertionSpeed
[          ] 3D cloud of 10000 points, 500 points added one by one, rebalancing factor=1
[          ]   linear    exhaustive                 insertions=    0.1 ms memory=    53 KB
[          ]   rtflann   kd-tree (4 randomized)     insertions=    0.6 ms memory=  2726 KB
[          ]   rtflann   kd-tree single             insertions=  512.0 ms memory=   235 KB
[          ]   nanoflann kd-tree single incremental insertions=    0.2 ms memory=  1348 KB
[          ] 3D cloud of 10000 points, 500 points added one by one, rebalancing factor=2
[          ]   linear    exhaustive                 insertions=    0.1 ms memory=  1107 KB
[          ]   rtflann   kd-tree (4 randomized)     insertions=    0.5 ms memory=  3781 KB
[          ]   rtflann   kd-tree single             insertions=  512.9 ms memory=  1289 KB
[          ]   nanoflann kd-tree single incremental insertions=    0.1 ms memory=  1348 KB
[       OK ] FlannIndexPerfTest.IncrementalInsertionSpeed (1048 ms)
[ RUN      ] FlannIndexPerfTest.RebalancingFactorOnAGrowingIndex
[          ] 128D float descriptors, 5000 indexed then 10000 added one by one, 500 queries, knn=2
[          ]   rtflann   kd-tree (4 randomized)     factor=1 insertions=   11.1 ms knn=    3.5 ms memory=  4874 KB recall= 81.9 %
[          ]   rtflann   kd-tree (4 randomized)     factor=2 insertions=   11.4 ms knn=    3.4 ms memory=  5401 KB recall= 81.0 %
[          ]   nanoflann kd-tree single incremental factor=1 insertions=   42.4 ms knn=  127.5 ms memory=  3492 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental factor=2 insertions=   41.6 ms knn=  129.6 ms memory=  3492 KB recall=100.0 %
[       OK ] FlannIndexPerfTest.RebalancingFactorOnAGrowingIndex (634 ms)
[ RUN      ] FlannIndexPerfTest.RebalancingFactorOnAChurningIndex
[          ] 128D float descriptors, 5000 indexed, then 10000 added and as many removed
[          ]   rtflann   kd-tree (4 randomized)     factor=1 add+remove=   13.6 ms knn=   13.7 ms memory=  4913 KB
[          ]   rtflann   kd-tree (4 randomized)     factor=2 add+remove=   28.4 ms knn=    8.6 ms memory=  3619 KB
[          ]   nanoflann kd-tree single incremental factor=1 add+remove=  321.8 ms knn=   68.1 ms memory=  3172 KB
[          ]   nanoflann kd-tree single incremental factor=2 add+remove=  206.5 ms knn=   60.0 ms memory=  2612 KB
[       OK ] FlannIndexPerfTest.RebalancingFactorOnAChurningIndex (788 ms)
[ RUN      ] FlannIndexPerfTest.GrownIndexAgainstFreshlyBuiltOne
[          ] 128D float descriptors, 20000 indexed, 200 queries, knn=2, averaged over 3 seeds
[          ]   all indexed in one go                build=    63.6 ms knn=    2.3 ms memory=   5092 KB recall= 99.7 %
[          ]   grown    2x (   10000 indexed first) build=    43.3 ms knn=    2.1 ms memory=   6147 KB recall= 99.6 %
[          ]   grown   10x (    2000 indexed first) build=    26.5 ms knn=    2.1 ms memory=   6991 KB recall= 98.9 %
[          ]   grown  100x (     200 indexed first) build=    23.5 ms knn=    2.2 ms memory=   7181 KB recall= 98.8 %
[          ]   grown 1000x (      20 indexed first) build=    23.0 ms knn=    2.3 ms memory=   7200 KB recall= 98.5 %
[       OK ] FlannIndexPerfTest.GrownIndexAgainstFreshlyBuiltOne (784 ms)
[ RUN      ] FlannIndexPerfTest.RecallAgainstTheFractionOfRemovedPoints
[          ] 128D float descriptors, 10000 indexed then partly removed, 200 queries, knn=2, averaged over 3 seeds
[          ]     0% removed, kept in the index      build=    30.9 ms knn=    1.9 ms memory=   2546 KB recall= 99.9 %
[          ]     0% removed, rebuilt without them   build=    30.8 ms knn=    1.9 ms memory=   2546 KB recall= 99.9 %
[          ]    25% removed, kept in the index      build=    31.1 ms knn=    2.2 ms memory=   2556 KB recall= 99.9 %
[          ]    25% removed, rebuilt without them   build=    23.0 ms knn=    1.6 ms memory=   1909 KB recall= 99.8 %
[          ]    50% removed, kept in the index      build=    30.7 ms knn=    2.7 ms memory=   2566 KB recall= 99.6 %
[          ]    50% removed, rebuilt without them   build=    14.9 ms knn=    1.5 ms memory=   1273 KB recall= 99.7 %
[          ]    75% removed, kept in the index      build=    30.8 ms knn=    4.2 ms memory=   2575 KB recall= 95.0 %
[          ]    75% removed, rebuilt without them   build=     7.4 ms knn=    1.3 ms memory=    636 KB recall= 94.8 %
[          ]    90% removed, kept in the index      build=    30.8 ms knn=    8.0 ms memory=   2581 KB recall= 83.6 %
[          ]    90% removed, rebuilt without them   build=     2.9 ms knn=    1.1 ms memory=    254 KB recall= 83.3 %
[       OK ] FlannIndexPerfTest.RecallAgainstTheFractionOfRemovedPoints (1039 ms)

Basically, what it shows is that for 2D/3D point clouds, nanoflann is faster to build and also faster to make queries.

[ RUN      ] FlannIndexPerfTest.AllBackendsOnPointClouds
[          ] 2D cloud of 100000 points, 10000 queries, knn=2, rebalancing factor=1
[          ]   linear    exhaustive                 build=    0.4 ms knn= 1674.9 ms radius= 1974.4 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=   72.2 ms knn=   54.7 ms radius=   73.3 ms memory= 25464 KB recall= 98.9 %
[          ]   rtflann   kd-tree single             build=   12.7 ms knn=    3.5 ms radius=   14.0 ms memory=  1747 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   11.6 ms knn=    3.6 ms radius=   14.8 ms memory=  2079 KB recall=100.0 %

However, for high dimensional descriptors (visual dictionary), it is significantly worst for queries:

[          ] 64D float descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive                 build=    0.0 ms knn=  214.2 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=  112.9 ms knn=    2.6 ms memory= 12732 KB recall= 92.8 %
[          ]   rtflann   kd-tree single             build=   58.8 ms knn=    7.3 ms memory=   982 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   20.2 ms knn=   18.0 ms memory=  1840 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=   86.7 ms knn=   49.4 ms memory=  6260 KB recall=100.0 %
[          ] 128D float descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive                 build=    0.0 ms knn=  501.4 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=  172.1 ms knn=    3.2 ms memory= 12732 KB recall= 89.2 %
[          ]   rtflann   kd-tree single             build=  117.5 ms knn=   42.3 ms memory=   985 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   33.2 ms knn=  153.8 ms memory=  1833 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=  209.5 ms knn=  418.4 ms memory=  6260 KB recall=100.0 %
[          ] 256D float descriptors, 50000 indexed, 300 queries, knn=2
[          ]   linear    exhaustive                 build=    0.0 ms knn= 1064.8 ms memory=     0 KB recall=100.0 %
[          ]   rtflann   kd-tree (4 randomized)     build=  255.8 ms knn=    3.7 ms memory= 12732 KB recall= 86.5 %
[          ]   rtflann   kd-tree single             build=  222.6 ms knn=  270.8 ms memory=   984 KB recall=100.0 %
[          ]   nanoflann kd-tree single             build=   58.8 ms knn= 1088.3 ms memory=  1845 KB recall=100.0 %
[          ]   nanoflann kd-tree single incremental build=  511.6 ms knn= 1832.2 ms memory=  6260 KB recall=100.0 %

Stereo 20 Hz integration benchmark

data/tests/stereo_20Hz.db, 1035 frames, odometry time reported by
test_rtabmap_integration --gtest_filter=*Stereo20Hz*. All three runs on the
same machine under nice -19, one run per configuration. The only difference
is the index used by the guess-projection matching of RegistrationVis
(Vis/CorGuessWinSize). CPU: 11th Gen Intel® Core™ i7-11700KF @ 3.60GHz × 16

Variant master (rtflann forest) FLANN_INDEX_KDTREE_SINGLE NANOFLANN_INDEX_KDTREE_SINGLE
g2o 16.98 s 15.99 s (−5.8 %) 15.18 s (−10.6 %)
gtsam 53.82 s 50.42 s (−6.3 %) 50.96 s (−5.3 %)
ceres 18.68 s 16.42 s (−12.1 %) 16.57 s (−11.3 %)
g2o_no_optical_flow 18.58 s 16.82 s (−9.5 %) 17.01 s (−8.5 %)
g2o_no_min_eigenvals 17.03 s 15.06 s (−11.6 %) 15.61 s (−8.3 %)
g2o_stereo_to_depth 15.76 s 13.76 s (−12.7 %) 14.19 s (−9.9 %)

What it shows

  • ~5–13 % less odometry time than master, consistent in direction over all
    six variants.
  • The win is the structure, not the library. master indexed the frame
    keypoints with rtflann::KDTreeIndexParams(), i.e. 4 randomized trees
    searched approximately, for an index that is built once, searched once and
    thrown away every frame. A single exact kd-tree is the right structure there,
    and rtflann's and nanoflann's are indistinguishable end to end (rtflann's is
    ahead in 5 variants of 6, by less than the run-to-run spread).

At the level of that call alone (1000 keypoints indexed, as many looked up in a
40 px radius, bin/test_flann_index_perf --gtest_filter=*RegistrationGuessMatching):

build + search
rtflann kd-tree (4 randomized) 5.709 ms/frame
rtflann kd-tree single 0.469 ms/frame
nanoflann kd-tree single 0.400 ms/frame

Caveats

  • Run-to-run spread is ±1 s (±6 %) on this test: the same nanoflann build
    measured 16.22 s and 15.18 s for the g2o variant across two runs. The
    master-vs-single-tree difference is above that floor and consistent across
    variants; the difference between the two single-tree implementations is not,
    and should be read as "no measurable difference".
  • Trajectory accuracy is unchanged in aggregate and moves in both directions
    (g2o 0.061 → 0.069 m, gtsam 0.062 → 0.060 m, ceres 0.056 → 0.060 m RMSE).
    Expected: master searched the forest approximately (checks=32), so an exact
    tree returns a slightly different correspondence set.
  • One run per configuration; tightening the percentages would need 3–5 repeats.

@MohammadWolf95

MohammadWolf95 commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @matlabbe,

Thanks for the feedback and for testing the high-dimensional descriptors!

To clarify, I completely agree with your benchmarks. I only focused on the 2D/3D point matching stage because I knew nanoflann shines in low-dimensional spaces. I wasn't actually aware that RTAB-Map routed high-dimensional visual descriptors (like SIFT/SURF) through the same FLANN backend in VWDictionary. Your 64D–256D tests perfectly show how a single KD-tree degrades in high-dimensional spaces and prove why the randomized forest in rtflann is still strictly necessary there.

Your change in commit 0a4d369 to separate NanoFlann into its own NN type makes perfect sense. Now that we isolated it, do you see other areas in RTAB-Map (e.g., proximity detection or laser scan matching / ICP) where we could reuse this zero-copy PointCloudAdapter approach to further eliminate heap allocation pressure on low-power embedded hardware?

Let me know if you need any further changes from my side

@MohammadWolf95

Copy link
Copy Markdown
Contributor Author

@matlabbe, What do you think about testing this PointCloudAdapter pattern together with nanoflann inside RegistrationIcp::computeTransformationImpl? Since it's a high-frequency, heavy part of the pipeline, we could adapt the structure for 3D data like this:

struct LaserScan3DAdapter {
    const rtabmap::LaserScan& scan;
    LaserScan3DAdapter(const rtabmap::LaserScan& s) : scan(s) {}

    inline size_t kdtree_get_point_count() const { return scan.size(); }
    inline float kdtree_get_pt(const size_t idx, const size_t dim) const {
        if (!scan.data().empty() && scan.dataType() == cv::CV_32FC3) {
            const cv::Vec3f* ptr = scan.data().ptr<cv::Vec3f>();
            return (dim == 0) ? ptr[idx][0] : ((dim == 1) ? ptr[idx][1] : ptr[idx][2]);
        }
        return 0.0f;
    }
    template <class BBOX> bool kdtree_get_bbox(BBOX& /* bb */) const { return false; }
};

Currently, inside the Point To Plane and Point To Point blocks, heavy conversions happen at the very beginning of the branch:

pcl::PointCloud<pcl::PointXYZINormal>::Ptr fromCloudNormals = util3d::laserScanToPointCloudINormal(fromScan, fromScan.localTransform());

This forces deep copying and heap allocations on every single frame, even if _strategy == 1 (libpointmatcher) is selected and the PCL clouds are not actually used for the core ICP matching calculations.

With LaserScan3DAdapter, we could completely avoid or delay these conversions, building the nanoflann index directly on top of the native scan.data() memory when the built-in ICP (_strategy == 0) is used. Do you think this would be worth exploring to further reduce heap allocation pressure during the ICP stage?

Let me know if you need any further adjustments for the current PR

@matlabbe
matlabbe merged commit f647014 into introlab:master Aug 16, 2026
29 checks passed
@matlabbe

Copy link
Copy Markdown
Member

Just saw your two comments after merging. Anyway, what is discussed could be done in another PR.

Internally, PCL is using original flann library and it expects input format to be a pcl::PointCloud. Not sure how could we make a pcl::PointCloud adapter for a LaserScan object without any copy. Note also that a transform is applied on the points, which requires a copy indirectly anyway.

if(pointToPlane)
{
pcl::PointCloud<pcl::PointXYZINormal>::Ptr fromCloudNormals = util3d::laserScanToPointCloudINormal(fromScan, fromScan.localTransform());
pcl::PointCloud<pcl::PointXYZINormal>::Ptr toCloudNormals = util3d::laserScanToPointCloudINormal(toScan, guess * toScan.localTransform());
fromCloudNormals = util3d::removeNaNNormalsFromPointCloud(fromCloudNormals);
toCloudNormals = util3d::removeNaNNormalsFromPointCloud(toCloudNormals);

// Point To Point
////////////////////
else
{
pcl::PointCloud<pcl::PointXYZI>::Ptr fromCloud = util3d::laserScanToPointCloudI(fromScan, fromScan.localTransform());
pcl::PointCloud<pcl::PointXYZI>::Ptr toCloud = util3d::laserScanToPointCloudI(toScan, guess * toScan.localTransform());

Looking at pcl::PointCloud, they have these fields which could remove the need to transform the points so that an adapter over the LaserScan data "may" be possible:

      /** \brief Sensor acquisition pose (origin/translation). */
      Eigen::Vector4f    sensor_origin_ = Eigen::Vector4f::Zero ();
      /** \brief Sensor acquisition pose (rotation). */
      Eigen::Quaternionf sensor_orientation_ = Eigen::Quaternionf::Identity ();

However, all ICP functions from PCL require a pcl::PointCloud<> templated format, which may not be possible to provide from data structure inside LaserScan format. I checked if pcl's icp can support PCLPointcloud2, though when used PCL will convert to templated format internally anyway.

Here are some possible optimizations that could be done though. We could spin off these two in separate PRs.

Saving copies

Checking up with Claude:

If cutting copies is the goal, the tractable target is inside RegistrationIcp rather than under PCL. The point-to-plane branch currently copies each scan three times: laserScanToPointCloudINormal, then removeNaNNormalsFromPointCloud (RegistrationIcp.cpp:712-713), then transformPointCloud (:746). The NaN removal could be folded into the conversion, and the transform applied in place.

  1. Add option to remove NANs from these functions during the conversion:
    /** @defgroup LaserScanToPointCloud LaserScan to PCL
    * @brief Functions to convert LaserScan data to various PCL point cloud formats.
    *
    * These functions convert an instance of rtabmap::LaserScan to
    * different PCL point cloud types, optionally applying a spatial transform.
    * Missing data such as RGB and intensity are handled with default values.
    * If the input LaserScan is 2D, as PCL doesn't have standard format that is just 2D,
    * Z will be set to zero. If the input LaserScan doesn't have normals but the output does,
    * the normals are set to zeros.
    *
    * laserScanToPoint() functions convert a single point from the LaserScan at the specificied index.
    * @{
    */
    /**
    * @brief Convert rtabmap::LaserScan to `pcl::PCLPointCloud2` with all supported fields (see rtabmap::LaserScan::Format)
    */
    pcl::PCLPointCloud2::Ptr RTABMAP_CORE_EXPORT laserScanToPointCloud2(const LaserScan & laserScan, const Transform & transform = Transform());
    /** @brief LaserScan &rarr; `PointXYZ` (x, y, z); any other field of the scan is dropped. */
    pcl::PointCloud<pcl::PointXYZ>::Ptr RTABMAP_CORE_EXPORT laserScanToPointCloud(const LaserScan & laserScan, const Transform & transform = Transform());
    /** @brief LaserScan &rarr; `PointNormal` (x, y, z, nx, ny, nz); normals are zeroed if the scan has none. */
    pcl::PointCloud<pcl::PointNormal>::Ptr RTABMAP_CORE_EXPORT laserScanToPointCloudNormal(const LaserScan & laserScan, const Transform & transform = Transform());
    /** @brief LaserScan &rarr; `PointXYZRGB` (x, y, z, rgb); @p r, @p g and @p b are used if the scan has no color. */
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr RTABMAP_CORE_EXPORT laserScanToPointCloudRGB(const LaserScan & laserScan, const Transform & transform = Transform(), unsigned char r = 100, unsigned char g = 100, unsigned char b = 100);
    /** @brief LaserScan &rarr; `PointXYZI` (x, y, z, intensity); @p intensity is used if the scan has none. */
    pcl::PointCloud<pcl::PointXYZI>::Ptr RTABMAP_CORE_EXPORT laserScanToPointCloudI(const LaserScan & laserScan, const Transform & transform = Transform(), float intensity = 0.0f);
    /** @brief LaserScan &rarr; `PointXYZRGBNormal` (x, y, z, rgb, nx, ny, nz); missing color and normals are filled as above. */
    pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr RTABMAP_CORE_EXPORT laserScanToPointCloudRGBNormal(const LaserScan & laserScan, const Transform & transform = Transform(), unsigned char r = 100, unsigned char g = 100, unsigned char b = 100);
    /** @brief LaserScan &rarr; `PointXYZINormal` (x, y, z, intensity, nx, ny, nz); missing intensity and normals are filled as above. */
    pcl::PointCloud<pcl::PointXYZINormal>::Ptr RTABMAP_CORE_EXPORT laserScanToPointCloudINormal(const LaserScan & laserScan, const Transform & transform = Transform(), float intensity = 0.0f);
  2. Make in-place conversion versions of these functions:
    /**
    * @defgroup TransformPointcloud Transform PCL Point Clouds
    * @brief Transforms various PCL point cloud types using the given transform.
    *
    * - For types with normals, the normals are transformed appriopriatly.
    * - If indices are provided, the returned point cloud contains only points matching the indices.
    *
    * @param cloud The input point cloud.
    * @param indices Optional subset of indices of the points to transform.
    * @param transform The transform to apply.
    * @return A new point cloud with the transform applied.
    * @{
    */
    /** @brief Transforms `pcl::PointXYZ` point cloud type. */
    pcl::PointCloud<pcl::PointXYZ>::Ptr RTABMAP_CORE_EXPORT transformPointCloud(
    const pcl::PointCloud<pcl::PointXYZ>::Ptr & cloud,
    const Transform & transform);
    /** @brief Transforms `pcl::PointXYZI` point cloud type. */
    pcl::PointCloud<pcl::PointXYZI>::Ptr RTABMAP_CORE_EXPORT transformPointCloud(
    const pcl::PointCloud<pcl::PointXYZI>::Ptr & cloud,
    const Transform & transform);
    /** @brief Transforms `pcl::PointXYZRGB` point cloud type. */
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr RTABMAP_CORE_EXPORT transformPointCloud(
    const pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
    const Transform & transform);
    /** @brief Transforms `pcl::PointNormal` point cloud type. */
    pcl::PointCloud<pcl::PointNormal>::Ptr RTABMAP_CORE_EXPORT transformPointCloud(
    const pcl::PointCloud<pcl::PointNormal>::Ptr & cloud,
    const Transform & transform);
    /** @brief Transforms `pcl::PointXYZRGBNormal` point cloud type. */
    pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr RTABMAP_CORE_EXPORT transformPointCloud(
    const pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr & cloud,
    const Transform & transform);
    /** @brief Transforms `pcl::PointXYZINormal` point cloud type. */
    pcl::PointCloud<pcl::PointXYZINormal>::Ptr RTABMAP_CORE_EXPORT transformPointCloud(
    const pcl::PointCloud<pcl::PointXYZINormal>::Ptr & cloud,
    const Transform & transform);

Using nanoflann in PCL's ICP

pcl::IterativeClosestPoint has setSearchMethodTarget() and setSearchMethodSource() (see this) that can be used to provide our own implementation of KdTree. It would require to do something like https://pointclouds.org/documentation/kdtree__flann_8h_source.html but with nanoflann instead, thu creating a new class KdTreeNanoFLANN. On rtabmap side, we would need to add a parameter to select nanoflann and setup it here

pcl::IterativeClosestPoint<PointT, PointT> icp;
// Set the input source and target
icp.setInputTarget (cloud_target);
icp.setInputSource (cloud_source);
if(icp2D)
{
typename pcl::registration::TransformationEstimation2D<PointT, PointT>::Ptr est;
est.reset(new pcl::registration::TransformationEstimation2D<PointT, PointT>);
icp.setTransformationEstimation(est);
}
// Set the max correspondence distance (e.g., correspondences with higher distances will be ignored)
icp.setMaxCorrespondenceDistance (maxCorrespondenceDistance);
// Set the maximum number of iterations (criterion 1)
icp.setMaximumIterations (maximumIterations);
// Set the transformation epsilon (criterion 2)
icp.setTransformationEpsilon (epsilon*epsilon);
// Set the euclidean distance difference epsilon (criterion 3)
//icp.setEuclideanFitnessEpsilon (-std::numeric_limits<double>::max());

@MohammadWolf95

Copy link
Copy Markdown
Contributor Author

@matlabbe, Thanks for the detailed breakdown! Your ideas about folding the NaN removal into the conversion and creating a custom KdTreeNanoFLANN wrapper sound great.I would love to work on these tasks! I'll spin them off into separate PRs as soon as I have some free time.Thanks again for the great collaboration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants