1 // Copyright (C) 2008 Tim Moore
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #ifndef SIMGEAR_QUADTREEBUILDER_HXX
17 #define SIMGEAR_QUADTREEBUILDER_HXX 1
22 #include <osg/BoundingBox>
23 #include <osg/ref_ptr>
29 #include "VectorArrayAdapter.hxx"
33 typedef std::map<osg::ref_ptr<osg::Node>,int> LodMap;
35 // Create a quad tree based on x, y extents
36 template <typename LeafType, typename ObjectType, typename MakeLeaf,
37 typename AddLeafObject, typename GetObjectLocalCoords>
38 class QuadTreeBuilder {
40 QuadTreeBuilder(const GetObjectLocalCoords& getLocalCoords,
41 const AddLeafObject& addLeafObject, int depth = 2,
42 const MakeLeaf& makeLeaf = MakeLeaf()) :
43 _root(new osg::Group), _depth(depth),
44 _dimension(1 << depth), _leafStorage(_dimension * _dimension),
45 _leaves(_leafStorage, _dimension), _getLocalCoords(getLocalCoords),
46 _addLeafObject(addLeafObject), _makeLeaf(makeLeaf)
50 generate(_leafStorage.begin(), _leafStorage.end(), _makeLeaf);
51 vector<Group*> parentNodes(1);
52 parentNodes[0] = _root.get();
54 for (int i = 0; i < depth - 1; ++i, leafDim *= 2) {
55 VectorArrayAdapter<vector<Group*> > parents(parentNodes, leafDim / 2);
56 vector<Group*> interiorNodes(leafDim * leafDim);
57 VectorArrayAdapter<vector<Group*> > interiors(interiorNodes, leafDim);
58 for (unsigned j = 0; j < leafDim; ++j) {
59 for (unsigned k = 0; k < leafDim; ++k) {
60 interiors(j, k) = new Group;
61 parents(j / 2, k / 2)->addChild(interiors(j, k));
64 parentNodes.swap(interiorNodes);
66 VectorArrayAdapter<vector<Group*> > leafParents(parentNodes,
68 for (int j = 0; j < _dimension; ++j)
69 for (int k =0; k < _dimension; ++k)
70 leafParents(j / 2, k / 2)->addChild(_leaves(j, k));
72 osg::Vec2 getMin() { return _min; }
73 void setMin(const osg::Vec2& min) { _min = min; }
74 osg::Vec2 getMax() { return _max; }
75 void setMax(const osg::Vec2& max) { _max = max; }
77 osg::Group* getRoot() { return _root.get(); }
79 void addNode(ObjectType& obj)
82 const Vec3 center(_getLocalCoords(obj));
84 if (_max.x() != _min.x())
85 x = (int)(_dimension * (center.x() - _min.x())
86 / (_max.x() - _min.x()));
87 x = clampTo(x, 0, (_dimension - 1));
89 if (_max.y() != _min.y())
90 y = (int)(_dimension * (center.y() - _min.y())
91 / (_max.y() - _min.y()));
92 y = clampTo(y, 0, (_dimension -1));
93 _addLeafObject(_leaves(y, x), obj);
98 AddNode(QuadTreeBuilder* qt) : _qt(qt) {}
99 AddNode(const AddNode& rhs) : _qt(rhs._qt) {}
100 void operator() (ObjectType& obj) const { _qt->addNode(obj); }
101 QuadTreeBuilder *_qt;
103 // Make a quadtree of nodes from a map of nodes and LOD values
104 template <typename ForwardIterator>
105 void buildQuadTree(const ForwardIterator& begin,
106 const ForwardIterator& end)
110 for (ForwardIterator iter = begin; iter != end; ++iter) {
111 const Vec3 center = _getLocalCoords(*iter);
112 extents.expandBy(center);
114 _min = Vec2(extents.xMin(), extents.yMin());
115 _max = Vec2(extents.xMax(), extents.yMax());
116 std::for_each(begin, end, AddNode(this));
120 typedef std::vector<LeafType> LeafVector;
121 osg::ref_ptr<osg::Group> _root;
126 LeafVector _leafStorage;
127 VectorArrayAdapter<LeafVector> _leaves;
128 const GetObjectLocalCoords _getLocalCoords;
129 const AddLeafObject _addLeafObject;
130 const MakeLeaf _makeLeaf;