Ocean
|
This class implements a k-d tree. More...
Data Structures | |
class | Node |
This class defines a node of the k-d tree. More... | |
Public Member Functions | |
KdTree (const unsigned int dimension) | |
Creates a new k-d tree. More... | |
~KdTree ()=default | |
Destructs a k-d tree. More... | |
bool | insert (const T **values, const size_t number) |
Inserts a set of values to this empty tree. More... | |
const T * | nearestNeighbor (const T *value, typename SquareValueTyper< T >::Type &distance) const |
Applies a nearest neighbor search for a given value. More... | |
size_t | radiusSearch (const T *value, const typename SquareValueTyper< T >::Type radius, const T **values, const size_t maxValues) const |
Applies a radius search for neighbors of a given value. More... | |
unsigned int | dimension () const |
Returns the dimension of the tree's values. More... | |
size_t | size () const |
Returns the number of tree nodes. More... | |
Protected Types | |
typedef std::vector< T > | Elements |
Definition of a vector holding single elements. More... | |
typedef std::vector< const T * > | Pointers |
Definition of a vector holding single pointers. More... | |
Protected Member Functions | |
void | insertLeft (Node &parent, const T **values, const size_t number, const unsigned int depth) |
Inserts a set of values to a given parent as left children. More... | |
void | insertRight (Node &parent, const T **values, const size_t number, const unsigned int depth) |
Inserts a set of values to a given parent as right children. More... | |
void | nearestNeighbor (Node &node, const T *value, const T *&nearest, typename SquareValueTyper< T >::Type &distance, const unsigned int index) const |
Applies a nearest neighbor search for a given node and value. More... | |
size_t | radiusSearch (Node &node, const T *value, const typename SquareValueTyper< T >::Type radius, const T **values, const size_t maxValues, const unsigned int index) const |
Applies a radius search for neighbors of a given value. More... | |
SquareValueTyper< T >::Type | determineSquareDistance (const T *first, const T *second) const |
Determines the square distance between two values. More... | |
Static Protected Member Functions | |
static T | median (const T **values, const size_t number, const unsigned int index) |
Returns the median for a given set of values. More... | |
static void | distribute (const T **values, const size_t number, const unsigned int index, const T *&medianValue, Pointers &leftValues, Pointers &rightValues) |
Distributes a given set of values into two subset according to the median. More... | |
Protected Attributes | |
std::unique_ptr< Node > | root_ |
Root node of this tree. More... | |
size_t | size_ = 0 |
Number of nodes. More... | |
const unsigned int | dimension_ = 0u |
Number of dimensions. More... | |
This class implements a k-d tree.
In general, k-d trees should be applied for problems with small dimensions only as the performance benefit decreases with increasing dimension significantly.
That means for the number of nodes (n) and the dimension (k) the following should hold: n >> 2^k.
T | The data type of one element for all dimensions. |
|
protected |
Definition of a vector holding single elements.
|
protected |
Definition of a vector holding single pointers.
|
inlineexplicit |
Creates a new k-d tree.
dimension | Number of dimensions the tree will have, with range [1, infinity) |
|
default |
Destructs a k-d tree.
|
inlineprotected |
Determines the square distance between two values.
first | The first value |
second | The second value |
|
inline |
Returns the dimension of the tree's values.
|
staticprotected |
Distributes a given set of values into two subset according to the median.
values | The values to distribute |
number | The number of values |
index | Dimension index to be used for distribution |
medianValue | Resulting median value |
leftValues | Resulting left values |
rightValues | Resulting right values |
bool Ocean::KdTree< T >::insert | ( | const T ** | values, |
const size_t | number | ||
) |
Inserts a set of values to this empty tree.
Beware: Adding elements to an already existing tree with nodes is not supported.
values | The values to be added |
number | The number of elements to be added, with range [0, infinity) |
|
protected |
Inserts a set of values to a given parent as left children.
parent | The parent node |
values | The values to be added |
number | The number of elements to be added |
depth | Current depth of the new nodes |
|
protected |
Inserts a set of values to a given parent as right children.
parent | The parent node |
values | The values to be added |
number | The number of elements to be added |
depth | Current depth of the new nodes |
|
staticprotected |
Returns the median for a given set of values.
values | The values to return the median for |
number | The number of values |
index | Dimension index |
const T * Ocean::KdTree< T >::nearestNeighbor | ( | const T * | value, |
typename SquareValueTyper< T >::Type & | distance | ||
) | const |
Applies a nearest neighbor search for a given value.
value | The value to be searched |
distance | Resulting minimal distance |
|
protected |
Applies a nearest neighbor search for a given node and value.
node | Reference node |
value | The value to be searched |
nearest | Resulting nearest value |
distance | Resulting minimal distance |
index | The index of the dimension for the recent search, with range [0, dimension()) |
size_t Ocean::KdTree< T >::radiusSearch | ( | const T * | value, |
const typename SquareValueTyper< T >::Type | radius, | ||
const T ** | values, | ||
const size_t | maxValues | ||
) | const |
Applies a radius search for neighbors of a given value.
Beware: Function offers performance boost over brute force search only if radius is so small that relatively few values are returned.
value | The value to be searched |
radius | The neighborhood radius, with range [0, infinity) |
values | Found values within radius distance from a given value |
maxValues | Limit number of returned values |
|
protected |
Applies a radius search for neighbors of a given value.
node | Reference node |
value | The value to be searched |
radius | The neighborhood radius, with range [0, infinity) |
values | Found values within radius distance from a given value |
maxValues | Limit number of returned values |
index | The index of the dimension for the recent search, with range [0, dimension()) |
|
inline |
Returns the number of tree nodes.
|
protected |
Number of dimensions.
|
protected |
Root node of this tree.
|
protected |
Number of nodes.