Adapted AABB_tree for new intersections

This commit is contained in:
Philipp Möller 2011-11-10 16:00:22 +00:00
parent 450f438518
commit bddbfcc8e4
6 changed files with 197 additions and 88 deletions

View File

@ -52,8 +52,14 @@ public:
typedef typename GeomTraits::Point_3 Point;
typedef typename std::pair<Object,typename Primitive::Id> Object_and_primitive_id;
typedef typename std::pair<Point,typename Primitive::Id> Point_and_primitive_id;
template<typename Query>
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,typename Primitive::Id> 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 <dim>_less_than (dim in {x,y,z})
*/
class Sort_primitives
{
public:
template<typename PrimitiveIterator>
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<typename PrimitiveIterator>
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 ConstPrimitiveIterator>
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 ConstPrimitiveIterator>
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<typename Query>
bool operator()(const Query& q, const Bounding_box& bbox) const
{
return CGAL::do_intersect(q, bbox);
}
template<typename Query>
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<typename Query>
boost::optional<typename AT::Object_and_primitive_id>
operator()(const Query& query, const typename AT::Primitive& primitive) const
{
typedef boost::optional<Object_and_primitive_id> 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 Query>
typename Intersection_and_primitive_id<Query>::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<typename Query>
bool operator()(const Query& q, const Bounding_box& bbox) const
{
return CGAL::do_intersect(q, bbox);
}
template<typename Query>
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<typename Query>
boost::optional<typename AT::Object_and_primitive_id>
operator()(const Query& query, const typename AT::Primitive& primitive) const
{
typedef boost::optional<Object_and_primitive_id> 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 {

View File

@ -145,8 +145,14 @@ namespace CGAL {
// any intersection
template <typename Query>
boost::optional<Primitive_id> any_intersected_primitive(const Query& query) const;
template <typename Query>
boost::optional<Object_and_primitive_id> any_intersection(const Query& query) const;
#if CGAL_INTERSECTION_VERSION < 2
boost::optional<Object_and_primitive_id>
#else
typename AABB_traits::template Intersection_and_primitive_id<Query>::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 <typename Tr>
template <typename Query>
#if CGAL_INTERSECTION_VERSION < 2
boost::optional<typename AABB_tree<Tr>::Object_and_primitive_id>
#else
typename Tr::template Intersection_and_primitive_id<Query>::type
#endif
AABB_tree<Tr>::any_intersection(const Query& query) const
{
using namespace CGAL::internal::AABB_tree;

View File

@ -76,13 +76,25 @@ class First_intersection_traits
typedef typename ::CGAL::AABB_tree<AABBTraits>::size_type size_type;
public:
typedef typename boost::optional<Object_and_primitive_id> Result;
typedef typename
#if CGAL_INTERSECTION_VERSION < 2
boost::optional<Object_and_primitive_id>
#else
AABBTraits::template Intersection_and_primitive_id<Query>::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<Object_and_primitive_id> intersection;
#if CGAL_INTERSECTION_VERSION < 2
boost::optional<Object_and_primitive_id>
#else
typename AABBTraits::template Intersection_and_primitive_id<Query>::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

View File

@ -29,6 +29,7 @@
#include <CGAL/AABB_polyhedron_triangle_primitive.h>
#include <CGAL/AABB_polyhedron_segment_primitive.h>
#include <boost/mem_fn.hpp>
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<Object_and_primitive_id> 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<Ray>::type r = tree.any_intersection(ray);
typename Tree::AABB_traits::template Intersection_and_primitive_id<Line>::type l = tree.any_intersection(line);
typename Tree::AABB_traits::template Intersection_and_primitive_id<Segment>::type s = tree.any_intersection(segment);
#endif
// any_intersected_primitive
boost::optional<typename Primitive::Id> 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<Object_and_primitive_id> 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<typename Tree::AABB_traits::template Intersection_and_primitive_id<Ray>::type> intersections_r;
std::list<typename Tree::AABB_traits::template Intersection_and_primitive_id<Line>::type> intersections_l;
std::list<typename Tree::AABB_traits::template Intersection_and_primitive_id<Segment>::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<Object_and_primitive_id> Intersection_result;
public:
template<typename Query>
@ -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<Query>::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<Object_and_primitive_id> Obj_Id_vector;
typedef
#if CGAL_INTERSECTION_VERSION < 2
Object_and_primitive_id
#else
typename Tree::AABB_traits::template Intersection_and_primitive_id<Query>::type
#endif
Obj_type;
typedef
std::vector<Obj_type>
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<Object_and_primitive_id> Any_intersection;
Any_intersection intersection = tree.any_intersection(query);
#if CGAL_INTERSECTION_VERSION < 2
boost::optional<Object_and_primitive_id>
#else
typename Tree::AABB_traits::template Intersection_and_primitive_id<Query>::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;
}

View File

@ -22,6 +22,8 @@
//
//******************************************************************************
#define CGAL_INTERSECTION_VERSION 1
#include <fstream>
#include <iostream>

View File

@ -22,6 +22,8 @@
//
//******************************************************************************
#define CGAL_INTERSECTION_VERSION 2
#include <fstream>
#include <iostream>