Skip to content

Commit

Permalink
Address warnings and review feedback
Browse files Browse the repository at this point in the history
- Resolve mixed-signs warnings where feasible
- Introduce new vector3::ReciprocalSafe() method to compute 1.0 / vector without introducing NaNs
- Fix initialization order warning in CollisionContact.h
  • Loading branch information
Web-eWorks committed Aug 13, 2024
1 parent e6740fe commit ceef715
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/collider/BVHTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ void SingleBVHTreeBase::Build(const AABBd &bounds, AABBd *objAabbs, uint32_t num
m_nodes.reserve(2 * numObjs + 1);

// compute a remapping term to express object positions with highest precision using single floating point
// use the average of the bounding volue to remap into [-1 .. 1] space
// use the average of the bounding volume to remap into [-1 .. 1] space
m_boundsCenter = (bounds.max + bounds.min) * 0.5;
vector3d inv_scale = 1.0 / (bounds.max - m_boundsCenter);
vector3d inv_scale = (bounds.max - m_boundsCenter).ReciprocalSafe();
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 Expand Up @@ -324,7 +324,7 @@ float BinnedAreaBVHTree::FindPivot(SortKey *keys, uint32_t numKeys, const AABBd
bins[bin_idx].bounds.Update(objAabbs[keys[i].index]);
}

constexpr size_t NUM_PLANES = NUM_BINS - 1;
constexpr int NUM_PLANES = NUM_BINS - 1;

float planeCost[NUM_PLANES];
float bestCost = FLT_MAX;
Expand Down
4 changes: 2 additions & 2 deletions src/collider/CollisionContact.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ struct CollisionContact {
distance(0),
timestep(0),
triIdx(-1),
geomFlag(0),
userData1(nullptr),
userData2(nullptr),
geomFlag(0)
userData2(nullptr)
{}

// ctor for collision with terrain
Expand Down
8 changes: 4 additions & 4 deletions src/collider/Geom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void Geom::CollideSphere(Sphere &sphere, void (*callback)(CollisionContact *)) c
std::vector<CollisionContact> Geom::Collide(Geom *b) const
{
PROFILE_SCOPED()
int max_contacts = MAX_CONTACTS;
size_t max_contacts = MAX_CONTACTS;
matrix4x4d transTo;

std::vector<CollisionContact> contacts;
Expand Down Expand Up @@ -108,7 +108,7 @@ static AABBd rotateAaabb(const AABBd &a, const matrix4x4d transA)
* Intersect this Geom's edge BVH tree with geom b's triangle BVH tree.
* Generate collision contacts.
*/
void Geom::CollideEdgesWithTrisOf(std::vector<CollisionContact> &contacts, int maxContacts, const Geom *b, const matrix4x4d &transTo) const
void Geom::CollideEdgesWithTrisOf(std::vector<CollisionContact> &contacts, size_t maxContacts, const Geom *b, const matrix4x4d &transTo) const
{
PROFILE_SCOPED()
struct stackobj {
Expand Down Expand Up @@ -197,7 +197,7 @@ void Geom::CollideEdgeTris(std::vector<CollisionContact> &contacts, const matrix
isect.triIdx = -1;

isect_buf.clear();
b->GetGeomTree()->GetTriTree()->TraceRay(v1, 1.0 / dir, edge.len, isect_buf, triNode);
b->GetGeomTree()->GetTriTree()->TraceRay(v1, dir.ReciprocalSafe(), edge.len, isect_buf, triNode);

// TODO
// This is subtly dependent on overwriting intersections with the last triangle to be processed
Expand All @@ -211,7 +211,7 @@ void Geom::CollideEdgeTris(std::vector<CollisionContact> &contacts, const matrix
return;

// in world coords
CollisionContact contact;
CollisionContact contact = {};
contact.pos = b->GetTransform() * (v1 + dir * double(isect.dist));
contact.normal = vector3d(b->m_geomtree->GetTriNormal(isect.triIdx));
contact.normal = b->GetTransform().ApplyRotationOnly(contact.normal);
Expand Down
2 changes: 1 addition & 1 deletion src/collider/Geom.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Geom {

private:
// Note: these functions could easily be made static and extracted into a separate collision system
void CollideEdgesWithTrisOf(std::vector<CollisionContact> &contacts, int maxContacts, const Geom *b, const matrix4x4d &transTo) const;
void CollideEdgesWithTrisOf(std::vector<CollisionContact> &contacts, size_t maxContacts, const Geom *b, const matrix4x4d &transTo) const;
void CollideEdgeTris(std::vector<CollisionContact> &contacts, const matrix4x4d &transToB, const Geom *b, uint32_t edgeIdx, uint32_t triIdx, std::vector<uint32_t> &isect_buf) const;

// double-buffer position so we can keep previous position
Expand Down
2 changes: 1 addition & 1 deletion src/collider/GeomTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ 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(1.f / dir), FLT_MAX, tri_isect);
m_triTree->TraceRay(vector3d(start), vector3d(dir).ReciprocalSafe(), FLT_MAX, tri_isect);

for (uint32_t triIdx : tri_isect) {
RayTriIntersect(1, start, &dir, triIdx * 3, isect);
Expand Down
9 changes: 9 additions & 0 deletions src/vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ 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 ceef715

Please sign in to comment.