Recast Navigation  1.0.35
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  enum { MAX_AREA_COSTS = 64 };
38  navAreaMask m_costMask[MAX_AREA_COSTS];
39  float m_areaCost[MAX_AREA_COSTS];
40  navAreaMask m_includeFlags;
41  navAreaMask m_excludeFlags;
42 
43 public:
44  dtQueryFilter();
45 
46 #ifdef DT_VIRTUAL_QUERYFILTER
47  virtual ~dtQueryFilter() { }
48 #endif
49 
54 #ifdef DT_VIRTUAL_QUERYFILTER
55  virtual bool passFilter(const dtPolyRef ref,
56  const dtMeshTile* tile,
57  const dtPoly* poly) const;
58 #else
59  bool passFilter(const dtPolyRef ref,
60  const dtMeshTile* tile,
61  const dtPoly* poly) const;
62 #endif
63 
77 #ifdef DT_VIRTUAL_QUERYFILTER
78  virtual float getCost(const float* pa, const float* pb,
79  const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
80  const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
81  const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
82 #else
83  float getCost(const float* pa, const float* pb,
84  const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
85  const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
86  const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
87 #endif
88 
91 
95  inline float getAreaCost( navAreaMask areaMask ) const
96  {
97  for ( int i = 0; i < MAX_AREA_COSTS; ++i )
98  {
99  if ( ( m_costMask[ i ] & areaMask ) == areaMask )
100  {
101  return m_areaCost[ i ];
102  }
103  }
104  return 1.0f;
105  }
106 
110  inline void setAreaCost( navAreaMask areaMask, const float cost )
111  {
112  for ( int i = 0; i < MAX_AREA_COSTS; ++i )
113  {
114  if ( ( m_costMask[ i ] & areaMask ) == areaMask )
115  {
116  m_areaCost[ i ] = cost;
117  return;
118  }
119  }
120  for ( int i = 0; i < MAX_AREA_COSTS; ++i )
121  {
122  if ( m_costMask[ i ] == 0 )
123  {
124  m_costMask[ i ] = areaMask;
125  m_areaCost[ i ] = cost;
126  return;
127  }
128  }
129  }
130 
134  inline navAreaMask getIncludeFlags() const { return m_includeFlags; }
135 
138  inline void setIncludeFlags(const navAreaMask flags) { m_includeFlags = flags; }
139 
143  inline navAreaMask getExcludeFlags() const { return m_excludeFlags; }
144 
147  inline void setExcludeFlags(const navAreaMask flags) { m_excludeFlags = flags; }
148 
150 
151 };
152 
153 
154 
159 {
161  float t;
162 
164  float hitNormal[3];
165 
168 
171 
174 
176  int maxPath;
177 
179  float pathCost;
180 };
181 
183 {
184  float mDest[ 3 ];
186  float mNavDist;
187  float mThreat;
188 
189  dtMultiPathGoal();
190 };
191 
196 {
197 public:
198  dtNavMeshQuery();
199  ~dtNavMeshQuery();
200 
205  dtStatus init(const dtNavMesh* nav, const int maxNodes);
206 
208  // /@{
209 
220  dtStatus findPath(dtPolyRef startRef, dtPolyRef endRef,
221  const float* startPos, const float* endPos,
222  const dtQueryFilter* filter,
223  dtPolyRef* path, int* pathCount, const int maxPath) const;
224 
237  dtStatus findStraightPath(const float* startPos, const float* endPos,
238  const dtPolyRef* path, const int pathSize,
239  float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
240  int* straightPathCount, const int maxStraightPath, const int options = 0) const;
241 
249 
259  const float* startPos, const float* endPos,
260  const dtQueryFilter* filter, const unsigned int options = 0);
261 
266  dtStatus updateSlicedFindPath(const int maxIter, int* doneIters);
267 
274  dtStatus finalizeSlicedFindPath(dtPolyRef* path, int* pathCount, const int maxPath);
275 
285  dtStatus finalizeSlicedFindPathPartial(const dtPolyRef* existing, const int existingSize,
286  dtPolyRef* path, int* pathCount, const int maxPath);
287 
291 
304  dtStatus findPolysAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
305  const dtQueryFilter* filter,
306  dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
307  int* resultCount, const int maxResult) const;
308 
309  dtStatus findMultiPath( dtPolyRef startRef, const float* startPos, const dtQueryFilter* filter, dtMultiPathGoal* goals, int numGoals ) const;
310 
324  dtStatus findPolysAroundShape(dtPolyRef startRef, const float* verts, const int nverts,
325  const dtQueryFilter* filter,
326  dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
327  int* resultCount, const int maxResult) const;
328 
332 
340  dtStatus findNearestPoly(const float* center, const float* extents,
341  const dtQueryFilter* filter,
342  dtPolyRef* nearestRef, float* nearestPt) const;
343 
352  dtStatus queryPolygons(const float* center, const float* extents,
353  const dtQueryFilter* filter,
354  dtPolyRef* polys, int* polyCount, const int maxPolys) const;
355 
367  dtStatus findLocalNeighbourhood(dtPolyRef startRef, const float* centerPos, const float radius,
368  const dtQueryFilter* filter,
369  dtPolyRef* resultRef, dtPolyRef* resultParent,
370  int* resultCount, const int maxResult) const;
371 
382  dtStatus moveAlongSurface(dtPolyRef startRef, const float* startPos, const float* endPos,
383  const dtQueryFilter* filter,
384  float* resultPos, dtPolyRef* visited, int* visitedCount, const int maxVisitedSize) const;
385 
400  dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
401  const dtQueryFilter* filter,
402  float* t, float* hitNormal, dtPolyRef* path, int* pathCount, const int maxPath) const;
403 
415  dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
416  const dtQueryFilter* filter, const unsigned int options,
417  dtRaycastHit* hit, dtPolyRef prevRef = 0) const;
418 
419 
430  dtStatus findDistanceToWall(dtPolyRef startRef, const float* centerPos, const float maxRadius,
431  const dtQueryFilter* filter,
432  float* hitDist, float* hitPos, float* hitNormal) const;
433 
444  float* segmentVerts, dtPolyRef* segmentRefs, int* segmentCount,
445  const int maxSegments) const;
446 
454  dtStatus findRandomPoint(const dtQueryFilter* filter, float (*frand)(),
455  dtPolyRef* randomRef, float* randomPt) const;
456 
457  dtStatus findRandomPointInPoly( const dtPolyRef ref, float( *frand )( ), float* randomPt ) const;
458 
470  dtStatus findRandomPointAroundCircle(dtPolyRef startRef, const float* centerPos, const float maxRadius,
471  const dtQueryFilter* filter, float (*frand)(),
472  dtPolyRef* randomRef, float* randomPt) const;
473 
480  dtStatus closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest, bool* posOverPoly) const;
481 
488  dtStatus closestPointOnPolyBoundary(dtPolyRef ref, const float* pos, float* closest) const;
489 
495  dtStatus getPolyHeight(dtPolyRef ref, const float* pos, float* height) const;
496 
500 
504  bool isValidPolyRef(dtPolyRef ref, const dtQueryFilter* filter) const;
505 
509  bool isInClosedList(dtPolyRef ref) const;
510 
513  class dtNodePool* getNodePool() const { return m_nodePool; }
514 
517  const dtNavMesh* getAttachedNavMesh() const { return m_nav; }
518 
520 
521 private:
522 
524  dtMeshTile* getNeighbourTileAt(int x, int y, int side) const;
525 
527  int queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax, const dtQueryFilter* filter,
528  dtPolyRef* polys, const int maxPolys) const;
529 
531  dtStatus getPortalPoints(dtPolyRef from, dtPolyRef to, float* left, float* right,
532  unsigned char& fromType, unsigned char& toType) const;
533  dtStatus getPortalPoints(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
534  dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
535  float* left, float* right) const;
536 
538  dtStatus getEdgeMidPoint(dtPolyRef from, dtPolyRef to, float* mid) const;
539  dtStatus getEdgeMidPoint(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
540  dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
541  float* mid) const;
542 
543  // Appends vertex to a straight path
544  dtStatus appendVertex(const float* pos, const unsigned char flags, const dtPolyRef ref,
545  float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
546  int* straightPathCount, const int maxStraightPath) const;
547 
548  // Appends intermediate portal points to a straight path.
549  dtStatus appendPortals(const int startIdx, const int endIdx, const float* endPos, const dtPolyRef* path,
550  float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
551  int* straightPathCount, const int maxStraightPath, const int options) const;
552 
553  const dtNavMesh* m_nav;
554 
555  struct dtQueryData
556  {
557  dtStatus status;
558  struct dtNode* lastBestNode;
559  float lastBestNodeCost;
560  dtPolyRef startRef, endRef;
561  float startPos[3], endPos[3];
562  const dtQueryFilter* filter;
563  unsigned int options;
564  float raycastLimitSqr;
565  };
566  dtQueryData m_query;
567 
568  class dtNodePool* m_tinyNodePool;
569  class dtNodePool* m_nodePool;
570  class dtNodeQueue* m_openList;
571 };
572 
577 
582 
583 #endif // DETOURNAVMESHQUERY_H
dtStatus queryPolygons(const float *center, const float *extents, const dtQueryFilter *filter, dtPolyRef *polys, int *polyCount, const int maxPolys) const
Finds polygons that overlap the search box.
Definition: DetourNavMeshQuery.cpp:913
bool isInClosedList(dtPolyRef ref) const
Returns true if the polygon reference is in the closed list.
Definition: DetourNavMeshQuery.cpp:3793
float t
The hit parameter. (FLT_MAX if no wall hit.)
Definition: DetourNavMeshQuery.h:161
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:355
unsigned int dtPolyRef
A handle to a polygon within a navigation mesh tile.
Definition: DetourNavMesh.h:44
Definition: DetourNavMeshQuery.h:182
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:2689
float pathCost
The cost of the path until hit.
Definition: DetourNavMeshQuery.h:179
const dtNavMesh * getAttachedNavMesh() const
Gets the navigation mesh the query object is using.
Definition: DetourNavMeshQuery.h:517
Definition: DetourNode.h:35
dtQueryFilter()
Definition: DetourNavMeshQuery.cpp:63
dtStatus findMultiPath(dtPolyRef startRef, const float *startPos, const dtQueryFilter *filter, dtMultiPathGoal *goals, int numGoals) const
Definition: DetourNavMeshQuery.cpp:2849
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:1787
bool left(const float *a, const float *b, const float *c)
Definition: DetourCommon.cpp:389
dtMultiPathGoal()
Definition: DetourNavMeshQuery.cpp:2842
void setExcludeFlags(const navAreaMask flags)
Sets the exclude flags for the filter.
Definition: DetourNavMeshQuery.h:147
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:2384
float mThreat
Definition: DetourNavMeshQuery.h:187
void setAreaCost(navAreaMask areaMask, const float cost)
Sets the traversal cost of the area.
Definition: DetourNavMeshQuery.h:110
static bool appendVertex(dtTempContour &cont, const int x, const int y, const int z, const int r)
Definition: DetourTileCacheBuilder.cpp:386
Provides information about raycast hit filled by dtNavMeshQuery::raycast.
Definition: DetourNavMeshQuery.h:158
dtPolyRef mDestPoly
Definition: DetourNavMeshQuery.h:185
void setIncludeFlags(const navAreaMask flags)
Sets the include flags for the filter.
Definition: DetourNavMeshQuery.h:138
Defines polygon filtering and traversal costs for navigation mesh query operations.
Definition: DetourNavMeshQuery.h:35
unsigned int dtStatus
Definition: DetourStatus.h:22
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:644
Definition: DetourNode.h:108
A navigation mesh based on tiles of convex polygons.
Definition: DetourNavMesh.h:328
dtStatus findRandomPointInPoly(const dtPolyRef ref, float(*frand)(), float *randomPt) const
Definition: DetourNavMeshQuery.cpp:320
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:964
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:103
dtPolyRef * path
Pointer to an array of reference ids of the visited polygons. [opt].
Definition: DetourNavMeshQuery.h:170
dtStatus finalizeSlicedFindPath(dtPolyRef *path, int *pathCount, const int maxPath)
Finalizes and returns the results of a sliced path query.
Definition: DetourNavMeshQuery.cpp:1505
dtNavMeshQuery()
Definition: DetourNavMeshQuery.cpp:147
navAreaMask getIncludeFlags() const
Returns the include flags for the filter.
Definition: DetourNavMeshQuery.h:134
int maxPath
The maximum number of polygons the path array can hold.
Definition: DetourNavMeshQuery.h:176
Defines a polyogn within a dtMeshTile object.
Definition: DetourNavMesh.h:153
int pathCount
The number of visited polygons. [opt].
Definition: DetourNavMeshQuery.h:173
dtStatus initSlicedFindPath(dtPolyRef startRef, dtPolyRef endRef, const float *startPos, const float *endPos, const dtQueryFilter *filter, const unsigned int options=0)
Intializes a sliced path query.
Definition: DetourNavMeshQuery.cpp:1227
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:1589
float hitNormal[3]
hitNormal The normal of the nearest wall hit. [(x, y, z)]
Definition: DetourNavMeshQuery.h:164
float mDest[3]
Definition: DetourNavMeshQuery.h:184
navAreaMask getExcludeFlags() const
Returns the exclude flags for the filter.
Definition: DetourNavMeshQuery.h:143
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:3227
~dtNavMeshQuery()
Definition: DetourNavMeshQuery.cpp:156
bool isValidPolyRef(dtPolyRef ref, const dtQueryFilter *filter) const
Returns true if the polygon reference is valid and passes the filter restrictions.
Definition: DetourNavMeshQuery.cpp:3774
int hitEdgeIndex
The index of the edge on the final polygon where the wall was hit.
Definition: DetourNavMeshQuery.h:167
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:696
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:3047
dtStatus findNearestPoly(const float *center, const float *extents, const dtQueryFilter *filter, dtPolyRef *nearestRef, float *nearestPt) const
Finds the polygon nearest to the specified center point.
Definition: DetourNavMeshQuery.cpp:753
class dtNodePool * getNodePool() const
Gets the node pool.
Definition: DetourNavMeshQuery.h:513
Definition: DetourNode.h:51
float mNavDist
Definition: DetourNavMeshQuery.h:186
dtNavMeshQuery * dtAllocNavMeshQuery()
Allocates a query object using the Detour allocator.
Definition: DetourNavMeshQuery.cpp:115
dtStatus updateSlicedFindPath(const int maxIter, int *doneIters)
Updates an in-progress sliced path query.
Definition: DetourNavMeshQuery.cpp:1288
dtStatus init(const dtNavMesh *nav, const int maxNodes)
Initializes the query object.
Definition: DetourNavMeshQuery.cpp:175
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:3592
dtStatus findRandomPoint(const dtQueryFilter *filter, float(*frand)(), dtPolyRef *randomRef, float *randomPt) const
Returns random location on navmesh.
Definition: DetourNavMeshQuery.cpp:228
void dtFreeNavMeshQuery(dtNavMeshQuery *query)
Frees the specified query object using the Detour allocator.
Definition: DetourNavMeshQuery.cpp:122
Defines a navigation mesh tile.
Definition: DetourNavMesh.h:282
float getAreaCost(navAreaMask areaMask) const
Returns the traversal cost of the area.
Definition: DetourNavMeshQuery.h:95
Provides the ability to perform pathfinding related queries against a navigation mesh.
Definition: DetourNavMeshQuery.h:195
bool passFilter(const dtPolyRef ref, const dtMeshTile *tile, const dtPoly *poly) const
Returns true if the polygon can be visited.
Definition: DetourNavMeshQuery.cpp:96
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:3442
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:2028
dtStatus closestPointOnPoly(dtPolyRef ref, const float *pos, float *closest, bool *posOverPoly) const
Finds the closest point on the specified polygon.
Definition: DetourNavMeshQuery.cpp:548