Skip to content

Commit

Permalink
Refactor: Attribute half-edges to Polygons and polygons to BspLeafs
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jun 3, 2013
1 parent df5d94a commit 8bbbbb2
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 24 deletions.
45 changes: 36 additions & 9 deletions doomsday/client/include/map/hedge.h
Expand Up @@ -24,16 +24,20 @@
#include <de/Error>

#include "MapElement"
#include "BspLeaf"
#include "Line"
#include "Vertex"

#ifdef __CLIENT__
struct BiasSurface;
#endif

class BspLeaf;
class Sector;

namespace de {
class Polygon;
}

/**
* Map geometry half-edge.
*
Expand All @@ -49,12 +53,12 @@ class HEdge : public de::MapElement
DENG2_NO_ASSIGN(HEdge)

public:
/// Required BSP leaf is missing. @ingroup errors
DENG2_ERROR(MissingBspLeafError);

/// Required neighbor half-edge is missing. @ingroup errors
DENG2_ERROR(MissingNeighborError);

/// Required polygon is missing. @ingroup errors
DENG2_ERROR(MissingPolygonError);

/// Required twin half-edge is missing. @ingroup errors
DENG2_ERROR(MissingTwinError);

Expand Down Expand Up @@ -87,9 +91,6 @@ class HEdge : public de::MapElement
/// Linked @em twin half-edge (that on the other side of "this" half-edge).
HEdge *_twin;

/// The @em face of the half-edge (a BSP leaf).
BspLeaf *_bspLeaf;

/// Point along the attributed line at which v1 occurs; otherwise @c 0.
coord_t _lineOffset;

Expand Down Expand Up @@ -186,15 +187,41 @@ class HEdge : public de::MapElement
*/
inline HEdge *twinPtr() const { return hasTwin()? &twin() : 0; }

/**
* Returns @c true iff the half-edge is part of some Polygon.
*/
bool hasPoly() const;

/**
* Returns the Polygon the half-edge is a part of.
*
* @see hasPoly()
*/
de::Polygon &poly() const;

/**
* Change the Polygon to which the half-edge is attributed.
*
* @param newPolygon New Polygon to attribute to the half-edge. Ownership is
* unaffected. Can be @c 0 (to clear the attribution).
*
* @see hasPoly(), poly()
*/
void setPoly(de::Polygon *newPolygon);

/**
* Returns @c true iff a BspLeaf is linked to the half-edge.
*
* @see hasPoly(), Polygon::hasBspLeaf()
*/
bool hasBspLeaf() const;
inline bool hasBspLeaf() const { return hasPoly() && poly().hasBspLeaf(); }

/**
* Returns the BspLeaf linked to the half-edge.
*
* @see poly(), Polygon::bspLeaf()
*/
BspLeaf &bspLeaf() const;
inline BspLeaf &bspLeaf() const { return poly().bspLeaf(); }

/**
* Convenience accessor which determines whether a BspLeaf with an attributed
Expand Down
28 changes: 28 additions & 0 deletions doomsday/client/include/map/polygon.h
Expand Up @@ -23,8 +23,10 @@

#include <de/aabox.h>

#include <de/Error>
#include <de/Vector>

class BspLeaf;
class HEdge;

namespace de {
Expand All @@ -36,6 +38,10 @@ namespace de {
*/
class Polygon
{
public:
/// No BSP leaf is attributed. @ingroup errors
DENG2_ERROR(MissingBspLeafError);

public: /// @todo Make private:
/// First half-edge in the face geometry. Ordered by angle, clockwise starting
/// from the smallest angle.
Expand All @@ -47,6 +53,28 @@ class Polygon
public:
Polygon();

/**
* Returns @c true iff a BSP leaf is attributed to the polygon.
*/
bool hasBspLeaf() const;

/**
* Returns the BSP leaf attributed to the polygon.
*
* @see hasBspLeaf()
*/
BspLeaf &bspLeaf() const;

/**
* Change the BSP leaf attributed to the polygon.
*
* @param newBspLeaf New BSP leaf to attribute the polygon. Ownership is
* unaffected. Can be @c 0 (to clear the attribution).
*
* @see hasBspLeaf(), bspLeaf()
*/
void setBspLeaf(BspLeaf *newBspLeaf);

/**
* Returns a pointer to the first half-edge of the Face of the polygon (note
* that half-edges are sorted in a clockwise order). May return @c 0 if there
Expand Down
4 changes: 4 additions & 0 deletions doomsday/client/src/map/bsp/convexsubspace.cpp
Expand Up @@ -433,6 +433,10 @@ Polygon *ConvexSubspace::buildLeafGeometry() const
hedge->_next = poly->_hedge;
poly->_hedge = hedge;

// Attribute the half edge to the Polygon.
/// @todo Encapsulate in Polygon.
hedge->setPoly(poly);

// There is now one more half-edge in this polygon.
poly->_hedgeCount += 1;

Expand Down
6 changes: 1 addition & 5 deletions doomsday/client/src/map/bsp/partitioner.cpp
Expand Up @@ -1417,15 +1417,11 @@ DENG2_PIMPL(Partitioner)
// the BSP leaf (takes ownership).
leaf->setPoly(subspace.buildLeafGeometry());

// Link the half-edges with the leaf and account.
// Account for the new half-edges.
HEdge *base = leaf->poly().firstHEdge();
HEdge *hedgeIt = base;
do
{
// Attribute the half edge to the BSP leaf.
/// @todo Encapsulate in BspLeaf.
hedgeIt->_bspLeaf = leaf;

// There is now one more HEdge.
numHEdges += 1;

Expand Down
6 changes: 6 additions & 0 deletions doomsday/client/src/map/bspleaf.cpp
Expand Up @@ -224,6 +224,12 @@ void BspLeaf::setPoly(Polygon *newPolygon)

// Assign the new polygon (if any).
d->polygon.reset(newPolygon);

if(newPolygon)
{
// Attribute the new polygon to "this" BSP leaf.
newPolygon->setBspLeaf(this);
}
}

Vector2d const &BspLeaf::worldGridOffset() const
Expand Down
27 changes: 18 additions & 9 deletions doomsday/client/src/map/hedge.cpp
Expand Up @@ -20,6 +20,7 @@

#include <de/Log>

#include "Polygon"
#include "Sector"

#ifdef __CLIENT__
Expand All @@ -35,9 +36,13 @@ DENG2_PIMPL(HEdge)
/// Map Line::Side attributed to the half-edge. Can be @c 0 (partition segment).
Line::Side *lineSide;

/// Polygon geometry to which the half-edge is attributed (if any).
Polygon *poly;

Instance(Public *i)
: Base(i),
lineSide(0)
lineSide(0),
poly(0)
{}

inline HEdge **neighborAdr(ClockDirection direction) {
Expand All @@ -52,7 +57,6 @@ HEdge::HEdge(Vertex &vertex, Line::Side *lineSide)
_next = 0;
_prev = 0;
_twin = 0;
_bspLeaf = 0;
_angle = 0;
_length = 0;
_lineOffset = 0;
Expand Down Expand Up @@ -113,19 +117,24 @@ HEdge &HEdge::twin() const
throw MissingTwinError("HEdge::twin", "No twin half-edge is associated");
}

bool HEdge::hasBspLeaf() const
bool HEdge::hasPoly() const
{
return _bspLeaf != 0;
return d->poly != 0;
}

BspLeaf &HEdge::bspLeaf() const
Polygon &HEdge::poly() const
{
if(_bspLeaf)
if(d->poly)
{
return *_bspLeaf;
return *d->poly;
}
/// @throw MissingBspLeafError Attempted with no BSP leaf associated.
throw MissingBspLeafError("HEdge::bspLeaf", "No BSP leaf is associated");
/// @throw MissingPolygonError Attempted with no Polygon attributed.
throw MissingPolygonError("HEdge::poly", "No polygon is attributed");
}

void HEdge::setPoly(Polygon *newPolygon)
{
d->poly = newPolygon;
}

bool HEdge::hasLineSide() const
Expand Down
27 changes: 26 additions & 1 deletion doomsday/client/src/map/polygon.cpp
Expand Up @@ -20,6 +20,7 @@

#include <de/mathutil.h>

#include "BspLeaf"
#include "HEdge"

#include "map/polygon.h"
Expand All @@ -34,8 +35,12 @@ DENG2_PIMPL(Polygon)
/// Center of vertices.
Vector2d center;

/// BSP leaf to which the polygon is attributed (if any).
BspLeaf *bspLeaf;

Instance(Public *i)
: Base(i)
: Base(i),
bspLeaf(0)
{}

~Instance()
Expand Down Expand Up @@ -71,6 +76,26 @@ Polygon::Polygon()
: _hedge(0), _hedgeCount(0), d(new Instance(this))
{}

bool Polygon::hasBspLeaf() const
{
return d->bspLeaf != 0;
}

BspLeaf &Polygon::bspLeaf() const
{
if(d->bspLeaf)
{
return *d->bspLeaf;
}
/// @throw MissingBspLeafError Attempted with no BSP leaf attributed.
throw MissingBspLeafError("Polygon::bspLeaf", "No BSP leaf is attributed");
}

void Polygon::setBspLeaf(BspLeaf *newBspLeaf)
{
d->bspLeaf = newBspLeaf;
}

HEdge *Polygon::firstHEdge() const
{
return _hedge;
Expand Down

0 comments on commit 8bbbbb2

Please sign in to comment.