c++optimizationcgalr-treerange-tree

Memory issue with CGAL RTree when working with 39,651,210 2D points


I am using the CGAL RTree to find 2D points within an interval in a point cloud. The size of the point cloud is 39,651,210 points. However, when I pass these points to the RTree, it causes a massive increase in RAM usage, going from 15 GB to 60 GB in the Windows Task Manager. As a result, I am forced to terminate the program execution.

Here the code:

// ... (previous code)
typedef CGAL::Exact_predicates_inexact_constructions_kernel EPIC;
typedef EPIC::Point_2 Point_2;
// bool because only the key itself is relevent, not the value.
typedef CGAL::Range_tree_map_traits_2<EPIC, bool> Traits;
typedef CGAL::Range_tree_2<Traits> Range_tree_2;
typedef Traits::Key Key;
typedef Traits::Interval Interval;
std::vector<Key> rTreeInput;

// Create a vector to hold the input for the RTree
std::vector<Key> rTreeInput;
rTreeInput.reserve(numPoints);

// Populate the vector with the 2D points as keys and dummy boolean as values
for (size_t pointIndex = 0; pointIndex < numPoints; ++pointIndex) {
    const size_t offset = pointIndex * pointDimension;
    const size_t xCoordinateIndex = offset;
    const size_t yCoordinateIndex = offset + 1;
    rTreeInput.push_back(Key(Point_2(pPoints[xCoordinateIndex], pPoints[yCoordinateIndex]), false));
}

// Create the RTree using the populated vector
Range_tree_2 rTree(rTreeInput.begin(), rTreeInput.end()); // <-- causes the RAM explosion

// ... (remaining code)

I noticed that the CGAL RTree expects a Key-Value pair, but I am only interested in the points. To optimize for efficiency, I have set a dummy boolean value as the "value" part of the pair and only work with the "keys" since they represent the points of interest.

Is there a way to optimize my code to reduce this excessive memory usage? The current memory consumption is not acceptable for my application. Any suggestions or insights would be greatly appreciated.


Solution

  • To overcome this issue, I replaced the CGAL RTree with the RTree from the Boost library. Using the Boost RTree, I achieved significantly reduced memory consumption, and my application now works smoothly with the same dataset.

    In conclusion, if you encounter excessive memory usage with the CGAL RTree when dealing with large datasets, consider trying the Boost RTree as a potential alternative, as it might provide a more memory-efficient solution for your specific use case.

    I hope this information helps others who might encounter a similar issue with the CGAL RTree and are looking for potential solutions.