Spatial Data Structures - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Spatial Data Structures

Description:

We will build a BSP tree, in 2D, for a 3 room building. Ignoring doors ... Drawing Order from BSP Trees ... hold an entire BSP tree for an object, e.g. a car. ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 31
Provided by: course61
Category:

less

Transcript and Presenter's Notes

Title: Spatial Data Structures


1
Spatial Data Structures
2
Spatial Data Structures
  • Spatial data structures organize geometries
    indexed in some way by their spatial location
  • Often organized in a hierarchy, e.g. a tree
    structure.
  • Accelerate queries about whether geometric
    entities collide. Improve from O(n) time to O(log
    n) time.
  • Multitude of uses in computer games
  • Visibility - What can I see?
  • Ray intersections - What did the player just
    shoot?
  • Collision detection - Did the player just hit a
    wall?
  • Proximity queries - Where is the nearest
    power-up?

3
Bounding Volume Hierarchies
  • General Bounding Volume Hierarchies (BVHs) start
    with a bounding volume for each object
  • Many possibilities Spheres, AABBs, OBBs, k-dops,
  • A leaf node holds actual geometry to be rendered,
    and it doesnt have any children.
  • Parents have a BV that bounds their childrens
    BVs
  • Typically, parents bound is of the same type as
    the childrens
  • Can use fixed or variable number of children per
    node
  • The root has a BV that contains the entire scene.

4
BVH Example
5
BVH Construction
  • BVH is represented as a k-ary tree, k 2, 4, 8
    etc.
  • Build BVH bottom-up
  • Bound individual objects
  • Group them into parent node
  • Repeat until only one BV exists, which becomes
    the root.
  • Simplest to build top-down
  • Start by a BV that bounds everything
  • Choose a split plane (or more), divide objects
    into sets
  • Recurse on child sets
  • Can also be built incrementally
  • Insert one bound at a time, growing as required
  • Good for environments where things are created
    dynamically

6
BVH Operations
  • BVH is efficient for many important operations in
    computer games
  • Collision detection
  • B root BV
  • Collision (A, B)
  • if(overlap(A, B))
  • if(isLeaf(B))
  • Return the object in B
  • else
  • for each child C in B
  • Collision (A, C)
  • else return false
  • Frustum culling
  • Ray intersection
  • BVHs are good for moving objects
  • Updating the tree is relatively easy
  • Incremental construction also helps (dont need
    to rebuild the whole tree if something changes)

7
Hierarchical Frustum Culling
  • We wish to eliminate objects that do not
    intersect the view frustum
  • Done on CPU. Invisible objects are not sent to
    the graphics pipeline
  • Start from the root, test the BV against the
    viewing frustum.
  • If the BV is fully outside the frustum, the node
    is not processed further
  • If the BV is fully inside the frustum, all
    objects in the BV are visible and no more testing
    necessary.
  • If the BV intersects the frustum, recursive for
    each of its children.
  • Similar algorithm exists for other spatial data
    structures.

8
Spatial Decompositions
  • Focus on spatial data structures that partition
    space into regions, or cells, of some type
  • Generally, cut up space with planes that separate
    regions
  • Almost always based on tree structures (surprise,
    huh?)
  • Octrees (Quadtrees) Axis aligned, regularly
    spaced planes cut space into cubes (squares)
  • Kd-trees Axis aligned planes, in alternating
    directions, cut space into rectilinear regions
  • BSP Trees Arbitrarily aligned planes cut space
    into convex regions

9
Octree
  • Root node represents a cube containing the entire
    world
  • Then, recursively, the eight children of each
    node represent the eight sub-cubes of the parent
  • Quadtree is for 2D decompositions - root is
    square and four children are sub-squares
  • What sorts of games might use quadtrees instead
    of octrees?
  • Objects can be assigned to nodes in one of two
    common ways
  • All objects are in leaf nodes
  • Each object is in the smallest node that fully
    contains it
  • What are the benefits and problems with each
    approach?

10
Octree Node Data Structure
  • What needs to be stored in a node?
  • Children pointers (at most eight)
  • Parent pointer - useful for moving about the tree
  • Extents of cube - can be inferred from tree
    structure, but easier to just store it
  • Data associated with the contents of the cube
  • Contents might be whole objects or individual
    polygons, or even something else
  • Neighbors are useful in some algorithms (but not
    all)

11
Building an Octree
  • Define a function, buildNode, that
  • Takes a node with its cube set and a list of its
    contents
  • Creates the children nodes, divides the objects
    among the children, and recurses on the children,
    or
  • Sets the node to be a leaf node
  • Find the root cube (how?), create the root node
    and call buildNode with all the objects
  • When do we choose to stop creating children?
  • Maximum recursion level reached or fewer than a
    threshold number of primitives in a box.
  • Is the tree necessarily balanced?

12
Example Construction
13
Assignment of Objects to Cells
  • Basic operation is to intersect an object with a
    cell
  • What can we exploit to make it faster for
    octrees?
  • Fast(est?) algorithm for polygons (Graphics Gem
    V)
  • Test for trivial accept/reject with each cell
    face plane
  • Look at which side of which planes the polygon
    vertices lie
  • Note speedups Vertices outside one plane must be
    inside the opposite plane
  • Test for trivial reject with edge and vertex
    planes
  • Planes through edges/vertices with normals like
    (1,1,1) and (0,1,1)
  • Test polygon edges against cell faces
  • Test a particular cell diagonal for intersection
    with the polygon
  • Information from one test informs the later
    tests. Code available online

14
Objects in Multiple Cells
  • Assume an object intersects more than one cell
  • Typically store pointers to it in all the cells
    it intersects
  • Why cant we store it in just one cell? Consider
    the ray intersection test
  • But it might be considered twice for some tests,
    and this might be a problem
  • One solution is to flag an object when it has
    been tested, and not consider it again until the
    next round of testing
  • Why is this inefficient?
  • Better solution is to tag it with the frame
    number it was last tested
  • Subtle point How long before the frame counter
    overflows?

15
Neighboring Cells
  • Sometimes it helps if a cell knows it neighbors
  • How far away might they be in the tree? (How many
    links to reach them?)
  • Neighbors of cell A are cells that
  • Share a face plane with A
  • Have all of As vertices contained within the
    neighbors part of the common plane
  • Have no child with the same property

16
Finding Neighbors
  • Your right neighbor in a binary tree is the
    leftmost node of the first sub-tree on your right
  • Go up to find first rightmost sub-tree
  • Go down and left to find leftmost node (but dont
    go down further than you went up)
  • Symmetric case for left neighbor
  • Find all neighbors for all nodes with an in-order
    traversal
  • Natural extensions for quadtrees and octrees

17
Octree Problems
  • Octrees become very unbalanced if the objects are
    far from a uniform distribution
  • Many nodes could contain no objects
  • The problem is the requirement that cube always
    be equally split amongst children

A bad octree case
18
BSP Trees
  • Binary Space Partition trees
  • A sequence of cuts that divide a region of space
    into two
  • Cutting planes can be of any orientation
  • an axis-aligned BSP tree is called a kd-tree.
  • The industry standard for spatial subdivision in
    game environments
  • General enough to handle most common environments
  • Easy enough to manage and understand
  • Big performance gains

19
Constructing a BSP Tree
  • The Binary Space Partitioning (BSP) algorithm
  • 1. Select a partitioning plane/facet.
  • 2. Partition the remaining planes/facets
    according to the side of the partitioning plane
    that they fall on ( or -).
  • 3. Repeat recursively with each of the two new
    sets.
  • Partitioning requires testing all facets in the
    active set to find if they lie entirely on the
    positive side of the partition plane, entirely on
    the negative side, or if they cross it. In the
    case of a crossing facet we clip it into two
    halves.

20
BSP-Tree Example
A
A
C
4
-
3

B
C
-
B
-


1
3
2
4
1
2
21
Building Example
  • We will build a BSP tree, in 2D, for a 3 room
    building
  • Ignoring doors
  • Splitting edge order is shown
  • Back side of edge is side with the number

5
2
3
4
1
6
22
Building Example (1)
5
1

-
3a, 4a, 6
2, 3b, 4b, 5
2
3b
4b
1
4a
3a
6
23
Building Example (2)
5a
5b
1

-
3a, 4a, 6
2
-

2
3b
4b
3b, 5b
4b, 5a
1
4a
3a
6
24
Building Example (3)
5a
5b
1

-
2
3a
-


2
3b
4b
4b, 5a
4a, 6
3b

5b
1
4a
3a
6
25
Building Example (Done)
5a
5b
1

-
2
3a
-


2
3b
4b
3b
4a
4b



6
5b
5a
1
4a
3a
6
26
Drawing Order from BSP Trees
  • BSP tress can be used to order polygons from back
    to front, or front to back
  • Descend tree with viewpoint
  • Things on the same side of a splitting plane as
    the viewpoint are always in front of things on
    the far side
  • Draw a scene from back to front.
  • Starting from the root of the tree.
  • 1. Classify viewpoint as being in the positive or
    negative half-space of our plane
  • 2. recursively call this routine with the
    opposite half-space
  • 3. Draw the current partitioning plane
  • 4. recursively call this routine with the same
    half-space

27
BSP tree rendering Example
5a
5b
1

-
2
3a
-


2
3b
4b
3b
4a
4b



6
5b
5a
1
4a
3a
6
28
BSP in Current Games
  • Use a BSP tree to partition space as you would
    with an octree or kd-tree
  • Leaf nodes are cells with lists of objects
  • Cells typically roughly correspond to rooms,
    but dont have to
  • The polygons to use in the partitioning are
    defined by the level designer as they build the
    space
  • A brush is a region of space that contributes
    planes to the BSP
  • Artists lay out brushes, then populate them with
    objects
  • Additional planes may also be specified
  • Sky planes for outdoor scenes, that dip down to
    touch the tops of trees and block off visibility
  • Planes specifically defined to block sight-lines,
    but not themselves visible

29
BSP Tree and Frustum Culling
  • You have a BSP tree, and a view frustum
  • With near and far clip planes
  • At each splitting plane
  • Test the boundaries of the frustum against the
    split plane
  • What if the entire frustum is on one side of the
    split plane?
  • What if the frustum intersects the split plane?
  • What do you do when you get to a leaf?

30
Scene Graph
  • The scene graph is a spatial tree structure that
    is augmented with textures, transforms, render
    states (material properties, ), light sources,
    and whatever else is found suitable.
  • A light source can be put at an internal node,
    which only affects the contents of its subtree.
  • A node in the scene graph often has a BV, thus
    similar to a BVH
  • A leaf in the scene graph stores geometry, which
    may hold an entire BSP tree for an object, e.g. a
    car.
  • Transform can be put in an internal node to
    realize hierarchical transformations.
Write a Comment
User Comments (0)
About PowerShow.com