Skip to content

Commit

Permalink
BSP Builder|Refactor: Removed redundant mhedge_t abstraction
Browse files Browse the repository at this point in the history
Each HEdge now stores an intstance of BspHEdgeInfo for use during
a BSP node build.
  • Loading branch information
danij-deng committed Mar 31, 2012
1 parent 7e1c506 commit b4a23c9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 70 deletions.
28 changes: 24 additions & 4 deletions doomsday/engine/portable/include/mapdata.hs
Expand Up @@ -78,16 +78,36 @@ internal
// HEdge frame flags
#define HEDGEINF_FACINGFRONT 0x0001

typedef struct mhedge_s {
/**
* BspHEdgeInfo. Plain old data structure storing additional information about
* a half-edge produced by BspBuilder.
*/
typedef struct bsphedgeinfo_s {
// Precomputed data for faster calculations.
double pSX, pSY;
double pEX, pEY;
double pDX, pDY;

double pLength;
double pAngle;
double pPara;
double pPerp;

struct hedge_s* nextOnSide;
struct hedge_s* prevOnSide;

// 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;

BspHEdgeInfo info;
} mhedge_t;
// Linedef that this half-edge goes along, or NULL if miniseg.
struct linedef_s* lineDef;

// Linedef that this half-edge initially comes from.
// For "real" half-edges, this is just the same as the 'linedef' field
// above. For "miniedges", this is the linedef of the partition line.
struct linedef_s* sourceLineDef;
} BspHEdgeInfo;
end

public
Expand All @@ -114,7 +134,7 @@ struct HEdge
FLOAT float offset
- biassurface_t*[3] bsuf // 0=middle, 1=top, 2=bottom
- short frameFlags
- mhedge_t buildData
- BspHEdgeInfo buildData
end

internal
Expand Down
20 changes: 8 additions & 12 deletions doomsday/engine/portable/include/p_maptypes.h
Expand Up @@ -91,6 +91,13 @@ typedef struct bsphedgeinfo_s {
double pPara;
double pPerp;

struct hedge_s* nextOnSide;
struct hedge_s* prevOnSide;

// 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;

// Linedef that this half-edge goes along, or NULL if miniseg.
struct linedef_s* lineDef;

Expand All @@ -100,17 +107,6 @@ typedef struct bsphedgeinfo_s {
struct linedef_s* sourceLineDef;
} BspHEdgeInfo;

typedef struct mhedge_s {
struct hedge_s* nextOnSide;
struct hedge_s* prevOnSide;

// 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;

BspHEdgeInfo info;
} mhedge_t;

typedef struct hedge_s {
runtime_mapdata_header_t header;
struct vertex_s* v[2]; // [Start, End] of the segment.
Expand All @@ -132,7 +128,7 @@ typedef struct hedge_s {
float offset;
biassurface_t* bsuf[3]; // 0=middle, 1=top, 2=bottom
short frameFlags;
mhedge_t buildData;
BspHEdgeInfo buildData;
} HEdge;

#define BLF_MIDPOINT 0x80 // Midpoint is tri-fan centre.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/bspbuilder/bspbuilder.cpp
Expand Up @@ -236,7 +236,7 @@ SuperBlockmap* BspBuilder::createInitialHEdges(GameMap* map)
{
HEdge* other;

other = newHEdge(front->buildData.info.lineDef, line, line->v[1], line->v[0],
other = newHEdge(front->buildData.lineDef, line, line->v[1], line->v[0],
line->buildData.windowEffect, true);

sbmap->root()->hedgePush(other);
Expand Down
32 changes: 12 additions & 20 deletions doomsday/engine/portable/src/bspbuilder/hedges.cpp
Expand Up @@ -54,7 +54,7 @@ void Bsp_PrintHEdgeIntercept(HEdgeIntercept* inter)
}
#endif

static void initBspHEdgeInfo(const HEdge* hedge, BspHEdgeInfo* info)
static void updateBspHEdgeInfo(const HEdge* hedge, BspHEdgeInfo* info)
{
assert(hedge);
if(!info) return;
Expand All @@ -71,14 +71,7 @@ static void initBspHEdgeInfo(const HEdge* hedge, BspHEdgeInfo* info)

info->pPerp = info->pSY * info->pDX - info->pSX * info->pDY;
info->pPara = -info->pSX * info->pDX - info->pSY * info->pDY;
}

static void updateBspHEdgeInfo(const HEdge* hedge, BspHEdgeInfo* info)
{
assert(hedge);
if(!info) return;

initBspHEdgeInfo(hedge, info);
if(info->pLength <= 0)
Con_Error("HEdge {%p} is of zero length.", hedge);
}
Expand All @@ -90,14 +83,13 @@ HEdge* BspBuilder::newHEdge(LineDef* lineDef, LineDef* sourceLineDef,

hedge->v[0] = start;
hedge->v[1] = end;
hedge->side = (back? 1 : 0);
hedge->twin = NULL;
hedge->sector = sec;
hedge->buildData.nextOnSide = hedge->buildData.prevOnSide = NULL;
hedge->side = (back? 1 : 0);

updateBspHEdgeInfo(hedge, &hedge->buildData.info);
hedge->buildData.info.lineDef = lineDef;
hedge->buildData.info.sourceLineDef = sourceLineDef;
updateBspHEdgeInfo(hedge, &hedge->buildData);
hedge->buildData.lineDef = lineDef;
hedge->buildData.sourceLineDef = sourceLineDef;
hedge->buildData.nextOnSide = hedge->buildData.prevOnSide = NULL;

return hedge;
}
Expand All @@ -124,8 +116,8 @@ HEdge* BspBuilder::splitHEdge(HEdge* oldHEdge, double x, double y)
newVert->buildData.refCount = (oldHEdge->twin? 4 : 2);

// Compute wall_tip info.
addEdgeTip(newVert, -oldHEdge->buildData.info.pDX, -oldHEdge->buildData.info.pDY, oldHEdge, oldHEdge->twin);
addEdgeTip(newVert, oldHEdge->buildData.info.pDX, oldHEdge->buildData.info.pDY, oldHEdge->twin, oldHEdge);
addEdgeTip(newVert, -oldHEdge->buildData.pDX, -oldHEdge->buildData.pDY, oldHEdge, oldHEdge->twin);
addEdgeTip(newVert, oldHEdge->buildData.pDX, oldHEdge->buildData.pDY, oldHEdge->twin, oldHEdge);

// Copy the old half-edge info.
newHEdge = HEdge_NewCopy(oldHEdge);
Expand All @@ -134,10 +126,10 @@ HEdge* BspBuilder::splitHEdge(HEdge* oldHEdge, double x, double y)
oldHEdge->buildData.nextOnSide = newHEdge;

oldHEdge->v[1] = newVert;
updateBspHEdgeInfo(oldHEdge, &oldHEdge->buildData.info);
updateBspHEdgeInfo(oldHEdge, &oldHEdge->buildData);

newHEdge->v[0] = newVert;
updateBspHEdgeInfo(newHEdge, &newHEdge->buildData.info);
updateBspHEdgeInfo(newHEdge, &newHEdge->buildData);

//DEBUG_Message(("Splitting Vertex is %04X at (%1.1f,%1.1f)\n",
// newVert->index, newVert->V_pos[VX], newVert->V_pos[VY]));
Expand All @@ -157,10 +149,10 @@ HEdge* BspBuilder::splitHEdge(HEdge* oldHEdge, double x, double y)
oldHEdge->twin->buildData.prevOnSide = newHEdge->twin;

oldHEdge->twin->v[0] = newVert;
updateBspHEdgeInfo(oldHEdge->twin, &oldHEdge->twin->buildData.info);
updateBspHEdgeInfo(oldHEdge->twin, &oldHEdge->twin->buildData);

newHEdge->twin->v[1] = newVert;
updateBspHEdgeInfo(newHEdge->twin, &newHEdge->twin->buildData.info);
updateBspHEdgeInfo(newHEdge->twin, &newHEdge->twin->buildData);

// Update superblock, if needed.
if(oldHEdge->twin->buildData.block)
Expand Down
54 changes: 27 additions & 27 deletions doomsday/engine/portable/src/bspbuilder/node.cpp
Expand Up @@ -88,8 +88,8 @@ void BSP_PrintSuperBlockhedges(SuperBlock* superblock);

static __inline int pointOnhedgeSide(double x, double y, const HEdge* part)
{
return P_PointOnLinedefSide2(x, y, part->buildData.info.pDX, part->buildData.info.pDY,
part->buildData.info.pPerp, part->buildData.info.pLength,
return P_PointOnLinedefSide2(x, y, part->buildData.pDX, part->buildData.pDY,
part->buildData.pPerp, part->buildData.pLength,
DIST_EPSILON);
}

Expand Down Expand Up @@ -282,10 +282,10 @@ static void sanityCheckSameSector(const BspLeaf* leaf)

if(verbose >= 1)
{
if(hedge->buildData.info.lineDef)
if(hedge->buildData.lineDef)
Con_Message("Sector #%d has sidedef facing #%d (line #%d).\n",
compare->sector->buildData.index, hedge->sector->buildData.index,
hedge->buildData.info.lineDef->buildData.index);
hedge->buildData.lineDef->buildData.index);
else
Con_Message("Sector #%d has sidedef facing #%d.\n",
compare->sector->buildData.index, hedge->sector->buildData.index);
Expand All @@ -298,7 +298,7 @@ static boolean sanityCheckHasRealhedge(const BspLeaf* leaf)
HEdge* hedge = leaf->hedge;
do
{
if(hedge->buildData.info.lineDef) return true;
if(hedge->buildData.lineDef) return true;
} while((hedge = hedge->next) != leaf->hedge);
return false;
}
Expand Down Expand Up @@ -349,10 +349,10 @@ static int C_DECL clockwiseLeaf(BinaryTree* tree, void* /*parameters*/)
hedge = leaf->hedge;
do
{
if(hedge->buildData.info.lineDef &&
hedge->buildData.info.lineDef->sideDefs[hedge->side])
if(hedge->buildData.lineDef &&
hedge->buildData.lineDef->sideDefs[hedge->side])
{
SideDef* side = hedge->buildData.info.lineDef->sideDefs[hedge->side];
SideDef* side = hedge->buildData.lineDef->sideDefs[hedge->side];
leaf->sector = side->sector;
}
} while(!leaf->sector && (hedge = hedge->next) != leaf->hedge);
Expand Down Expand Up @@ -396,27 +396,27 @@ static void evalPartitionCostForHEdge(const BspHEdgeInfo* partInfo,
int costFactorMultiplier, const HEdge* hedge, PartitionCost& cost)
{
#define ADD_LEFT() \
if (hedge->buildData.info.lineDef) cost.realLeft += 1; \
else cost.miniLeft += 1; \
if (hedge->buildData.lineDef) cost.realLeft += 1; \
else cost.miniLeft += 1; \

#define ADD_RIGHT() \
if (hedge->buildData.info.lineDef) cost.realRight += 1; \
else cost.miniRight += 1; \
if (hedge->buildData.lineDef) cost.realRight += 1; \
else cost.miniRight += 1; \

double qnty, a, b, fa, fb;
assert(hedge);

// Get state of lines' relation to each other.
if(hedge->buildData.info.sourceLineDef == partInfo->sourceLineDef)
if(hedge->buildData.sourceLineDef == partInfo->sourceLineDef)
{
a = b = fa = fb = 0;
}
else
{
a = M_PerpDist(partInfo->pDX, partInfo->pDY, partInfo->pPerp, partInfo->pLength,
hedge->buildData.info.pSX, hedge->buildData.info.pSY);
hedge->buildData.pSX, hedge->buildData.pSY);
b = M_PerpDist(partInfo->pDX, partInfo->pDY, partInfo->pPerp, partInfo->pLength,
hedge->buildData.info.pEX, hedge->buildData.info.pEY);
hedge->buildData.pEX, hedge->buildData.pEY);

fa = fabs(a);
fb = fabs(b);
Expand All @@ -427,7 +427,7 @@ static void evalPartitionCostForHEdge(const BspHEdgeInfo* partInfo,
{
// This half-edge runs along the same line as the partition.
// hedge whether it goes in the same direction or the opposite.
if(hedge->buildData.info.pDX * partInfo->pDX + hedge->buildData.info.pDY * partInfo->pDY < 0)
if(hedge->buildData.pDX * partInfo->pDX + hedge->buildData.pDY * partInfo->pDY < 0)
{
ADD_LEFT();
}
Expand Down Expand Up @@ -658,7 +658,7 @@ static int chooseHEdgeFromSuperBlock(SuperBlock* partList, void* parameters)
// hedge->v[1]->V_pos[VX], hedge->v[1]->V_pos[VY]));

// "Mini-hedges" are never potential candidates.
LineDef* lineDef = hedge->buildData.info.lineDef;
LineDef* lineDef = hedge->buildData.lineDef;
if(!lineDef) continue;

// Only test half-edges from the same linedef once per round of
Expand All @@ -667,7 +667,7 @@ static int chooseHEdgeFromSuperBlock(SuperBlock* partList, void* parameters)
lineDef->validCount = validCount;

// Unsuitable or too costly?
int cost = evalPartition(*p->hedgeList, p->splitCostFactor, &hedge->buildData.info, p->bestCost);
int cost = evalPartition(*p->hedgeList, p->splitCostFactor, &hedge->buildData, p->bestCost);
if(cost >= 0 && cost < p->bestCost)
{
// We have a new better choice.
Expand Down Expand Up @@ -702,7 +702,7 @@ boolean BspBuilder::choosePartition(SuperBlock* hedgeList, size_t /*depth*/, HPl
HEdge* best = parm.best;
if(best)
{
LineDef* lineDef = best->buildData.info.lineDef;
LineDef* lineDef = best->buildData.lineDef;

// This must not be a "mini hedge".
assert(lineDef);
Expand All @@ -719,7 +719,7 @@ boolean BspBuilder::choosePartition(SuperBlock* hedgeList, size_t /*depth*/, HPl
lineDef->L_v(best->side^1)->buildData.pos[VY] - lineDef->L_v(best->side)->buildData.pos[VY]);

BspHEdgeInfo* info = partition->partitionHEdgeInfo();
memcpy(info, &best->buildData.info, sizeof(*info));
memcpy(info, &best->buildData, sizeof(*info));

return true;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ const HPlaneIntercept* BspBuilder::makeHPlaneIntersection(HPlane* hplane, HEdge*
distance = M_ParallelDist(info->pDX, info->pDY, info->pPara, info->pLength,
vertex->buildData.pos[VX], vertex->buildData.pos[VY]);

hedgeIntercept = newHEdgeIntercept(vertex, info, (hedge->buildData.info.lineDef && lineDefHasSelfRef(hedge->buildData.info.lineDef)));
hedgeIntercept = newHEdgeIntercept(vertex, info, (hedge->buildData.lineDef && lineDefHasSelfRef(hedge->buildData.lineDef)));
return hplane->newIntercept(distance, hedgeIntercept);
}

Expand Down Expand Up @@ -816,10 +816,10 @@ void BspBuilder::divideHEdge(HEdge* hedge, HPlane* partition, SuperBlock* rightL
double a, b;

// Get state of lines' relation to each other.
a = M_PerpDist(info->pDX, info->pDY, info->pPerp, info->pLength, hedge->buildData.info.pSX, hedge->buildData.info.pSY);
b = M_PerpDist(info->pDX, info->pDY, info->pPerp, info->pLength, hedge->buildData.info.pEX, hedge->buildData.info.pEY);
a = M_PerpDist(info->pDX, info->pDY, info->pPerp, info->pLength, hedge->buildData.pSX, hedge->buildData.pSY);
b = M_PerpDist(info->pDX, info->pDY, info->pPerp, info->pLength, hedge->buildData.pEX, hedge->buildData.pEY);

if(hedge->buildData.info.sourceLineDef == info->sourceLineDef)
if(hedge->buildData.sourceLineDef == info->sourceLineDef)
a = b = 0;

// Check for being on the same line.
Expand All @@ -830,7 +830,7 @@ void BspBuilder::divideHEdge(HEdge* hedge, HPlane* partition, SuperBlock* rightL

// This hedge runs along the same line as the partition. Check whether it goes in
// the same direction or the opposite.
if(hedge->buildData.info.pDX * info->pDX + hedge->buildData.info.pDY * info->pDY < 0)
if(hedge->buildData.pDX * info->pDX + hedge->buildData.pDY * info->pDY < 0)
{
leftList->hedgePush(hedge);
}
Expand Down Expand Up @@ -868,7 +868,7 @@ void BspBuilder::divideHEdge(HEdge* hedge, HPlane* partition, SuperBlock* rightL
// When we reach here, we have a and b non-zero and opposite sign, hence this edge
// will be split by the partition line.

calcIntersection(&hedge->buildData.info, info, a, b, &x, &y);
calcIntersection(&hedge->buildData, info, a, b, &x, &y);
newhedge = splitHEdge(hedge, x, y);
makeIntersection(partition, hedge, LEFT);

Expand Down Expand Up @@ -1023,7 +1023,7 @@ boolean BspBuilder::buildNodes(SuperBlock* superblock, BinaryTree** parent, size
static void printHEdge(HEdge* hedge)
{
Con_Message("Build: %s %p sector=%d (%1.1f,%1.1f) -> (%1.1f,%1.1f)\n",
(hedge->buildData.info.lineDef? "NORM" : "MINI"), hedge,
(hedge->buildData.lineDef? "NORM" : "MINI"), hedge,
hedge->sector->buildData.index,
hedge->v[0]->buildData.pos[VX], hedge->v[0]->buildData.pos[VY],
hedge->v[1]->buildData.pos[VX], hedge->v[1]->buildData.pos[VY]);
Expand Down
8 changes: 4 additions & 4 deletions doomsday/engine/portable/src/bspbuilder/superblockmap.cpp
Expand Up @@ -67,15 +67,15 @@ struct SuperBlock::Instance
void incrementHEdgeCount(HEdge* hedge)
{
if(!hedge) return;
if(hedge->buildData.info.lineDef) realNum++;
else miniNum++;
if(hedge->buildData.lineDef) realNum++;
else miniNum++;
}

void decrementHEdgeCount(HEdge* hedge)
{
if(!hedge) return;
if(hedge->buildData.info.lineDef) realNum--;
else miniNum--;
if(hedge->buildData.lineDef) realNum--;
else miniNum--;
}
};

Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/edit_bsp.c
Expand Up @@ -133,8 +133,8 @@ static void finishHEdges(GameMap* map)
{
HEdge* hedge = map->hedges[i];

if(hedge->buildData.info.lineDef)
hedge->lineDef = &map->lineDefs[hedge->buildData.info.lineDef->buildData.index - 1];
if(hedge->buildData.lineDef)
hedge->lineDef = &map->lineDefs[hedge->buildData.lineDef->buildData.index - 1];

if(hedge->lineDef)
{
Expand Down

0 comments on commit b4a23c9

Please sign in to comment.