dlr::computerVision::KDTree< Dimension, Type > Class Template Reference

This class implements a basic KD-Tree data structure. More...

#include <kdTree.h>

Collaboration diagram for dlr::computerVision::KDTree< Dimension, Type >:
[legend]

List of all members.

Public Member Functions

 KDTree ()
 The default constructor creates an empty tree.
template<class Iter >
 KDTree (Iter beginIter, Iter endIter)
 This constructor creates a tree and populates it with the specified sample points.
virtual ~KDTree ()
 The destructor cleans up any system resources during destruction.
bool find (Type const &point) const
 This member function returns true if the specified Type instance has already been inserted into the tree.
Type const & findNearest (Type const &point, double &distance) const
 This member function returns a const reference to the tree element that is closest (Euclidean distance) to the specified point.

Protected Member Functions

template<class Iter >
 KDTree (Iter beginIter, Iter endIter, size_t vectorSize, size_t level)
template<class Iter >
void construct (Iter beginIter, Iter endIter, size_t vectorSize, size_t level)
void findNearestIterative (Type const &point, Type const *&bestPointPtr, double &bestDistance) const
void findNearestRecursive (Type const &point, Type const *&bestPointPtr, double &bestDistance) const

Protected Attributes

KDComparator< Dimension, Type > m_comparator
Type m_point
KDTreem_leftChild
KDTreem_rightChild


Detailed Description

template<unsigned int Dimension, class Type>
class dlr::computerVision::KDTree< Dimension, Type >

This class implements a basic KD-Tree data structure.

Template argument Dimension specifies how many dimensions the tree will span. Template argument Type specifies what kind of element will be contained in the tree. Logically, Type represents a point in multi-dimensional space. It must support default construction, copying and assignment. It must also fulfill the requirements of the KDComparator class template. Note that if you need to use a Type that doesn't support the KDComparator requirements, you can always specialize KDComparator for your specific Type. This class currently does not support adding points after construction or rebalancing.

Here's an example of how to use the KDTree class template:

   namespce cv = dlr::computerVision;
   namespce num = dlr::numeric;

   std::vector<num::Vector3D> myPoints;
   myPoints.push_back(num::Vector3D(2.1, 3.5, 2.0));
   myPoints.push_back(num::Vector3D(5.0, 3.2, 2.5));
   myPoints.push_back(num::Vector3D(2.4, 1.6, 1.3));
   myPoints.push_back(num::Vector3D(7.7, 4.7, 1.1));
   myPoints.push_back(num::Vector3D(-2.0, 6.3, 5.0));
   myPoints.push_back(num::Vector3D(0.0, 0.0, 0.0));
   myPoints.push_back(num::Vector3D(3.1, 4.7, 5.4));
   
   cv::KDTree<3, num::Vector3D> kdTree(myPoints.begin(), myPoints.end());

   double distance;
   num::Vector3D testPoint(3.5, 6.9, 4.4);
   num::Vector3D nearestPoint = kdTree.findNearest(testPoint, distance);

   std::cout << "The closest point was " << nearestPoint << ", "
             << "which was " << distance << " distance from "
             << testPoint << std::endl;

Definition at line 231 of file kdTree.h.


Constructor & Destructor Documentation

template<unsigned int Dimension, class Type >
dlr::computerVision::KDTree< Dimension, Type >::KDTree (  )  [inline]

The default constructor creates an empty tree.

Definition at line 359 of file kdTree.h.

template<unsigned int Dimension, class Type >
template<class Iter >
dlr::computerVision::KDTree< Dimension, Type >::KDTree ( Iter  beginIter,
Iter  endIter 
) [inline]

This constructor creates a tree and populates it with the specified sample points.

It has complexity O(N*log(N)), where N is the number of elements to be inserted into the tree.

Parameters:
beginIter This argument is an iterator pointing to the beginning of a sequence of Type instances that is to be inserted into the tree.
endIter This argument is an interator pointing one element past the last Type instance in the sequence that is to be inserted into the tree.

Definition at line 370 of file kdTree.h.

template<unsigned int Dimension, class Type >
dlr::computerVision::KDTree< Dimension, Type >::~KDTree (  )  [inline, virtual]

The destructor cleans up any system resources during destruction.

Definition at line 391 of file kdTree.h.


Member Function Documentation

template<unsigned int Dimension, class Type >
bool dlr::computerVision::KDTree< Dimension, Type >::find ( Type const &  point  )  const [inline]

This member function returns true if the specified Type instance has already been inserted into the tree.

It has complexity O(log(N), where N is the number of points contained in the tree.

Parameters:
point This argument is the Type instance to search for. It will be compared to elements in the tree using KDComparator<Dimension, Type>::isEqual().
Returns:
The return value is true if a matching point is found in the tree, false otherwise.

Definition at line 404 of file kdTree.h.

References dlr::computerVision::KDTree< Dimension, Type >::find().

Referenced by dlr::computerVision::KDTree< Dimension, Type >::find().

template<unsigned int Dimension, class Type >
Type const & dlr::computerVision::KDTree< Dimension, Type >::findNearest ( Type const &  point,
double &  distance 
) const [inline]

This member function returns a const reference to the tree element that is closest (Euclidean distance) to the specified point.

It has complexity O(log(N), where N is the number of points contained in the tree.

Parameters:
point This argument is the Type instance to search for. It will be compared to elements in the tree using KDComparator<Dimension, Type>::computeDistance(Type const&, Type const&).
distance This argument is used to return the distance between the point for which we're searching and the closest point in the tree. It will be computed using KDComparator<Dimension, Type>::computeDistance(Type const&, Type const&).
Returns:
The return value is a const reference to the closest point in the tree.

Definition at line 427 of file kdTree.h.


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

Generated on Wed Nov 25 12:15:09 2009 for dlrComputerVision Utility Library by  doxygen 1.5.8