Skip to content

Commit

Permalink
Amend review feedback
Browse files Browse the repository at this point in the history
- Remove ReciprocalSafe as division by zero is an intended feature of computing the inverse of the ray direction
  • Loading branch information
sturnclaw committed Oct 7, 2024
1 parent 7472199 commit 184fd42
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/collider/BVHTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void SingleBVHTreeBase::Build(const AABBd &bounds, AABBd *objAabbs, uint32_t num
// compute a remapping term to express object positions with highest precision using single floating point
// use the average of the bounding volume to remap into [-1 .. 1] space
m_boundsCenter = (bounds.max + bounds.min) * 0.5;
vector3d inv_scale = (bounds.max - m_boundsCenter).ReciprocalSafe();
vector3d inv_scale = 1.0 / (bounds.max - m_boundsCenter);
m_inv_scale_factor = (inv_scale.x + inv_scale.y + inv_scale.z) / 3.0;

// Build a vector of sort keys for individual nodes (position within system)
Expand Down
4 changes: 3 additions & 1 deletion src/collider/Geom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ void Geom::CollideEdgeTris(std::vector<CollisionContact> &contacts, const matrix
isect.triIdx = -1;

isect_buf.clear();
b->GetGeomTree()->GetTriTree()->TraceRay(v1, dir.ReciprocalSafe(), edge.len, isect_buf, triNode);
// Taking the reciprocal of the direction computes the inverse of the vector.
// Division by zero is intended and correct in this situation.
b->GetGeomTree()->GetTriTree()->TraceRay(v1, 1.0 / dir, edge.len, isect_buf, triNode);

// TODO
// This is subtly dependent on overwriting intersections with the last triangle to be processed
Expand Down
3 changes: 2 additions & 1 deletion src/collider/GeomTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ GeomTree::GeomTree(Serializer::Reader &rd)
void GeomTree::TraceRay(const vector3f &start, const vector3f &dir, isect_t *isect) const
{
std::vector<uint32_t> tri_isect;
m_triTree->TraceRay(vector3d(start), vector3d(dir).ReciprocalSafe(), FLT_MAX, tri_isect);
// Division by zero is intended and produces correct results in this case
m_triTree->TraceRay(vector3d(start), 1.0 / vector3d(dir), FLT_MAX, tri_isect);

for (uint32_t triIdx : tri_isect) {
RayTriIntersect(1, start, &dir, triIdx * 3, isect);
Expand Down
9 changes: 0 additions & 9 deletions src/vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,6 @@ class alignas(sizeof(T)) vector3 {
}
}

// Compute the reciprocal of the vector (1.0 / v), avoiding division by zero
vector3 ReciprocalSafe() const
{
return vector3(
x == 0.0 ? 0.0 : 1.0 / x,
y == 0.0 ? 0.0 : 1.0 / y,
z == 0.0 ? 0.0 : 1.0 / z);
}

void Print() const { printf("v(%f,%f,%f)\n", x, y, z); }

/* Rotate this vector about point o, in axis defined by v. */
Expand Down

0 comments on commit 184fd42

Please sign in to comment.