A* Pathfinding Project  3.8.5
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
TileHandler Class Reference

Utility class for updating tiles of recast graphs. More...

Detailed Description

Utility class for updating tiles of recast graphs.

Primarily used by the TileHandlerHelper for navmesh cutting.

Most operations that this class does are asynchronous. They will be added as work items to the AstarPath class and executed when the pathfinding threads have finished calculating their current paths.

See Also
TileHandlerHelper

Classes

class  Cut
 Internal class describing a single NavmeshCut. More...
 
class  TileType
 A template for a single tile in a recast graph. More...
 

Public Types

enum  CutMode { CutAll = 1, CutDual = 2, CutExtra = 4 }
 

Public Member Functions

 TileHandler (RecastGraph graph)
 
void ClearTile (int x, int z)
 Clear the tile at the specified tile coordinates.
 
void CreateTileTypesFromGraph ()
 
void CutShapeWithTile (int x, int z, Int3[] shape, ref Int3[] verts, ref int[] tris, out int vCount, out int tCount)
 
void EndBatchLoad ()
 
int GetActiveRotation (Int2 p)
 
TileType GetTileType (int index)
 
int GetTileTypeCount ()
 
void LoadTile (TileType tile, int x, int z, int rotation, int yoffset)
 Load a tile at tile coordinate x, z.
 
void OnRecalculatedTiles (RecastGraph.NavmeshTile[] recalculatedTiles)
 Call to update the specified tiles with new information based on the recast graph.
 
TileType RegisterTileType (Mesh source, Int3 centerOffset, int width=1, int depth=1)
 Register that a tile can be loaded from source.
 
void ReloadInBounds (Bounds b)
 Reloads all tiles intersecting with the specified bounds.
 
void ReloadTile (int x, int z)
 Reload tile at tile coordinate.
 
bool StartBatchLoad ()
 Start batch loading.
 

Static Protected Member Functions

static T[] ShrinkArray< T > (T[] arr, int newLength)
 Returns a new array with at most length newLength.
 

Properties

RecastGraph graph [get]
 The underlaying graph which this object handles.
 
bool isValid [get]
 True if the tile handler still has the same number of tiles and tile layout as the graph.
 

Private Member Functions

int ClipAgainstRectangle (Int3[] clipIn, Int3[] clipOut, Int2 size)
 Clips the input polygon against a rectangle with one corner at the origin and one at size in XZ space.
 
void CompressMesh (List< Int3 > vertices, List< int > triangles, ref Int3[] outVertices, ref int[] outTriangles, out int outVertexCount, out int outTriangleCount)
 Compress the mesh by removing duplicate vertices.
 
void CopyMesh (Int3[] vertices, int[] triangles, List< Int3 > outVertices, List< int > outTriangles)
 Copy mesh from (vertices, triangles) to (outVertices, outTriangles)
 
void CutAll (List< IntPoint > poly, List< int > intersectingCutIndices, List< Cut > cuts, Pathfinding.ClipperLib.PolyTree result)
 
void CutDual (List< IntPoint > poly, List< int > tmpIntersectingCuts, List< Cut > cuts, bool hasDual, List< List< IntPoint > > intermediateResult, Pathfinding.ClipperLib.PolyTree result)
 
void CutExtra (List< IntPoint > poly, List< IntPoint > extraClipShape, Pathfinding.ClipperLib.PolyTree result)
 
void CutPoly (Int3[] verts, int[] tris, ref Int3[] outVertsArr, ref int[] outTrisArr, out int outVCount, out int outTCount, Int3[] extraShape, Int3 cuttingOffset, Bounds realBounds, CutMode mode=CutMode.CutAll|CutMode.CutDual, int perturbate=-1)
 Cuts a piece of navmesh using navmesh cuts.
 
void DelaunayRefinement (Int3[] verts, int[] tris, ref int vCount, ref int tCount, bool delaunay, bool colinear, Int3 worldOffset)
 Refine a mesh using delaunay refinement.
 
Int3 IntPoint2Int3 (IntPoint p)
 Converts between point types.
 
Vector3 Point2D2V3 (Poly2Tri.TriangulationPoint p)
 Converts between point types.
 
void UpdateTileType (RecastGraph.NavmeshTile tile)
 

Static Private Member Functions

static void PoolPolygon (Poly2Tri.Polygon polygon, Stack< Poly2Tri.Polygon > pool)
 
static List< CutPrepareNavmeshCutsForCutting (List< NavmeshCut > navmeshCuts, Int3 cuttingOffset, IntRect bounds, int perturbate, bool anyNavmeshAdds)
 Generates a list of cuts from the navmesh cut components.
 
static int SampleYCoordinateInTriangle (Int3 p1, Int3 p2, Int3 p3, Int3 p)
 Sample Y coordinate of the triangle (p1, p2, p3) at the point p in XZ space.
 

Private Attributes

readonly RecastGraph _graph
 Graph which is handled by this instance.
 
readonly int[] activeTileOffsets
 Offsets along the Y axis of the active tiles.
 
readonly int[] activeTileRotations
 Rotations of the active tiles.
 
readonly TileType[] activeTileTypes
 Which tile type is active on each tile index.
 
readonly Dictionary< Int2, int > cached_Int2_int_dict = new Dictionary<Int2, int>()
 Cached dictionary to avoid excessive allocations.
 
readonly Dictionary< Int3, int > cached_Int3_int_dict = new Dictionary<Int3, int>()
 Cached dictionary to avoid excessive allocations.
 
int[] cached_int_array = new int[32]
 Cached int array to avoid excessive allocations.
 
readonly Clipper clipper = new Clipper()
 Handles polygon clipping operations.
 
bool isBatching
 True while batching tile updates.
 
readonly bool[] reloadedInBatch
 A flag for each tile that is set to true if it has been reloaded while batching is in progress.
 
readonly
Pathfinding.Voxels.VoxelPolygonClipper 
simpleClipper
 Utility for clipping polygons to rectangles.
 
readonly int tileXCount
 Number of tiles along the x axis.
 
readonly int tileZCount
 Number of tiles along the z axis.
 

Member Enumeration Documentation

enum CutMode
Enumerator:
CutAll 
CutDual 
CutExtra 

Constructor & Destructor Documentation

Member Function Documentation

void ClearTile ( int  x,
int  z 
)

Clear the tile at the specified tile coordinates.

int ClipAgainstRectangle ( Int3[]  clipIn,
Int3[]  clipOut,
Int2  size 
)
private

Clips the input polygon against a rectangle with one corner at the origin and one at size in XZ space.

Parameters
clipInInput vertices
clipOutOutput vertices. This buffer must be large enough to contain all output vertices.
sizeThe clipping rectangle has one corner at the origin and one at this position in XZ space.
Returns
Number of output vertices
void CompressMesh ( List< Int3 vertices,
List< int >  triangles,
ref Int3[]  outVertices,
ref int[]  outTriangles,
out int  outVertexCount,
out int  outTriangleCount 
)
private

Compress the mesh by removing duplicate vertices.

Parameters
verticesVertices of the input mesh
trianglesTriangles of the input mesh
outVerticesVertices of the output mesh. If non-null, this buffer will be reused instead of allocating a new one if it is large enough. The indices up to (but not including) outVertexCount are the final vertices.
outTrianglesTriangles of the output mesh. If non-null, this buffer will be reused instead of allocating a new one if it is large enough. The indices up to (but not including) outVertexCount are the final triangles.
outVertexCountThe number of vertices in the output mesh.
outTriangleCountThe number of used indices in the outTriangles array. This will be the number of triangles multiplied by 3.
See Also
CutPoly
void CopyMesh ( Int3[]  vertices,
int[]  triangles,
List< Int3 outVertices,
List< int >  outTriangles 
)
private

Copy mesh from (vertices, triangles) to (outVertices, outTriangles)

void CreateTileTypesFromGraph ( )
void CutAll ( List< IntPoint >  poly,
List< int >  intersectingCutIndices,
List< Cut cuts,
Pathfinding.ClipperLib.PolyTree  result 
)
private
void CutDual ( List< IntPoint >  poly,
List< int >  tmpIntersectingCuts,
List< Cut cuts,
bool  hasDual,
List< List< IntPoint > >  intermediateResult,
Pathfinding.ClipperLib.PolyTree  result 
)
private
void CutExtra ( List< IntPoint >  poly,
List< IntPoint >  extraClipShape,
Pathfinding.ClipperLib.PolyTree  result 
)
private
void CutPoly ( Int3[]  verts,
int[]  tris,
ref Int3[]  outVertsArr,
ref int[]  outTrisArr,
out int  outVCount,
out int  outTCount,
Int3[]  extraShape,
Int3  cuttingOffset,
Bounds  realBounds,
CutMode  mode = CutMode.CutAll | CutMode.CutDual,
int  perturbate = -1 
)
private

Cuts a piece of navmesh using navmesh cuts.

Note
I am sorry for the really messy code in this method. It really needs to be refactored.
void CutShapeWithTile ( int  x,
int  z,
Int3[]  shape,
ref Int3[]  verts,
ref int[]  tris,
out int  vCount,
out int  tCount 
)
void DelaunayRefinement ( Int3[]  verts,
int[]  tris,
ref int  vCount,
ref int  tCount,
bool  delaunay,
bool  colinear,
Int3  worldOffset 
)
private

Refine a mesh using delaunay refinement.

Loops through all pairs of neighbouring triangles and check if it would be better to flip the diagonal joining them using the delaunay criteria.

Does not require triangles to be clockwise, triangles will be checked for if they are clockwise and made clockwise if not. The resulting mesh will have all triangles clockwise.

See Also
https://en.wikipedia.org/wiki/Delaunay_triangulation
void EndBatchLoad ( )
int GetActiveRotation ( Int2  p)
TileType GetTileType ( int  index)
int GetTileTypeCount ( )
Int3 IntPoint2Int3 ( IntPoint  p)
private

Converts between point types.

Internal method used for debugging

void LoadTile ( TileType  tile,
int  x,
int  z,
int  rotation,
int  yoffset 
)

Load a tile at tile coordinate x, z.

Parameters
tileTile type to load
xTile x coordinate (first tile is at (0,0), second at (1,0) etc.. ).
zTile z coordinate.
rotationRotate tile by 90 degrees * value.
yoffsetOffset Y coordinates by this amount. In Int3 space, so if you have a world space offset, multiply by Int3.Precision and round to the nearest integer before calling this function.
void OnRecalculatedTiles ( RecastGraph.NavmeshTile[]  recalculatedTiles)

Call to update the specified tiles with new information based on the recast graph.

This is usually called right after a recast graph has recalculated some tiles and thus some calculations need to be done to take navmesh cutting into account as well.

Will reload all tiles in the list.

Vector3 Point2D2V3 ( Poly2Tri.TriangulationPoint  p)
private

Converts between point types.

Internal method used for debugging

static void PoolPolygon ( Poly2Tri.Polygon  polygon,
Stack< Poly2Tri.Polygon >  pool 
)
staticprivate
static List<Cut> PrepareNavmeshCutsForCutting ( List< NavmeshCut navmeshCuts,
Int3  cuttingOffset,
IntRect  bounds,
int  perturbate,
bool  anyNavmeshAdds 
)
staticprivate

Generates a list of cuts from the navmesh cut components.

Each cut has a single contour (NavmeshCut components may contain multiple).

TileType RegisterTileType ( Mesh  source,
Int3  centerOffset,
int  width = 1,
int  depth = 1 
)

Register that a tile can be loaded from source.

Parameters
centerOffsetAssumes that the mesh has its pivot point at the center of the tile. If it has not, you can supply a non-zero centerOffset to offset all vertices.
widthwidth of the tile. In base tiles, not world units.
depthdepth of the tile. In base tiles, not world units.
sourceSource mesh, must be readable.
Returns
Identifier for loading that tile type
void ReloadInBounds ( Bounds  b)

Reloads all tiles intersecting with the specified bounds.

void ReloadTile ( int  x,
int  z 
)

Reload tile at tile coordinate.

The last tile loaded at that position will be reloaded (e.g to account for moved NavmeshCut components)

static int SampleYCoordinateInTriangle ( Int3  p1,
Int3  p2,
Int3  p3,
Int3  p 
)
staticprivate

Sample Y coordinate of the triangle (p1, p2, p3) at the point p in XZ space.

The y coordinate of #p is ignored.

Returns
The interpolated y coordinate unless the triangle is degenerate in which case a DivisionByZeroException will be thrown
See Also
https://en.wikipedia.org/wiki/Barycentric_coordinate_system
static T [] ShrinkArray< T > ( T[]  arr,
int  newLength 
)
staticprotected

Returns a new array with at most length newLength.

The array will contain a copy of all elements of arr up to but excluding the index newLength.

bool StartBatchLoad ( )

Start batch loading.

Returns
True if batching wasn't started yet, and thus EndBatchLoad should be called, False if batching was already started by some other part of the code and you should not call EndBatchLoad
void UpdateTileType ( RecastGraph.NavmeshTile  tile)
private

Member Data Documentation

readonly RecastGraph _graph
private

Graph which is handled by this instance.

readonly int [] activeTileOffsets
private

Offsets along the Y axis of the active tiles.

readonly int [] activeTileRotations
private

Rotations of the active tiles.

readonly TileType [] activeTileTypes
private

Which tile type is active on each tile index.

This array will be tileXCount*tileZCount elements long.

readonly Dictionary<Int2, int> cached_Int2_int_dict = new Dictionary<Int2, int>()
private

Cached dictionary to avoid excessive allocations.

readonly Dictionary<Int3, int> cached_Int3_int_dict = new Dictionary<Int3, int>()
private

Cached dictionary to avoid excessive allocations.

int [] cached_int_array = new int[32]
private

Cached int array to avoid excessive allocations.

readonly Clipper clipper = new Clipper()
private

Handles polygon clipping operations.

bool isBatching
private

True while batching tile updates.

Batching tile updates has a positive effect on performance

readonly bool [] reloadedInBatch
private

A flag for each tile that is set to true if it has been reloaded while batching is in progress.

readonly Pathfinding.Voxels.VoxelPolygonClipper simpleClipper
private

Utility for clipping polygons to rectangles.

Implemented as a struct and not a bunch of static methods because it needs some buffer arrays that are best cached to avoid excessive allocations

readonly int tileXCount
private

Number of tiles along the x axis.

readonly int tileZCount
private

Number of tiles along the z axis.

Property Documentation

RecastGraph graph
get

The underlaying graph which this object handles.

bool isValid
get

True if the tile handler still has the same number of tiles and tile layout as the graph.

If the graph is rescanned the tile handler will get out of sync and needs to be recreated.


The documentation for this class was generated from the following file: