00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <osg/BoundingBox>
00018 #include <osg/Math>
00019 #include <simgear/scene/util/SGNodeMasks.hxx>
00020
00021 #include "QuadTreeBuilder.hxx"
00022
00023 using namespace std;
00024 using namespace osg;
00025
00026 namespace simgear
00027 {
00028 #if 0
00029 QuadTreeBuilder::QuadTreeBuilder(const Vec2& min, const Vec2& max, int depth) :
00030 _root(new osg::Group), _min(min), _max(max), _depth(depth),
00031 _dimension(1 << depth), _leafStorage(_dimension * _dimension),
00032 _leaves(_leafStorage, _dimension)
00033 {
00034 for (LeafVector::iterator iter = _leafStorage.begin();
00035 iter != _leafStorage.end();
00036 ++iter)
00037 *iter = new LOD;
00038 vector<Group*> parentNodes(1);
00039 parentNodes[0] = _root.get();
00040 unsigned leafDim = 2;
00041 for (int i = 0; i < depth - 1; ++i, leafDim *= 2) {
00042 VectorArrayAdapter<vector<Group*> > parents(parentNodes, leafDim / 2);
00043 vector<Group*> interiorNodes(leafDim * leafDim);
00044 VectorArrayAdapter<vector<Group*> > interiors(interiorNodes, leafDim);
00045 for (unsigned j = 0; j < leafDim; ++j) {
00046 for (unsigned k = 0; k < leafDim; ++k) {
00047 interiors(j, k) = new Group;
00048 parents(j / 2, k / 2)->addChild(interiors(j, k));
00049 }
00050 }
00051 parentNodes.swap(interiorNodes);
00052 }
00053 VectorArrayAdapter<vector<Group*> > leafParents(parentNodes,
00054 _dimension / 2);
00055 for (int j = 0; j < _dimension; ++j)
00056 for (int k =0; k < _dimension; ++k)
00057 leafParents(j / 2, k / 2)->addChild(_leaves(j, k));
00058 }
00059
00060 void QuadTreeBuilder::addNode(Node* node, int lod, const Matrix& transform)
00061 {
00062 Vec3 center = node->getBound().center() * transform;
00063
00064 int x = (int)(_dimension * (center.x() - _min.x()) / (_max.x() - _min.x()));
00065 x = clampTo(x, 0, (_dimension - 1));
00066 int y = (int)(_dimension * (center.y() - _min.y()) / (_max.y() - _min.y()));
00067 y = clampTo(y, 0, (_dimension -1));
00068
00069 _leaves(y, x)->addChild(node, 0, lod);
00070 }
00071
00072 osg::Group* QuadTreeBuilder::makeQuadTree(LodMap& models,
00073 const Matrix& transform)
00074 {
00075 typedef vector<ref_ptr<Node> > NodeList;
00076 BoundingBox extents;
00077 for (LodMap::iterator iter = models.begin(); iter != models.end(); ++iter) {
00078 const Vec3 center = (*iter).first->getBound().center() * transform;
00079 extents.expandBy(center);
00080 }
00081 const Vec2 quadMin(extents.xMin(), extents.yMin());
00082 const Vec2 quadMax(extents.xMax(), extents.yMax());
00083 ref_ptr<Group> result;
00084 {
00085 QuadTreeBuilder quadTree(quadMin, quadMax);
00086 for (LodMap::iterator iter = models.begin();
00087 iter != models.end();
00088 ++iter) {
00089 quadTree.addNode(iter->first.get(), iter->second, transform);
00090 }
00091 result = quadTree.getRoot();
00092 }
00093 return result.release();
00094 }
00095 #endif
00096 }