DetourNavMeshQuery.h 29 KB

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