68template<
class T,
class T1>
97template<
class T,
class T1>
161template<
int d,
class T,
class T1>
195template<
int d,
class T,
class T1>
213 for (
int i = 0; i < n_nodes; ++i) {
234template<
int d,
class T,
class T1>
246int prev_ = -1, next_ = 0;
255if (n_nodes == 0) {
return(index); };
262while ((next_ >= 0) && (!check)) {
263 check = ((*P_) == (*(nodes[next_].object_)));
266 if ((*P_)[dim] <= (*(nodes[next_].object_))[dim]) {
267 next_ = nodes[next_].lchild_;
270 next_ = nodes[next_].rchild_;
294template<
int d,
class T,
class T1>
307int prev_ = -1, next_ = 0;
316if (n_nodes == 0) {
return(index); };
323while ((next_ >= 0) && (!check)) {
324 check = ((*P_) == (*(nodes[next_].object_)));
327 if ((*P_)[dim] <= (*(nodes[next_].object_))[dim]) {
328 next_ = nodes[next_].lchild_;
331 next_ = nodes[next_].rchild_;
337 label = nodes[prev_].label;
359template<
int d,
class T,
class T1 >
374int index_l = -1, index_r = -1;
384if (n_nodes == 0) {
return(-1); };
392if (distance<T2>(*(nodes[prev_].object_), *P_) <= h) {
return(prev_); }
396if (((*(nodes[prev_].object_))[dim] >= (*P_)[dim] - h)
397 && (nodes[prev_].lchild_ >= 0)) {
399 next_ = nodes[prev_].lchild_;
400 index_l = hNeighbor(P_, h, debug, next_, lev+1);
402if (((*(nodes[prev_].object_))[dim] <= (*P_)[dim] + h)
403 && (nodes[prev_].rchild_ >= 0)) {
405 next_ = nodes[prev_].rchild_;
406 index_r = hNeighbor(P_, h, debug, next_, lev+1);
410return(std::max(index_l, index_r)); };
430template<
int d,
class T,
class T1 >
446int index_l = -1, index_r = -1;
479return(std::max(index_l, index_r)); };
487template<
int d,
class T,
class T1>
498int prev_ = -1, next_ = 0;
508 nodes[0].object_ = P_;
521 if ((*P_)[dim] <= (*(nodes[next_].object_))[dim]) {
522 next_ = nodes[next_].lchild_;
526 next_ = nodes[next_].rchild_;
535if (n_nodes+1 > nodes.size()) {
541 nodes[prev_].lchild_ = n_nodes;
544 nodes[prev_].rchild_ = n_nodes;
548nodes[n_nodes].object_ = P_;
561template<
int d,
class T,
class T1>
573int prev_ = -1, next_ = 0;
583 nodes[0].object_ = P_;
584 nodes[0].label = label;
597 if ((*P_)[dim] <= (*(nodes[next_].object_))[dim]) {
598 next_ = nodes[next_].lchild_;
602 next_ = nodes[next_].rchild_;
611if (n_nodes+1 > (
long) nodes.size()) {
617 nodes[prev_].lchild_ = n_nodes;
620 nodes[prev_].rchild_ = n_nodes;
624nodes[n_nodes].object_ = P_;
625nodes[n_nodes].label = label;
636template<
int d,
class T,
class T1>
654nodes.resize(nodes.size() + MAXSTK);
663template<
int d,
class T,
class T1>
664void KdTree<d, T, T1>::decreaseStack(
681nodes.resize(
max(MAXSTK, nodes.size() - MAXSTK));
693template<
int d,
class T,
class T1 >
695T2 KdTree<d, T, T1>::distance(
701typedef typename std::remove_const<typename std::remove_reference<decltype(std::declval<T>()[0])>::type>::type CoordType;
704std::array<CoordType, d> delta;
705for (
int i = 0; i < d; ++i) {
706 delta[i] = P1[i] - P2[i];
731template<
int d,
class T,
class T1 >
737 std::vector<T1> *EX_,
756if (n_nodes == 0) {
return; };
763if (distance<T2>(*(nodes[prev_].object_), *P_) <= h) {
766 if (std::find(EX_->begin(), EX_->end(), nodes[prev_].label) == EX_->end() ) {
767 L_->push_back(nodes[prev_].label);
771 L_->push_back(nodes[prev_].label);
779if (((*(nodes[prev_].object_))[dim] >= (*P_)[dim] - h)
780 && (nodes[prev_].lchild_ >= 0)) {
781 next_ = nodes[prev_].lchild_;
782 hNeighbors(P_, h, L_, EX_, next_, lev+1);
784if (((*(nodes[prev_].object_))[dim] <= (*P_)[dim] + h)
785 && (nodes[prev_].rchild_ >= 0)) {
786 next_ = nodes[prev_].rchild_;
787 hNeighbors(P_, h, L_, EX_, next_, lev+1);
class for kd-tree data structure.
KdTree(int stack_size=10)
int hNeighbor(const T *, T2, bool, int n=0, int l=0)
void hNeighbors(const T *, T2, std::vector< T1 > *, std::vector< T1 > *, int n=0, int l=0)
double norm2(const std::array< T, d > &x)
std::array< T, d > max(const std::array< T, d > &x, const std::array< T, d > &y)