Data Structures for 3D Searching partly based on: chapter 12 in Fundamentals of Computer Graphics, 3rd ed. (Shirley & Marschner) Slides by Marc van Kreveld 1 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 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 selfintersections of the manifold (straightforward in O(n) time) – Also: for ray tracing, windowing 3 Data structures for searching • • • • • Grid structure Quadtree and Octree Bounding Volume Hierarchy, R-tree Kd-tree BSP tree (Binary Space Partition) 4 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 Simple grid structure 6 Simple grid structure ray tracing query 7 Simple grid structure k nearest neighbors query 8 Simple grid structure RANSAC line support computation 9 Simple grid structure windowing query 10 Simple grid structure • A hierarchical version of a grid may give more efficient querying: one emptiness test at a higher level square may make several tests at lower level squares unnecessary 11 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 Quadtrees and Octrees NW NE NW NE SW SE root leaf leaf leaf storing one object SW SE 13 Quadtrees and Octrees NW NE NW NE SW SE root leaf leaf leaf storing one object SW SE 14 Quadtrees and Octrees NW NE This ray tracing query visits 10 nodes in the quadtree (12 squares in the original grid) SW SE 15 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 17 Octree 18 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 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 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 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 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 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 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 axisparallel 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 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 Object descriptions 27 Grouping of objects Windowing query: the fewer rectangles intersected, the fewer subtrees to descend into 28 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 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 Example R-tree 31 32 33 34 35 Object descriptions 36 point containment query 37 point containment query 38 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 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 pruned closest object so far queried 40 1 4 5 2 3 41 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 43 44 45 46 47 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 Split Node, example new bounding rectangles 49 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 Example Split Node 51 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 Example Split Node x-extent is larger 53 Example Split Node split in the middle 54 Example Split Node split close to the middle 55 56 57 58 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 60 61 62 63 Insert as rectangle on middle level 64 Insert in a leaf object 65 66 67 68 69 70 71 72 73 74 75 76 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 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 point 78 Kd-trees 79 Kd-trees 80 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 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 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 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 3 2 • In 3D this is O(n + 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 84 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 3D kd-tree 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 BSP-trees • BSP-trees were used in Doom and in Quake 88 BSP-trees 89 BSP-trees 90 BSP-trees B E A D B D A C E F G H H F C G 91 BSP-trees B E A D B C P D A P R F H E F G Q H P Q Q R Q C G 92 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 BSP-trees, painter’s algorithm 94 BSP-trees, painter’s algorithm B A E B D D A C E F G H H F C G Given a viewpoint, draw the objects back to front, “overpainting” what was drawn before 95 BSP-trees, painter’s algorithm B E Given a viewpoint, draw the objects back to front, “overpainting” what was drawn before D A H For each split line, all object parts “behind” it are drawn first, and then all objects in front of it (w.r.t. viewpoint) F C G 96 BSP-trees, painter’s algorithm B E seventh Given a viewpoint, draw second fifth the objects back to front, “overpainting” what was sixth eighth drawn before first D third A fourth tenth ninth H For each split line, all object parts “behind” it are drawn first, and then all objects in front of it (w.r.t. viewpoint) F eleventh C G 97 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 BSP-trees, painter’s algorithm 99 BSP-trees, painter’s algorithm 100 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 Questions 1. 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