Title: Acceleration Structures
1Acceleration Structures
- CS 148 Introduction to Computer Graphics and
Imaging
Image "BL Object 5" by Douglas Eichenberg (2003)
2Ray-Scene Intersection
Traversing all primitives in the scene for each
ray is inefficient! Need auxiliary structures to
accelerate this process.
3Acceleration Structures for Vertex Normals (for
rendering)
- for each Vertex v in V
- for each Triangle t in T
- if Triangle t contains Vertex v
- inner_loop(v,t)
- O(T V) running time
- for each Triangle t in T
- for each v in t.v1, t.v2, t.v3
- v.relevant_triangles.append(t)
- for each Vertex v in V
- for each t in v.relevant_triangles
- inner_loop(v,t)
- O(TV) running time
insert
Data Structure
retrieve
4Acceleration Structures for Ray Tracing
- Two basic ideas
- Partition the objects
- Flat bounding volumes
- Hierarchical bounding volume hierarchies (BVH)
- Partition the space
- Flat Uniform Grid
- Hierarchical Octree, Kd-Tree
Uniform Grid
BVH
Bounding Volumes
Kd-Tree
5How to evaluate an acceleration structure?
- How fast is it to construct the structure?
- Building a hierarchy for n primitives takes
O(nlogn) time - Inserting n primitives into a uniform grid takes
O(n) time - How much memory will it use?
- A tree for n primitives takes O(n) space
- A uniform grid subdividing the space takes
O(nxnynz) space - How fast is it for a ray to traverse in the
structure? - Root to Leaf traversal is O(log n)
- Need (efficient) methods for finding neighbors in
both hierarchies and flat data structures
6Bounding Volume
- Enclosing each object with a simpler piece of
geometry - Sphere, box (axis-aligned, object-aligned)
- If the ray does not hit the bounding volume, it
cannot hit the enclosed object - Avoid expensive intersection test with the object
7Bounding Sphere
- A sphere enclosing all of the vertices of the
object. - Fastest bounding volume for intersection test
with a ray. - Minimize the radius of the bounding sphere
- For a triangle or a tetrahedron, the minimal
radius is simply the radius of its circumsphere. - How about more complicated geometry?
8Constructing Bounding Sphere
- An intuitive way
- Compute the centroid PC of all vertices
- Loop through all vertices and compute the maximal
Pi-PC2 to find the farthest vertex Pfarthest
from PC - Return Pfarthest-PC as the minimal radius
- A counter example a crowd of points with one
discrete - point far away, the radius calculated in this
way is about - twice as big as the minimal one.
- A hierarchical way
- Build the bounding sphere for each triangle
- Recursively merge the neighboring spheres
- into a larger one until all the spheres are
merged - into one sphere
- Return the radius of that sphere as the minimal
radius - Optimal methods
- Randomized Linear programming, running in
expected O(n) time.
9Pros and Cons of Bounding Sphere
- Advantages
- Constructing a bounding sphere is fast.
- Ray-sphere intersection test is easy.
- No need to change when object rotates
- Disadvantages
- Cannot tightly enclose the object in some cases,
e.g., long and thin objects, - which leads to false positives
10Axis-Aligned Bounding Box (AABB)
- An axis-aligned box containing the object.
- Constructing an AABB is trivial
- Loop through all of the vertices and find the min
and max values in each dimension separately. - Use the min/max values as the bottom-left/top-righ
t corners of the bounding box.
11Ray-Box (Axis Aligned) Intersection
- A box is the intersection of 3 pairs of slabs in
3 dimensions - Each slab contains two parallel planes and the
space between them - We can detect the intersection between the ray
and the box by detecting the intersections
between the ray and the three pairs of slabs. - For each pair of slabs there are two intersection
points Pnear and Pfar, and there are 6
intersection points in total for 3 pairs. - If the maximal Pnear is ahead of the minimal Pfar
on the ray, the ray misses the box. Otherwise it
hits the box.
12Pseudocode
- float RayBoxIntersection (Ray ray, Box box,
Interval tmin, tmax) -
- calculate tnear,x , tfar,x , tnear,y , tfar,y ,
tnear,z , tfar,z on 3 axes - tnearmax(tnear,x , tnear,y , tnear,z)
- tfarmin(tfar,x , tfar,y , tfar,z)
- if(tnearlttfar tneargttmin tnearlttmax)
- return tnear ////report intersection at tnear
-
- else
- return -1 ////no intersection
-
13Pros and Cons of AABB
- Advantages
- Constructing an AABB is simple.
- Simply scan over all vertices and find min and
max values in each dimension in O(n) time - Ray-box intersection test is fast.
- Disadvantages
- Need to recalculate the bounding box any time an
object rotates (unless the ray is transformed
into object space) - Cannot tightly enclose the object (similar to
spheres)
14Oriented Bounding Box (OBB)
- The orientation of the box depends on the
orientation of the object - Dont need to recompute the box when an object
rotates - Can pre-compute OBB in object space and transform
OBB to world space with the object (same is true
for spheres)
15Constructing OBB
- How to compute the orientation of the object?
- Singular Value Decomposition
- Compute the covariance matrix and the eigenvalues
and the corresponding eigenvectors of the 3x3
matrix - Using these eigenvectors as the basis of the
local coordinate system of the bounding box
16Constructing an OBB for a Triangle Mesh
- Compute the mean centroid of all triangles
-
- where pi, qi, and ri are the vertices of ith
triangle, and n is the number of triangles. - Construct the 3x3 covariance matrix C, the
element Cjk is computed as -
-
- where and
- Calculate the eigenvectors e1, e2, e3 of C and
take them as the basis vectors for the local
coordinate system of the OBB. - All of the eigenvectors are mutually orthogonal
since C is symmetric. - Find the extremal vertices along each basis
vector and resize the bounding box to bound those
vertices (similar to the AABB)
Gottschalk et.al. 96
17Ray-Box (Object Oriented) Intersection
- Similar to Ray-AABB intersection
- Calculate the maximum tnear and the minimum tfar
- The planes of the slabs are not axis aligned any
more - Recall how to compute intersection between a ray
and an arbitrary plane in 3D space
Ray-AABB intersection
Ray-OBB intersection
18Or we can transform the ray
- Transform the ray into the OBB coordinate system
and perform ray-AABB intersection test.
World Coordinate
OBB Coordinate
19Pros and Cons of OBB
- Advantages
- Fit the object tighter than AABB
- Disadvantages
- Extra cost for ray-box intersection
- Finding an OBB is computationally expensive (but
is done as a precomputation) - Finding the minimal OBB is hard (but an
approximation such as the one we just proposed
- is usually good enough)
20Bounding Volume Hierarchies (BVH)
- A bounding volume hierarchy is a tree
- Each leaf encloses a primitive object
- Each interior node encloses all the bounding
volumes of its children
21Constructing the BVH
- Bottom up
- Begin with the bounding volume for each primitive
- Recursively merge them into larger volumes
according to some criteria - E.g., merge nearest neighbors, minimizing the sum
of surface area - Stop in case of a single bounding volume at the
root - Top down
- Begin with the group of all primitives and its
bounding volume - Recursively split primitives and the
corresponding bounding volume according to some
criteria - E.g., split primitives w.r.t coordinate axis,
minimizing the sum of surface area - Stop when all leaf nodes are indivisible.
22Example Splitting Strategies in Constructing
OBB Tree to Bound a Object
- Split using center points along the longest axis
of object bounded with OBB
Gottschalk et.al. 96
Kamat and Martinez, 2007
23Where/When to create the BVHs?
- Create the BVH individually for each object
- The BVH is in object space
- Can be translated with the object and does not
need to be updated - Create BVH for unioning together all BVs of all
objects in a scene - This global BVH is in the world space
- Must be updated whenever something moves in the
scene - Do both!
24Ray Traversal in BVH
- Use hierarchy to accelerate ray intersections
- Intersect node contents only if the ray
intersects the bounding volume
25How to backtrack the ray in BVH?
- Depth-first search
- Find the nearest intersection inside a node by
recursively finding the intersections of all its
subnodes and return the minimum t. - How to improve the efficiency?
To report ray-primitive in node C, we need to
recursively traverse all its subnodes (D, G, H,
I) to find all the ray-primitive intersections
(triangles in G and I) and report the minimum one
(triangle in G).
26Early Termination in Ray Traversal
- Sort hits detect early termination
- Sort the intersections of the ray and the child
node bounding volumes and terminate when the
first ray-primitive intersection is found.
27Early Termination in Ray Traversal
To report ray-primitive in node C, we can avoid
testing triangles in H and I by recursively
sorting the intersections of the ray and the
subnodes of C (D and I of C, and subnodes G and H
of D). Searching terminates when it finds the
first triangle (in G) in the sorted nodes.
28Some cases that BVH doesnt work well
- Objects with similar sizes are approximately
uniformly distributed in space - BVH has an extra log n constant that is not
warranted in this case i.e. not enough
successful pruning occurs
"Warm Up" by Norbert Kern (2001)
Shorebirds by Jim Charter (2000)
http//hof.povray.org/warm_up.html
http//hof.povray.org/shorebir.html
29Partition the space instead
- Instead of using the objects to divide space just
divide all of space and register objects with the
cells they (or their bounding volumes) overlap
30Uniform Grids
- Divide 3D space into nxnynz axis-aligned grid
cells - Perform Ray-object intersection test only when
the ray hits the grid cell containing the object - The size of the grid cell is crucial to the
performance - No speedup if the cell size is too big, no
pruning, everything in one cell - A lot of empty cells if the cell size is too
small - A practical way to choose cell size is to average
the edge lengths of the bounding boxes of all
primitives
31Constructing a Uniform Grid
- Find the bounding box of the scene
- Initialize the grid with that bounding box and a
proper cell size - Each cell maintains a list (or an array) to store
the overlapped primitives - Insert primitives into cells
- One primitive may be inserted into multiple cells
32Ray Traversal in a Uniform Grid
- Traverse all the cells pierced by the ray before
the ray intersects with a primitive or reaches
the boundary of the bounding box
33Incremental Traversal
- An incremental algorithm similar to line
rasterization - From the current intersection point P on the face
of cell (i, j, k), perform ray-plane intersection
tests with the next 3 grid planes along the ray
direction to get the 3 candidate intersection
points for the next intersection. - The next intersection point is the nearest one
among the 3 candidates. - Update the cell index according to the new
intersection point. Perform ray-primitive
intersection tests in the new cell. - Repeat the above process until the ray intersects
with a primitive or reaches the boundary of the
bounding box.
34Improving the Algorithm
- Efficiently finding the next intersection point
on the grid - The intersections with the grid planes have the
same spacing in each independent dimension - Using the precomputed dtx, dty, and dtz, we dont
need to perform ray-plane intersection tests
every time. - dtx Cx/Dx, dty Cy/Dy, dtz Cz/Dz, in which (Cx,
Cy, Cz) is the cell size and (Dx, Dy, Dz) is the
ray direction.
35Pseudocode
- ////tnext denotes the t for the 3 candidate
intersections on planes in 3 dimensions, - ////it is initialized based on the first cell
pierced by the ray in the grid - Initialize tnext
- ////s is a 3d (integer) vector denoting the
direction for incrementing cell index along the
ray, - ////for each direction, if(ray.dirdgt0) sd1 else
sd-1 - Initialize s
- void IncrementalRayTraverse(Ray ray, Vec3 cell,
Vec3 tnext) -
- float tintersect min(tnext,x tnext,y tnext,z)
- int d the axis of tintersect ////the face of
intersection - celldsd
- ray.tminray.tmax
- ray.tmax tintersect
- tnext,d dtd
- process ray-primitive intersections in the new
cell - between ray.tmin and ray.tmax
36Avoid Redundant Intersection Test
- A primitive may be stored in multiple cells
- For each cell, an intersection test on that
primitive may be performed - Solution associate each primitive with a bool
- If the primitive has already been determined to
not intersect the ray, or is not the closest
intersecting primitive, store false in the bool - Before the actual ray-primitive intersection
test, check the bool first
37Pros and Cons of the Uniform Grids
- Advantages
- Construction is fast.
- Regular Layout, Cache coherent
- Ray traversal is easy.
- Disadvantages
- Empty cells in a sparse scene will waste memory
- Hard to choose the cell size
- No adaptivity
38Ray Traversal in Viewing Frustum
- Sending rays in the camera space
- Create a uniform grid in the frustum
- Avoid the traversal steps and intersection tests
- Cache coherent, all the cells are aligned along
the ray! - Can traverse a bunch of rays in one time
39Optimizing the Uniform Grid
- Optimizing the storage
- Spatial Hashing use a hash table instead of a 3D
array. - Avoid the extra storage for a large number of
empty cells. - Optimizing the performance
- Adaptive grids rectilinear grid, embedded grid,
octree
Rectilinear Grid
Embedded Grid
Octree (Quadtree)
40Octree
- Each node potentially has exactly 8 children
- Each node can equally subdivide its space (an
AABB) into eight subboxes by 3 midplanes. - Children of a node are contained within the box
of the node itself.
41Constructing an Octree
- In a top-down way
- Find the global bounding box that contains all
the primitives and correspond it to the root of
the tree - Recursively partition a node into 8 octants by 3
midplanes. - If a primitive belongs to multiple octants, put
it in each octant. - Recursion stops when the termination criteria are
satisfied. - e.g., maximum depth, minimum number of primitives
in a node.
42Ray Traversal in an Octree
- Traverse all leaf nodes in the octree passed
through by the ray and perform intersection tests
for the primitives inside those leaves.
43Recursive Traversal
- Basic idea
- For a leaf node, perform intersection test for
its primitives and terminate if an intersection
is detected. - For an interior node, recursively process all the
subnodes inside it. - Two important questions
- How to find the first subbox at which the ray
enters the current box? - How to find the next subbox (or
- report exit) when the ray exits a
- subbox?
44Observation
- For a given ray and a box, the number of its
subboxes intersected with the ray and the
intersection sequence of these subboxes is only
determined by the two intersection points and the
corresponding box faces.
45Find the First Entry Subnode
- First find the entry face by computing
min(tnear,x,tnear,y,tnear,z) - Recall what we did for ray-AABB intersection test
- On each face there are 4 corresponding subboxes,
decide which subbox the ray enters using
midplanes. - Find the midplane is crossed before or after the
entry.
Crossed before the entry
Crossed after the entry
46Find the Next Subnode
- Build an automaton whose states correspond to the
boxes and whose transitions are associated to the
movements between neighboring sub-nodes visited
sequentially by the ray.
47Recursive Traversal Example
48Pros and Cons of the Octree
- Advantages
- Adaptivity
- Memory efficiency
- Disadvantages
- Ray traversal is complicated
- Partition strategy is not flexible
- Not cache coherent
49Hybrid Structure
- Start with a uniform grid and subdivide each node
using hierarchies - Each node of a uniform grid can contain a whole
octree - Improve the performance of the octree no need to
traverse the tree from a single root every time
Losasso et.al. 2006
50K-d Tree
- Using a hyperplane translated in one dimension
51K-d Tree
- A binary tree for space searching
- Every non-leaf node can be thought of as
implicitly generating a splitting hyperplane that
divides the space into two parts - Points to the left of this hyperplane are
represented by the left subtree of that node and
points right of the hyperplane are represented by
the right subtree.
52Constructing a K-d Tree
- In a top-down way
- Begin with the global bounding box containing all
primitives. - Choose an axis and a splitting plane
perpendicular to that plane, subdivide the
primitives on both sides of the plane into two
groups. - Stop when the number of primitives in each single
group is below a threshold.
53Ray Traversal in a K-d Tree
- Traverse all leaf nodes in the k-d tree passed
through by the ray
54Recursive Traversal
- Similar to recursive traversal in an octree
- Each interior node has two children, there are
only 3 cases for subnode intersections - intersecting with the left child only, the right
child only, and both. - The number of intersected subboxes and its
sequence can be determined by the intersection
points and faces of the ray and the node and the
splitting plane of the node. - Notice the difference with an octree the
splitting plane does not have to be in the middle
of the box
55Non-Recursive Traversal
- Recursion is expensive on GPU for real-time ray
tracing. - Kd-Restart
- Restart the traversal to the root every time it
reaches a leaf - Kd-Backtrack
- return to parent nodes that enables traversing
other unvisited nodes - Neighbor-link
- Each leaf stores ropes that directly link it to
the adjacent node via its 6 faces
Popov et.al. 07
56Pros and Cons of the K-d Tree
- Advantages
- Adaptivity
- Fast ray traversal
- Disadvantages
- Construction is complicated
57Partitioning Objects or Space?
- Any of the acceleration structures can be applied
in both object space and world space in the same
manner - When a structure is in the world space, it has to
be updated whenever some object or primitive in
the structure is transformed - When a structure is in the object space, it can
be transformed with the object and does not need
to be updated - E.g., a uniform grid in world space for a bunch
of birds with a BVH in object space for each bird