PCL
pcl::KDTree< T > Class Template Reference

Bucket PR K-d tree for point data in arbitrary dimensions. More...

#include <KDTree.h>

Public Types

using component = typename point::component
 
using component_vector = GenericVector< component >
 
using point = T
 
using point_list = Array< point >
 

Public Member Functions

 KDTree ()=default
 
 KDTree (const KDTree &)=delete
 
 KDTree (const point_list &points, int bucketCapacity=16)
 
 KDTree (const point_list &points, int dimension, int bucketCapacity)
 
 KDTree (KDTree &&x)
 
 ~KDTree ()
 
void Build (const point_list &points, int bucketCapacity=16)
 
void Build (const point_list &points, int dimension, int bucketCapacity)
 
void Clear ()
 
int Dimension () const
 
bool IsEmpty ()
 
size_type Length () const
 
KDTreeoperator= (const KDTree &)=delete
 
KDTreeoperator= (KDTree &&x)
 
point_list Search (const point &pt, component epsilon) const
 
template<class F >
void Search (const point &pt, component epsilon, F callback, void *data) const
 

Friends

void Swap (KDTree &x1, KDTree &x2)
 

Detailed Description

template<class T>
class pcl::KDTree< T >

An n-dimensional K-d tree is a specialized binary tree for partitioning of a set of points in an n-dimensional space. K-d trees have important applications in computational geometry problems requiring efficient rectangular range searching.

This class implements a bucket point region K-d tree structure (see Reference 2).

The template type argument T represents the type of a point object stored in a KDTree structure. The type T must have the following properties:

  • The standard default and copy constructors are required:

    T::T()
    T::T( const T& )
  • The T::component subtype must be defined. It represents a component of an object of type T. For example, if T is a vector type, T::component must be the type of a vector component.
  • The array subscript operator must be defined as follows:

    component T::operator []( int i ) const

    This operator must return the value of the i-th component of an object being stored in the K-d tree, such that 0 <= i < N, where N > 0 is the dimension of the point space.
Note
We use this implementation of K-d trees in some essential PixInsight tools with success (e.g., StarAlignment), and hopefully it will be also useful for you, but we don't claim it to be complete. This is a practical and relatively simple implementation, where only the construction, destruction and range search operations are available. In particular, this implementation does not include point addition, deletion and iteration operations. Future versions of PCL will include more complete implementations of this fundamental data structure.

References

  • 1. Mark de Berg et al., Computational Geometry: Algorithms and Applications Third Edition, Springer, 2010, Section 5.2.
  • 2. Hanan Samet, Foundations of Multidimensional and Metric Data Structures, Morgan Kaufmann, 2006, Section 1.5.
See also
QuadTree

Definition at line 119 of file KDTree.h.

Member Typedef Documentation

◆ component

template<class T >
using pcl::KDTree< T >::component = typename point::component

Represents a point component.

Definition at line 131 of file KDTree.h.

◆ component_vector

template<class T >
using pcl::KDTree< T >::component_vector = GenericVector<component>

A vector of point components. Used internally for tree build and range search operations.

Definition at line 137 of file KDTree.h.

◆ point

template<class T >
using pcl::KDTree< T >::point = T

Represents an N-dimensional point stored in this K-d tree.

Definition at line 126 of file KDTree.h.

◆ point_list

template<class T >
using pcl::KDTree< T >::point_list = Array<point>

A list of points. Used for tree build and search operations.

Definition at line 142 of file KDTree.h.

Constructor & Destructor Documentation

◆ KDTree() [1/5]

template<class T >
pcl::KDTree< T >::KDTree ( )
default

Constructs an empty K-d tree.

◆ KDTree() [2/5]

template<class T >
pcl::KDTree< T >::KDTree ( const point_list points,
int  bucketCapacity = 16 
)
inline

Constructs a K-d tree and builds it for the specified list of points.

Parameters
pointsA list of points that will be stored in this K-d tree.
bucketCapacityThe maximum number of points in a leaf tree node. Must be >= 1. The default value is 16.

The dimension of the point space is taken as the length of the first point in the list (by calling points[0].Length()), and must be > 0. All points in the points list must be able to provide at least dimension components through a zero-based array subscript operator.

If the specified list of points is empty, this constructor yields an empty K-d tree. If the dimension of the point space is less than one, an Error exception is thrown.

Definition at line 167 of file KDTree.h.

◆ KDTree() [3/5]

template<class T >
pcl::KDTree< T >::KDTree ( const point_list points,
int  dimension,
int  bucketCapacity 
)
inline

Constructs a K-d tree of the specified dimension and builds it for a list of points.

Parameters
pointsA list of points that will be stored in this K-d tree.
dimensionThe dimension of the point space. Must be > 0.
bucketCapacityThe maximum number of points in a leaf tree node. Must be >= 1.

All points in the points list must be able to provide at least dimension components through a zero-based array subscript operator.

If the specified list of points is empty, this constructor yields an empty K-d tree. If the dimension of the point space is less than one, an Error exception is thrown.

Definition at line 191 of file KDTree.h.

◆ KDTree() [4/5]

template<class T >
pcl::KDTree< T >::KDTree ( const KDTree< T > &  )
delete

Copy constructor. Copy construction is disabled because this class uses internal data structures that cannot be copy-constructed. However, KDTree implements move construction and move assignment.

◆ KDTree() [5/5]

template<class T >
pcl::KDTree< T >::KDTree ( KDTree< T > &&  x)
inline

Move constructor.

Definition at line 213 of file KDTree.h.

◆ ~KDTree()

template<class T >
pcl::KDTree< T >::~KDTree ( )
inline

Destroys a K-d tree. All the stored point objects are deleted.

Definition at line 244 of file KDTree.h.

Member Function Documentation

◆ Build() [1/2]

template<class T >
void pcl::KDTree< T >::Build ( const point_list points,
int  bucketCapacity = 16 
)
inline

Builds a new K-d tree for the specified list of points.

Parameters
pointsA list of points that will be stored in this K-d tree.
bucketCapacityThe maximum number of points in a leaf tree node. Must be >= 1. The default value is 16.

The dimension of the point space is taken as the length of the first point in the list (by calling points[0].Length()), and must be > 0. All points in the points list must be able to provide at least dimension components through a zero-based array subscript operator.

If the tree stores point objects before calling this function, they are destroyed and removed before building a new tree.

If the specified list of points is empty, this member function yields an empty K-d tree. If the dimension of the point space is less than one, an Error exception is thrown.

Definition at line 280 of file KDTree.h.

◆ Build() [2/2]

template<class T >
void pcl::KDTree< T >::Build ( const point_list points,
int  dimension,
int  bucketCapacity 
)
inline

Builds a new K-d tree of the specified dimension for a list of points.

Parameters
pointsA list of points that will be stored in this K-d tree.
dimensionThe dimension of the point space. Must be > 0.
bucketCapacityThe maximum number of points in a leaf tree node. Must be >= 1.

All points in the points list must be able to provide at least dimension components through a zero-based array subscript operator.

If the tree stores point objects before calling this function, they are destroyed and removed before building a new tree.

If the specified list of points is empty, this member function yields an empty K-d tree. If the dimension of the point space is less than one, an Error exception is thrown.

Definition at line 315 of file KDTree.h.

◆ Clear()

template<class T >
void pcl::KDTree< T >::Clear ( )
inline

Removes all the stored point objects, yielding an empty K-d tree.

Definition at line 252 of file KDTree.h.

◆ Dimension()

template<class T >
int pcl::KDTree< T >::Dimension ( ) const
inline

Returns the dimension of this K-d tree. This is the number of components in a point stored in the tree.

Definition at line 406 of file KDTree.h.

◆ IsEmpty()

template<class T >
bool pcl::KDTree< T >::IsEmpty ( )
inline

Returns true iff this K-d tree is empty.

Definition at line 422 of file KDTree.h.

◆ Length()

template<class T >
size_type pcl::KDTree< T >::Length ( ) const
inline

Returns the total number of points stored in this K-d tree.

Definition at line 414 of file KDTree.h.

◆ operator=() [1/2]

template<class T >
KDTree& pcl::KDTree< T >::operator= ( const KDTree< T > &  )
delete

Copy assignment operator. Copy assignment is disabled because this class uses internal data structures that cannot be copy-assigned. However, KDTree implements move assignment and move construction.

◆ operator=() [2/2]

template<class T >
KDTree& pcl::KDTree< T >::operator= ( KDTree< T > &&  x)
inline

Move assignment operator. Returns a reference to this object.

Definition at line 226 of file KDTree.h.

◆ Search() [1/2]

template<class T >
point_list pcl::KDTree< T >::Search ( const point pt,
component  epsilon 
) const
inline

Performs a range search in this K-d tree.

Parameters
ptReference to the point being searched for. The coordinates of this point define the center of the hyper-rectangular search range in the N-dimensional point space.
epsilonSearch tolerance, or half-side of the search hyperrectangle.

Returns a (possibly empty) list with all the points found in the tree within the search range. In two dimensions, the search range would be the rectangle defined by the points:

(pt[0] - epsilon, pt[1] - epsilon) and
(pt[0] + epsilon, pt[1] + epsilon)

with an obvious extension to higher dimensions. If epsilon is zero, the search can only return the set of stored points that are identical to the specified search point.

Definition at line 345 of file KDTree.h.

◆ Search() [2/2]

template<class T >
template<class F >
void pcl::KDTree< T >::Search ( const point pt,
component  epsilon,
callback,
void *  data 
) const
inline

Performs a range search in this K-d tree.

Parameters
ptReference to the point being searched for. The coordinates of this point define the center of the hyper-rectangular search range in the N-dimensional point space.
epsilonSearch tolerance, or half-side of the search hyperrectangle.
callbackCallback functional.
dataCallback data.

The callback function prototype should be:

void callback( const point& pt, void* data )

The callback function will be called once for each point found in the tree within the specified search range. In two dimensions, the search range would be the rectangle defined by the points:

(pt[0] - epsilon, pt[1] - epsilon) and
(pt[0] + epsilon, pt[1] + epsilon)

with an obvious extension to higher dimensions. If epsilon is zero, the search can only return the set of stored points that are identical to the specified search point.

Definition at line 390 of file KDTree.h.

Friends And Related Function Documentation

◆ Swap

template<class T >
void Swap ( KDTree< T > &  x1,
KDTree< T > &  x2 
)
friend

Exchanges two KDTree objects x1 and x2.

Definition at line 430 of file KDTree.h.


The documentation for this class was generated from the following file: