34 #define MAX_STACK_SIZE 64
78 tree.push_back(3u << 30u);
79 tree.insert(tree.end(), 2, 0);
82 BIH() { init_empty(); }
83 template<
class BoundsFunc,
class PrimArray >
84 void build(
const PrimArray &primitives, BoundsFunc &getBounds,
uint32 leafSize = 3,
bool printStats=
false)
86 if (primitives.size() == 0)
97 getBounds(primitives[0], bounds);
101 getBounds(primitives[i], dat.
primBound[i]);
104 std::vector<uint32> tempTree;
106 buildHierarchy(tempTree, dat, stats);
120 template<
typename RayCallback>
121 void intersectRay(
const G3D::Ray &r, RayCallback& intersectCallback,
float &maxDist,
bool stopAtFirst=
false)
const
123 float intervalMin = -1.f;
124 float intervalMax = -1.f;
128 for (
int i=0; i<3; ++i)
130 invDir[i] = 1.f / dir[i];
133 float t1 = (bounds.low()[i] - org[i]) * invDir[i];
134 float t2 = (bounds.high()[i] - org[i]) * invDir[i];
137 if (t1 > intervalMin)
139 if (t2 < intervalMax || intervalMax < 0.f)
143 if (intervalMax <= 0 || intervalMin >= maxDist)
148 if (intervalMin > intervalMax)
150 intervalMin =
std::max(intervalMin, 0.f);
151 intervalMax =
std::min(intervalMax, maxDist);
159 for (
int i=0; i<3; ++i)
162 offsetBack[i] = offsetFront[i] ^ 1;
163 offsetFront3[i] = offsetFront[i] * 3;
164 offsetBack3[i] = offsetBack[i] * 3;
179 uint32 axis = (tn & (3 << 30)) >> 30;
180 bool BVH2 = (tn & (1 << 29)) != 0;
181 int offset = tn & ~(7 << 29);
187 float tf = (
intBitsToFloat(tree[node + offsetFront[axis]]) - org[axis]) * invDir[axis];
188 float tb = (
intBitsToFloat(tree[node + offsetBack[axis]]) - org[axis]) * invDir[axis];
190 if (tf < intervalMin && tb > intervalMax)
192 int back = offset + offsetBack3[axis];
195 if (tf < intervalMin) {
196 intervalMin = (tb >= intervalMin) ? tb : intervalMin;
199 node = offset + offsetFront3[axis];
201 if (tb > intervalMax) {
202 intervalMax = (tf <= intervalMax) ? tf : intervalMax;
207 stack[stackPos].
node = back;
208 stack[stackPos].
tnear = (tb >= intervalMin) ? tb : intervalMin;
209 stack[stackPos].
tfar = intervalMax;
212 intervalMax = (tf <= intervalMax) ? tf : intervalMax;
218 int n = tree[node + 1];
220 bool hit = intersectCallback(r, objects[offset], maxDist, stopAtFirst);
221 if (stopAtFirst && hit)
return;
232 float tf = (
intBitsToFloat(tree[node + offsetFront[axis]]) - org[axis]) * invDir[axis];
233 float tb = (
intBitsToFloat(tree[node + offsetBack[axis]]) - org[axis]) * invDir[axis];
235 intervalMin = (tf >= intervalMin) ? tf : intervalMin;
236 intervalMax = (tb <= intervalMax) ? tb : intervalMax;
237 if (intervalMin > intervalMax)
249 intervalMin = stack[stackPos].
tnear;
250 if (maxDist < intervalMin)
252 node = stack[stackPos].
node;
253 intervalMax = stack[stackPos].
tfar;
259 template<
typename IsectCallback>
262 if (!bounds.contains(p))
273 uint32 axis = (tn & (3 << 30)) >> 30;
274 bool BVH2 = (tn & (1 << 29)) != 0;
275 int offset = tn & ~(7 << 29);
284 if (tl < p[axis] && tr > p[axis])
286 int right = offset + 3;
299 stack[stackPos].
node = right;
306 int n = tree[node + 1];
308 intersectCallback(p, objects[offset]);
322 if (tl > p[axis] || tr < p[axis])
333 node = stack[stackPos].
node;
337 bool writeToFile(FILE* wf)
const;
338 bool readFromFile(FILE* rf);
375 numNodes(0), numLeaves(0), sumObjects(0), minObjects(0x0FFFFFFF),
376 maxObjects(0xFFFFFFFF), sumDepth(0), minDepth(0x0FFFFFFF),
377 maxDepth(0xFFFFFFFF), numBVH2(0)
379 for (
int i=0; i<6; ++i) numLeavesN[i] = 0;
384 void updateLeaf(
int depth,
int n);
388 void buildHierarchy(std::vector<uint32> &tempTree, buildData &dat, BuildStats &stats);
393 tempTree[nodeIndex + 0] = (3 << 30) | left;
394 tempTree[nodeIndex + 1] = right - left + 1;
397 void subdivide(
int left,
int right, std::vector<uint32> &tempTree, buildData &dat,
AABound &gridBox,
AABound &nodeBox,
int nodeIndex,
int depth, BuildStats &stats);
G3D::Vector3 lo
Definition: BoundingIntervalHierarchy.h:60
BuildStats()
Definition: BoundingIntervalHierarchy.h:374
int maxObjects
Definition: BoundingIntervalHierarchy.h:366
Definition: BoundingIntervalHierarchy.h:58
uint32 numPrims
Definition: BoundingIntervalHierarchy.h:349
int minObjects
Definition: BoundingIntervalHierarchy.h:365
uint32 node
Definition: BoundingIntervalHierarchy.h:354
void updateBVH2()
Definition: BoundingIntervalHierarchy.h:383
float tfar
Definition: BoundingIntervalHierarchy.h:356
int sumDepth
Definition: BoundingIntervalHierarchy.h:367
int numLeaves
Definition: BoundingIntervalHierarchy.h:363
uint32 primCount() const
Definition: BoundingIntervalHierarchy.h:118
int maxPrims
Definition: BoundingIntervalHierarchy.h:350
static uint32 floatToRawIntBits(float f)
Definition: BoundingIntervalHierarchy.h:36
Definition: BoundingIntervalHierarchy.h:352
bool fuzzyNe(double a, double b)
Definition: g3dmath.h:861
float tnear
Definition: BoundingIntervalHierarchy.h:355
T max(const T &x, const T &y)
Definition: g3dmath.h:320
int numBVH2
Definition: BoundingIntervalHierarchy.h:371
Definition: BoundingIntervalHierarchy.h:359
void updateInner()
Definition: BoundingIntervalHierarchy.h:382
void init_empty()
Definition: BoundingIntervalHierarchy.h:73
std::vector< uint32 > objects
Definition: BoundingIntervalHierarchy.h:342
static float intBitsToFloat(uint32 i)
Definition: BoundingIntervalHierarchy.h:47
G3D::AABox bounds
Definition: BoundingIntervalHierarchy.h:343
T min(const T &x, const T &y)
Definition: g3dmath.h:305
void printStats()
Definition: BoundingIntervalHierarchy.cpp:291
int minDepth
Definition: BoundingIntervalHierarchy.h:368
int numNodes
Definition: BoundingIntervalHierarchy.h:362
uint32_t uint32
Definition: Define.h:150
G3D::Vector3 hi
Definition: BoundingIntervalHierarchy.h:60
bool left(const int *a, const int *b, const int *c)
Definition: RecastContour.cpp:487
#define TC_COMMON_API
Definition: Define.h:116
Definition: BoundingIntervalHierarchy.h:345
G3D::AABox * primBound
Definition: BoundingIntervalHierarchy.h:348
const Point3 & origin() const
Definition: Ray.h:56
void createNode(std::vector< uint32 > &tempTree, int nodeIndex, uint32 left, uint32 right) const
Definition: BoundingIntervalHierarchy.h:390
static void subdivide(BVItem *items, int nitems, int imin, int imax, int &curNode, dtBVNode *nodes)
Definition: DetourNavMeshBuilder.cpp:114
uint32 * indices
Definition: BoundingIntervalHierarchy.h:347
void intersectPoint(const G3D::Vector3 &p, IsectCallback &intersectCallback) const
Definition: BoundingIntervalHierarchy.h:260
uint32_t uint32
Definition: g3dmath.h:168
void build(const PrimArray &primitives, BoundsFunc &getBounds, uint32 leafSize=3, bool printStats=false)
Definition: BoundingIntervalHierarchy.h:84
#define MAX_STACK_SIZE
Definition: BoundingIntervalHierarchy.h:34
BIH()
Definition: BoundingIntervalHierarchy.h:82
const Vector3 & direction() const
Definition: Ray.h:61
Definition: BoundingIntervalHierarchy.h:70
void intersectRay(const G3D::Ray &r, RayCallback &intersectCallback, float &maxDist, bool stopAtFirst=false) const
Definition: BoundingIntervalHierarchy.h:121
int maxDepth
Definition: BoundingIntervalHierarchy.h:369
std::vector< uint32 > tree
Definition: BoundingIntervalHierarchy.h:341
int sumObjects
Definition: BoundingIntervalHierarchy.h:364