diff --git a/include/geode/geometry/aabb.hpp b/include/geode/geometry/aabb.hpp index 6f3232ccf..a242c2132 100644 --- a/include/geode/geometry/aabb.hpp +++ b/include/geode/geometry/aabb.hpp @@ -70,6 +70,17 @@ namespace geode [[nodiscard]] const BoundingBox< dimension >& bounding_box() const; + /*! + * @brief Whether or not the queries run on this tree are parallelized. + */ + [[nodiscard]] bool parallel() const; + + /*! + * @brief Sets whether or not the queries run on this tree are + * parallelized. + */ + void set_parallel( bool parallel ); + /*! * @brief Gets all the boxes containing a point * @param[in] query the point to test diff --git a/include/geode/geometry/detail/aabb_impl.hpp b/include/geode/geometry/detail/aabb_impl.hpp index e7b310869..aa9781300 100644 --- a/include/geode/geometry/detail/aabb_impl.hpp +++ b/include/geode/geometry/detail/aabb_impl.hpp @@ -111,6 +111,21 @@ namespace geode return mapping_morton_.size(); } + [[nodiscard]] bool parallel() const + { + return parallel_; + } + + void set_parallel( bool parallel ) + { + parallel_ = parallel; + } + + [[nodiscard]] index_t initial_depth() const + { + return parallel_ ? 0 : async_depth_ + 1; + } + [[nodiscard]] static bool is_leaf( index_t element_begin, index_t element_end ) { @@ -547,6 +562,7 @@ namespace geode std::vector< index_t > mapping_morton_; index_t depth_{ 1 }; index_t async_depth_{ 0 }; + bool parallel_{ true }; }; template < index_t dimension > @@ -586,8 +602,8 @@ namespace geode { return; } - impl_->self_intersect_recursive( Impl::ROOT_INDEX, 0, nb_bboxes(), 0, - Impl::ROOT_INDEX, 0, nb_bboxes(), action ); + impl_->self_intersect_recursive( Impl::ROOT_INDEX, 0, nb_bboxes(), + impl_->initial_depth(), Impl::ROOT_INDEX, 0, nb_bboxes(), action ); } template < index_t dimension > @@ -600,8 +616,9 @@ namespace geode { return; } - impl_->other_intersect_recursive( Impl::ROOT_INDEX, 0, nb_bboxes(), 0, - other_tree, Impl::ROOT_INDEX, 0, other_tree.nb_bboxes(), action ); + impl_->other_intersect_recursive( Impl::ROOT_INDEX, 0, nb_bboxes(), + impl_->initial_depth(), other_tree, Impl::ROOT_INDEX, 0, + other_tree.nb_bboxes(), action ); } template < index_t dimension > @@ -635,8 +652,8 @@ namespace geode { return; } - impl_->generic_intersect_recursive( - box_filter, Impl::ROOT_INDEX, 0, nb_bboxes(), 0, action ); + impl_->generic_intersect_recursive( box_filter, Impl::ROOT_INDEX, 0, + nb_bboxes(), impl_->initial_depth(), action ); } template < index_t dimension > diff --git a/src/geode/geometry/aabb.cpp b/src/geode/geometry/aabb.cpp index 1e95ccbc3..bbe6f5b55 100644 --- a/src/geode/geometry/aabb.cpp +++ b/src/geode/geometry/aabb.cpp @@ -74,6 +74,18 @@ namespace geode return impl_->node( Impl::ROOT_INDEX ); } + template < index_t dimension > + bool AABBTree< dimension >::parallel() const + { + return impl_->parallel(); + } + + template < index_t dimension > + void AABBTree< dimension >::set_parallel( bool parallel ) + { + impl_->set_parallel( parallel ); + } + template < index_t dimension > std::vector< index_t > AABBTree< dimension >::containing_boxes( const Point< dimension >& query ) const @@ -84,8 +96,8 @@ namespace geode } std::vector< index_t > result; std::mutex mutex; - impl_->containing_boxes_recursive( - Impl::ROOT_INDEX, 0, nb_bboxes(), 0, query, result, mutex ); + impl_->containing_boxes_recursive( Impl::ROOT_INDEX, 0, nb_bboxes(), + impl_->initial_depth(), query, result, mutex ); return result; } diff --git a/tests/geometry/test-aabb.cpp b/tests/geometry/test-aabb.cpp index 063e59b50..a6831a4a9 100644 --- a/tests/geometry/test-aabb.cpp +++ b/tests/geometry/test-aabb.cpp @@ -209,16 +209,17 @@ class BoxAABBIntersection absl::Span< const geode::BoundingBox< dimension > > bounding_boxes_; }; -template < geode::index_t dimension > +template < geode::index_t dimension, bool parallel > void test_intersections_with_query_box() { - geode::Logger::info( - "TEST", " Box-Box intersection AABB ", dimension, "D" ); + geode::Logger::info( "TEST", " Box-Box intersection AABB ", dimension, "D ", + parallel ? "parallel" : "sequential" ); const geode::index_t nb_boxes{ 10 }; const double box_size{ 0.5 }; const auto box_vector = create_box_vector< dimension >( nb_boxes, box_size ); - const geode::AABBTree< dimension > aabb{ box_vector }; + geode::AABBTree< dimension > aabb{ box_vector }; + aabb.set_parallel( parallel ); BoxAABBIntersection< dimension > eval_intersection{ box_vector }; @@ -296,17 +297,18 @@ class RayAABBIntersection absl::Span< const geode::BoundingBox< dimension > > bounding_boxes_; }; -template < geode::index_t dimension > +template < geode::index_t dimension, bool parallel > void test_intersections_with_ray_trace() { - geode::Logger::info( - "TEST", " Box-Ray intersection AABB ", dimension, "D" ); + geode::Logger::info( "TEST", " Box-Ray intersection AABB ", dimension, "D ", + parallel ? "parallel" : "sequential" ); const geode::index_t nb_boxes{ 10 }; const double box_size{ 0.5 }; const auto box_vector = create_box_vector< dimension >( nb_boxes, box_size ); - const geode::AABBTree< dimension > aabb{ box_vector }; + geode::AABBTree< dimension > aabb{ box_vector }; + aabb.set_parallel( parallel ); RayAABBIntersection< dimension > eval_intersection{ box_vector }; @@ -396,11 +398,11 @@ void test_intersections_with_ray_trace() "[Test] Box-Ray intersection - Wrong set of boxes" ); } -template < geode::index_t dimension > +template < geode::index_t dimension, bool parallel > void test_self_intersections() { - geode::Logger::info( - "TEST", " Box self intersection AABB ", dimension, "D" ); + geode::Logger::info( "TEST", " Box self intersection AABB ", dimension, + "D ", parallel ? "parallel" : "sequential" ); const geode::index_t nb_boxes{ 10 }; // Create a grid of intersecting boxes @@ -410,7 +412,9 @@ void test_self_intersections() box_vector.insert( box_vector.end(), box_vector2.begin(), box_vector2.end() ); - const geode::AABBTree< dimension > aabb{ box_vector }; + geode::AABBTree< dimension > aabb{ box_vector }; + aabb.set_parallel( parallel ); + BoxAABBIntersection< dimension > eval_intersection{ box_vector }; // investigate box inclusions eval_intersection.included_box_.clear(); @@ -445,14 +449,16 @@ class OtherAABBIntersection std::mutex mutex_; }; -template < geode::index_t dimension > +template < geode::index_t dimension, bool parallel > void test_other_intersections() { - geode::Logger::info( - "TEST", " Box other intersection AABB ", dimension, "D" ); + geode::Logger::info( "TEST", " Box other intersection AABB ", dimension, + "D ", parallel ? "parallel" : "sequential" ); - const geode::AABBTree< dimension > aabb{ create_box_vector< dimension >( + geode::AABBTree< dimension > aabb{ create_box_vector< dimension >( 5, 0.2 ) }; + aabb.set_parallel( parallel ); + const geode::AABBTree< dimension > other{ create_box_vector< dimension >( 2, 0.4 ) }; OtherAABBIntersection< dimension > action; @@ -467,21 +473,26 @@ void test_other_intersections() } } -template < geode::index_t dimension > +template < geode::index_t dimension, bool parallel > void do_test() { - test_build_aabb< dimension >(); - test_nearest_neighbor_search< dimension >(); - test_intersections_with_query_box< dimension >(); - test_intersections_with_ray_trace< dimension >(); - test_self_intersections< dimension >(); - test_other_intersections< dimension >(); + if( !parallel ) + { + test_build_aabb< dimension >(); + test_nearest_neighbor_search< dimension >(); + } + test_intersections_with_query_box< dimension, parallel >(); + test_intersections_with_ray_trace< dimension, parallel >(); + test_self_intersections< dimension, parallel >(); + test_other_intersections< dimension, parallel >(); } void test() { - do_test< 2 >(); - do_test< 3 >(); + do_test< 2, false >(); + do_test< 3, false >(); + do_test< 2, true >(); + do_test< 3, true >(); } OPENGEODE_TEST( "aabb" )