Navigation
API > API/Runtime > API/Runtime/AIModule
References
| Module | AIModule |
| Header | /Engine/Source/Runtime/AIModule/Public/HierarchicalHashGrid2D.h |
| Include | #include "HierarchicalHashGrid2D.h" |
Syntax
template<int32 InNumLevels, int32 InLevelRatio, typename InItemIDType>
class THierarchicalHashGrid2D
Remarks
Hierarchical Hash Grid in 2D.
Items are added to the "infinite" grid at specific level based on their size. The grid size, and size ratio between levels are defined as the template parameter. This allows some computation to be optimized based on the values.
Items that cannot be fitted in the grid are added to a spill list, which contents are included in all queries. Each item is added to just one grid cell and thus can overlap some neighbour cells up to half cell size at that grid level. This is compensated during the query by expanding the query box.
Potential optimizations:
- (X, Y, Level) could probably be int16.
- Level ratio could be a shift so LevelDown() could become just shift.
- FloorToInt ends up calling floorf, which will be a function call. int FloorToInt(float a) { return (int)a + ((int)a > a ? -1 : 0); } auto vectorizes nicely on clang, but not on VC.
- Add helper function to allow to tweak the cell size to reset the grid when spill list gets too largeLWC_TODO_AI Note we are using int32 here for X and Y which does mean that we could overflow the limit of an int for LWCoords unless fairly large grid cell sizes are used.As WORLD_MAX is currently in flux until we have a better idea of what we are going to be able to support its probably not worth investing time in this potential issue right now.
Variables
| Type | Name | Description | |
|---|---|---|---|
| TSet< FCell > | Cells | Number items per level, can be used to skip certain levels. | |
| TStaticArray< float, NumLevels > | CellSize | ||
| TStaticArray< float, NumLevels > | InvCellSize | Lowest level cell size | |
| TSparseArray< FItem > | Items | TSet uses hash buckets to locate items. | |
| TStaticArray< int32, NumLevels > | LevelItemCount | 1 / CellSize | |
| int32 | SpillList | TSparseArray uses freelist to recycled used items. |
Constructors
| Type | Name | Description | |
|---|---|---|---|
THierarchicalHashGrid2D
(
const float InCellSize |
Constructor, initializes the grid for specific cell size. |
Functions
| Type | Name | Description | |
|---|---|---|---|
| void | Add
(
const ItemIDType ID, |
Adds item to the grid. | |
| FCellLocation | Add
(
const ItemIDType ID, |
Adds item to the grid. | |
| FBox | CalcCellBounds
(
const FCellLocation& CellLocation |
Finds the world box from a cell. CellLocation - Cell location. | |
| FCellLocation | CalcCellLocation
(
const FBox& Bounds |
Finds grid level where the bounds fit inside a cell. | |
| FCellRect | CalcQueryBounds
(
const FBox& Bounds, |
Calculates cell based query rectangle. | |
| const FCell * | FindCell
(
const int X, |
Returns a cell for specific location and level. | |
| FCell * | FindCellMutable
(
const int X, |
Returns a cell for specific location and level. | |
| FCell & | FindOrAddCell
(
const int X, |
Returns a cell for specific location and level, creates new cell if it does not exist. | |
| const TSet< FCell > & | GetCells () |
||
| float | GetCellSize
(
const int32 Level |
||
| int32 | |||
| float | GetInvCellSize
(
const int32 Level |
||
| const TSparseArray< FItem > & | GetItems () |
||
| int32 | GetLevelItemCount
(
const int32 Level |
||
| void | Initialize
(
const float InCellSize |
Resets and initializes the grid for specific cell size. | |
| FCellRect | IntersectRect
(
const FCellRect& Left, |
Returns intersection of the two cell bounding rectangles. | |
| int32 | LevelDown
(
int32 X |
Levels down a coordinate using floor rounding. | |
| FCellLocation | Move
(
const ItemIDType ID, |
Moves item based on previous bounding box and new bounding box. | |
| FCellLocation | Move
(
const ItemIDType ID, |
Moves item based on previous bounding box and new bounding box. | |
| void | Query
(
const FBox& Bounds, |
Returns items that potentially touch the bounds. Operates on grid level, can have false positives. | |
| void | QuerySmall
(
const FBox& Bounds, |
Returns items that potentially touch the bounds. | |
| void | Remove
(
const ItemIDType ID, |
Removes item based on the bounding box it was added with. | |
| void | Remove
(
const ItemIDType ID, |
Removes item based on the cell location it was added with. | |
| void | Reset () |
Reset the grid to empty. |
Classes
| Type | Name | Description | |
|---|---|---|---|
| FCell | Ratio in cells between consecutive levels. | ||
| FCellLocation | Specifies cell location within the grid at specific level. | ||
| FCellRect | Rectangle bounds, coordinates inclusive. | ||
| FCellRectIterator | Iterator state over a rectangle | ||
| FItem | Item stored in a grid cell. |
Typedefs
| Name | Description |
|---|---|
| ItemIDType |
Constants
| Name | Description |
|---|---|
| LevelRatio | Number of grid levels in the grid. |
| NumLevels |