00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ARCHON_UTILITIES_KDTREE_H
00021 #define ARCHON_UTILITIES_KDTREE_H
00022
00023 #include <vector>
00024 #include <algorithm>
00025 #include <queue>
00026
00027 #include <archon/math/geometry.H>
00028
00029 namespace Archon
00030 {
00031 namespace Utilities
00032 {
00033 struct KDTree
00034 {
00035 struct Element
00036 {
00037 virtual Math::Vector3 getPosition() const = 0;
00038
00039 protected:
00040 virtual ~Element() {}
00041 };
00042
00043 void clear() { elements.clear(); }
00044 void insert(Element *element) { elements.push_back(element); }
00045 void balanceTree() { buildSubKDTree(0, elements.size()-1, 0); }
00046
00047 void findNearest(const Math::Vector3 &position,
00048 unsigned count, double max_radius, double &radius,
00049 std::vector<const Element *> &_elements);
00050
00051 private:
00052 class QElement
00053 {
00054 public:
00055 const Element *element;
00056 double dist;
00057 };
00058
00059 double radius;
00060
00061 void buildSubKDTree(int start, int end, int axis);
00062 void findSubNearest(const Math::Vector3 &position, unsigned count, int start, int end, int axis);
00063 void findSubNearestNew(const Math::Vector3 &position, unsigned count, int start, int end, int axis);
00064
00065 struct sort_func: public std::binary_function<Element, Element, bool>
00066 {
00067 int sort_axis;
00068 sort_func(int axis) : sort_axis(axis) {}
00069 bool operator()(const Element *a, const Element *b)
00070 {
00071 return a->getPosition()[sort_axis] < b->getPosition()[sort_axis];
00072 }
00073 };
00074
00075 struct priority_func: public std::binary_function<QElement, QElement, bool>
00076 {
00077 bool operator()(QElement *a, QElement *b) { return a->dist < b->dist; }
00078 };
00079
00080 std::priority_queue<QElement *, std::vector<QElement *>, priority_func> nearest_queue;
00081
00082 std::vector<const Element *> elements;
00083 };
00084 }
00085 }
00086
00087 #endif // ARCHON_UTILITIES_KDTREE_H