Atoms Crowd  7.0.0
DetourTileCacheBuilder.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 DETOURTILECACHEBUILDER_H
20 #define DETOURTILECACHEBUILDER_H
21 
22 #include <AtomsUtils/Globals.h>
23 #include <AtomsUtils/NavigationMesh/Detour/DetourAlloc.h>
24 #include <AtomsUtils/NavigationMesh/Detour/DetourStatus.h>
25 
26 namespace AtomsUtils {
27 
28 static const int DT_TILECACHE_MAGIC = 'D'<<24 | 'T'<<16 | 'L'<<8 | 'R';
29 static const int DT_TILECACHE_VERSION = 1;
30 
31 static const unsigned char DT_TILECACHE_NULL_AREA = 0;
32 static const unsigned char DT_TILECACHE_WALKABLE_AREA = 63;
33 static const unsigned short DT_TILECACHE_NULL_IDX = 0xffff;
34 
36 {
37  int magic;
38  int version;
39  int tx,ty,tlayer;
40  float bmin[3], bmax[3];
41  unsigned short hmin, hmax;
42  unsigned short width, height;
43  unsigned short minx, maxx, miny, maxy;
44 };
45 
47 {
48  dtTileCacheLayerHeader* header;
49  unsigned short regCount;
50  unsigned short* heights;
51  unsigned char* areas;
52  unsigned char* cons;
53  unsigned short* regs;
54 };
55 
57 {
58  int nverts;
59  unsigned short* verts;
60  unsigned short reg;
61  unsigned char area;
62 };
63 
65 {
66  int nconts;
67  dtTileCacheContour* conts;
68 };
69 
71 {
72  int nclusters;
73  int nregs;
74  int npolys;
75  unsigned short* regMap;
76  unsigned short* polyMap;
77 };
78 
80 {
81  int nvp;
82  int nverts;
83  int npolys;
84  unsigned short* verts;
85  unsigned short* polys;
86  unsigned short* flags;
87  unsigned char* areas;
88  unsigned short* regs;
89 };
90 
91 
93 {
94  int nmeshes;
95  int nverts;
96  int ntris;
97  unsigned int* meshes;
98  float* verts;
99  unsigned char* tris;
100 };
101 
103 {
104  unsigned short maxDist;
105  unsigned short* data;
106 };
107 
108 class ATOMSUTILS_EXPORT dtTileCacheLogContext
109 {
110 public:
114  void dtLog(const char* format, ...);
115 
116 protected:
117 
122  virtual void doDtLog(const char* /*msg*/, const int /*len*/) {}
123 };
124 
125 
126 
127 struct ATOMSUTILS_EXPORT dtTileCacheAlloc
128 {
129  virtual ~dtTileCacheAlloc() = default;
130 
131  virtual void reset()
132  {
133  }
134 
135  virtual void* alloc(const int size)
136  {
137  return dtAlloc(size, DT_ALLOC_TEMP);
138  }
139 
140  virtual void free(void* ptr)
141  {
142  dtFree(ptr);
143  }
144 };
145 
146 struct ATOMSUTILS_EXPORT dtTileCacheCompressor
147 {
148  virtual int maxCompressedSize(const int bufferSize) = 0;
149  virtual dtStatus compress(const unsigned char* buffer, const int bufferSize,
150  unsigned char* compressed, const int maxCompressedSize, int* compressedSize) = 0;
151  virtual dtStatus decompress(const unsigned char* compressed, const int compressedSize,
152  unsigned char* buffer, const int maxBufferSize, int* bufferSize) = 0;
153 };
154 
155 
156 template<class T> class dtFixedArray
157 {
158  dtTileCacheAlloc* m_alloc;
159  T* m_ptr;
160  const int m_size;
161  inline T* operator=(T* p);
162  inline void operator=(dtFixedArray<T>& p);
163  inline dtFixedArray();
164 public:
165  inline dtFixedArray(dtTileCacheAlloc* a, const int s) : m_alloc(a), m_ptr((T*)a->alloc(sizeof(T)*s)), m_size(s) {}
166  inline ~dtFixedArray() { if (m_alloc) m_alloc->free(m_ptr); }
167  inline operator T*() { return m_ptr; }
168  inline int size() const { return m_size; }
169  inline void set(unsigned char v) { memset(m_ptr, v, sizeof(T)*m_size); }
170 };
171 
172 inline int getDirOffsetX(int dir)
173 {
174  const int offset[4] = { -1, 0, 1, 0, };
175  return offset[dir&0x03];
176 }
177 
178 inline int getDirOffsetY(int dir)
179 {
180  const int offset[4] = { 0, 1, 0, -1 };
181  return offset[dir&0x03];
182 }
183 
184 
185 ATOMSUTILS_EXPORT dtStatus dtBuildTileCacheLayer(dtTileCacheCompressor* comp,
186  dtTileCacheLayerHeader* header,
187  const unsigned short* heights,
188  const unsigned char* areas,
189  const unsigned char* cons,
190  unsigned char** outData, int* outDataSize);
191 
192 ATOMSUTILS_EXPORT void dtFreeTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheLayer* layer);
193 
194 ATOMSUTILS_EXPORT dtStatus dtDecompressTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheCompressor* comp,
195  unsigned char* compressed, const int compressedSize,
196  dtTileCacheLayer** layerOut);
197 
198 ATOMSUTILS_EXPORT dtTileCacheContourSet* dtAllocTileCacheContourSet(dtTileCacheAlloc* alloc);
199 ATOMSUTILS_EXPORT void dtFreeTileCacheContourSet(dtTileCacheAlloc* alloc, dtTileCacheContourSet* cset);
200 
201 ATOMSUTILS_EXPORT dtTileCacheClusterSet* dtAllocTileCacheClusterSet(dtTileCacheAlloc* alloc);
202 ATOMSUTILS_EXPORT void dtFreeTileCacheClusterSet(dtTileCacheAlloc* alloc, dtTileCacheClusterSet* clusters);
203 
204 ATOMSUTILS_EXPORT dtTileCachePolyMesh* dtAllocTileCachePolyMesh(dtTileCacheAlloc* alloc);
205 ATOMSUTILS_EXPORT void dtFreeTileCachePolyMesh(dtTileCacheAlloc* alloc, dtTileCachePolyMesh* lmesh);
206 
207 
208 ATOMSUTILS_EXPORT dtTileCachePolyMeshDetail* dtAllocTileCachePolyMeshDetail(dtTileCacheAlloc* alloc);
209 ATOMSUTILS_EXPORT void dtFreeTileCachePolyMeshDetail(dtTileCacheAlloc* alloc, dtTileCachePolyMeshDetail* dmesh);
210 
211 ATOMSUTILS_EXPORT dtTileCacheDistanceField* dtAllocTileCacheDistanceField(dtTileCacheAlloc* alloc);
212 ATOMSUTILS_EXPORT void dtFreeTileCacheDistanceField(dtTileCacheAlloc* alloc, dtTileCacheDistanceField* dfield);
213 
214 
215 ATOMSUTILS_EXPORT dtStatus dtMarkCylinderArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
216  const float* pos, const float radius, const float height, const unsigned char areaId);
217 
218 
219 ATOMSUTILS_EXPORT dtStatus dtMarkBoxArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
220  const float* pos, const float* extent, const unsigned char areaId);
221 
222 ATOMSUTILS_EXPORT dtStatus dtMarkConvexArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
223  const float* verts, const int nverts, const float hmin, const float hmax, const unsigned char areaId);
224 
225 ATOMSUTILS_EXPORT dtStatus dtReplaceCylinderArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
226  const float* pos, const float radius, const float height, const unsigned char areaId,
227  const unsigned char filterAreaId);
228 
229 ATOMSUTILS_EXPORT dtStatus dtReplaceBoxArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
230  const float* pos, const float* extent, const unsigned char areaId, const unsigned char filterAreaId);
231 
232 ATOMSUTILS_EXPORT dtStatus dtReplaceConvexArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
233  const float* verts, const int nverts, const float hmin, const float hmax, const unsigned char areaId,
234  const unsigned char filterAreaId);
235 
236 ATOMSUTILS_EXPORT dtStatus dtReplaceArea(dtTileCacheLayer& layer, const unsigned char areaId, const unsigned char filterAreaId);
237 
238 
239 
240 ATOMSUTILS_EXPORT dtStatus dtBuildTileCacheDistanceField(dtTileCacheAlloc* alloc, dtTileCacheLayer& layer, dtTileCacheDistanceField& dfield);
241 
242 ATOMSUTILS_EXPORT dtStatus dtBuildTileCacheRegions(dtTileCacheAlloc* alloc,
243  const int minRegionArea, const int mergeRegionArea,
244  dtTileCacheLayer& layer, dtTileCacheDistanceField dfield);
245 
246 ATOMSUTILS_EXPORT dtStatus dtBuildTileCacheRegionsMonotone(dtTileCacheAlloc* alloc,
247  const int minRegionArea, const int mergeRegionArea,
248  dtTileCacheLayer& layer);
249 
250 ATOMSUTILS_EXPORT dtStatus dtBuildTileCacheRegionsChunky(dtTileCacheAlloc* alloc,
251  const int minRegionArea, const int mergeRegionArea,
252  dtTileCacheLayer& layer, int regionChunkSize);
253 
254 
255 ATOMSUTILS_EXPORT dtStatus dtBuildTileCacheContours(dtTileCacheAlloc* alloc,
256  dtTileCacheLayer& layer,
257  const int walkableClimb, const float maxError,
258  const float cs, const float ch,
259  dtTileCacheContourSet& lcset,
260  dtTileCacheClusterSet& lclusters);
261 
262 ATOMSUTILS_EXPORT dtStatus dtBuildTileCachePolyMesh(dtTileCacheAlloc* alloc,
263  dtTileCacheLogContext* ctx,
264  dtTileCacheContourSet& lcset,
265  dtTileCachePolyMesh& mesh);
266 
267 
268 ATOMSUTILS_EXPORT dtStatus dtBuildTileCachePolyMeshDetail(dtTileCacheAlloc* alloc,
269  const float cs, const float ch,
270  const float sampleDist, const float sampleMaxError,
271  dtTileCacheLayer& layer,
272  dtTileCachePolyMesh& lmesh,
273  dtTileCachePolyMeshDetail& dmesh);
274 
275 ATOMSUTILS_EXPORT dtStatus dtBuildTileCacheClusters(dtTileCacheAlloc* alloc,
276  dtTileCacheClusterSet& lclusters,
277  dtTileCachePolyMesh& lmesh);
278 
279 
285 ATOMSUTILS_EXPORT bool dtTileCacheHeaderSwapEndian(unsigned char* data, const int dataSize);
286 
287 }
288 
289 #endif // DETOURTILECACHEBUILDER_H
Definition: DetourTileCacheBuilder.h:157
Definition: DetourTileCacheBuilder.h:109
void dtLog(const char *format,...)
virtual void doDtLog(const char *, const int)
Definition: DetourTileCacheBuilder.h:122
AtomsCore namespace.
Definition: Base64.h:13
ATOMSUTILS_EXPORT void * dtAlloc(int size, dtAllocHint hint)
@ DT_ALLOC_TEMP
Memory used temporarily within a function.
Definition: DetourAlloc.h:31
ATOMSUTILS_EXPORT bool dtTileCacheHeaderSwapEndian(unsigned char *data, const int dataSize)
ATOMSUTILS_EXPORT void dtFree(void *ptr)
Definition: DetourTileCacheBuilder.h:128
Definition: DetourTileCacheBuilder.h:71
int npolys
The number of polys.
Definition: DetourTileCacheBuilder.h:74
int nclusters
The number of clusters.
Definition: DetourTileCacheBuilder.h:72
int nregs
The number of regions.
Definition: DetourTileCacheBuilder.h:73
unsigned short * regMap
Cluster Id for each region [size: nregs].
Definition: DetourTileCacheBuilder.h:75
unsigned short * polyMap
Cluster Id for each poly [size: npolys].
Definition: DetourTileCacheBuilder.h:76
Definition: DetourTileCacheBuilder.h:147
Definition: DetourTileCacheBuilder.h:57
Definition: DetourTileCacheBuilder.h:65
Definition: DetourTileCacheBuilder.h:103
unsigned short maxDist
Max distance.
Definition: DetourTileCacheBuilder.h:104
unsigned short * data
distance for every cell in layer
Definition: DetourTileCacheBuilder.h:105
Definition: DetourTileCacheBuilder.h:36
int magic
Data magic.
Definition: DetourTileCacheBuilder.h:37
unsigned short height
Dimension of the layer.
Definition: DetourTileCacheBuilder.h:42
int version
Data version.
Definition: DetourTileCacheBuilder.h:38
unsigned short hmax
Height min/max range.
Definition: DetourTileCacheBuilder.h:41
unsigned short maxy
Usable sub-region.
Definition: DetourTileCacheBuilder.h:43
Definition: DetourTileCacheBuilder.h:47
unsigned short regCount
Region count.
Definition: DetourTileCacheBuilder.h:49
Definition: DetourTileCacheBuilder.h:93
int nverts
The number of vertices in verts.
Definition: DetourTileCacheBuilder.h:95
float * verts
The mesh vertices. [Size: 3*nverts].
Definition: DetourTileCacheBuilder.h:98
unsigned int * meshes
The sub-mesh data. [Size: 4*nmeshes].
Definition: DetourTileCacheBuilder.h:97
int ntris
The number of triangles in tris.
Definition: DetourTileCacheBuilder.h:96
unsigned char * tris
The mesh triangles. [Size: 4*ntris].
Definition: DetourTileCacheBuilder.h:99
int nmeshes
The number of sub-meshes defined by meshes.
Definition: DetourTileCacheBuilder.h:94
Definition: DetourTileCacheBuilder.h:80
unsigned short * flags
Per polygon flags.
Definition: DetourTileCacheBuilder.h:86
unsigned short * verts
Vertices of the mesh, 3 elements per vertex.
Definition: DetourTileCacheBuilder.h:84
int npolys
Number of polygons.
Definition: DetourTileCacheBuilder.h:83
unsigned short * polys
Polygons of the mesh, nvp*2 elements per polygon.
Definition: DetourTileCacheBuilder.h:85
unsigned char * areas
Area ID of polygons.
Definition: DetourTileCacheBuilder.h:87
unsigned short * regs
Region ID of polygon.
Definition: DetourTileCacheBuilder.h:88
int nverts
Number of vertices.
Definition: DetourTileCacheBuilder.h:82