Skip to content

Commit

Permalink
Refactor: BspBuilder now directly constructs the HEdges used by GameMap
Browse files Browse the repository at this point in the history
It is no longer necessary to harden the HEdges into an array post
BSP build (a LUT is built however).

Todo: Cleanup.
  • Loading branch information
danij-deng committed Mar 30, 2012
1 parent 06101e3 commit cd49856
Show file tree
Hide file tree
Showing 19 changed files with 306 additions and 366 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/api/dd_maptypes.h
Expand Up @@ -9,7 +9,7 @@

#define DMT_HEDGE_V DDVT_PTR // [Start, End] of the segment.
#define DMT_HEDGE_LINEDEF DDVT_PTR
#define DMT_HEDGE_SEC DDVT_PTR
#define DMT_HEDGE_SECTOR DDVT_PTR
#define DMT_HEDGE_BSPLEAF DDVT_PTR
#define DMT_HEDGE_TWIN DDVT_PTR
#define DMT_HEDGE_ANGLE DDVT_ANGLE
Expand Down
50 changes: 11 additions & 39 deletions doomsday/engine/portable/include/bspbuilder/bspbuilder.hh
Expand Up @@ -29,7 +29,6 @@
#define LIBDENG_BSPBUILDER_HH

#include "dd_types.h"
#include "dd_zone.h"
#include "p_mapdata.h"

#include "bspbuilder/hedges.hh"
Expand All @@ -40,7 +39,7 @@ struct binarytree_s;

namespace de {

/// Number of bsp_hedge_t to block allocate.
/// Number of HEdge to block allocate.
#define BSPBUILDER_HEDGE_ALLOCATOR_BLOCKSIZE 512

/// Default cost factor attributed to splitting an existing half-edge.
Expand All @@ -49,16 +48,10 @@ namespace de {
class BspBuilder {
public:
BspBuilder() : splitCostFactor(BSPBUILDER_PARTITION_COST_HEDGESPLIT)
{
// Init the half-edge block allocator.
hedgeBlockSet = ZBlockSet_New(sizeof(bsp_hedge_t), BSPBUILDER_HEDGE_ALLOCATOR_BLOCKSIZE, PU_APPSTATIC);
}
{}

~BspBuilder()
{
// Shutdown the half-edge block allocator.
ZBlockSet_Delete(hedgeBlockSet);
}
{}

BspBuilder* setSplitCostFactor(int factor)
{
Expand Down Expand Up @@ -86,31 +79,12 @@ public:
*/
void deleteHEdgeIntercept(HEdgeIntercept* intercept);

/**
* Destroys the given half-edge.
*
* @param hedge Ptr to the half-edge to be destroyed.
*/
void deleteHEdge(bsp_hedge_t* hedge);

private:
inline bsp_hedge_t* allocHEdge(void)
{
bsp_hedge_t* hedge = (bsp_hedge_t*)ZBlockSet_Allocate(hedgeBlockSet);
memset(hedge, 0, sizeof(bsp_hedge_t));
return hedge;
}

inline void freeHEdge(bsp_hedge_t* /*hedge*/)
{
// Ignore @a hedge it'll be free'd along with the block allocator itself.
}

BspLeaf* createBSPLeaf(SuperBlock* hedgeList);

const HPlaneIntercept* makeHPlaneIntersection(HPlane* hplane, bsp_hedge_t* hedge, int leftSide);
const HPlaneIntercept* makeHPlaneIntersection(HPlane* hplane, HEdge* hedge, int leftSide);

const HPlaneIntercept* makeIntersection(HPlane* hplane, bsp_hedge_t* hedge, int leftSide);
const HPlaneIntercept* makeIntersection(HPlane* hplane, HEdge* hedge, int leftSide);

/**
* Initially create all half-edges, one for each side of a linedef.
Expand All @@ -124,8 +98,8 @@ private:
void buildHEdgesAtIntersectionGaps(HPlane* hplane,
SuperBlock* rightList, SuperBlock* leftList);

void addEdgeTip(Vertex* vert, double dx, double dy, bsp_hedge_t* back,
bsp_hedge_t* front);
void addEdgeTip(Vertex* vert, double dx, double dy, HEdge* back,
HEdge* front);

/**
* Splits the given half-edge at the point (x,y). The new half-edge is returned.
Expand All @@ -141,7 +115,7 @@ private:
* half-edge (and/or backseg), so that future processing is not messed up
* by incorrect counts.
*/
bsp_hedge_t* splitHEdge(bsp_hedge_t* oldHEdge, double x, double y);
HEdge* splitHEdge(HEdge* oldHEdge, double x, double y);

public:
/**
Expand All @@ -157,7 +131,7 @@ public:
* reworked, heavily). I think it is important that both these routines follow
* the exact same logic.
*/
void divideHEdge(bsp_hedge_t* hedge, HPlane* hplane,
void divideHEdge(HEdge* hedge, HPlane* hplane,
SuperBlock* rightList, SuperBlock* leftList);

private:
Expand Down Expand Up @@ -214,7 +188,7 @@ private:
SuperBlock* leftList, HPlane* hplane);

void addHEdgesBetweenIntercepts(HPlane* hplane,
HEdgeIntercept* start, HEdgeIntercept* end, bsp_hedge_t** right, bsp_hedge_t** left);
HEdgeIntercept* start, HEdgeIntercept* end, HEdge** right, HEdge** left);

/**
* Analyze the intersection list, and add any needed minihedges to the given half-edge lists
Expand Down Expand Up @@ -244,7 +218,7 @@ private:
/**
* Create a new half-edge.
*/
bsp_hedge_t* newHEdge(LineDef* line, LineDef* sourceLine, Vertex* start, Vertex* end,
HEdge* newHEdge(LineDef* line, LineDef* sourceLine, Vertex* start, Vertex* end,
Sector* sec, boolean back);

HEdgeIntercept* hedgeInterceptByVertex(HPlane* hplane, Vertex* vertex);
Expand All @@ -258,8 +232,6 @@ private:

private:
int splitCostFactor;

zblockset_t* hedgeBlockSet;
};

} // namespace de
Expand Down
54 changes: 2 additions & 52 deletions doomsday/engine/portable/include/bspbuilder/hedges.hh
Expand Up @@ -29,7 +29,6 @@
#define LIBDENG_BSPBUILDER_HEDGES_HH

#include "dd_types.h"
#include "p_mapdata.h"

#define IFFY_LEN 4.0

Expand All @@ -39,57 +38,8 @@
// Smallest degrees between two angles before being considered equal.
#define ANG_EPSILON (1.0 / 1024.0)

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

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

typedef struct bsp_hedge_s {
struct vertex_s* v[2]; // [Start, End] of the half-edge..

// Half-edge on the other side, or NULL if one-sided. This relationship
// is always one-to-one -- if one of the half-edges is split, the twin
// must also be split.
struct bsp_hedge_s* twin;

//struct bsp_hedge_s* nextInBlock;
struct bsp_hedge_s* nextInLeaf;
struct bsp_hedge_s* nextOnSide;
struct bsp_hedge_s* prevOnSide;

// Index of the half-edge. Only valid once the half-edge has been added
// to a polygon. A negative value means it is invalid -- there
// shouldn't be any of these once the BSP tree has been built.
int index;

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

struct sector_s* sector; // Adjacent sector, or NULL if invalid sidedef or minihedge.
byte side; // 0 for right, 1 for left.
} bsp_hedge_t;
struct vertex_s;
struct sector_s;

/**
* Plain-old-data structure containing additional information for a half-edge
Expand Down
2 changes: 2 additions & 0 deletions doomsday/engine/portable/include/bspbuilder/intersection.hh
Expand Up @@ -28,6 +28,8 @@
#ifndef LIBDENG_BSPBUILDER_INTERSECTION
#define LIBDENG_BSPBUILDER_INTERSECTION

#include "p_mapdata.h"

#include "bspbuilder/hedges.hh"

#include <list>
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/include/bspbuilder/superblockmap.hh
Expand Up @@ -59,7 +59,7 @@ class SuperBlock {
friend class SuperBlockmap;

public:
typedef std::list<bsp_hedge_t*> HEdges;
typedef std::list<HEdge*> HEdges;

enum ChildId
{
Expand Down Expand Up @@ -164,15 +164,15 @@ public:
*
* @param hedge HEdge instance to add.
*/
SuperBlock* hedgePush(bsp_hedge_t* hedge);
SuperBlock* hedgePush(HEdge* hedge);

/**
* Pop (unlink) the next HEdge from the FIFO list of half-edges linked
* to this superblock.
*
* @return Previous top-most HEdge instance or @c NULL if empty.
*/
bsp_hedge_t* hedgePop();
HEdge* hedgePop();

private:
struct Instance;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/edit_bsp.h
Expand Up @@ -59,7 +59,7 @@
struct hplane_s;
struct superblock_s;
struct hplaneintercept_s;
struct bsp_hedge_s;
struct hedge_s;
struct edgetip_s;
struct vertex_s;
struct sector_s;
Expand Down
4 changes: 1 addition & 3 deletions doomsday/engine/portable/include/edit_map.h
Expand Up @@ -32,8 +32,6 @@
#include "m_binarytree.h"
#include "materials.h"

struct bsp_hedge_s;

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -121,7 +119,7 @@ typedef struct edgetip_s {
// Half-edge on each side of the edge. Left is the side of increasing
// angles, right is the side of decreasing angles. Either can be NULL
// for one sided edges.
struct bsp_hedge_s* hedges[2];
struct hedge_s* hedges[2];
} edgetip_t;

struct edgetip_s* MPE_NewEdgeTip(void);
Expand Down
12 changes: 12 additions & 0 deletions doomsday/engine/portable/include/hedge.h
Expand Up @@ -26,8 +26,16 @@
#include "r_data.h"
#include "p_dmu.h"

#ifdef __cplusplus
extern "C" {
#endif

HEdge* HEdge_New(void);

HEdge* HEdge_NewCopy(HEdge* other);

void HEdge_Delete(HEdge* hedge);

/**
* Get a property value, selected by DMU_* name.
*
Expand All @@ -46,4 +54,8 @@ int HEdge_GetProperty(const HEdge* hedge, setargs_t* args);
*/
int HEdge_SetProperty(HEdge* hedge, const setargs_t* args);

#ifdef __cplusplus
}
#endif

#endif /// LIBDENG_MAP_HEDGE
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/mapdata.hs
Expand Up @@ -89,7 +89,7 @@ end
struct HEdge
PTR vertex_s*[2] v // [Start, End] of the segment.
PTR linedef_s* lineDef
PTR sector_s*[2] sec
PTR sector_s* sector
PTR bspleaf_s* bspLeaf
PTR hedge_s* twin
ANGLE angle_t angle
Expand All @@ -107,7 +107,7 @@ internal
#define BLF_MIDPOINT 0x80 ///< Midpoint is tri-fan centre.

typedef struct {
struct bsp_hedge_s* hedges; // Head ptr to a list of half-edges at this leaf.
struct hedge_s* hedges; // Head ptr to a list of half-edges at this leaf.
} mbspleaf_t;
end

Expand Down

0 comments on commit cd49856

Please sign in to comment.