Planeshift
DetourNavMesh.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 // 1. The origin of this software must not be misrepresented; you must not
11 // claim that you wrote the original software. If you use this software
12 // in a product, an acknowledgment in the product documentation would be
13 // appreciated but is not required.
14 // 2. Altered source versions must be plainly marked as such, and must not be
15 // misrepresented as being the original software.
16 // 3. This notice may not be removed or altered from any source distribution.
17 //
18 
19 #ifndef DETOURNAVMESH_H
20 #define DETOURNAVMESH_H
21 
22 #include "DetourAlloc.h"
23 #include "DetourStatus.h"
24 
25 // Note: If you want to use 64-bit refs, change the types of both dtPolyRef & dtTileRef.
26 // It is also recommended that you change dtHashRef() to a proper 64-bit hash.
27 
30 typedef unsigned int dtPolyRef;
31 
34 typedef unsigned int dtTileRef;
35 
38 static const int DT_VERTS_PER_POLYGON = 6;
39 
45 
47 static const int DT_NAVMESH_MAGIC = 'D'<<24 | 'N'<<16 | 'A'<<8 | 'V';
48 
50 static const int DT_NAVMESH_VERSION = 7;
51 
53 static const int DT_NAVMESH_STATE_MAGIC = 'D'<<24 | 'N'<<16 | 'M'<<8 | 'S';
54 
56 static const int DT_NAVMESH_STATE_VERSION = 1;
57 
59 
62 static const unsigned short DT_EXT_LINK = 0x8000;
63 
65 static const unsigned int DT_NULL_LINK = 0xffffffff;
66 
68 static const unsigned int DT_OFFMESH_CON_BIDIR = 1;
69 
72 static const int DT_MAX_AREAS = 64;
73 
77 {
80 };
81 
84 {
88 };
89 
92 {
97 };
98 
99 
102 struct dtPoly
103 {
105  unsigned int firstLink;
106 
109  unsigned short verts[DT_VERTS_PER_POLYGON];
110 
112  unsigned short neis[DT_VERTS_PER_POLYGON];
113 
115  unsigned short flags;
116 
118  unsigned char vertCount;
119 
122  unsigned char areaAndtype;
123 
125  inline void setArea(unsigned char a) { areaAndtype = (areaAndtype & 0xc0) | (a & 0x3f); }
126 
128  inline void setType(unsigned char t) { areaAndtype = (areaAndtype & 0x3f) | (t << 6); }
129 
131  inline unsigned char getArea() const { return areaAndtype & 0x3f; }
132 
134  inline unsigned char getType() const { return areaAndtype >> 6; }
135 };
136 
139 {
140  unsigned int vertBase;
141  unsigned int triBase;
142  unsigned char vertCount;
143  unsigned char triCount;
144 };
145 
149 struct dtLink
150 {
152  unsigned int next;
153  unsigned char edge;
154  unsigned char side;
155  unsigned char bmin;
156  unsigned char bmax;
157 };
158 
162 struct dtBVNode
163 {
164  unsigned short bmin[3];
165  unsigned short bmax[3];
166  int i;
167 };
168 
172 {
174  float pos[6];
175 
177  float rad;
178 
180  unsigned short poly;
181 
185  unsigned char flags;
186 
188  unsigned char side;
189 
191  unsigned int userId;
192 };
193 
197 {
198  int magic;
199  int version;
200  int x;
201  int y;
202  int layer;
203  unsigned int userId;
204  int polyCount;
205  int vertCount;
208 
211 
219  float bmin[3];
220  float bmax[3];
221 
224 };
225 
229 {
230  unsigned int salt;
231 
232  unsigned int linksFreeList;
235  float* verts;
238 
240  float* detailVerts;
241 
243  unsigned char* detailTris;
244 
248 
250 
251  unsigned char* data;
252  int dataSize;
253  int flags;
255 };
256 
262 {
263  float orig[3];
264  float tileWidth;
265  float tileHeight;
266  int maxTiles;
267  int maxPolys;
268 };
269 
273 {
274 public:
275  dtNavMesh();
276  ~dtNavMesh();
277 
280 
284  dtStatus init(const dtNavMeshParams* params);
285 
292  dtStatus init(unsigned char* data, const int dataSize, const int flags);
293 
295  const dtNavMeshParams* getParams() const;
296 
304  dtStatus addTile(unsigned char* data, int dataSize, int flags, dtTileRef lastRef, dtTileRef* result);
305 
311  dtStatus removeTile(dtTileRef ref, unsigned char** data, int* dataSize);
312 
314 
317 
322  void calcTileLoc(const float* pos, int* tx, int* ty) const;
323 
329  const dtMeshTile* getTileAt(const int x, const int y, const int layer) const;
330 
337  int getTilesAt(const int x, const int y,
338  dtMeshTile const** tiles, const int maxTiles) const;
339 
345  dtTileRef getTileRefAt(int x, int y, int layer) const;
346 
350  dtTileRef getTileRef(const dtMeshTile* tile) const;
351 
356  const dtMeshTile* getTileByRef(dtTileRef ref) const;
357 
360  int getMaxTiles() const;
361 
365  const dtMeshTile* getTile(int i) const;
366 
372  dtStatus getTileAndPolyByRef(const dtPolyRef ref, const dtMeshTile** tile, const dtPoly** poly) const;
373 
378  void getTileAndPolyByRefUnsafe(const dtPolyRef ref, const dtMeshTile** tile, const dtPoly** poly) const;
379 
383  bool isValidPolyRef(dtPolyRef ref) const;
384 
388  dtPolyRef getPolyRefBase(const dtMeshTile* tile) const;
389 
396  dtStatus getOffMeshConnectionPolyEndPoints(dtPolyRef prevRef, dtPolyRef polyRef, float* startPos, float* endPos) const;
397 
401  const dtOffMeshConnection* getOffMeshConnectionByRef(dtPolyRef ref) const;
402 
404 
408 
413  dtStatus setPolyFlags(dtPolyRef ref, unsigned short flags);
414 
419  dtStatus getPolyFlags(dtPolyRef ref, unsigned short* resultFlags) const;
420 
425  dtStatus setPolyArea(dtPolyRef ref, unsigned char area);
426 
431  dtStatus getPolyArea(dtPolyRef ref, unsigned char* resultArea) const;
432 
436  int getTileStateSize(const dtMeshTile* tile) const;
437 
443  dtStatus storeTileState(const dtMeshTile* tile, unsigned char* data, const int maxDataSize) const;
444 
450  dtStatus restoreTileState(dtMeshTile* tile, const unsigned char* data, const int maxDataSize);
451 
453 
457 
463  inline dtPolyRef encodePolyId(unsigned int salt, unsigned int it, unsigned int ip) const
464  {
465  return ((dtPolyRef)salt << (m_polyBits+m_tileBits)) | ((dtPolyRef)it << m_polyBits) | (dtPolyRef)ip;
466  }
467 
475  inline void decodePolyId(dtPolyRef ref, unsigned int& salt, unsigned int& it, unsigned int& ip) const
476  {
477  const dtPolyRef saltMask = ((dtPolyRef)1<<m_saltBits)-1;
478  const dtPolyRef tileMask = ((dtPolyRef)1<<m_tileBits)-1;
479  const dtPolyRef polyMask = ((dtPolyRef)1<<m_polyBits)-1;
480  salt = (unsigned int)((ref >> (m_polyBits+m_tileBits)) & saltMask);
481  it = (unsigned int)((ref >> m_polyBits) & tileMask);
482  ip = (unsigned int)(ref & polyMask);
483  }
484 
489  inline unsigned int decodePolyIdSalt(dtPolyRef ref) const
490  {
491  const dtPolyRef saltMask = ((dtPolyRef)1<<m_saltBits)-1;
492  return (unsigned int)((ref >> (m_polyBits+m_tileBits)) & saltMask);
493  }
494 
499  inline unsigned int decodePolyIdTile(dtPolyRef ref) const
500  {
501  const dtPolyRef tileMask = ((dtPolyRef)1<<m_tileBits)-1;
502  return (unsigned int)((ref >> m_polyBits) & tileMask);
503  }
504 
509  inline unsigned int decodePolyIdPoly(dtPolyRef ref) const
510  {
511  const dtPolyRef polyMask = ((dtPolyRef)1<<m_polyBits)-1;
512  return (unsigned int)(ref & polyMask);
513  }
514 
516 
518  dtMeshTile* getTile(int i);
519 
520 private:
521 
523  int getTilesAt(const int x, const int y,
524  dtMeshTile** tiles, const int maxTiles) const;
525 
527  int getNeighbourTilesAt(const int x, const int y, const int side,
528  dtMeshTile** tiles, const int maxTiles) const;
529 
531  int findConnectingPolys(const float* va, const float* vb,
532  const dtMeshTile* tile, int side,
533  dtPolyRef* con, float* conarea, int maxcon) const;
534 
536  void connectIntLinks(dtMeshTile* tile);
538  void baseOffMeshLinks(dtMeshTile* tile);
539 
541  void connectExtLinks(dtMeshTile* tile, dtMeshTile* target, int side);
543  void connectExtOffMeshLinks(dtMeshTile* tile, dtMeshTile* target, int side);
544 
546  void unconnectExtLinks(dtMeshTile* tile, dtMeshTile* target);
547 
548 
549  // TODO: These methods are duplicates from dtNavMeshQuery, but are needed for off-mesh connection finding.
550 
552  int queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax,
553  dtPolyRef* polys, const int maxPolys) const;
555  dtPolyRef findNearestPolyInTile(const dtMeshTile* tile, const float* center,
556  const float* extents, float* nearestPt) const;
558  void closestPointOnPolyInTile(const dtMeshTile* tile, unsigned int ip,
559  const float* pos, float* closest) const;
560 
561  dtNavMeshParams m_params;
562  float m_orig[3];
563  float m_tileWidth, m_tileHeight;
564  int m_maxTiles;
565  int m_tileLutSize;
566  int m_tileLutMask;
567 
568  dtMeshTile** m_posLookup;
569  dtMeshTile* m_nextFree;
570  dtMeshTile* m_tiles;
571 
572  unsigned int m_saltBits;
573  unsigned int m_tileBits;
574  unsigned int m_polyBits;
575 };
576 
581 
585 void dtFreeNavMesh(dtNavMesh* navmesh);
586 
587 #endif // DETOURNAVMESH_H
588 
590 
591 // This section contains detailed documentation for members that don't have
592 // a source file. It reduces clutter in the main section of the header.
593 
unsigned int decodePolyIdTile(dtPolyRef ref) const
Extracts the tile&#39;s index from the specified polygon reference.
The navigation mesh owns the tile memory and is responsible for freeing it.
Definition: DetourNavMesh.h:79
unsigned int dtPolyRef
A handle to a polygon within a navigation mesh tile.
Definition: DetourNavMesh.h:30
unsigned char areaAndtype
The bit packed area id and polygon type.
dtMeshHeader * header
The tile header.
unsigned short poly
The polygon reference of the connection within the tile.
dtStraightPathFlags
Vertex flags returned by dtNavMeshQuery::findStraightPath.
Definition: DetourNavMesh.h:83
unsigned int dtTileRef
A handle to a tile within a navigation mesh.
Definition: DetourNavMesh.h:34
unsigned char side
End point side.
Defines the location of detail sub-mesh data within a dtMeshTile.
float walkableHeight
The height of the agents using the tile.
float walkableClimb
The maximum climb height of the agents using the tile.
unsigned int decodePolyIdSalt(dtPolyRef ref) const
Extracts a tile&#39;s salt value from the specified polygon reference.
unsigned short flags
The user defined polygon flags.
static const int DT_NAVMESH_STATE_VERSION
A version number used to detect compatibility of navigation tile states.
Definition: DetourNavMesh.h:56
unsigned char triCount
The number of triangles in the sub-mesh.
dtOffMeshConnection * offMeshCons
The tile off-mesh connections. [Size: dtMeshHeader::offMeshConCount].
float walkableRadius
The radius of the agents using the tile.
unsigned char getArea() const
Gets the user defined area id.
float bvQuantFactor
The bounding volume quantization factor.
void decodePolyId(dtPolyRef ref, unsigned int &salt, unsigned int &it, unsigned int &ip) const
Decodes a standard polygon reference.
unsigned char flags
Link flags.
void setArea(unsigned char a)
Sets the user defined area id. [Limit: < DT_MAX_AREAS].
int offMeshConCount
The number of off-mesh connections.
dtBVNode * bvTree
The tile bounding volume nodes. Size: dtMeshHeader::bvNodeCount
static const unsigned int DT_OFFMESH_CON_BIDIR
A flag that indicates that an off-mesh connection can be traversed in both directions. (Is bidirectional.)
Definition: DetourNavMesh.h:68
int detailMeshCount
The number of sub-meshes in the detail mesh.
unsigned short verts[DT_VERTS_PER_POLYGON]
The indices of the polygon&#39;s vertices. The actual vertices are located in dtMeshTile::verts.
unsigned int dtStatus
Definition: DetourStatus.h:22
void dtFreeNavMesh(dtNavMesh *navmesh)
Frees the specified navigation mesh object using the Detour allocator.
unsigned char vertCount
The number of vertices in the sub-mesh.
dtPolyTypes
Flags representing the type of a navigation mesh polygon.
Definition: DetourNavMesh.h:91
The vertex is the start position in the path.
Definition: DetourNavMesh.h:85
unsigned int userId
The id of the offmesh connection. (User assigned when the navigation mesh is built.)
A navigation mesh based on tiles of convex polygons.
int i
The node&#39;s index. (Negative for escape sequence.)
int offMeshBase
The index of the first polygon which is an off-mesh connection.
Bounding volume node.
unsigned int vertBase
The offset of the vertices in the dtMeshTile::detailVerts array.
int layer
The layer of the tile within the dtNavMesh tile grid. (x, y, layer)
int flags
Tile flags. (See: dtTileFlags)
unsigned char getType() const
Gets the polygon type. (See: dtPolyTypes)
Provides high level information related to a dtMeshTile object.
unsigned short neis[DT_VERTS_PER_POLYGON]
Packed data representing neighbor polygons references and flags for each edge.
static const int DT_MAX_AREAS
The maximum number of user defined area ids.
Definition: DetourNavMesh.h:72
dtMeshTile * next
The next free tile, or the next tile in the spatial grid.
The vertex is the end position in the path.
Definition: DetourNavMesh.h:86
Defines a polyogn within a dtMeshTile object.
float rad
The radius of the endpoints. [Limit: >= 0].
dtTileFlags
Tile flags used for various functions and fields. For an example, see dtNavMesh::addTile().
Definition: DetourNavMesh.h:76
int polyCount
The number of polygons in the tile.
int bvNodeCount
The number of bounding volume nodes. (Zero if bounding volumes are disabled.)
int x
The x-position of the tile within the dtNavMesh tile grid. (x, y, layer)
unsigned int linksFreeList
Index to the next free link.
unsigned int salt
Counter describing modifications to the tile.
int version
Tile data format version number.
int detailVertCount
The number of unique vertices in the detail mesh. (In addition to the polygon vertices.)
int maxLinkCount
The number of allocated links.
float * verts
The tile vertices. [Size: dtMeshHeader::vertCount].
unsigned int userId
The user defined id of the tile.
dtLink * links
The tile links. [Size: dtMeshHeader::maxLinkCount].
int dataSize
Size of the tile data.
float * detailVerts
The detail mesh&#39;s unique vertices. [(x, y, z) * dtMeshHeader::detailVertCount].
unsigned char * detailTris
The detail mesh&#39;s triangles. [(vertA, vertB, vertC) * dtMeshHeader::detailTriCount].
static const unsigned short DT_EXT_LINK
A flag that indicates that an entity links to an external entity. (E.g. A polygon edge is a portal th...
Definition: DetourNavMesh.h:62
unsigned int decodePolyIdPoly(dtPolyRef ref) const
Extracts the polygon&#39;s index (within its tile) from the specified polygon reference.
dtPolyDetail * detailMeshes
The tile&#39;s detail sub-meshes. [Size: dtMeshHeader::detailMeshCount].
int maxPolys
The maximum number of polygons each tile can contain.
int magic
Tile magic number. (Used to identify the data format.)
int detailTriCount
The number of triangles in the detail mesh.
unsigned char * data
The tile data. (Not directly accessed under normal situations.)
float tileWidth
The width of each tile. (Along the x-axis.)
The polygon is an off-mesh connection consisting of two vertices.
Definition: DetourNavMesh.h:96
unsigned int firstLink
Index to first link in linked list. (Or DT_NULL_LINK if there is no link.)
static const int DT_NAVMESH_STATE_MAGIC
A magic number used to detect the compatibility of navigation tile states.
Definition: DetourNavMesh.h:53
Configuration parameters used to define multi-tile navigation meshes. The values are used to allocate...
unsigned char vertCount
The number of vertices in the polygon.
dtNavMesh * dtAllocNavMesh()
Allocates a navigation mesh object using the Detour allocator.
dtPoly * polys
The tile polygons. [Size: dtMeshHeader::polyCount].
float tileHeight
The height of each tile. (Along the z-axis.)
unsigned int triBase
The offset of the triangles in the dtMeshTile::detailTris array.
dtPolyRef encodePolyId(unsigned int salt, unsigned int it, unsigned int ip) const
Derives a standard polygon reference.
static const int DT_NAVMESH_MAGIC
A magic number used to detect compatibility of navigation tile data.
Definition: DetourNavMesh.h:47
Defines a navigation mesh tile.
int maxTiles
The maximum number of tiles the navigation mesh can contain.
int y
The y-position of the tile within the dtNavMesh tile grid. (x, y, layer)
The vertex is the start of an off-mesh connection.
Definition: DetourNavMesh.h:87
void setType(unsigned char t)
Sets the polygon type. (See: dtPolyTypes.)
static const int DT_NAVMESH_VERSION
A version number used to detect compatibility of navigation tile data.
Definition: DetourNavMesh.h:50
The polygon is a standard convex polygon that is part of the surface of the mesh. ...
Definition: DetourNavMesh.h:94
int vertCount
The number of vertices in the tile.
Defines an navigation mesh off-mesh connection within a dtMeshTile object. An off-mesh connection is ...
static const int DT_VERTS_PER_POLYGON
The maximum number of vertices per navigation polygon.
Definition: DetourNavMesh.h:38
static const unsigned int DT_NULL_LINK
A value that indicates the entity does not link to anything.
Definition: DetourNavMesh.h:65