Skip to content

Commit

Permalink
BSP Builder: Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Apr 12, 2012
1 parent ffccdfa commit 45ba67b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 80 deletions.
38 changes: 32 additions & 6 deletions doomsday/engine/portable/include/map/bsp/hedgeinfo.h
Expand Up @@ -25,12 +25,17 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_BSPBUILDER_HEDGEINFO
#define LIBDENG_BSPBUILDER_HEDGEINFO
#ifndef LIBDENG_BSP_HEDGEINFO
#define LIBDENG_BSP_HEDGEINFO

#include "dd_types.h"
#include "m_misc.h"
#include "p_mapdata.h"

namespace de {
namespace bsp {

class SuperBlock;

/**
* Plain old data (POD) structure storing additional information about a
* half-edge produced by BspBuilder.
Expand All @@ -52,7 +57,7 @@ struct HEdgeInfo

// The superblock that contains this half-edge, or NULL if the half-edge
// is no longer in any superblock (e.g. now in a leaf).
void* block;
class SuperBlock* bmapBlock;

// Linedef that this half-edge initially comes from.
// For "real" half-edges, this is just the same as the 'linedef' field
Expand All @@ -61,9 +66,30 @@ struct HEdgeInfo

HEdgeInfo()
: pSX(0), pSY(0), pEX(0), pEY(0), pDX(0), pDY(0), pLength(0), pAngle(0), pPara(0), pPerp(0),
nextOnSide(0), prevOnSide(0), block(0), sourceLineDef(0)
nextOnSide(0), prevOnSide(0), bmapBlock(0), sourceLineDef(0)
{}

HEdgeInfo& initFromHEdge(const HEdge& hedge)
{
pSX = hedge.v[0]->buildData.pos[VX];
pSY = hedge.v[0]->buildData.pos[VY];
pEX = hedge.v[1]->buildData.pos[VX];
pEY = hedge.v[1]->buildData.pos[VY];
pDX = pEX - pSX;
pDY = pEY - pSY;

pLength = M_Length(pDX, pDY);
pAngle = M_SlopeToAngle(pDX, pDY);

pPerp = pSY * pDX - pSX * pDY;
pPara = -pSX * pDX - pSY * pDY;

Q_ASSERT(pLength > 0);
return *this;
}
};

} // namespace bsp
} // namespace de

#endif /// LIBDENG_BSPBUILDER_HEDGEINFO
#endif /// LIBDENG_BSP_HEDGEINFO
6 changes: 3 additions & 3 deletions doomsday/engine/portable/include/map/bsp/hedgeintercept.h
Expand Up @@ -25,8 +25,8 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_BSPBUILDER_HEDGEINTERCEPT
#define LIBDENG_BSPBUILDER_HEDGEINTERCEPT
#ifndef LIBDENG_BSP_HEDGEINTERCEPT
#define LIBDENG_BSP_HEDGEINTERCEPT

#include "dd_types.h"
#include "p_mapdata.h"
Expand Down Expand Up @@ -72,4 +72,4 @@ struct HEdgeIntercept
} // namespace bsp
} // namespace de

#endif /// LIBDENG_BSPBUILDER_HEDGEINTERCEPT
#endif /// LIBDENG_BSP_HEDGEINTERCEPT
6 changes: 3 additions & 3 deletions doomsday/engine/portable/include/map/bsp/hplane.h
Expand Up @@ -25,8 +25,8 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_BSPBUILDER_HPLANE
#define LIBDENG_BSPBUILDER_HPLANE
#ifndef LIBDENG_BSP_HPLANE
#define LIBDENG_BSP_HPLANE

#include <list>

Expand Down Expand Up @@ -159,4 +159,4 @@ class HPlane {
} // namespace bsp
} // namespace de

#endif /// LIBDENG_BSPBUILDER_HPLANE
#endif /// LIBDENG_BSP_HPLANE
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/map/bsp/linedefinfo.h
Expand Up @@ -25,8 +25,8 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_BSPBUILDER_LINEDEFINFO
#define LIBDENG_BSPBUILDER_LINEDEFINFO
#ifndef LIBDENG_BSP_LINEDEFINFO
#define LIBDENG_BSP_LINEDEFINFO

#include "p_mapdata.h"

Expand Down
36 changes: 10 additions & 26 deletions doomsday/engine/portable/include/map/bsp/partitioner.h
Expand Up @@ -34,32 +34,6 @@
namespace de {
namespace bsp {

/**
* @algorithm High-level description (courtesy of Raphael Quinet)
* 1 - Create one Seg for each SideDef: pick each LineDef in turn. If it
* has a "first" SideDef, then create a normal Seg. If it has a
* "second" SideDef, then create a flipped Seg.
* 2 - Call CreateNodes with the current list of Segs. The list of Segs is
* the only argument to CreateNodes.
* 3 - Save the Nodes, Segs and BspLeafs to disk. Start with the leaves of
* the Nodes tree and continue up to the root (last Node).
*
* CreateNodes does the following:
* 1 - Pick a nodeline amongst the Segs (minimize the number of splits and
* keep the tree as balanced as possible).
* 2 - Move all Segs on the right of the nodeline in a list (segs1) and do
* the same for all Segs on the left of the nodeline (in segs2).
* 3 - If the first list (segs1) contains references to more than one
* Sector or if the angle between two adjacent Segs is greater than
* 180 degrees, then call CreateNodes with this (smaller) list.
* Else, create a BspLeaf with all these Segs.
* 4 - Do the same for the second list (segs2).
* 5 - Return the new node (its two children are already OK).
*
* Each time CreateBspLeaf is called, the Segs are put in a global list.
* When there is no more Seg in CreateNodes' list, then they are all in the
* global list and ready to be saved to disk.
*/
class Partitioner
{
public:
Expand All @@ -76,6 +50,16 @@ class Partitioner

/**
* Build the BSP for the given map.
*
* @algorithm High-level description (courtesy of Raphael Quinet)
* 1 - Create one Seg for each SideDef: pick each LineDef in turn. If it
* has a "first" SideDef, then create a normal Seg. If it has a
* "second" SideDef, then create a flipped Seg.
* 2 - Call CreateNodes with the current list of Segs. The list of Segs is
* the only argument to CreateNodes.
* 3 - Save the Nodes, Segs and BspLeafs to disk. Start with the leaves of
* the Nodes tree and continue up to the root (last Node).
*
* @return @c true= iff completed successfully.
*/
bool build();
Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/portable/include/map/bsp/superblockmap.h
Expand Up @@ -30,6 +30,7 @@
#ifndef LIBDENG_BSP_SUPERBLOCKMAP
#define LIBDENG_BSP_SUPERBLOCKMAP

#include "dd_def.h"
#include "dd_types.h"
#include "p_mapdata.h"

Expand Down
47 changes: 12 additions & 35 deletions doomsday/engine/portable/src/map/bsp/partitioner.cpp
Expand Up @@ -68,8 +68,6 @@ static bool getAveragedCoords(BspLeaf* leaf, pvec2d_t midPoint);
static void logUnclosed(const BspLeaf* leaf);
static void initAABoxFromEditableLineDefVertexes(AABoxf* aaBox, const LineDef* line);
static int linkBspTreeNode(BspTreeNode& tree, void* /*parameters*/);
static int hedgeCounter(BspTreeNode& tree, void* parameters);
static void updateHEdgeInfo(const HEdge* hedge, HEdgeInfo& info);
static Sector* findFirstSectorInHEdgeList(const BspLeaf* leaf);

DENG_DEBUG_ONLY(static int printSuperBlockHEdgesWorker(SuperBlock* block, void* /*parameters*/));
Expand Down Expand Up @@ -231,7 +229,7 @@ struct Partitioner::Instance
SuperBlock* subblock = &block.push(*hedge);

// Associate this half-edge with the final subblock.
hInfo.block = static_cast<void*>(subblock);
hInfo.bmapBlock = subblock;
}
return hedge;
}
Expand Down Expand Up @@ -363,7 +361,7 @@ struct Partitioner::Instance
bool hedgeIsInLeaf(const HEdge& hedge) const
{
/// @todo Are we now able to determine this by testing hedge->leaf ?
return !hedgeInfo(hedge).block;
return !hedgeInfo(hedge).bmapBlock;
}

const HPlaneIntercept* makePartitionIntersection(HEdge* hedge, int leftSide)
Expand Down Expand Up @@ -561,10 +559,10 @@ struct Partitioner::Instance
oldInfo.nextOnSide = newHEdge;

oldHEdge->v[1] = newVert;
updateHEdgeInfo(oldHEdge, oldInfo);
oldInfo.initFromHEdge(*oldHEdge);

newHEdge->v[0] = newVert;
updateHEdgeInfo(newHEdge, newInfo);
newInfo.initFromHEdge(*newHEdge);

// Handle the twin.
if(oldHEdge->twin)
Expand All @@ -579,10 +577,10 @@ struct Partitioner::Instance
hedgeInfo(*oldHEdge->twin).prevOnSide = newHEdge->twin;

oldHEdge->twin->v[0] = newVert;
updateHEdgeInfo(oldHEdge->twin, hedgeInfo(*oldHEdge->twin));
hedgeInfo(*oldHEdge->twin).initFromHEdge(*oldHEdge->twin);

newHEdge->twin->v[1] = newVert;
updateHEdgeInfo(newHEdge->twin, hedgeInfo(*newHEdge->twin));
hedgeInfo(*newHEdge->twin).initFromHEdge(*newHEdge->twin);

// Has this already been added to a leaf?
if(hedgeIsInLeaf(*oldHEdge->twin))
Expand Down Expand Up @@ -682,9 +680,9 @@ struct Partitioner::Instance
// Ensure the new twin is inserted into the same block as the old twin.
if(hedge->twin && !hedgeIsInLeaf(*hedge->twin))
{
SuperBlock* block = reinterpret_cast<SuperBlock*>(hedgeInfo(*hedge->twin).block);
Q_ASSERT(block);
linkHEdgeInSuperBlockmap(*block, newHEdge->twin);
SuperBlock* bmapBlock = hedgeInfo(*hedge->twin).bmapBlock;
Q_ASSERT(bmapBlock);
linkHEdgeInSuperBlockmap(*bmapBlock, newHEdge->twin);
}

makePartitionIntersection(hedge, LEFT);
Expand Down Expand Up @@ -722,7 +720,7 @@ struct Partitioner::Instance
while((hedge = cur->pop()))
{
// Disassociate the half-edge from the blockmap.
hedgeInfo(*hedge).block = 0;
hedgeInfo(*hedge).bmapBlock = 0;

divideHEdge(hedge, rights, lefts);
}
Expand Down Expand Up @@ -1940,7 +1938,7 @@ struct Partitioner::Instance

HEdgeInfo& info = hedgeInfo(*hedge);
info.sourceLineDef = sourceLineDef;
updateHEdgeInfo(hedge, info);
info.initFromHEdge(*hedge);

return hedge;
}
Expand Down Expand Up @@ -1983,7 +1981,7 @@ struct Partitioner::Instance
while((hedge = cur->pop()))
{
// Disassociate the half-edge from the blockmap.
hedgeInfo(*hedge).block = 0;
hedgeInfo(*hedge).bmapBlock = 0;

// Link it into head of the leaf's list.
hedge->next = leaf->hedge;
Expand Down Expand Up @@ -2495,27 +2493,6 @@ static int linkBspTreeNode(BspTreeNode& tree, void* /*parameters*/)
return false; // Continue iteration.
}

static void updateHEdgeInfo(const HEdge* hedge, HEdgeInfo& info)
{
assert(hedge);

info.pSX = hedge->v[0]->buildData.pos[VX];
info.pSY = hedge->v[0]->buildData.pos[VY];
info.pEX = hedge->v[1]->buildData.pos[VX];
info.pEY = hedge->v[1]->buildData.pos[VY];
info.pDX = info.pEX - info.pSX;
info.pDY = info.pEY - info.pSY;

info.pLength = M_Length(info.pDX, info.pDY);
info.pAngle = M_SlopeToAngle(info.pDX, info.pDY);

info.pPerp = info.pSY * info.pDX - info.pSX * info.pDY;
info.pPara = -info.pSX * info.pDX - info.pSY * info.pDY;

if(info.pLength <= 0)
Con_Error("HEdge {%p} is of zero length.", hedge);
}

static Sector* findFirstSectorInHEdgeList(const BspLeaf* leaf)
{
Q_ASSERT(leaf);
Expand Down
5 changes: 0 additions & 5 deletions doomsday/engine/portable/src/map/bsp/superblockmap.cpp
Expand Up @@ -23,12 +23,7 @@
* 02110-1301 USA</small>
*/

#include "de_platform.h"
#include "de_console.h"
#include "p_mapdata.h"

#include "kdtree.h"

#include "map/bsp/superblockmap.h"

using namespace de::bsp;
Expand Down

0 comments on commit 45ba67b

Please sign in to comment.