Skip to content

Commit

Permalink
BSP Builder: Cleanup in preparation for the next round of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 23, 2012
1 parent 3f6fbed commit 34b9b2a
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 448 deletions.
73 changes: 35 additions & 38 deletions doomsday/engine/portable/include/bspbuilder/bspbuilder.hh
Expand Up @@ -31,16 +31,13 @@
#include "dd_types.h"
#include "dd_zone.h"

#include "p_mapdata.h"
#include "bspbuilder/hedges.hh"
#include "bspbuilder/intersection.hh"

struct hedgeintercept_s;
struct vertex_s;
struct gamemap_s;
struct binarytree_s;
struct superblockmap_s;
struct superblock_s;
struct bspleafdata_s;

namespace de {

Expand All @@ -52,16 +49,16 @@ public:
BspBuilder()
{
// Init the half-edge block allocator.
hEdgeBlockSet = ZBlockSet_New(sizeof(bsp_hedge_t), BSPBUILDER_HEDGE_ALLOCATOR_BLOCKSIZE, PU_APPSTATIC);
hedgeBlockSet = ZBlockSet_New(sizeof(bsp_hedge_t), BSPBUILDER_HEDGE_ALLOCATOR_BLOCKSIZE, PU_APPSTATIC);
}

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

void initForMap(struct gamemap_s* map);
void initForMap(GameMap* map);

/**
* Build the BSP for the given map.
Expand All @@ -72,7 +69,7 @@ public:
*
* @return @c true= iff completed successfully.
*/
boolean build(struct gamemap_s* map, struct vertex_s*** vertexes, uint* numVertexes);
boolean build(GameMap* map, Vertex*** vertexes, uint* numVertexes);

void deleteLeaf(struct bspleafdata_s* leaf);

Expand All @@ -81,51 +78,51 @@ public:
*
* @param inter Ptr to the intersection to be destroyed.
*/
void deleteHEdgeIntercept(struct hedgeintercept_s* intercept);
void deleteHEdgeIntercept(HEdgeIntercept* intercept);

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;
bsp_hedge_t* hedge = (bsp_hedge_t*)ZBlockSet_Allocate(hedgeBlockSet);
memset(hedge, 0, sizeof(bsp_hedge_t));
return hedge;
}

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

struct bspleafdata_s* createBSPLeaf(struct superblock_s* hEdgeList);
struct bspleafdata_s* createBSPLeaf(struct superblock_s* hedgeList);

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

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

/**
* Initially create all half-edges, one for each side of a linedef.
*
* @return The list of created half-edges.
*/
struct superblockmap_s* createInitialHEdges(struct gamemap_s* map);
struct superblockmap_s* createInitialHEdges(GameMap* map);

struct bspleafdata_s* newLeaf(void);

void mergeIntersections(HPlane* intersections);

void buildHEdgesAtIntersectionGaps(HPlane* hPlane,
void buildHEdgesAtIntersectionGaps(HPlane* hplane,
struct superblock_s* rightList, struct superblock_s* leftList);

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

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

/**
* Splits the given half-edge at the point (x,y). The new half-edge is returned.
Expand Down Expand Up @@ -157,20 +154,20 @@ 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(bsp_hedge_t* hedge, HPlane* hplane,
struct superblock_s* rightList, struct superblock_s* leftList);

private:
/**
* Find the best half-edge in the list to use as a partition.
*
* @param hEdgeList List of half-edges to choose from.
* @param hedgeList List of half-edges to choose from.
* @param depth Current node depth.
* @param partition Ptr to partition to be updated with the results.
*
* @return @c true= A suitable partition was found.
*/
boolean choosePartition(struct superblock_s* hEdgeList, size_t depth, HPlane* hPlane);
boolean choosePartition(struct superblock_s* hedgeList, size_t depth, HPlane* hplane);

/**
* Takes the half-edge list and determines if it is convex, possibly converting it
Expand All @@ -189,11 +186,11 @@ private:
* @param superblock Ptr to the list of half edges at the current node.
* @param parent Ptr to write back the address of any newly created subtree.
* @param depth Current tree depth.
* @param hPlane HPlaneIntercept list for storing any new intersections.
* @param hplane HPlaneIntercept list for storing any new intersections.
* @return @c true iff successfull.
*/
boolean buildNodes(struct superblock_s* superblock, struct binarytree_s** parent,
size_t depth, HPlane* hPlane);
size_t depth, HPlane* hplane);

/**
* Traverse the BSP tree and put all the half-edges in each BSP leaf into clockwise
Expand All @@ -210,19 +207,19 @@ private:
* lists based on the given partition line. Adds any intersections onto the
* intersection list as it goes.
*/
void partitionHEdges(struct superblock_s* hEdgeList, struct superblock_s* rightList,
struct superblock_s* leftList, HPlane* hPlane);
void partitionHEdges(struct superblock_s* hedgeList, struct superblock_s* rightList,
struct superblock_s* leftList, HPlane* hplane);

void addHEdgesBetweenIntercepts(HPlane* hPlane,
struct hedgeintercept_s* start, struct hedgeintercept_s* end, bsp_hedge_t** right, bsp_hedge_t** left);
void addHEdgesBetweenIntercepts(HPlane* hplane,
HEdgeIntercept* start, HEdgeIntercept* end, bsp_hedge_t** right, bsp_hedge_t** left);

/**
* Analyze the intersection list, and add any needed minihedges to the given half-edge lists
* (one minihedge on each side).
*
* @note All the intersections in the hPlane will be free'd back into the quick-alloc list.
* @note All the intersections in the hplane will be free'd back into the quick-alloc list.
*/
void addMiniHEdges(HPlane* hPlane, struct superblock_s* rightList,
void addMiniHEdges(HPlane* hplane, struct superblock_s* rightList,
struct superblock_s* leftList);

/**
Expand All @@ -233,21 +230,21 @@ private:
*
* @return Ptr to the found intercept, else @c NULL;
*/
const HPlaneIntercept* hplaneInterceptByVertex(HPlane* hPlane, Vertex* vertex);
const HPlaneIntercept* hplaneInterceptByVertex(HPlane* hplane, Vertex* vertex);

/**
* Create a new intersection.
*/
struct hedgeintercept_s* newHEdgeIntercept(struct vertex_s* vertex,
const struct hplanebuildinfo_s* partition, boolean lineDefIsSelfReferencing);
HEdgeIntercept* newHEdgeIntercept(Vertex* vertex,
const BspHEdgeInfo* partition, boolean lineDefIsSelfReferencing);

/**
* Create a new half-edge.
*/
bsp_hedge_t* newHEdge(LineDef* line, LineDef* sourceLine, Vertex* start, Vertex* end,
Sector* sec, boolean back);

struct hedgeintercept_s* hedgeInterceptByVertex(HPlane* hPlane, Vertex* vertex);
HEdgeIntercept* hedgeInterceptByVertex(HPlane* hplane, Vertex* vertex);

/**
* Check whether a line with the given delta coordinates and beginning at this
Expand All @@ -257,7 +254,7 @@ private:
Sector* openSectorAtPoint(Vertex* vert, double dx, double dy);

private:
zblockset_t* hEdgeBlockSet;
zblockset_t* hedgeBlockSet;
};

} // namespace de
Expand Down
52 changes: 28 additions & 24 deletions doomsday/engine/portable/include/bspbuilder/hedges.hh
Expand Up @@ -28,12 +28,8 @@
#ifndef LIBDENG_MAP_BSP_HEDGE
#define LIBDENG_MAP_BSP_HEDGE

//#include "dd_types.h"
//#include "bsp_main.h"

struct vertex_s;
struct linedef_s;
struct sector_s;
#include "dd_types.h"
#include "p_mapdata.h"

#define IFFY_LEN 4.0

Expand All @@ -43,6 +39,30 @@ struct sector_s;
// 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..

Expand All @@ -65,30 +85,14 @@ typedef struct bsp_hedge_s {
// is no longer in any superblock (e.g. now in a leaf).
struct superblock_s* block;

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

typedef struct bspleafdata_s {
struct bsp_hedge_s* hEdges; // Head ptr to a list of half-edges at this leaf.
struct bsp_hedge_s* hedges; // Head ptr to a list of half-edges at this leaf.
} bspleafdata_t;

/**
Expand Down
65 changes: 40 additions & 25 deletions doomsday/engine/portable/include/bspbuilder/intersection.hh
Expand Up @@ -31,23 +31,31 @@
#include "bspbuilder/hedges.hh"

#include <list>
#include <algorithm>

struct linedef_s;

namespace de {

class BspBuilder;

typedef struct hplanebuildinfo_s {
struct linedef_s* lineDef; // Not NULL if partition originated from a linedef.
struct linedef_s* sourceLineDef;
struct HPlanePartition {
double origin[2];
double angle[2];

double pSX, pSY;
double pDX, pDY;
double pPara, pPerp;
double pLength;
} HPlaneBuildInfo;
HPlanePartition(double x=0, double y=0, double dX=0, double dY=0)
{
origin[0] = x;
origin[1] = y;
angle[0] = dX;
angle[1] = dY;
}

HPlanePartition(double const _origin[2], double const _angle[2])
{
origin[0] = _origin[0];
origin[1] = _origin[1];
angle[0] = _angle[0];
angle[1] = _angle[1];
}
};

struct HPlaneIntercept {
// How far along the partition line the vertex is. Zero is at the
Expand Down Expand Up @@ -76,11 +84,15 @@ public:
typedef std::list<HPlaneIntercept> Intercepts;

HPlane(BspBuilder* builder) :
builder(builder), intercepts(0)
builder(builder), intercepts(0), partition()
{
initHEdgeInfo();
}

HPlane(BspBuilder* builder, double const origin[2], double const angle[2]) :
builder(builder), intercepts(0), partition(origin, angle)
{
partition.origin[0] = partition.origin[1] = 0;
partition.angle[0] = partition.angle[1] = 0;
memset(&info, 0, sizeof(info));
initHEdgeInfo();
}

~HPlane() { clear(); }
Expand All @@ -103,8 +115,8 @@ public:
HPlane* setDX(double dx);
HPlane* setDY(double dy);

/// @fixme Should be immutable.
HPlaneBuildInfo* buildInfo() { return &info; }
/// @todo Does not belong here.
BspHEdgeInfo* partitionHEdgeInfo() { return &hedgeInfo; }

/**
* Empty all intersections from the specified HPlane.
Expand All @@ -131,20 +143,23 @@ public:
inline Intercepts::size_type size() const { return intercepts.size(); }

private:
/// BspBuilder instance which produced this.
/// @todo Remove me.
BspBuilder* builder;
void initHEdgeInfo()
{
memset(&hedgeInfo, 0, sizeof(hedgeInfo));
}

struct {
double origin[2];
double angle[2];
} partition;
private:
HPlanePartition partition;

/// The intercept list. Kept sorted by along_dist, in ascending order.
Intercepts intercepts;

/// Additional information used by the node builder during construction.
HPlaneBuildInfo info;
BspHEdgeInfo hedgeInfo;

/// BspBuilder instance which produced this.
/// @todo Remove me.
BspBuilder* builder;
};

} // namespace de
Expand Down

0 comments on commit 34b9b2a

Please sign in to comment.