kdtree.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
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; // distance to point
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

Generated on Sun Jul 30 22:55:44 2006 for Archon by  doxygen 1.4.4