Debian Codegraph @

DetourTileCache/Include/DetourTileCache.h

raw symbols SCIP
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
9 /// Flags for addTile
10 enum dtCompressedTileFlags
11 {
12 DT_COMPRESSEDTILE_FREE_DATA = 0x01 ///< Navmesh owns the tile memory and should free it.
13 };
14
15 struct dtCompressedTile
16 {
17 unsigned int salt; ///< Counter describing modifications to the tile.
18 struct dtTileCacheLayerHeader* header;
19 unsigned char* compressed;
20 int compressedSize;
21 unsigned char* data;
22 int dataSize;
23 unsigned int flags;
24 dtCompressedTile* next;
25 };
26
27 enum ObstacleState
28 {
29 DT_OBSTACLE_EMPTY,
30 DT_OBSTACLE_PROCESSING,
31 DT_OBSTACLE_PROCESSED,
32 DT_OBSTACLE_REMOVING
33 };
34
35 enum ObstacleType
36 {
37 DT_OBSTACLE_CYLINDER,
38 DT_OBSTACLE_BOX, // AABB
39 DT_OBSTACLE_ORIENTED_BOX // OBB
40 };
41
42 struct dtObstacleCylinder
43 {
44 float pos[ 3 ];
45 float radius;
46 float height;
47 };
48
49 struct dtObstacleBox
50 {
51 float bmin[ 3 ];
52 float bmax[ 3 ];
53 };
54
55 struct dtObstacleOrientedBox
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;
63 struct dtTileCacheObstacle
64 {
65 union
66 {
67 dtObstacleCylinder cylinder;
68 dtObstacleBox box;
69 dtObstacleOrientedBox orientedBox;
70 };
71
72 dtCompressedTileRef touched[DT_MAX_TOUCHED_TILES];
73 dtCompressedTileRef pending[DT_MAX_TOUCHED_TILES];
74 unsigned short salt;
75 unsigned char type;
76 unsigned char state;
77 unsigned char ntouched;
78 unsigned char npending;
79 dtTileCacheObstacle* next;
80 };
81
82 struct dtTileCacheParams
83 {
84 float orig[3];
85 float cs, ch;
86 int width, height;
87 float walkableHeight;
88 float walkableRadius;
89 float walkableClimb;
90 float maxSimplificationError;
91 int maxTiles;
92 int maxObstacles;
93 };
94
95 struct dtTileCacheMeshProcess
96 {
97 virtual ~dtTileCacheMeshProcess();
98 virtual void process(struct dtNavMeshCreateParams* params, unsigned char* polyAreas, unsigned short* polyFlags) = 0;
99 };
100
101 class dtTileCache
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
117 const dtTileCacheObstacle* getObstacleByRef(dtObstacleRef ref);
118
119 dtObstacleRef getObstacleRef(const dtTileCacheObstacle* obmin) const;
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);
129 dtCompressedTileRef getTileRef(const dtCompressedTile* tile) const;
130 const dtCompressedTile* getTileByRef(dtCompressedTileRef ref) const;
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
145 dtStatus removeObstacle(const dtObstacleRef ref);
146
147 dtStatus queryTiles(const float* bmin, const float* bmax,
148 dtCompressedTileRef* results, int* resultCount, const int maxResults) const;
149
150 /// Updates the tile cache by rebuilding tiles touched by unfinished obstacle requests.
151 /// @param[in] dt The time step size. Currently not used.
152 /// @param[in] navmesh The mesh to affect when rebuilding tiles.
153 /// @param[out] upToDate Whether the tile cache is fully up to date with obstacle requests and tile rebuilds.
154 /// If the tile cache is up to date another (immediate) call to update will have no effect;
155 /// otherwise another call will continue processing obstacle requests and tile rebuilds.
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
167 /// Encodes a tile id.
168 inline dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
169 {
170 return ((dtCompressedTileRef)salt << m_tileBits) | (dtCompressedTileRef)it;
171 }
172
173 /// Decodes a tile salt.
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
180 /// Decodes a tile id.
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
187 /// Encodes an obstacle id.
188 inline dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
189 {
190 return ((dtObstacleRef)salt << 16) | (dtObstacleRef)it;
191 }
192
193 /// Decodes an obstacle salt.
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
200 /// Decodes an obstacle id.
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; ///< Tile hash lookup size (must be pot).
226 int m_tileLutMask; ///< Tile hash lookup mask.
227
228 dtCompressedTile** m_posLookup; ///< Tile hash lookup.
229 dtCompressedTile* m_nextFreeTile; ///< Freelist of tiles.
230 dtCompressedTile* m_tiles; ///< List of tiles.
231
232 unsigned int m_saltBits; ///< Number of salt bits in the tile ID.
233 unsigned int m_tileBits; ///< Number of tile bits in the tile ID.
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
253 dtTileCache* dtAllocTileCache();
254 void dtFreeTileCache(dtTileCache* tc);
255
256 #endif