Debian Codegraph @

RecastDemo/Source/NavMeshPruneTool.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 <string.h>
22 #include <float.h>
23 #include <vector>
24 #include "SDL.h"
25 #include "SDL_opengl.h"
26 #include "imgui.h"
27 #include "NavMeshPruneTool.h"
28 #include "InputGeom.h"
29 #include "Sample.h"
30 #include "DetourNavMesh.h"
31 #include "DetourCommon.h"
32 #include "DetourAssert.h"
33 #include "DetourDebugDraw.h"
34
35 #ifdef WIN32
36 # define snprintf _snprintf
37 #endif
38
39 class NavmeshFlags
40 {
41 struct TileFlags
42 {
43 inline void purge() { dtFree(flags); }
44 unsigned char* flags;
45 int nflags;
46 dtPolyRef base;
47 };
48
49 const dtNavMesh* m_nav;
50 TileFlags* m_tiles;
51 int m_ntiles;
52
53 public:
54 NavmeshFlags() :
55 m_nav(0), m_tiles(0), m_ntiles(0)
56 {
57 }
58
59 ~NavmeshFlags()
60 {
61 for (int i = 0; i < m_ntiles; ++i)
62 m_tiles[i].purge();
63 dtFree(m_tiles);
64 }
65
66 bool init(const dtNavMesh* nav)
67 {
68 m_ntiles = nav->getMaxTiles();
69 if (!m_ntiles)
70 return true;
71 m_tiles = (TileFlags*)dtAlloc(sizeof(TileFlags)*m_ntiles, DT_ALLOC_TEMP);
72 if (!m_tiles)
73 {
74 return false;
75 }
76 memset(m_tiles, 0, sizeof(TileFlags)*m_ntiles);
77
78 // Alloc flags for each tile.
79 for (int i = 0; i < nav->getMaxTiles(); ++i)
80 {
81 const dtMeshTile* tile = nav->getTile(i);
82 if (!tile->header) continue;
83 TileFlags* tf = &m_tiles[i];
84 tf->nflags = tile->header->polyCount;
85 tf->base = nav->getPolyRefBase(tile);
86 if (tf->nflags)
87 {
88 tf->flags = (unsigned char*)dtAlloc(tf->nflags, DT_ALLOC_TEMP);
89 if (!tf->flags)
90 return false;
91 memset(tf->flags, 0, tf->nflags);
92 }
93 }
94
95 m_nav = nav;
96
97 return false;
98 }
99
100 inline void clearAllFlags()
101 {
102 for (int i = 0; i < m_ntiles; ++i)
103 {
104 TileFlags* tf = &m_tiles[i];
105 if (tf->nflags)
106 memset(tf->flags, 0, tf->nflags);
107 }
108 }
109
110 inline unsigned char getFlags(dtPolyRef ref)
111 {
112 dtAssert(m_nav);
113 dtAssert(m_ntiles);
114 // Assume the ref is valid, no bounds checks.
115 unsigned int salt, it, ip;
116 m_nav->decodePolyId(ref, salt, it, ip);
117 return m_tiles[it].flags[ip];
118 }
119
120 inline void setFlags(dtPolyRef ref, unsigned char flags)
121 {
122 dtAssert(m_nav);
123 dtAssert(m_ntiles);
124 // Assume the ref is valid, no bounds checks.
125 unsigned int salt, it, ip;
126 m_nav->decodePolyId(ref, salt, it, ip);
127 m_tiles[it].flags[ip] = flags;
128 }
129
130 };
131
132 static void floodNavmesh(dtNavMesh* nav, NavmeshFlags* flags, dtPolyRef start, unsigned char flag)
133 {
134 // If already visited, skip.
135 if (flags->getFlags(start))
136 return;
137
138 flags->setFlags(start, flag);
139
140 std::vector<dtPolyRef> openList;
141 openList.push_back(start);
142
143 while (openList.size())
144 {
145 const dtPolyRef ref = openList.back();
146 openList.pop_back();
147
148 // Get current poly and tile.
149 // The API input has been cheked already, skip checking internal data.
150 const dtMeshTile* tile = 0;
151 const dtPoly* poly = 0;
152 nav->getTileAndPolyByRefUnsafe(ref, &tile, &poly);
153
154 // Visit linked polygons.
155 for (unsigned int i = poly->firstLink; i != DT_NULL_LINK; i = tile->links[i].next)
156 {
157 const dtPolyRef neiRef = tile->links[i].ref;
158 // Skip invalid and already visited.
159 if (!neiRef || flags->getFlags(neiRef))
160 continue;
161 // Mark as visited
162 flags->setFlags(neiRef, flag);
163 // Visit neighbours
164 openList.push_back(neiRef);
165 }
166 }
167 }
168
169 static void disableUnvisitedPolys(dtNavMesh* nav, NavmeshFlags* flags)
170 {
171 for (int i = 0; i < nav->getMaxTiles(); ++i)
172 {
173 const dtMeshTile* tile = ((const dtNavMesh*)nav)->getTile(i);
174 if (!tile->header) continue;
175 const dtPolyRef base = nav->getPolyRefBase(tile);
176 for (int j = 0; j < tile->header->polyCount; ++j)
177 {
178 const dtPolyRef ref = base | (unsigned int)j;
179 if (!flags->getFlags(ref))
180 {
181 unsigned short f = 0;
182 nav->getPolyFlags(ref, &f);
183 nav->setPolyFlags(ref, f | SAMPLE_POLYFLAGS_DISABLED);
184 }
185 }
186 }
187 }
188
189 NavMeshPruneTool::NavMeshPruneTool() :
190 m_sample(0),
191 m_flags(0),
192 m_hitPosSet(false)
193 {
194 }
195
196 NavMeshPruneTool::~NavMeshPruneTool()
197 {
198 delete m_flags;
199 }
200
201 void NavMeshPruneTool::init(Sample* sample)
202 {
203 m_sample = sample;
204 }
205
206 void NavMeshPruneTool::reset()
207 {
208 m_hitPosSet = false;
209 delete m_flags;
210 m_flags = 0;
211 }
212
213 void NavMeshPruneTool::handleMenu()
214 {
215 dtNavMesh* nav = m_sample->getNavMesh();
216 if (!nav) return;
217 if (!m_flags) return;
218
219 if (imguiButton("Clear Selection"))
220 {
221 m_flags->clearAllFlags();
222 }
223
224 if (imguiButton("Prune Unselected"))
225 {
226 disableUnvisitedPolys(nav, m_flags);
227 delete m_flags;
228 m_flags = 0;
229 }
230 }
231
232 void NavMeshPruneTool::handleClick(const float* s, const float* p, bool shift)
233 {
234 rcIgnoreUnused(s);
235 rcIgnoreUnused(shift);
236
237 if (!m_sample) return;
238 InputGeom* geom = m_sample->getInputGeom();
239 if (!geom) return;
240 dtNavMesh* nav = m_sample->getNavMesh();
241 if (!nav) return;
242 dtNavMeshQuery* query = m_sample->getNavMeshQuery();
243 if (!query) return;
244
245 dtVcopy(m_hitPos, p);
246 m_hitPosSet = true;
247
248 if (!m_flags)
249 {
250 m_flags = new NavmeshFlags;
251 m_flags->init(nav);
252 }
253
254 const float halfExtents[3] = { 2, 4, 2 };
255 dtQueryFilter filter;
256 dtPolyRef ref = 0;
257 query->findNearestPoly(p, halfExtents, &filter, &ref, 0);
258
259 floodNavmesh(nav, m_flags, ref, 1);
260 }
261
262 void NavMeshPruneTool::handleToggle()
263 {
264 }
265
266 void NavMeshPruneTool::handleStep()
267 {
268 }
269
270 void NavMeshPruneTool::handleUpdate(const float /*dt*/)
271 {
272 }
273
274 void NavMeshPruneTool::handleRender()
275 {
276 duDebugDraw& dd = m_sample->getDebugDraw();
277
278 if (m_hitPosSet)
279 {
280 const float s = m_sample->getAgentRadius();
281 const unsigned int col = duRGBA(255,255,255,255);
282 dd.begin(DU_DRAW_LINES);
283 dd.vertex(m_hitPos[0]-s,m_hitPos[1],m_hitPos[2], col);
284 dd.vertex(m_hitPos[0]+s,m_hitPos[1],m_hitPos[2], col);
285 dd.vertex(m_hitPos[0],m_hitPos[1]-s,m_hitPos[2], col);
286 dd.vertex(m_hitPos[0],m_hitPos[1]+s,m_hitPos[2], col);
287 dd.vertex(m_hitPos[0],m_hitPos[1],m_hitPos[2]-s, col);
288 dd.vertex(m_hitPos[0],m_hitPos[1],m_hitPos[2]+s, col);
289 dd.end();
290 }
291
292 const dtNavMesh* nav = m_sample->getNavMesh();
293 if (m_flags && nav)
294 {
295 for (int i = 0; i < nav->getMaxTiles(); ++i)
296 {
297 const dtMeshTile* tile = nav->getTile(i);
298 if (!tile->header) continue;
299 const dtPolyRef base = nav->getPolyRefBase(tile);
300 for (int j = 0; j < tile->header->polyCount; ++j)
301 {
302 const dtPolyRef ref = base | (unsigned int)j;
303 if (m_flags->getFlags(ref))
304 {
305 duDebugDrawNavMeshPoly(&dd, *nav, ref, duRGBA(255,255,255,128));
306 }
307 }
308 }
309 }
310
311 }
312
313 void NavMeshPruneTool::handleRenderOverlay(double* proj, double* model, int* view)
314 {
315 rcIgnoreUnused(model);
316 rcIgnoreUnused(proj);
317
318 // Tool help
319 const int h = view[3];
320
321 imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB: Click fill area.", imguiRGBA(255,255,255,192));
322 }