Class

CGAL::AABB_tree<AT>

Definition

Class AABB_tree<AT> is a static data structure for efficient intersection and distance computations in 3D. It builds a hierarchy of axis-aligned bounding boxes (an AABB tree) from a set of 3D geometric objects, and can receive intersection and distance queries, provided that the corresponding predicates are implemented in the traits class AT. The template parameter AT stands for a traits class which must be a model of the concept AABBTraits.

#include <CGAL/AABB_tree.h>

Types

AABB_tree<AT>::size_type;
Unsigned integral size type.

typedef AT::FT FT; Number type returned by the distance queries.
typedef AT::Point_3 Point; Type of 3D point.
typedef AT::Primitive Primitive; Type of input primitive.
typedef AT::Bounding_box Bounding_box; Type of bounding box.
typedef std::pair<Point, Primitive::Id>
Point_and_primitive_id;
typedef std::pair<CGAL::Object, Primitive::Id>
Object_and_primitive_id;

Creation

AABB_tree<AT> tree;
Constructs an empty tree.


template < class InputIterator>
AABB_tree<AT> tree ( InputIterator begin, InputIterator beyond);
Builds the AABB tree data structure. Type InputIterator can be any const iterator such that Primitive has a constructor taking a InputIterator as argument. The tree stays empty if the memory allocation is not successful.

Operations

template < class InputIterator>
void tree.rebuild ( InputIterator begin, InputIterator beyond)
Clears the current tree and rebuilds it from scratch. See constructor above for the parameters.
void tree.clear () Clears the AABB tree.
template <class InputIterator>
void tree.insert ( InputIterator begin, InputIterator beyond)
Add a sequence of primitives to the set of primitive of the AABB tree. Type InputIterator can be any const iterator such that Primitive has a constructor taking an InputIterator as argument.
void tree.insert ( const Primitive p) Add a primitive to the set of primitives of the AABB tree.

void tree.build () After one or more calls to insert , the internal data structure of AABB_tree<AT> must be reconstructed. This procedure has a complexity of O(n log(n)), where n is the number of primitives of the tree. This procedure is called implicitly at the first call to a query member function. You can call build() explicitly to ensure that the next call to query functions will not trigger the reconstruction of the data structure.

Bounding_box tree.bbox () const Returns the axis-aligned bounding box of the whole tree.
size_type tree.size () const Returns the number of primitives in the tree.
bool tree.empty () const Returns true, iff tree contains no primitive.

Intersection Tests

template <class Query>
bool tree.do_intersect ( Query query) const
Returns true, iff the query intersects at least one of the input primitives. Type Query must be a type for which do_intersect predicates are defined in the AT class.
template <class Query>
size_type tree.number_of_intersected_primitives ( Query query) const
Returns the number of primitives intersected by the query. Type Query must be a type for which do_intersect predicates are defined in the AT class.
template <class Query, class OutputIterator>
OutputIterator tree.all_intersected_primitives ( Query query, OutputIterator out) const
Outputs to the iterator the list of all intersected primitives ids. This function does not compute the intersection points and is hence faster than the function all_intersections function below. Type Query must be a type for which do_intersect predicates are defined in the AT class.
template <class Query>
boost::optional<Primitive::Id> tree.any_intersected_primitive ( Query query) const
Returns the first encountered intersected primitive id, iff the query intersects at least one of the input primitives. No particular order is guaranteed over the tree traversal, such that, e.g, the primitive returned is not necessarily the closest from the source point of a ray query. Type Query must be a type for which do_intersect predicates are defined in the AT class.

Intersections

template <class Query, class OutputIterator>
OutputIterator tree.all_intersections ( Query query, OutputIterator out) const
Outputs to the iterator the list of all intersections between the query and input data, as objects of type Object_and_primitive_id. Type Query must be a type for which do_intersect predicates and intersections are defined in the AT class.
template <class Query>
boost::optional<Object_and_primitive_id>
tree.any_intersection ( Query query) const
Returns the first encountered intersection, iff the query intersects at least one of the input primitives. No particular order is guaranteed over the tree traversal, such that, e.g, the primitive returned is not necessarily the closest from the source point of a ray query. Type Query must be a type for which do_intersect predicates and intersections are defined in the AT class.

Distance Queries

FT tree.squared_distance ( Point query) const
Returns the minimum squared distance between the query point and all input primitives. Method accelerate_distance_queries should be called before the first distance query, so that an internal secondary search structure is build, for improving performance.
Point tree.closest_point ( Point query) const
Returns the point in the union of all input primitives which is closest to the query. In case there are several closest points, one arbitrarily chosen closest point is returned. Method accelerate_distance_queries should be called before the first distance query, so that an internal secondary search structure is build, for improving performance.
Point_and_primitive_id tree.closest_point_and_primitive ( Point query) const
Returns a Point_and_primitive_id which realizes the smallest distance between the query point and all input primitives. Method accelerate_distance_queries should be called before the first distance query, so that an internal secondary search structure is build, for improving performance.

Accelerating the Distance Queries

bool tree.accelerate_distance_queries () const
Constructs an internal data structure for accelerating distance queries. This method should be called once, before the first distance query. Returns true, iff the memory allocation is successful.

In the following paragraphs, we discuss details of the implementation of the distance queries. We explain the internal use of hints, how the user can pass his own hints to the tree, and how the user can influence the construction of the secondary data structure used for accelerating distance queries.

Internally, the distance queries algorithms are initialized with some hint, which has the same type as the return type of the query, and this value is refined along a traversal of the tree, until it is optimal, that is to say until it realizes the shortest distance to the primitives. In particular, the exact specification of these internal algorithms is that they minimize the distance to the object composed of the union of the primitives and the hint.

It follows that

This second observation is reasonable, in the sense that providing a hint to the algorithm means claiming that this hint belongs to the union of the primitives. These considerations about the hints being exactly on the primitives or not are important: in the case where the set of primitives is a triangle soup, and if some of the primitives are large, one may want to provide a much better hint than a vertex of the triangle soup could be. It could be, for example, the barycenter of one of the triangles. But, except with the use of an exact constructions kernel, one cannot easily construct points other than the vertices, that lie exactly on a triangle soup. Hence, providing a good hint sometimes means not being able to provide it exactly on the primitives. In rare occasions, this hint can be returned as the closest point.

In order to accelerate distance queries significantly, the AABB tree builds an internal KD-tree containing a set of potential hints, when the method accelerate_distance_queries is called. This KD-tree provides very good hints that allow the algorithms to run much faster than with a default hint (such as the reference_point of the first primitive). The set of potential hints is a sampling of the union of the primitives, which is obtained, by default, by calling the method reference_point of each of the primitives. However, such a sampling with one point per primitive may not be the most relevant one: if some primitives are very large, it helps inserting more than one sample on them. Conversely, a sparser sampling with less than one point per input primitive is relevant in some cases.

For this reason, the user can provide his own set of sample points:

template <class InputIterator>
bool tree.accelerate_distance_queries ( InputIterator begin, InputIterator beyond) const
Constructs an internal KD-tree containing the specified point set, to be used as the set of potential hints for accelerating the distance queries. InputIterator is an iterator with value type Point_and_primitive_id.

Note that, in some cases, the user is not able to provide ids of the primitives on which the points lie. In these cases, providing a default value for the ids of the hints is possible. Still, the user should be aware that if she uses the closest_point_and_primitive method, there is a (tiny) chance that a hint is returned, along with this default value as corresponding primitive id. Hence, the validity of the returned primitive id should be checked in these cases.

As an alternative to using the KD-tree, the user can also provide the hints directly, by using the following methods:

FT tree.squared_distance ( Point query, Point hint) const
Returns the minimum squared distance between the query point and all input primitives. The internal KD-tree is not used.

Point tree.closest_point ( Point query, Point hint) const
Returns the point in the union of all input primitives which is closest to the query. In case there are several closest points, one arbitrarily chosen closest point is returned. The internal KD-tree is not used.

Point_and_primitive_id tree.closest_point_and_primitive ( Point query, Point_and_primitive_id hint) const
Returns a Point_and_primitive_id which realizes the smallest distance between the query point and all input primitives. The internal KD-tree is not used.

See Also

AABBTraits,
AABBPrimitive.