You are on page 1of 90

MASTER OF COMPUTER SCIENCE/MASTER OF SCIENCE IN COMPUTER SCIENCE

MCS 3206- Advanced Graphics and Gaming

Handout 6: Spatial Data Structures

Prasad Wimalaratne, PhD, SMIEEE


spw@ucsc.cmb.ac.lk

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING


Applications of Spatial Data Structures:
Computer Graphics, Image Processing and GIS

2
https://slideplayer.com/slide/10312288/

3
Spatial Data Structures
• Spatial data structures store data indexed in some way by their spatial
location
• For instance, store points according to their location, or polygons, …
• Before graphics, used for queries like “Where is the nearest hotel?” or
“Which stars are strong enough to influence the sun?”
• Multitude of uses in computer Graphics Applications
1. Visibility - What can I see?
2. Ray intersections - What did the player just shoot?
3. Collision detection - Did the player just hit a wall?
4. Proximity queries - Where is the nearest power-up?

4
Applications of Spatial Data structures in Graphics
• Collision Detection
• Ray Tracing
• Culling
• GIS

5
Spatial Decompositions
• Focus on spatial data structures that partition space into
regions, or cells, of some type

6
7
VTK

8
Insight Segmentation and Registration Toolkit (ITK)
Visualization ToolKit (VTK)
https://www.mevislab.de/mevislab/features/itk-and-vtk-integration/

9
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
• Octrees (Quadtrees in 3D): Axis aligned, regularly spaced planes cut
space into cubes (squares) /space into convex regions

10
Octrees
• Octrees are hierarchical tree structures used to represent solid
objects
• Octrees are particularly useful in applications that require cross
sectional views – for example medical applications
• Octrees are typically used when the interior of objects is
important

11
Octrees & Quadtrees
• Octrees are based on a two-dimensional representation
scheme called quadtree encoding
• Quadtree encoding divides a square region of space into four
equal areas
• These regions can then be arranged in a tree

12
Quadtree Example

13
Octrees
• Quadtree encodings provide considerable savings in storage
when large colour areas exist in a region of space
• An octree takes the same approach as quadtrees, but divides a
cube region of 3D space into octants
• Each region within an octree is referred to as a volume
element or voxel
• Division is continued until homogeneous regions are
discovered

14
Octrees (cont…)

15
Octrees (cont…)
• In 3 dimensions regions can be considered to
be homogeneous in terms of colour, material
type, density or any other physical
characteristics
• Voxels also have the unique possibility of
being empty

16
Octree Examples

17
Octree Examples (cont…)

18
Using Decompositions
• The best way to reduce the cost is with fast,
approximate queries that eliminate most objects
quickly
1. Trees with a containment property allow us to do this
2. The cell of a parent completely contains all the cells of its children
3. If a query fails for the cell, we know it will fail for all its children
4. If the query succeeds, we try it for the children
5. If we get to a leaf, we do the expensive query for things in the cell
• Spatial decompositions are most frequently used in
this way
• Visibility Processing: For example, if we cannot see any part of a cell,
we cannot see its children

19
Octree
• An octree is a tree data structure in which each internal node
has up to eight children.
• Octrees are most often used to partition a three dimensional
space by recursively subdividing it into eight octants.
• Octrees are the three-dimensional analog of quad-trees.
• The name is formed from oct + tree, and normally written
"octree", not "octtree".

20
Octree

21
Octree

22
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
• Objects can be assigned to nodes in one of two
common ways:
1. All objects are in leaf nodes
2. Each object is in the smallest node that fully contains it
• Ex: What are the benefits and problems with each of the two
approach?

23
Octree Node Data Structure

• What needs to be stored in a node?


1. Children pointers (at most eight)
2. Parent pointer - useful for moving about the tree
3. Extents of cube - can be inferred from tree structure, but easier to
just store it
4. Data associated with the contents of the cube
• Contents might be whole objects or individual polygons, etc
5. Neighbors are useful in some algorithms

24
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 Cell


• create the root node and call buildNode with all the objects

• Ex: When do we choose to stop creating children?

25
Example Construction

26
Assignment of Objects to Cells
• Basic operation is to intersect an object with
a cell
• Fast(est?) algorithm for polygons
• 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

27
Approximate Assignment
• Recall, we typically use spatial decompositions to
answer approximate queries
• Conservative approximation: We will sometimes answer yes for
something that should be no, but we will never answer no for
something that should be yes

28
Approximate Assignment
• Observation 1: If one polygon of an object is inside a cell,
most of its other polygons probably are also
• Ex : Should we store lists of objects or polygons?
• Observation 2: If a bounding volume for an object intersects
the cell, the object probably also does
• Ex: Should we test objects or their bounding volumes?

29
Objects in Multiple Cells
• Assume an object intersects more than one cell
• Typically store pointers to it in all the cells it intersects
• 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
• Better solution is to tag it with the frame number it was last tested

30
Neighboring Cells
• Sometimes it helps if a cell knows it’s neighbors
• How far away might they be in the tree? (How many links to reach
them?)
• Ex: How does neighbor information help with ray intersection?

31
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

32
Spatial Decompositions
• Kd-trees: Axis aligned planes, in alternating
directions, cut space into rectilinear regions
• BSP Trees: Arbitrarily aligned planes
• Many geometric queries are expensive to answer
precisely

33
Kd-trees
• A kd-tree is a tree with the following properties
• Each node represents a rectilinear region (faces
aligned with axes)
• Each node is associated with an axis aligned plane
that cuts its region into two, and it has a child for
each sub-region
• Kd-trees generalize octrees by allowing splitting
planes at variable positions
• Note that cut planes in different sub-trees at the
same level need not be the same
34
K-d Tree

35
KDtree

36
Kd-tree Example
4 6
1
10
8 2 3
11
3
13
2
4 5 6 7
12

9 8 9 10 11 12 13

5 1 7
37
38
Kd-tree Node Data Structure
• What needs to be stored in a node?
• Children pointers (always two)
• Parent pointer - useful for moving about the tree
• Extents of cell - xmax, xmin, ymax, ymin, zmax, zmin
• List of pointers to the contents of the cell
• Neighbors are complicated in kd-trees, so typically not stored

39
Building a Kd-tree
• Define a function, buildNode, that:
• Takes a node with its cell defined and a list of its contents
• Sets the splitting plane, 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 cell (how?), create the root node and call
buildNode with all the objects
• When do we choose to stop creating children?

40
Choosing a Splitting Plane
• Two common goals in selecting a splitting plane for each cell
• Minimize the number of objects cut by the plane
• Balance the tree: Use the plane that equally divides the objects into
two sets (the median cut plane)
• One possible global goal is to minimize the number of objects cut throughout
the entire tree
• One method (assuming splitting on plane perpendicular to x-axis):
• Sort all the vertices of all the objects to be stored according to x
• Put plane through median vertex, or locally search for low cut plane

41
Kd-tree Applications
• Kd-trees work well when axis aligned planes cut things into
meaningful cells

• Kd-trees are frequently used as data structures for other algorithms


– particularly in visibility

• Ex: Explain how spatial data structures are using Visibility


processing in Graphics Applications

42
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
• A generalization of kd-trees, and sometimes a kd-tree is called an axis-
aligned BSP tree
• Divides space into convex cells
• The industry standard for spatial subdivision in game environments
• General enough to handle most common environments
• Easy enough to manage and understand
• Significant performance gains

43
BSP

1. A is the root of the tree and the entire polygon


2. A is split into B and C
3. B is split into D and E.
4. D is split into F and G, which are convex and hence become
leaves on the tree.
44
45
BSP Example
1 1
2
2 4
3 out 5 7
A
A out B 6 8 out
3
C out D out
6 C5B

D
• Notes:
• Splitting planes end when they intersect
4 8 their parent node’s planes (e.g 2,3)
7
• Internal node labeled with planes, leaf
nodes with regions
46
http://underpop.free.fr/j/java/developing-games-in-java/1592730051_ch10lev1sec4.html

47
48
BSP Tree Node Data Structure
• What needs to be stored in a node?
• Children pointers (always two)
• Parent pointer - useful for moving about the tree
• If a leaf node: Extents of cell
• How might we store it?
• If an internal node: The split plane
• List of pointers to the contents of the cell
• Neighbors are useful in many algorithms
• Typically only store neighbors at leaf nodes
• Cells can have many neighboring cells

49
Building a BSP Tree
• Define a function, buildNode, that:
• Takes a node with its cell defined and a list of its contents
• Sets the splitting plane, creates the children nodes, divides the
objects among the children, and recurses on the children, or
• Sets the node to be a leaf node
• Create the root node and call buildNode with all the objects
• Qs?
• Do we need the root node’s cell? What do we set it to?
• When do we choose to stop creating children?

50
Choosing Splitting Planes
• Goals:
• Trees with few cells
• Objects not split across cells
• Some heuristics:
• Choose planes that are also polygon planes
• Choose large polygons first
• Choose planes that don’t split many polygons
• Try to choose planes that evenly divide the data
• Let the user select or otherwise guide the splitting process
• Random choice of splitting planes doesn’t do too badly
• Further Reading
• How BSP Work
• https://www.youtube.com/watch?v=Hebi641Ph7c

51
Spatial Partitionings

52
53
How can spatial partitioning help?
View-frustum culling

Ray-tracing
Uniform spatial
partitioning

Collision detection

54
What are they? How do they Compare?
Bounding Volume Spatial Partitioning
Hierarchies
• Hierarchical spatial representation
• Hierarchical object representation • Spatial subdivision
• Object subdivision • Hierarchical clustering of space
• Hierarchical clustering of objects • Spatial levels of detail
• Object levels of detail
• Classifies objects around regions of
• Classifies regions of space around
objects
space

Examples: Examples:
• OBB-trees – Uniform grids
• AABB-trees – Quadtrees & Octrees
• Sphere-trees – BSP-trees
• k-DOPs
– kD-trees
55
Bounding Volume Hierarchies
• So far, we have had subdivisions that break the world into cell
• General Bounding Volume Hierarchies (BVHs) start with a
bounding volume for each object
• Many possibilities: Spheres, AABBs, OBBs, k-dops, …
• Parents have a bound that bounds their children’s bounds
• Typically, parent’s bound is of the same type as the children’s
• Can use fixed or variable number of children per node
• No notion of cells in this structure
56
A bounding volume hierarchy (BVH)

• A bounding volume hierarchy (BVH) is a tree structure on a set


of geometric objects. All geometric objects are wrapped in
bounding volumes that form the leaf nodes of the tree. These
nodes are then grouped as small sets and enclosed within
larger bounding volumes. These, in turn, are also grouped and
enclosed within other larger bounding volumes in a recursive
fashion, eventually resulting in a tree structure with a single
bounding volume at the top of the tree

57
How Collisions Work in Games

https://www.youtube.com/watch?v=z7xMIRzIDpU
58
How can bounding volume hierarchies help?
View-frustum culling

Ray-tracing

Collision detection

59
How can bounding volume hierarchies help?
View-frustum culling

Ray-tracing

Collision detection

60
How can bounding volume hierarchies help?
View-frustum culling

Ray-tracing

Collision detection

61
How can bounding volume hierarchies help?
View-frustum culling

Ray-tracing

Collision detection

62
BVH Example

63
64
65
BVH Example

66
BVH Construction
1. Simplest to build top-down
• Bound everything
• Choose a split plane (or more), divide objects into sets
• Recurse on child sets
2. Can also build bottom up
• Bound individual objects, group them into sets, create parent, recurse

• Ex: “BVH can be built in top-down or bottom-up style?”


• Illustrate the answer with an appropriate examples

67
BVH Operations

• Some of the operations that work with BVHs


• Frustum culling
• Collision detection
• BVHs are good for moving objects
• Updating the tree is easier than for other methods
• Incremental construction also helps (don’t need to rebuild the whole tree if
something changes)

68
Visibility Culling
1. Back face culling
2. View-frustrum culling
3. Detail culling
4. Occlusion culling

69
View-Frustum Culling

• Remove objects that are outside the


viewing frustum
1. Construct bounding
volumes (BVs)
2. Create hierarchy
3. BV/V-F intersection
tests

Mostly done in “Application Stage”


70
View-Frustum Culling
• Culling against bounding volumes to save time
• Bounding volumes – AABB, OBB, Spheres, etc. – easy to
compute, as tight as possible

AABB Sphere OBB


71
View-Frustum Culling

• Often done hierarchically to save time

In-order, top-down
traversal and test

72
View-Frustum Culling
• Two popular hierarchical data structures – BSP Tree and Octree

Axis-Aligned BSP Polygon-Aligned BSP

Intersecting?
73
What is Portal Culling?
Portal culling is a method by which the 3D scene can be broken
into areas called cells, which are linked together by portals.
A portal is a window in 3D space that allows objects in one cell
(the "out" cell) to be seen from another cell (the "in" cell). Portal
culling works best in scenes where there is limited visibility from
one area to another, for example a building or network of caves.
It has very limited use in wide open scenes.

74
What is Portal Culling?
In this diagram the original camera
frustum is seen in blue, this is used to cull
the current cell.
The shaded areas represent areas visible
after culling.
The camera is looking through a portal
into the next cell, and so the frustum is
reduced (seen in cyan) so as to render
only that which is visible through the
portal.
This reduced frustum is then used to
check visibility of additional portals in
other cells, and it will be reduced again as
it passed through more portals (seen in
red and green). Note that even though
more cells are visible to the original
camera frustum, there is no direct line of
visibility and so they are not even
considered. 75
View-Frustum Culling
• Octree

• A parent has 8 children

• Subdivide the space until the


number of primitives within
each leaf node is less than a
threshold

• In-order, top-down traversal

76
Detail Culling

• A technique that sacrifices quality for speed


• Base on the size of projected BV – if it is too
small, discard it.
• Also often done hierarchically

Always helps to create a hierarchical


structure, or scene graph.
77
Occulusion Culling

• Discard objects that are occluded


• Z-buffer is not the smartest algorithm in the
world (particularly for high depth-
complexity scenes)
• We want to avoid the processing of invisible
objects

78
Hierarchical Visibility
• Object-space octree
• Primitives in a octree node are hidden if the octree
node (cube) is hidden
• A octree cube is hidden if its 6 faces are hidden
polygons
• Hierarchical visibility test:

• Refer : Occlusion Culling Algorithms


• http://www.gamasutra.com/view/feature/3394/oc
clusion_culling_algorithms.php?print=1
79
80
81
Testing a Node for Occlusion
• If the box representing a node is not visible then nothing in it is
either
• The faces of the box are projected onto the image plane and
tested for occlusion

82
83
View Frustum Culling
• the viewing frustum or view frustum is the region of space in the
modeled world that may appear on the screen; it is the field of view of
the notional camera.
• The exact shape of this region varies depending on what kind of camera
lens is being simulated, but typically it is a frustum of a rectangular
pyramid (hence the name).
• The planes that cut the frustum perpendicular to the viewing direction
are called the near plane and the far plane. Objects closer to the camera
than the near plane or beyond the far plane are not drawn.

84
View Frustum Culling
• Often, the far plane is placed infinitely far away from the
camera so all objects within the frustum are drawn
regardless of their distance from the camera.
• Viewing frustum culling or view frustum culling is the
process of removing objects that lie completely outside the
viewing frustum from the rendering process.
• Rendering these objects would be a waste of time since
they are not directly visible
• Ref http://www.lighthouse3d.com/opengl/viewfrustum/

85
Frustum Culling With Octrees

• We wish to eliminate objects that do not intersect the view


frustum
• Ex
• Which node/cell do we test first? What is the test?
• If the test succeeds, what do we know?
• If the test fails, what do we know? What do we do?

86
Frustum Culling With Octrees
• We wish to eliminate objects that do not intersect
the view frustum
• Have a test that succeeds if a cell may be visible
• Test the corners (e.g Min Max etc) of the cell
against each clip plane. If all the corners are
outside one clip plane, the cell is not visible
• Otherwise, is the cell itself definitely visible?
• Starting with the root node cell, perform the test
• If it fails, nothing inside the cell is visible
• If it succeeds, something inside the cell might be
visible
• Recurse for each of the children of a visible cell
87
Interference Testing

• Consider the problem of finding out which cells an object


interferes with (collides with)
• When do we need to answer such questions?
Ex: Consider a sphere and an octree
1. Which octree node do we start at?
2. What question do we ask at each node?
3. What action do we take at each node?

88
Ray Intersection Testing

• Consider the problem of finding which cells a ray


intersects
• Consider a ray (start and direction) and an octree
1. Which cell do we start at?
2. How does the algorithm proceed?

• Further Reading
• Spatial Partition: Game Programming PatternsOptimization
Patterns
• http://gameprogrammingpatterns.com/spatial-partition.html

89
From spatial data structures to Scnegraphs

• https://www.bogotobogo.com/Games/spatial
datastructure.php

90

You might also like