spatial data structure

The spatial data structure is used in the project to solve some problems, including scene management in the engine! I made a ppt to give a speech, so some people in the underground directly use the contents of ppt or screenshots ~
use quadtree, Kd-tree, etc. in 2D, and use octree, bvh-tree, BSP-tree
BVH- in 3D tree is used in dynamic scenes (the most used in games); Oc-tree octree is for large outdoor scenes; BSP-tree is for indoor scenes; purposes: scene management, collision detection, LOD, ray tracing,
visual Vertebral face culling, geometry acceleration;

KD-tree (take light tracing as an example)

It divides the space into two parts each time, and divides along the x-axis, y-axis, and z-axis in turn, that is, as shown in the figure, the first time divides the 2-dimensional space horizontally into upper and lower parts, and the second time again Vertically divide the upper and lower subspaces into left and right parts respectively, and divide them recursively in turn. The termination condition is similar to that of the octree, but based on the space division;
insert image description here

Cycle through the following steps:
1. Divide along the x-axis, y-axis, and z-axis in turn to make the space more balanced. 2. The
position of the division is determined by the distribution of the triangles in the space, and the details are not expanded.
3. The leaf nodes store the correspondence For all objects or triangular surface information in the space, the intermediate nodes only store pointers pointing to two subspaces
4 When the divided space is too small or there are only a few triangles in the subspace, the division will stop.
Difference: Divide the space directly, it is a dichotomous space! For example, first divide the current scene left and right, and then divide the sub-scene up and down

Advantages:
The advantage of using the KD-Tree structure to construct an AABB is that if the light does not intersect with which part of the space, then the judgment process of all subspaces in that part of the space can be omitted, and the acceleration is further improved on the basis of the original pure AABB efficiency.
Disadvantages:
The disadvantage is that it is difficult to judge whether the bounding box and the triangular surface intersect, so the division process is not as simple as imagined. Secondly, the same triangular surface may be occupied by different bounding boxes at the same time. The leaf nodes in these two different bounding boxes This triangular surface will be stored at the same time,
and it has been used less in the past ten years;

Bounding Volume Hierarchy Based On Tree

Space is no longer used as the basis for division, but from the perspective of objects, that is, triangular surfaces (mainly applied to dynamic scenes)

Hierarchical bounding box tree (BVH tree) is a multi-fork tree used to store bounding box shapes. Its root node represents a largest bounding box, and its multiple child nodes represent multiple sub-bounding boxes. In addition, in order to unify the shape of the hierarchical bounding box tree, it can only store the same bounding box shape. The core of the algorithm is roughly: the displacement/rotation/stretch of the shape updates the corresponding leaf nodes, and then updates the upper nodes one by one, so that their bounding volumes enclose the child nodes, and the construction time complexity is only O(NlogN).
insert image description here

Difference: Use the Bounding box (AABB) to start dividing from the object

**Advantages: **The most used in modern times
1. Generally choose the longest axis for each division. Assuming it is the x-axis, then the division point selects the median of the barycentric coordinates of all triangular faces on the x-coordinate for division. , so as to ensure that the number of triangles on the left and right sides of the divided triangle is as similar as possible, of course, it makes the tree structure more balanced, the depth is smaller, the average number of searches is less, and the efficiency is improved. These are the knowledge of the data structure. I believe that everyone has mastered it well, so I won’t repeat it.
2. Like KD-Tree, the intermediate node does not store the triangle information of the object, but only stores it in the leaf node, and the termination condition can be set as the number of triangles in the current bounding box is sufficiently small (eg 5)

Quadtree and Octree:

Two-dimensional quadtrees are used, and
insert image description here
three-dimensional ones use octrees (large outdoor scenes). In order to prevent full trees, optimization requires linear octrees (trees are essentially arrays) to recursively
insert image description here
divide cubes

  • F: The sub-cube is filled with the content of the scene and the content is marked as F (full). The sub-cube will no longer be divided;

  • E: The sub-cube has no scene content marked as E (empty) and the sub-cube is no longer divided;

  • The leaf node is F or E; (disadvantage)

Loose quadtree/octree: reduce bounds problems

To solve a problem of quadtree/octree, the object may go back and forth at the boundary (that is, in quadrant A and quadrant B), which causes the object to always switch nodes, so that the quadtree/octree has to be continuously updated. Otherwise smaller objects may be stored in an octree node with a very large bounding volume due to their special position.

In "Game Programming
Gems", this problem is described as a "sticky" area generated during the hierarchical division process. Smaller objects remain at higher levels in the tree, reducing the efficiency of division

insert image description here

The loose quadtree/octree is just one way to solve this boundary problem:

  1. First it defines a node with an inner boundary and an outer boundary.
  2. Determine which node an object is currently in
  3. If the object has not been added to the quadtree/octree, detect which node is currently within the entry boundary;
  4. If the object has existed in a certain node before, first check whether it is beyond the exit boundary of the node, and if it is beyond, then detect which node it is in the entry boundary.

In a non-loose quadtree/octree, the entry boundary is the same as the exit boundary. The looseness of the loose quadtree/octree means that the exit boundary is slightly wider than the entry boundary (the exit boundary of each node will also partially overlap, and the looseness is more in line with this description), so that the node is not easy to cross the exit Boundary, reducing the number of times objects switch nodes.
A subsequent question is how to define the length of the exit boundary. Because if it is too short, it will degenerate into a normal quadtree/octree, and if it is too long, it may cause nodes to store redundant objects. And in a paper on loose quadtrees/octrees, experiments show that egress boundaries that are twice as long as ingress boundaries perform well.

Linear octree:

insert image description here

BSP tree (Binary Space Partitioning Tree): binary space partition

It is considered an old hero in the game industry. It was first used in the commercial game "DOOM" in 1993. However, with the advancement of rendering hardware at any time, the rendering based on the BSP tree is gradually eliminated. But even today, BSP is an important data structure that is indispensable in other branches (engine editors).
(Used in indoor scenes) Space hyperplanes are used to recursively divide the space into convex polyhedrons, and binary trees are used to represent the divided space (AVL tree). The planes in the scene have front planes and back planes (the normal vector of the plane is front plane)
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/chenweiyu11962/article/details/120909765