Recast Navigation
Navigation-mesh Toolset for Games
DetourTileCache.h
Go to the documentation of this file.
1 #ifndef DETOURTILECACHE_H
2 #define DETOURTILECACHE_H
3 
4 #include "DetourStatus.h"
5 
6 typedef unsigned int dtObstacleRef;
7 typedef unsigned int dtCompressedTileRef;
8 
11 {
13 };
14 
16 {
17  unsigned int salt;
19  unsigned char* compressed;
21  unsigned char* data;
22  int dataSize;
23  unsigned int flags;
25 };
26 
28 {
33 };
34 
36 {
38  DT_OBSTACLE_BOX, // AABB
40 };
41 
43 {
44  float pos[ 3 ];
45  float radius;
46  float height;
47 };
48 
50 {
51  float bmin[ 3 ];
52  float bmax[ 3 ];
53 };
54 
56 {
57  float center[ 3 ];
58  float halfExtents[ 3 ];
59  float rotAux[ 2 ]; //{ cos(0.5f*angle)*sin(-0.5f*angle); cos(0.5f*angle)*cos(0.5f*angle) - 0.5 }
60 };
61 
62 static const int DT_MAX_TOUCHED_TILES = 8;
64 {
65  union
66  {
70  };
71 
74  unsigned short salt;
75  unsigned char type;
76  unsigned char state;
77  unsigned char ntouched;
78  unsigned char npending;
80 };
81 
83 {
84  float orig[3];
85  float cs, ch;
86  int width, height;
91  int maxTiles;
93 };
94 
96 {
97  virtual ~dtTileCacheMeshProcess();
98  virtual void process(struct dtNavMeshCreateParams* params, unsigned char* polyAreas, unsigned short* polyFlags) = 0;
99 };
100 
102 {
103 public:
104  dtTileCache();
105  ~dtTileCache();
106 
107  struct dtTileCacheAlloc* getAlloc() { return m_talloc; }
108  struct dtTileCacheCompressor* getCompressor() { return m_tcomp; }
109  const dtTileCacheParams* getParams() const { return &m_params; }
110 
111  inline int getTileCount() const { return m_params.maxTiles; }
112  inline const dtCompressedTile* getTile(const int i) const { return &m_tiles[i]; }
113 
114  inline int getObstacleCount() const { return m_params.maxObstacles; }
115  inline const dtTileCacheObstacle* getObstacle(const int i) const { return &m_obstacles[i]; }
116 
118 
120 
121  dtStatus init(const dtTileCacheParams* params,
122  struct dtTileCacheAlloc* talloc,
123  struct dtTileCacheCompressor* tcomp,
124  struct dtTileCacheMeshProcess* tmproc);
125 
126  int getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const ;
127 
128  dtCompressedTile* getTileAt(const int tx, const int ty, const int tlayer);
131 
132  dtStatus addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result);
133 
134  dtStatus removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize);
135 
136  // Cylinder obstacle.
137  dtStatus addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result);
138 
139  // Aabb obstacle.
140  dtStatus addBoxObstacle(const float* bmin, const float* bmax, dtObstacleRef* result);
141 
142  // Box obstacle: can be rotated in Y.
143  dtStatus addBoxObstacle(const float* center, const float* halfExtents, const float yRadians, dtObstacleRef* result);
144 
146 
147  dtStatus queryTiles(const float* bmin, const float* bmax,
148  dtCompressedTileRef* results, int* resultCount, const int maxResults) const;
149 
156  dtStatus update(const float dt, class dtNavMesh* navmesh, bool* upToDate = 0);
157 
158  dtStatus buildNavMeshTilesAt(const int tx, const int ty, class dtNavMesh* navmesh);
159 
160  dtStatus buildNavMeshTile(const dtCompressedTileRef ref, class dtNavMesh* navmesh);
161 
162  void calcTightTileBounds(const struct dtTileCacheLayerHeader* header, float* bmin, float* bmax) const;
163 
164  void getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const;
165 
166 
168  inline dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
169  {
170  return ((dtCompressedTileRef)salt << m_tileBits) | (dtCompressedTileRef)it;
171  }
172 
174  inline unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
175  {
176  const dtCompressedTileRef saltMask = ((dtCompressedTileRef)1<<m_saltBits)-1;
177  return (unsigned int)((ref >> m_tileBits) & saltMask);
178  }
179 
181  inline unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
182  {
183  const dtCompressedTileRef tileMask = ((dtCompressedTileRef)1<<m_tileBits)-1;
184  return (unsigned int)(ref & tileMask);
185  }
186 
188  inline dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
189  {
190  return ((dtObstacleRef)salt << 16) | (dtObstacleRef)it;
191  }
192 
194  inline unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
195  {
196  const dtObstacleRef saltMask = ((dtObstacleRef)1<<16)-1;
197  return (unsigned int)((ref >> 16) & saltMask);
198  }
199 
201  inline unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
202  {
203  const dtObstacleRef tileMask = ((dtObstacleRef)1<<16)-1;
204  return (unsigned int)(ref & tileMask);
205  }
206 
207 
208 private:
209  // Explicitly disabled copy constructor and copy assignment operator.
210  dtTileCache(const dtTileCache&);
211  dtTileCache& operator=(const dtTileCache&);
212 
213  enum ObstacleRequestAction
214  {
215  REQUEST_ADD,
216  REQUEST_REMOVE
217  };
218 
219  struct ObstacleRequest
220  {
221  int action;
222  dtObstacleRef ref;
223  };
224 
225  int m_tileLutSize;
226  int m_tileLutMask;
227 
228  dtCompressedTile** m_posLookup;
229  dtCompressedTile* m_nextFreeTile;
230  dtCompressedTile* m_tiles;
231 
232  unsigned int m_saltBits;
233  unsigned int m_tileBits;
234 
235  dtTileCacheParams m_params;
236 
237  dtTileCacheAlloc* m_talloc;
238  dtTileCacheCompressor* m_tcomp;
239  dtTileCacheMeshProcess* m_tmproc;
240 
241  dtTileCacheObstacle* m_obstacles;
242  dtTileCacheObstacle* m_nextFreeObstacle;
243 
244  static const int MAX_REQUESTS = 64;
245  ObstacleRequest m_reqs[MAX_REQUESTS];
246  int m_nreqs;
247 
248  static const int MAX_UPDATE = 64;
249  dtCompressedTileRef m_update[MAX_UPDATE];
250  int m_nupdate;
251 };
252 
254 void dtFreeTileCache(dtTileCache* tc);
255 
256 #endif
unsigned int dtStatus
Definition: DetourStatus.h:22
static const int DT_MAX_TOUCHED_TILES
Definition: DetourTileCache.h:62
ObstacleType
Definition: DetourTileCache.h:36
@ DT_OBSTACLE_CYLINDER
Definition: DetourTileCache.h:37
@ DT_OBSTACLE_ORIENTED_BOX
Definition: DetourTileCache.h:39
@ DT_OBSTACLE_BOX
Definition: DetourTileCache.h:38
unsigned int dtObstacleRef
Definition: DetourTileCache.h:6
ObstacleState
Definition: DetourTileCache.h:28
@ DT_OBSTACLE_EMPTY
Definition: DetourTileCache.h:29
@ DT_OBSTACLE_PROCESSED
Definition: DetourTileCache.h:31
@ DT_OBSTACLE_REMOVING
Definition: DetourTileCache.h:32
@ DT_OBSTACLE_PROCESSING
Definition: DetourTileCache.h:30
dtTileCache * dtAllocTileCache()
Definition: DetourTileCache.cpp:12
void dtFreeTileCache(dtTileCache *tc)
Definition: DetourTileCache.cpp:19
unsigned int dtCompressedTileRef
Definition: DetourTileCache.h:7
dtCompressedTileFlags
Flags for addTile.
Definition: DetourTileCache.h:11
@ DT_COMPRESSEDTILE_FREE_DATA
Navmesh owns the tile memory and should free it.
Definition: DetourTileCache.h:12
A navigation mesh based on tiles of convex polygons.
Definition: DetourNavMesh.h:339
Definition: DetourTileCache.h:102
dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
Encodes an obstacle id.
Definition: DetourTileCache.h:188
struct dtTileCacheCompressor * getCompressor()
Definition: DetourTileCache.h:108
unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
Decodes a tile salt.
Definition: DetourTileCache.h:174
dtCompressedTileRef getTileRef(const dtCompressedTile *tile) const
Definition: DetourTileCache.cpp:214
unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
Decodes an obstacle id.
Definition: DetourTileCache.h:201
int getTilesAt(const int tx, const int ty, dtCompressedTileRef *tiles, const int maxTiles) const
Definition: DetourTileCache.cpp:173
dtStatus addObstacle(const float *pos, const float radius, const float height, dtObstacleRef *result)
Definition: DetourTileCache.cpp:359
dtStatus buildNavMeshTile(const dtCompressedTileRef ref, class dtNavMesh *navmesh)
Definition: DetourTileCache.cpp:657
const dtTileCacheObstacle * getObstacle(const int i) const
Definition: DetourTileCache.h:115
const dtTileCacheObstacle * getObstacleByRef(dtObstacleRef ref)
Definition: DetourTileCache.cpp:228
const dtCompressedTile * getTileByRef(dtCompressedTileRef ref) const
Definition: DetourTileCache.cpp:103
void getObstacleBounds(const struct dtTileCacheObstacle *ob, float *bmin, float *bmax) const
Definition: DetourTileCache.cpp:795
dtStatus addBoxObstacle(const float *bmin, const float *bmax, dtObstacleRef *result)
Definition: DetourTileCache.cpp:394
int getObstacleCount() const
Definition: DetourTileCache.h:114
dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
Encodes a tile id.
Definition: DetourTileCache.h:168
dtStatus removeTile(dtCompressedTileRef ref, unsigned char **data, int *dataSize)
Definition: DetourTileCache.cpp:293
dtStatus update(const float dt, class dtNavMesh *navmesh, bool *upToDate=0)
Updates the tile cache by rebuilding tiles touched by unfinished obstacle requests.
Definition: DetourTileCache.cpp:523
unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
Decodes a tile id.
Definition: DetourTileCache.h:181
dtStatus init(const dtTileCacheParams *params, struct dtTileCacheAlloc *talloc, struct dtTileCacheCompressor *tcomp, struct dtTileCacheMeshProcess *tmproc)
Definition: DetourTileCache.cpp:118
dtObstacleRef getObstacleRef(const dtTileCacheObstacle *obmin) const
Definition: DetourTileCache.cpp:221
~dtTileCache()
Definition: DetourTileCache.cpp:83
const dtCompressedTile * getTile(const int i) const
Definition: DetourTileCache.h:112
void calcTightTileBounds(const struct dtTileCacheLayerHeader *header, float *bmin, float *bmax) const
Definition: DetourTileCache.cpp:784
dtStatus addTile(unsigned char *data, const int dataSize, unsigned char flags, dtCompressedTileRef *result)
Definition: DetourTileCache.cpp:247
unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
Decodes an obstacle salt.
Definition: DetourTileCache.h:194
struct dtTileCacheAlloc * getAlloc()
Definition: DetourTileCache.h:107
const dtTileCacheParams * getParams() const
Definition: DetourTileCache.h:109
dtTileCache()
Definition: DetourTileCache.cpp:63
dtCompressedTile * getTileAt(const int tx, const int ty, const int tlayer)
Definition: DetourTileCache.cpp:195
dtStatus buildNavMeshTilesAt(const int tx, const int ty, class dtNavMesh *navmesh)
Definition: DetourTileCache.cpp:641
int getTileCount() const
Definition: DetourTileCache.h:111
dtStatus queryTiles(const float *bmin, const float *bmax, dtCompressedTileRef *results, int *resultCount, const int maxResults) const
Definition: DetourTileCache.cpp:482
dtStatus removeObstacle(const dtObstacleRef ref)
Definition: DetourTileCache.cpp:467
Definition: DetourTileCache.h:16
unsigned char * data
Definition: DetourTileCache.h:21
dtCompressedTile * next
Definition: DetourTileCache.h:24
unsigned int flags
Definition: DetourTileCache.h:23
unsigned char * compressed
Definition: DetourTileCache.h:19
int compressedSize
Definition: DetourTileCache.h:20
struct dtTileCacheLayerHeader * header
Definition: DetourTileCache.h:18
int dataSize
Definition: DetourTileCache.h:22
unsigned int salt
Counter describing modifications to the tile.
Definition: DetourTileCache.h:17
Represents the source data used to build an navigation mesh tile.
Definition: DetourNavMeshBuilder.h:27
Definition: DetourTileCache.h:50
float bmax[3]
Definition: DetourTileCache.h:52
float bmin[3]
Definition: DetourTileCache.h:51
Definition: DetourTileCache.h:43
float pos[3]
Definition: DetourTileCache.h:44
float height
Definition: DetourTileCache.h:46
float radius
Definition: DetourTileCache.h:45
Definition: DetourTileCache.h:56
float rotAux[2]
Definition: DetourTileCache.h:59
float center[3]
Definition: DetourTileCache.h:57
float halfExtents[3]
Definition: DetourTileCache.h:58
Definition: DetourTileCacheBuilder.h:80
Definition: DetourTileCacheBuilder.h:97
Definition: DetourTileCacheBuilder.h:33
Definition: DetourTileCache.h:96
virtual void process(struct dtNavMeshCreateParams *params, unsigned char *polyAreas, unsigned short *polyFlags)=0
virtual ~dtTileCacheMeshProcess()
Definition: DetourTileCache.cpp:242
Definition: DetourTileCache.h:64
dtObstacleBox box
Definition: DetourTileCache.h:68
unsigned char ntouched
Definition: DetourTileCache.h:77
dtCompressedTileRef touched[DT_MAX_TOUCHED_TILES]
Definition: DetourTileCache.h:72
unsigned char state
Definition: DetourTileCache.h:76
unsigned char npending
Definition: DetourTileCache.h:78
dtObstacleCylinder cylinder
Definition: DetourTileCache.h:67
dtTileCacheObstacle * next
Definition: DetourTileCache.h:79
unsigned short salt
Definition: DetourTileCache.h:74
dtObstacleOrientedBox orientedBox
Definition: DetourTileCache.h:69
unsigned char type
Definition: DetourTileCache.h:75
dtCompressedTileRef pending[DT_MAX_TOUCHED_TILES]
Definition: DetourTileCache.h:73
Definition: DetourTileCache.h:83
int width
Definition: DetourTileCache.h:86
int height
Definition: DetourTileCache.h:86
int maxTiles
Definition: DetourTileCache.h:91
int maxObstacles
Definition: DetourTileCache.h:92
float ch
Definition: DetourTileCache.h:85
float maxSimplificationError
Definition: DetourTileCache.h:90
float walkableClimb
Definition: DetourTileCache.h:89
float cs
Definition: DetourTileCache.h:85
float walkableHeight
Definition: DetourTileCache.h:87
float walkableRadius
Definition: DetourTileCache.h:88
float orig[3]
Definition: DetourTileCache.h:84