Graphics Reference
In-Depth Information
to be able to make an early exit out of a recursive test query. In C (and to a lesser extent
in C++) another option is to use the library function setjmp() to exit a recursion. The
test query version of the recursive generic informed depth-first traversal code can
now be written as:
#include <setjmp.h>
jmp_buf gJmpBuf;
int BVHTestCollision(BVTree a, BVTree b)
{
int r = setjmp(gJmpBuf);
if (r == 0) {
BVHTestCollisionR(a, b);
return 0;
} else returnr-1;
}
// Generic recursive BVH traversal code
// assumes that leaves too have BVs
void BVHTestCollisionR(BVTree a, BVTree b)
{
if (!BVOverlap(a, b))
longjmp(gJmpBuf,0+1); /* false */
if (IsLeaf(a) && IsLeaf(b)) {
if (PrimitivesOverlap(a, b))
longjmp(gJmpBuf,1+1); /* true */
} else {
if (DescendA(a, b)) {
BVHTestCollisionR(a- > left, b);
BVHTestCollisionR(a- > right, b);
} else {
BVHTestCollisionR(a, b- > left);
BVHTestCollisionR(a, b- > right);
}
}
}
Note that as longjmp() cannot return a value of 0 it also cannot return a bool value of
false. To work around this slight complication, a small value (here 1) must be added
to the returned value, to be subtracted off in the main routine. In C++, instead of
using setjmp() a more appropriate choice might be to use exception handling to
accomplish the same thing.
 
Search WWH ::




Custom Search