Skip to content

Commit

Permalink
BSP Builder|Refactor: Began refactoring SuperBlock into a typical two…
Browse files Browse the repository at this point in the history
… dimensional kd-tree
  • Loading branch information
danij-deng committed Mar 20, 2012
1 parent 858904c commit 6e684ec
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 162 deletions.
2 changes: 2 additions & 0 deletions doomsday/engine/engine.pro
Expand Up @@ -202,6 +202,7 @@ DENG_HEADERS = \
portable/include/hedge.h \
portable/include/huffman.h \
portable/include/image.h \
portable/include/kdtree.h \
portable/include/library.h \
portable/include/linedef.h \
portable/include/lumpdirectory.h \
Expand Down Expand Up @@ -455,6 +456,7 @@ SOURCES += \
portable/src/hedge.c \
portable/src/huffman.c \
portable/src/image.c \
portable/src/kdtree.c \
portable/src/library.c \
portable/src/linedef.c \
portable/src/lumpdirectory.c \
Expand Down
24 changes: 5 additions & 19 deletions doomsday/engine/portable/include/bsp_superblock.h
Expand Up @@ -114,15 +114,18 @@ int SuperBlock_IterateHEdges(SuperBlock* superblock, int (*callback)(bsp_hedge_t
* Retrieve a pointer to a sub-block of this superblock.
*
* @param superblock SuperBlock instance.
* @param left @c true= pick the "left" child.
* @param left non-zero= pick the "left" child.
*
* @return Selected child superblock else @c NULL if none.
*/
SuperBlock* SuperBlock_Child(SuperBlock* superblock, boolean left);
SuperBlock* SuperBlock_Child(SuperBlock* superblock, int left);

int SuperBlock_Traverse2(SuperBlock* superblock, int (*callback)(SuperBlock*, void*), void* parameters);
int SuperBlock_Traverse(SuperBlock* superblock, int (*callback)(SuperBlock*, void*)/*, parameters=NULL*/);

int SuperBlock_PostTraverse2(SuperBlock* sb, int(*callback)(SuperBlock*, void*), void* parameters);
int SuperBlock_PostTraverse(SuperBlock* sb, int(*callback)(SuperBlock*, void*)/*, parameters = NULL*/);

/**
* Find the axis-aligned bounding box defined by the vertices of all
* HEdges within this superblock. If no HEdges are linked then @a bounds
Expand All @@ -133,21 +136,4 @@ int SuperBlock_Traverse(SuperBlock* superblock, int (*callback)(SuperBlock*, voi
*/
void SuperBlock_FindHEdgeListBounds(SuperBlock* superblock, AABoxf* bounds);

/**
* @todo The following functions do not belong in this module.
*/

/**
* Init the superblock allocator.
*/
void BSP_InitSuperBlockAllocator(void);

/**
* Free all the superblocks on the quick-alloc list.
*/
void BSP_ShutdownSuperBlockAllocator(void);

SuperBlock* BSP_NewSuperBlock(const AABox* bounds);
void BSP_RecycleSuperBlock(SuperBlock* superblock);

#endif /// LIBDENG_MAP_BSP_SUPERBLOCK
57 changes: 57 additions & 0 deletions doomsday/engine/portable/include/kdtree.h
@@ -0,0 +1,57 @@
/**
* @file kdtree.h
* Kd-Tree data structure. @ingroup data
*
* Based on glBSP 2.24 (in turn, based on BSP 2.3), which is hosted on
* SourceForge: http://sourceforge.net/projects/glbsp/
*
* @authors Copyright © 2007-2012 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2000-2007 Andrew Apted <ajapted@gmail.com>
* @authors Copyright © 1998-2000 Colin Reed <cph@moria.org.uk>
* @authors Copyright © 1998-2000 Lee Killough <killough@rsn.hp.com>
*
* @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_KDTREE
#define LIBDENG_KDTREE

#include "de_platform.h"

struct kdtree_s;

typedef struct kdtree_s KdTree;

KdTree* KdTree_New(const AABox* bounds);
KdTree* KdTree_NewWithUserData(const AABox* bounds, void* userData);

void KdTree_Delete(KdTree* kdTree);

const AABox* KdTree_Bounds(KdTree* kdTree);

void* KdTree_UserData(KdTree* kdTree);
KdTree* KdTree_SetUserData(KdTree* kdTree, void* userData);

KdTree* KdTree_Child(KdTree* kdTree, int left);

KdTree* KdTree_AddChild(KdTree* kdTree, const AABox* bounds, int left, void* userData);

int KdTree_Traverse2(KdTree* kdTree, int (*callback)(KdTree*, void*), void* parameters);
int KdTree_Traverse(KdTree* kdTree, int (*callback)(KdTree*, void*), void* parameters);

int KdTree_PostTraverse2(KdTree* kdTree, int(*callback)(KdTree*, void*), void* parameters);
int KdTree_PostTraverse(KdTree* kdTree, int(*callback)(KdTree*, void*)/*, parameters=NULL*/);

#endif /// LIBDENG_KDTREE
6 changes: 2 additions & 4 deletions doomsday/engine/portable/src/bsp_main.c
Expand Up @@ -138,7 +138,7 @@ static SuperBlock* createInitialHEdges(GameMap* map)
blockBounds.maxX = blockBounds.minX + 128 * M_CeilPow2(bw);
blockBounds.maxY = blockBounds.minY + 128 * M_CeilPow2(bh);

block = BSP_NewSuperBlock(&blockBounds);
block = SuperBlock_New(&blockBounds);

for(i = 0; i < map->numLineDefs; ++i)
{
Expand Down Expand Up @@ -269,7 +269,6 @@ boolean BSP_Build(GameMap* map, Vertex*** vertexes, uint* numVertexes)
// It begins...
startTime = Sys_GetRealTime();

BSP_InitSuperBlockAllocator();
BSP_InitHPlaneInterceptAllocator();
BSP_InitHEdgeAllocator();

Expand All @@ -296,7 +295,7 @@ boolean BSP_Build(GameMap* map, Vertex*** vertexes, uint* numVertexes)
VERBOSE2( Con_Message("BuildNodes: Done in %.2f seconds.\n", (Sys_GetRealTime() - buildStartTime) / 1000.0f));
}

BSP_RecycleSuperBlock(hEdgeList);
SuperBlock_Delete(hEdgeList);

if(builtOK)
{
Expand Down Expand Up @@ -334,7 +333,6 @@ boolean BSP_Build(GameMap* map, Vertex*** vertexes, uint* numVertexes)
// Free temporary storage.
BSP_ShutdownHEdgeAllocator();
BSP_ShutdownHPlaneInterceptAllocator();
BSP_ShutdownSuperBlockAllocator();

// How much time did we spend?
VERBOSE2( Con_Message(" Done in %.2f seconds.\n", (Sys_GetRealTime() - startTime) / 1000.0f) );
Expand Down
8 changes: 4 additions & 4 deletions doomsday/engine/portable/src/bsp_node.c
Expand Up @@ -989,8 +989,8 @@ boolean BuildNodes(SuperBlock* hEdgeList, binarytree_t** parent, size_t depth,

// Create left and right super blocks.
// Copy the bounding box of the edge list to the superblocks.
hEdgeSet[RIGHT] = BSP_NewSuperBlock(SuperBlock_Bounds(hEdgeList));
hEdgeSet[LEFT] = BSP_NewSuperBlock(SuperBlock_Bounds(hEdgeList));
hEdgeSet[RIGHT] = SuperBlock_New(SuperBlock_Bounds(hEdgeList));
hEdgeSet[LEFT] = SuperBlock_New(SuperBlock_Bounds(hEdgeList));

// Divide the half-edges into two lists: left & right.
BSP_PartitionHEdges(hEdgeList, hEdgeSet[RIGHT], hEdgeSet[LEFT], hPlane);
Expand All @@ -1009,15 +1009,15 @@ boolean BuildNodes(SuperBlock* hEdgeList, binarytree_t** parent, size_t depth,

builtOK = BuildNodes(hEdgeSet[RIGHT], &subTree, depth + 1, hPlane);
BinaryTree_SetChild(*parent, RIGHT, subTree);
BSP_RecycleSuperBlock(hEdgeSet[RIGHT]);
SuperBlock_Delete(hEdgeSet[RIGHT]);

if(builtOK)
{
builtOK = BuildNodes(hEdgeSet[LEFT], &subTree, depth + 1, hPlane);
BinaryTree_SetChild(*parent, LEFT, subTree);
}

BSP_RecycleSuperBlock(hEdgeSet[LEFT]);
SuperBlock_Delete(hEdgeSet[LEFT]);

return builtOK;
}
Expand Down

0 comments on commit 6e684ec

Please sign in to comment.