Planeshift
DetourNavMeshQuery.h
Go to the documentation of this file.
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 DETOURNAVMESHQUERY_H
20 #define DETOURNAVMESHQUERY_H
21 
22 #include "DetourNavMesh.h"
23 #include "DetourStatus.h"
24 
25 
26 // Define DT_VIRTUAL_QUERYFILTER if you wish to derive a custom filter from dtQueryFilter.
27 // On certain platforms indirect or virtual function call is expensive. The default
28 // setting is to use non-virtual functions, the actual implementations of the functions
29 // are declared as inline for maximum speed.
30 
31 //#define DT_VIRTUAL_QUERYFILTER 1
32 
36 {
37  float m_areaCost[DT_MAX_AREAS];
38  unsigned short m_includeFlags;
39  unsigned short m_excludeFlags;
40 
41 public:
42  dtQueryFilter();
43 
48 #ifdef DT_VIRTUAL_QUERYFILTER
49  virtual bool passFilter(const dtPolyRef ref,
50  const dtMeshTile* tile,
51  const dtPoly* poly) const;
52 #else
53  bool passFilter(const dtPolyRef ref,
54  const dtMeshTile* tile,
55  const dtPoly* poly) const;
56 #endif
57 
71 #ifdef DT_VIRTUAL_QUERYFILTER
72  virtual float getCost(const float* pa, const float* pb,
73  const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
74  const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
75  const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
76 #else
77  float getCost(const float* pa, const float* pb,
78  const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
79  const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
80  const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
81 #endif
82 
85 
89  inline float getAreaCost(const int i) const { return m_areaCost[i]; }
90 
94  inline void setAreaCost(const int i, const float cost) { m_areaCost[i] = cost; }
95 
99  inline unsigned short getIncludeFlags() const { return m_includeFlags; }
100 
103  inline void setIncludeFlags(const unsigned short flags) { m_includeFlags = flags; }
104 
108  inline unsigned short getExcludeFlags() const { return m_excludeFlags; }
109 
112  inline void setExcludeFlags(const unsigned short flags) { m_excludeFlags = flags; }
113 
115 
116 };
117 
122 {
123 public:
124  dtNavMeshQuery();
125  ~dtNavMeshQuery();
126 
131  dtStatus init(const dtNavMesh* nav, const int maxNodes);
132 
134  // /@{
135 
146  dtStatus findPath(dtPolyRef startRef, dtPolyRef endRef,
147  const float* startPos, const float* endPos,
148  const dtQueryFilter* filter,
149  dtPolyRef* path, int* pathCount, const int maxPath) const;
150 
162  dtStatus findStraightPath(const float* startPos, const float* endPos,
163  const dtPolyRef* path, const int pathSize,
164  float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
165  int* straightPathCount, const int maxStraightPath) const;
166 
174 
182  dtStatus initSlicedFindPath(dtPolyRef startRef, dtPolyRef endRef,
183  const float* startPos, const float* endPos,
184  const dtQueryFilter* filter);
185 
190  dtStatus updateSlicedFindPath(const int maxIter, int* doneIters);
191 
198  dtStatus finalizeSlicedFindPath(dtPolyRef* path, int* pathCount, const int maxPath);
199 
209  dtStatus finalizeSlicedFindPathPartial(const dtPolyRef* existing, const int existingSize,
210  dtPolyRef* path, int* pathCount, const int maxPath);
211 
215 
228  dtStatus findPolysAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
229  const dtQueryFilter* filter,
230  dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
231  int* resultCount, const int maxResult) const;
232 
246  dtStatus findPolysAroundShape(dtPolyRef startRef, const float* verts, const int nverts,
247  const dtQueryFilter* filter,
248  dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
249  int* resultCount, const int maxResult) const;
250 
254 
262  dtStatus findNearestPoly(const float* center, const float* extents,
263  const dtQueryFilter* filter,
264  dtPolyRef* nearestRef, float* nearestPt) const;
265 
274  dtStatus queryPolygons(const float* center, const float* extents,
275  const dtQueryFilter* filter,
276  dtPolyRef* polys, int* polyCount, const int maxPolys) const;
277 
289  dtStatus findLocalNeighbourhood(dtPolyRef startRef, const float* centerPos, const float radius,
290  const dtQueryFilter* filter,
291  dtPolyRef* resultRef, dtPolyRef* resultParent,
292  int* resultCount, const int maxResult) const;
293 
304  dtStatus moveAlongSurface(dtPolyRef startRef, const float* startPos, const float* endPos,
305  const dtQueryFilter* filter,
306  float* resultPos, dtPolyRef* visited, int* visitedCount, const int maxVisitedSize) const;
307 
321  dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
322  const dtQueryFilter* filter,
323  float* t, float* hitNormal, dtPolyRef* path, int* pathCount, const int maxPath) const;
324 
335  dtStatus findDistanceToWall(dtPolyRef startRef, const float* centerPos, const float maxRadius,
336  const dtQueryFilter* filter,
337  float* hitDist, float* hitPos, float* hitNormal) const;
338 
348  dtStatus getPolyWallSegments(dtPolyRef ref, const dtQueryFilter* filter,
349  float* segmentVerts, dtPolyRef* segmentRefs, int* segmentCount,
350  const int maxSegments) const;
351 
359  dtStatus findRandomPoint(const dtQueryFilter* filter, float (*frand)(),
360  dtPolyRef* randomRef, float* randomPt) const;
361 
372  dtStatus findRandomPointAroundCircle(dtPolyRef startRef, const float* centerPos, const float maxRadius,
373  const dtQueryFilter* filter, float (*frand)(),
374  dtPolyRef* randomRef, float* randomPt) const;
375 
381  dtStatus closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest) const;
382 
389  dtStatus closestPointOnPolyBoundary(dtPolyRef ref, const float* pos, float* closest) const;
390 
396  dtStatus getPolyHeight(dtPolyRef ref, const float* pos, float* height) const;
397 
401 
405  bool isValidPolyRef(dtPolyRef ref, const dtQueryFilter* filter) const;
406 
410  bool isInClosedList(dtPolyRef ref) const;
411 
414  class dtNodePool* getNodePool() const { return m_nodePool; }
415 
418  const dtNavMesh* getAttachedNavMesh() const { return m_nav; }
419 
421 
422 private:
423 
425  dtMeshTile* getNeighbourTileAt(int x, int y, int side) const;
426 
428  int queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax, const dtQueryFilter* filter,
429  dtPolyRef* polys, const int maxPolys) const;
431  dtPolyRef findNearestPolyInTile(const dtMeshTile* tile, const float* center, const float* extents,
432  const dtQueryFilter* filter, float* nearestPt) const;
434  void closestPointOnPolyInTile(const dtMeshTile* tile, const dtPoly* poly, const float* pos, float* closest) const;
435 
437  dtStatus getPortalPoints(dtPolyRef from, dtPolyRef to, float* left, float* right,
438  unsigned char& fromType, unsigned char& toType) const;
439  dtStatus getPortalPoints(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
440  dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
441  float* left, float* right) const;
442 
444  dtStatus getEdgeMidPoint(dtPolyRef from, dtPolyRef to, float* mid) const;
445  dtStatus getEdgeMidPoint(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
446  dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
447  float* mid) const;
448 
449  const dtNavMesh* m_nav;
450 
451  struct dtQueryData
452  {
453  dtStatus status;
454  struct dtNode* lastBestNode;
455  float lastBestNodeCost;
456  dtPolyRef startRef, endRef;
457  float startPos[3], endPos[3];
458  const dtQueryFilter* filter;
459  };
460  dtQueryData m_query;
461 
462  class dtNodePool* m_tinyNodePool;
463  class dtNodePool* m_nodePool;
464  class dtNodeQueue* m_openList;
465 };
466 
471 
476 
477 #endif // DETOURNAVMESHQUERY_H
void setIncludeFlags(const unsigned short flags)
Sets the include flags for the filter.
unsigned int dtPolyRef
A handle to a polygon within a navigation mesh tile.
Definition: DetourNavMesh.h:30
const dtNavMesh * getAttachedNavMesh() const
Gets the navigation mesh the query object is using.
Defines polygon filtering and traversal costs for navigation mesh query operations.
unsigned int dtStatus
Definition: DetourStatus.h:22
A navigation mesh based on tiles of convex polygons.
float getCost(const float *pa, const float *pb, const dtPolyRef prevRef, const dtMeshTile *prevTile, const dtPoly *prevPoly, const dtPolyRef curRef, const dtMeshTile *curTile, const dtPoly *curPoly, const dtPolyRef nextRef, const dtMeshTile *nextTile, const dtPoly *nextPoly) const
Returns cost to move from the beginning to the end of a line segment that is fully contained within a...
static const int DT_MAX_AREAS
The maximum number of user defined area ids.
Definition: DetourNavMesh.h:72
unsigned short getIncludeFlags() const
Returns the include flags for the filter. Any polygons that include one or more of these flags will b...
Defines a polyogn within a dtMeshTile object.
unsigned short getExcludeFlags() const
Returns the exclude flags for the filter. Any polygons that include one ore more of these flags will ...
float getAreaCost(const int i) const
Returns the traversal cost of the area.
class dtNodePool * getNodePool() const
Gets the node pool.
dtNavMeshQuery * dtAllocNavMeshQuery()
Allocates a query object using the Detour allocator.
void setAreaCost(const int i, const float cost)
Sets the traversal cost of the area.
void dtFreeNavMeshQuery(dtNavMeshQuery *query)
Frees the specified query object using the Detour allocator.
void setExcludeFlags(const unsigned short flags)
Sets the exclude flags for the filter.
Defines a navigation mesh tile.
Provides the ability to perform pathfinding related queries against a navigation mesh.
bool passFilter(const dtPolyRef ref, const dtMeshTile *tile, const dtPoly *poly) const
Returns true if the polygon can be visited. (I.e. Is traversable.)