440 likes | 632 Views
Advanced Computer Graphics Spring 2014. K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology. Bounding Volume Hierarchies.
E N D
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology
Bounding Volume Hierarchies • Wrapping objects in bounding volumes and performing tests on the bounding volumes before testing the object geometry itself can result in significant performance improvement. • By arranging the bounding volumes into a tree hierarchy called a bounding volume hierarchy (BVH), the time complexity can be reduced to logarithmic in the number of tests performed. • With a hierarchy in place, during collision testing children do not have to be examined if their parent volume is not intersected. • Scene graphs, ray tracing, view-frustum culling.
Bounding Volume Hierarchies • Comparison between bounding volume hierarchies and spatial partitioning schemes • The main differences are that two or more volumes in a BVH can cover the same space and objects are generally only inserted in a single volume. • In contrast, in a spatial partitioning scheme the partitions are disjoint and objects contained in the spatial partitioning are typically allowed to be represented in two or more partitions.
Bounding Volume Hierarchies • Manual creation of hierarchies • Designers tend to think functionally rather than spatially. • A grouping may not be suitable for collision detection. • The hierarchy is also likely to be either too shallow or too deep in the wrong places. • A better solution is to automate the generation of hierarchies from the provided models.
Bounding Volume Hierarchies • Hierarchy Design Issues • The nodes contained in any given subtree should be near each other. • Each node in the hierarchy should be of minimal volume. • The sum of all bounding volumes should be minimal. • Greater attention should be paid to nodes near the root of the hierarchy. • The volume of overlap of sibling nodes should be minimal. • The hierarchy should be balanced with respect to both its node structure and its content.
Bounding Volume Hierarchies • For real-time applications, an important addition to the previous list is the requirement that the worst-case time for queries not be much worse than the average-case query time. • It is also desired that the hierarchy can be automatically generated without user intervention. • A very important factor often glossed over in treatments of collision detection is the total memory requirement for the data structures used to represent the bounding volume hierarchy.
Bounding Volume Hierarchies • Cost function: The expected cost of bounding volume hierarchy queries • Example: T = NVCV + NPCP+NUCU + CO • T is the total cost of intersecting the two hierarchies • NV is the number of BV pairs tested for overlap • CV is the cost of testing a pair of BVs for overlap • NP is the number of primitive pairs tested. • CP is the cost of testing a primitive pair. • NU is the number of nodes that need to be updated. • CU is the cost of updating each such node • CO is the cost of a one-time processing.
Bounding Volume Hierarchies • NV and NP are minimized by making the bounding volume fit the object as tightly as possible. • By making the overlap tests as fast as possible, CV and CP are minimized. • In general the values are so intrinsically linked that minimizing one value often causes another to increase. • Finding a compromise among existing requirements is a challenge in all collision detection systems.
Bounding Volume Hierarchies • Degree of tree? • What degree or branching factor should be use in the tree representing the bounding volume hierarchy? • A tree of a higher degree will be of smaller height, minimizing root-to-leaf traversal time. • More work has to be expended at each visited node to check its children for overlap. • The opposite holds for a low-degree tree. • Then what degree should be used? • It is a difficult one and no definitive answer has been forthcoming. • It appears that binary trees are by far the most common hierarchical representation. • It is easier to build, to represent and traverse than any other trees.
Building Strategies for Hierarchy Construction • As the number of possible trees grows exponentially in terms of the number of elements in the input set, an exhaustive search for the best tree is infeasible. • This rules out finding an optimal tree. • Instead, heuristic rules are used to guide the construction, examining a few alternatives at each decision – making step, picking the best alternative. • There are three primary categories of tree construction methods: top-down, bottom-up and insertion methods.
Building Strategies for Hierarchy Construction • Top-down method • It proceeds by partitioning the input set into two subsets, bounding them in the chosen bounding volume, then recursing over the bounded subsets. • It is by far the most popular due to its ease of implementation. • However, they do not generally result in the best possible trees.
Top-down Construction • A top-down method can be described in terms of a recursive procedure. • It starts out by bounding the input set of primitives in a bounding volume. • These primitives are then partitioned into two subsets. • The procedure is now called recursively to form sub-hierarchies for the two subsets, which are then connected as children to the parent volume. • The recursion stops when the input set consists of a single primitive, at which point the procedure just returns after creating the bounding volume for the primitive.
Top-down Construction • The choice of how the input set is partitioned into two subsets. • Apart from the selection of what bounding volume to use, only a single guiding criterion controls the structure of the resulting tree • A set of n elements can be partitioned into two nonempty subsets in 2n-1-1 ways • Only a small subset of all partitions can reasonably be considered.
Top-down Construction • Partitioning Strategies • Median-cut algorithm • The set is divided in two equal-size parts with respect to their projection along the selected axis, resulting in a balanced tree. • Minimize the sum of the volumes (or surface areas) of the child volumes • Minimize the maximum volume (surface area) of the child volumes • Minimize the volume (surface area) of the intersection of the child volumes • Maximize the separation of child volumes • Divide primitives equally between the child volumes • Combinations of the previous strategies.
Top-down Construction • Stopping criteria • The node contains just a single primitive, or less than some k primitives • The volume of the bounding volume has fallen below a set cut-off limit • The depth of the node has reached a predefined cut-off depth. • Partitioning can fail when • All primitives fall on one side of the split plane. • One or both child volumes end up with as many primitives as the parent volume. • Both child volumes are as large as the parent volume. • When partitioning fails, it is reasonable to try other partitioning criteria before stopping.
Top-down Construction • Choice of Partitioning Axis • A single axis must be selected as the partitioning axis. • An iterative optimization can locate the best possible axis but it is too expensive. • Consequently, the search must be limited to a small number of axes from which the best one is picked.
Top-down Construction • Choice of Partitioning Axis • Common choices of axes • Local x, y, and z coordinate axes • Axes from the intended aligned bounding volume • Axes of the parent bounding volume • Axis through the two most distant points • Axis along which variance is greatest • We may apply a small number of hill-climbing steps to improve on the axis.
Top-down Construction • Choice of Split Point • As there are infinitely many splitting points along the axis, the selection must be restricted to a small set of choices • Median of the centroid coordinates (object median) • Mean of the centroid coordinates (object mean) • Median of the bounding volume projection extents (spatial median) • Splitting at k evenly spaced points along the bounding volume projection extents • Splitting between (random subset of) the centroid coordinates Figure 6.3 here
Top-down Construction • The advantages include ease of implementation and fast tree construction. • The disadvantage is that as critical decisions are made early in the algorithm at a point where all information is not available the trees are typically not as good as possible.
Building Strategies for Hierarchy Construction • Bottom-up method • It starts with the input set as the leaves of the tree and then group two or more of them to form a new node, proceeding in the same manner until everything has been grouped under a single node. • Although bottom-up methods are likely to produce better trees than the other methods, they are also more difficult to implement and slow in construction.
Building Strategies for Hierarchy Construction • Bottom-up Construction • The first step is to enclose each primitive within a bounding volume. • These volumes form the leaf nodes of the tree. • From the resulting set of volumes, two (or more) leaf nodes are selected based on some merging criterion. – a clustering rule. • These nodes are then bound within a bounding volume, which replaces the original nodes in the set. • This pairing procedure repeats until the set consists of a single bounding volume representing the root node of the constructed tree.
Building Strategies for Hierarchy Construction • Bottom-up Construction • One of the more meaningful merging criteria is to select the pair so that the volume of their bounding volume is minimized. • A brute-force approach for finding which two nodes to merge : O(n2) • Repeated n-1 times to form a full tree, the total construction time becomes O(n3) • There exists more sophisticated methods which outperform the brute force method.
Building Strategies for Hierarchy Construction • Insertion method • It builds the hierarchy incrementally by inserting objects one at a time into the tree. • The insertion position is chosen so as to minimize some cost measurement for the resulting tree. • It is considered an on-line method in that it requires all primitives to be available before construction starts. • It allows updates to be performed at runtime.
Building Strategies for Hierarchy Construction • Insertion method
Building Strategies for Hierarchy Construction • Insertion method • The tree is built by inserting one object at a time, starting from an empty tree. • Objects are inserted by finding the insertion location in the tree that causes the tree to grow as little as possible according to a cost metric. • Normally the cost associated with inserting an object at a given position is taken to be the cost of its bounding volume plus the increase in volume its insertion causes in all ancestor volumes above it in the tree. • If the object being inserted is large compared to the existing nodes, it will tend to end up near the top of the hierarchy.
Building Strategies for Hierarchy Construction • Insertion method • Smaller objects are more likely to lie within existing bounding volumes and will instead end up near the bottom. • When the new object is far away from existing objects it will also end up near the top. • Overall the resulting tree will therefore tend to reflect the actual clustering in the original set of objects.
Building Strategies for Hierarchy Construction • Insertion method • Because the structure of a tree is dependent on the objects inserted into it and because insertion decisions are made based on the current structure, it follows that the order in which objects are inserted is significant. • The best approach seems to be to randomly shuffle the objects before insertion. • A simple insertion method implementation would be to perform a single root-leaf traversal by consistently descending the child for which the insertion into would be cheaper. • Then the insertion node would be selected from the visited nodes along the traced path such that the total tree volume would be minimized. -> O(nlog n)
Building Strategies for Hierarchy Construction • Insertion method • Insertion strategies can be as fast as or even faster than top-down methods and could produce better results. • They are considered on-line methods in the sense that not all objects need be present when the process starts. • The Goldsmith-Salmon Incremental Construction Method for constructing bounding volume hierarchies for use in ray tracing.
Hierarchy Traversal • To determine the overlap status of two bounding volume hierarchies, some rule must be established for how to descend the trees when their top-level volumes overlap. • If the one hierarchy fully descended before the other one is? • Are they both descended? • The two most fundamental tree-traversing methods are breadth-first search and depth-first search.
Hierarchy Traversal • Breadth-first search (BFS) explores nodes at a given depth before proceeding deeper into the tree. • It suffers from the fact that stacking all nodes during traversal requires a substantial amount of memory. • Depth-first search (DFS) proceeds into the tree, searching deeper rather than wider, backtracking up the tree when it reaches the leaves. • It seems to be the most popular choice for collision detection systems. • It often enhanced by a simple heuristic to guide the search along, improving on the basic blind DFS approach without the overhead of a full best-first search.
Hierarchy Traversal • Uninformed or blind search methods • BFS and DFS methods • They do not examine and make traversal decisions based on the data contained in the traversed structure. • They only look at the structure itself to determine what nodes to visit next. • Informed search methods • These attempts to utilize known information about the domain being searched through heuristic rules. • Best-first search, a greedy algorithm that always moves to the node that scores best on some search criterion.
Hierarchy Traversal • Descent Rules • Given two hierarchies A and B there are several possible traversal alternatives. • Descend A before B is descended. • Descend B before A is descended. • Descend the larger volume. • Descend A and B simultaneously. • Descend A and B alternatingly. • Descend based on overlap. • Combinations of the previous or other complex rules based on traversal history. • Which type of traversal is most effective depends entirely on the structure of the data. • For a very large data sets, hybrid approaches are likely to be more effective than a single tree hierarchy.
Collision Detection System • First, using a pruning algorithm, all pairs of objects whose fixed cubes overlap are detected and stored in the pair list. • Second, the object pairs are sent to exact collision detection algorithms. • Finally, the results from collision detection are forwarded and taken care of by the application, so that action can be taken.
Efficient Tree Representation and Traversal • It is important to optimize both the traversal code and the tree representation for an industrial-strength implementation. • As memory accesses and branch prediction misses tend to cause large penalties in modern architectures, two obvious optimizations are • to minimize the size of the data structures involved • to arrange the data in a more cache-friendly way so that relevant information is encountered as soon as possible. • It is difficult to predict cache behaviors. So some of more advanced techniques might not always provide the expected speedups. • Keep the traversal code as short and straightforward as possible might in fact be a simple way of making it faster.
Efficient Tree Representation and Traversal • Array Representation • Assume a complete binary tree of n nodes is given as a collision hierarchy. • This tree can be stored into an array of n elements by mapping its nodes in a breadth-first level-by-level manner. • Being at the node stored at array[i] the corresponding left child to the node will be found at array[2*i+1] and its right child at array[2*i+2]. • Instead of representing the tree using nodes containing left and right pointers to the node’s children it is possible to completely remove all pointers and store the pointerless nodes in an array in the manner.
Efficient Tree Representation and Traversal • A perfectly balanced tree saves memory space and pointer reads during tree traversal. • But for a nonbalanced tree, space will be wasted. • Space still has to be allocated as if the tree were complemented with extra nodes to make it fully balanced.
Efficient Tree Representation and Traversal • This representation is most useful when the actual node data (“payload”) is small compared to the combined size of the child pointers or when the tree really is fully balanced.
Efficient Tree Representation and Traversal • Preorder Traversal Order • Even when no guarantees can be made about the balance of the tree hierarchy, it is still possible to output the data in a more effective representation. • If the tree nodes are output in preorder traversal order, the left child when present will always immediately follow its parent. • This way, although a link is still needed to point at the right child only a single bit is needed to indicate whether there is a left child.
Efficient Tree Representation and Traversal • Preorder Traversal Order • In addition to reducing the needed number of child pointers by half, this representation also has the benefit of being quite cache friendly. • The left child is very likely already in cache, having been fetched at the same time as its parent, making traversal more efficient.
Efficient Tree Representation and Traversal • Offsets Instead of Pointers • A typical tree implementation uses (32-bit) pointers to represent node child links. • For most trees a pointer representation is overkill. • More often than not, allocating the tree nodes from within an array a 16-bit index value from the start of the array can be used instead. • This will work for both static and dynamic trees.
Efficient Tree Representation and Traversal • Cache-friendlier Structures (Nonbinary Trees) • Execution time in modern architectures is often more limited by cache issues related to fetching data from memory than the number of instructions executed. • It can pay off to use nonconventional structures that although more complicated to traverse take up less memory and have a more cache-friendly access pattern.
Efficient Tree Representation and Traversal • One possible representation method • Merging sets of three binary tree nodes (parent, left and right child) into a “tri-node”, a fused node containing all three nodes.
Efficient Tree Representation and Traversal • Drawbacks • The drawback is the additional processing required to traverse the tree. • In addition, when a tree is not complete nodes go empty in the tri-node, wasting memory. • A flag is also needed to indicate whether the node is used or empty. • Tri-node trees are better suited for dense trees.