Skip to content

Commit

Permalink
BspNode|World: Added BspNode method for calculating the height of a s…
Browse files Browse the repository at this point in the history
…ubtree
  • Loading branch information
danij-deng committed Jan 19, 2014
1 parent 2d173d5 commit 1a396ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
19 changes: 17 additions & 2 deletions doomsday/client/include/world/bspnode.h
Expand Up @@ -69,6 +69,11 @@ class BspNode : public de::MapElement
*/
de::Partition const &partition() const;

/**
* Calculates the height of this BSP subtree (note result is not cached).
*/
size_t height() const;

/**
* Returns @c true iff the specified child is configured.
*/
Expand All @@ -91,7 +96,14 @@ class BspNode : public de::MapElement
*
* @see hasChild()
*/
de::MapElement &child(int left) const;
de::MapElement &child(int left);
de::MapElement const &child(int left) const;

inline de::MapElement &right() { return child(Right); }
inline de::MapElement const &right() const { return child(Right); }

inline de::MapElement &left() { return child(Left); }
inline de::MapElement const &left() const { return child(Left); }

/**
* Returns a pointer to the specified child of the BSP node, which may be
Expand All @@ -101,7 +113,10 @@ class BspNode : public de::MapElement
*
* @see hasChild()
*/
inline de::MapElement *childPtr(int left) const {
inline de::MapElement *childPtr(int left) {
return hasChild(left)? &child(left) : 0;
}
inline de::MapElement const *childPtr(int left) const {
return hasChild(left)? &child(left) : 0;
}

Expand Down
19 changes: 18 additions & 1 deletion doomsday/client/src/world/bspnode.cpp
Expand Up @@ -67,12 +67,24 @@ Partition const &BspNode::partition() const
return d->partition;
}

size_t BspNode::height() const
{
DENG2_ASSERT(hasLeft() || hasRight());
size_t rHeight = 0;
if(hasRight() && right().type() == DMU_BSPNODE)
rHeight = right().as<BspNode>().height();
size_t lHeight = 0;
if(hasLeft() && left().type() == DMU_BSPNODE)
lHeight = left().as<BspNode>().height();
return (rHeight> lHeight? rHeight : lHeight) + 1;
}

bool BspNode::hasChild(int left) const
{
return *d->childAdr(left) != 0;
}

MapElement &BspNode::child(int left) const
MapElement &BspNode::child(int left)
{
if(MapElement *childElm = *d->childAdr(left))
{
Expand All @@ -82,6 +94,11 @@ MapElement &BspNode::child(int left) const
throw MissingChildError("BspNode::child", QString("No %1 child is configured").arg(left? "left" : "right"));
}

MapElement const &BspNode::child(int left) const
{
return const_cast<MapElement &>(const_cast<BspNode *>(this)->child(left));
}

void BspNode::setChild(int left, MapElement *newChild)
{
if(!newChild || newChild != this)
Expand Down

0 comments on commit 1a396ec

Please sign in to comment.