Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920...

53
Spatial Partitioning Data Structures

Transcript of Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920...

Page 1: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Spatial Partitioning Data Structures

Page 2: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

A Quick Calculation

Number of pixels on screen (1080P):

• 1920 x 1080 = 2,073,600

Page 3: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

A Quick Calculation

Number of pixels on screen (1080P):

• 1920 x 1080 = 2,073,600

Number of triangles

• ~millions

Number of ray-triangle intersections:

• ~10^12 intersections per frame

Page 4: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

A Quick Calculation

Number of pixels on screen (1080P):

• 1920 x 1080 = 2,073,600

Number of triangles

• ~millions

Number of ray-triangle intersections:

• ~10^12 intersections per frame

Now add antialiasing, shadow rays, reflection rays, ……

Page 5: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Boxes

Fit boxes around objects

Page 6: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Boxes

Fit boxes around objects

Check ray-box first

Page 7: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Boxes

Fit boxes around objects

Check ray-box first

Then check objects

Page 8: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Boxes

What if we have a single complex object?

Cut into pieces, treat as separate?

Page 9: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Volume Hierarchy

For points:root

Page 10: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Volume Hierarchy

For points:root

Page 11: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Volume Hierarchy

For points:root

Page 12: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Volume Hierarchy

For points:root

Page 13: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Volume Hierarchy

Top-down approach:

BuildBVH(points P)

if P contains one point

return leaf;

compute bounding box

find longest axis

split points into groups {L, R} along this axis

return { BuildBVH(L), BuildBVH(R) };

Page 14: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Bounding Volume Hierarchy

Bottom-up approach (faster, harder):

• sort along space-fillingfractal (z-order curve)

• implement using bitfiddling (Morton codes)

Page 15: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Traversal

For points:root

Page 16: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Traversal

For points:root

Page 17: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Traversal

For points:root

Page 18: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Traversal

For points:root

Page 19: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Traversal

For points:root

Page 20: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Traversal

For points:root

Page 21: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Analysis

Build time: O(N log^2 N) (top-down)

Traverse time:

Page 22: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Analysis

Build time: O(N log^2 N) (top-down)

Traverse time:

• worst case: O(N)

• typical case: O(log N)

Advanced traversal strategies possible

Page 23: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH in Practice

Build around triangle primitives

leaves are individual triangles

when building, sort by e.g.triangle center

note: nodes can overlap

Page 24: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Node Types

Most typical: AABBs

• “axis-aligned bounding boxes”

Other options possible:

Page 25: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Node Types

Most typical: AABBs

• “axis-aligned bounding boxes”

Other options possible:

• sphere trees

• OBBs (oriented bounding boxes)

Page 26: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Node Types

Most typical: AABBs

• “axis-aligned bounding boxes”

Other options possible:

• sphere trees

• OBBs

• k-DOPs

Page 27: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Node Types

Most typical: AABBs

• “axis-aligned bounding boxes”

Other options possible

Complex tradeoff between

• tightness of fit

• traverse cost

• build cost

• memory usage

Page 28: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

BVH Visualized

Page 29: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Spatial Hashing

Divide space into coarse grid

Each grid cell stores its contents

Page 30: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Spatial Hashing

Divide space into coarse grid

Each grid cell stores its contents

How to build?

Page 31: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Spatial Hashing

Divide space into coarse grid

Each grid cell stores its contents

How to build?

• hash function maps points to their cell

• usually very fast (bit twiddling)

Why useful?

Page 32: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

What if primitives aren’t point?

Spatial Hashing

Page 33: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

What if primitives aren’t point?

Spatial Hashing

must rasterizeobjects to grid

object overlapsmultiple cells--> multiple refs

Page 34: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Spatial Hashing

Pros:

• (relatively) simple to build

• simple data structure (array of pointers)

Cons:

• must pick a good cell size

• works poorly on heterogeneous object distributions

Page 35: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Quadtree

Start with spatial hash

Split crowded cells into child squares

Page 36: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Quadtree

Works also for non-point primitives

Danger – must pick maximum depth

Page 37: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Quadtree

Pros:

• very space-efficient even for heterogeneous object distributions

• simple to build and traverse (bit tricks often used)

Cons:

• must pick max tree depth

• tree not balanced

Page 38: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Octree

3D version of quadtree

Page 39: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:
Page 40: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Binary Space Partition

Recursively split space using planes

Page 41: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Binary Space Partition

Recursively split space using planes

Each node stores splitting plane

Each leaf stores object references

Page 42: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Binary Space Partition

Recursively split space using planes

Each node stores splitting plane

Each leaf stores object references

How to pick good splitting plane?

Page 43: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Binary Space Partition

Recursively split space using planes

Each node stores splitting plane

Each leaf stores object references

How to pick good splitting plane?

• heuristics / black magic

• good partitioning vs good balance

• special case: axis-aligned planes

Page 44: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

kD Tree

“k-Dimensional Tree”

BSP where each node is vertical or horizontal plane

Page 45: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

kD Tree

How to pick splitting plane?

Goals:

• balance area of two children

• balance number of objects in children

• avoid splitting objects

Page 46: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

kD Tree

How to pick splitting plane?

Common strategy: split next to medianobject along longest direction

Page 47: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

3D Tree

Page 48: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

kD Tree

Pros:

• can tailor cell shape to fit objects

• balanced tree

Cons:

• cells not uniformly placed or shaped

• must pick good max tree depth

Page 49: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Devils Lurk in the Details

Building the leaves:

• what is the bounding box? (AABBs)

• is my object inside, outside, or crossing a grid cell? (spatial hash/octree)

• is my object on the left, right, or both sides of the split plane? (BSP/kD tree)

• how do I duplicate object referencescorrectly? (all but BVHs)

Page 50: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Devils Lurk in the Details

Traversing the tree:

• how exactly do I do ray-node intersection?

• ray/box (AABBs, octree)

• ray/plane (BSP and kD trees)

Page 51: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Devils Lurk in the Details

Traversing the tree:

• how exactly do I do ray-node intersection?

• how do I do it efficiently?

• what if my ray starts inside the scene?

Page 52: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Kinetic Data Structures

During animation, objects move slowly

Cumulatively update data structures instead of rebuilding every frame

Page 53: Spatial Partitioning Data StructuresA Quick Calculation Number of pixels on screen (1080P): • 1920 x 1080 = 2,073,600 Number of triangles • ~millions Number of ray-triangle intersections:

Kinetic Data Structures

During animation, objects move slowly

Cumulatively update data structures instead of rebuilding every frame

Easy:

• spatial hash

• octree

Annoying:

• BSP trees (kD trees)