Pointers to references for in/out parameters, renamed line parameter to more

descriptive "distance."
This commit is contained in:
Andrzej Kapolka 2013-05-04 07:56:30 -07:00
parent ff4e21e504
commit 34565a4956
4 changed files with 29 additions and 29 deletions

View file

@ -80,37 +80,37 @@ bool AABox::contains(const glm::vec3& point) const {
isWithin(point.z, _corner.z, _size.z); isWithin(point.z, _corner.z, _size.z);
} }
// finds the intersection between the closer plane in one direction // finds the intersection between a ray and the facing plane on one axis
static bool findIntersection(float origin, float direction, float corner, float size, float* t) { static bool findIntersection(float origin, float direction, float corner, float size, float& distance) {
if (direction > EPSILON) { if (direction > EPSILON) {
*t = (corner - origin) / direction; distance = (corner - origin) / direction;
return true; return true;
} else if (direction < -EPSILON) { } else if (direction < -EPSILON) {
*t = (corner + size - origin) / direction; distance = (corner + size - origin) / direction;
return true; return true;
} }
return false; return false;
} }
bool AABox::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float* t) const { bool AABox::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance) const {
// handle the trivial case where the box contains the origin // handle the trivial case where the box contains the origin
if (contains(origin)) { if (contains(origin)) {
*t = 0; distance = 0;
return true; return true;
} }
// check each direction // check each axis
float nt; float axisDistance;
if (findIntersection(origin.x, direction.x, _corner.x, _size.x, &nt) && nt >= 0 && if (findIntersection(origin.x, direction.x, _corner.x, _size.x, axisDistance) && axisDistance >= 0 &&
isWithin(origin.y + nt*direction.y, _corner.y, _size.y) && isWithin(origin.y + axisDistance*direction.y, _corner.y, _size.y) &&
isWithin(origin.z + nt*direction.z, _corner.z, _size.z) || isWithin(origin.z + axisDistance*direction.z, _corner.z, _size.z) ||
findIntersection(origin.y, direction.y, _corner.y, _size.y, &nt) && nt >= 0 && findIntersection(origin.y, direction.y, _corner.y, _size.y, axisDistance) && axisDistance >= 0 &&
isWithin(origin.x + nt*direction.x, _corner.x, _size.x) && isWithin(origin.x + axisDistance*direction.x, _corner.x, _size.x) &&
isWithin(origin.z + nt*direction.z, _corner.z, _size.z) || isWithin(origin.z + axisDistance*direction.z, _corner.z, _size.z) ||
findIntersection(origin.z, direction.z, _corner.z, _size.z, &nt) && nt >= 0 && findIntersection(origin.z, direction.z, _corner.z, _size.z, axisDistance) && axisDistance >= 0 &&
isWithin(origin.y + nt*direction.y, _corner.y, _size.y) && isWithin(origin.y + axisDistance*direction.y, _corner.y, _size.y) &&
isWithin(origin.x + nt*direction.x, _corner.x, _size.x)) { isWithin(origin.x + axisDistance*direction.x, _corner.x, _size.x)) {
*t = nt; distance = axisDistance;
return true; return true;
} }
return false; return false;

View file

@ -36,7 +36,7 @@ public:
const glm::vec3& getSize() const { return _size; }; const glm::vec3& getSize() const { return _size; };
bool contains(const glm::vec3& point) const; bool contains(const glm::vec3& point) const;
bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float* t) const; bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance) const;
private: private:
glm::vec3 _corner; glm::vec3 _corner;

View file

@ -551,8 +551,8 @@ class RayArgs {
public: public:
glm::vec3 origin; glm::vec3 origin;
glm::vec3 direction; glm::vec3 direction;
VoxelNode** node; VoxelNode*& node;
float* t; float& distance;
bool found; bool found;
}; };
@ -560,24 +560,24 @@ bool findRayOperation(VoxelNode* node, void* extraData) {
RayArgs* args = static_cast<RayArgs*>(extraData); RayArgs* args = static_cast<RayArgs*>(extraData);
AABox box; AABox box;
node->getAABox(box); node->getAABox(box);
float t; float distance;
if (!box.findRayIntersection(args->origin, args->direction, &t)) { if (!box.findRayIntersection(args->origin, args->direction, distance)) {
return false; return false;
} }
if (!node->isLeaf()) { if (!node->isLeaf()) {
return true; // recurse on children return true; // recurse on children
} }
if (!args->found || t < *(args->t)) { if (!args->found || distance < args->distance) {
*(args->node) = node; args->node = node;
*(args->t) = t; args->distance = distance;
args->found = true; args->found = true;
} }
return false; return false;
} }
bool VoxelTree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode** node, float* t) bool VoxelTree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode*& node, float& distance)
{ {
RayArgs args = { origin / (float)TREE_SCALE, direction, node, t }; RayArgs args = { origin / (float)TREE_SCALE, direction, node, distance };
recurseTreeWithOperation(findRayOperation, &args); recurseTreeWithOperation(findRayOperation, &args);
return args.found; return args.found;
} }

View file

@ -62,7 +62,7 @@ public:
void clearDirtyBit() { _isDirty = false; }; void clearDirtyBit() { _isDirty = false; };
unsigned long int getNodesChangedFromBitstream() const { return _nodesChangedFromBitstream; }; unsigned long int getNodesChangedFromBitstream() const { return _nodesChangedFromBitstream; };
bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode** node, float* t); bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelNode*& node, float& distance);
private: private:
int encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEncodeLevel, int encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEncodeLevel,