Recast Navigation
Navigation-mesh Toolset for Games
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 
44 #ifdef DT_VIRTUAL_QUERYFILTER
45  virtual ~dtQueryFilter() { }
46 #endif
47 
52 #ifdef DT_VIRTUAL_QUERYFILTER
53  virtual bool passFilter(const dtPolyRef ref,
54  const dtMeshTile* tile,
55  const dtPoly* poly) const;
56 #else
57  bool passFilter(const dtPolyRef ref,
58  const dtMeshTile* tile,
59  const dtPoly* poly) const;
60 #endif
61 
75 #ifdef DT_VIRTUAL_QUERYFILTER
76  virtual float getCost(const float* pa, const float* pb,
77  const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
78  const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
79  const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
80 #else
81  float getCost(const float* pa, const float* pb,
82  const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
83  const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
84  const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
85 #endif
86 
89 
93  inline float getAreaCost(const int i) const { return m_areaCost[i]; }
94 
98  inline void setAreaCost(const int i, const float cost) { m_areaCost[i] = cost; }
99 
103  inline unsigned short getIncludeFlags() const { return m_includeFlags; }
104 
107  inline void setIncludeFlags(const unsigned short flags) { m_includeFlags = flags; }
108 
112  inline unsigned short getExcludeFlags() const { return m_excludeFlags; }
113 
116  inline void setExcludeFlags(const unsigned short flags) { m_excludeFlags = flags; }
117 
119 
120 };
121 
126 {
128  float t;
129 
131  float hitNormal[3];
132 
135 
138 
141 
143  int maxPath;
144 
146  float pathCost;
147 };
148 
153 {
154 public:
155  virtual ~dtPolyQuery();
156 
159  virtual void process(const dtMeshTile* tile, dtPoly** polys, dtPolyRef* refs, int count) = 0;
160 };
161 
166 {
167 public:
168  dtNavMeshQuery();
169  ~dtNavMeshQuery();
170 
175  dtStatus init(const dtNavMesh* nav, const int maxNodes);
176 
179 
190  dtStatus findPath(dtPolyRef startRef, dtPolyRef endRef,
191  const float* startPos, const float* endPos,
192  const dtQueryFilter* filter,
193  dtPolyRef* path, int* pathCount, const int maxPath) const;
194 
207  dtStatus findStraightPath(const float* startPos, const float* endPos,
208  const dtPolyRef* path, const int pathSize,
209  float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
210  int* straightPathCount, const int maxStraightPath, const int options = 0) const;
211 
219 
229  const float* startPos, const float* endPos,
230  const dtQueryFilter* filter, const unsigned int options = 0);
231 
236  dtStatus updateSlicedFindPath(const int maxIter, int* doneIters);
237 
244  dtStatus finalizeSlicedFindPath(dtPolyRef* path, int* pathCount, const int maxPath);
245 
255  dtStatus finalizeSlicedFindPathPartial(const dtPolyRef* existing, const int existingSize,
256  dtPolyRef* path, int* pathCount, const int maxPath);
257 
261 
274  dtStatus findPolysAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
275  const dtQueryFilter* filter,
276  dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
277  int* resultCount, const int maxResult) const;
278 
292  dtStatus findPolysAroundShape(dtPolyRef startRef, const float* verts, const int nverts,
293  const dtQueryFilter* filter,
294  dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
295  int* resultCount, const int maxResult) const;
296 
309  dtStatus getPathFromDijkstraSearch(dtPolyRef endRef, dtPolyRef* path, int* pathCount, int maxPath) const;
310 
314 
324  dtStatus findNearestPoly(const float* center, const float* halfExtents,
325  const dtQueryFilter* filter,
326  dtPolyRef* nearestRef, float* nearestPt) const;
327 
338  dtStatus findNearestPoly(const float* center, const float* halfExtents,
339  const dtQueryFilter* filter,
340  dtPolyRef* nearestRef, float* nearestPt, bool* isOverPoly) const;
341 
350  dtStatus queryPolygons(const float* center, const float* halfExtents,
351  const dtQueryFilter* filter,
352  dtPolyRef* polys, int* polyCount, const int maxPolys) const;
353 
359  dtStatus queryPolygons(const float* center, const float* halfExtents,
360  const dtQueryFilter* filter, dtPolyQuery* query) const;
361 
373  dtStatus findLocalNeighbourhood(dtPolyRef startRef, const float* centerPos, const float radius,
374  const dtQueryFilter* filter,
375  dtPolyRef* resultRef, dtPolyRef* resultParent,
376  int* resultCount, const int maxResult) const;
377 
388  dtStatus moveAlongSurface(dtPolyRef startRef, const float* startPos, const float* endPos,
389  const dtQueryFilter* filter,
390  float* resultPos, dtPolyRef* visited, int* visitedCount, const int maxVisitedSize) const;
391 
406  dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
407  const dtQueryFilter* filter,
408  float* t, float* hitNormal, dtPolyRef* path, int* pathCount, const int maxPath) const;
409 
421  dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
422  const dtQueryFilter* filter, const unsigned int options,
423  dtRaycastHit* hit, dtPolyRef prevRef = 0) const;
424 
425 
436  dtStatus findDistanceToWall(dtPolyRef startRef, const float* centerPos, const float maxRadius,
437  const dtQueryFilter* filter,
438  float* hitDist, float* hitPos, float* hitNormal) const;
439 
450  float* segmentVerts, dtPolyRef* segmentRefs, int* segmentCount,
451  const int maxSegments) const;
452 
460  dtStatus findRandomPoint(const dtQueryFilter* filter, float (*frand)(),
461  dtPolyRef* randomRef, float* randomPt) const;
462 
474  dtStatus findRandomPointAroundCircle(dtPolyRef startRef, const float* centerPos, const float maxRadius,
475  const dtQueryFilter* filter, float (*frand)(),
476  dtPolyRef* randomRef, float* randomPt) const;
477 
484  dtStatus closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest, bool* posOverPoly) const;
485 
492  dtStatus closestPointOnPolyBoundary(dtPolyRef ref, const float* pos, float* closest) const;
493 
499  dtStatus getPolyHeight(dtPolyRef ref, const float* pos, float* height) const;
500 
504 
508  bool isValidPolyRef(dtPolyRef ref, const dtQueryFilter* filter) const;
509 
513  bool isInClosedList(dtPolyRef ref) const;
514 
517  class dtNodePool* getNodePool() const { return m_nodePool; }
518 
521  const dtNavMesh* getAttachedNavMesh() const { return m_nav; }
522 
524 
525 private:
526  // Explicitly disabled copy constructor and copy assignment operator
528  dtNavMeshQuery& operator=(const dtNavMeshQuery&);
529 
531  void queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax,
532  const dtQueryFilter* filter, dtPolyQuery* query) const;
533 
535  dtStatus getPortalPoints(dtPolyRef from, dtPolyRef to, float* left, float* right,
536  unsigned char& fromType, unsigned char& toType) const;
537  dtStatus getPortalPoints(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
538  dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
539  float* left, float* right) const;
540 
542  dtStatus getEdgeMidPoint(dtPolyRef from, dtPolyRef to, float* mid) const;
543  dtStatus getEdgeMidPoint(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
544  dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
545  float* mid) const;
546 
547  // Appends vertex to a straight path
548  dtStatus appendVertex(const float* pos, const unsigned char flags, const dtPolyRef ref,
549  float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
550  int* straightPathCount, const int maxStraightPath) const;
551 
552  // Appends intermediate portal points to a straight path.
553  dtStatus appendPortals(const int startIdx, const int endIdx, const float* endPos, const dtPolyRef* path,
554  float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
555  int* straightPathCount, const int maxStraightPath, const int options) const;
556 
557  // Gets the path leading to the specified end node.
558  dtStatus getPathToNode(struct dtNode* endNode, dtPolyRef* path, int* pathCount, int maxPath) const;
559 
560  const dtNavMesh* m_nav;
561 
562  struct dtQueryData
563  {
564  dtStatus status;
565  struct dtNode* lastBestNode;
566  float lastBestNodeCost;
567  dtPolyRef startRef, endRef;
568  float startPos[3], endPos[3];
569  const dtQueryFilter* filter;
570  unsigned int options;
571  float raycastLimitSqr;
572  };
573  dtQueryData m_query;
574 
575  class dtNodePool* m_tinyNodePool;
576  class dtNodePool* m_nodePool;
577  class dtNodeQueue* m_openList;
578 };
579 
584 
589 
590 #endif // DETOURNAVMESHQUERY_H
unsigned int dtStatus
Definition: DetourStatus.h:22
static bool appendVertex(dtTempContour &cont, const int x, const int y, const int z, const int r)
Definition: DetourTileCacheBuilder.cpp:394
bool left(const unsigned char *a, const unsigned char *b, const unsigned char *c)
Definition: DetourTileCacheBuilder.cpp:1098
static float frand()
Definition: NavMeshTesterTool.cpp:49
Provides the ability to perform pathfinding related queries against a navigation mesh.
Definition: DetourNavMeshQuery.h:166
dtStatus finalizeSlicedFindPathPartial(const dtPolyRef *existing, const int existingSize, dtPolyRef *path, int *pathCount, const int maxPath)
Finalizes and returns the results of an incomplete sliced path query, returning the path to the furth...
Definition: DetourNavMeshQuery.cpp:1587
const dtNavMesh * getAttachedNavMesh() const
Gets the navigation mesh the query object is using.
Definition: DetourNavMeshQuery.h:521
dtStatus queryPolygons(const float *center, const float *halfExtents, const dtQueryFilter *filter, dtPolyRef *polys, int *polyCount, const int maxPolys) const
Finds polygons that overlap the search box.
Definition: DetourNavMeshQuery.cpp:899
dtStatus closestPointOnPoly(dtPolyRef ref, const float *pos, float *closest, bool *posOverPoly) const
Finds the closest point on the specified polygon.
Definition: DetourNavMeshQuery.cpp:511
dtStatus findPolysAroundCircle(dtPolyRef startRef, const float *centerPos, const float radius, const dtQueryFilter *filter, dtPolyRef *resultRef, dtPolyRef *resultParent, float *resultCost, int *resultCount, const int maxResult) const
Finds the polygons along the navigation graph that touch the specified circle.
Definition: DetourNavMeshQuery.cpp:2720
dtStatus findDistanceToWall(dtPolyRef startRef, const float *centerPos, const float maxRadius, const dtQueryFilter *filter, float *hitDist, float *hitPos, float *hitNormal) const
Finds the distance from the specified position to the nearest polygon wall.
Definition: DetourNavMeshQuery.cpp:3470
~dtNavMeshQuery()
Definition: DetourNavMeshQuery.cpp:152
dtStatus getPolyWallSegments(dtPolyRef ref, const dtQueryFilter *filter, float *segmentVerts, dtPolyRef *segmentRefs, int *segmentCount, const int maxSegments) const
Returns the segments for the specified polygon, optionally including portals.
Definition: DetourNavMeshQuery.cpp:3314
dtStatus findNearestPoly(const float *center, const float *halfExtents, const dtQueryFilter *filter, dtPolyRef *nearestRef, float *nearestPt) const
Finds the polygon nearest to the specified center point.
Definition: DetourNavMeshQuery.cpp:693
class dtNodePool * getNodePool() const
Gets the node pool.
Definition: DetourNavMeshQuery.h:517
dtStatus raycast(dtPolyRef startRef, const float *startPos, const float *endPos, const dtQueryFilter *filter, float *t, float *hitNormal, dtPolyRef *path, int *pathCount, const int maxPath) const
Casts a 'walkability' ray along the surface of the navigation mesh from the start position toward the...
Definition: DetourNavMeshQuery.cpp:2408
dtStatus moveAlongSurface(dtPolyRef startRef, const float *startPos, const float *endPos, const dtQueryFilter *filter, float *resultPos, dtPolyRef *visited, int *visitedCount, const int maxVisitedSize) const
Moves from the start to the end position constrained to the navigation mesh.
Definition: DetourNavMeshQuery.cpp:2044
dtStatus findLocalNeighbourhood(dtPolyRef startRef, const float *centerPos, const float radius, const dtQueryFilter *filter, dtPolyRef *resultRef, dtPolyRef *resultParent, int *resultCount, const int maxResult) const
Finds the non-overlapping navigation polygons in the local neighbourhood around the center position.
Definition: DetourNavMeshQuery.cpp:3092
dtStatus finalizeSlicedFindPath(dtPolyRef *path, int *pathCount, const int maxPath)
Finalizes and returns the results of a sliced path query.
Definition: DetourNavMeshQuery.cpp:1497
dtStatus findPath(dtPolyRef startRef, dtPolyRef endRef, const float *startPos, const float *endPos, const dtQueryFilter *filter, dtPolyRef *path, int *pathCount, const int maxPath) const
Finds a path from the start polygon to the end polygon.
Definition: DetourNavMeshQuery.cpp:973
dtStatus updateSlicedFindPath(const int maxIter, int *doneIters)
Updates an in-progress sliced path query.
Definition: DetourNavMeshQuery.cpp:1280
dtStatus findPolysAroundShape(dtPolyRef startRef, const float *verts, const int nverts, const dtQueryFilter *filter, dtPolyRef *resultRef, dtPolyRef *resultParent, float *resultCost, int *resultCount, const int maxResult) const
Finds the polygons along the naviation graph that touch the specified convex polygon.
Definition: DetourNavMeshQuery.cpp:2894
dtStatus closestPointOnPolyBoundary(dtPolyRef ref, const float *pos, float *closest) const
Returns a point on the boundary closest to the source point if the source point is outside the polygo...
Definition: DetourNavMeshQuery.cpp:536
dtNavMeshQuery()
Definition: DetourNavMeshQuery.cpp:143
dtStatus findRandomPointAroundCircle(dtPolyRef startRef, const float *centerPos, const float maxRadius, const dtQueryFilter *filter, float(*frand)(), dtPolyRef *randomRef, float *randomPt) const
Returns random location on navmesh within the reach of specified location.
Definition: DetourNavMeshQuery.cpp:317
dtStatus findStraightPath(const float *startPos, const float *endPos, const dtPolyRef *path, const int pathSize, float *straightPath, unsigned char *straightPathFlags, dtPolyRef *straightPathRefs, int *straightPathCount, const int maxStraightPath, const int options=0) const
Finds the straight path from the start to the end position within the polygon corridor.
Definition: DetourNavMeshQuery.cpp:1793
bool isValidPolyRef(dtPolyRef ref, const dtQueryFilter *filter) const
Returns true if the polygon reference is valid and passes the filter restrictions.
Definition: DetourNavMeshQuery.cpp:3657
dtStatus init(const dtNavMesh *nav, const int maxNodes)
Initializes the query object.
Definition: DetourNavMeshQuery.cpp:171
bool isInClosedList(dtPolyRef ref) const
Returns true if the polygon reference is in the closed list.
Definition: DetourNavMeshQuery.cpp:3676
dtStatus initSlicedFindPath(dtPolyRef startRef, dtPolyRef endRef, const float *startPos, const float *endPos, const dtQueryFilter *filter, const unsigned int options=0)
Initializes a sliced path query.
Definition: DetourNavMeshQuery.cpp:1216
dtStatus getPathFromDijkstraSearch(dtPolyRef endRef, dtPolyRef *path, int *pathCount, int maxPath) const
Gets a path from the explored nodes in the previous search.
Definition: DetourNavMeshQuery.cpp:3055
dtStatus getPolyHeight(dtPolyRef ref, const float *pos, float *height) const
Gets the height of the polygon at the provided position using the height detail.
Definition: DetourNavMeshQuery.cpp:591
dtStatus findRandomPoint(const dtQueryFilter *filter, float(*frand)(), dtPolyRef *randomRef, float *randomPt) const
Returns random location on navmesh.
Definition: DetourNavMeshQuery.cpp:226
A navigation mesh based on tiles of convex polygons.
Definition: DetourNavMesh.h:339
Definition: DetourNode.h:50
Definition: DetourNode.h:109
Provides custom polygon query behavior.
Definition: DetourNavMeshQuery.h:153
virtual void process(const dtMeshTile *tile, dtPoly **polys, dtPolyRef *refs, int count)=0
Called for each batch of unique polygons touched by the search area in dtNavMeshQuery::queryPolygons.
virtual ~dtPolyQuery()
Definition: DetourNavMeshQuery.cpp:120
Defines polygon filtering and traversal costs for navigation mesh query operations.
Definition: DetourNavMeshQuery.h:36
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...
Definition: DetourNavMeshQuery.cpp:94
void setExcludeFlags(const unsigned short flags)
Sets the exclude flags for the filter.
Definition: DetourNavMeshQuery.h:116
float getAreaCost(const int i) const
Returns the traversal cost of the area.
Definition: DetourNavMeshQuery.h:93
void setAreaCost(const int i, const float cost)
Sets the traversal cost of the area.
Definition: DetourNavMeshQuery.h:98
unsigned short getIncludeFlags() const
Returns the include flags for the filter.
Definition: DetourNavMeshQuery.h:103
bool passFilter(const dtPolyRef ref, const dtMeshTile *tile, const dtPoly *poly) const
Returns true if the polygon can be visited.
Definition: DetourNavMeshQuery.cpp:87
void setIncludeFlags(const unsigned short flags)
Sets the include flags for the filter.
Definition: DetourNavMeshQuery.h:107
unsigned short getExcludeFlags() const
Returns the exclude flags for the filter.
Definition: DetourNavMeshQuery.h:112
dtQueryFilter()
Definition: DetourNavMeshQuery.cpp:63
void dtFreeNavMeshQuery(dtNavMeshQuery *query)
Frees the specified query object using the Detour allocator.
Definition: DetourNavMeshQuery.cpp:113
static const int DT_MAX_AREAS
The maximum number of user defined area ids.
Definition: DetourNavMesh.h:95
unsigned int dtPolyRef
A handle to a polygon within a navigation mesh tile.
Definition: DetourNavMesh.h:48
dtNavMeshQuery * dtAllocNavMeshQuery()
Allocates a query object using the Detour allocator.
Definition: DetourNavMeshQuery.cpp:106
Defines a navigation mesh tile.
Definition: DetourNavMesh.h:282
Definition: DetourNode.h:37
Defines a polygon within a dtMeshTile object.
Definition: DetourNavMesh.h:156
Provides information about raycast hit filled by dtNavMeshQuery::raycast.
Definition: DetourNavMeshQuery.h:126
int hitEdgeIndex
The index of the edge on the final polygon where the wall was hit.
Definition: DetourNavMeshQuery.h:134
float hitNormal[3]
hitNormal The normal of the nearest wall hit. [(x, y, z)]
Definition: DetourNavMeshQuery.h:131
int maxPath
The maximum number of polygons the path array can hold.
Definition: DetourNavMeshQuery.h:143
float t
The hit parameter. (FLT_MAX if no wall hit.)
Definition: DetourNavMeshQuery.h:128
float pathCost
The cost of the path until hit.
Definition: DetourNavMeshQuery.h:146
int pathCount
The number of visited polygons. [opt].
Definition: DetourNavMeshQuery.h:140
dtPolyRef * path
Pointer to an array of reference ids of the visited polygons. [opt].
Definition: DetourNavMeshQuery.h:137