data structures for 3d searching

102
Data Structures for 3D Searching partly based on: chapter 12 in Fundamentals of Computer Graphics, 3 rd ed. (Shirley & Marschner) Slides by Marc van Kreveld 1

Upload: graceland

Post on 23-Feb-2016

54 views

Category:

Documents


0 download

DESCRIPTION

Data Structures for 3D Searching. partly based on: chapter 12 in Fundamentals of Computer Graphics, 3 rd ed. (Shirley & Marschner ) Slides by Marc van Kreveld. Data structures. Data structures for representation geometry (coordinates) topology (connectivity) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Structures for 3D Searching

Data Structures for 3D Searchingpartly based on:

chapter 12 inFundamentals of Computer Graphics, 3rd ed.

(Shirley & Marschner)Slides by Marc van Kreveld

1

Page 2: Data Structures for 3D Searching

Data structures• Data structures for representation– geometry (coordinates)– topology (connectivity)– attributes (anything else like material, …)

• Data structures for searching– use the geometry to reduce the number objects that need

be tested from all of them to some small subset– windowing queries, ray tracing, nearest neighbors

2

Page 3: Data Structures for 3D Searching

Data structures for searching• Several steps in the algorithms we have seen until

now can be sped up using data structures for efficient searching– Find the k nearest neighbors to each point

(straightforward in O(n2) time; linear time per point)– Find the support of a plane tried in RANSAC

(straightforward in O(n) time)– Test whether a mesh-changing operator causes self-

intersections of the manifold (straightforward in O(n) time)– Also: for ray tracing, windowing

3

Page 4: Data Structures for 3D Searching

Data structures for searching• Grid structure• Quadtree and Octree• Bounding Volume Hierarchy, R-tree• Kd-tree• BSP tree (Binary Space Partition)

4

Page 5: Data Structures for 3D Searching

Simple grid structure• Also: cubical grid, cubic partitioning• Tile the space with equal-size cubes and store each

object with every cube it intersects • The 3D grid is a 3D array of lists; list elements are

pointers to the objects• Small grid cells: much storage overhead, but good

for query time• Big grid cells: little storage overhead, worse query

time

5

Page 6: Data Structures for 3D Searching

Simple grid structure

6

Page 7: Data Structures for 3D Searching

Simple grid structure

7ray tracing query

Page 8: Data Structures for 3D Searching

Simple grid structure

8k nearest neighbors query

Page 9: Data Structures for 3D Searching

Simple grid structure

9RANSAC line support computation

Page 10: Data Structures for 3D Searching

Simple grid structure

10windowing query

Page 11: Data Structures for 3D Searching

Simple grid structure• A hierarchical version of a grid may give more efficient

querying: one emptiness test at a higher level square may makeseveral tests at lower levelsquares unnecessary

11

Page 12: Data Structures for 3D Searching

Quadtrees and Octrees• Tree version of the (hierarchical) square/cube grid• Storage requirements better than grid when large

parts of the grid contain no objects• In a quadtree – the root corresponds to a bounding square– children correspond to four subsquares : NW, NE, SW, SE– nodes with 0 or 1 objects in their square are leaves

• In an octree – the root corresponds to a bounding cube– children correspond to eight subcubes– nodes with 0 or 1 objects in their cube are leaves

12

Page 13: Data Structures for 3D Searching

Quadtrees and Octrees

NW NE SW SE

NW NE

SW SE

leafleaf

root

leaf storing one object13

Page 14: Data Structures for 3D Searching

Quadtrees and Octrees

NW NE SW SE

NW NE

SW SE

leafleaf

root

leaf storing one object14

Page 15: Data Structures for 3D Searching

Quadtrees and OctreesNW NE

SW SE

This ray tracing query visits 10 nodes in the quadtree (12 squares in the original grid)

15

Page 16: Data Structures for 3D Searching

Quadtrees and Octrees• Other queries are performed in the straightforward

way– windowing query: easy– RANSAC support of line: determine all cells intersected by

the strip, and test points in those cells only for inclusion in the strip, right normal, and therefore support

– k nearest neighbors: explore cells further and further away from the query point until we know for sure that we have the k nearest neighbors explore all cells that intersect the circle centered at the query point and through the k-th closest point

16

Page 17: Data Structures for 3D Searching

17

Page 18: Data Structures for 3D Searching

Octree

18

Page 19: Data Structures for 3D Searching

Quadtrees and Octrees• Often a quadtree or octree is not made deeper than a

desired level of precision• E.g., given an object of 1 x 1 x 1 meters stored in a

triangle mesh, store the triangles in an octree up to a cell size of 4 x 4 x 4 millimeters

• Leaves that contain more than one object store them in a list (it is assumed that there would only be O(1) of them at this detail level)

• Also possible: split a square/cube until at most some constant c objects or points intersect a cell

19

Page 20: Data Structures for 3D Searching

Bounding Volume Hierarchies• Tree structure where internal nodes store bounding

shapes of all objects in the subtree• If a query object intersects the bounding shape, it

may intersect some object in that subtree, otherwise certainly not

• Bounding shapes can be spheres, axis-parallel bounding boxes, or arbitrarily oriented bounding boxes; axis-parallel BB is most common

20

Page 21: Data Structures for 3D Searching

Bounding Volume Hierarchies• In graphics usually binary trees• For huge data sets where the data must be on disk,

usually higher-degree trees: R-tree

21

Page 22: Data Structures for 3D Searching

R-trees• 2-dimensional version of the B-tree:

B-tree of maximum degree 8; degree between 3 and 8

Internal nodes with k children have k – 1 split values

22

Page 23: Data Structures for 3D Searching

R-trees• Can store:– a set of polygons (regions of a subdivision)– a set of polygonal lines (or boundaries)– a set of points– a mix of the above

• Stored objects may overlap

23

Page 24: Data Structures for 3D Searching

R-trees• Originally by Guttman, 1984• Dozens of variations and optimizations since• Suitable for windowing, point location, intersection

queries, and ray tracing• Heuristic structure, no order bounds ( O(..) )• Example of a bounding volume hierarchy

24

Page 25: Data Structures for 3D Searching

R-trees

• Every internal node contains entries (rectangle, pointer to child node)

• All leaves contain entries (rectangle, pointer to object) in database or file

• Rectangles are minimal axis-parallel bounding rectangles

• The root has 2 and M entries

• All other nodes have at least m and at most M entries

• All leaves have the same depth

• m > 1 and M > 2m(e.g. m = 200; M = 1000)

25

Page 26: Data Structures for 3D Searching

R-trees• M is chosen so that a full node still (just) fits in a single

block of disk memory• m is chosen depending on a trade-off in search time

and update time– larger m: faster queries, slower updates– smaller m: slower queries, faster updates

26

Page 27: Data Structures for 3D Searching

Object descriptions

27

Page 28: Data Structures for 3D Searching

Grouping of objects

Windowing query: the fewer rectangles intersected, the fewer subtrees to descend into

28

Page 29: Data Structures for 3D Searching

Grouping of objects• Objects close together in same leaves small

rectangles queries descend in only few subtrees • Group the child nodes under a parent node such that

small rectangles arise

29

Page 30: Data Structures for 3D Searching

Heuristics for fast queries• Small area of rectangles• Small perimeter of rectangles• Little overlap among rectangles• Well-filled nodes (tree less deep fewer disk

accesses on each search path)

30

Page 31: Data Structures for 3D Searching

Example R-tree

31

Page 32: Data Structures for 3D Searching

32

Page 33: Data Structures for 3D Searching

33

Page 34: Data Structures for 3D Searching

34

Page 35: Data Structures for 3D Searching

35

Page 36: Data Structures for 3D Searching

Object descriptions

36

Page 37: Data Structures for 3D Searching

point containment query

37

Page 38: Data Structures for 3D Searching

point containment query

38

Page 39: Data Structures for 3D Searching

Searching in an R-tree• Q is query object (point, window, object); we search

for intersections with stored objects

• For each rectangle R in the current node,if Q and R intersect,– search recursively in the subtree under the pointer at R

(at an internal node)– get the object corresponding to R and test for intersection

with R (at a leaf)

39

Page 40: Data Structures for 3D Searching

Nearest neighbor queries• An R-tree can be used for nearest neighbor queries• The idea is to perform a DFS, maintain the closest

object so far and use the distance for pruning

closest object so far

queried

pruned

40

Page 41: Data Structures for 3D Searching

1

2

3

4

541

Page 42: Data Structures for 3D Searching

Inserting in an R-tree• Determine minimal bounding rectangle of new object• When not yet at a leaf (choose subtree):– determine rectangle whose area increment after insertion

of R is smallest– increase this rectangle if necessary and insert R

• At a leaf:– if there is space, insert, otherwise Split Node

42

Page 43: Data Structures for 3D Searching

43

Page 44: Data Structures for 3D Searching

44

Page 45: Data Structures for 3D Searching

45

Page 46: Data Structures for 3D Searching

46

Page 47: Data Structures for 3D Searching

47

Page 48: Data Structures for 3D Searching

Split Node

• Divide the M+1 rectangles into two groups, each with at least m and at most M rectangles

• Make a node for each group, with the rectangles and corresponding subtrees as entries

• Hang the two new nodes under the parent node in the place of the overfull node; determine the new bounding rectangles (if the root was overfull, make a new root with two child nodes)

• If the parent has M+1 children, repeat Split Node with this parent

48

Page 49: Data Structures for 3D Searching

Split Node, example

new bounding rectangles

49

Page 50: Data Structures for 3D Searching

Strategies for Split Node

• Determine R1 and R2 with largest bounding rectangle: the seeds for sets S1 and S2

• While |S1| , |S2| < M – m and not all rectangles distributed:– Take not yet distributed rectangle Rj , add to the set whose

bounding rectangle increases least

Linear R-tree of Guttman, 1984

50

Page 51: Data Structures for 3D Searching

Example Split Node

51

Page 52: Data Structures for 3D Searching

Strategies for Split Node

• If the total x-extent is larger than the total y-extent, then sort the rectangles by x-coordinate of center, otherwise by y-coordinate of center

• Split halfway in this sorted order• Alternatively: choose a split close to halfway if this

gives smaller resulting bounding box overlap

52

Page 53: Data Structures for 3D Searching

Example Split Node

53

x-extent is larger

Page 54: Data Structures for 3D Searching

Example Split Node

54

split in the middle

Page 55: Data Structures for 3D Searching

Example Split Node

55

split close to the middle

Page 56: Data Structures for 3D Searching

56

Page 57: Data Structures for 3D Searching

57

Page 58: Data Structures for 3D Searching

58

Page 59: Data Structures for 3D Searching

Deletion from an R-tree• Find the leaf (node) and delete object; determine new

(possibly smaller) bounding rectangle• If the node is too empty (< m entries):– delete the node recursively at its parent– insert all entries of the deleted node into the R-tree

• Note: Insertion of entries/subtrees always occurs at the level where it came from

59

Page 60: Data Structures for 3D Searching

60

Page 61: Data Structures for 3D Searching

61

Page 62: Data Structures for 3D Searching

62

Page 63: Data Structures for 3D Searching

63

Page 64: Data Structures for 3D Searching

Insert as rectangle on middle level

64

Page 65: Data Structures for 3D Searching

Insert in a leaf

object

65

Page 66: Data Structures for 3D Searching

66

Page 67: Data Structures for 3D Searching

67

Page 68: Data Structures for 3D Searching

68

Page 69: Data Structures for 3D Searching

69

Page 70: Data Structures for 3D Searching

70

Page 71: Data Structures for 3D Searching

71

Page 72: Data Structures for 3D Searching

72

Page 73: Data Structures for 3D Searching

73

Page 74: Data Structures for 3D Searching

74

Page 75: Data Structures for 3D Searching

75

Page 76: Data Structures for 3D Searching

76

Page 77: Data Structures for 3D Searching

Bounding Volume Hierarchy• Other examples of bounding volume hierarchies are

often binary trees• Not all leaves will be on the same depth• Different balancing schemes are needed if insertions

and deletions occur• All bounding volume hierarchies try to group objects

suitably in their subtrees

Page 78: Data Structures for 3D Searching

Kd-trees• Binary search tree that splits a point set through the

middle, alternating on x- and y- (3D: on x-, y- and z-) coordinate

split on x, vertical line

split on y, horizontal line

leaves, each with one point78

Page 79: Data Structures for 3D Searching

Kd-trees

79

Page 80: Data Structures for 3D Searching

Kd-trees

80

Page 81: Data Structures for 3D Searching

Kd-trees in 3D• The point set is split on x-, y- and z-coordinate in the

topmost three levels, and this is repeated• Geometrically, this is splitting by axis-parallel planes

x = c, y = c, or z = c

81

Page 82: Data Structures for 3D Searching

Kd-trees• Every node corresponds to a region of the plane that

is a rectangle (possibly unbounded)• The points in the subtree below that node are exactly

the points of the stored set in that region• Denote the region of a node by Region()– Region(root) = R2 (the whole plane)– Region(child of root) = half-plane left/right of vertical line– “most” nodes : Region() = some bounded rectangle

82

Page 83: Data Structures for 3D Searching

Kd-trees, windowing query• Windowing query (report all points in the window) in

a kd-tree with window W– [ at node , initially the root ]– if is a leaf, then report the stored point if it is in W– otherwise, if Region() intersects W then recursively query

further in both subtrees

Note: if Region() does not intersect W, then we return from recursion (we do nothing at this node, nor its subtree)

83

Page 84: Data Structures for 3D Searching

Kd-trees, window query• For a set of n points in the plane, a kd-tree that stores

them allows windowing queries with an axis-parallel rectangular window takes O(n + k) time in the worst case, where k is the number of answers reported

• In 3D this is O(n2 + k) = O(n2/3 + k) time • For circular windowing queries similar bounds hold in

practice, but these are not provable worst-case bounds

• For queries with small windows the observed bounds are better

3

84

Page 85: Data Structures for 3D Searching

Kd-trees, other objects, other queries

• kd-trees can store triangles, etc., where objects are split by the vertical and horizontal lines need different splitting rule storage may become large

• Ray tracing query can be performed easily• k-nearest neighbor query can be done similar to such

a query in a quadtree

85

Page 86: Data Structures for 3D Searching

3D kd-tree

Page 87: Data Structures for 3D Searching

BSP-trees• BSP = Binary Space Partition• Similar to a kd-tree that stores

triangles, but with arbitrarily oriented splitting lines/planes

• Often lines/planes are chosen through edges/triangles of the set to be stored

• For query answering and for producing depth orders for the painter’s algorithm

87

Page 88: Data Structures for 3D Searching

BSP-trees• BSP-trees were used in Doom and in Quake

88

Page 89: Data Structures for 3D Searching

BSP-trees

89

Page 90: Data Structures for 3D Searching

BSP-trees

90

Page 91: Data Structures for 3D Searching

BSP-trees

A

B

A

B

C

D

E

G

F

C

D E GF

H

H

91

Page 92: Data Structures for 3D Searching

BSP-trees

A

B

A

B

C

D

E

G

F

C

D E GF

H

P

P P

Q

QQQ

RR

H

92

Page 93: Data Structures for 3D Searching

BSP-trees• BSP-trees are not necessarily balanced– no problem for the painter’s algorithm– unbalanced is not good for querying

• BSP-trees split objects; such fragmentation is undesirable because it costs memory space and lowers efficiency

93

Page 94: Data Structures for 3D Searching

BSP-trees, painter’s algorithm

94

Page 95: Data Structures for 3D Searching

BSP-trees, painter’s algorithmA

B

A

B

C

D

E

G

F

C

D E GF

H

H Given a viewpoint, draw the objects back to front, “overpainting” what was drawn before

95

Page 96: Data Structures for 3D Searching

BSP-trees, painter’s algorithm

A

B

C

D

E

G

FH

Given a viewpoint, draw the objects back to front, “overpainting” what was drawn before

For each split line, all object parts “behind” it are drawn first, and then all objects in front of it (w.r.t. viewpoint)

96

Page 97: Data Structures for 3D Searching

BSP-trees, painter’s algorithm

A

B

C

D

E

G

FH

Given a viewpoint, draw the objects back to front, “overpainting” what was drawn before

For each split line, all object parts “behind” it are drawn first, and then all objects in front of it (w.r.t. viewpoint)

first second

thirdfourth

fifth

sixth

seventh

eighth

ninth

tenth

eleventh97

Page 98: Data Structures for 3D Searching

BSP-trees, painter’s algorithm• In 3D it works similarly: choose splitting planes that

contain triangles (and may cut other triangles)• First draw stuff behind the plane, then the triangle

in the plane, then the stuff in front of the plane• Cutting may be necessary: cyclic overlap

98

Page 99: Data Structures for 3D Searching

BSP-trees, painter’s algorithm

99

Page 100: Data Structures for 3D Searching

BSP-trees, painter’s algorithm

100

Page 101: Data Structures for 3D Searching

Summary data structures• There are two types of data structures: those for

representation and those for efficient searching• Data structures can partition the underlying space, or

partition the objects it stores• Choosing a data structure:– small data set: not worthwhile– medium size data set: choose a simple structure, it will help– large data set: orders of magnitude in efficiency are gained– huge data sets: use a structure suitable for disk storage

101

Page 102: Data Structures for 3D Searching

Questions1. How would you implement ray tracing in an R-tree?2. How many levels does the octree of slide 19 have, at most?3. How many cyclic

overlaps do you see? ;-)

102