Spatial Data Structures Hierarchical Bounding Volumes Hierarchical - - PowerPoint PPT Presentation

spatial data structures
SMART_READER_LITE
LIVE PREVIEW

Spatial Data Structures Hierarchical Bounding Volumes Hierarchical - - PowerPoint PPT Presentation

Spatial Data Structures Hierarchical Bounding Volumes Hierarchical Bounding Volumes Grids Grids Octrees Octrees BSP Trees BSP Trees 11/7/02 Thursday, April 1, 2010 Speeding Up Computations Ray Tracing Spend a lot of time doing


slide-1
SLIDE 1

Spatial Data Structures

Hierarchical Bounding Volumes Grids Octrees BSP Trees Hierarchical Bounding Volumes Grids Octrees BSP Trees

11/7/02

Thursday, April 1, 2010

slide-2
SLIDE 2

3

Speeding Up Computations

  • Ray Tracing

– Spend a lot of time doing ray object intersection tests

  • Hidden Surface Removal – painters algorithm

– Sorting polygons front to back

  • Collision between objects

– Quickly determine if two objects collide

n2 computations

Spatial data-structures

Thursday, April 1, 2010

slide-3
SLIDE 3

3

Speeding Up Computations

  • Ray Tracing

– Spend a lot of time doing ray object intersection tests

  • Hidden Surface Removal – painters algorithm

– Sorting polygons front to back

  • Collision between objects

– Quickly determine if two objects collide

n2 computations

Spatial data-structures

Thursday, April 1, 2010

slide-4
SLIDE 4

3

Speeding Up Computations

  • Ray Tracing

– Spend a lot of time doing ray object intersection tests

  • Hidden Surface Removal – painters algorithm

– Sorting polygons front to back

  • Collision between objects

– Quickly determine if two objects collide

n2 computations

Spatial data-structures

Thursday, April 1, 2010

slide-5
SLIDE 5

3

Speeding Up Computations

  • Ray Tracing

– Spend a lot of time doing ray object intersection tests

  • Hidden Surface Removal – painters algorithm

– Sorting polygons front to back

  • Collision between objects

– Quickly determine if two objects collide

n2 computations

Spatial data-structures

Thursday, April 1, 2010

slide-6
SLIDE 6

3

Speeding Up Computations

  • Ray Tracing

– Spend a lot of time doing ray object intersection tests

  • Hidden Surface Removal – painters algorithm

– Sorting polygons front to back

  • Collision between objects

– Quickly determine if two objects collide

n2 computations

Spatial data-structures

Thursday, April 1, 2010

slide-7
SLIDE 7

4

Spatial Data Structures

  • We’ll look at

– Hierarchical bounding volumes – Grids – Octrees – K-d trees and BSP trees

  • Good data structures can give speed up ray tracing by 10x or 100x

Thursday, April 1, 2010

slide-8
SLIDE 8

5

Bounding Volumes

  • Wrap things that are hard to check for intersection in things that are

easy to check

– Example: wrap a complicated polygonal mesh in a box – Ray can’t hit the real object unless it hits the box – Adds some overhead, but generally pays for itself.

  • Most common bounding volume types: sphere and box

– box can be axis-aligned or not

  • You want a snug fit!
  • But you don’t want expensive intersection tests!

Good! Bad!

Thursday, April 1, 2010

slide-9
SLIDE 9

5

Bounding Volumes

  • Wrap things that are hard to check for intersection in things that are

easy to check

– Example: wrap a complicated polygonal mesh in a box – Ray can’t hit the real object unless it hits the box – Adds some overhead, but generally pays for itself.

  • Most common bounding volume types: sphere and box

– box can be axis-aligned or not

  • You want a snug fit!
  • But you don’t want expensive intersection tests!

Good! Bad!

Thursday, April 1, 2010

slide-10
SLIDE 10

5

Bounding Volumes

  • Wrap things that are hard to check for intersection in things that are

easy to check

– Example: wrap a complicated polygonal mesh in a box – Ray can’t hit the real object unless it hits the box – Adds some overhead, but generally pays for itself.

  • Most common bounding volume types: sphere and box

– box can be axis-aligned or not

  • You want a snug fit!
  • But you don’t want expensive intersection tests!

Good! Bad!

Thursday, April 1, 2010

slide-11
SLIDE 11

6

Bounding Volumes

  • You want a snug fit!
  • But you don’t want expensive intersection tests!
  • Use the ratio of the object volume to the enclosed volume as a measure
  • f fit.
  • Cost = n*B + m*I

n - is the number of rays tested against the bounding volume B - is the cost of each test (Do not need to compute exact intersection!) m - is the number of rays which actually hit the bounding volume I - is the cost of intersecting the object within

Thursday, April 1, 2010

slide-12
SLIDE 12

7

Bounding Volumes

  • You want a snug fit!
  • But you don’t want expensive intersection tests!
  • Use the ratio of the object volume to the enclosed volume as a measure
  • f fit.
  • Cost = n*B + m*I

n - is the number of rays tested against the bounding volume B - is the cost of each test (Do not need to compute exact intersection!) m - is the number of rays which actually hit the bounding volume I - is the cost of intersecting the object within

Thursday, April 1, 2010

slide-13
SLIDE 13

8

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Thursday, April 1, 2010

slide-14
SLIDE 14

9

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Thursday, April 1, 2010

slide-15
SLIDE 15

10

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Thursday, April 1, 2010

slide-16
SLIDE 16

11

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Thursday, April 1, 2010

slide-17
SLIDE 17

12

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Check intersect root If not return no intersections

Thursday, April 1, 2010

slide-18
SLIDE 18

13

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Check intersect root If intersect check intersect left sub-tree check intersect right sub-tree

Thursday, April 1, 2010

slide-19
SLIDE 19

14

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Check intersect root If intersect check intersect left sub-tree check intersect right sub-tree

Thursday, April 1, 2010

slide-20
SLIDE 20

15

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Check intersect root If intersect check intersect left sub-tree check intersect right sub-tree

Thursday, April 1, 2010

slide-21
SLIDE 21

16

Hierarchical Bounding Volumes

  • Still need to check ray against every object --- O(n)
  • Use tree data structure

– Larger bounding volumes contain smaller ones

Check intersect root If intersect check intersect left sub-tree check intersect right sub-tree

Thursday, April 1, 2010

slide-22
SLIDE 22

17

Hierarchical Bounding Volumes

  • Many ways to build a tree for the hierarchy
  • Works well:

– Binary – Roughly balanced – Boxes of sibling trees not overlap too much

Thursday, April 1, 2010

slide-23
SLIDE 23

18

Hierarchical Bounding Volumes

  • Sort the surfaces along the axis before dividing into two boxes
  • Carefully choose axis each time
  • Choose axis that minimizes sum of volumes

Thursday, April 1, 2010

slide-24
SLIDE 24

19

Hierarchical Bounding Volumes

  • Sort the surfaces along the axis before dividing into two boxes
  • Carefully choose axis each time
  • Choose axis that minimizes sum of volumes

Thursday, April 1, 2010

slide-25
SLIDE 25

20

Hierarchical Bounding Volumes

  • Works well if you use good (appropriate) bounding

volumes and hierarchy

  • Should give O(log n) rather than O(n) complexity

(n=# of objects)

  • Can have multiple classes of bounding volumes and pick

the best for each enclosed object

Thursday, April 1, 2010

slide-26
SLIDE 26

Questions

x1 x2 y1 y2 x3 x4 y2 y4 x5 x6 y6 y5

Given two bounding boxes at

  • ne level of the hierarchy,

how do you compute the boxes for the next level?

x1,y1 r1 x2,y2 r2 x3,y3 r3

How about for bounding spheres?

Thursday, April 1, 2010

slide-27
SLIDE 27

Tight Bounding Spheres

Figure 3: The wrapped hierarchy (left) has smaller spheres than the layered hierarchy (right). The base geometry is shown in green, with five vertices. Notice that in a wrapped hierarchy the bounding sphere of a node at one level need not contain the spheres of its de- scendents and so can be significantly smaller. However, since each sphere contains all the points in the base geometry, it is sufficient for collision detection.

Thursday, April 1, 2010

slide-28
SLIDE 28

21

Hierarchical bounding volumes Spatial Subdivision

  • Grids
  • Octrees
  • K-d trees and BSP trees

Thursday, April 1, 2010

slide-29
SLIDE 29

22

3D Spatial Subdivision

  • Bounding volumes enclose the objects (object-

centric)

  • Instead could divide up the space—the further

an object is from the ray the less time we want to spend checking it

– Grids – Octrees – K-d trees and BSP trees

Thursday, April 1, 2010

slide-30
SLIDE 30

23

Grids

  • Data structure: a 3-D array of cells (voxels) that tile space

– Each cell points to list of all surfaces intersecting that cell

  • Intersection testing:

– Start tracing at cell where ray begins – Step from cell to cell, searching for the first intersection point – At each cell, test for intersection with all surfaces pointed to by that cell – If there is an intersection, return the closest one

Thursday, April 1, 2010

slide-31
SLIDE 31

24

Grids

  • Cells are traversed in an incremental fashion
  • Hits of sets of parallel lines are very regular

Thursday, April 1, 2010

slide-32
SLIDE 32

25

More on Grids

  • Be Careful! The fact that a ray passes through a cell and hits an
  • bject doesn’t mean the ray hit that object in that cell
  • Optimization: cache intersection point and ray id in “mailbox”

associated with each object

  • Step from cell to cell
  • Get object intersecting cell
  • Find closest intersection
  • If found intersection --- done

Thursday, April 1, 2010

slide-33
SLIDE 33

26

More on Grids

  • Grids are a poor choice when the world is nonhomogeneous

(clumpy)

– many polygons clustered in a small space

  • How many cells to use?

– too few ! many objects per cell ! slow – too many ! many empty cells to step through ! slow

  • Non-uniform spatial subdivision is better!

Thursday, April 1, 2010

slide-34
SLIDE 34

27

Octrees

  • Quadtree is the 2-D generalization of binary tree

– node (cell) is a square – recursively split into four equal sub-squares – stop when leaves get “simple enough”

Thursday, April 1, 2010

slide-35
SLIDE 35

28

Octrees

  • Quadtree is the 2-D generalization of binary tree

– node (cell) is a square – recursively split into four equal sub-squares – stop when leaves get “simple enough”

  • Octree is the 3-D generalization of quadtree

– node (cell) is a cube, recursively split into eight equal sub-cubes – for ray tracing: ! stop subdivision based on number of objects ! internal nodes store pointers to children, leaves store list of surfaces – more expensive to traverse than a grid – but an octree adapts to non-homogeneous scenes better

trace(cell, ray) { // returns object hit or NONE if cell is leaf, return closest(objects_in_cell(cell)) for child cells pierced by ray, in order // 1 to 4 of these

  • bj = trace(child, ray)

if obj!=NONE return obj return NONE }

Thursday, April 1, 2010

slide-36
SLIDE 36

29

Which Data Structure is Best for Ray Tracing?

Grids

Easy to implement Require a lot of memory Poor results for inhomogeneous scenes

Octrees

Better on most scenes (more adaptive)

Spatial subdivision expensive for animations

Hierarchical bounding volumes Better for dynamic scenes Natural for hierarchical objects

Thursday, April 1, 2010

slide-37
SLIDE 37

30

k-d Trees and BSP Trees

  • Relax the rules for quadtrees and octrees:
  • k-dimensional (k-d) tree

– don’t always split at midpoint – split only one dimension at a time (i.e. x or y or z)

  • binary space partitioning (BSP) tree

– permit splits with any line – In 2-D space split with lines (most of our examples) – 3-D space split with planes – K-D space split with k-1 dimensional hyperplanes

  • useful for Painter’s algorithm (hidden surface

removal)

Thursday, April 1, 2010

slide-38
SLIDE 38

31

Painters Algorithm

Hidden Surface Elimination

Thursday, April 1, 2010

slide-39
SLIDE 39

32

Painters Algorithm

  • Need to sort objects back to front
  • Order depends on the view point
  • Partition objects using BSP tree
  • View independent

Thursday, April 1, 2010

slide-40
SLIDE 40

33

Building a BSP Tree

  • Let’s look at simple example with 3 line segments
  • Arrowheads are to show left and right sides of lines.
  • Using line 1 or 2 as root is easy.
  • (examples from http://www.geocities.com/SiliconValley/2151/bsp.html)

Line 2 Line 3 Line 1

Viewpoint

1 2 3

a BSP tree using 2 as root

Thursday, April 1, 2010

slide-41
SLIDE 41

34

Drawing Objects

  • Traverse the tree from the root
  • If view point is on the left of the line --- traverse right sub-tree first
  • Draw the root
  • Traverse left sub-tree

Line 2 Line 3 Line 1

Viewpoint

1 2 3

a BSP tree using 2 as root

Thursday, April 1, 2010

slide-42
SLIDE 42

35

Building the Tree 2

Using line 3 for the root requires a split

Line 2a Line 3 Line 1

Viewpoint

1 2b 2a

Line 2b

3 D C B A

Thursday, April 1, 2010

slide-43
SLIDE 43

36

Triangles

Use plane containing triangle T1 to split the space If view point is on one side of the plane draw polygons on the other side first T2 does not intersect plane of T1

Thursday, April 1, 2010

slide-44
SLIDE 44

37

Triangles

Use plane containing triangle T1 to split the space If view point is on one side of the plane draw polygons on the other side first T2 does not intersect plane of T1

Thursday, April 1, 2010

slide-45
SLIDE 45

38

Triangles

Split Triangle

Thursday, April 1, 2010

slide-46
SLIDE 46

39

Building a Good Tree - the tricky part

  • A naïve partitioning of n polygons will yield O(n3)

polygons because of splitting!

  • Algorithms exist to find partitionings that produce O(n2).

– For example, try all remaining polygons and add the one which causes the fewest splits – Fewer splits -> larger polygons -> better polygon fill efficiency

  • Also, we want a balanced tree.

Thursday, April 1, 2010

slide-47
SLIDE 47

40

Painter’s Algorithm with BSP trees

  • Build the tree

– Involves splitting some polygons – Slow, but done only once for static scene

  • Correct traversal lets you draw in back-to-front or front-

to-back order for any viewpoint

– Order is view-dependent – Pre-compute tree once – Do the “sort” on the fly

  • Will not work for changing scenes

Thursday, April 1, 2010

slide-48
SLIDE 48

41

Drawing a BSP Tree

  • Each polygon has a set of coefficients:

Ax + By + Cz + D

  • Plug the coordinates of the viewpoint in and see:

>0 : front side <0 : back facing =0 : on plane of polygon

  • Back-to-front draw: inorder traversal, do farther child first
  • Front-to-back draw: inorder traversal, do near child first

front_to_back(tree, viewpt) { if (tree == null) return; if (positive_side_of(root(tree), viewpt)) { front_to_back(positive_branch(tree, viewpt); display_polygon(root(tree)); front_to_back(negative_branch(tree, viewpt); } else { …draw negative branch first…} }

Thursday, April 1, 2010

slide-49
SLIDE 49

42

Drawing Back to Front

  • Use Painter’s Algorithm for hidden surface removal

Line 2a Line 3 Line 1

Viewpoint

1 2b 2a 3

Line 2b

Steps:

–Draw objects on far side of line 3 »Draw objects on far side of line 2a –Draw line 1 »Draw line 2a –Draw line 3 –Draw objects on near side of line 3 »Draw line 2b

Thursday, April 1, 2010

slide-50
SLIDE 50

43

Further Speedups

  • Do backface culling
  • Draw front to back, and…

– Keep track of partially filled spans – Only render parts that fall into spans that are still open – Quit when the image is filled

Thursday, April 1, 2010

slide-51
SLIDE 51

44

Clipping Using Spatial Data Structures

Clip the BSP tree against the portions of space that you can see! Accelerate Clipping

— The goal is to accept or reject whole sets of polygons — Can use spatial data structure — Much faster than clipping every polygon — The O(n) task becomes O(log n)

– terrain fly-throughs – gaming Hierarchical bounding volumes Octrees

Thursday, April 1, 2010

slide-52
SLIDE 52

45

Further Speedups

  • Clip the BSP tree against the portions of space that you

can see!

– Called portals – Initial view volume is entire viewing frustum – When you look through a doorway, intersect current volume with “beam” defined by doorway

Thursday, April 1, 2010

slide-53
SLIDE 53

46

Demos BSP Tree construction http://symbolcraft.com/graphics/bsp/index.html

  • KD Tree construction

http://www.cs.umd.edu/~brabec/quadtree/index.html

Thursday, April 1, 2010

slide-54
SLIDE 54

47

Real-time and Interactive Ray Tracing The OpenRT Real-Time Ray-Tracing Project http://www.openrt.de/index.php

  • Interactive ray tracing via space subdivision

http://www.cs.utah.edu/~reinhard/egwr/

  • Interactive ray tracing with good hardware

http://www.cs.utah.edu/vissim/projects/raytracing/

Thursday, April 1, 2010

slide-55
SLIDE 55

48

Announcements

  • Written Assignment2 is out, due March 8
  • Graded Programming Assignment2 next Tuesday

Thursday, April 1, 2010