ATLAS Offline Software
Loading...
Searching...
No Matches
TrigVSI::KDTree< T, D > Class Template Reference

KDTree. More...

#include <KDPoint.h>

Collaboration diagram for TrigVSI::KDTree< T, D >:

Classes

struct  Node
 Node class for KDTree. More...

Public Member Functions

 KDTree (std::vector< KDPoint< T, D > > &v_data)
 KDTree ()
void genTree ()
 Command to generate tree.
void lock ()
void unlock ()
KDPoint< T, D > at (size_t n)

Private Member Functions

std::unique_ptr< NodebuildTree (int, int, int)
 recursive function to create tree structure.
void nearestNeighborRec (const KDPoint< T, D > &, const Node *, double &, int &, std::function< double(const KDPoint< T, D > &, const KDPoint< T, D > &)> &)
 recursive function for nearest neighbor searching.

Private Attributes

std::unique_ptr< Nodem_rootNode
 The root node of the tree.
std::vector< KDPoint< T, D > > m_datas
 Container of the points.
std::vector< size_t > m_indices
 A list of indices of points in m_datas.
size_t m_idLength
bool m_locked

Detailed Description

template<typename T, size_t D>
class TrigVSI::KDTree< T, D >

KDTree.

Can be used for fast point searching, which is needed in clustering.

Definition at line 159 of file KDPoint.h.

Constructor & Destructor Documentation

◆ KDTree() [1/2]

template<typename T, size_t D>
TrigVSI::KDTree< T, D >::KDTree ( std::vector< KDPoint< T, D > > & v_data)
inline

Definition at line 174 of file KDPoint.h.

174 : m_datas( v_data ), m_idLength( m_datas.size() ), m_locked( false )
175 {
176 m_indices.clear();
177 for (size_t i = 0; i < m_idLength; i++) { m_indices.emplace_back(i); }
178 };
std::vector< KDPoint< T, D > > m_datas
Container of the points.
Definition KDPoint.h:190
size_t m_idLength
Definition KDPoint.h:192
std::vector< size_t > m_indices
A list of indices of points in m_datas.
Definition KDPoint.h:191

◆ KDTree() [2/2]

template<typename T, size_t D>
TrigVSI::KDTree< T, D >::KDTree ( )
inline

Definition at line 180 of file KDPoint.h.

180: m_idLength(0), m_locked(false){};

Member Function Documentation

◆ at()

template<typename T, size_t D>
KDPoint< T, D > TrigVSI::KDTree< T, D >::at ( size_t n)
inline

Definition at line 186 of file KDPoint.h.

186{ return m_datas.at(n); };

◆ buildTree()

template<typename T, size_t D>
std::unique_ptr< typename KDTree< T, D >::Node > TrigVSI::KDTree< T, D >::buildTree ( int l,
int r,
int depth )
private

recursive function to create tree structure.

Returns
unique ptr to current node
Parameters
[in]lIndex of the left edge of current region.
[in]rIndex of the right edge of current region.
[in]depthDepth of current node. Axe to be separated is selected according to this param.

Definition at line 220 of file KDPoint.h.

221{
222 if ( l >= r ) return std::make_unique<Node>(nullptr);
223
224 const int axis_ = depth % D;
225 const int mid = ( l + r ) >> 1;
226
227 std::nth_element( m_indices.begin()+l, m_indices.begin()+l+mid, m_indices.begin()+r,
228 [this, axis_](size_t lcnt, size_t rcnt) {
229 return m_datas[lcnt].getId(axis_) < m_datas[rcnt].getId(axis_);
230 } );
231
233 node_ptr->axis = axis_;
234
235 node_ptr->leftPtr = buildTree( l, mid, depth+1 );
236 node_ptr->rightPtr = buildTree( mid+1, r, depth+1 );
237
238 return node_ptr;
239}
std::unique_ptr< Node > buildTree(int, int, int)
recursive function to create tree structure.
Definition KDPoint.h:220

◆ genTree()

template<typename T, size_t D>
void TrigVSI::KDTree< T, D >::genTree ( )

Command to generate tree.

Needed to be locked before execute this command.

Definition at line 205 of file KDPoint.h.

206{
207 if (m_locked) return;
209}
std::unique_ptr< Node > m_rootNode
The root node of the tree.
Definition KDPoint.h:189

◆ lock()

template<typename T, size_t D>
void TrigVSI::KDTree< T, D >::lock ( )
inline

Definition at line 183 of file KDPoint.h.

183{ m_locked = true; };

◆ nearestNeighborRec()

template<typename T, size_t D>
void TrigVSI::KDTree< T, D >::nearestNeighborRec ( const KDPoint< T, D > & ,
const Node * ,
double & ,
int & ,
std::function< double(const KDPoint< T, D > &, const KDPoint< T, D > &)> &  )
private

recursive function for nearest neighbor searching.

Parameters
[in]queryquery point for searching.
[in]nodecurrent node to check.
[out]rminimum distance to query point.
[out]idxindex of candidate point.
[in]dist_funcfunction to calculate the distance between current point and the query point.

Definition at line 251 of file KDPoint.h.

253{
254 // end processing when reach leaf
255 if ( node == nullptr ) {
256 return;
257 }
258
259 const KDPoint<T,D>& point = node->dataRef;
260
261 // update minimum distance and candidate point
262 const double dist = dist_func(query, point);
263 if (dist < r) {
264 idx = node->dataIdx;
265 r = dist;
266 }
267
268 const int axis_ = node->axis;
269 const Node* next_node = (query.at(axis_) < point.at(axis_))? node->leftPtr.get() : node->rightPtr.get();
270
271 // recursively call self until reach leaf node
273
274 // Check if hyper-sphere with radius r overlaps neighbor hyper-plane.
276 proj[axis_] = point.at(axis_); // proj is the projected point of query on hyperplane x[axis_]=point[axis_]
277 double diff = dist_func(proj, query);
278 if ( diff < r ) {
279 // if hyper-sphere overlaps, check the other region separated by the hyper-plane.
280 const Node* next_node_opps = (query.at(axis_) < point.at(axis_))? node->rightPtr.get() : node->leftPtr.get();
282 }
283 return;
284}
void nearestNeighborRec(const KDPoint< T, D > &, const Node *, double &, int &, std::function< double(const KDPoint< T, D > &, const KDPoint< T, D > &)> &)
recursive function for nearest neighbor searching.
Definition KDPoint.h:251
KDPoint< T, D > at(size_t n)
Definition KDPoint.h:186
Node class for KDTree.
Definition KDPoint.h:164

◆ unlock()

template<typename T, size_t D>
void TrigVSI::KDTree< T, D >::unlock ( )
inline

Definition at line 184 of file KDPoint.h.

184{ m_locked = false; };

Member Data Documentation

◆ m_datas

template<typename T, size_t D>
std::vector<KDPoint<T,D> > TrigVSI::KDTree< T, D >::m_datas
private

Container of the points.

Definition at line 190 of file KDPoint.h.

◆ m_idLength

template<typename T, size_t D>
size_t TrigVSI::KDTree< T, D >::m_idLength
private

Definition at line 192 of file KDPoint.h.

◆ m_indices

template<typename T, size_t D>
std::vector<size_t> TrigVSI::KDTree< T, D >::m_indices
private

A list of indices of points in m_datas.

Definition at line 191 of file KDPoint.h.

◆ m_locked

template<typename T, size_t D>
bool TrigVSI::KDTree< T, D >::m_locked
private

Definition at line 193 of file KDPoint.h.

◆ m_rootNode

template<typename T, size_t D>
std::unique_ptr<Node> TrigVSI::KDTree< T, D >::m_rootNode
private

The root node of the tree.

Definition at line 189 of file KDPoint.h.


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