Skip to content

Commit

Permalink
BSP Builder|Refactor: Further clean up of SuperBlockmap
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 26, 2012
1 parent 7e0eb4a commit e2243fe
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 107 deletions.
2 changes: 2 additions & 0 deletions doomsday/engine/engine.pro
Expand Up @@ -124,6 +124,7 @@ DENG_HEADERS = \
portable/include/bspbuilder/intersection.hh \
portable/include/bspbuilder/superblockmap.hh \
portable/include/bspleaf.h \
portable/include/bspnode.h \
portable/include/b_command.h \
portable/include/b_context.h \
portable/include/b_device.h \
Expand Down Expand Up @@ -398,6 +399,7 @@ SOURCES += \
portable/src/bspbuilder/node.cpp \
portable/src/bspbuilder/superblockmap.cpp \
portable/src/bspleaf.c \
portable/src/bspnode.c \
portable/src/cbuffer.c \
portable/src/cl_frame.c \
portable/src/cl_infine.c \
Expand Down
71 changes: 54 additions & 17 deletions doomsday/engine/portable/include/bspbuilder/superblockmap.hh
Expand Up @@ -38,6 +38,14 @@
#include <list>
#include <assert.h>

#ifdef RIGHT
# undef RIGHT
#endif

#ifdef LEFT
# undef LEFT
#endif

namespace de {

class SuperBlockmap;
Expand All @@ -48,9 +56,20 @@ friend class SuperBlockmap;
public:
typedef std::list<bsp_hedge_t*> HEdges;

enum ChildId
{
RIGHT,
LEFT
};

static void inline assertValidChildId(ChildId childId)
{
assert(childId == RIGHT || childId == LEFT);
}

private:
SuperBlock(SuperBlockmap& blockmap) :
bmap(blockmap), hedges(0), realNum(0), miniNum(0)
_bmap(blockmap), _hedges(0), _realNum(0), _miniNum(0)
{}

~SuperBlock() { clear(); }
Expand All @@ -61,7 +80,7 @@ public:
*
* @return The owning SuperBlockmap instance.
*/
SuperBlockmap& blockmap() const { return bmap; }
SuperBlockmap& blockmap() const { return _bmap; }

/**
* Retrieve the axis-aligned bounding box defined for this superblock
Expand All @@ -72,16 +91,34 @@ public:
*/
const AABox& bounds() const;

bool hasChild(ChildId childId) const;

inline bool hasRight() const { return hasChild(RIGHT); }
inline bool hasLeft() const { return hasChild(LEFT); }

/**
* Retrieve a pointer to a sub-block of this superblock.
*
* @param left non-zero= pick the "left" child.
*
* @return Selected child superblock else @c NULL if none.
* Retrieve a pointer to a sub-block if present.
* @param childId Identifier to the sub-block.
* @return Selected sub-block else @c NULL.
*/
SuperBlock* child(int left);
SuperBlock& child(ChildId childId);

/**
* Retrieve the right sub-block. Use SuperBlock::hasRight() before calling.
* @return Right sub-block.
*/
inline SuperBlock& right() { return child(RIGHT); }

/**
* Retrieve the right sub-block. Use SuperBlock::hasLeft() before calling.
* @return Left sub-block else @c NULL.
*/
inline SuperBlock& left() { return child(LEFT); }

SuperBlock* addChild(ChildId childId, bool splitVertical);

SuperBlock* addChild(int vertical, int left);
inline SuperBlock* addRight(bool splitVertical) { return addChild(RIGHT, splitVertical); }
inline SuperBlock* addLeft(bool splitVertical) { return addChild(LEFT, splitVertical); }

/**
* Perform a depth-first traversal over all child superblocks and
Expand All @@ -96,8 +133,8 @@ public:
*/
int traverse(int (C_DECL *callback)(SuperBlock*, void*), void* parameters=NULL);

inline HEdges::const_iterator hedgesBegin() const { return hedges.begin(); }
inline HEdges::const_iterator hedgesEnd() const { return hedges.end(); }
inline HEdges::const_iterator hedgesBegin() const { return _hedges.begin(); }
inline HEdges::const_iterator hedgesEnd() const { return _hedges.end(); }

void findHEdgeBounds(AABoxf& bounds);

Expand Down Expand Up @@ -135,7 +172,7 @@ public:

protected:
/// KdTree node in the owning SuperBlockmap.
KdTreeNode* tree;
KdTreeNode* _tree;

private:
void clear();
Expand All @@ -144,15 +181,15 @@ private:
void inline linkHEdge(bsp_hedge_t* hedge);

/// SuperBlockmap that owns this SuperBlock.
SuperBlockmap& bmap;
SuperBlockmap& _bmap;

/// Half-edges completely contained by this block.
HEdges hedges;
HEdges _hedges;

/// Number of real half-edges and minihedges contained by this block
/// (including all sub-blocks below it).
int realNum;
int miniNum;
int _realNum;
int _miniNum;
};

class SuperBlockmap {
Expand Down Expand Up @@ -193,7 +230,7 @@ private:
* Division of a block always occurs horizontally:
* e.g. 512x512 -> 256x512 -> 256x256.
*/
KdTree* kdTree;
KdTree* _kdTree;
};

} // namespace de
Expand Down
44 changes: 44 additions & 0 deletions doomsday/engine/portable/include/bspnode.h
@@ -0,0 +1,44 @@
/**
* @file bspnode.h
* Map BSP node. @ingroup map
*
* @authors Copyright &copy; 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright &copy; 2006-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_MAP_BSPNODE
#define LIBDENG_MAP_BSPNODE

#include "r_data.h"
#include "p_dmu.h"

#ifdef __cplusplus
extern "C" {
#endif

BspNode* BspNode_New(double const origin[2], double const angle[2]);

BspNode* BspNode_SetChildBounds(BspNode* node, int left, AABoxf* bounds);

#define BspNode_SetRightBounds(node, bounds) BspNode_SetChildBounds((node), false, (bounds))
#define BspNode_SetLeftBounds(node, bounds) BspNode_SetChildBounds((node), true, (bounds))

#ifdef __cplusplus
} // extern "C"
#endif

#endif /// LIBDENG_MAP_BSPNODE
1 change: 1 addition & 0 deletions doomsday/engine/portable/include/de_play.h
Expand Up @@ -38,6 +38,7 @@
#include "plane.h"
#include "hedge.h"
#include "bspleaf.h"
#include "bspnode.h"
#include "sector.h"
#include "p_polyobjs.h"
#include "p_dmu.h"
Expand Down
6 changes: 6 additions & 0 deletions doomsday/engine/portable/include/kdtree.h
Expand Up @@ -69,8 +69,14 @@ KdTreeNode* KdTreeNode_Parent(KdTreeNode* kdTreeNode);

KdTreeNode* KdTreeNode_Child(KdTreeNode* kdTreeNode, int left);

#define KdTreeNode_Right(kdTreeNode) KdTreeNode_Child((kdTreeNode), false)
#define KdTreeNode_Left(kdTreeNode) KdTreeNode_Child((kdTreeNode), true)

KdTreeNode* KdTreeNode_AddChild(KdTreeNode* kdTreeNode, double distance, int vertical, int left, void* userData);

#define KdTreeNode_AddRight(kdTreeNode), distance, vertical, userData) KdTreeNode_AddChild((kdTreeNode), (distance), (vertical), false, (userData))
#define KdTreeNode_AddLeft(kdTreeNode), distance, vertical, userData) KdTreeNode_AddChild((kdTreeNode), (distance), (vertical), true, (userData))

int KdTreeNode_Traverse2(KdTreeNode* kdTreeNode, int (*callback)(KdTreeNode*, void*), void* parameters);
int KdTreeNode_Traverse(KdTreeNode* kdTreeNode, int (*callback)(KdTreeNode*, void*), void* parameters);

Expand Down
6 changes: 6 additions & 0 deletions doomsday/engine/portable/include/m_binarytree.h
Expand Up @@ -75,6 +75,9 @@ void BinaryTree_Delete(BinaryTree* tree);
*/
BinaryTree* BinaryTree_Child(BinaryTree* tree, boolean left);

#define BinaryTree_Right(tree) BinaryTree_Child((tree), false)
#define BinaryTree_Left(tree) BinaryTree_Child((tree), true)

/**
* Retrieve the user data associated with the specified (sub)tree.
*
Expand All @@ -94,6 +97,9 @@ void* BinaryTree_UserData(BinaryTree* tree);
*/
void BinaryTree_SetChild(BinaryTree* tree, boolean left, BinaryTree* subtree);

#define BinaryTree_SetRight(tree, subtree) BinaryTree_SetChild((tree), false, (subtree))
#define BinaryTree_SetLeft(tree, subtree) BinaryTree_SetChild((tree), true, (subtree))

/**
* Set the user data assoicated with the specified (sub)tree.
*
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/bspbuilder/bspbuilder.cpp
Expand Up @@ -366,8 +366,8 @@ boolean BspBuilder::build(GameMap* map, Vertex*** vertexes, uint* numVertexes)

if(rootNode && !BinaryTree_IsLeaf(rootNode))
{
rHeight = (long) BinaryTree_Height(BinaryTree_Child(rootNode, RIGHT));
lHeight = (long) BinaryTree_Height(BinaryTree_Child(rootNode, LEFT));
rHeight = (long) BinaryTree_Height(BinaryTree_Right(rootNode));
lHeight = (long) BinaryTree_Height(BinaryTree_Left(rootNode));
}
else
{
Expand Down

0 comments on commit e2243fe

Please sign in to comment.