Skip to content

Commit

Permalink
Bsp Builder: Further minor cleanup refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Aug 27, 2012
1 parent a6be3a8 commit 4f013cf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 70 deletions.
23 changes: 7 additions & 16 deletions doomsday/engine/portable/include/map/bsp/hplane.h
Expand Up @@ -94,9 +94,9 @@ class HPlane {
public:
typedef std::list<HPlaneIntercept> Intercepts;

HPlane() : partition(), intercepts(0){}
HPlane() : partition(), intercepts_(0){}
HPlane(coord_t const origin[2], coord_t const direction[2]) :
partition(origin, direction), intercepts(0) {}
partition(origin, direction), intercepts_(0) {}

~HPlane() { clear(); }

Expand Down Expand Up @@ -130,29 +130,20 @@ class HPlane {
*
* @param userData Ownership passes to HPlane.
*/
HPlaneIntercept* newIntercept(coord_t distance, void* userData=NULL);
HPlaneIntercept& newIntercept(coord_t distance, void* userData=NULL);

Intercepts::const_iterator deleteIntercept(Intercepts::iterator at);
Intercepts::const_iterator deleteIntercept(Intercepts::const_iterator at);

inline bool empty() const { return intercepts.empty(); }

inline Intercepts::iterator begin() { return intercepts.begin(); }

inline Intercepts::const_iterator begin() const { return intercepts.begin(); }

inline Intercepts::iterator end() { return intercepts.end(); }

inline Intercepts::const_iterator end() const { return intercepts.end(); }

inline Intercepts::size_type size() const { return intercepts.size(); }
const Intercepts& intercepts() const;

static void DebugPrint(const HPlane& inst);

private:
HPlanePartition partition;

/// The intercept list. Kept sorted by along_dist, in ascending order.
Intercepts intercepts;
/// The intercept list. Kept sorted by distance, in ascending order.
Intercepts intercepts_;
};

} // namespace bsp
Expand Down
14 changes: 6 additions & 8 deletions doomsday/engine/portable/include/map/bsp/superblockmap.h
Expand Up @@ -232,21 +232,19 @@ class SuperBlock
*/
HEdge* pop();

HEdges::const_iterator hedgesBegin() const;
HEdges::const_iterator hedgesEnd() const;
const HEdges& hedges() const;

DENG_DEBUG_ONLY(
static void DebugPrint(SuperBlock const& inst)
{
for(SuperBlock::HEdges::const_iterator it = inst.hedgesBegin();
it != inst.hedgesEnd(); ++it)
DENG2_FOR_EACH(it, inst.hedges(), SuperBlock::HEdges::const_iterator)
{
HEdge* hedge = *it;
LOG_DEBUG("Build: %s %p sector: %d [%1.1f, %1.1f] -> [%1.1f, %1.1f]")
<< (hedge->lineDef? "NORM" : "MINI")
<< hedge << hedge->sector->buildData.index
<< hedge->v[0]->origin[VX] << hedge->v[0]->origin[VY]
<< hedge->v[1]->origin[VX] << hedge->v[1]->origin[VY];
<< (hedge->lineDef? "NORM" : "MINI")
<< hedge << hedge->sector->buildData.index
<< hedge->v[0]->origin[VX] << hedge->v[0]->origin[VY]
<< hedge->v[1]->origin[VX] << hedge->v[1]->origin[VY];
}
})

Expand Down
31 changes: 19 additions & 12 deletions doomsday/engine/portable/src/map/bsp/hplane.cpp
Expand Up @@ -35,7 +35,7 @@ using namespace de::bsp;

void HPlane::clear()
{
intercepts.clear();
intercepts_.clear();
}

HPlane* HPlane::setOrigin(coord_t const newOrigin[2])
Expand Down Expand Up @@ -99,33 +99,40 @@ HPlane* HPlane::setDY(coord_t newDY)
return this;
}

HPlaneIntercept* HPlane::newIntercept(coord_t distance, void* userData)
HPlaneIntercept& HPlane::newIntercept(coord_t distance, void* userData)
{
Intercepts::reverse_iterator after;
HPlaneIntercept* inter;

for(after = intercepts.rbegin();
after != intercepts.rend() && distance < (*after).distance(); after++)
for(after = intercepts_.rbegin();
after != intercepts_.rend() && distance < (*after).distance(); after++)
{}

inter = &*intercepts.insert(after.base(), HPlaneIntercept(distance, userData));
return inter;
return *intercepts_.insert(after.base(), HPlaneIntercept(distance, userData));
}

HPlane::Intercepts::const_iterator HPlane::deleteIntercept(Intercepts::iterator at)
{
//if(at < intercepts.begin() || at >= intercepts.end()) return at;
return intercepts.erase(at);
return intercepts_.erase(at);
}

HPlane::Intercepts::const_iterator HPlane::deleteIntercept(Intercepts::const_iterator at)
{
return intercepts_.erase(at);
}

const HPlane::Intercepts& HPlane::intercepts() const
{
return intercepts_;
}

#if _DEBUG
void HPlane::DebugPrint(const HPlane& inst)
{
uint n = 0;
for(HPlane::Intercepts::const_iterator it = inst.begin(); it != inst.end(); it++, n++)
uint index = 0;
DENG2_FOR_EACH(it, inst.intercepts(), HPlane::Intercepts::const_iterator)
{
const HPlaneIntercept* inter = &*it;
Con_Printf(" %u: >%1.2f ", n, inter->distance());
Con_Printf(" %u: >%1.2f ", index++, inter->distance());
HEdgeIntercept::DebugPrint(*static_cast<HEdgeIntercept*>(inter->userData()));
}
}
Expand Down
36 changes: 19 additions & 17 deletions doomsday/engine/portable/src/map/bsp/partitioner.cpp
Expand Up @@ -452,7 +452,7 @@ struct Partitioner::Instance
LineDef* line = hedge->lineDef;
HEdgeIntercept* intercept = newHEdgeIntercept(vertex, line && lineDefInfo(*line).flags.testFlag(LineDefInfo::SELFREF));

return partition->newIntercept(vertexDistanceFromPartition(vertex), intercept);
return &partition->newIntercept(vertexDistanceFromPartition(vertex), intercept);
}

/**
Expand Down Expand Up @@ -495,17 +495,19 @@ struct Partitioner::Instance

void mergeIntersections()
{
HPlane::Intercepts::iterator node = partition->begin();
while(node != partition->end())
HPlane::Intercepts::const_iterator node = partition->intercepts().begin();
while(node != partition->intercepts().end())
{
HPlane::Intercepts::iterator np = node; np++;
if(np == partition->end()) break;
HPlane::Intercepts::const_iterator np = node; np++;
if(np == partition->intercepts().end()) break;

coord_t len = *np - *node;
if(len < -0.1)
{
throw de::Error("Partitioner::MergeIntersections",
QString("Invalid intercept order - %1 > %2").arg(node->distance(), 0, 'f', 3).arg(np->distance(), 0, 'f', 3));
QString("Invalid intercept order - %1 > %2")
.arg(node->distance(), 0, 'f', 3)
.arg( np->distance(), 0, 'f', 3));
}
else if(len > 0.2)
{
Expand All @@ -532,11 +534,11 @@ struct Partitioner::Instance

void buildHEdgesAtIntersectionGaps(SuperBlock& rightList, SuperBlock& leftList)
{
HPlane::Intercepts::const_iterator node = partition->begin();
while(node != partition->end())
HPlane::Intercepts::const_iterator node = partition->intercepts().begin();
while(node != partition->intercepts().end())
{
HPlane::Intercepts::const_iterator np = node; np++;
if(np == partition->end()) break;
if(np == partition->intercepts().end()) break;

HEdgeIntercept* cur = reinterpret_cast<HEdgeIntercept*>((*node).userData());
HEdgeIntercept* next = reinterpret_cast<HEdgeIntercept*>((*np).userData());
Expand Down Expand Up @@ -1019,8 +1021,7 @@ struct Partitioner::Instance
}

// Check partition against all half-edges.
for(SuperBlock::HEdges::const_iterator it = block.hedgesBegin();
it != block.hedgesEnd(); ++it)
DENG2_FOR_EACH(it, block.hedges(), SuperBlock::HEdges::const_iterator)
{
// Do we already have a better choice?
if(best && !(cost < bestCost)) return false; // Stop iteration.
Expand Down Expand Up @@ -1112,8 +1113,7 @@ struct Partitioner::Instance
DENG_ASSERT(best);

// Test each half-edge as a potential partition.
for(SuperBlock::HEdges::const_iterator it = partList.hedgesBegin();
it != partList.hedgesEnd(); ++it)
DENG2_FOR_EACH(it, partList.hedges(), SuperBlock::HEdges::const_iterator)
{
HEdge* hedge = *it;

Expand Down Expand Up @@ -1444,7 +1444,7 @@ struct Partitioner::Instance

void clearPartitionIntercepts()
{
for(HPlane::Intercepts::iterator it = partition->begin(); it != partition->end(); ++it)
DENG2_FOR_EACH(it, partition->intercepts(), HPlane::Intercepts::const_iterator)
{
HEdgeIntercept* intercept = static_cast<HEdgeIntercept*>((*it).userData());
deleteHEdgeIntercept(*intercept);
Expand Down Expand Up @@ -1753,7 +1753,8 @@ struct Partitioner::Instance
mergeIntersections();

LOG_TRACE("Building HEdges along partition [%1.1f, %1.1f] > [%1.1f, %1.1f]")
<< partitionInfo.start[VX] << partitionInfo.start[VY] << partitionInfo.direction[VX] << partitionInfo.direction[VY];
<< partitionInfo.start[VX] << partitionInfo.start[VY]
<< partitionInfo.direction[VX] << partitionInfo.direction[VY];

// Find connections in the intersections.
buildHEdgesAtIntersectionGaps(rightList, leftList);
Expand All @@ -1771,10 +1772,11 @@ struct Partitioner::Instance
{
if(!vertex) return NULL; // Hmm...

for(HPlane::Intercepts::const_iterator it = partition->begin(); it != partition->end(); ++it)
DENG2_FOR_EACH(it, partition->intercepts(), HPlane::Intercepts::const_iterator)
{
const HPlaneIntercept* inter = &*it;
if(reinterpret_cast<HEdgeIntercept*>(inter->userData())->vertex == vertex) return inter;
HEdgeIntercept* hedgeInter = reinterpret_cast<HEdgeIntercept*>(inter->userData());
if(hedgeInter->vertex == vertex) return inter;
}

return NULL;
Expand Down
28 changes: 11 additions & 17 deletions doomsday/engine/portable/src/map/bsp/superblockmap.cpp
Expand Up @@ -44,27 +44,27 @@ struct SuperBlock::Instance
int realNum;
int miniNum;

Instance(SuperBlockmap& blockmap) :
bmap(blockmap), tree(0), hedges(0), realNum(0), miniNum(0)
Instance(SuperBlockmap& blockmap)
: bmap(blockmap), tree(0), hedges(0), realNum(0), miniNum(0)
{}

~Instance()
{
KdTreeNode_SetUserData(tree, NULL);
}

void inline linkHEdge(HEdge& hedge)
inline void linkHEdge(HEdge& hedge)
{
hedges.push_front(&hedge);
}

void inline incrementHEdgeCount(HEdge const& hedge)
inline void incrementHEdgeCount(HEdge const& hedge)
{
if(hedge.lineDef) realNum++;
else miniNum++;
}

void inline decrementHEdgeCount(HEdge const& hedge)
inline void decrementHEdgeCount(HEdge const& hedge)
{
if(hedge.lineDef) realNum--;
else miniNum--;
Expand Down Expand Up @@ -136,14 +136,9 @@ SuperBlock* SuperBlock::addChild(ChildId childId, bool splitVertical)
return child;
}

SuperBlock::HEdges::const_iterator SuperBlock::hedgesBegin() const
const SuperBlock::HEdges& SuperBlock::hedges() const
{
return d->hedges.begin();
}

SuperBlock::HEdges::const_iterator SuperBlock::hedgesEnd() const
{
return d->hedges.end();
return d->hedges;
}

uint SuperBlock::hedgeCount(bool addReal, bool addMini) const
Expand Down Expand Up @@ -171,7 +166,7 @@ void SuperBlock::findHEdgeBounds(AABoxd& bounds)
bool initialized = false;
AABoxd hedgeAABox;

for(HEdges::iterator it = d->hedges.begin(); it != d->hedges.end(); ++it)
DENG2_FOR_EACH(it, d->hedges, HEdges::iterator)
{
HEdge* hedge = *it;
initAABoxFromHEdgeVertexes(&hedgeAABox, hedge);
Expand All @@ -191,9 +186,9 @@ void SuperBlock::findHEdgeBounds(AABoxd& bounds)
SuperBlock& SuperBlock::push(HEdge& hedge)
{
SuperBlock* sb = this;
for(;;)
forever
{
Q_ASSERT(sb);
DENG2_ASSERT(sb);

// Update half-edge counts.
sb->d->incrementHEdgeCount(hedge);
Expand Down Expand Up @@ -241,7 +236,6 @@ SuperBlock& SuperBlock::push(HEdge& hedge)

sb = sb->child(p1);
}

return *sb;
}

Expand Down Expand Up @@ -344,7 +338,7 @@ void SuperBlockmap::clear()

static void findHEdgeBoundsWorker(SuperBlock& block, AABoxd& bounds, bool* initialized)
{
Q_ASSERT(initialized);
DENG2_ASSERT(initialized);
if(block.hedgeCount(true, true))
{
AABoxd blockHEdgeAABox;
Expand Down

0 comments on commit 4f013cf

Please sign in to comment.