From 03f23b235ae82ef7d324a6a08c22ae3d53d754bb Mon Sep 17 00:00:00 2001 From: bzzt Date: Wed, 20 Feb 2019 13:37:00 +0000 Subject: [PATCH] Optimize terrain QuadTree build --- components/terrain/quadtreenode.cpp | 10 ---------- components/terrain/quadtreenode.hpp | 17 +++++++++++++---- components/terrain/quadtreeworld.cpp | 8 +++++--- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/components/terrain/quadtreenode.cpp b/components/terrain/quadtreenode.cpp index 4b4df4365..f8237306e 100644 --- a/components/terrain/quadtreenode.cpp +++ b/components/terrain/quadtreenode.cpp @@ -71,16 +71,6 @@ QuadTreeNode::~QuadTreeNode() { } -QuadTreeNode* QuadTreeNode::getParent() -{ - return mParent; -} - -QuadTreeNode *QuadTreeNode::getChild(unsigned int i) -{ - return static_cast(Group::getChild(i)); -} - QuadTreeNode *QuadTreeNode::getNeighbour(Direction dir) { return mNeighbours[dir]; diff --git a/components/terrain/quadtreenode.hpp b/components/terrain/quadtreenode.hpp index 30e998119..9c925a4d9 100644 --- a/components/terrain/quadtreenode.hpp +++ b/components/terrain/quadtreenode.hpp @@ -35,10 +35,19 @@ namespace Terrain QuadTreeNode(QuadTreeNode* parent, ChildDirection dir, float size, const osg::Vec2f& center); virtual ~QuadTreeNode(); - QuadTreeNode* getParent(); - - QuadTreeNode* getChild(unsigned int i); - using osg::Group::getNumChildren; + inline QuadTreeNode* getParent() { return mParent; } + inline QuadTreeNode* getChild(unsigned int i) { return static_cast(Group::getChild(i)); } + inline unsigned int getNumChildren() const { return _children.size(); } + + // osg::Group::addChild() does a lot of unrelated stuff, but we just really want to add a child node. + void addChild(QuadTreeNode* child) + { + // QuadTree node should not contain more than 4 child nodes. + // Reserve enough space if this node is supposed to have child nodes. + _children.reserve(4); + _children.push_back(child); + child->addParent(this); + }; /// Returns our direction relative to the parent node, or Root if we are the root node. ChildDirection getDirection() { return mDirection; } diff --git a/components/terrain/quadtreeworld.cpp b/components/terrain/quadtreeworld.cpp index 594b2b755..b486f8d60 100644 --- a/components/terrain/quadtreeworld.cpp +++ b/components/terrain/quadtreeworld.cpp @@ -158,9 +158,12 @@ public: osg::BoundingBox boundingBox; for (unsigned int i=0; i<4; ++i) { - QuadTreeNode* child = addChild(parent, static_cast(i), halfSize); + osg::ref_ptr child = addChild(parent, static_cast(i), halfSize); if (child) + { boundingBox.expandBy(child->getBoundingBox()); + parent->addChild(child); + } } if (!boundingBox.valid()) @@ -169,7 +172,7 @@ public: parent->setBoundingBox(boundingBox); } - QuadTreeNode* addChild(QuadTreeNode* parent, ChildDirection direction, float size) + osg::ref_ptr addChild(QuadTreeNode* parent, ChildDirection direction, float size) { float halfSize = size/2.f; osg::Vec2f center; @@ -194,7 +197,6 @@ public: osg::ref_ptr node = new QuadTreeNode(parent, direction, size, center); node->setLodCallback(parent->getLodCallback()); node->setViewDataMap(mViewDataMap); - parent->addChild(node); if (node->getSize() > mMinSize) {