Skip to content

Commit

Permalink
BSP Builder|Refactor: Partitioner now allocates additional Vertexes f…
Browse files Browse the repository at this point in the history
…rom its own store

Partitioner has owership of these additional vertexes and will delete
them automatically when Partitioner itself is destroyed.

Todo for later: Allow the caller to acquire ownership of the vertexes.
  • Loading branch information
danij-deng committed Apr 6, 2012
1 parent b17aa24 commit 23c8fe5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 23 deletions.
12 changes: 10 additions & 2 deletions doomsday/engine/portable/include/map/bsp/partitioner.h
Expand Up @@ -127,6 +127,10 @@ class Partitioner

uint numLeafs();

uint numVertexes();

Vertex const& vertex(uint idx);

private:
void initForMap();

Expand Down Expand Up @@ -393,14 +397,18 @@ class Partitioner
uint* numEditableVertexes;
Vertex*** editableVertexes;

/// Extra info about LineDefs in the current map.
/// Extended info about LineDefs in the current map.
typedef std::vector<LineDefInfo> LineDefInfos;
LineDefInfos lineDefInfos;

/// Extra info about Vertexes in the current map.
/// Extended info about Vertexes in the current map (including extras).
typedef std::vector<VertexInfo> VertexInfos;
VertexInfos vertexInfos;

/// Extra vertexes allocated for the current map.
typedef std::vector<Vertex*> Vertexes;
Vertexes vertexes;

/// Root node of our internal binary tree around which the final BSP data
/// objects are constructed.
BspTreeNode* rootNode;
Expand Down
4 changes: 4 additions & 0 deletions doomsday/engine/portable/include/map/bspbuilder.h
Expand Up @@ -83,6 +83,10 @@ class BspBuilder

uint numLeafs();

uint numVertexes();

Vertex const& vertex(uint idx);

private:
bsp::Partitioner* partitioner;
};
Expand Down
56 changes: 40 additions & 16 deletions doomsday/engine/portable/src/edit_bsp.cpp
Expand Up @@ -240,22 +240,46 @@ static void hardenBSP(BspBuilder& builder, GameMap* dest)
BspTreeNode::PostOrder(*rootNode, populateBspObjectLuts, &p);
}

static void hardenVertexes(GameMap* dest, Vertex*** vertexes, uint* numVertexes)
static void copyVertex(Vertex& vtx, Vertex const& other)
{
dest->numVertexes = *numVertexes;
dest->vertexes = static_cast<Vertex*>(Z_Calloc(dest->numVertexes * sizeof(Vertex), PU_MAPSTATIC, 0));
vtx.numLineOwners = other.numLineOwners;
vtx.lineOwners = other.lineOwners;

for(uint i = 0; i < dest->numVertexes; ++i)
vtx.buildData.index = other.buildData.index;
vtx.buildData.refCount = other.buildData.refCount;
vtx.buildData.equiv = other.buildData.equiv;
V2d_Copy(vtx.buildData.pos, other.buildData.pos);

// Apply the final coordinates.
vtx.V_pos[VX] = float(vtx.buildData.pos[VX]);
vtx.V_pos[VY] = float(vtx.buildData.pos[VY]);
}

static void hardenVertexes(BspBuilder& builder, GameMap* map,
uint* numEditableVertexes, Vertex*** editableVertexes)
{
uint bspVertexCount = builder.numVertexes();

map->numVertexes = *numEditableVertexes + bspVertexCount;
map->vertexes = static_cast<Vertex*>(Z_Calloc(map->numVertexes * sizeof(Vertex), PU_MAPSTATIC, 0));

uint n = 0;
for(; n < *numEditableVertexes; ++n)
{
Vertex* destV = &dest->vertexes[i];
Vertex* srcV = (*vertexes)[i];
Vertex& dest = map->vertexes[n];
Vertex const& src = *((*editableVertexes)[n]);

destV->header.type = DMU_VERTEX;
destV->numLineOwners = srcV->numLineOwners;
destV->lineOwners = srcV->lineOwners;
dest.header.type = DMU_VERTEX;
copyVertex(dest, src);
}

destV->V_pos[VX] = float(srcV->buildData.pos[VX]);
destV->V_pos[VY] = float(srcV->buildData.pos[VY]);
for(uint i = 0; i < bspVertexCount; ++i, ++n)
{
Vertex& dest = map->vertexes[n];
Vertex const& src = builder.vertex(i);

dest.header.type = DMU_VERTEX;
copyVertex(dest, src);
}
}

Expand All @@ -278,19 +302,19 @@ static void updateVertexLinks(GameMap* map)
}
}

void MPE_SaveBsp(BspBuilder_c* builder_c, GameMap* map, uint* numVertexes, Vertex*** vertexes)
void MPE_SaveBsp(BspBuilder_c* builder_c, GameMap* map, uint* numEditableVertexes, Vertex*** editableVertexes)
{
Q_ASSERT(builder_c);
BspBuilder* builder = builder_c->inst;
BspBuilder& builder = *builder_c->inst;

BspTreeNode* rootNode = builder->root();
BspTreeNode* rootNode = builder.root();

buildHEdgeLut(map, rootNode);
hardenVertexes(map, vertexes, numVertexes);
hardenVertexes(builder, map, numEditableVertexes, editableVertexes);
updateVertexLinks(map);

finishHEdges(map);
hardenBSP(*builder, map);
hardenBSP(builder, map);

long rHeight = 0, lHeight = 0;
if(rootNode && !rootNode->isLeaf())
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/edit_map.c
Expand Up @@ -55,7 +55,7 @@ static usecrecord_t *unclosedSectors;

static Vertex *rootVtx; // Used when sorting vertex line owners.

Vertex* createVertex(void)
static Vertex* createVertex(void)
{
Vertex* vtx;

Expand Down
33 changes: 29 additions & 4 deletions doomsday/engine/portable/src/map/bsp/partitioner.cpp
Expand Up @@ -36,7 +36,6 @@
#include "de_base.h"
#include "de_console.h"
#include "de_play.h"
#include "edit_map.h"
#include "p_mapdata.h"
#include "m_misc.h"

Expand Down Expand Up @@ -122,6 +121,16 @@ Partitioner::~Partitioner()
deleteHEdgeTips((*editableVertexes)[i]);
}

for(Vertexes::iterator it = vertexes.begin(); it != vertexes.end(); ++it)
{
Vertex* vtx = *it;
// Has ownership of this vertex been claimed?
if(!vtx) continue;

deleteHEdgeTips(vtx);
free(vtx);
}

// We are finished with the BSP data.
if(rootNode)
{
Expand Down Expand Up @@ -1587,11 +1596,27 @@ void Partitioner::addMiniHEdges(SuperBlock& rightList, SuperBlock& leftList)
buildHEdgesAtIntersectionGaps(rightList, leftList);
}

uint Partitioner::numVertexes()
{
return vertexes.size();
}

Vertex const& Partitioner::vertex(uint idx)
{
Q_ASSERT(idx < vertexes.size());
return *vertexes[idx];
}

Vertex* Partitioner::newVertex(const_pvec2d_t point)
{
/// @todo Vertex should not come from the editable map but from a store
/// within our own domain.
Vertex* vtx = createVertex();
Vertex* vtx;

// Allocate with calloc for uniformity with the editable vertexes.
vtx = static_cast<Vertex*>(calloc(1, sizeof *vtx));
vtx->header.type = DMU_VERTEX;
vtx->buildData.index = *numEditableVertexes + uint(vertexes.size() + 1); // 1-based index, 0 = NIL.
vertexes.push_back(vtx);

vertexInfos.push_back(VertexInfo());
if(point)
{
Expand Down
10 changes: 10 additions & 0 deletions doomsday/engine/portable/src/map/bspbuilder.cpp
Expand Up @@ -59,3 +59,13 @@ uint BspBuilder::numLeafs()
{
return partitioner->numLeafs();
}

uint BspBuilder::numVertexes()
{
return partitioner->numVertexes();
}

Vertex const& BspBuilder::vertex(uint idx)
{
return partitioner->vertex(idx);
}

0 comments on commit 23c8fe5

Please sign in to comment.