]> git.mxchange.org Git - simgear.git/blob - simgear/scene/util/QuadTreeBuilder.hxx
Merge branch 'maint' into next
[simgear.git] / simgear / scene / util / QuadTreeBuilder.hxx
1 // Copyright (C) 2008  Tim Moore
2 //
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.
7 //
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.
12 //
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
18
19 #include <algorithm>
20 #include <functional>
21 #include <vector>
22 #include <osg/BoundingBox>
23 #include <osg/ref_ptr>
24 #include <osg/Node>
25 #include <osg/Group>
26 #include <osg/Matrix>
27 #include <osg/Vec2>
28 #include <osg/LOD>
29 #include "VectorArrayAdapter.hxx"
30
31 namespace simgear
32 {
33 typedef std::map<osg::ref_ptr<osg::Node>,int> LodMap;
34
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 {
39 public:
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)
47     {
48         using namespace std;
49         using namespace osg;
50         generate(_leafStorage.begin(), _leafStorage.end(), _makeLeaf);
51         vector<Group*> parentNodes(1);
52         parentNodes[0] = _root.get();
53         unsigned leafDim = 2;
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));
62                 }
63             }
64             parentNodes.swap(interiorNodes);
65         }
66         VectorArrayAdapter<vector<Group*> > leafParents(parentNodes,
67                                                         _dimension / 2);
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));
71     }
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; }
76     ~QuadTreeBuilder() {}
77     osg::Group* getRoot() { return _root.get(); }
78
79     void addNode(ObjectType& obj)
80     {
81         using namespace osg;
82         const Vec3 center(_getLocalCoords(obj));
83         int x = 0;
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));
88         int y = 0;
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);
94     }
95     // STL craziness
96     struct AddNode
97     {
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;
102     };
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)
107     {
108         using namespace osg;
109         BoundingBox extents;
110         for (ForwardIterator iter = begin; iter != end; ++iter) {
111             const Vec3 center = _getLocalCoords(*iter);
112             extents.expandBy(center);
113         }
114         _min = Vec2(extents.xMin(), extents.yMin());
115         _max = Vec2(extents.xMax(), extents.yMax());
116         std::for_each(begin, end, AddNode(this));
117     }
118
119 protected:
120     typedef std::vector<LeafType> LeafVector;
121     osg::ref_ptr<osg::Group> _root;
122     osg::Vec2 _min;
123     osg::Vec2 _max;
124     int _depth;
125     int _dimension;
126     LeafVector _leafStorage;
127     VectorArrayAdapter<LeafVector> _leaves;
128     const GetObjectLocalCoords _getLocalCoords;
129     const AddLeafObject _addLeafObject;
130     const MakeLeaf _makeLeaf;
131 };
132
133 }
134 #endif