Skip to content

Conversation

Copy link

Copilot AI commented Nov 2, 2025

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>Consider a “query_pairs” method</issue_title>
<issue_description>Hi,

Really interesting tool that you have there, thank you for making it available!

I wonder if it would be possible to introduce a query_pairs type method in a future version, like the one provided by cKDTree in the scipy.spatial module. Ideally, it would return a numpy array of index pairs.

This could prove particularly useful for quickly accessing colliding pairs of AABBs without having to perform any further manipulation in Python.

Currently, to do this I need to use np.vectorize, np.repeat and np.concatenate to convert the nested lists from batch_query into flat arrays and then apply a boolean mask to remove duplicate and undesired pairs. Unfortunately, this whole process cancels out any performance gains from the parallelized C++ calculations.

Please, let me know!

</issue_description>

Comments on the Issue (you are @copilot in this section)

@atksh Hi, @matsupin

Thank you for your suggestion! I will adding a method query_intersections() that would efficiently enumerate pairs of intersecting AABBs.

Since AABB intersection checks are based on simple bounding box overlap rather than distance calculations, I believe parameters like r (radius) and p (norm) from query_pairs() are unnecessary for this method.

Would this approach meet your needs? Let me know if you have any specific requirements or use cases I should consider!</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@atksh atksh closed this Nov 2, 2025
@atksh atksh deleted the copilot/add-query-pairs-method branch November 2, 2025 15:01
Copilot AI requested a review from atksh November 2, 2025 15:01
atksh pushed a commit that referenced this pull request Nov 3, 2025
Implements a new query_intersections() method that efficiently finds all
pairs of intersecting bounding boxes in the tree, addressing the feature
request in issue #46.

Key features:
- Returns numpy array of shape (n_pairs, 2) with index pairs (i, j) where i < j
- Parallel processing using std::thread for improved performance
- Automatic double-precision refinement when exact coordinates are available
- No duplicate pairs or self-pairs
- Similar to scipy.spatial.cKDTree.query_pairs but for AABBs

This eliminates the need for manual post-processing of batch_query results
using np.vectorize, np.repeat, and np.concatenate, which previously
canceled out the performance gains from C++ parallelization.

Changes:
- cpp/prtree.h: Added query_intersections() method to PRTree class
- cpp/main.cc: Added Python bindings for all dimensions (2D, 3D, 4D)
- tests/test_PRTree.py: Added comprehensive tests including edge cases
- README.md: Updated documentation with usage examples

All tests pass (45 new tests + existing tests).

Resolves #46
atksh added a commit that referenced this pull request Nov 3, 2025
* Add query_intersections() method for efficient AABB pair detection

Implements a new query_intersections() method that efficiently finds all
pairs of intersecting bounding boxes in the tree, addressing the feature
request in issue #46.

Key features:
- Returns numpy array of shape (n_pairs, 2) with index pairs (i, j) where i < j
- Parallel processing using std::thread for improved performance
- Automatic double-precision refinement when exact coordinates are available
- No duplicate pairs or self-pairs
- Similar to scipy.spatial.cKDTree.query_pairs but for AABBs

This eliminates the need for manual post-processing of batch_query results
using np.vectorize, np.repeat, and np.concatenate, which previously
canceled out the performance gains from C++ parallelization.

Changes:
- cpp/prtree.h: Added query_intersections() method to PRTree class
- cpp/main.cc: Added Python bindings for all dimensions (2D, 3D, 4D)
- tests/test_PRTree.py: Added comprehensive tests including edge cases
- README.md: Updated documentation with usage examples

All tests pass (45 new tests + existing tests).

Resolves #46

* Add comprehensive Makefile and development documentation

Adds a complete development workflow automation through Make commands,
making it easier for developers to build, test, and contribute to the project.

Key additions:
- Makefile: Comprehensive build automation with 30+ commands
  - Build: build, build-release, rebuild, debug-build
  - Test: test, test-verbose, test-fast, test-coverage, test-one
  - Packaging: wheel, sdist, release
  - Maintenance: clean, clean-all, format, lint
  - Development: init, dev, install-deps, check

- CONTRIBUTING.md: Complete developer guide
  - Development setup instructions
  - Workflow documentation
  - Coding standards
  - Troubleshooting guide

- Enhanced .gitignore: Additional patterns for build artifacts,
  coverage reports, and temporary files

All content is in English and designed to be generic and reusable
for any feature development, not specific to any particular functionality.

Benefits:
- Streamlined development workflow
- Consistent build and test procedures
- Easy onboarding for new contributors
- CI-compatible check commands

* Add Makefile usage reference guide

* Format C++ code with clang-format

Apply clang-format to all C++ source files for consistent code style.
This is an automatic formatting change with no functional modifications.

Files affected:
- cpp/main.cc: Brace positioning and line wrapping
- cpp/parallel.h: Formatting improvements
- cpp/prtree.h: Consistent formatting throughout
- cpp/small_vector.h: Style consistency updates

These changes were generated by running 'make format'.

* Fix: Guard against zero hardware_concurrency in query_intersections

When std::thread::hardware_concurrency() returns 0 (which is permitted
when the runtime cannot detect the CPU count), n_threads was set to zero.
This caused undefined behavior:
- In single-threaded path: writes to thread_pairs[0] on empty vector
- In parallel path: loops never execute, returning no intersections

Apply the same guard used in find_all() to ensure at least one thread.

This fixes a critical bug that could cause crashes or incorrect results
on systems where hardware_concurrency() returns 0.

Fixes: P1 Badge Guard against zero hardware_concurrency
Reported-by: User feedback

---------

Co-authored-by: Claude <noreply@anthropic.com>
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.

Consider a “query_pairs” method

2 participants