Skip to content

Conversation

@algol83
Copy link
Contributor

@algol83 algol83 commented Nov 15, 2025

Summary

This PR adds a new findWithinBox method to KDTree that provides direct bounding box search functionality with significant performance improvements over the traditional radius search approach.

Motivation

Currently, searching for points within an axis-aligned bounding box requires:

  1. Performing a radius search from the box center
  2. Manually filtering results to exclude points outside the box boundaries

This approach is inefficient, especially for 2D spatial queries where we need to find or count all points within rectangular regions.

Changes

New functionality

  • Added findWithinBox method that directly traverses the KD-tree using box boundaries for pruning
  • Eliminates the need for over-searching and post-filtering

Performance improvements

Benchmarked on a 300k point dataset in 2D space:

  • 2x to 300x speedup compared to radius search + filtering
  • Largest improvements (up to 300x) when searching rectangular regions
  • Consistent performance gains across various box sizes
  • Particularly beneficial for counting points in regions or exhaustive region searches

Testing

  • Added randomized tests for search completeness, following the existing test patterns
  • Verified that all points within the box are found correctly

Minor formatting

  • 5 lines adjusted by clang-format for consistency with project style

@jlblancoc
Copy link
Owner

@algol83 awesome contribution, thanks!

It totally LGTM and tests pass, so it's perfect. I only miss updating the README to show this new API, but I'm doing it myself. Again, thank you!

@jlblancoc jlblancoc merged commit c5a339f into jlblancoc:master Nov 16, 2025
6 checks passed
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