Skip to content

Commit

Permalink
World: Reimplemented BSP tree with de::BinaryTree
Browse files Browse the repository at this point in the history
de::Map now takes ownership of what was Partitioner's internal tree
for use as the final BSP tree, rather than claiming each map element
individually.

Todo: Cleanup
  • Loading branch information
danij-deng committed Oct 22, 2014
1 parent 6996d81 commit 6d1f593
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 256 deletions.
6 changes: 3 additions & 3 deletions doomsday/client/include/world/bsp/bsptreenode.h
Expand Up @@ -20,14 +20,14 @@
#ifndef DENG_WORLD_MAP_BSPTREE_H
#define DENG_WORLD_MAP_BSPTREE_H

#include <de/BinaryTree>
#include "BspNode"
//#include <de/BinaryTree>
//#include "MapElement"

/**
* Nodes in BspBuilder's internal tree are modelled with this type.
*
* @ingroup bsp
*/
typedef de::BinaryTree<BspElement *> BspTreeNode;
//typedef de::BinaryTree<de::MapElement *> BspElement;

#endif // DENG_WORLD_MAP_BSPTREE_H
10 changes: 5 additions & 5 deletions doomsday/client/include/world/bsp/partitioner.h
Expand Up @@ -27,7 +27,7 @@
#include <de/Observers>
#include <de/Vector>

#include "world/bsp/bsptreenode.h" /// @todo remove me
#include "world/map.h"

class Line;
class Sector;
Expand Down Expand Up @@ -96,7 +96,7 @@ class Partitioner
* @return Root tree node of the resultant BSP otherwise @c 0 if no usable
* tree data was produced.
*/
BspTreeNode *buildBsp(LineSet const &lines, Mesh &mesh);
BspElement *buildBsp(LineSet const &lines, Mesh &mesh);

/**
* Retrieve a pointer to the root BinaryTree node for the constructed BSP.
Expand All @@ -105,7 +105,7 @@ class Partitioner
* The only time upon which @c 0 is returned is if called prior to calling
* build()
*/
BspTreeNode *root() const;
BspElement *root() const;

/**
* Retrieve the number of Segments owned by the partitioner. When the build
Expand Down Expand Up @@ -145,9 +145,9 @@ class Partitioner
/**
* Relinquish ownership of the specified BSP data element to the caller.
*
* @param bspElement BSP data element to relinquish ownership of.
* @param mapElement BSP data element to relinquish ownership of.
*/
void take(BspElement *bspElement);
void take(MapElement *mapElement);

private:
DENG2_PRIVATE(d)
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/include/world/bspleaf.h
Expand Up @@ -40,7 +40,7 @@ class Sector;
*
* @ingroup world
*/
class BspLeaf : public BspElement
class BspLeaf : public de::MapElement
{
public:
/// Required subspace is missing. @ingroup errors
Expand All @@ -51,7 +51,7 @@ class BspLeaf : public BspElement
* Construct a new BSP leaf and optionally attribute it to @a sector.
* Ownership is unaffected.
*/
BspLeaf(Sector *sector = 0);
BspLeaf();//Sector *sector = 0);

/**
* Determines whether a subspace geometry is attributed to the BSP leaf half-space.
Expand Down
79 changes: 1 addition & 78 deletions doomsday/client/include/world/bspnode.h
Expand Up @@ -26,20 +26,6 @@
#include <de/Error>
#include <de/aabox.h>

class BspElement : public de::MapElement
{
public:
enum {
Node,
Leaf
};

BspElement(int t = Node, de::MapElement *parent = 0);
virtual ~BspElement() {}

DENG2_AS_IS_METHODS()
};

/**
* Represents a node in the map's binary space partition (BSP) tree. Each node
* defines a partition line which divides the subspace in two, a left child and
Expand All @@ -54,18 +40,12 @@ class BspElement : public de::MapElement
*
* @ingroup world
*/
class BspNode : public BspElement
class BspNode : public de::MapElement
{
DENG2_NO_COPY (BspNode)
DENG2_NO_ASSIGN(BspNode)

public:
/// An invalid child element was specified. @ingroup errors
DENG2_ERROR(InvalidChildError);

/// Required child element is missing. @ingroup errors
DENG2_ERROR(MissingChildError);

/// Child element identifiers:
enum { Right, Left };

Expand All @@ -83,63 +63,6 @@ class BspNode : public BspElement
*/
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.
*/
bool hasChild(int left) const;

/**
* Returns @c true iff a @em right child is configured.
*/
inline bool hasRight() const { return hasChild(Right); }

/**
* Returns @c true iff a @em left child is configured.
*/
inline bool hasLeft() const { return hasChild(Left); }

/**
* Returns the specified child of the node.
*
* @param left If not @c 0 return the Left child; otherwise the Right child.
*
* @see hasChild()
*/
BspElement &child(int left);
BspElement const &child(int left) const;

inline BspElement &right() { return child(Right); }
inline BspElement const &right() const { return child(Right); }

inline BspElement &left() { return child(Left); }
inline BspElement const &left() const { return child(Left); }

/**
* Returns a pointer to the specified child of the BSP node, which may be
* @c 0 if no child is configured.
*
* @param left If not @c 0 return the Left child; otherwise the Right child.
*
* @see hasChild()
*/
inline BspElement *childPtr(int left) {
return hasChild(left)? &child(left) : 0;
}
inline BspElement const *childPtr(int left) const {
return hasChild(left)? &child(left) : 0;
}

void setChild(int left, BspElement *newChild);

inline void setRight(BspElement *newChild) { setChild(Right, newChild); }

inline void setLeft(BspElement *newChild) { setChild(Left, newChild); }

/**
* Returns the axis-aligned bounding box for the specified child, which,
* encompases all the vertexes which define the geometry of that subspace
Expand Down
3 changes: 1 addition & 2 deletions doomsday/client/include/world/linesighttest.h
Expand Up @@ -23,8 +23,7 @@

#include <de/libcore.h>
#include <de/Vector>

class BspElement;
#include "world/map.h"

namespace de {

Expand Down
10 changes: 10 additions & 0 deletions doomsday/client/include/world/map.h
Expand Up @@ -63,6 +63,16 @@ class BiasTracker;

class LineBlockmap;

#include <de/BinaryTree>
#include "BspNode"

/**
* Nodes in BspBuilder's internal tree are modelled with this type.
*
* @ingroup bsp
*/
typedef de::BinaryTree<de::MapElement *> BspElement;

namespace de {

class Blockmap;
Expand Down
10 changes: 5 additions & 5 deletions doomsday/client/src/render/rend_main.cpp
Expand Up @@ -2944,16 +2944,16 @@ static void traverseBspAndDrawSubspaces(BspElement *bspElement)
{
DENG2_ASSERT(bspElement != 0);

while(bspElement->type() != BspElement::Leaf)
while(!bspElement->isLeaf())
{
// Descend deeper into the nodes.
BspNode &bspNode = bspElement->as<BspNode>();
BspNode &bspNode = bspElement->userData()->as<BspNode>();

// Decide which side the view point is on.
int eyeSide = bspNode.partition().pointOnSide(eyeOrigin) < 0;

// Recursively divide front space.
traverseBspAndDrawSubspaces(bspNode.childPtr(eyeSide));
traverseBspAndDrawSubspaces(bspElement->childPtr(BspElement::ChildId(eyeSide)));

// If the clipper is full we're pretty much done. This means no geometry
// will be visible in the distance because every direction has already
Expand All @@ -2962,12 +2962,12 @@ static void traverseBspAndDrawSubspaces(BspElement *bspElement)
return;

// ...and back space.
bspElement = bspNode.childPtr(eyeSide ^ 1);
bspElement = bspElement->childPtr(BspElement::ChildId(eyeSide ^ 1));
}
// We've arrived at a leaf.

// Only leafs with a convex subspace geometry have drawable geometries.
if(ConvexSubspace *subspace = bspElement->as<BspLeaf>().subspacePtr())
if(ConvexSubspace *subspace = bspElement->userData()->as<BspLeaf>().subspacePtr())
{
DENG2_ASSERT(subspace->hasCluster());

Expand Down

0 comments on commit 6d1f593

Please sign in to comment.