spatial subdivision graham rhodes senior software developer, applied research associates, inc

73
Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc.

Upload: gary-blair

Post on 02-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

Spatial Subdivision

Graham RhodesSenior Software Developer,Applied Research Associates, Inc.

Page 2: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

How do you find a needle in a haystack?

2Image is in the public domain. Painting date 1874. File source is http://commons.wikimedia.org/wiki/File:Haystacks_Autumn_1873_Jean-Francois_Millet.jpg

Page 3: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

4

Spatial subdivision: what is it?

●A structured partitioning of geometry

X

Y

Page 4: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

5

Spatial subdivision: what is it for?

●Optimization!

X

Y

Page 5: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

6

Spatial subdivision: what is it for?

●Optimization!● Manage rendering overhead● Support a geometry paging system● Minimize unnecessary geometry interrogation

and pair-wise object tests for physics and AI●Not all games need this!

● (many do)

Page 6: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

7

Classes of spatial subdivision

●Grid-based●Tree-based●Others

● Bounding Volume Hierarchy● Scene Graph● Portals

Page 7: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

8

Grid-based Spatial Subdivision

Page 8: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

9

Overview of uniform grids

●A 2 x 1 grid

X

Y

Page 9: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

10

Overview of uniform grids

●A 2 x 2 grid

X

Y

Page 10: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

11

Overview of uniform grids

●A 4 x 3 grid

X

Y

Page 11: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

12

Overview of uniform grids

●A 4 x 3 grid

X

Y

Page 12: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

13

Overview of uniform grids

●The spatial and dimensional properties

numX = 4

numY = 3

cellSize.x

cellSize.y

origin

X

Y

Page 13: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

14

Overview of uniform grids

●Spatial index of a cell: (i, j) or (i, j, k)

X, i

Y, j

(0,0)

(i,j)

Page 14: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

15

Overview of uniform grids

●Logical address of a cell: memory location

X, i

Y, jCellAddress = f(i, j) (i,j)

Page 15: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

16

Implementation: ideas

●Conceptual data structuresGrid2D{ …Container<Cell> gridCells;

}

Cell{Container<Object> gameObjects;

}

X

Y

Page 16: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

17

Implementation: array of cells

●A naïve UniformGrid data structure

X

Y

NaiveUniformGrid2D{ …Array<Cell> gridCells;

}

Cell{Container<Object> gameObjects;

}

Page 17: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

18

Implementation: array of cells

●Retrieving the cell at a point in space

0 1 2 3 4 5 6 7 8 9 10 11

gridCells arrayX

Y

Page 18: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

19

0 1 2 3 4 5 6 7 8 9 10 11

Implementation: array of cells

●Retrieving the cell at a point in space

X

Y

gridCells array

0 1 2 3

4 5 6 7

8 9 10 11

Page 19: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

20

0 1 2 3 4 5 6 7 8 9 10 11

Implementation: array of cells

●Retrieving the cell at a point in space

X

Y

(i,j) = (2,0)cellAddress = 2gridCells array

const int X = 0, Y = 1;int getCellIndex(int d, Vector2 pt) { return (int)(floor((p[d] – origin[d])/cellSize[d]));}

int getCellAddress(Vector2 pt){ int i = getCellIndex(X, pt); int j = getCellIndex(Y, pt);

return (numX * j) + i;}

(i,j)

Page 20: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

21

Grid-size selection strategies

●What size should we choose for the grid cells?

X

Y

Page 21: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

22

Grid-size selection strategies

●What size should we choose for the grid cells?

X

Y

Page 22: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

23

Grid-size selection strategies

●What size should we choose for the grid cells?

X

Y

Page 23: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

24

Grid-size selection strategies

●Optimum size ~ max object size + e

X

Y

Page 24: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

25

Grid-size selection strategies

●What if object size varies significantly?

X

Y

Page 25: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

26

Populating the grid

● Inserting an object into the grid

X

Y

Page 26: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

27

Populating the grid

● Insert into every overlapped cell

X

Y

void addObject(Object obj){ pt = obj.minAABBPoint(); addrLL = getCellAddress(pt); addrLR = addrLL + 1; addrUL = addrLL + numX; addrUR = addrUL + 1; gridCells[addrLL].add(obj); gridCells[addrLR].add(obj); gridCells[addrUL].add(obj); gridCells[addrUR].add(obj); }

0 1 2 3 4 5 6 7 8 9 10 11gridCellsarray

LL LR

UL UR

indexLL = (2,1)indexLR = (3,1)indexUL = (2,2)indexUR = (3,2)

Page 27: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

28

Populating the grid

● Inserting into one cell (others are implicit)

X

Y

void addObject(Object obj){ pt = obj.minAABBPoint(); addr = getCellAddress(pt); gridCells[addr].add(obj);}

0 1 2 3 4 5 6 7 8 9 10 11gridCellsarray

baseIndex = (2,0)

Page 28: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

29

Pairwise testing: visit which cells?

● If insert objects into every overlapped cell

X

Y

gridCellsarray

0 1 2 3 4 5 6 7 8 9 10 11

Page 29: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

30

Pairwise testing: visit which cells?

● If insert objects only into one key cell

X

Y

0 1 2 3 4 5 6 7 8 9 10 11gridCellsarray

Page 30: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

31

Avoiding duplicate tests

X

Y

●Bitfield, time stamping…

Page 31: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

32

Ray intersection/line of sight tests

●Find all objects that intersect a ray

X

Y

Page 32: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

33

Ray intersection/line of sight tests

●Find all objects that intersect a ray

X

Y

Page 33: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

34

Ray intersection

●Walking along the ray X

Y

dit

dit

dit

dit

t

Page 34: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

35

Ray intersection

●Walking along the ray X

Y

djt

djt

djt

t

Page 35: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

36

Ray intersection

●Walking along the ray X

Y

texit,i

texit,j

tcurrent

Page 36: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

37

Ray intersection

●Walking along the ray X

Y

texit,j

texit,i = texit,i + dit

tcurrent = texit,i

Page 37: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

38

Ray intersection

●Walking along the ray X

Y

texit,j = texit,j + djt

texit,i

tcurrent = texit,j

Page 38: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

39

Ray intersection

●Walking along the ray X

Y

texit,j texit,i

tcurrent

Page 39: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

40

Ray intersection

● Initializing the walk X

Y

�⃗�𝑜

(i,j) (i+1,j)

�⃗��⃗� (𝑡𝑒𝑥𝑖𝑡 , 𝑖 )

float getCellPos(int d, int index) { return (((float)index) * cellSize[d]) + origin[d]; }

CELLPOS(x, i) CELLPOS(x, i+1)

Page 40: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

41

Avoiding duplicate tests

X

Y

●Time stamping easier than with pairwise tests

Page 41: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

42

Avoiding duplicate tests

X

Y

●May find an intersection in a different cell

Page 42: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

43

Avoiding duplicate tests

X

Y

●Batch ray tests as optimization strategy

Page 43: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

44

Back to array of cells

●Does anyone see a problem with this naïve approach?

● Most cells are likely empty● Doesn’t scale well due in part to large

memory requirements●For these reasons, this naïve array of cells

approach is often a bad choice in practice

Page 44: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

45

Implementation: spatial hash

●Consider the following grid

X

Y

Page 45: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

46

Implementation: spatial hash

●A multiplicative hash based on cell indices assigns each cell to a bucket

X

Y

0 1 2 3 4 5 6 7 8 9 … NB

(2,0)

(2,1)

(0,2) (1,2) (3,2)

(3,3)

cellBuckets

Page 46: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

Implementation: spatial hash

●Each bucket contains a list of cells

X

Y

47

0 1 3 4 5 6 7 8 9 … NB2

(2,0)

(2,1)

(0,2) (1,2) (3,2)

(3,3)

(2,1) cellContents

(1,2) cellContents

Page 47: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

48

Implementation: spatial hash

●Spatial hash grid data structuresBucket{ Container<BucketRecord> records;}

BucketRecord{ IntVector2 cellIndex; Cell cellContents;}

SpatialHashGrid2D{ … Array<Bucket> cellBuckets;}

Page 48: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

49

Implementation: spatial hash

●Spatial hash gridint getCellIndex(int d, Vector2 pt) { return (int)(floor(p[d]/cellSize[d])); }float getCellPos(int d, int index) { return ((float)index) * cellSize[d]; }

int prime1 = 0xAB1D261;int prime2 = 0x16447CD5;

int bucketAddress = (prime1 * i + prime2 * j) % numBuckets;

Page 49: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

50

Implementation: spatial hash

●Spatial hash gridCell getCell(Vector2 pt){ int bucketAddress = getBucketAddress(pt); IntVec2 index = { getCellIndex(X, pt), getCellIndex(X, pt) }; if (!cellBuckets[bucketIndex].contains(bucketAddress)) { cellBuckets[bucketIndex].insert(new BucketRecord({ cellIndex = index, cellContents = new Cell})); } return record.recordAt(bucketAddress);}

Page 50: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

51

Tree-based Spatial Subdivision

Page 51: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

52

Overview of hierarchical subdivision

●A recursive partitioning of space

●Objects appear to the “left” or “right” of the partition boundary

X

Y

A

C

B

Page 52: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

53

Overview of hierarchical subdivision

●Notice that we can represent this as a binary tree

A

C

B

A

C B

X

Y

Page 53: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

54

Implementation: Kd-tree

●A Kd-tree is an axis-aligned BSP tree

A

C

BX

Y

A

C B

Page 54: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

55

Implementation: Kd-tree

●Data structures for a Kd-treeKdTree { KdNode rootNode; }

KdNode{ int nodeType; int splitAxis; float splitPos; union { KdNode *childNodes; Container<Object> gameObjects; }}

A

C

B

splitAxis = x

A.x < splitPosB.x > splitPos

x = splitPos

X

Y

Page 55: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

56

Implementation: Kd-tree

●Locating a node given a point

0

1 2

3 4

C

splitAxis = x

splitAxis = y A

C

B

�⃗�

.x > node[0].splitPos

.y < node[2].splitPosA

X

Y

B�⃗�

Page 56: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

57

Implementation: Kd-tree

●Locating a node given a pointKdNode findNode(Vector2 pt){ currentNode = rootNode; while (currentNode.hasChildren) { if (pt[currentNode.splitAxis] <= currentNode.splitPos) currentNode = currentNode.childNodes[0]; else currentNode = currentNode.childNodes[1]; }

return currentNode;}

Page 57: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

58

Implementation: Kd-tree

●Ray intersection

A

C

BX

Y

D

F

A

DC

G

B

H

EGE

II

J

H JJF

Page 58: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

59

Implementation: Kd-tree

●Ray intersection

A

C

BX

Y

DE

F

G

H

I

J

A

DC B

GE

I H JJF

Page 59: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

60

Implementation: Kd-tree

●Ray intersection

A

C

BX

Y

DE

F

G

H

I

J

A

DC B

GE

I H JJF

Page 60: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

61

Implementation: Kd-tree

●Ray intersection

A

C

BX

Y

DE

F

G

H

I

J

A

DC B

GE

I H JJF

Page 61: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

62

Implementation: Kd-tree

●Ray intersection

A

C

BX

Y

DE

F

G

H

I

J

A

DC B

GE

I H JJF

Page 62: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

63

Implementation: Kd-tree● Constructing a cost-optimized tree

● Cost(cell) = Cost(traverse) + Probability(left hit)*Cost(left hit)+

Probability(right hit)*Cost(right hit)● Isolate complexity and seek large empty spaces● Deeply subdivided trees tend to be more efficient on modern hardware● Profile performance for your use case

Page 63: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

64

Implementation: quadtree/octree

● If desired, a quadtree/octree can be implemented via a Kd-tree

X

YA

C

B

Page 64: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

65

Problems with hierarchical subdivision

●Not suitable for dynamic/moving objects

Page 65: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

66

Memory cache considerations

● Typically 3-4 classes of system memory● L1 cache● L2 cache● L3 cache● Main memory

● Penalty to access to main memory w/cache miss● 50-200 clock cycles vs. 1-2 cycles for L1 cache hit

● Desirable to minimize occurrence of cache miss

Page 66: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

67

Memory cache considerations

● Cache memory population● Cache lines on modern hardware are usually 32 or 64

bytes

Chipset/Processor L1 Data Cache Line Size

Intel i7 64 bytes

Intel Atom 64 bytes

AMD Athlon 64 64 bytes

AMD Jaguar (Xbox One/PS4) 64 bytes

ARM Cortex A8 64 bytes

ARM Cortex A9 32 bytes

Page 67: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

68

Cache considerations for grid

●Linked lists are bad. Real bad.●Minimize structure size for cell bucket

● Bucket record stores spatial index and pointer to cell. Cell data stored elsewhere

● Closed hashing● Structure packing● Align buckets to cache-line boundaries

●C++11 std::aligned_storage

Page 68: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

69

Cache considerations for Kd-tree

● With care and compromise, we can put a lot of tree into a single L1 cache line

● Apply Christer Ericson’s bit packing approach● Cell data stored separate from tree itself● Binary heap data structure● Align structure to 64-byte boundary● A 64-byte cache line can store a fully subdivided 4 level

Kd-tree● With 4 bytes left over to store sub-tree pointers

Page 69: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

70

Cache considerations for Kd-tree

●Ericson’s Compact KdNodeUnion CompactKdNode{ float splitPos_type; uint32 cellDataIndex_type;} Mantissa

splitPos

cellDataIndex

031

Page 70: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

71

Cache considerations for Kd-tree

●Ericson’s Compact KdNode

031

Mantissa

splitPos_type

cellDataIndex_type

Node T

ype

0 0 Interior, axis=x 1 0 Interior, axis=y 0 1 Interior, axis=z 1 1 Leaf

Union CompactKdNode{ float splitPos_type; uint32 cellDataIndex_type;}

Page 71: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

72

Cache considerations for Kd-tree

●Ericson’s Compact KdNode● 4 level Kd-tree = 15 nodes● 15 x 4 bytes = 60 bytes● 4 bytes left point to sub-trees

031

splitPos_type

cellDataIndex_type

Page 72: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

73

References● Ericson, Christer, Real-Time Collision Detection, Morgan Kaufmann

Publishers/Elsevier, 2005● Hughes, John F., et al., Computer Graphics Principles and Practice, Third Edition,

Addison-Wesley, 2014● Pharr, Matt, and Greg Humphreys, Physically-based Rendering, Morgan Kaufmann

Publishers/Elsevier, 2004● Stoll, Gordon, “Ray Tracing Performance,” SIGGRAPH 2005.● Pascucci, Valerio, and Randall J. Frank, “Global Static Indexing for Real-time

Exploration of Very Large Regular Grids,” Supercomputing, ACM/IEEE 2001 Conference, 2001

● http://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/

● http://www.bigprimes.net/● http://www.7-cpu.com

Page 73: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc

Any Questions?

74Image is in the public domain. Painting date 1905. file source is http://commons.wikimedia.org/wiki/File:Hooiberg_by_emile_claus.jpg