space-based partitioning data structures and their...

32
Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Upload: others

Post on 15-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Space-based Partitioning Data Structures and their Algorithms

Jon Leonard

Page 2: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Purpose

● Space-based Partitioning Data Structures are an efficient way of organizing data that lies in an n-dimensional space

– 2D, 3D, even 4D and beyond● Reasoning about the data is usually much

easier● Bottom Line: Cut down on the search space

Page 3: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Example 1

X Values Y Values1 35 69 33 18 84 91 22 48 14 27 2

Page 4: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Outline

● Quadtrees– Octrees

● Binary Space Partitions (BSP)– Kd-Trees

● R-Tree● Bounded Volume Hierarchy (BVH)

Page 5: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Outline

● Quadtrees– Octrees

● Binary Space Partitions (BSP)– Kd-Trees

● R-Tree● Bounded Volume Hierarchy (BVH)

Page 6: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Quadtree Origins

● The term Quadtree was coined by Raphael Finkel and J.L. Bentley in 1974

● A data structure to store data in two dimensions

● Follows the same intuition as a binary tree– Binary Tree: I have one range of data I want

to partition– Quadtree: I have two ranges of data I want to

partition

Page 7: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Quadtree

● Quadtrees represent finite, square areas– The internal nodes of a quadtree are also

finite, square areas that make up a quadrant of their parent node's region

● As such, each internal node has four children, one for each quadrant

– The leaf nodes of a quadtree hold a finite number of elements

● Filled leaf nodes will break into four quadrants and the data is inserted into the quadrants

Page 8: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Quadtree

Page 9: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Quadtree Insert Codequadtree_insert(quadtree node, data_type data)

if(node has quadrants)if in first quadrant

quadtree_insert(node.firstQuad, data)if in second quadrant

quadtree_insert(node.secondQuad, data)...

elseif node.data.size < threshold

node.data.insert(data)else

break up into quadrantsinsert existing data into quadrantsinsert data into quadrant

Page 10: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Quadtree

● Alternative Implementations– Rectangular Quadrants

● Point Quadtree● Quadtrees are flexible, implementations can

vary to work for many situations– Conway's Game of Life– Bitmap Image Representation

● Not limited to spacial data

Page 11: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Octree

● Octrees are the 3D analog to Quadtrees● Octree internal nodes contain 8 octants

– Aside from more coordinate comparisons, quadtrees and octrees are identical in code

Page 12: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Quadtree/Octree Uses

● Ray tracing– Line of Sight

● Hidden Surface Removal– Occlusion Culling

● Collision Detection

Page 13: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Outline

● Quadtrees– Octrees

● Binary Space Partitions (BSP)– Kd-Trees

● R-Tree● Bounded Volume Hierarchy (BVH)

Page 14: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Binary Space Partition Trees

● Binary Space Partition Trees (BSPs) are binary trees

● Each internal node in a BSP has a partition, oriented any arbitrary direction

– The two branches of the BSP, “Front” and “Back”

– All data down either side of the node is guaranteed to be either in front of the partition or behind

Page 15: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Binary Space Partition Construction

bsp_tree create_bsp(data_type data[])node = new bsp_tree;if data[].size == 1

node.data = dataelse

good_partition = find the partition that cuts data[] into two near equal sets

node.partition = good_partitionnode.front = create_bsp(data[] in front of partition

done.back = create_bsp(data[] behind partitionreturn node;

Page 16: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Binary Space Partition Uses

● Rendering Scenes– Painter's Algorithm

● Back to Front Tree Traversal● Ray Tracing

Page 17: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Back to Front Traversaltraverse_tree(bsp_tree node, point eyepoint){

location = determine which side of the partition eyepoint is on

if location is in fronttraverse_tree(bsp.back, eyepoint)show(node.data)traverse_tree(bsp.front, eyepoint)

else // location in behind or eyepoint is on the partitiontraverse_tree(bsp.front, eyepoint)show(node.data)traverse_tree(bsp.back, eyepoint)

}

Page 18: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Binary Space Partition

● Has now been replaced for graphical applications

– Only works in 2D/Pseudo-3D spaces● Historically solved computational barriers

– BSPs utilized in Doom (1993)● Quake (1996) as well

● Other Applications– Image compression

Page 19: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Doom BSP

● In a paper released by Daniel Gordon and Shuhong Chen in September 1991

● Utilized by John Carmack ● Uses walls and other naturally flat objects in

the game as the partition● Does a front to back traversal of the tree

– Keeps track of what areas of the screen have been drawn to already

Page 20: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Kd-tree

● A special form of a BSP– Instead of arbitrarily directed partitions,

Kd-tree partitions are parallel to the x and y axis

● Works just like a binary tree– Each internal node has

2 children– Each internal node either

splits the x axis or the y axis

Page 21: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Kd-tree ConstructionKdtree construct_kdtree(int axis, data_type data[]){

find the datapoint closest to the average/median value for the given axis as the pivot point

store the data in the nodeleftNode = construct_kdtree(next axis, data less than

pivot point)rightNode = construct_kdtree(next axis, data greater

than pivot point)}

Page 22: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Nearest Neighbor Search1. Traverse down the tree to a leaf node that contains the query point 2. Return current leaf node as best choice3. When returning up the tree, check to see if a circle centered on the query point with radius equal to the distance to the current best choice will cross the pivoting axis at that node

If so, following this procedure again down the other branch and compare the result to the current best choice

Otherwise, continue up the tree continuing to check if another branch should be considered.

Page 23: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Outline

● Quadtrees– Octrees

● Binary Space Partitions (BSP)– Kd-Trees

● R-Tree● Bounded Volume Hierarchy (BVH)

Page 24: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

R-tree

● Stands for Rectangle-Tree– Variation of a B-tree

● Made up of minimum bounding rectangles (MBRs)

– Unlike quadtrees quadrants, MBRs can overlap

● For large datasets

Page 25: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

R-Tree

Page 26: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

R-Tree Variations

● R*-tree– Indexes spacial information more efficiently

than a R-tree by minimizing MBR overlap● R+ tree

– No overlapping MBRs, it will insert an object in multiple nodes if needed

● Priority R-tree– Better overall R-tree, but more efficient for

extreme datasets

Page 27: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Outline

● Quadtrees– Octrees

● Binary Space Partitions (BSP)– Kd-Trees

● R-Tree● Bounded Volume Hierarchy (BVH)

Page 28: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Bounded Volume Hierarchy

● Similar to a 3D R-Tree– Not limited to simple bounding rectangles

● Any volume to surround objects can be utilized

– Sphere, Cone, etc.● Tries to maximize “tightness” property of

spacial data structures

Page 29: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Tightness

● Querying any of these data structures is only as efficient as how tight the data structure fits the data set

– Minimize the amount of unused space bounded by the bounding shape

– While minimizing the height of the tree

Page 30: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Bounded Volume Hierarchy

Page 31: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

Conclusions

Page 32: Space-based Partitioning Data Structures and their Algorithmsdcm/Teaching/COT4810-Spring2011/Presen… · Space-based Partitioning Data Structures and their Algorithms Jon Leonard

ResourcesArge, Lars et al. “The Priority R-Tree: A Practically Efficient and Worst-Case Optimal R-Tree” Proceedings of the 2004 ACM SIGMOD. June 2004.Gordon, Dan and Chen, Shuhong. “Front-to-Back Display of BSP Trees.” IEEE Computer Graphics & Algorithms, pp 79–85. September 1991.Krishnaswamy, Ravinder and Alijani, Ghasem S. “On Constructing Binary Space Partitioning Trees.” Proceeding CSC '90 Proceedings of the 1990 ACM annual conference on Cooperation, pp 230-235. 1990.Samet, Hanan. “The Quadtree and Related Hierarchical Data Structures.” ACM Computing Surveys Volume 16 Issue 2, pp 187-260. June 1984.