Skip to content

Commit

Permalink
BSP Builder|Refactor: Moved logging of migrant HEdges into Partitioner
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Apr 6, 2012
1 parent 1cc013d commit bdac866
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 65 deletions.
21 changes: 20 additions & 1 deletion doomsday/engine/portable/include/map/bsp/partitioner.h
Expand Up @@ -87,7 +87,7 @@ class Partitioner
splitCostFactor(_splitCostFactor),
map(_map),
rootNode(0), partition(0),
unclosedSectors(),
unclosedSectors(), migrantHEdges(),
builtOK(false)
{
initPartitionInfo();
Expand Down Expand Up @@ -337,6 +337,12 @@ class Partitioner
*/
bool registerUnclosedSector(Sector* sector, double x, double y);

bool registerMigrantHEdge(Sector* sector, HEdge* migrant);

public:
void registerMigrantHEdges(const BspLeaf* leaf);

private:
/// HEdge split cost factor.
int splitCostFactor;

Expand Down Expand Up @@ -372,6 +378,19 @@ class Partitioner
typedef std::list<UnclosedSectorRecord> UnclosedSectors;
UnclosedSectors unclosedSectors;

/// Migrant hedges are recorded here so we don't print too many warnings.
struct MigrantHEdgeRecord
{
HEdge* hedge;
Sector* facingSector;

MigrantHEdgeRecord(HEdge* _hedge, Sector* _facingSector)
: hedge(_hedge), facingSector(_facingSector)
{}
};
typedef std::list<MigrantHEdgeRecord> MigrantHEdges;
MigrantHEdges migrantHEdges;

/// @c true = a BSP for the current map has been built successfully.
bool builtOK;
};
Expand Down
3 changes: 0 additions & 3 deletions doomsday/engine/portable/include/mapdata.hs
Expand Up @@ -301,9 +301,6 @@ internal
typedef struct msector_s {
// Sector index. Always valid after loading & pruning.
int index;

// Suppress superfluous mini warnings.
int warnedFacing;
int refCount;
} msector_t;
end
Expand Down
3 changes: 0 additions & 3 deletions doomsday/engine/portable/include/p_maptypes.h
Expand Up @@ -280,9 +280,6 @@ typedef struct plane_s {
typedef struct msector_s {
// Sector index. Always valid after loading & pruning.
int index;

// Suppress superfluous mini warnings.
int warnedFacing;
int refCount;
} msector_t;

Expand Down
133 changes: 75 additions & 58 deletions doomsday/engine/portable/src/map/bsp/nodes.cpp
Expand Up @@ -34,7 +34,6 @@
#include <de/Log>

#include "de_base.h"
#include "de_bsp.h"
#include "de_console.h"
#include "de_play.h"
#include "edit_map.h"
Expand Down Expand Up @@ -239,56 +238,6 @@ static void logUnclosed(const BspLeaf* leaf)
}
}

static Sector* findFirstSectorInHEdgeList(const BspLeaf* leaf)
{
Q_ASSERT(leaf);
HEdge* hedge = leaf->hedge;
do
{
if(hedge->sector)
{
return hedge->sector;
}
} while((hedge = hedge->next) != leaf->hedge);
return NULL; // Nothing??
}

static void logMigrantHEdge(Sector* sector, HEdge* migrant)
{
if(!sector || !migrant) return;

// Prevent an excessive number of warnings per sector.
if(sector->buildData.warnedFacing == migrant->sector->buildData.index) return;
sector->buildData.warnedFacing = migrant->sector->buildData.index;

if(migrant->bspBuildInfo->lineDef)
LOG_INFO("Sector #%d has SideDef facing #%d (line #%d).")
<< sector->buildData.index << migrant->sector->buildData.index
<< migrant->bspBuildInfo->lineDef->buildData.index;
else
LOG_INFO("Sector #%d has SideDef facing #%d.")
<< sector->buildData.index << migrant->sector->buildData.index;
}

static void logMigrantHEdges(const BspLeaf* leaf)
{
if(!leaf) return;

// Find a suitable half-edge for comparison.
Sector* sector = findFirstSectorInHEdgeList(leaf);
if(!sector) return;

// Log migrants.
HEdge* hedge = leaf->hedge;
do
{
if(hedge->sector && hedge->sector != sector)
{
logMigrantHEdge(sector, hedge);
}
} while((hedge = hedge->next) != leaf->hedge);
}

static boolean sanityCheckHasRealhedge(const BspLeaf* leaf)
{
Q_ASSERT(leaf);
Expand Down Expand Up @@ -318,11 +267,16 @@ static void findSideDefHEdges(SideDef* side, HEdge* hedge)
side->hedgeRight = side->hedgeRight->bspBuildInfo->nextOnSide;
}

typedef struct {
Partitioner* partitioner;
HEdgeSortBuffer sortBuffer;
} clockwiseleafparams_t;

static int clockwiseLeaf(BspTreeNode& tree, void* parameters)
{
if(tree.isLeaf())
{
HEdgeSortBuffer& sortBuffer = *static_cast<HEdgeSortBuffer*>(parameters);
clockwiseleafparams_t* p = static_cast<clockwiseleafparams_t*>(parameters);
BspLeaf* leaf = reinterpret_cast<BspLeaf*>(tree.userData());
double midPoint[2] = { 0, 0 };
HEdge* hedge;
Expand All @@ -334,7 +288,7 @@ static int clockwiseLeaf(BspTreeNode& tree, void* parameters)
for(hedge = leaf->hedge; hedge; hedge = hedge->next)
leaf->hedgeCount++;

clockwiseOrder(sortBuffer, &leaf->hedge, leaf->hedgeCount, midPoint[VX], midPoint[VY]);
clockwiseOrder(p->sortBuffer, &leaf->hedge, leaf->hedgeCount, midPoint[VX], midPoint[VY]);

if(leaf->hedge)
{
Expand Down Expand Up @@ -388,12 +342,12 @@ static int clockwiseLeaf(BspTreeNode& tree, void* parameters)

if(!leaf->sector)
{
LOG_DEBUG("BspLeaf %p is orphan.") << leaf;
LOG_WARNING("BspLeaf %p is orphan.") << leaf;
}

if(verbose)
{
logMigrantHEdges(leaf);
p->partitioner->registerMigrantHEdges(leaf);
logUnclosed(leaf);
}

Expand All @@ -408,8 +362,9 @@ static int clockwiseLeaf(BspTreeNode& tree, void* parameters)

void Partitioner::windLeafs()
{
HEdgeSortBuffer sortBuffer;
BspTreeNode::PostOrder(*rootNode, clockwiseLeaf, static_cast<void*>(&sortBuffer));
clockwiseleafparams_t parm;
parm.partitioner = this;
BspTreeNode::PostOrder(*rootNode, clockwiseLeaf, static_cast<void*>(&parm));
}

static void evalPartitionCostForHEdge(const BspHEdgeInfo* partInfo,
Expand Down Expand Up @@ -1707,7 +1662,69 @@ bool Partitioner::registerUnclosedSector(Sector* sector, double x, double y)
// In the absence of a better mechanism, simply log this right away.
/// @todo Implement something better!
LOG_WARNING("Sector %p #%d is unclosed near [%1.1f, %1.1f].")
<< sector << sector->buildData.index - 1 << x << y;
<< sector << sector->buildData.index - 1 << x << y;

return true;
}

bool Partitioner::registerMigrantHEdge(Sector* sector, HEdge* migrant)
{
if(!sector || !migrant) return false;

// Has this pair already been registered?
for(MigrantHEdges::const_iterator it = migrantHEdges.begin();
it != migrantHEdges.end(); ++it)
{
MigrantHEdgeRecord const& record = *it;
if(record.facingSector == sector && record.hedge == migrant)
return false;
}

// Add a new record.
migrantHEdges.push_back(MigrantHEdgeRecord(migrant, sector));

// In the absence of a better mechanism, simply log this right away.
/// @todo Implement something better!
if(migrant->bspBuildInfo->lineDef)
LOG_WARNING("Sector #%d has HEdge facing #%d (line #%d).")
<< sector->buildData.index << migrant->sector->buildData.index
<< migrant->bspBuildInfo->lineDef->buildData.index;
else
LOG_WARNING("Sector #%d has HEdge facing #%d.")
<< sector->buildData.index << migrant->sector->buildData.index;

return true;
}

static Sector* findFirstSectorInHEdgeList(const BspLeaf* leaf)
{
Q_ASSERT(leaf);
HEdge* hedge = leaf->hedge;
do
{
if(hedge->sector)
{
return hedge->sector;
}
} while((hedge = hedge->next) != leaf->hedge);
return NULL; // Nothing??
}

void Partitioner::registerMigrantHEdges(const BspLeaf* leaf)
{
if(!leaf) return;

// Find a suitable half-edge for comparison.
Sector* sector = findFirstSectorInHEdgeList(leaf);
if(!sector) return;

// Log migrants.
HEdge* hedge = leaf->hedge;
do
{
if(hedge->sector && hedge->sector != sector)
{
registerMigrantHEdge(sector, hedge);
}
} while((hedge = hedge->next) != leaf->hedge);
}

0 comments on commit bdac866

Please sign in to comment.