Skip to content

Commit

Permalink
Various Cleanups (vmap/)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmoozerd committed Jul 19, 2012
1 parent a027045 commit d28bf87
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 354 deletions.
38 changes: 24 additions & 14 deletions src/game/vmap/BIH.cpp
Expand Up @@ -18,7 +18,7 @@

#include "BIH.h"

void BIH::buildHierarchy(std::vector<uint32> &tempTree, buildData &dat, BuildStats &stats)
void BIH::buildHierarchy(std::vector<uint32>& tempTree, buildData& dat, BuildStats& stats)
{
// create space for the first node
tempTree.push_back(3 << 30); // dummy leaf
Expand All @@ -32,7 +32,7 @@ void BIH::buildHierarchy(std::vector<uint32> &tempTree, buildData &dat, BuildSta
subdivide(0, dat.numPrims - 1, tempTree, dat, gridBox, nodeBox, 0, 1, stats);
}

void BIH::subdivide(int left, int right, std::vector<uint32> &tempTree, buildData &dat, AABound &gridBox, AABound &nodeBox, int nodeIndex, int depth, BuildStats &stats)
void BIH::subdivide(int left, int right, std::vector<uint32>& tempTree, buildData& dat, AABound& gridBox, AABound& nodeBox, int nodeIndex, int depth, BuildStats& stats)
{
if ((right - left + 1) <= dat.maxPrims || depth >= MAX_STACK_SIZE)
{
Expand All @@ -51,7 +51,7 @@ void BIH::subdivide(int left, int right, std::vector<uint32> &tempTree, buildDat
prevAxis = axis;
prevSplit = split;
// perform quick consistency checks
Vector3 d( gridBox.hi - gridBox.lo );
Vector3 d(gridBox.hi - gridBox.lo);
if (d.x < 0 || d.y < 0 || d.z < 0)
throw std::logic_error("negative node extents");
for (int i = 0; i < 3; i++)
Expand Down Expand Up @@ -127,13 +127,15 @@ void BIH::subdivide(int left, int right, std::vector<uint32> &tempTree, buildDat
if (right == rightOrig)
{
// all left
if (prevAxis == axis && G3D::fuzzyEq(prevSplit, split)) {
if (prevAxis == axis && G3D::fuzzyEq(prevSplit, split))
{
// we are stuck here - create a leaf
stats.updateLeaf(depth, right - left + 1);
createNode(tempTree, nodeIndex, left, right);
return;
}
if (clipL <= split) {
if (clipL <= split)
{
// keep looping on left half
gridBox.hi[axis] = split;
prevClip = clipL;
Expand All @@ -146,14 +148,16 @@ void BIH::subdivide(int left, int right, std::vector<uint32> &tempTree, buildDat
else if (left > right)
{
// all right
if (prevAxis == axis && G3D::fuzzyEq(prevSplit, split)) {
if (prevAxis == axis && G3D::fuzzyEq(prevSplit, split))
{
// we are stuck here - create a leaf
stats.updateLeaf(depth, right - left + 1);
createNode(tempTree, nodeIndex, left, right);
return;
}
right = rightOrig;
if (clipR >= split) {
if (clipR >= split)
{
// keep looping on right half
gridBox.lo[axis] = split;
prevClip = clipR;
Expand All @@ -175,14 +179,17 @@ void BIH::subdivide(int left, int right, std::vector<uint32> &tempTree, buildDat
tempTree.push_back(0);
tempTree.push_back(0);
tempTree.push_back(0);
if (wasLeft) {
if (wasLeft)
{
// create a node with a left child
// write leaf node
stats.updateInner();
tempTree[nodeIndex + 0] = (prevAxis << 30) | nextIndex;
tempTree[nodeIndex + 1] = floatToRawIntBits(prevClip);
tempTree[nodeIndex + 2] = floatToRawIntBits(G3D::inf());
} else {
}
else
{
// create a node with a right child
// write leaf node
stats.updateInner();
Expand All @@ -204,14 +211,17 @@ void BIH::subdivide(int left, int right, std::vector<uint32> &tempTree, buildDat
// allocate left node
int nl = right - left + 1;
int nr = rightOrig - (right + 1) + 1;
if (nl > 0) {
if (nl > 0)
{
tempTree.push_back(0);
tempTree.push_back(0);
tempTree.push_back(0);
} else
}
else
nextIndex -= 3;
// allocate right node
if (nr > 0) {
if (nr > 0)
{
tempTree.push_back(0);
tempTree.push_back(0);
tempTree.push_back(0);
Expand All @@ -238,7 +248,7 @@ void BIH::subdivide(int left, int right, std::vector<uint32> &tempTree, buildDat
stats.updateLeaf(depth + 1, 0);
}

bool BIH::writeToFile(FILE *wf) const
bool BIH::writeToFile(FILE* wf) const
{
uint32 treeSize = tree.size();
uint32 check=0, count=0;
Expand All @@ -252,7 +262,7 @@ bool BIH::writeToFile(FILE *wf) const
return check == (3 + 3 + 2 + treeSize + count);
}

bool BIH::readFromFile(FILE *rf)
bool BIH::readFromFile(FILE* rf)
{
uint32 treeSize;
Vector3 lo, hi;
Expand Down
80 changes: 45 additions & 35 deletions src/game/vmap/BIH.h
Expand Up @@ -34,9 +34,9 @@
#define MAX_STACK_SIZE 64

#ifdef _MSC_VER
#define isnan(x) _isnan(x)
#define isnan(x) _isnan(x)
#else
#define isnan(x) std::isnan(x)
#define isnan(x) std::isnan(x)
#endif

using G3D::Vector3;
Expand Down Expand Up @@ -82,9 +82,9 @@ class BIH
public:
BIH() {};
template< class T, class BoundsFunc >
void build(const std::vector<T> &primitives, BoundsFunc &getBounds, uint32 leafSize = 3, bool printStats=false)
void build(const std::vector<T>& primitives, BoundsFunc& getBounds, uint32 leafSize = 3, bool printStats=false)
{
if(primitives.size() == 0)
if (primitives.size() == 0)
return;
buildData dat;
dat.maxPrims = leafSize;
Expand Down Expand Up @@ -116,7 +116,7 @@ class BIH
uint32 primCount() { return objects.size(); }

template<typename RayCallback>
void intersectRay(const Ray &r, RayCallback& intersectCallback, float &maxDist, bool stopAtFirst=false) const
void intersectRay(const Ray& r, RayCallback& intersectCallback, float& maxDist, bool stopAtFirst=false) const
{
float intervalMin = -1.f;
float intervalMax = -1.f;
Expand Down Expand Up @@ -154,7 +154,7 @@ class BIH
uint32 offsetBack3[3];
// compute custom offsets from direction sign bit

for(int i=0; i<3; ++i)
for (int i=0; i<3; ++i)
{
offsetFront[i] = floatToRawIntBits(dir[i]) >> 31;
offsetBack[i] = offsetFront[i] ^ 1;
Expand All @@ -170,7 +170,8 @@ class BIH
int stackPos = 0;
int node = 0;

while (true) {
while (true)
{
while (true)
{
uint32 tn = tree[node];
Expand All @@ -190,13 +191,15 @@ class BIH
int back = offset + offsetBack3[axis];
node = back;
// ray passes through far node only
if (tf < intervalMin) {
if (tf < intervalMin)
{
intervalMin = (tb >= intervalMin) ? tb : intervalMin;
continue;
}
node = offset + offsetFront3[axis]; // front
// ray passes through near node only
if (tb > intervalMax) {
if (tb > intervalMax)
{
intervalMax = (tf <= intervalMax) ? tf : intervalMax;
continue;
}
Expand All @@ -214,9 +217,10 @@ class BIH
{
// leaf - test some objects
int n = tree[node + 1];
while (n > 0) {
while (n > 0)
{
bool hit = intersectCallback(r, objects[offset], maxDist, stopAtFirst);
if(stopAtFirst && hit) return;
if (stopAtFirst && hit) return;
--n;
++offset;
}
Expand Down Expand Up @@ -250,12 +254,13 @@ class BIH
node = stack[stackPos].node;
intervalMax = stack[stackPos].tfar;
break;
} while (true);
}
while (true);
}
}

template<typename IsectCallback>
void intersectPoint(const Vector3 &p, IsectCallback& intersectCallback) const
void intersectPoint(const Vector3& p, IsectCallback& intersectCallback) const
{
if (!bounds.contains(p))
return;
Expand All @@ -264,7 +269,8 @@ class BIH
int stackPos = 0;
int node = 0;

while (true) {
while (true)
{
while (true)
{
uint32 tn = tree[node];
Expand All @@ -284,12 +290,14 @@ class BIH
int right = offset + 3;
node = right;
// point is in right node only
if (tl < p[axis]) {
if (tl < p[axis])
{
continue;
}
node = offset; // left
// point is in left node only
if (tr > p[axis]) {
if (tr > p[axis])
{
continue;
}
// point is in both nodes
Expand All @@ -302,7 +310,8 @@ class BIH
{
// leaf - test some objects
int n = tree[node + 1];
while (n > 0) {
while (n > 0)
{
intersectCallback(p, objects[offset]); // !!!
--n;
++offset;
Expand Down Expand Up @@ -332,8 +341,8 @@ class BIH
}
}

bool writeToFile(FILE *wf) const;
bool readFromFile(FILE *rf);
bool writeToFile(FILE* wf) const;
bool readFromFile(FILE* rf);

protected:
std::vector<uint32> tree;
Expand All @@ -342,8 +351,8 @@ class BIH

struct buildData
{
uint32 *indices;
AABox *primBound;
uint32* indices;
AABox* primBound;
uint32 numPrims;
int maxPrims;
};
Expand All @@ -369,29 +378,30 @@ class BIH
int numBVH2;

public:
BuildStats():
numNodes(0), numLeaves(0), sumObjects(0), minObjects(0x0FFFFFFF),
maxObjects(0xFFFFFFFF), sumDepth(0), minDepth(0x0FFFFFFF),
maxDepth(0xFFFFFFFF), numBVH2(0)
{
for(int i=0; i<6; ++i) numLeavesN[i] = 0;
}
BuildStats():
numNodes(0), numLeaves(0), sumObjects(0), minObjects(0x0FFFFFFF),
maxObjects(0xFFFFFFFF), sumDepth(0), minDepth(0x0FFFFFFF),
maxDepth(0xFFFFFFFF), numBVH2(0)
{
for (int i=0; i<6; ++i) numLeavesN[i] = 0;
}

void updateInner() { numNodes++; }
void updateBVH2() { numBVH2++; }
void updateLeaf(int depth, int n);
void printStats();
void updateInner() { numNodes++; }
void updateBVH2() { numBVH2++; }
void updateLeaf(int depth, int n);
void printStats();
};

void buildHierarchy(std::vector<uint32> &tempTree, buildData &dat, BuildStats &stats);
void buildHierarchy(std::vector<uint32>& tempTree, buildData& dat, BuildStats& stats);

void createNode(std::vector<uint32> &tempTree, int nodeIndex, uint32 left, uint32 right) {
void createNode(std::vector<uint32>& tempTree, int nodeIndex, uint32 left, uint32 right)
{
// write leaf node
tempTree[nodeIndex + 0] = (3 << 30) | left;
tempTree[nodeIndex + 1] = right - left + 1;
}

void subdivide(int left, int right, std::vector<uint32> &tempTree, buildData &dat, AABound &gridBox, AABound &nodeBox, int nodeIndex, int depth, BuildStats &stats);
void subdivide(int left, int right, std::vector<uint32>& tempTree, buildData& dat, AABound& gridBox, AABound& nodeBox, int nodeIndex, int depth, BuildStats& stats);
};

#endif // _BIH_H
14 changes: 7 additions & 7 deletions src/game/vmap/IVMapManager.h
Expand Up @@ -38,8 +38,8 @@ namespace VMAP
VMAP_LOAD_RESULT_IGNORED,
};

#define VMAP_INVALID_HEIGHT -100000.0f // for check
#define VMAP_INVALID_HEIGHT_VALUE -200000.0f // real assigned value in unknown height case
#define VMAP_INVALID_HEIGHT -100000.0f // for check
#define VMAP_INVALID_HEIGHT_VALUE -200000.0f // real assigned value in unknown height case

//===========================================================
class IVMapManager
Expand All @@ -66,11 +66,11 @@ namespace VMAP
test if we hit an object. return true if we hit one. rx,ry,rz will hold the hit position or the dest position, if no intersection was found
return a position, that is pReduceDist closer to the origin
*/
virtual bool getObjectHitPos(unsigned int pMapId, float x1, float y1, float z1, float x2, float y2, float z2, float& rx, float &ry, float& rz, float pModifyDist) = 0;
virtual bool getObjectHitPos(unsigned int pMapId, float x1, float y1, float z1, float x2, float y2, float z2, float& rx, float& ry, float& rz, float pModifyDist) = 0;
/**
send debug commands
*/
virtual bool processCommand(char *pCommand)= 0;
virtual bool processCommand(char* pCommand)= 0;

/**
Enable/disable LOS calculation
Expand All @@ -85,15 +85,15 @@ namespace VMAP

bool isLineOfSightCalcEnabled() const { return(iEnableLineOfSightCalc); }
bool isHeightCalcEnabled() const { return(iEnableHeightCalc); }
bool isMapLoadingEnabled() const { return(iEnableLineOfSightCalc || iEnableHeightCalc ); }
bool isMapLoadingEnabled() const { return(iEnableLineOfSightCalc || iEnableHeightCalc); }

virtual std::string getDirFileName(unsigned int pMapId, int x, int y) const =0;
/**
Query world model area info.
\param z gets adjusted to the ground height for which this are info is valid
*/
virtual bool getAreaInfo(unsigned int pMapId, float x, float y, float &z, uint32 &flags, int32 &adtId, int32 &rootId, int32 &groupId) const=0;
virtual bool GetLiquidLevel(uint32 pMapId, float x, float y, float z, uint8 ReqLiquidType, float &level, float &floor, uint32 &type) const=0;
virtual bool getAreaInfo(unsigned int pMapId, float x, float y, float& z, uint32& flags, int32& adtId, int32& rootId, int32& groupId) const=0;
virtual bool GetLiquidLevel(uint32 pMapId, float x, float y, float z, uint8 ReqLiquidType, float& level, float& floor, uint32& type) const=0;
};

}
Expand Down

0 comments on commit d28bf87

Please sign in to comment.