1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#define KDTREE_DEFINE_OSTREAM_OPERATORS
#include <kdtree++/kdtree.hpp>
#include <iostream>
#include <vector>
struct triplet {
typedef int value_type;
inline value_type operator[](size_t const N) const { return d[N]; }
value_type d[3];
};
inline bool operator==(triplet const& A, triplet const& B) {
return A.d[0] == B.d[0] && A.d[1] == B.d[1] && A.d[2] == B.d[2];
}
std::ostream& operator<<(std::ostream& out, triplet const& T)
{
return out << '(' << T.d[0] << ',' << T.d[1] << ',' << T.d[2] << ')';
}
double tac( triplet t, int k ) { return t[k]; }
typedef KDTree::KDTree<3, triplet, std::pointer_to_binary_function<triplet,int,double> > tree_type;
int main()
{
tree_type t(std::ptr_fun(tac));
triplet c0 = { {5, 4, 0} }; t.insert(c0);
triplet c1 = { {4, 2, 1} }; t.insert(c1);
triplet c2 = { {7, 6, 9} }; t.insert(c2);
triplet c3 = { {2, 2, 1} }; t.insert(c3);
triplet c4 = { {8, 0, 5} }; t.insert(c4);
triplet c5 = { {5, 7, 0} }; t.insert(c5);
triplet c6 = { {3, 3, 8} }; t.insert(c6);
triplet c7 = { {9, 7, 3} }; t.insert(c7);
triplet c8 = { {2, 2, 6} }; t.insert(c8);
triplet c9 = { {2, 0, 6} }; t.insert(c9);
std::cout << t << std::endl;
t.erase(c0);
t.erase(c1);
t.erase(c3);
t.erase(c5);
t.optimise();
std::cout << std::endl << t << std::endl;
int i=0;
for (tree_type::const_iterator iter=t.begin(); iter!=t.end(); ++iter, ++i);
std::cout << "iterator walked through " << i << " nodes in total" << std::endl;
if (i!=6)
{
std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl;
return 1;
}
i=0;
for (tree_type::const_reverse_iterator iter=t.rbegin(); iter!=t.rend(); ++iter, ++i);
std::cout << "reverse_iterator walked through " << i << " nodes in total" << std::endl;
if (i!=6)
{
std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl;
return 1;
}
triplet s = { {5, 4, 3} };
std::vector<triplet> v;
unsigned int const RANGE = 3;
size_t count = t.count_within_range(s, RANGE);
std::cout << "counted " << count
<< " nodes within range " << RANGE << " of " << s << ".\n";
t.find_within_range(s, RANGE, std::back_inserter(v));
std::cout << "found " << v.size() << " nodes within range " << RANGE
<< " of " << s << ":\n";
std::vector<triplet>::const_iterator ci = v.begin();
for (; ci != v.end(); ++ci)
std::cout << *ci << " ";
std::cout << "\n" << std::endl;
std::cout << "Nearest to " << s << ": " <<
*t.find_nearest(s,std::numeric_limits<double>::max()).first << std::endl;
triplet s2 = { {10, 10, 2} };
std::cout << "Nearest to " << s2 << ": " <<
*t.find_nearest(s2,std::numeric_limits<double>::max()).first << std::endl;
std::cout << std::endl;
std::cout << t << std::endl;
return 0;
}
/* COPYRIGHT --
*
* This file is part of libkdtree++, a C++ template KD-Tree sorting container.
* libkdtree++ is (c) 2004-2007 Martin F. Krafft <libkdtree@pobox.madduck.net>
* and Sylvain Bougerel <sylvain.bougerel.devel@gmail.com> distributed under the
* terms of the Artistic License 2.0. See the ./COPYING file in the source tree
* root for more information.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
* OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
|