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);
}
// finds the intersection between the closer plane in one direction
static bool findIntersection(float origin, float direction, float corner, float size, float* t) {
// 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& distance) {
if (direction > EPSILON) {
*t = (corner - origin) / direction;
distance = (corner - origin) / direction;
return true;
} else if (direction < -EPSILON) {
*t = (corner + size - origin) / direction;
distance = (corner + size - origin) / direction;
return true;
}
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
if (contains(origin)) {
*t = 0;
distance = 0;
return true;
}
// check each direction
float nt;
if (findIntersection(origin.x, direction.x, _corner.x, _size.x, &nt) && nt >= 0 &&
isWithin(origin.y + nt*direction.y, _corner.y, _size.y) &&
isWithin(origin.z + nt*direction.z, _corner.z, _size.z) ||
findIntersection(origin.y, direction.y, _corner.y, _size.y, &nt) && nt >= 0 &&
isWithin(origin.x + nt*direction.x, _corner.x, _size.x) &&
isWithin(origin.z + nt*direction.z, _corner.z, _size.z) ||
findIntersection(origin.z, direction.z, _corner.z, _size.z, &nt) && nt >= 0 &&
isWithin(origin.y + nt*direction.y, _corner.y, _size.y) &&
isWithin(origin.x + nt*direction.x, _corner.x, _size.x)) {
*t = nt;
// check each axis
float axisDistance;
if (findIntersection(origin.x, direction.x, _corner.x, _size.x, axisDistance) && axisDistance >= 0 &&
isWithin(origin.y + axisDistance*direction.y, _corner.y, _size.y) &&
isWithin(origin.z + axisDistance*direction.z, _corner.z, _size.z) ||
findIntersection(origin.y, direction.y, _corner.y, _size.y, axisDistance) && axisDistance >= 0 &&
isWithin(origin.x + axisDistance*direction.x, _corner.x, _size.x) &&
isWithin(origin.z + axisDistance*direction.z, _corner.z, _size.z) ||
findIntersection(origin.z, direction.z, _corner.z, _size.z, axisDistance) && axisDistance >= 0 &&
isWithin(origin.y + axisDistance*direction.y, _corner.y, _size.y) &&
isWithin(origin.x + axisDistance*direction.x, _corner.x, _size.x)) {
distance = axisDistance;
return true;
}
return false;

View file

@ -36,7 +36,7 @@ public:
const glm::vec3& getSize() const { return _size; };
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:
glm::vec3 _corner;

View file

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

View file

@ -62,7 +62,7 @@ public:
void clearDirtyBit() { _isDirty = false; };
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:
int encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEncodeLevel,