Hierarchical Data Structure in Game Programming Yanci Zhang Game Programming Practice.

Slides:



Advertisements
Similar presentations
Christian Lauterbach COMP 770, 2/16/2009. Overview  Acceleration structures  Spatial hierarchies  Object hierarchies  Interactive Ray Tracing techniques.
Advertisements

Nearest Neighbor Search

Efficient access to TIN Regular square grid TIN Efficient access to TIN Let q := (x, y) be a point. We want to estimate an elevation at a point q: 1. should.
CSE 681 Bounding Volumes. CSE 681 Bounding Volumes Use simple volume enclose object(s) tradeoff for rays where there is extra intersection test for object.
Collision Detection CSCE /60 What is Collision Detection?  Given two geometric objects, determine if they overlap.  Typically, at least one of.
Collision Detection and Resolution Zhi Yuan Course: Introduction to Game Development 11/28/
Computer graphics & visualization Collisions. computer graphics & visualization Simulation and Animation – SS07 Jens Krüger – Computer Graphics and Visualization.
Ray Tracing CMSC 635. Basic idea How many intersections?  Pixels  ~10 3 to ~10 7  Rays per Pixel  1 to ~10  Primitives  ~10 to ~10 7  Every ray.
Searching on Multi-Dimensional Data
CSE 381 – Advanced Game Programming Scene Management
Week 14 - Monday.  What did we talk about last time?  Bounding volume/bounding volume intersections.
Multiple-key indexes Index on one attribute provides pointer to an index on the other. If V is a value of the first attribute, then the index we reach.
Ray Tracing Acceleration Structures Solomon Boulos 4/16/2004.
Bounding Volume Hierarchy “Efficient Distance Computation Between Non-Convex Objects” Sean Quinlan Stanford, 1994 Presented by Mathieu Brédif.
Tomas Mőller © 2000 Speeding up your game The scene graph Culling techniques Level-of-detail rendering (LODs) Collision detection Resources and pointers.
Introduction General Data Structures - Arrays, Linked Lists - Stacks & Queues - Hash Tables & Binary Search Trees - Graphs Spatial Data Structures -Why.
Introduction General Data Structures - Arrays, Linked Lists - Stacks & Queues - Hash Tables & Binary Search Trees - Graphs Spatial Data Structures -Why.
OBBTree: A Hierarchical Structure for Rapid Interference Detection Gottschalk, M. C. Lin and D. ManochaM. C. LinD. Manocha Department of Computer Science,
Bounding Volume Hierarchies and Spatial Partitioning Kenneth E. Hoff III COMP-236 lecture Spring 2000.
Efficient Distance Computation between Non-Convex Objects by Sean Quinlan presented by Teresa Miller CS 326 – Motion Planning Class.
1 Advanced Scene Management System. 2 A tree-based or graph-based representation is good for 3D data management A tree-based or graph-based representation.
Efficient Distance Computation between Non-Convex Objects By Sean Quinlan Presented by Sean Augenstein and Nicolas Lee.
Collision Detection David Johnson Cs6360 – Virtual Reality.
10/11/2001CS 638, Fall 2001 Today Kd-trees BSP Trees.
10/02/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Octrees.
BSP Trees, Quadtrees & Octrees Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, January 28, 2004.
Computer Graphics 2 Lecture x: Acceleration Techniques for Ray-Tracing Benjamin Mora 1 University of Wales Swansea Dr. Benjamin Mora.
Data Structures for Computer Graphics Point Based Representations and Data Structures Lectured by Vlastimil Havran.
Spatial Data Structures Jason Goffeney, 4/26/2006 from Real Time Rendering.
Managing Your Objects Paul Taylor 2009 Assignment Due Fri 5/6/2009.
10/09/2001CS 638, Fall 2001 Today Spatial Data Structures –Why care? –Octrees/Quadtrees –Kd-trees.
1 Speeding Up Ray Tracing Images from Virtual Light Field Project ©Slides Anthony Steed 1999 & Mel Slater 2004.
CSE 381 – Advanced Game Programming Quickhull and GJK.
CS-378: Game Technology Lecture #10: Spatial Data Structures Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran.
12/4/2001CS 638, Fall 2001 Today Managing large numbers of objects Some special cases.
PRESENTED BY – GAURANGI TILAK SHASHANK AGARWAL Collision Detection.
1 KIPA Game Engine Seminars Jonathan Blow Ajou University December 6, 2002 Day 10.
CIS 350 – I Game Programming Instructor: Rolf Lakaemper.
Chapter 11 Collision Detection 가상현실 입문 그래픽스 연구실 민성환.
Spatial Indexing Techniques Introduction to Spatial Computing CSE 5ISC Some slides adapted from Spatial Databases: A Tour by Shashi Shekhar Prentice Hall.
Ray Tracing II. HW1 Part A due October 10 Camera module Object module –Read from a file –Sphere and Light only Ray tracer module: –No shading. No reflection.
Presented by Paul Phipps
Maths & Technologies for Games Spatial Partitioning 2
CSE554Contouring IISlide 1 CSE 554 Lecture 5: Contouring (faster) Fall 2015.
CSE554Contouring IISlide 1 CSE 554 Lecture 3: Contouring II Fall 2011.
CSE554Contouring IISlide 1 CSE 554 Lecture 5: Contouring (faster) Fall 2013.
1 CSCE 441: Computer Graphics Hidden Surface Removal Jinxiang Chai.
BSP Tree Supplement Jyun-Ming Chen. Fall Kd-Tree and BSP tree kd-tree is a special kind of bsp tree.
Ray Tracing Optimizations
David Luebke 3/5/2016 Advanced Computer Graphics Lecture 4: Faster Ray Tracing David Luebke
Ray Tracing Acceleration (5). Ray Tracing Acceleration Techniques Too Slow! Uniform grids Spatial hierarchies K-D Octtree BSP Hierarchical grids Hierarchical.
1 Advanced Scene Management. 2 This is a game-type-oriented issue Bounding Volume Hierarchies (BVHs) Binary space partition trees (BSP Trees) “Quake”
Ray Tracing Acceleration (3)

CSE 554 Lecture 5: Contouring (faster)
Bounding Volume Hierarchies and Spatial Partitioning
Distance Computation “Efficient Distance Computation Between Non-Convex Objects” Sean Quinlan Stanford, 1994 Presentation by Julie Letchner.
Collision Detection Spring 2004.
Bounding Volume Hierarchies and Spatial Partitioning
Deformable Collision Detection
Modeliranje kompleksnih modelov
Parts of these slides are based on
KD Tree A binary search tree where every node is a
Quadtrees 1.
Collision Detection.
Deformable Collision Detection
BVH Student: Jack Chang.
David Johnson Cs6360 – Virtual Reality
Modeliranje kompleksnih modelov
Presentation transcript:

Hierarchical Data Structure in Game Programming Yanci Zhang Game Programming Practice

 Overview of hierarchical data structures  Spatial partition  Bounding volume hierarchies Outline Game Programming Practice

 STL and Boost provide excellent implementations for most of basic data structures  Dynamic array, list, queue, hash table, graph…  Do not implement these data structures by yourself  Basic data structures are far from meeting all the requirements in game programming Basic Data Structures Game Programming Practice

 Bases for many advanced data structures in game programming  Typical applications  Scene graph  State graph  Decision tree  Kd-tree, quad tree  … Tree and Graph Game Programming Practice

 Tree data structure  Organize spatial data to implement querying, searching efficiently  Widely used in game: collision detection, ray tracing…  An example in our daily life  University -> College -> Grade -> Major Hierarchical Data Structure Game Programming Practice

 Task: find the closest intersection between a ray and multiple objects  Solution1: for-loop  Simple  Low efficiency Example Example 1/2 t_min = MAX_FLT; FOR EACH object { float t = computeIntersection(object); if (t < t_min) t_min = t; } Game Programming Practice

 Solution2: organize objects wisely  Complicate solution  High efficiency  Not necessary to compute intersections between ray and all objects  Early exit Example Example 2/2 Game Programming Practice

 Spatial partition  Typical data structures: quadtree, octree, BSP, KD-tree…  Object-based partition  Typical data structures: AABB, OBB… Two Ways to Build HDS Game Programming Practice

 Partition the space occupied by objects  Uniform partition  Adaptive partition  One object might belong to multiple nodes Spatial Partition Game Programming Practice

 Partition according to objects themselves  Space occupied by objects might overlap Object-based Partition Game Programming Practice

 Each node has 4 children nodes at most  Features  Uniform partition  Not balanced Quadtree Game Programming Practice

 Terrain rendering is very important in many games  Size of terrain data is always huge  Inefficient to render all terrain data  How to cull invisible parts? Example: Terrain Rendering Game Programming Practice

 Suppose terrain is divided to 16x16 blocks  Solution 1: check every block one by one  All blocks have to be checked  Very inefficient Culling Game Programming Practice

 Solution 2: use quadtree to organize data  Uniformly divide terrain to four sub-blocks  Root node with four children nodes Building Quadtree Building Quadtree 1/ Game Programming Practice

 Keep dividing each children node  Until each child node only contains one block Building Quadtree Building Quadtree 2/2 Game Programming Practice

 Node 2,3,4 are invisible  Not necessary to check offspring nodes of 2,3,4  Node 5 is visible  Keep testing children node of 5 Culling With Quadtree Culling With Quadtree 1/ Game Programming Practice

 Node a, b, c are invisible  Not necessary to check offspring nodes of a,b,c  Node d is visible  Keep testing children node of d Culling With Quadtree Culling With Quadtree 2/3 5 d cb a Game Programming Practice

 Continue check node d until leaf nodes are reached  Performance comparison  Solution 1: 16*16 = 256 visibility tests  Solution 2: = 29 visibility tests Culling With Quadtree Culling With Quadtree 3/3 Game Programming Practice

 Adaptively build quadtree  Connect centers of empty block to form a graph  Route planning based on graph Route Planning With Quadtree Game Programming Practice

 Quadtree is defined in 2D space  Extend quadtree to 3D space to get octree  Each node has 8 children nodes at most  Uniform subdivision Octree Game Programming Practice

 Produce very unbalanced tree from unevenly distributed objects  Consequence of uniform subdivision  High memory cost  Lots of wasted memory because empty space Disadvantages Game Programming Practice

 Stands for K-Dimensional Tree  Organize data stored in k-dimensional space  Use a clever way to split space  Quadtree/Octree: fixed split strategy (uniform subdivision)  KD-tree: determine split according to data distribution  Widely adopted in ray tracing, collision detection KD-tree Game Programming Practice

 Binary tree  Every non-leaf node split its space to two sub space by a split plane  Split plane is perpendicular to some axis KD-tree X Y Game Programming Practice

 Determine a split axis and split position p  Split space occupied by current node into two sub- space  Split plane is a hyper plane in K-dimensional space which is perpendicular to the split axis  Suppose X axis is chosen as split axis  Object belongs to left child if its x coordinate is smaller than p x  Object belongs to right child if its x coordinate is larger than p x Building KD-tree Game Programming Practice

Building KD-tree: Example Game Programming Practice

Building KD-tree: Example Game Programming Practice

 How to determine split axis?  Use every axis in turn  Choose the longest axis  Not necessary to use the same axis for brother nodes  How to determine split position?  Center of split axis  Center of data inside current node  Make the number of data in two children node equal  When to stop splitting? Building KD-tree Game Programming Practice

 KNN: K-Nearest Neighbor  Given data set P{p 1,p 2,…} in N-dimensional space, find the k-closest points in P to point q  Widely used in many applications (like fluid simulation)  Consider a simplified problem: K=1, N=2  Two solutions  Quadtree  KD-tree KNN Query Game Programming Practice

Solution 1: Quadtree R = ∞; stack.push(RootNode); While (!stack.empty()) { T = stack.pop(); for each children nodes C of T { if (C is leaf node) { d = minimum distance from q to points in C; if (d < R) R = d; } else { if (C has intersection with circle centered at q with radius R) stack.push(C); } Game Programming Practice

Solution 2: KD-tree Solution 2: KD-tree 1/4  Start from root node  Start from root node A  Depth-first traversal  Initial estimation of the nearest distance D min = dis(q, )  Initial estimation of the nearest distance D min = dis(q, A ) Game Programming Practice

Solution 2: KD-tree Solution 2: KD-tree 2/4  Check left child node  Check left child node B  d = dis(q, B)  D min is updated to d because d < D min  Check children nodes of (left to right)  Check children nodes of B (left to right) Game Programming Practice

Solution 2: KD-tree Solution 2: KD-tree 3/4  Check left child node  Check left child node D  d = dis(q, D)  D min is not updated because d > D min  D is a leaf node, then E is checked Game Programming Practice

Solution 2: KD-tree Solution 2: KD-tree 4/4  Check for left sub-tree of is finished  Check for left sub-tree of A is finished  The region occupied by right sub-tree of A has no intersection with the best-estimate sphere  The whole right sub-tree can be safely ignored Game Programming Practice

BSP Tree BSP Tree 1/2  BSP = Binary Space Partition  Binary tree  Similar to KD-tree, using a hyper-plane to split current space into two sub-space  Different from KD-tree, hyper-plane is not required to perpendicular to axis Game Programming Practice

BSP Tree BSP Tree 2/2  Selection of hyper-plane is very important  Build a good BSP tree is not easy  Choosing best hyper-plane from multiple candidates  Not suitable for dynamic scene Game Programming Practice

Summary: Space Partition  Organize data by partitioning space  The whole sub tree is skipped if its root fails test  Different partition methods produce different data structures  Fixed partition way: quadtree, octree  Perpendicular to axis: KD-tree  Arbitrary way: BSP-tree  One object may belong to different nodes Game Programming Practice

Object-based Partition  Core idea: bounding volume  Typical application: collision detection  Input: objects A, B representing by two triangle sets respectively S={S 1,S 2,…,S n } and T= { T 1,T 2,…,T m }  Question: How to efficiently determine whether A collides with B?  Simple solution: check all {S i, T j } pairs  Better solution: use bounding volume technique to accelerate Game Programming Practice

How to Choose BV  Conflict targets  Bound objects as tight as possible  Low update cost when objects are moved or deformed  Fast intersection test Game Programming Practice

Bounding Volume Tree  Binary tree  How to create?  Root node is the bounding volume for whole object  Recursively split object in current node to two sub-objects  Create two children bounding volume for the two sub-objects Game Programming Practice

Bounding Sphere  The sphere enclosing object with minimum radius  Features  Good: quick intersection test  Bad: Not tight enough  Representation: center point c and radius r Game Programming Practice

AABB  AABB = Axis Aligned Bounding Box  Features  Good: quick intersection test  Bad: cannot rotate with objects, not tight enough…  Representation: min and max values along each axis Game Programming Practice

OBB  OBB = Oriented Bounding Box  Features  Arbitrary orientation  No recomputation when object is rotated  More expensive to construct Game Programming Practice

OBB Construction  Top-down construction  Root node: OBB for whole object  Split plane:  Perpendicular to the longest axis of current OBB  Pass the center of OBB Game Programming Practice

How to Determine OBB Axis  Want OBB to be aligned with the best fit plane of model  Achieve this by examining the principle components of statistical distribution of geometry  PCA: Principle Component Analysis  A very important feature extraction method in patter recognition field Game Programming Practice

Feature Extraction  Object/Data is represented by features  Number of features is too big to analysis  Can we reduce the number of features so that the smaller number still represents the data accurately?  Definition of accurate representation: the large feature vector can be reconstructed from small one with a small error in the least square sense Game Programming Practice

Principle Component Analysis  D-dimensional feature vectors  D-dimensional feature vectors x  Transform to whereis the mean of all  Transform x to y= A(x -μ) where μ is the mean of all x is a orthogonal matrix, with so is an -dimensional vector  A is a M x D orthogonal matrix, with M < D so y is an M-dimensional vector  Reconstruct by  Reconstruct x by x appr = μ+ A t y  For which isminimal?  For which A is E[||x-x appr || 2 ] minimal? Game Programming Practice

Principle Component Analysis  If we want to represent this 2-D data by 0-D data (a point), which point is the best? Game Programming Practice

Principle Component Analysis  So the 0-D best representation of a data set, in terms of least square error, is the mean Game Programming Practice

Principle Component Analysis  If we want to represent this 2-D data by 1-D data (a line), which line is the best?  It must be a line going through the mean Game Programming Practice

Computing OBB Axis  Input: 3D point set P{p 1,p 2,…,p n }  Compute the mean of point set  Compute covariance matrix C Game Programming Practice

Computing OBB Axis  Compute eigenvalues ofC  Compute eigenvalues λ of C | λE - C | =0  Compute eigenvectors X (λE – C)X = 0  Three eigenvectors are OBB’s axes Game Programming Practice

Issues

Exercises  Implement a binary tree CBinaryTree with the following functions:  Add child to the specified node;  Get child of the specified node;  Implement a KD-tree, which is derived from CBinaryTree  Implement a AABB tree, which is derived from CBinaryTree