diff --git a/AABB_tree/include/CGAL/AABB_traits.h b/AABB_tree/include/CGAL/AABB_traits.h index 9f5fe2fee0d..df316ba0547 100644 --- a/AABB_tree/include/CGAL/AABB_traits.h +++ b/AABB_tree/include/CGAL/AABB_traits.h @@ -52,8 +52,14 @@ public: typedef typename GeomTraits::Point_3 Point; typedef typename std::pair Object_and_primitive_id; - typedef typename std::pair Point_and_primitive_id; + template + struct Intersection_and_primitive_id { + typedef std::pair< typename IT< Query, typename Primitive::Datum >::result_type, typename Primitive::Id > type; + }; + + typedef typename std::pair Point_and_primitive_id; + // types for search tree typedef typename GeomTraits::FT FT; typedef typename GeomTraits::Point_3 Point_3; @@ -87,33 +93,33 @@ public: * axis, using the comparison function _less_than (dim in {x,y,z}) */ -class Sort_primitives -{ -public: -template -void operator()(PrimitiveIterator first, - PrimitiveIterator beyond, - const typename AT::Bounding_box& bbox) const + class Sort_primitives { - PrimitiveIterator middle = first + (beyond - first)/2; - switch(longest_axis(bbox)) - { - case AT::CGAL_AXIS_X: // sort along x - std::nth_element(first, middle, beyond, less_x); - break; - case AT::CGAL_AXIS_Y: // sort along y - std::nth_element(first, middle, beyond, less_y); - break; - case AT::CGAL_AXIS_Z: // sort along z - std::nth_element(first, middle, beyond, less_z); - break; - default: - CGAL_error(); - } - } -}; + public: + template + void operator()(PrimitiveIterator first, + PrimitiveIterator beyond, + const typename AT::Bounding_box& bbox) const + { + PrimitiveIterator middle = first + (beyond - first)/2; + switch(longest_axis(bbox)) + { + case AT::CGAL_AXIS_X: // sort along x + std::nth_element(first, middle, beyond, less_x); + break; + case AT::CGAL_AXIS_Y: // sort along y + std::nth_element(first, middle, beyond, less_y); + break; + case AT::CGAL_AXIS_Z: // sort along z + std::nth_element(first, middle, beyond, less_z); + break; + default: + CGAL_error(); + } + } + }; -Sort_primitives sort_primitives_object() {return Sort_primitives();} + Sort_primitives sort_primitives_object() {return Sort_primitives();} /** @@ -124,58 +130,66 @@ Sort_primitives sort_primitives_object() {return Sort_primitives();} */ class Compute_bbox { -public: -template -typename AT::Bounding_box operator()(ConstPrimitiveIterator first, - ConstPrimitiveIterator beyond) const - { - typename AT::Bounding_box bbox = compute_bbox(*first); - for(++first; first != beyond; ++first) - { - bbox = bbox + compute_bbox(*first); + public: + template + typename AT::Bounding_box operator()(ConstPrimitiveIterator first, + ConstPrimitiveIterator beyond) const + { + typename AT::Bounding_box bbox = compute_bbox(*first); + for(++first; first != beyond; ++first) + { + bbox = bbox + compute_bbox(*first); + } + return bbox; + } + }; + + Compute_bbox compute_bbox_object() {return Compute_bbox();} + + + class Do_intersect { + public: + template + bool operator()(const Query& q, const Bounding_box& bbox) const + { + return CGAL::do_intersect(q, bbox); + } + + template + bool operator()(const Query& q, const Primitive& pr) const + { + return GeomTraits().do_intersect_3_object()(q, pr.datum()); + } + }; + + Do_intersect do_intersect_object() {return Do_intersect();} + + class Intersection { + public: + #if CGAL_INTERSECTION_VERSION < 2 + template + boost::optional + operator()(const Query& query, const typename AT::Primitive& primitive) const + { + typedef boost::optional Intersection; + + CGAL::Object object = GeomTraits().intersect_3_object()(primitive.datum(),query); + if ( object.empty() ) + return Intersection(); + else + return Intersection(Object_and_primitive_id(object,primitive.id())); + } + #else + template + typename Intersection_and_primitive_id::type + operator()(const Query& query, const typename AT::Primitive& primitive) const { + return std::make_pair(GeomTraits().intersect_3_object()(primitive.datum(),query), + primitive.id()); } - return bbox; - } -}; - -Compute_bbox compute_bbox_object() {return Compute_bbox();} - - -class Do_intersect { -public: - template - bool operator()(const Query& q, const Bounding_box& bbox) const - { - return CGAL::do_intersect(q, bbox); - } - - template - bool operator()(const Query& q, const Primitive& pr) const - { - return GeomTraits().do_intersect_3_object()(q, pr.datum()); - } -}; - -Do_intersect do_intersect_object() {return Do_intersect();} - -class Intersection { -public: -template -boost::optional -operator()(const Query& query, const typename AT::Primitive& primitive) const -{ - typedef boost::optional Intersection; - - CGAL::Object object = GeomTraits().intersect_3_object()(primitive.datum(),query); - if ( object.empty() ) - return Intersection(); - else - return Intersection(Object_and_primitive_id(object,primitive.id())); -} -}; - -Intersection intersection_object() {return Intersection();} - + #endif + }; + + Intersection intersection_object() {return Intersection();} // This should go down to the GeomTraits, i.e. the kernel class Closest_point { diff --git a/AABB_tree/include/CGAL/AABB_tree.h b/AABB_tree/include/CGAL/AABB_tree.h index 83fa98978ed..ade831c01d5 100644 --- a/AABB_tree/include/CGAL/AABB_tree.h +++ b/AABB_tree/include/CGAL/AABB_tree.h @@ -145,8 +145,14 @@ namespace CGAL { // any intersection template boost::optional any_intersected_primitive(const Query& query) const; + template - boost::optional any_intersection(const Query& query) const; + #if CGAL_INTERSECTION_VERSION < 2 + boost::optional + #else + typename AABB_traits::template Intersection_and_primitive_id::type + #endif + any_intersection(const Query& query) const; // distance queries FT squared_distance(const Point& query) const; @@ -422,9 +428,14 @@ namespace CGAL { return out; } + template template + #if CGAL_INTERSECTION_VERSION < 2 boost::optional::Object_and_primitive_id> + #else + typename Tr::template Intersection_and_primitive_id::type + #endif AABB_tree::any_intersection(const Query& query) const { using namespace CGAL::internal::AABB_tree; diff --git a/AABB_tree/include/CGAL/internal/AABB_tree/AABB_traversal_traits.h b/AABB_tree/include/CGAL/internal/AABB_tree/AABB_traversal_traits.h index 7896eb79812..15d0bb5d856 100644 --- a/AABB_tree/include/CGAL/internal/AABB_tree/AABB_traversal_traits.h +++ b/AABB_tree/include/CGAL/internal/AABB_tree/AABB_traversal_traits.h @@ -76,13 +76,25 @@ class First_intersection_traits typedef typename ::CGAL::AABB_tree::size_type size_type; public: - typedef typename boost::optional Result; + typedef typename + #if CGAL_INTERSECTION_VERSION < 2 + boost::optional + #else + AABBTraits::template Intersection_and_primitive_id::type + #endif + Result; public: First_intersection_traits() : m_result() {} - bool go_further() const { return !m_result; } + bool go_further() const { + #if CGAL_INTERSECTION_VERSION < 2 + return !m_result; + #else + return !m_result.first; + #endif + } void intersection(const Query& query, const Primitive& primitive) { @@ -95,7 +107,13 @@ public: } Result result() const { return m_result; } - bool is_intersection_found() const { return m_result; } + bool is_intersection_found() const { + #if CGAL_INTERSECTION_VERSION < 2 + return m_result; + #else + return m_result.first; + #endif + } private: Result m_result; @@ -126,12 +144,24 @@ public: void intersection(const Query& query, const Primitive& primitive) { - boost::optional intersection; + #if CGAL_INTERSECTION_VERSION < 2 + boost::optional + #else + typename AABBTraits::template Intersection_and_primitive_id::type + #endif intersection = AABBTraits().intersection_object()(query, primitive); + + #if CGAL_INTERSECTION_VERSION < 2 if(intersection) { *m_out_it++ = *intersection; } + #else + if(intersection.first) + { + *m_out_it++ = intersection; + } + #endif } bool do_intersect(const Query& query, const Node& node) const diff --git a/AABB_tree/test/AABB_tree/AABB_test_util.h b/AABB_tree/test/AABB_tree/AABB_test_util.h index 938f0a8dca1..fbcae67c538 100644 --- a/AABB_tree/test/AABB_tree/AABB_test_util.h +++ b/AABB_tree/test/AABB_tree/AABB_test_util.h @@ -29,6 +29,7 @@ #include #include +#include double random_in(const double a, const double b) @@ -98,10 +99,16 @@ void test_all_intersection_query_types(Tree& tree) tree.all_intersected_primitives(segment,std::back_inserter(primitives)); // any_intersection + #if CGAL_INTERSECTION_VERSION < 2 boost::optional optional_object_and_primitive; optional_object_and_primitive = tree.any_intersection(ray); optional_object_and_primitive = tree.any_intersection(line); optional_object_and_primitive = tree.any_intersection(segment); + #else + typename Tree::AABB_traits::template Intersection_and_primitive_id::type r = tree.any_intersection(ray); + typename Tree::AABB_traits::template Intersection_and_primitive_id::type l = tree.any_intersection(line); + typename Tree::AABB_traits::template Intersection_and_primitive_id::type s = tree.any_intersection(segment); + #endif // any_intersected_primitive boost::optional optional_primitive; @@ -110,10 +117,19 @@ void test_all_intersection_query_types(Tree& tree) optional_primitive = tree.any_intersected_primitive(segment); // all_intersections + #if CGAL_INTERSECTION_VERSION < 2 std::list intersections; tree.all_intersections(ray,std::back_inserter(intersections)); tree.all_intersections(line,std::back_inserter(intersections)); tree.all_intersections(segment,std::back_inserter(intersections)); + #else + std::list::type> intersections_r; + std::list::type> intersections_l; + std::list::type> intersections_s; + tree.all_intersections(ray,std::back_inserter(intersections_r)); + tree.all_intersections(line,std::back_inserter(intersections_l)); + tree.all_intersections(segment,std::back_inserter(intersections_s)); + #endif } @@ -314,6 +330,7 @@ class Naive_implementations typedef typename Traits::Point_and_primitive_id Point_and_primitive_id; typedef boost::optional Intersection_result; + public: template @@ -368,9 +385,17 @@ public: Polyhedron_primitive_iterator it = Pr_generator().begin(p); for ( ; it != Pr_generator().end(p) ; ++it ) { - Intersection_result intersection = Traits().intersection_object()(query, Pr(it)); + #if CGAL_INTERSECTION_VERSION < 2 + Intersection_result + intersection = Traits().intersection_object()(query, Pr(it)); if ( intersection ) *out++ = *intersection; + #else + typename Traits::template Intersection_and_primitive_id::type + intersection = Traits().intersection_object()(query, Pr(it)); + if ( intersection.first ) + *out++ = intersection; + #endif } return out; @@ -674,8 +699,18 @@ private: Tree& tree, const Naive_implementation& naive) const { - typedef std::vector Obj_Id_vector; + typedef + #if CGAL_INTERSECTION_VERSION < 2 + Object_and_primitive_id + #else + typename Tree::AABB_traits::template Intersection_and_primitive_id::type + #endif + Obj_type; + typedef + std::vector + Obj_Id_vector; + Obj_Id_vector intersections_naive; naive_timer.start(); naive.all_intersections(query, p, std::back_inserter(intersections_naive)); @@ -693,7 +728,8 @@ private: std::transform(intersections_naive.begin(), intersections_naive.end(), std::back_inserter(intersections_naive_id), - primitive_id); + boost::mem_fn(&Obj_type::second) + ); for ( typename Obj_Id_vector::iterator it = intersections_tree.begin() ; it != intersections_tree.end() ; @@ -706,10 +742,15 @@ private: } // Any intersection test (do not count time here) - typedef boost::optional Any_intersection; - Any_intersection intersection = tree.any_intersection(query); + #if CGAL_INTERSECTION_VERSION < 2 + boost::optional + #else + typename Tree::AABB_traits::template Intersection_and_primitive_id::type + #endif + intersection = tree.any_intersection(query); // Check: verify we do get the result by naive method + #if CGAL_INTERSECTION_VERSION < 2 if ( intersection ) { assert( std::find(intersections_naive_id.begin(), @@ -717,12 +758,21 @@ private: intersection->second) != intersections_naive_id.end()); } + #else + if ( intersection.first ) + { + assert( std::find(intersections_naive_id.begin(), + intersections_naive_id.end(), + intersection.second) + != intersections_naive_id.end()); + } + #endif else if ( intersections_naive.size() != 0 ) assert(false); } - static typename Primitive::Id primitive_id(const Object_and_primitive_id& o) + static typename Primitive::Id primitive_id_o(const Object_and_primitive_id& o) { return o.second; } diff --git a/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_distance_triangle_test.cpp b/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_distance_triangle_test.cpp index f1a458ebfea..a04e37bfd6d 100644 --- a/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_distance_triangle_test.cpp +++ b/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_distance_triangle_test.cpp @@ -22,6 +22,8 @@ // //****************************************************************************** +#define CGAL_INTERSECTION_VERSION 1 + #include #include diff --git a/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_triangle_test.cpp b/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_triangle_test.cpp index 12c5181af10..f6b45d77af4 100644 --- a/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_triangle_test.cpp +++ b/AABB_tree/test/AABB_tree/aabb_naive_vs_tree_triangle_test.cpp @@ -22,6 +22,8 @@ // //****************************************************************************** +#define CGAL_INTERSECTION_VERSION 2 + #include #include