Skip to content

Commit

Permalink
Refactor: Applied the pimpl macro in BspBuilder; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 17, 2013
1 parent e139c9f commit 3cc6c09
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 96 deletions.
68 changes: 35 additions & 33 deletions doomsday/client/include/map/bsp/partitioner.h
@@ -1,9 +1,4 @@
/**
* @file partitioner.h
* BSP space partitioner. @ingroup bsp
*
* Based on glBSP 2.24 (in turn, based on BSP 2.3), which is hosted on
* SourceForge: http://sourceforge.net/projects/glbsp/
/** @file partitioner.h BSP space partitioner.
*
* @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2000-2007 Andrew Apted <ajapted@gmail.com>
Expand Down Expand Up @@ -43,19 +38,27 @@ static const coord_t DIST_EPSILON = (1.0 / 128.0);
/// Smallest difference between two angles before being considered equal (in degrees).
static const coord_t ANG_EPSILON = (1.0 / 1024.0);

/**
* BSP space partitioner.
*
* Originally based on glBSP 2.24 (in turn, based on BSP 2.3).
* @see http://sourceforge.net/projects/glbsp/
*
* @ingroup bsp
*/
class Partitioner
{
public:
Partitioner(GameMap& _map, uint numEditableVertexes,
Vertex const **editableVertexes, int _splitCostFactor=7);
Partitioner(GameMap &map, uint numEditableVertexes,
Vertex const **editableVertexes, int splitCostFactor = 7);
~Partitioner();

/**
* Set the cost factor associated with splitting an existing half-edge.
* @param factor New factor value.
* @return Reference to this Partitioner.
*
* @param newFactor New split cost factor.
*/
Partitioner& setSplitCostFactor(int factor);
void setSplitCostFactor(int newFactor);

/**
* Build the BSP for the given map.
Expand All @@ -73,24 +76,24 @@ class Partitioner
*/
bool build();

/**
* Retrieve the number of BspNodes owned by this Partitioner. When the
* build completes this number will be the total number of BspNodes that
* were produced during that process. Note that as BspNode ownership is
* claimed this number will decrease respectively.
*
* @return Current number of BspNodes owned by this Partitioner.
*/
uint numNodes();

/**
* Retrieve a pointer to the root BinaryTree node for the constructed BSP.
* Even if construction fails this will return a valid node.
*
* The only time upon which @c NULL is returned is if called prior to calling
* BspBuilder::build()
*/
BspTreeNode* root() const;
BspTreeNode *root() const;

/**
* Retrieve the number of HEdges owned by this Partitioner. When the build
* completes this number will be the total number of half-edges that were
* produced during that process. Note that as BspLeaf ownership is claimed
* this number will decrease respectively.
*
* @return Current number of HEdges owned by this Partitioner.
*/
uint numHEdges();

/**
* Retrieve the number of BspLeafs owned by this Partitioner. When the
Expand All @@ -103,14 +106,14 @@ class Partitioner
uint numLeafs();

/**
* Retrieve the number of HEdges owned by this Partitioner. When the build
* completes this number will be the total number of half-edges that were
* produced during that process. Note that as BspLeaf ownership is claimed
* this number will decrease respectively.
* Retrieve the number of BspNodes owned by this Partitioner. When the
* build completes this number will be the total number of BspNodes that
* were produced during that process. Note that as BspNode ownership is
* claimed this number will decrease respectively.
*
* @return Current number of HEdges owned by this Partitioner.
* @return Current number of BspNodes owned by this Partitioner.
*/
uint numHEdges();
uint numNodes();

/**
* Retrieve the total number of Vertexes produced during the build process.
Expand All @@ -122,15 +125,14 @@ class Partitioner
* this will result in fatal error. The caller should ensure the index is
* within valid range using Partitioner::numVertexes()
*/
Vertex& vertex(uint index);
Vertex &vertex(uint index);

/**
* Release ownership of the specified object.
* Relinquish ownership of the specified map data element to the caller.
*
* @param ob Map data object to release ownership of.
* @return Reference to this Partitioner.
* @param mapElement Map data element to relinquish ownership of.
*/
Partitioner& release(de::MapElement* ob);
void release(MapElement *mapElement);

private:
struct Instance;
Expand Down
66 changes: 29 additions & 37 deletions doomsday/client/include/map/bspbuilder.h
@@ -1,9 +1,4 @@
/**
* @file bspbuilder.h
* BSP Builder (public interface). @ingroup map
*
* Based on glBSP 2.24 (in turn, based on BSP 2.3), which is hosted on
* SourceForge: http://sourceforge.net/projects/glbsp/
/** @file bspbuilder.h BSP Builder.
*
* @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2000-2007 Andrew Apted <ajapted@gmail.com>
Expand Down Expand Up @@ -33,10 +28,8 @@

namespace de {

namespace bsp { class Partitioner; }

/**
* BSP node builder.
* Map geometry partitioner and BSP tree builder.
*
* BspBuilder constructs a BSP object object tree for the specified map.
*
Expand All @@ -48,37 +41,27 @@ class BspBuilder
/// Default cost factor attributed to splitting an existing half-edge.
static const int DEFAULT_PARTITION_COST_HEDGESPLIT = 7;

public:
/**
* Create a new BspBuilder initialized for construction using the specified map.
* @param map GameMap for which to construct a BSP object tree.
* @param splitCostFactor Cost factor attributed to splitting an existing half-edge.
*/
BspBuilder(GameMap& map, uint numEditableVertexes, Vertex const **editableVertexes,
BspBuilder(GameMap &map, uint numEditableVertexes, Vertex const **editableVertexes,
int splitCostFactor = DEFAULT_PARTITION_COST_HEDGESPLIT);
~BspBuilder();

/**
* Set the cost factor associated with splitting an existing half-edge.
* @param factor New factor value.
* @return Reference to this BspBuilder.
*
* @param newFactor New split cost factor.
*/
BspBuilder& setSplitCostFactor(int factor);
void setSplitCostFactor(int newFactor);

/**
* Build the BSP for the given map.
* @return @c true= iff completed successfully.
*/
bool build();

/**
* Retrieve the number of BspNodes owned by this Partitioner. When the
* build completes this number will be the total number of BspNodes that
* were produced during that process. Note that as BspNode ownership is
* claimed this number will decrease respectively.
*
* @return Current number of BspNodes owned by this Partitioner.
*/
uint numNodes();
bool buildBsp();

/**
* Retrieve a pointer to the root BinaryTree node for the constructed BSP.
Expand All @@ -87,7 +70,17 @@ class BspBuilder
* The only time upon which @c NULL is returned is if called prior to calling
* BspBuilder::build()
*/
BspTreeNode* root() const;
BspTreeNode *root() const;

/**
* Retrieve the number of HEdges owned by this Partitioner. When the build
* completes this number will be the total number of half-edges that were
* produced during that process. Note that as BspLeaf ownership is claimed
* this number will decrease respectively.
*
* @return Current number of HEdges owned by this Partitioner.
*/
uint numHEdges();

/**
* Retrieve the number of BspLeafs owned by this Partitioner. When the
Expand All @@ -100,14 +93,14 @@ class BspBuilder
uint numLeafs();

/**
* Retrieve the number of HEdges owned by this Partitioner. When the build
* completes this number will be the total number of half-edges that were
* produced during that process. Note that as BspLeaf ownership is claimed
* this number will decrease respectively.
* Retrieve the number of BspNodes owned by this Partitioner. When the
* build completes this number will be the total number of BspNodes that
* were produced during that process. Note that as BspNode ownership is
* claimed this number will decrease respectively.
*
* @return Current number of HEdges owned by this Partitioner.
* @return Current number of BspNodes owned by this Partitioner.
*/
uint numHEdges();
uint numNodes();

/**
* Retrieve the total number of Vertexes produced during the build process.
Expand All @@ -119,18 +112,17 @@ class BspBuilder
* this will result in fatal error. The caller should ensure the index is
* within valid range using Partitioner::numVertexes()
*/
Vertex& vertex(uint idx);
Vertex &vertex(uint idx);

/**
* Release ownership of the specified object.
*
* @param ob Map data object to release ownership of.
* @return Reference to this Partitioner.
* @param mapElement Map data object to relinquish ownership of.
*/
BspBuilder& take(de::MapElement* ob);
void take(MapElement *mapElement);

private:
bsp::Partitioner* partitioner;
DENG2_PRIVATE(d)
};

} // namespace de
Expand Down
50 changes: 24 additions & 26 deletions doomsday/client/src/map/bsp/partitioner.cpp
@@ -1,8 +1,4 @@
/** @file partitioner.cpp BSP Partitioner. Recursive node creation and sorting.
* @ingroup map
*
* Originally based on glBSP 2.24 (in turn, based on BSP 2.3), which is hosted
* on SourceForge: http://sourceforge.net/projects/glbsp/
*
* @authors Copyright &copy; 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright &copy; 2006-2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
Expand Down Expand Up @@ -57,6 +53,7 @@
#include "render/r_main.h" // validCount
#include "m_misc.h" // M_BoxOnLineSide2

using namespace de;
using namespace de::bsp;

// Misc utility routines (most don't belong here...):
Expand Down Expand Up @@ -1450,10 +1447,10 @@ struct Partitioner::Instance
return node;
}

BspTreeNode* newTreeNode(de::MapElement* bspOb,
BspTreeNode* rightChild = 0, BspTreeNode* leftChild = 0)
BspTreeNode *newTreeNode(de::MapElement *bspOb,
BspTreeNode *rightChild = 0, BspTreeNode *leftChild = 0)
{
BspTreeNode* subtree = new BspTreeNode(bspOb);
BspTreeNode *subtree = new BspTreeNode(bspOb);
if(rightChild)
{
subtree->setRight(rightChild);
Expand All @@ -1465,7 +1462,7 @@ struct Partitioner::Instance
leftChild->setParent(subtree);
}

treeNodeMap.insert(std::pair<de::MapElement*, BspTreeNode*>(bspOb, subtree));
treeNodeMap.insert(std::pair<de::MapElement *, BspTreeNode *>(bspOb, subtree));
return subtree;
}

Expand All @@ -1487,15 +1484,15 @@ struct Partitioner::Instance
* @param partList The list of half-edges to be carved into convex subregions.
* @return Newly created subtree else @c NULL if degenerate.
*/
BspTreeNode* buildNodes(SuperBlock& partList)
BspTreeNode *buildNodes(SuperBlock &partList)
{
LOG_AS("Partitioner::buildNodes");

de::MapElement* bspObject = 0; ///< Built BSP object at this node.
BspTreeNode* rightTree = 0, *leftTree = 0;
de::MapElement *bspObject = 0; ///< Built BSP object at this node.
BspTreeNode *rightTree = 0, *leftTree = 0;

// Pick a half-edge to use as the next partition plane.
HEdge* partHEdge = chooseNextPartition(partList);
HEdge *partHEdge = chooseNextPartition(partList);
if(partHEdge)
{
//LOG_TRACE("Partition %p [%1.0f, %1.0f] -> [%1.0f, %1.0f].") << de::dintptr(hedge)
Expand Down Expand Up @@ -1540,7 +1537,7 @@ struct Partitioner::Instance
else
{
// No partition required/possible - already convex (or degenerate).
BspLeaf* leaf = buildBspLeaf(collectHEdges(partList));
BspLeaf *leaf = buildBspLeaf(collectHEdges(partList));
partList.clear();

// Not a leaf? (collapse upward).
Expand Down Expand Up @@ -2400,10 +2397,10 @@ struct Partitioner::Instance
}
};

Partitioner::Partitioner(GameMap& map, uint numEditableVertexes,
Partitioner::Partitioner(GameMap &map, uint numEditableVertexes,
Vertex const **editableVertexes, int splitCostFactor)
: d(new Instance(&map, numEditableVertexes, editableVertexes, splitCostFactor))
{
d = new Instance(&map, numEditableVertexes, editableVertexes, splitCostFactor);
d->initForMap();
}

Expand All @@ -2412,18 +2409,20 @@ Partitioner::~Partitioner()
delete d;
}

Partitioner& Partitioner::setSplitCostFactor(int factor)
void Partitioner::setSplitCostFactor(int newFactor)
{
d->splitCostFactor = factor;
return *this;
if(d->splitCostFactor != newFactor)
{
d->splitCostFactor = newFactor;
}
}

bool Partitioner::build()
{
return d->buildBsp(SuperBlockmap(blockmapBounds(d->mapBounds)));
}

BspTreeNode* Partitioner::root() const
BspTreeNode *Partitioner::root() const
{
return d->rootNode;
}
Expand All @@ -2448,22 +2447,21 @@ uint Partitioner::numVertexes()
return d->numVertexes;
}

Vertex& Partitioner::vertex(uint idx)
Vertex &Partitioner::vertex(uint idx)
{
DENG2_ASSERT(idx < d->vertexes.size());
DENG2_ASSERT(d->vertexes[idx]);
return *d->vertexes[idx];
}

Partitioner& Partitioner::release(de::MapElement* ob)
void Partitioner::release(MapElement *mapElement)
{
LOG_AS("Partitioner::release");

if(!d->release(ob))
if(!d->release(mapElement))
{
LOG_DEBUG("Attempted to release an unknown/unowned object %p.") << de::dintptr(ob);
LOG_AS("Partitioner::release");
LOG_DEBUG("Attempted to release an unknown/unowned object %p.")
<< de::dintptr(mapElement);
}
return *this;
}

static LineRelationship lineRelationship(coord_t a, coord_t b, coord_t distEpsilon)
Expand Down

0 comments on commit 3cc6c09

Please sign in to comment.