DetourNavMeshQuery.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. #ifndef DETOURNAVMESHQUERY_H
  19. #define DETOURNAVMESHQUERY_H
  20. #include "DetourNavMesh.h"
  21. #include "DetourStatus.h"
  22. // Define DT_VIRTUAL_QUERYFILTER if you wish to derive a custom filter from dtQueryFilter.
  23. // On certain platforms indirect or virtual function call is expensive. The default
  24. // setting is to use non-virtual functions, the actual implementations of the functions
  25. // are declared as inline for maximum speed.
  26. //#define DT_VIRTUAL_QUERYFILTER 1
  27. /// Defines polygon filtering and traversal costs for navigation mesh query operations.
  28. /// @ingroup detour
  29. class dtQueryFilter
  30. {
  31. float m_areaCost[DT_MAX_AREAS]; ///< Cost per area type. (Used by default implementation.)
  32. unsigned short m_includeFlags; ///< Flags for polygons that can be visited. (Used by default implementation.)
  33. unsigned short m_excludeFlags; ///< Flags for polygons that should not be visted. (Used by default implementation.)
  34. public:
  35. dtQueryFilter();
  36. #ifdef DT_VIRTUAL_QUERYFILTER
  37. virtual ~dtQueryFilter() { }
  38. #endif
  39. /// Returns true if the polygon can be visited. (I.e. Is traversable.)
  40. /// @param[in] ref The reference id of the polygon test.
  41. /// @param[in] tile The tile containing the polygon.
  42. /// @param[in] poly The polygon to test.
  43. #ifdef DT_VIRTUAL_QUERYFILTER
  44. virtual bool passFilter(const dtPolyRef ref,
  45. const dtMeshTile* tile,
  46. const dtPoly* poly) const;
  47. #else
  48. bool passFilter(const dtPolyRef ref,
  49. const dtMeshTile* tile,
  50. const dtPoly* poly) const;
  51. #endif
  52. /// Returns cost to move from the beginning to the end of a line segment
  53. /// that is fully contained within a polygon.
  54. /// @param[in] pa The start position on the edge of the previous and current polygon. [(x, y, z)]
  55. /// @param[in] pb The end position on the edge of the current and next polygon. [(x, y, z)]
  56. /// @param[in] prevRef The reference id of the previous polygon. [opt]
  57. /// @param[in] prevTile The tile containing the previous polygon. [opt]
  58. /// @param[in] prevPoly The previous polygon. [opt]
  59. /// @param[in] curRef The reference id of the current polygon.
  60. /// @param[in] curTile The tile containing the current polygon.
  61. /// @param[in] curPoly The current polygon.
  62. /// @param[in] nextRef The refernece id of the next polygon. [opt]
  63. /// @param[in] nextTile The tile containing the next polygon. [opt]
  64. /// @param[in] nextPoly The next polygon. [opt]
  65. #ifdef DT_VIRTUAL_QUERYFILTER
  66. virtual float getCost(const float* pa, const float* pb,
  67. const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
  68. const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
  69. const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
  70. #else
  71. float getCost(const float* pa, const float* pb,
  72. const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
  73. const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
  74. const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const;
  75. #endif
  76. /// @name Getters and setters for the default implementation data.
  77. ///@{
  78. /// Returns the traversal cost of the area.
  79. /// @param[in] i The id of the area.
  80. /// @returns The traversal cost of the area.
  81. inline float getAreaCost(const int i) const { return m_areaCost[i]; }
  82. /// Sets the traversal cost of the area.
  83. /// @param[in] i The id of the area.
  84. /// @param[in] cost The new cost of traversing the area.
  85. inline void setAreaCost(const int i, const float cost) { m_areaCost[i] = cost; }
  86. /// Returns the include flags for the filter.
  87. /// Any polygons that include one or more of these flags will be
  88. /// included in the operation.
  89. inline unsigned short getIncludeFlags() const { return m_includeFlags; }
  90. /// Sets the include flags for the filter.
  91. /// @param[in] flags The new flags.
  92. inline void setIncludeFlags(const unsigned short flags) { m_includeFlags = flags; }
  93. /// Returns the exclude flags for the filter.
  94. /// Any polygons that include one ore more of these flags will be
  95. /// excluded from the operation.
  96. inline unsigned short getExcludeFlags() const { return m_excludeFlags; }
  97. /// Sets the exclude flags for the filter.
  98. /// @param[in] flags The new flags.
  99. inline void setExcludeFlags(const unsigned short flags) { m_excludeFlags = flags; }
  100. ///@}
  101. };
  102. /// Provides information about raycast hit
  103. /// filled by dtNavMeshQuery::raycast
  104. /// @ingroup detour
  105. struct dtRaycastHit
  106. {
  107. /// The hit parameter. (FLT_MAX if no wall hit.)
  108. float t;
  109. /// hitNormal The normal of the nearest wall hit. [(x, y, z)]
  110. float hitNormal[3];
  111. /// The index of the edge on the final polygon where the wall was hit.
  112. int hitEdgeIndex;
  113. /// Pointer to an array of reference ids of the visited polygons. [opt]
  114. dtPolyRef* path;
  115. /// The number of visited polygons. [opt]
  116. int pathCount;
  117. /// The maximum number of polygons the @p path array can hold.
  118. int maxPath;
  119. /// The cost of the path until hit.
  120. float pathCost;
  121. };
  122. /// Provides custom polygon query behavior.
  123. /// Used by dtNavMeshQuery::queryPolygons.
  124. /// @ingroup detour
  125. class dtPolyQuery
  126. {
  127. public:
  128. virtual ~dtPolyQuery() { }
  129. /// Called for each batch of unique polygons touched by the search area in dtNavMeshQuery::queryPolygons.
  130. /// This can be called multiple times for a single query.
  131. virtual void process(const dtMeshTile* tile, dtPoly** polys, dtPolyRef* refs, int count) = 0;
  132. };
  133. /// Provides the ability to perform pathfinding related queries against
  134. /// a navigation mesh.
  135. /// @ingroup detour
  136. class dtNavMeshQuery
  137. {
  138. public:
  139. dtNavMeshQuery();
  140. ~dtNavMeshQuery();
  141. /// Initializes the query object.
  142. /// @param[in] nav Pointer to the dtNavMesh object to use for all queries.
  143. /// @param[in] maxNodes Maximum number of search nodes. [Limits: 0 < value <= 65535]
  144. /// @returns The status flags for the query.
  145. dtStatus init(const dtNavMesh* nav, const int maxNodes);
  146. /// @name Standard Pathfinding Functions
  147. // /@{
  148. /// Finds a path from the start polygon to the end polygon.
  149. /// @param[in] startRef The refrence id of the start polygon.
  150. /// @param[in] endRef The reference id of the end polygon.
  151. /// @param[in] startPos A position within the start polygon. [(x, y, z)]
  152. /// @param[in] endPos A position within the end polygon. [(x, y, z)]
  153. /// @param[in] filter The polygon filter to apply to the query.
  154. /// @param[out] path An ordered list of polygon references representing the path. (Start to end.)
  155. /// [(polyRef) * @p pathCount]
  156. /// @param[out] pathCount The number of polygons returned in the @p path array.
  157. /// @param[in] maxPath The maximum number of polygons the @p path array can hold. [Limit: >= 1]
  158. dtStatus findPath(dtPolyRef startRef, dtPolyRef endRef,
  159. const float* startPos, const float* endPos,
  160. const dtQueryFilter* filter,
  161. dtPolyRef* path, int* pathCount, const int maxPath) const;
  162. /// Finds the straight path from the start to the end position within the polygon corridor.
  163. /// @param[in] startPos Path start position. [(x, y, z)]
  164. /// @param[in] endPos Path end position. [(x, y, z)]
  165. /// @param[in] path An array of polygon references that represent the path corridor.
  166. /// @param[in] pathSize The number of polygons in the @p path array.
  167. /// @param[out] straightPath Points describing the straight path. [(x, y, z) * @p straightPathCount].
  168. /// @param[out] straightPathFlags Flags describing each point. (See: #dtStraightPathFlags) [opt]
  169. /// @param[out] straightPathRefs The reference id of the polygon that is being entered at each point. [opt]
  170. /// @param[out] straightPathCount The number of points in the straight path.
  171. /// @param[in] maxStraightPath The maximum number of points the straight path arrays can hold. [Limit: > 0]
  172. /// @param[in] options Query options. (see: #dtStraightPathOptions)
  173. /// @returns The status flags for the query.
  174. dtStatus findStraightPath(const float* startPos, const float* endPos,
  175. const dtPolyRef* path, const int pathSize,
  176. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  177. int* straightPathCount, const int maxStraightPath, const int options = 0) const;
  178. ///@}
  179. /// @name Sliced Pathfinding Functions
  180. /// Common use case:
  181. /// -# Call initSlicedFindPath() to initialize the sliced path query.
  182. /// -# Call updateSlicedFindPath() until it returns complete.
  183. /// -# Call finalizeSlicedFindPath() to get the path.
  184. ///@{
  185. /// Intializes a sliced path query.
  186. /// @param[in] startRef The refrence id of the start polygon.
  187. /// @param[in] endRef The reference id of the end polygon.
  188. /// @param[in] startPos A position within the start polygon. [(x, y, z)]
  189. /// @param[in] endPos A position within the end polygon. [(x, y, z)]
  190. /// @param[in] filter The polygon filter to apply to the query.
  191. /// @param[in] options query options (see: #dtFindPathOptions)
  192. /// @returns The status flags for the query.
  193. dtStatus initSlicedFindPath(dtPolyRef startRef, dtPolyRef endRef,
  194. const float* startPos, const float* endPos,
  195. const dtQueryFilter* filter, const unsigned int options = 0);
  196. /// Updates an in-progress sliced path query.
  197. /// @param[in] maxIter The maximum number of iterations to perform.
  198. /// @param[out] doneIters The actual number of iterations completed. [opt]
  199. /// @returns The status flags for the query.
  200. dtStatus updateSlicedFindPath(const int maxIter, int* doneIters);
  201. /// Finalizes and returns the results of a sliced path query.
  202. /// @param[out] path An ordered list of polygon references representing the path. (Start to end.)
  203. /// [(polyRef) * @p pathCount]
  204. /// @param[out] pathCount The number of polygons returned in the @p path array.
  205. /// @param[in] maxPath The max number of polygons the path array can hold. [Limit: >= 1]
  206. /// @returns The status flags for the query.
  207. dtStatus finalizeSlicedFindPath(dtPolyRef* path, int* pathCount, const int maxPath);
  208. /// Finalizes and returns the results of an incomplete sliced path query, returning the path to the furthest
  209. /// polygon on the existing path that was visited during the search.
  210. /// @param[in] existing An array of polygon references for the existing path.
  211. /// @param[in] existingSize The number of polygon in the @p existing array.
  212. /// @param[out] path An ordered list of polygon references representing the path. (Start to end.)
  213. /// [(polyRef) * @p pathCount]
  214. /// @param[out] pathCount The number of polygons returned in the @p path array.
  215. /// @param[in] maxPath The max number of polygons the @p path array can hold. [Limit: >= 1]
  216. /// @returns The status flags for the query.
  217. dtStatus finalizeSlicedFindPathPartial(const dtPolyRef* existing, const int existingSize,
  218. dtPolyRef* path, int* pathCount, const int maxPath);
  219. ///@}
  220. /// @name Dijkstra Search Functions
  221. /// @{
  222. /// Finds the polygons along the navigation graph that touch the specified circle.
  223. /// @param[in] startRef The reference id of the polygon where the search starts.
  224. /// @param[in] centerPos The center of the search circle. [(x, y, z)]
  225. /// @param[in] radius The radius of the search circle.
  226. /// @param[in] filter The polygon filter to apply to the query.
  227. /// @param[out] resultRef The reference ids of the polygons touched by the circle. [opt]
  228. /// @param[out] resultParent The reference ids of the parent polygons for each result.
  229. /// Zero if a result polygon has no parent. [opt]
  230. /// @param[out] resultCost The search cost from @p centerPos to the polygon. [opt]
  231. /// @param[out] resultCount The number of polygons found. [opt]
  232. /// @param[in] maxResult The maximum number of polygons the result arrays can hold.
  233. /// @returns The status flags for the query.
  234. dtStatus findPolysAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
  235. const dtQueryFilter* filter,
  236. dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
  237. int* resultCount, const int maxResult) const;
  238. /// Finds the polygons along the naviation graph that touch the specified convex polygon.
  239. /// @param[in] startRef The reference id of the polygon where the search starts.
  240. /// @param[in] verts The vertices describing the convex polygon. (CCW)
  241. /// [(x, y, z) * @p nverts]
  242. /// @param[in] nverts The number of vertices in the polygon.
  243. /// @param[in] filter The polygon filter to apply to the query.
  244. /// @param[out] resultRef The reference ids of the polygons touched by the search polygon. [opt]
  245. /// @param[out] resultParent The reference ids of the parent polygons for each result. Zero if a
  246. /// result polygon has no parent. [opt]
  247. /// @param[out] resultCost The search cost from the centroid point to the polygon. [opt]
  248. /// @param[out] resultCount The number of polygons found.
  249. /// @param[in] maxResult The maximum number of polygons the result arrays can hold.
  250. /// @returns The status flags for the query.
  251. dtStatus findPolysAroundShape(dtPolyRef startRef, const float* verts, const int nverts,
  252. const dtQueryFilter* filter,
  253. dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
  254. int* resultCount, const int maxResult) const;
  255. /// Gets a path from the explored nodes in the previous search.
  256. /// @param[in] endRef The reference id of the end polygon.
  257. /// @param[out] path An ordered list of polygon references representing the path. (Start to end.)
  258. /// [(polyRef) * @p pathCount]
  259. /// @param[out] pathCount The number of polygons returned in the @p path array.
  260. /// @param[in] maxPath The maximum number of polygons the @p path array can hold. [Limit: >= 0]
  261. /// @returns The status flags. Returns DT_FAILURE | DT_INVALID_PARAM if any parameter is wrong, or if
  262. /// @p endRef was not explored in the previous search. Returns DT_SUCCESS | DT_BUFFER_TOO_SMALL
  263. /// if @p path cannot contain the entire path. In this case it is filled to capacity with a partial path.
  264. /// Otherwise returns DT_SUCCESS.
  265. /// @remarks The result of this function depends on the state of the query object. For that reason it should only
  266. /// be used immediately after one of the two Dijkstra searches, findPolysAroundCircle or findPolysAroundShape.
  267. dtStatus getPathFromDijkstraSearch(dtPolyRef endRef, dtPolyRef* path, int* pathCount, int maxPath) const;
  268. /// @}
  269. /// @name Local Query Functions
  270. ///@{
  271. /// Finds the polygon nearest to the specified center point.
  272. /// @param[in] center The center of the search box. [(x, y, z)]
  273. /// @param[in] halfExtents The search distance along each axis. [(x, y, z)]
  274. /// @param[in] filter The polygon filter to apply to the query.
  275. /// @param[out] nearestRef The reference id of the nearest polygon.
  276. /// @param[out] nearestPt The nearest point on the polygon. [opt] [(x, y, z)]
  277. /// @returns The status flags for the query.
  278. dtStatus findNearestPoly(const float* center, const float* halfExtents,
  279. const dtQueryFilter* filter,
  280. dtPolyRef* nearestRef, float* nearestPt) const;
  281. /// Finds polygons that overlap the search box.
  282. /// @param[in] center The center of the search box. [(x, y, z)]
  283. /// @param[in] halfExtents The search distance along each axis. [(x, y, z)]
  284. /// @param[in] filter The polygon filter to apply to the query.
  285. /// @param[out] polys The reference ids of the polygons that overlap the query box.
  286. /// @param[out] polyCount The number of polygons in the search result.
  287. /// @param[in] maxPolys The maximum number of polygons the search result can hold.
  288. /// @returns The status flags for the query.
  289. dtStatus queryPolygons(const float* center, const float* halfExtents,
  290. const dtQueryFilter* filter,
  291. dtPolyRef* polys, int* polyCount, const int maxPolys) const;
  292. /// Finds polygons that overlap the search box.
  293. /// @param[in] center The center of the search box. [(x, y, z)]
  294. /// @param[in] halfExtents The search distance along each axis. [(x, y, z)]
  295. /// @param[in] filter The polygon filter to apply to the query.
  296. /// @param[in] query The query. Polygons found will be batched together and passed to this query.
  297. dtStatus queryPolygons(const float* center, const float* halfExtents,
  298. const dtQueryFilter* filter, dtPolyQuery* query) const;
  299. /// Finds the non-overlapping navigation polygons in the local neighbourhood around the center position.
  300. /// @param[in] startRef The reference id of the polygon where the search starts.
  301. /// @param[in] centerPos The center of the query circle. [(x, y, z)]
  302. /// @param[in] radius The radius of the query circle.
  303. /// @param[in] filter The polygon filter to apply to the query.
  304. /// @param[out] resultRef The reference ids of the polygons touched by the circle.
  305. /// @param[out] resultParent The reference ids of the parent polygons for each result.
  306. /// Zero if a result polygon has no parent. [opt]
  307. /// @param[out] resultCount The number of polygons found.
  308. /// @param[in] maxResult The maximum number of polygons the result arrays can hold.
  309. /// @returns The status flags for the query.
  310. dtStatus findLocalNeighbourhood(dtPolyRef startRef, const float* centerPos, const float radius,
  311. const dtQueryFilter* filter,
  312. dtPolyRef* resultRef, dtPolyRef* resultParent,
  313. int* resultCount, const int maxResult) const;
  314. /// Moves from the start to the end position constrained to the navigation mesh.
  315. /// @param[in] startRef The reference id of the start polygon.
  316. /// @param[in] startPos A position of the mover within the start polygon. [(x, y, x)]
  317. /// @param[in] endPos The desired end position of the mover. [(x, y, z)]
  318. /// @param[in] filter The polygon filter to apply to the query.
  319. /// @param[out] resultPos The result position of the mover. [(x, y, z)]
  320. /// @param[out] visited The reference ids of the polygons visited during the move.
  321. /// @param[out] visitedCount The number of polygons visited during the move.
  322. /// @param[in] maxVisitedSize The maximum number of polygons the @p visited array can hold.
  323. /// @returns The status flags for the query.
  324. dtStatus moveAlongSurface(dtPolyRef startRef, const float* startPos, const float* endPos,
  325. const dtQueryFilter* filter,
  326. float* resultPos, dtPolyRef* visited, int* visitedCount, const int maxVisitedSize) const;
  327. /// Casts a 'walkability' ray along the surface of the navigation mesh from
  328. /// the start position toward the end position.
  329. /// @note A wrapper around raycast(..., RaycastHit*). Retained for backward compatibility.
  330. /// @param[in] startRef The reference id of the start polygon.
  331. /// @param[in] startPos A position within the start polygon representing
  332. /// the start of the ray. [(x, y, z)]
  333. /// @param[in] endPos The position to cast the ray toward. [(x, y, z)]
  334. /// @param[out] t The hit parameter. (FLT_MAX if no wall hit.)
  335. /// @param[out] hitNormal The normal of the nearest wall hit. [(x, y, z)]
  336. /// @param[in] filter The polygon filter to apply to the query.
  337. /// @param[out] path The reference ids of the visited polygons. [opt]
  338. /// @param[out] pathCount The number of visited polygons. [opt]
  339. /// @param[in] maxPath The maximum number of polygons the @p path array can hold.
  340. /// @returns The status flags for the query.
  341. dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
  342. const dtQueryFilter* filter,
  343. float* t, float* hitNormal, dtPolyRef* path, int* pathCount, const int maxPath) const;
  344. /// Casts a 'walkability' ray along the surface of the navigation mesh from
  345. /// the start position toward the end position.
  346. /// @param[in] startRef The reference id of the start polygon.
  347. /// @param[in] startPos A position within the start polygon representing
  348. /// the start of the ray. [(x, y, z)]
  349. /// @param[in] endPos The position to cast the ray toward. [(x, y, z)]
  350. /// @param[in] filter The polygon filter to apply to the query.
  351. /// @param[in] flags govern how the raycast behaves. See dtRaycastOptions
  352. /// @param[out] hit Pointer to a raycast hit structure which will be filled by the results.
  353. /// @param[in] prevRef parent of start ref. Used during for cost calculation [opt]
  354. /// @returns The status flags for the query.
  355. dtStatus raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
  356. const dtQueryFilter* filter, const unsigned int options,
  357. dtRaycastHit* hit, dtPolyRef prevRef = 0) const;
  358. /// Finds the distance from the specified position to the nearest polygon wall.
  359. /// @param[in] startRef The reference id of the polygon containing @p centerPos.
  360. /// @param[in] centerPos The center of the search circle. [(x, y, z)]
  361. /// @param[in] maxRadius The radius of the search circle.
  362. /// @param[in] filter The polygon filter to apply to the query.
  363. /// @param[out] hitDist The distance to the nearest wall from @p centerPos.
  364. /// @param[out] hitPos The nearest position on the wall that was hit. [(x, y, z)]
  365. /// @param[out] hitNormal The normalized ray formed from the wall point to the
  366. /// source point. [(x, y, z)]
  367. /// @returns The status flags for the query.
  368. dtStatus findDistanceToWall(dtPolyRef startRef, const float* centerPos, const float maxRadius,
  369. const dtQueryFilter* filter,
  370. float* hitDist, float* hitPos, float* hitNormal) const;
  371. /// Returns the segments for the specified polygon, optionally including portals.
  372. /// @param[in] ref The reference id of the polygon.
  373. /// @param[in] filter The polygon filter to apply to the query.
  374. /// @param[out] segmentVerts The segments. [(ax, ay, az, bx, by, bz) * segmentCount]
  375. /// @param[out] segmentRefs The reference ids of each segment's neighbor polygon.
  376. /// Or zero if the segment is a wall. [opt] [(parentRef) * @p segmentCount]
  377. /// @param[out] segmentCount The number of segments returned.
  378. /// @param[in] maxSegments The maximum number of segments the result arrays can hold.
  379. /// @returns The status flags for the query.
  380. dtStatus getPolyWallSegments(dtPolyRef ref, const dtQueryFilter* filter,
  381. float* segmentVerts, dtPolyRef* segmentRefs, int* segmentCount,
  382. const int maxSegments) const;
  383. /// Returns random location on navmesh.
  384. /// Polygons are chosen weighted by area. The search runs in linear related to number of polygon.
  385. /// @param[in] filter The polygon filter to apply to the query.
  386. /// @param[in] frand Function returning a random number [0..1).
  387. /// @param[out] randomRef The reference id of the random location.
  388. /// @param[out] randomPt The random location.
  389. /// @returns The status flags for the query.
  390. dtStatus findRandomPoint(const dtQueryFilter* filter, float (*frand)(),
  391. dtPolyRef* randomRef, float* randomPt) const;
  392. /// Returns random location on navmesh within the reach of specified location.
  393. /// Polygons are chosen weighted by area. The search runs in linear related to number of polygon.
  394. /// The location is not exactly constrained by the circle, but it limits the visited polygons.
  395. /// @param[in] startRef The reference id of the polygon where the search starts.
  396. /// @param[in] centerPos The center of the search circle. [(x, y, z)]
  397. /// @param[in] filter The polygon filter to apply to the query.
  398. /// @param[in] frand Function returning a random number [0..1).
  399. /// @param[out] randomRef The reference id of the random location.
  400. /// @param[out] randomPt The random location. [(x, y, z)]
  401. /// @returns The status flags for the query.
  402. dtStatus findRandomPointAroundCircle(dtPolyRef startRef, const float* centerPos, const float maxRadius,
  403. const dtQueryFilter* filter, float (*frand)(),
  404. dtPolyRef* randomRef, float* randomPt) const;
  405. /// Finds the closest point on the specified polygon.
  406. /// @param[in] ref The reference id of the polygon.
  407. /// @param[in] pos The position to check. [(x, y, z)]
  408. /// @param[out] closest The closest point on the polygon. [(x, y, z)]
  409. /// @param[out] posOverPoly True of the position is over the polygon.
  410. /// @returns The status flags for the query.
  411. dtStatus closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest, bool* posOverPoly) const;
  412. /// Returns a point on the boundary closest to the source point if the source point is outside the
  413. /// polygon's xz-bounds.
  414. /// @param[in] ref The reference id to the polygon.
  415. /// @param[in] pos The position to check. [(x, y, z)]
  416. /// @param[out] closest The closest point. [(x, y, z)]
  417. /// @returns The status flags for the query.
  418. dtStatus closestPointOnPolyBoundary(dtPolyRef ref, const float* pos, float* closest) const;
  419. /// Gets the height of the polygon at the provided position using the height detail. (Most accurate.)
  420. /// @param[in] ref The reference id of the polygon.
  421. /// @param[in] pos A position within the xz-bounds of the polygon. [(x, y, z)]
  422. /// @param[out] height The height at the surface of the polygon.
  423. /// @returns The status flags for the query.
  424. dtStatus getPolyHeight(dtPolyRef ref, const float* pos, float* height) const;
  425. /// @}
  426. /// @name Miscellaneous Functions
  427. /// @{
  428. /// Returns true if the polygon reference is valid and passes the filter restrictions.
  429. /// @param[in] ref The polygon reference to check.
  430. /// @param[in] filter The filter to apply.
  431. bool isValidPolyRef(dtPolyRef ref, const dtQueryFilter* filter) const;
  432. /// Returns true if the polygon reference is in the closed list.
  433. /// @param[in] ref The reference id of the polygon to check.
  434. /// @returns True if the polygon is in closed list.
  435. bool isInClosedList(dtPolyRef ref) const;
  436. /// Gets the node pool.
  437. /// @returns The node pool.
  438. class dtNodePool* getNodePool() const { return m_nodePool; }
  439. /// Gets the navigation mesh the query object is using.
  440. /// @return The navigation mesh the query object is using.
  441. const dtNavMesh* getAttachedNavMesh() const { return m_nav; }
  442. /// @}
  443. private:
  444. // Explicitly disabled copy constructor and copy assignment operator
  445. dtNavMeshQuery(const dtNavMeshQuery&);
  446. dtNavMeshQuery& operator=(const dtNavMeshQuery&);
  447. /// Queries polygons within a tile.
  448. void queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax,
  449. const dtQueryFilter* filter, dtPolyQuery* query) const;
  450. /// Returns portal points between two polygons.
  451. dtStatus getPortalPoints(dtPolyRef from, dtPolyRef to, float* left, float* right,
  452. unsigned char& fromType, unsigned char& toType) const;
  453. dtStatus getPortalPoints(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
  454. dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
  455. float* left, float* right) const;
  456. /// Returns edge mid point between two polygons.
  457. dtStatus getEdgeMidPoint(dtPolyRef from, dtPolyRef to, float* mid) const;
  458. dtStatus getEdgeMidPoint(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
  459. dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
  460. float* mid) const;
  461. // Appends vertex to a straight path
  462. dtStatus appendVertex(const float* pos, const unsigned char flags, const dtPolyRef ref,
  463. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  464. int* straightPathCount, const int maxStraightPath) const;
  465. // Appends intermediate portal points to a straight path.
  466. dtStatus appendPortals(const int startIdx, const int endIdx, const float* endPos, const dtPolyRef* path,
  467. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  468. int* straightPathCount, const int maxStraightPath, const int options) const;
  469. // Gets the path leading to the specified end node.
  470. dtStatus getPathToNode(struct dtNode* endNode, dtPolyRef* path, int* pathCount, int maxPath) const;
  471. const dtNavMesh* m_nav; ///< Pointer to navmesh data.
  472. struct dtQueryData
  473. {
  474. dtStatus status;
  475. struct dtNode* lastBestNode;
  476. float lastBestNodeCost;
  477. dtPolyRef startRef, endRef;
  478. float startPos[3], endPos[3];
  479. const dtQueryFilter* filter;
  480. unsigned int options;
  481. float raycastLimitSqr;
  482. };
  483. dtQueryData m_query; ///< Sliced query state.
  484. class dtNodePool* m_tinyNodePool; ///< Pointer to small node pool.
  485. class dtNodePool* m_nodePool; ///< Pointer to node pool.
  486. class dtNodeQueue* m_openList; ///< Pointer to open list queue.
  487. };
  488. /// Allocates a query object using the Detour allocator.
  489. /// @return An allocated query object, or null on failure.
  490. /// @ingroup detour
  491. dtNavMeshQuery* dtAllocNavMeshQuery();
  492. /// Frees the specified query object using the Detour allocator.
  493. /// @param[in] query A query object allocated using #dtAllocNavMeshQuery
  494. /// @ingroup detour
  495. void dtFreeNavMeshQuery(dtNavMeshQuery* query);
  496. #endif // DETOURNAVMESHQUERY_H