Debian Codegraph @

DebugUtils/Source/RecastDump.cpp

raw
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
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 #include <math.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include "Recast.h"
24 #include "RecastAlloc.h"
25 #include "RecastDump.h"
26
27 duFileIO::~duFileIO()
28 {
29 // Defined out of line to fix the weak v-tables warning
30 }
31
32 static void ioprintf(duFileIO* io, const char* format, ...)
33 {
34 char line[256];
35 va_list ap;
36 va_start(ap, format);
37 const int n = vsnprintf(line, sizeof(line), format, ap);
38 va_end(ap);
39 if (n > 0)
40 io->write(line, sizeof(char)*n);
41 }
42
43 bool duDumpPolyMeshToObj(rcPolyMesh& pmesh, duFileIO* io)
44 {
45 if (!io)
46 {
47 printf("duDumpPolyMeshToObj: input IO is null.\n");
48 return false;
49 }
50 if (!io->isWriting())
51 {
52 printf("duDumpPolyMeshToObj: input IO not writing.\n");
53 return false;
54 }
55
56 const int nvp = pmesh.nvp;
57 const float cs = pmesh.cs;
58 const float ch = pmesh.ch;
59 const float* orig = pmesh.bmin;
60
61 ioprintf(io, "# Recast Navmesh\n");
62 ioprintf(io, "o NavMesh\n");
63
64 ioprintf(io, "\n");
65
66 for (int i = 0; i < pmesh.nverts; ++i)
67 {
68 const unsigned short* v = &pmesh.verts[i*3];
69 const float x = orig[0] + v[0]*cs;
70 const float y = orig[1] + (v[1]+1)*ch + 0.1f;
71 const float z = orig[2] + v[2]*cs;
72 ioprintf(io, "v %f %f %f\n", x,y,z);
73 }
74
75 ioprintf(io, "\n");
76
77 for (int i = 0; i < pmesh.npolys; ++i)
78 {
79 const unsigned short* p = &pmesh.polys[i*nvp*2];
80 for (int j = 2; j < nvp; ++j)
81 {
82 if (p[j] == RC_MESH_NULL_IDX) break;
83 ioprintf(io, "f %d %d %d\n", p[0]+1, p[j-1]+1, p[j]+1);
84 }
85 }
86
87 return true;
88 }
89
90 bool duDumpPolyMeshDetailToObj(rcPolyMeshDetail& dmesh, duFileIO* io)
91 {
92 if (!io)
93 {
94 printf("duDumpPolyMeshDetailToObj: input IO is null.\n");
95 return false;
96 }
97 if (!io->isWriting())
98 {
99 printf("duDumpPolyMeshDetailToObj: input IO not writing.\n");
100 return false;
101 }
102
103 ioprintf(io, "# Recast Navmesh\n");
104 ioprintf(io, "o NavMesh\n");
105
106 ioprintf(io, "\n");
107
108 for (int i = 0; i < dmesh.nverts; ++i)
109 {
110 const float* v = &dmesh.verts[i*3];
111 ioprintf(io, "v %f %f %f\n", v[0],v[1],v[2]);
112 }
113
114 ioprintf(io, "\n");
115
116 for (int i = 0; i < dmesh.nmeshes; ++i)
117 {
118 const unsigned int* m = &dmesh.meshes[i*4];
119 const unsigned int bverts = m[0];
120 const unsigned int btris = m[2];
121 const unsigned int ntris = m[3];
122 const unsigned char* tris = &dmesh.tris[btris*4];
123 for (unsigned int j = 0; j < ntris; ++j)
124 {
125 ioprintf(io, "f %d %d %d\n",
126 (int)(bverts+tris[j*4+0])+1,
127 (int)(bverts+tris[j*4+1])+1,
128 (int)(bverts+tris[j*4+2])+1);
129 }
130 }
131
132 return true;
133 }
134
135 static const int CSET_MAGIC = ('c' << 24) | ('s' << 16) | ('e' << 8) | 't';
136 static const int CSET_VERSION = 2;
137
138 bool duDumpContourSet(struct rcContourSet& cset, duFileIO* io)
139 {
140 if (!io)
141 {
142 printf("duDumpContourSet: input IO is null.\n");
143 return false;
144 }
145 if (!io->isWriting())
146 {
147 printf("duDumpContourSet: input IO not writing.\n");
148 return false;
149 }
150
151 io->write(&CSET_MAGIC, sizeof(CSET_MAGIC));
152 io->write(&CSET_VERSION, sizeof(CSET_VERSION));
153
154 io->write(&cset.nconts, sizeof(cset.nconts));
155
156 io->write(cset.bmin, sizeof(cset.bmin));
157 io->write(cset.bmax, sizeof(cset.bmax));
158
159 io->write(&cset.cs, sizeof(cset.cs));
160 io->write(&cset.ch, sizeof(cset.ch));
161
162 io->write(&cset.width, sizeof(cset.width));
163 io->write(&cset.height, sizeof(cset.height));
164 io->write(&cset.borderSize, sizeof(cset.borderSize));
165
166 for (int i = 0; i < cset.nconts; ++i)
167 {
168 const rcContour& cont = cset.conts[i];
169 io->write(&cont.nverts, sizeof(cont.nverts));
170 io->write(&cont.nrverts, sizeof(cont.nrverts));
171 io->write(&cont.reg, sizeof(cont.reg));
172 io->write(&cont.area, sizeof(cont.area));
173 io->write(cont.verts, sizeof(int)*4*cont.nverts);
174 io->write(cont.rverts, sizeof(int)*4*cont.nrverts);
175 }
176
177 return true;
178 }
179
180 bool duReadContourSet(struct rcContourSet& cset, duFileIO* io)
181 {
182 if (!io)
183 {
184 printf("duReadContourSet: input IO is null.\n");
185 return false;
186 }
187 if (!io->isReading())
188 {
189 printf("duReadContourSet: input IO not reading.\n");
190 return false;
191 }
192
193 int magic = 0;
194 int version = 0;
195
196 io->read(&magic, sizeof(magic));
197 io->read(&version, sizeof(version));
198
199 if (magic != CSET_MAGIC)
200 {
201 printf("duReadContourSet: Bad voodoo.\n");
202 return false;
203 }
204 if (version != CSET_VERSION)
205 {
206 printf("duReadContourSet: Bad version.\n");
207 return false;
208 }
209
210 io->read(&cset.nconts, sizeof(cset.nconts));
211
212 cset.conts = (rcContour*)rcAlloc(sizeof(rcContour)*cset.nconts, RC_ALLOC_PERM);
213 if (!cset.conts)
214 {
215 printf("duReadContourSet: Could not alloc contours (%d)\n", cset.nconts);
216 return false;
217 }
218 memset(cset.conts, 0, sizeof(rcContour)*cset.nconts);
219
220 io->read(cset.bmin, sizeof(cset.bmin));
221 io->read(cset.bmax, sizeof(cset.bmax));
222
223 io->read(&cset.cs, sizeof(cset.cs));
224 io->read(&cset.ch, sizeof(cset.ch));
225
226 io->read(&cset.width, sizeof(cset.width));
227 io->read(&cset.height, sizeof(cset.height));
228 io->read(&cset.borderSize, sizeof(cset.borderSize));
229
230 for (int i = 0; i < cset.nconts; ++i)
231 {
232 rcContour& cont = cset.conts[i];
233 io->read(&cont.nverts, sizeof(cont.nverts));
234 io->read(&cont.nrverts, sizeof(cont.nrverts));
235 io->read(&cont.reg, sizeof(cont.reg));
236 io->read(&cont.area, sizeof(cont.area));
237
238 cont.verts = (int*)rcAlloc(sizeof(int)*4*cont.nverts, RC_ALLOC_PERM);
239 if (!cont.verts)
240 {
241 printf("duReadContourSet: Could not alloc contour verts (%d)\n", cont.nverts);
242 return false;
243 }
244 cont.rverts = (int*)rcAlloc(sizeof(int)*4*cont.nrverts, RC_ALLOC_PERM);
245 if (!cont.rverts)
246 {
247 printf("duReadContourSet: Could not alloc contour rverts (%d)\n", cont.nrverts);
248 return false;
249 }
250
251 io->read(cont.verts, sizeof(int)*4*cont.nverts);
252 io->read(cont.rverts, sizeof(int)*4*cont.nrverts);
253 }
254
255 return true;
256 }
257
258
259 static const int CHF_MAGIC = ('r' << 24) | ('c' << 16) | ('h' << 8) | 'f';
260 static const int CHF_VERSION = 3;
261
262 bool duDumpCompactHeightfield(struct rcCompactHeightfield& chf, duFileIO* io)
263 {
264 if (!io)
265 {
266 printf("duDumpCompactHeightfield: input IO is null.\n");
267 return false;
268 }
269 if (!io->isWriting())
270 {
271 printf("duDumpCompactHeightfield: input IO not writing.\n");
272 return false;
273 }
274
275 io->write(&CHF_MAGIC, sizeof(CHF_MAGIC));
276 io->write(&CHF_VERSION, sizeof(CHF_VERSION));
277
278 io->write(&chf.width, sizeof(chf.width));
279 io->write(&chf.height, sizeof(chf.height));
280 io->write(&chf.spanCount, sizeof(chf.spanCount));
281
282 io->write(&chf.walkableHeight, sizeof(chf.walkableHeight));
283 io->write(&chf.walkableClimb, sizeof(chf.walkableClimb));
284 io->write(&chf.borderSize, sizeof(chf.borderSize));
285
286 io->write(&chf.maxDistance, sizeof(chf.maxDistance));
287 io->write(&chf.maxRegions, sizeof(chf.maxRegions));
288
289 io->write(chf.bmin, sizeof(chf.bmin));
290 io->write(chf.bmax, sizeof(chf.bmax));
291
292 io->write(&chf.cs, sizeof(chf.cs));
293 io->write(&chf.ch, sizeof(chf.ch));
294
295 int tmp = 0;
296 if (chf.cells) tmp |= 1;
297 if (chf.spans) tmp |= 2;
298 if (chf.dist) tmp |= 4;
299 if (chf.areas) tmp |= 8;
300
301 io->write(&tmp, sizeof(tmp));
302
303 if (chf.cells)
304 io->write(chf.cells, sizeof(rcCompactCell)*chf.width*chf.height);
305 if (chf.spans)
306 io->write(chf.spans, sizeof(rcCompactSpan)*chf.spanCount);
307 if (chf.dist)
308 io->write(chf.dist, sizeof(unsigned short)*chf.spanCount);
309 if (chf.areas)
310 io->write(chf.areas, sizeof(unsigned char)*chf.spanCount);
311
312 return true;
313 }
314
315 bool duReadCompactHeightfield(struct rcCompactHeightfield& chf, duFileIO* io)
316 {
317 if (!io)
318 {
319 printf("duReadCompactHeightfield: input IO is null.\n");
320 return false;
321 }
322 if (!io->isReading())
323 {
324 printf("duReadCompactHeightfield: input IO not reading.\n");
325 return false;
326 }
327
328 int magic = 0;
329 int version = 0;
330
331 io->read(&magic, sizeof(magic));
332 io->read(&version, sizeof(version));
333
334 if (magic != CHF_MAGIC)
335 {
336 printf("duReadCompactHeightfield: Bad voodoo.\n");
337 return false;
338 }
339 if (version != CHF_VERSION)
340 {
341 printf("duReadCompactHeightfield: Bad version.\n");
342 return false;
343 }
344
345 io->read(&chf.width, sizeof(chf.width));
346 io->read(&chf.height, sizeof(chf.height));
347 io->read(&chf.spanCount, sizeof(chf.spanCount));
348
349 io->read(&chf.walkableHeight, sizeof(chf.walkableHeight));
350 io->read(&chf.walkableClimb, sizeof(chf.walkableClimb));
351 io->read(&chf.borderSize, sizeof(chf.borderSize));
352
353 io->read(&chf.maxDistance, sizeof(chf.maxDistance));
354 io->read(&chf.maxRegions, sizeof(chf.maxRegions));
355
356 io->read(chf.bmin, sizeof(chf.bmin));
357 io->read(chf.bmax, sizeof(chf.bmax));
358
359 io->read(&chf.cs, sizeof(chf.cs));
360 io->read(&chf.ch, sizeof(chf.ch));
361
362 int tmp = 0;
363 io->read(&tmp, sizeof(tmp));
364
365 if (tmp & 1)
366 {
367 chf.cells = (rcCompactCell*)rcAlloc(sizeof(rcCompactCell)*chf.width*chf.height, RC_ALLOC_PERM);
368 if (!chf.cells)
369 {
370 printf("duReadCompactHeightfield: Could not alloc cells (%d)\n", chf.width*chf.height);
371 return false;
372 }
373 io->read(chf.cells, sizeof(rcCompactCell)*chf.width*chf.height);
374 }
375 if (tmp & 2)
376 {
377 chf.spans = (rcCompactSpan*)rcAlloc(sizeof(rcCompactSpan)*chf.spanCount, RC_ALLOC_PERM);
378 if (!chf.spans)
379 {
380 printf("duReadCompactHeightfield: Could not alloc spans (%d)\n", chf.spanCount);
381 return false;
382 }
383 io->read(chf.spans, sizeof(rcCompactSpan)*chf.spanCount);
384 }
385 if (tmp & 4)
386 {
387 chf.dist = (unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount, RC_ALLOC_PERM);
388 if (!chf.dist)
389 {
390 printf("duReadCompactHeightfield: Could not alloc dist (%d)\n", chf.spanCount);
391 return false;
392 }
393 io->read(chf.dist, sizeof(unsigned short)*chf.spanCount);
394 }
395 if (tmp & 8)
396 {
397 chf.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_PERM);
398 if (!chf.areas)
399 {
400 printf("duReadCompactHeightfield: Could not alloc areas (%d)\n", chf.spanCount);
401 return false;
402 }
403 io->read(chf.areas, sizeof(unsigned char)*chf.spanCount);
404 }
405
406 return true;
407 }
408
409
410 static void logLine(rcContext& ctx, rcTimerLabel label, const char* name, const float pc)
411 {
412 const int t = ctx.getAccumulatedTime(label);
413 if (t < 0) return;
414 ctx.log(RC_LOG_PROGRESS, "%s:\t%.2fms\t(%.1f%%)", name, t/1000.0f, t*pc);
415 }
416
417 void duLogBuildTimes(rcContext& ctx, const int totalTimeUsec)
418 {
419 const float pc = 100.0f / totalTimeUsec;
420
421 ctx.log(RC_LOG_PROGRESS, "Build Times");
422 logLine(ctx, RC_TIMER_RASTERIZE_TRIANGLES, "- Rasterize", pc);
423 logLine(ctx, RC_TIMER_BUILD_COMPACTHEIGHTFIELD, "- Build Compact", pc);
424 logLine(ctx, RC_TIMER_FILTER_BORDER, "- Filter Border", pc);
425 logLine(ctx, RC_TIMER_FILTER_WALKABLE, "- Filter Walkable", pc);
426 logLine(ctx, RC_TIMER_ERODE_AREA, "- Erode Area", pc);
427 logLine(ctx, RC_TIMER_MEDIAN_AREA, "- Median Area", pc);
428 logLine(ctx, RC_TIMER_MARK_BOX_AREA, "- Mark Box Area", pc);
429 logLine(ctx, RC_TIMER_MARK_CONVEXPOLY_AREA, "- Mark Convex Area", pc);
430 logLine(ctx, RC_TIMER_MARK_CYLINDER_AREA, "- Mark Cylinder Area", pc);
431 logLine(ctx, RC_TIMER_BUILD_DISTANCEFIELD, "- Build Distance Field", pc);
432 logLine(ctx, RC_TIMER_BUILD_DISTANCEFIELD_DIST, " - Distance", pc);
433 logLine(ctx, RC_TIMER_BUILD_DISTANCEFIELD_BLUR, " - Blur", pc);
434 logLine(ctx, RC_TIMER_BUILD_REGIONS, "- Build Regions", pc);
435 logLine(ctx, RC_TIMER_BUILD_REGIONS_WATERSHED, " - Watershed", pc);
436 logLine(ctx, RC_TIMER_BUILD_REGIONS_EXPAND, " - Expand", pc);
437 logLine(ctx, RC_TIMER_BUILD_REGIONS_FLOOD, " - Find Basins", pc);
438 logLine(ctx, RC_TIMER_BUILD_REGIONS_FILTER, " - Filter", pc);
439 logLine(ctx, RC_TIMER_BUILD_LAYERS, "- Build Layers", pc);
440 logLine(ctx, RC_TIMER_BUILD_CONTOURS, "- Build Contours", pc);
441 logLine(ctx, RC_TIMER_BUILD_CONTOURS_TRACE, " - Trace", pc);
442 logLine(ctx, RC_TIMER_BUILD_CONTOURS_SIMPLIFY, " - Simplify", pc);
443 logLine(ctx, RC_TIMER_BUILD_POLYMESH, "- Build Polymesh", pc);
444 logLine(ctx, RC_TIMER_BUILD_POLYMESHDETAIL, "- Build Polymesh Detail", pc);
445 logLine(ctx, RC_TIMER_MERGE_POLYMESH, "- Merge Polymeshes", pc);
446 logLine(ctx, RC_TIMER_MERGE_POLYMESHDETAIL, "- Merge Polymesh Details", pc);
447 ctx.log(RC_LOG_PROGRESS, "=== TOTAL:\t%.2fms", totalTimeUsec/1000.0f);
448 }
449