spatial subdivision graham rhodes senior software developer, applied research associates, inc
TRANSCRIPT
![Page 1: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/1.jpg)
Spatial Subdivision
Graham RhodesSenior Software Developer,Applied Research Associates, Inc.
![Page 2: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/2.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/3.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/4.jpg)
5
Spatial subdivision: what is it for?
●Optimization!
X
Y
![Page 5: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/5.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/6.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/7.jpg)
8
Grid-based Spatial Subdivision
![Page 8: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/8.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/9.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/10.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/11.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/12.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/13.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/14.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/15.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/16.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/17.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/18.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/19.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/20.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/21.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/22.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/23.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/24.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/25.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/26.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/27.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/28.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/29.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/30.jpg)
31
Avoiding duplicate tests
X
Y
●Bitfield, time stamping…
![Page 31: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/31.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/32.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/33.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/34.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/35.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/36.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/37.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/38.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/39.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/40.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/41.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/42.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/43.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/44.jpg)
45
Implementation: spatial hash
●Consider the following grid
X
Y
![Page 45: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/45.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/46.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/47.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/48.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/49.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/50.jpg)
51
Tree-based Spatial Subdivision
![Page 51: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/51.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/52.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/53.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/54.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/55.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/56.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/57.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/58.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/59.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/60.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/61.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/62.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/63.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/64.jpg)
65
Problems with hierarchical subdivision
●Not suitable for dynamic/moving objects
![Page 65: Spatial Subdivision Graham Rhodes Senior Software Developer, Applied Research Associates, Inc](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/65.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/66.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/67.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/68.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/69.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/70.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/71.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/72.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032805/56649ee85503460f94bf8f97/html5/thumbnails/73.jpg)
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