Advanced Computer Graphics Spring 2014

42
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology

description

Advanced Computer Graphics Spring 2014. K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology. Efficient Tree Representation and Traversal. Tree Node and Primitive Ordering - PowerPoint PPT Presentation

Transcript of Advanced Computer Graphics Spring 2014

Page 1: Advanced Computer Graphics  Spring 2014

Advanced Computer Graphics Spring 2014

K. H. Ko

School of MechatronicsGwangju Institute of Science and Technology

Page 2: Advanced Computer Graphics  Spring 2014

2

Efficient Tree Representation and Traversal

Tree Node and Primitive Ordering When a simultaneous traversal is used, four

recursive calls are generated in the node-node part of the traversal algorithm.

The order in which these recursive calls were issued is completely determined by the relative ordering of the nodes within the two trees. The performance of the program is governed by

such an order of those recursive calls. How can we determine the best order?

Page 3: Advanced Computer Graphics  Spring 2014

3

Efficient Tree Representation and Traversal

Tree Node and Primitive Ordering One possibility would be to attempt to figure

out at runtime the best order in which to issue the recursive calls. It is not easy, as nothing is known about the

structure further down the hierarchies at this point. A more feasible alternative (with no runtime

overhead) is to rearrange the children of a parent node heuristically during construction. The child more likely to lead to an early intersection

comes first.

Page 4: Advanced Computer Graphics  Spring 2014

4

Efficient Tree Representation and Traversal

Tree Node and Primitive Ordering A few possible ways to order the nodes

include Arrange children so that leaves come before nodes Put the smallest subtrees first Sort children by their hits-to-tests ratio Sort children from near to far along predominant

query direction.

Page 5: Advanced Computer Graphics  Spring 2014

5

Efficient Tree Representation and Traversal On Recursion

Recursion is not the most effective form in which to express the code. Exiting the recursion and immediately returning a value is not

directly supported in most languages Using a flag variable to tell the code to exit is not just an complicated

solution but requires the recursion to “unwind” all the way to the top. The ideal solution is to rewrite any recursive code using explicit

stacking. As the code is no longer recursive, it is possible to exit the function

at any time. It also provides the ability to interrupt and resume the code at any

point by simply saving away a few variable values. Having an explicit stack pointer makes it easy to limit traversals to a

specified maximum depth if so desired. Not using recursion also avoids the overhead of recursive function

calls.

Page 6: Advanced Computer Graphics  Spring 2014

6

Improved Queries Through Caching

Caching serves as a very important means of optimizing collision queries. By keeping track of various information about

the last collision query an object was involved in, that information can often be used in a subsequent query to speed it up by “jump-starting” the calculations, thanks to the spatial and temporal coherence objects tend to exhibit.

The information cashed can be classified in two types: positive and negative.

Page 7: Advanced Computer Graphics  Spring 2014

7

Improved Queries Through Caching

The information cashed can be classified in two types: positive and negative. Positive: it helps in more quickly detecting that

the two objects are colliding. Negative: It aids in determining the separation

of the two objects. Specific pieces of information that can help

quickly answer decision problems are referred to as witnesses.

Page 8: Advanced Computer Graphics  Spring 2014

8

Improved Queries Through Caching

Surface Caching: Caching Intersecting Primitives The most obvious caching scheme is to store one or more

overlapping primitives from each object when the objects are found colliding. These primitives serve as witnesses of the collision.

The next time the same two objects are tested for overlap these cached primitives can be tested first to see if they are still in collision. If so, a test query can immediately return without having to

examine the full hierarchies, worst case. The drawback is that this type of caching method only

works for test queries.

Page 9: Advanced Computer Graphics  Spring 2014

9

Improved Queries Through Caching

Two possible cache organizations for holding the stored primitives: a shared or a distributed cache.

A shared cache: a global cache that contains any number of pair entries Typically implemented as a hash table wherein the colliding

primitives are registered under a key constructed from the two object IDs.

The benefit is that the system easily handles an object being involved in any number of collisions.

Drawbacks include the cost of performing a hash table lookup and having to remove the pair when the objects are no longer in collision.

Page 10: Advanced Computer Graphics  Spring 2014

10

Improved Queries Through Caching

Two possible cache organizations for holding the stored primitives: a shared or a distributed cache. A distributed cache: a local cache wherein the

primitives are stored within the objects themselves. The benefit is that cached primitives are instantly

available. However, if only a single primitive is cached per object

the contact with multiple objects will cause the cache to be prematurely flushed, rendering it useless.

Page 11: Advanced Computer Graphics  Spring 2014

11

Improved Queries Through Caching

Front Tracking It is an advanced type of caching mechanism

that uses temporal coherence for speeding up collision queries.

It can be applied to any CD system that uses hierarchies of bounding volumes.

Page 12: Advanced Computer Graphics  Spring 2014

12

Improved Queries Through Caching

Front Tracking Two bounding volumes hierarchies are tested

against each other. Ones with three and seven nodes.

BV test tree

Page 13: Advanced Computer Graphics  Spring 2014

13

Improved Queries Through Caching

Front Tracking Each node in the BV test tree is a test between a

node in each tree. Not all nodes overlap when testing two BVs

against each other, and so the dashed line illustrates where the overlapping borders the nonoverlapping pairs. That is, above this line each pair of nodes overlap, and

below it they are disjoint. Node “C” overlaps b, so that the node “Cb” is above the line. Node “C” does not overlap “c”, so that node “Cc” is below.

Page 14: Advanced Computer Graphics  Spring 2014

14

Improved Queries Through Caching

Front Tracking The algorithm keeps track of a front, and this

front is the dashed line in the figure.

Page 15: Advanced Computer Graphics  Spring 2014

15

Improved Queries Through Caching

Front Tracking By storing this front for the next frame, testing

can start just above the front, thereby saving traversal time at the upper parts of the trees. The effect is the same as starting from the root of the

BV test tree. The BV test tree will not be the same from

frame to frame. If more nodes are found to overlap, then the BV test

tree grows, and the front is lowered. If a node is not overlapping any longer, then the front

has to be raised.

Page 16: Advanced Computer Graphics  Spring 2014

16

Improved Queries Through Caching

Front Tracking From tests on sphere tree, it is reported that an

average speedup of 40% for the basic front-updating method.

With deferred updating, an average speedup of 84% could be possible.

Page 17: Advanced Computer Graphics  Spring 2014

17

Spatial Partitioning

Spatial partitioning techniques provide broad-phase processing by dividing space into regions and testing if objects overlap the same region of space. Because objects can only intersect if they

overlap the same region of space, the number of pair-wise tests is drastically reduced.

Page 18: Advanced Computer Graphics  Spring 2014

18

Spatial Partitioning

Uniform Grids A very effective space subdivision scheme.

Overlay space with a regular grid. It divides space into a number of regions, or grid cells of

equal size. Each object is then associated with the cells it overlays.

As only objects overlapping a common cell could possibly be in contact, in-depth tests are only performed against those objects found sharing cells with the object tested for collision.

The farther apart two objects are the less likely they overlap the same cells.

Page 19: Advanced Computer Graphics  Spring 2014

19

Spatial Partitioning

Uniform Grids Uniformity of the grid – simple and fast

accessing to a cell. Given a specific cell, computing neighboring cells are

trivial to locate. It is a good and popular choice for space subdivision. Grids are known by many names: regions, buckets,

sectors and zones.

Page 20: Advanced Computer Graphics  Spring 2014

20

Spatial Partitioning

Cell Size Issues The grid is too fine. The grid is too coarse (with respect to object

size) The grid is too coarse (with respect to object

complexity) The grid is both too find and too coarse.

Page 21: Advanced Computer Graphics  Spring 2014

21

Spatial Partitioning

Cell Size Issues Having objects overlap only a small number of

cells is important. It limits the amount of work required to insert and

update objects in the grid, and to perform the actual overlap tests.

Page 22: Advanced Computer Graphics  Spring 2014

22

Spatial Partitioning

Storing objects in a grid. Grids as Arrays of Linked Lists

Allocate an array of corresponding dimension, mapping grid cells to array elements one-to-one. To handle the case of multiple objects ending up in a

given cell, each array element would point to a linked list of objects, or be NULL if empty.

The easiest way of maintaining these linked lists is to embed the links within the objects themselves.

A drawback with using a dense array of this type is that for large grids just storing the list headers in each grid cell becomes prohibitive in terms of memory requirements.

Page 23: Advanced Computer Graphics  Spring 2014

23

Spatial Partitioning

Storing objects in a grid. Hashed Storage

The most effective alternative to using a dense array to store the grid is to map each cell into a hash table of a fixed set of n buckets.

The buckets contain the linked lists of objects. The grid itself is conceptual and does not use any memory.

Page 24: Advanced Computer Graphics  Spring 2014

24

Spatial Partitioning

Storing objects in a grid. Hashed Storage

The most effective alternative to using a dense array to store the grid is to map each cell into a hash table of a fixed set of n buckets.

Storing Static Data When grid data is static (when it represents world polygon

data instead of dynamic objects), the need for a linked list can be removed altogether. Store all cells data in a single contiguous array.

Page 25: Advanced Computer Graphics  Spring 2014

25

Spatial Partitioning

Storing objects in a grid. Implicit Grids

An alternative to storing a grid explicitly is to store it implicitly as the Cartesian product of two or more arrays.

The grid is now represented by two arrays (three in 3D), wherein one array corresponds to the grid rows and the other to the grid columns.

Each array element points to a linked list of objects. An object is inserted into the grid by adding it to the lists of

the grid cells it overlaps for both the row and column array. This scheme can result in either fewer or more list insertions

compared to the dense array representation.

Page 26: Advanced Computer Graphics  Spring 2014

26

Spatial Partitioning

Storing objects in a grid. Implicit Grids

A 4 by 5 grid implicitly defined as the intersection of 9(4+5) linked lists. Five objects have been inserted into the lists and their implied positions in the grid are indicated.

Page 27: Advanced Computer Graphics  Spring 2014

27

Spatial Partitioning

Hierarchical Grids The most significant problem with uniform grids is their

inability to deal with objects of greatly varying sizes in a graceful way. When objects are much larger than the grid cell size and end

up overlapping many cells, updating the position of a moving object becomes very expensive.

If the cell size is adjusted to hold the larger objects, the grid becomes less spatially discriminatory as more small objects now end up in the same cell, with loss of performance as a result.

Such a size problem can effectively be addressed by the use of hierarchical grids, a grid structure particularly well suited to holding dynamically moving objects.

Page 28: Advanced Computer Graphics  Spring 2014

28

Spatial Partitioning

The hierarchical grid consists of a number of grids of varying cell sizes, all overlapping to cover the same space.

Given a hierarchy containing n levels of grids and letting rk represent the size of the grid cells at level k in the hierarchy, the grids are arranged in increasing cell size order, with r1 < r2 < .. < rn. Level 1 is the lowest level, and level n is the

highest level.

Page 29: Advanced Computer Graphics  Spring 2014

29

Spatial Partitioning

Page 30: Advanced Computer Graphics  Spring 2014

30

Spatial Partitioning

The grid cells can assume any relative size and should ideally be selected with respect to existing object sizes.

A simple procedure for setting up the hierarchy Let the cells at level 1 just encompass the smallest

objects. Then successively double the cell size so that cells of

level k+1 are twice as wide as cells of level k. Repeat the doubling process until the highest level cells

are large enough to encompass the largest objects.

Page 31: Advanced Computer Graphics  Spring 2014

31

Spatial Partitioning

Octree (and Quadtrees) It is an axis-aligned hierarchical partitioning of a volume

of 3D world space. Each parent node in the octree has eight children. Each node also has a finite volume associated with it. The root node volume is generally taken to be the

smallest axis-aligned cube fully enclosing the world. The volume is then subdivided into eight smaller equal-size

subcubes by simultaneously dividing the cube in half along each of the x,y, and z axes.

These subcubes form the child nodes of the root node.

Page 32: Advanced Computer Graphics  Spring 2014

32

Spatial Partitioning

Octree (and Quadtrees) Typical criteria for stopping the recursive creation of the

octree include the tree reaching a maximum depth or the cubes getting smaller than some preset minimum size.

Initially, all primitives in the world are associated with the root volume.

As the root volume is split, the set of primitives is reassigned to the child cells it overlaps, duplicating the primitives when they overlap more than one cell.

The procedure is repeated recursively.

Page 33: Advanced Computer Graphics  Spring 2014

33

Spatial Partitioning

Octree (and Quadtrees)

Page 34: Advanced Computer Graphics  Spring 2014

34

Spatial Partitioning

k-d Trees A generalization of octrees and quadtrees can be found in

the k-dimensional tree, or k-d tree. k represents the number of dimensions subdivided, which

does not have to match the dimensionality of the space used. The k-d tree divides space along one dimension at a time.

Traditionally, k-d trees are split along x, then y, then z, then x again, and so on, in a cyclic fashion.

Often the splitting axis is freely selected among the k dimensions.

One level of an octree can be seen as a three-level k-d tree split along x, then y, then z (all splits selected to divide the space in half).

Page 35: Advanced Computer Graphics  Spring 2014

35

Spatial Partitioning

k-d Trees

Page 36: Advanced Computer Graphics  Spring 2014

36

Spatial Partitioning

Hybrid Schemes The grid serves as a fast way of locating the

appropriate trees. Grids can also be used as “acceleration

structures” into a single tree hierarchy.

Page 37: Advanced Computer Graphics  Spring 2014

37

Spatial Partitioning

Sweep-and-Prune It exploits the temporal coherence most often

found in virtual environments. Temporal coherence means that objects

undergo small changes in their position and orientation from frame to frame.

This can be used to check if two AABBs overlap.

The three-dimensional problem for AABBs is solved by using the one-dimensional algorithm for each of the three main axes.

Page 38: Advanced Computer Graphics  Spring 2014

38

Spatial Partitioning

Sweep-and-Prune

Page 39: Advanced Computer Graphics  Spring 2014

39

Collision Response

Collision response is the action that should be taken to avoid interpenetration of objects.

The simplest technique is introduced here. What happens to a sphere’s motion at the time

of collision?

Assume that a sphere is moving towards a plane. v: the velocity vector of the sphere nᆞ x + d = 0.

Page 40: Advanced Computer Graphics  Spring 2014

40

Collision Response

Page 41: Advanced Computer Graphics  Spring 2014

41

Collision Response

The velocity vector v = vn + vp

vn = (vᆞ n)n vp = v – vn.

After the collision, the velocity vector is v’ = vp – nv

Here we have assumed that the response was totally elastic.

Page 42: Advanced Computer Graphics  Spring 2014

42

Collision Response

If some energy is lost at collision, then v’ = vp – kvn

As k gets smaller, more and more energy is lost, and the collision appears less and less bouncy.

More sophisticated collision response is based on physics, and involves creating a system of ODEs.