Skip to main content

Triangle bounding boxes in a single byte

Just thought of a way to store the bounding box for a single triangle in only one byte. It's not really practical or something you'd ever really want to use, but what the hell.

Assume we have some kind of indexed mesh structure with a list of vertex positions and a list of triangle indices:

  struct Mesh {
    std::vector<vec3> verts;
    std::vector<uvec3> triangles;
  };

We can find the bounding box of a triangle by taking the min and max of all three vertices:

  vec3 Mesh::lowerBound(uint32_t tri) const {
    vec3 v0 = verts[triangles[tri].x];
    vec3 v1 = verts[triangles[tri].y];
    vec3 v2 = verts[triangles[tri].z];
    return min(min(v0, v1), v2);
  }

  vec3 Mesh::upperBound(uint32_t tri) const {
    vec3 v0 = verts[triangles[tri].x];
    vec3 v1 = verts[triangles[tri].y];
    vec3 v2 = verts[triangles[tri].z];
    return max(max(v0, v1), v2);
  }

This is nice and simple and probably way better than what I'm about to suggest.

We can store a byte that tells us which of the three vertices defines the lower and upper bounds along each axis. In other words, for each of the x, y and z axes it tells us to get the lower bound value from vertex i and the upper bound value from vertex j. This avoids the need for the min() and max() calls, but replaces it with some division and modulus operations, so it's probably not a performance optimisation.

For component k of each vertex there are 4 possibilities:

  1. v[k] is the lower bound value for axis k.
  2. v[k] is the upper bound value for axis k.
  3. v[k] is neither the upper or lower bound value for axis k.
  4. v[k] is both the upper and lower bound value for axis k.

If v[k] is both the upper and lower bound, it means that the triangle is exactly perpendicular to axis k. In this case all three vertices will have the same value for axis k, which means we can choose any of them for the upper bound and any of them for the lower bound. As long as we make sure we choose a different vertex for each, we can avoid having to represent condition #4.

So that means along any given axis k, we have 6 possible permutations of conditions 1-3 above:

  • LHN
  • LNH
  • HLN
  • HNL
  • NLH
  • NHL
Where the first letter is the condition that holds for vertex v0, the second is the condition for v1 and the third is the condition for v3. The letter 'L' means this vertex is where the lower bound comes from, 'H' means it's the upper bound and 'N' means it's neither.

6 possibilities for each axis and 3 axes means we have 6x6x6 = 216 total possibilities. That's less than 256 so we can fit it all into a byte.

We can encode the per-axis possibilities into a pair of 6-element lookup tables:

  static const uint8_t kLow[6] = { 0, 0, 1, 2, 1, 2 };
  static const uint8_t kHigh[6] = { 1, 2, 0, 0, 2, 1 };

And we can store the bounding box as a single byte per triangle, so our mesh structure becomes:


  struct Mesh {
    std::vector<vec3> verts;
    std::vector<uvec3> triangles;
    std::vector<uint8_t> bboxes;
  };

Given all that, we can decode the bounding box like this:

  vec3 Mesh::lowerBound(uint32_t tri) const {
    uint8_t bbox = bboxes[tri];
    uint8_t ix = kLow[bbox % 6];
    uint8_t iy = kLow[(bbox / 6) % 6];
    uint8_t iy = kLow[bbox / 36];
    return vec3(verts[triangles[tri][ix]].x, 
                verts[triangles[tri][iy]].y, 
                verts[triangles[tri][iz]].z);
  }

  vec3 Mesh::upperBound(uint32_t tri) const {
    uint8_t bbox = bboxes[tri];
    uint8_t ix = kHigh[bbox % 6];
    uint8_t iy = kHigh[(bbox / 6) % 6];
    uint8_t iy = kHigh[bbox / 36];
    return vec3(verts[triangles[tri][ix]].x, 
                verts[triangles[tri][iy]].y, 
                verts[triangles[tri][iz]].z);
  }

Encoding the bounding box is left as an exercise for the reader. :-)

So when would you use this? Well, probably never. If the min and max operations were extremely slow compared to the arithmetic operations and memory lookups but I think you'd be very hard pressed to find a platform where this is actually the case. It's an interesting thought exercise though.

Comments

  1. Nice. Always pushing the, ahem, boundaries, Vil ;)

    ReplyDelete
    Replies
    1. Groan. Get back in your box. :-) (Pun entirely intended)

      Delete

Post a Comment

Popular posts from this blog

How to outperform std::vector in 1 easy step

Everyone who's familiar with C++ knows that you should avoid resizing a std::vector inside a loop wherever possible. The reasoning's pretty obvious: the memory allocated for the vector doubles in size each time it fills up and that doubling is a costly operation. Have you ever wondered why it's so costly though?

It's tempting to assume that because implementations of the STL have been around for so long that they must be pretty efficient. It turns out that's a bad assumption because the problem, in this case, is the standard itself: specifically, the allocator interface.

The allocator interface provides two methods that obtain and release memory:

allocate allocates uninitialized storage
(public member function)deallocate deallocates storage
(public member function)

(taken from this page).

What's missing is away of growing an existing memory allocation in place. In C this is provided by the realloc function, but there's no equivalent in the std::allocator interfa…

OpenGL ES and occlusion queries

This is a follow-up to my earlier post "WebGL doesn't have query objects".

Since I wrote that post, the situation has changed a bit. It's still true to say that WebGL doesn't have query objects, but the underlying reason - that OpenGL ES doesn't - is no longer true.

For OpenGL ES 2.0, there's an extension which provides basic query functionality: EXT_occlusion_query_boolean (which seems to have been based on ARB_occlusion_query2 from regular OpenGL). For OpenGL ES 3.0, the functionality from that extension appears to have been adopted into the standard. The extension provides two query types, both of which set a boolean value to indicate whether any pixels passed the depth and stencil tests.

While this is progress, unfortunately it's still not sufficient to implement the pixel accurate collision detection method I described in an earlier post. For that purpose it's not enough to know whether any pixels passed the tests; you want to know whether al…

Octree node identifiers

Let's say we have an octree and we want to come up with a unique integer that can identify any node in the tree - including interior nodes, not just leaf nodes. Let's also say that the octree has a maximum depth no greater than 9 levels, i.e. the level containing the leaf nodes divides space into 512 parts along each axis.

The encoding The morton encoding of a node's i,j,k coordinates within the tree lets us identify a node uniquely if we already know it's depth. Without knowing the depth, there's no way to differentiate between cells at different depths in the tree. For example, the node at depth 1 with coords 0,0,0 has exactly the same morton encoding as the node at depth 2 with coords 0,0,0.

We can fix this by appending the depth of the node to the morton encoding. If we have an octree of depth 9 then we need up to 27 bits for the morton encoding and 4 bits for the depth, which still fits nicely into a 32-bit integer. We'll shift the morton code up so that i…