Atoms Crowd  7.0.0
DetourTileCache.h
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 DETOURTILECACHE_H
20 #define DETOURTILECACHE_H
21 
22 #include <AtomsUtils/Globals.h>
23 #include <AtomsUtils/NavigationMesh/Detour/DetourStatus.h>
24 
25 namespace AtomsUtils {
26 
27 struct dtTileCacheAlloc;
28 struct dtTileCacheCompressor;
29 
30 typedef unsigned int dtObstacleRef;
31 
32 typedef unsigned int dtCompressedTileRef;
33 
36 {
38 };
39 
41 {
42  unsigned int salt;
43  struct dtTileCacheLayerHeader* header;
44  unsigned char* compressed;
45  int compressedSize;
46  unsigned char* data;
47  int dataSize;
48  unsigned int flags;
49  dtCompressedTile* next;
50 };
51 
52 enum ObstacleState
53 {
54  DT_OBSTACLE_EMPTY,
55  DT_OBSTACLE_PROCESSING,
56  DT_OBSTACLE_PROCESSED,
57  DT_OBSTACLE_REMOVING,
58 };
59 
60 static const int DT_MAX_TOUCHED_TILES = 8;
62 {
63  float pos[3], radius, height;
64  dtCompressedTileRef touched[DT_MAX_TOUCHED_TILES];
65  dtCompressedTileRef pending[DT_MAX_TOUCHED_TILES];
66  unsigned short salt;
67  unsigned char state;
68  unsigned char ntouched;
69  unsigned char npending;
70  dtTileCacheObstacle* next;
71 };
72 
74 {
75  float orig[3];
76  float cs, ch;
77  int width, height;
78  float walkableHeight;
79  float walkableRadius;
80  float walkableClimb;
81  float maxSimplificationError;
82  int maxTiles;
83  int maxObstacles;
84 
85  float detailSampleDist;
86  float detailSampleMaxError;
87  int minRegionArea;
88  int mergeRegionArea;
89  int regionChunkSize;
90  int regionPartitioning;
91 
92 };
93 
94 struct ATOMSUTILS_EXPORT dtTileCacheMeshProcess
95 {
96  virtual void markAreas(struct dtTileCacheLayer* layer, const float* orig, const float cs, const float ch) = 0;
97  virtual void process(struct dtNavMeshCreateParams* params,
98  unsigned char* polyAreas, unsigned short* polyFlags) = 0;
99 };
100 
101 
102 class ATOMSUTILS_EXPORT dtTileCache
103 {
104 public:
105  dtTileCache();
106  ~dtTileCache();
107 
108  struct dtTileCacheAlloc* getAlloc() { return m_talloc; }
109  struct dtTileCacheCompressor* getCompressor() { return m_tcomp; }
110  struct dtTileCacheMeshProcess* getProcessor() { return m_tmproc; }
111  const dtTileCacheParams* getParams() const { return &m_params; }
112 
113  inline int getTileCount() const { return m_params.maxTiles; }
114  inline const dtCompressedTile* getTile(const int i) const { return &m_tiles[i]; }
115 
116  inline int getObstacleCount() const { return m_params.maxObstacles; }
117  inline const dtTileCacheObstacle* getObstacle(const int i) const { return &m_obstacles[i]; }
118 
119  const dtTileCacheObstacle* getObstacleByRef(dtObstacleRef ref);
120 
121  dtObstacleRef getObstacleRef(const dtTileCacheObstacle* obmin) const;
122 
123  dtStatus init(const dtTileCacheParams* params,
124  struct dtTileCacheAlloc* talloc,
125  struct dtTileCacheCompressor* tcomp,
126  struct dtTileCacheMeshProcess* tmproc);
127 
128  int getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const ;
129 
130  dtCompressedTile* getTileAt(const int tx, const int ty, const int tlayer);
131  dtCompressedTileRef getTileRef(const dtCompressedTile* tile) const;
132  const dtCompressedTile* getTileByRef(dtCompressedTileRef ref) const;
133 
134  dtStatus addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result);
135 
136  dtStatus removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize);
137 
138  dtStatus addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result);
139  dtStatus removeObstacle(const dtObstacleRef ref);
140 
141  dtStatus queryTiles(const float* bmin, const float* bmax,
142  dtCompressedTileRef* results, int* resultCount, const int maxResults) const;
143 
144  dtStatus update(const float /*dt*/, class dtNavMesh* navmesh);
145 
146  dtStatus buildNavMeshTilesAt(const int tx, const int ty, class dtNavMesh* navmesh);
147 
148  dtStatus buildNavMeshTile(const dtCompressedTileRef ref, class dtNavMesh* navmesh);
149 
150  void calcTightTileBounds(const struct dtTileCacheLayerHeader* header, float* bmin, float* bmax) const;
151 
152  void getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const;
153 
154 
156  inline dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
157  {
158  return ((dtCompressedTileRef)salt << m_tileBits) | (dtCompressedTileRef)it;
159  }
160 
162  inline unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
163  {
164  const dtCompressedTileRef saltMask = ((dtCompressedTileRef)1<<m_saltBits)-1;
165  return (unsigned int)((ref >> m_tileBits) & saltMask);
166  }
167 
169  inline unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
170  {
171  const dtCompressedTileRef tileMask = ((dtCompressedTileRef)1<<m_tileBits)-1;
172  return (unsigned int)(ref & tileMask);
173  }
174 
176  inline dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
177  {
178  return ((dtObstacleRef)salt << 16) | (dtObstacleRef)it;
179  }
180 
182  inline unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
183  {
184  const dtObstacleRef saltMask = ((dtObstacleRef)1<<16)-1;
185  return (unsigned int)((ref >> 16) & saltMask);
186  }
187 
189  inline unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
190  {
191  const dtObstacleRef tileMask = ((dtObstacleRef)1<<16)-1;
192  return (unsigned int)(ref & tileMask);
193  }
194 
195 
196 private:
197 
198  enum ObstacleRequestAction
199  {
200  REQUEST_ADD,
201  REQUEST_REMOVE,
202  };
203 
204  struct ObstacleRequest
205  {
206  int action;
207  dtObstacleRef ref;
208  };
209 
210  int m_tileLutSize;
211  int m_tileLutMask;
212 
213  dtCompressedTile** m_posLookup;
214  dtCompressedTile* m_nextFreeTile;
215  dtCompressedTile* m_tiles;
216 
217  unsigned int m_saltBits;
218  unsigned int m_tileBits;
219 
220  dtTileCacheParams m_params;
221 
222  dtTileCacheAlloc* m_talloc;
223  dtTileCacheCompressor* m_tcomp;
224  dtTileCacheMeshProcess* m_tmproc;
225 
226  dtTileCacheObstacle* m_obstacles;
227  dtTileCacheObstacle* m_nextFreeObstacle;
228 
229  static const int MAX_REQUESTS = 64;
230  ObstacleRequest m_reqs[MAX_REQUESTS];
231  int m_nreqs;
232 
233  static const int MAX_UPDATE = 64;
234  dtCompressedTileRef m_update[MAX_UPDATE];
235  int m_nupdate;
236 
237 };
238 
239 ATOMSUTILS_EXPORT dtTileCache* dtAllocTileCache();
240 ATOMSUTILS_EXPORT void dtFreeTileCache(dtTileCache* tc);
241 
242 }
243 
244 #endif
Definition: DetourNavMesh.h:427
Definition: DetourTileCache.h:103
unsigned int decodeObstacleIdSalt(dtObstacleRef ref) const
Decodes an obstacle salt.
Definition: DetourTileCache.h:182
dtCompressedTileRef encodeTileId(unsigned int salt, unsigned int it) const
Encodes a tile id.
Definition: DetourTileCache.h:156
unsigned int decodeObstacleIdObstacle(dtObstacleRef ref) const
Decodes an obstacle id.
Definition: DetourTileCache.h:189
unsigned int decodeTileIdTile(dtCompressedTileRef ref) const
Decodes a tile id.
Definition: DetourTileCache.h:169
dtObstacleRef encodeObstacleId(unsigned int salt, unsigned int it) const
Encodes an obstacle id.
Definition: DetourTileCache.h:176
unsigned int decodeTileIdSalt(dtCompressedTileRef ref) const
Decodes a tile salt.
Definition: DetourTileCache.h:162
AtomsCore namespace.
Definition: Base64.h:13
dtCompressedTileFlags
Flags for addTile.
Definition: DetourTileCache.h:36
@ DT_COMPRESSEDTILE_FREE_DATA
Navmesh owns the tile memory and should free it.
Definition: DetourTileCache.h:37
Definition: DetourTileCache.h:41
unsigned int salt
Counter describing modifications to the tile.
Definition: DetourTileCache.h:42
Definition: DetourNavMeshBuilder.h:62
Definition: DetourTileCacheBuilder.h:128
Definition: DetourTileCacheBuilder.h:147
Definition: DetourTileCacheBuilder.h:36
Definition: DetourTileCacheBuilder.h:47
Definition: DetourTileCache.h:95
Definition: DetourTileCache.h:62
Definition: DetourTileCache.h:74