Broad phase collision API#286
Conversation
Signed-off-by: twidmer <twidmer@nvidia.com>
Signed-off-by: twidmer <twidmer@nvidia.com>
Signed-off-by: twidmer <twidmer@nvidia.com>
|
Thanks Tobi, looks good! Just added a few cosmetic comments. |
Signed-off-by: twidmer <twidmer@nvidia.com>
9bb488a to
813b330
Compare
|
I think you can remove the examples you are adding in this PR since MuJoCoSolver doesn't use this broadphase anyway and we have a test for example_rigid.py in |
3b08fc5 to
d39b1ab
Compare
Signed-off-by: twidmer <twidmer@nvidia.com>
d39b1ab to
295761c
Compare
Signed-off-by: twidmer <twidmer@nvidia.com>
📝 WalkthroughWalkthroughThe changes introduce a modular, GPU-accelerated broad phase collision detection system with three algorithms: all-pairs, explicit-pairs, and sweep-and-prune (SAP). Each algorithm is implemented in its own module with Warp device functions and kernels. The package's Changes
Sequence Diagram(s)All-Pairs Broad Phase (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
newton/geometry/broad_phase_sap.py (1)
363-366: Consider implementing adaptive sweep direction selection.The fixed sweep direction works but could be suboptimal for certain object distributions. The TODO comment correctly identifies this optimization opportunity.
For better performance across different scenarios, consider:
- Using principal component analysis (PCA) on the bounding box centers to find the direction of maximum variance
- Caching and periodically updating the sweep direction based on object movement patterns
- Allowing users to specify a custom sweep direction for domain-specific optimizations
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
newton/geometry/__init__.py(2 hunks)newton/geometry/broad_phase_common.py(1 hunks)newton/geometry/broad_phase_nxn.py(1 hunks)newton/geometry/broad_phase_sap.py(1 hunks)newton/tests/test_broad_phase.py(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
newton/tests/test_broad_phase.py (3)
newton/geometry/broad_phase_nxn.py (4)
BroadPhaseAllPairs(118-188)BroadPhaseExplicit(191-251)launch(137-188)launch(205-251)newton/geometry/broad_phase_sap.py (2)
BroadPhaseSAP(288-464)launch(331-464)newton/geometry/broad_phase_common.py (1)
test_group_pair(70-87)
newton/geometry/broad_phase_nxn.py (2)
newton/geometry/broad_phase_common.py (3)
check_aabb_overlap(22-38)test_group_pair(70-87)write_pair(54-65)newton/geometry/broad_phase_sap.py (1)
launch(331-464)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
- GitHub Check: run-asv-benchmarks
🔇 Additional comments (5)
newton/geometry/__init__.py (1)
18-19: LGTM! Clean integration of the new broad phase classes.The imports and public API exposure follow the existing patterns and use the consistent "BroadPhase" prefix as requested in previous reviews.
Also applies to: 98-100
newton/geometry/broad_phase_common.py (1)
22-87: Well-structured utility functions for broad phase operations.The common functions are correctly implemented with proper thread safety (atomic operations) and clear collision group semantics. The AABB overlap check correctly handles cutoff distances, and the binary search provides an efficient utility for the SAP implementation.
newton/geometry/broad_phase_nxn.py (2)
55-75: Efficient lower triangular indexing implementation.The mathematical approach to convert linear indices to triangular matrix coordinates is well-implemented with proper bounds checking. This avoids duplicate pair checks efficiently.
118-189: Well-documented BroadPhaseAllPairs implementation.The class provides a clean API with comprehensive documentation explaining the collision detection logic, group filtering, and output format. The launch method correctly calculates the number of pairs and configures the kernel appropriately.
newton/geometry/broad_phase_sap.py (1)
22-44: Clever use of native code for sortable key generation.The native C++ snippet efficiently converts floats to sortable integers while preserving order, which is crucial for the radix sort performance. The sort key construction properly combines collision groups with projected values.
Signed-off-by: twidmer <twidmer@nvidia.com>
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
newton/tests/test_broad_phase.py (3)
153-178: Consider refactoring duplicated verbose debugging code.The verbose debugging section is duplicated across all three test methods with identical logic. This increases maintenance burden and violates the DRY principle.
Consider extracting the verbose debugging logic into a helper method:
+ def _print_verbose_debug(self, pairs_wp, num_candidate_pair, geom_bounding_box_lower, + geom_bounding_box_upper, np_geom_cutoff, np_collision_group=None): + """Helper method to print verbose debugging information for collision pairs.""" + print("Warp contact pairs:") + for i in range(num_candidate_pair): + pair = pairs_wp[i] + body_a, body_b = pair[0], pair[1] + if np_collision_group is not None: + group_a = np_collision_group[body_a] + group_b = np_collision_group[body_b] + print(f" Pair {i}: bodies ({body_a}, {body_b}) with collision groups ({group_a}, {group_b})") + else: + print(f" Pair {i}: bodies ({body_a}, {body_b})") + + print("Checking if bounding boxes actually overlap:") + for i in range(num_candidate_pair): + pair = pairs_wp[i] + body_a, body_b = pair[0], pair[1] + + # Get bounding boxes and cutoffs for both bodies + box_a_lower = geom_bounding_box_lower[body_a] + box_a_upper = geom_bounding_box_upper[body_a] + box_b_lower = geom_bounding_box_lower[body_b] + box_b_upper = geom_bounding_box_upper[body_b] + cutoff_a = np_geom_cutoff[body_a] + cutoff_b = np_geom_cutoff[body_b] + + overlap = check_aabb_overlap_host( + wp.vec3(box_a_lower[0], box_a_lower[1], box_a_lower[2]), + wp.vec3(box_a_upper[0], box_a_upper[1], box_a_upper[2]), + cutoff_a, + wp.vec3(box_b_lower[0], box_b_lower[1], box_b_lower[2]), + wp.vec3(box_b_upper[0], box_b_upper[1], box_b_upper[2]), + cutoff_b, + ) + print(f" Pair {i}: bodies ({body_a}, {body_b}) - overlap: {overlap}")Then replace the verbose sections in each test with:
if verbose: - print("Warp contact pairs:") - for i in range(num_candidate_pair): - # ... existing verbose code ... + self._print_verbose_debug(pairs_wp, num_candidate_pair, geom_bounding_box_lower, + geom_bounding_box_upper, np_geom_cutoff, np_collision_group)
435-436: Remove commented debug code.The commented debug print statements should be removed to keep the code clean.
- # print("pairs_np:", pairs_np) - # print("pairs_wp[:num_candidate_pair]:", pairs_wp[:num_candidate_pair])
79-80: Consider using constants for magic numbers.The repeated magic numbers (30 for ngeom, 5 for num_groups) could be extracted to class constants for better maintainability.
class TestBroadPhase(unittest.TestCase): + NGEOM = 30 + NUM_GROUPS = 5 + def test_nxn_broadphase(self): verbose = False # Create random bounding boxes in min-max format - ngeom = 30 + ngeom = self.NGEOM # ... later in the method ... - num_groups = 5 # The zero group does not need to be counted + num_groups = self.NUM_GROUPS # The zero group does not need to be countedAlso applies to: 196-197, 324-325
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
newton/tests/test_broad_phase.py(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: run-asv-benchmarks
- GitHub Check: run-newton-tests / newton-unittests (windows-latest)
- GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
🔇 Additional comments (3)
newton/tests/test_broad_phase.py (3)
26-42: LGTM! Clean AABB overlap implementation.The function correctly implements axis-aligned bounding box overlap detection with cutoff distance support. The logic properly handles the combined cutoff by taking the maximum of both cutoffs and applying it to expand the overlap check.
45-71: LGTM! Correct numpy reference implementation.The brute-force algorithm correctly implements collision group filtering and AABB overlap detection. The lower triangular iteration pattern (i < j) efficiently avoids duplicate pair checks and self-collisions.
406-431: Confirmed: Indentation issue has been resolved.The code block is now properly indented inside the for loop. The variables
body_aandbody_bare correctly defined within the loop scope before being used in the subsequent lines.
Signed-off-by: twidmer <twidmer@nvidia.com> Co-authored-by: Eric Heiden <eheiden@nvidia.com>
Signed-off-by: twidmer <twidmer@nvidia.com> Co-authored-by: Eric Heiden <eheiden@nvidia.com>
Description
This PR adds three broad phase collision detection strategies:
NxN Broad Phase: The brute force approach that checks all possible pairs for AABB overlaps. It's optimized with:
Explicit Pairs Broad Phase:
SAP (Sweep and Prune):
Added unit tests with random boxes, collision groups, and cutoffs. Everything is validated against a numpy reference implementation. Debug output is there if you need it.
Before your PR is "Ready for review"
pre-commit run -aSummary by CodeRabbit
New Features
Tests