Recast.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. #ifndef RECAST_H
  19. #define RECAST_H
  20. /// The value of PI used by Recast.
  21. static const float RC_PI = 3.14159265f;
  22. /// Recast log categories.
  23. /// @see rcContext
  24. enum rcLogCategory
  25. {
  26. RC_LOG_PROGRESS = 1, ///< A progress log entry.
  27. RC_LOG_WARNING, ///< A warning log entry.
  28. RC_LOG_ERROR, ///< An error log entry.
  29. };
  30. /// Recast performance timer categories.
  31. /// @see rcContext
  32. enum rcTimerLabel
  33. {
  34. /// The user defined total time of the build.
  35. RC_TIMER_TOTAL,
  36. /// A user defined build time.
  37. RC_TIMER_TEMP,
  38. /// The time to rasterize the triangles. (See: #rcRasterizeTriangle)
  39. RC_TIMER_RASTERIZE_TRIANGLES,
  40. /// The time to build the compact heightfield. (See: #rcBuildCompactHeightfield)
  41. RC_TIMER_BUILD_COMPACTHEIGHTFIELD,
  42. /// The total time to build the contours. (See: #rcBuildContours)
  43. RC_TIMER_BUILD_CONTOURS,
  44. /// The time to trace the boundaries of the contours. (See: #rcBuildContours)
  45. RC_TIMER_BUILD_CONTOURS_TRACE,
  46. /// The time to simplify the contours. (See: #rcBuildContours)
  47. RC_TIMER_BUILD_CONTOURS_SIMPLIFY,
  48. /// The time to filter ledge spans. (See: #rcFilterLedgeSpans)
  49. RC_TIMER_FILTER_BORDER,
  50. /// The time to filter low height spans. (See: #rcFilterWalkableLowHeightSpans)
  51. RC_TIMER_FILTER_WALKABLE,
  52. /// The time to apply the median filter. (See: #rcMedianFilterWalkableArea)
  53. RC_TIMER_MEDIAN_AREA,
  54. /// The time to filter low obstacles. (See: #rcFilterLowHangingWalkableObstacles)
  55. RC_TIMER_FILTER_LOW_OBSTACLES,
  56. /// The time to build the polygon mesh. (See: #rcBuildPolyMesh)
  57. RC_TIMER_BUILD_POLYMESH,
  58. /// The time to merge polygon meshes. (See: #rcMergePolyMeshes)
  59. RC_TIMER_MERGE_POLYMESH,
  60. /// The time to erode the walkable area. (See: #rcErodeWalkableArea)
  61. RC_TIMER_ERODE_AREA,
  62. /// The time to mark a box area. (See: #rcMarkBoxArea)
  63. RC_TIMER_MARK_BOX_AREA,
  64. /// The time to mark a cylinder area. (See: #rcMarkCylinderArea)
  65. RC_TIMER_MARK_CYLINDER_AREA,
  66. /// The time to mark a convex polygon area. (See: #rcMarkConvexPolyArea)
  67. RC_TIMER_MARK_CONVEXPOLY_AREA,
  68. /// The total time to build the distance field. (See: #rcBuildDistanceField)
  69. RC_TIMER_BUILD_DISTANCEFIELD,
  70. /// The time to build the distances of the distance field. (See: #rcBuildDistanceField)
  71. RC_TIMER_BUILD_DISTANCEFIELD_DIST,
  72. /// The time to blur the distance field. (See: #rcBuildDistanceField)
  73. RC_TIMER_BUILD_DISTANCEFIELD_BLUR,
  74. /// The total time to build the regions. (See: #rcBuildRegions, #rcBuildRegionsMonotone)
  75. RC_TIMER_BUILD_REGIONS,
  76. /// The total time to apply the watershed algorithm. (See: #rcBuildRegions)
  77. RC_TIMER_BUILD_REGIONS_WATERSHED,
  78. /// The time to expand regions while applying the watershed algorithm. (See: #rcBuildRegions)
  79. RC_TIMER_BUILD_REGIONS_EXPAND,
  80. /// The time to flood regions while applying the watershed algorithm. (See: #rcBuildRegions)
  81. RC_TIMER_BUILD_REGIONS_FLOOD,
  82. /// The time to filter out small regions. (See: #rcBuildRegions, #rcBuildRegionsMonotone)
  83. RC_TIMER_BUILD_REGIONS_FILTER,
  84. /// The time to build heightfield layers. (See: #rcBuildHeightfieldLayers)
  85. RC_TIMER_BUILD_LAYERS,
  86. /// The time to build the polygon mesh detail. (See: #rcBuildPolyMeshDetail)
  87. RC_TIMER_BUILD_POLYMESHDETAIL,
  88. /// The time to merge polygon mesh details. (See: #rcMergePolyMeshDetails)
  89. RC_TIMER_MERGE_POLYMESHDETAIL,
  90. /// The maximum number of timers. (Used for iterating timers.)
  91. RC_MAX_TIMERS
  92. };
  93. /// Provides an interface for optional logging and performance tracking of the Recast
  94. /// build process.
  95. /// @ingroup recast
  96. class rcContext
  97. {
  98. public:
  99. /// Contructor.
  100. /// @param[in] state TRUE if the logging and performance timers should be enabled. [Default: true]
  101. inline rcContext(bool state = true) : m_logEnabled(state), m_timerEnabled(state) {}
  102. virtual ~rcContext() {}
  103. /// Enables or disables logging.
  104. /// @param[in] state TRUE if logging should be enabled.
  105. inline void enableLog(bool state) { m_logEnabled = state; }
  106. /// Clears all log entries.
  107. inline void resetLog() { if (m_logEnabled) doResetLog(); }
  108. /// Logs a message.
  109. /// @param[in] category The category of the message.
  110. /// @param[in] format The message.
  111. void log(const rcLogCategory category, const char* format, ...);
  112. /// Enables or disables the performance timers.
  113. /// @param[in] state TRUE if timers should be enabled.
  114. inline void enableTimer(bool state) { m_timerEnabled = state; }
  115. /// Clears all peformance timers. (Resets all to unused.)
  116. inline void resetTimers() { if (m_timerEnabled) doResetTimers(); }
  117. /// Starts the specified performance timer.
  118. /// @param label The category of the timer.
  119. inline void startTimer(const rcTimerLabel label) { if (m_timerEnabled) doStartTimer(label); }
  120. /// Stops the specified performance timer.
  121. /// @param label The category of the timer.
  122. inline void stopTimer(const rcTimerLabel label) { if (m_timerEnabled) doStopTimer(label); }
  123. /// Returns the total accumulated time of the specified performance timer.
  124. /// @param label The category of the timer.
  125. /// @return The accumulated time of the timer, or -1 if timers are disabled or the timer has never been started.
  126. inline int getAccumulatedTime(const rcTimerLabel label) const { return m_timerEnabled ? doGetAccumulatedTime(label) : -1; }
  127. protected:
  128. /// Clears all log entries.
  129. virtual void doResetLog() {}
  130. /// Logs a message.
  131. /// @param[in] category The category of the message.
  132. /// @param[in] msg The formatted message.
  133. /// @param[in] len The length of the formatted message.
  134. virtual void doLog(const rcLogCategory /*category*/, const char* /*msg*/, const int /*len*/) {}
  135. /// Clears all timers. (Resets all to unused.)
  136. virtual void doResetTimers() {}
  137. /// Starts the specified performance timer.
  138. /// @param[in] label The category of timer.
  139. virtual void doStartTimer(const rcTimerLabel /*label*/) {}
  140. /// Stops the specified performance timer.
  141. /// @param[in] label The category of the timer.
  142. virtual void doStopTimer(const rcTimerLabel /*label*/) {}
  143. /// Returns the total accumulated time of the specified performance timer.
  144. /// @param[in] label The category of the timer.
  145. /// @return The accumulated time of the timer, or -1 if timers are disabled or the timer has never been started.
  146. virtual int doGetAccumulatedTime(const rcTimerLabel /*label*/) const { return -1; }
  147. /// True if logging is enabled.
  148. bool m_logEnabled;
  149. /// True if the performance timers are enabled.
  150. bool m_timerEnabled;
  151. };
  152. /// A helper to first start a timer and then stop it when this helper goes out of scope.
  153. /// @see rcContext
  154. class rcScopedTimer
  155. {
  156. public:
  157. /// Constructs an instance and starts the timer.
  158. /// @param[in] ctx The context to use.
  159. /// @param[in] label The category of the timer.
  160. inline rcScopedTimer(rcContext* ctx, const rcTimerLabel label) : m_ctx(ctx), m_label(label) { m_ctx->startTimer(m_label); }
  161. inline ~rcScopedTimer() { m_ctx->stopTimer(m_label); }
  162. private:
  163. // Explicitly disabled copy constructor and copy assignment operator.
  164. rcScopedTimer(const rcScopedTimer&);
  165. rcScopedTimer& operator=(const rcScopedTimer&);
  166. rcContext* const m_ctx;
  167. const rcTimerLabel m_label;
  168. };
  169. /// Specifies a configuration to use when performing Recast builds.
  170. /// @ingroup recast
  171. struct rcConfig
  172. {
  173. /// The width of the field along the x-axis. [Limit: >= 0] [Units: vx]
  174. int width;
  175. /// The height of the field along the z-axis. [Limit: >= 0] [Units: vx]
  176. int height;
  177. /// The width/height size of tile's on the xz-plane. [Limit: >= 0] [Units: vx]
  178. int tileSize;
  179. /// The size of the non-navigable border around the heightfield. [Limit: >=0] [Units: vx]
  180. int borderSize;
  181. /// The xz-plane cell size to use for fields. [Limit: > 0] [Units: wu]
  182. float cs;
  183. /// The y-axis cell size to use for fields. [Limit: > 0] [Units: wu]
  184. float ch;
  185. /// The minimum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  186. float bmin[3];
  187. /// The maximum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  188. float bmax[3];
  189. /// The maximum slope that is considered walkable. [Limits: 0 <= value < 90] [Units: Degrees]
  190. float walkableSlopeAngle;
  191. /// Minimum floor to 'ceiling' height that will still allow the floor area to
  192. /// be considered walkable. [Limit: >= 3] [Units: vx]
  193. int walkableHeight;
  194. /// Maximum ledge height that is considered to still be traversable. [Limit: >=0] [Units: vx]
  195. int walkableClimb;
  196. /// The distance to erode/shrink the walkable area of the heightfield away from
  197. /// obstructions. [Limit: >=0] [Units: vx]
  198. int walkableRadius;
  199. /// The maximum allowed length for contour edges along the border of the mesh. [Limit: >=0] [Units: vx]
  200. int maxEdgeLen;
  201. /// The maximum distance a simplfied contour's border edges should deviate
  202. /// the original raw contour. [Limit: >=0] [Units: vx]
  203. float maxSimplificationError;
  204. /// The minimum number of cells allowed to form isolated island areas. [Limit: >=0] [Units: vx]
  205. int minRegionArea;
  206. /// Any regions with a span count smaller than this value will, if possible,
  207. /// be merged with larger regions. [Limit: >=0] [Units: vx]
  208. int mergeRegionArea;
  209. /// The maximum number of vertices allowed for polygons generated during the
  210. /// contour to polygon conversion process. [Limit: >= 3]
  211. int maxVertsPerPoly;
  212. /// Sets the sampling distance to use when generating the detail mesh.
  213. /// (For height detail only.) [Limits: 0 or >= 0.9] [Units: wu]
  214. float detailSampleDist;
  215. /// The maximum distance the detail mesh surface should deviate from heightfield
  216. /// data. (For height detail only.) [Limit: >=0] [Units: wu]
  217. float detailSampleMaxError;
  218. };
  219. /// Defines the number of bits allocated to rcSpan::smin and rcSpan::smax.
  220. static const int RC_SPAN_HEIGHT_BITS = 13;
  221. /// Defines the maximum value for rcSpan::smin and rcSpan::smax.
  222. static const int RC_SPAN_MAX_HEIGHT = (1 << RC_SPAN_HEIGHT_BITS) - 1;
  223. /// The number of spans allocated per span spool.
  224. /// @see rcSpanPool
  225. static const int RC_SPANS_PER_POOL = 2048;
  226. /// Represents a span in a heightfield.
  227. /// @see rcHeightfield
  228. struct rcSpan
  229. {
  230. unsigned int smin : RC_SPAN_HEIGHT_BITS; ///< The lower limit of the span. [Limit: < #smax]
  231. unsigned int smax : RC_SPAN_HEIGHT_BITS; ///< The upper limit of the span. [Limit: <= #RC_SPAN_MAX_HEIGHT]
  232. unsigned int area : 6; ///< The area id assigned to the span.
  233. rcSpan* next; ///< The next span higher up in column.
  234. };
  235. /// A memory pool used for quick allocation of spans within a heightfield.
  236. /// @see rcHeightfield
  237. struct rcSpanPool
  238. {
  239. rcSpanPool* next; ///< The next span pool.
  240. rcSpan items[RC_SPANS_PER_POOL]; ///< Array of spans in the pool.
  241. };
  242. /// A dynamic heightfield representing obstructed space.
  243. /// @ingroup recast
  244. struct rcHeightfield
  245. {
  246. int width; ///< The width of the heightfield. (Along the x-axis in cell units.)
  247. int height; ///< The height of the heightfield. (Along the z-axis in cell units.)
  248. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  249. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  250. float cs; ///< The size of each cell. (On the xz-plane.)
  251. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  252. rcSpan** spans; ///< Heightfield of spans (width*height).
  253. rcSpanPool* pools; ///< Linked list of span pools.
  254. rcSpan* freelist; ///< The next free span.
  255. };
  256. /// Provides information on the content of a cell column in a compact heightfield.
  257. struct rcCompactCell
  258. {
  259. unsigned int index : 24; ///< Index to the first span in the column.
  260. unsigned int count : 8; ///< Number of spans in the column.
  261. };
  262. /// Represents a span of unobstructed space within a compact heightfield.
  263. struct rcCompactSpan
  264. {
  265. unsigned short y; ///< The lower extent of the span. (Measured from the heightfield's base.)
  266. unsigned short reg; ///< The id of the region the span belongs to. (Or zero if not in a region.)
  267. unsigned int con : 24; ///< Packed neighbor connection data.
  268. unsigned int h : 8; ///< The height of the span. (Measured from #y.)
  269. };
  270. /// A compact, static heightfield representing unobstructed space.
  271. /// @ingroup recast
  272. struct rcCompactHeightfield
  273. {
  274. int width; ///< The width of the heightfield. (Along the x-axis in cell units.)
  275. int height; ///< The height of the heightfield. (Along the z-axis in cell units.)
  276. int spanCount; ///< The number of spans in the heightfield.
  277. int walkableHeight; ///< The walkable height used during the build of the field. (See: rcConfig::walkableHeight)
  278. int walkableClimb; ///< The walkable climb used during the build of the field. (See: rcConfig::walkableClimb)
  279. int borderSize; ///< The AABB border size used during the build of the field. (See: rcConfig::borderSize)
  280. unsigned short maxDistance; ///< The maximum distance value of any span within the field.
  281. unsigned short maxRegions; ///< The maximum region id of any span within the field.
  282. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  283. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  284. float cs; ///< The size of each cell. (On the xz-plane.)
  285. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  286. rcCompactCell* cells; ///< Array of cells. [Size: #width*#height]
  287. rcCompactSpan* spans; ///< Array of spans. [Size: #spanCount]
  288. unsigned short* dist; ///< Array containing border distance data. [Size: #spanCount]
  289. unsigned char* areas; ///< Array containing area id data. [Size: #spanCount]
  290. };
  291. /// Represents a heightfield layer within a layer set.
  292. /// @see rcHeightfieldLayerSet
  293. struct rcHeightfieldLayer
  294. {
  295. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  296. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  297. float cs; ///< The size of each cell. (On the xz-plane.)
  298. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  299. int width; ///< The width of the heightfield. (Along the x-axis in cell units.)
  300. int height; ///< The height of the heightfield. (Along the z-axis in cell units.)
  301. int minx; ///< The minimum x-bounds of usable data.
  302. int maxx; ///< The maximum x-bounds of usable data.
  303. int miny; ///< The minimum y-bounds of usable data. (Along the z-axis.)
  304. int maxy; ///< The maximum y-bounds of usable data. (Along the z-axis.)
  305. int hmin; ///< The minimum height bounds of usable data. (Along the y-axis.)
  306. int hmax; ///< The maximum height bounds of usable data. (Along the y-axis.)
  307. unsigned char* heights; ///< The heightfield. [Size: width * height]
  308. unsigned char* areas; ///< Area ids. [Size: Same as #heights]
  309. unsigned char* cons; ///< Packed neighbor connection information. [Size: Same as #heights]
  310. };
  311. /// Represents a set of heightfield layers.
  312. /// @ingroup recast
  313. /// @see rcAllocHeightfieldLayerSet, rcFreeHeightfieldLayerSet
  314. struct rcHeightfieldLayerSet
  315. {
  316. rcHeightfieldLayer* layers; ///< The layers in the set. [Size: #nlayers]
  317. int nlayers; ///< The number of layers in the set.
  318. };
  319. /// Represents a simple, non-overlapping contour in field space.
  320. struct rcContour
  321. {
  322. int* verts; ///< Simplified contour vertex and connection data. [Size: 4 * #nverts]
  323. int nverts; ///< The number of vertices in the simplified contour.
  324. int* rverts; ///< Raw contour vertex and connection data. [Size: 4 * #nrverts]
  325. int nrverts; ///< The number of vertices in the raw contour.
  326. unsigned short reg; ///< The region id of the contour.
  327. unsigned char area; ///< The area id of the contour.
  328. };
  329. /// Represents a group of related contours.
  330. /// @ingroup recast
  331. struct rcContourSet
  332. {
  333. rcContour* conts; ///< An array of the contours in the set. [Size: #nconts]
  334. int nconts; ///< The number of contours in the set.
  335. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  336. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  337. float cs; ///< The size of each cell. (On the xz-plane.)
  338. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  339. int width; ///< The width of the set. (Along the x-axis in cell units.)
  340. int height; ///< The height of the set. (Along the z-axis in cell units.)
  341. int borderSize; ///< The AABB border size used to generate the source data from which the contours were derived.
  342. float maxError; ///< The max edge error that this contour set was simplified with.
  343. };
  344. /// Represents a polygon mesh suitable for use in building a navigation mesh.
  345. /// @ingroup recast
  346. struct rcPolyMesh
  347. {
  348. unsigned short* verts; ///< The mesh vertices. [Form: (x, y, z) * #nverts]
  349. unsigned short* polys; ///< Polygon and neighbor data. [Length: #maxpolys * 2 * #nvp]
  350. unsigned short* regs; ///< The region id assigned to each polygon. [Length: #maxpolys]
  351. unsigned short* flags; ///< The user defined flags for each polygon. [Length: #maxpolys]
  352. unsigned char* areas; ///< The area id assigned to each polygon. [Length: #maxpolys]
  353. int nverts; ///< The number of vertices.
  354. int npolys; ///< The number of polygons.
  355. int maxpolys; ///< The number of allocated polygons.
  356. int nvp; ///< The maximum number of vertices per polygon.
  357. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  358. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  359. float cs; ///< The size of each cell. (On the xz-plane.)
  360. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  361. int borderSize; ///< The AABB border size used to generate the source data from which the mesh was derived.
  362. float maxEdgeError; ///< The max error of the polygon edges in the mesh.
  363. };
  364. /// Contains triangle meshes that represent detailed height data associated
  365. /// with the polygons in its associated polygon mesh object.
  366. /// @ingroup recast
  367. struct rcPolyMeshDetail
  368. {
  369. unsigned int* meshes; ///< The sub-mesh data. [Size: 4*#nmeshes]
  370. float* verts; ///< The mesh vertices. [Size: 3*#nverts]
  371. unsigned char* tris; ///< The mesh triangles. [Size: 4*#ntris]
  372. int nmeshes; ///< The number of sub-meshes defined by #meshes.
  373. int nverts; ///< The number of vertices in #verts.
  374. int ntris; ///< The number of triangles in #tris.
  375. };
  376. /// @name Allocation Functions
  377. /// Functions used to allocate and de-allocate Recast objects.
  378. /// @see rcAllocSetCustom
  379. /// @{
  380. /// Allocates a heightfield object using the Recast allocator.
  381. /// @return A heightfield that is ready for initialization, or null on failure.
  382. /// @ingroup recast
  383. /// @see rcCreateHeightfield, rcFreeHeightField
  384. rcHeightfield* rcAllocHeightfield();
  385. /// Frees the specified heightfield object using the Recast allocator.
  386. /// @param[in] hf A heightfield allocated using #rcAllocHeightfield
  387. /// @ingroup recast
  388. /// @see rcAllocHeightfield
  389. void rcFreeHeightField(rcHeightfield* hf);
  390. /// Allocates a compact heightfield object using the Recast allocator.
  391. /// @return A compact heightfield that is ready for initialization, or null on failure.
  392. /// @ingroup recast
  393. /// @see rcBuildCompactHeightfield, rcFreeCompactHeightfield
  394. rcCompactHeightfield* rcAllocCompactHeightfield();
  395. /// Frees the specified compact heightfield object using the Recast allocator.
  396. /// @param[in] chf A compact heightfield allocated using #rcAllocCompactHeightfield
  397. /// @ingroup recast
  398. /// @see rcAllocCompactHeightfield
  399. void rcFreeCompactHeightfield(rcCompactHeightfield* chf);
  400. /// Allocates a heightfield layer set using the Recast allocator.
  401. /// @return A heightfield layer set that is ready for initialization, or null on failure.
  402. /// @ingroup recast
  403. /// @see rcBuildHeightfieldLayers, rcFreeHeightfieldLayerSet
  404. rcHeightfieldLayerSet* rcAllocHeightfieldLayerSet();
  405. /// Frees the specified heightfield layer set using the Recast allocator.
  406. /// @param[in] lset A heightfield layer set allocated using #rcAllocHeightfieldLayerSet
  407. /// @ingroup recast
  408. /// @see rcAllocHeightfieldLayerSet
  409. void rcFreeHeightfieldLayerSet(rcHeightfieldLayerSet* lset);
  410. /// Allocates a contour set object using the Recast allocator.
  411. /// @return A contour set that is ready for initialization, or null on failure.
  412. /// @ingroup recast
  413. /// @see rcBuildContours, rcFreeContourSet
  414. rcContourSet* rcAllocContourSet();
  415. /// Frees the specified contour set using the Recast allocator.
  416. /// @param[in] cset A contour set allocated using #rcAllocContourSet
  417. /// @ingroup recast
  418. /// @see rcAllocContourSet
  419. void rcFreeContourSet(rcContourSet* cset);
  420. /// Allocates a polygon mesh object using the Recast allocator.
  421. /// @return A polygon mesh that is ready for initialization, or null on failure.
  422. /// @ingroup recast
  423. /// @see rcBuildPolyMesh, rcFreePolyMesh
  424. rcPolyMesh* rcAllocPolyMesh();
  425. /// Frees the specified polygon mesh using the Recast allocator.
  426. /// @param[in] pmesh A polygon mesh allocated using #rcAllocPolyMesh
  427. /// @ingroup recast
  428. /// @see rcAllocPolyMesh
  429. void rcFreePolyMesh(rcPolyMesh* pmesh);
  430. /// Allocates a detail mesh object using the Recast allocator.
  431. /// @return A detail mesh that is ready for initialization, or null on failure.
  432. /// @ingroup recast
  433. /// @see rcBuildPolyMeshDetail, rcFreePolyMeshDetail
  434. rcPolyMeshDetail* rcAllocPolyMeshDetail();
  435. /// Frees the specified detail mesh using the Recast allocator.
  436. /// @param[in] dmesh A detail mesh allocated using #rcAllocPolyMeshDetail
  437. /// @ingroup recast
  438. /// @see rcAllocPolyMeshDetail
  439. void rcFreePolyMeshDetail(rcPolyMeshDetail* dmesh);
  440. /// @}
  441. /// Heighfield border flag.
  442. /// If a heightfield region ID has this bit set, then the region is a border
  443. /// region and its spans are considered unwalkable.
  444. /// (Used during the region and contour build process.)
  445. /// @see rcCompactSpan::reg
  446. static const unsigned short RC_BORDER_REG = 0x8000;
  447. /// Polygon touches multiple regions.
  448. /// If a polygon has this region ID it was merged with or created
  449. /// from polygons of different regions during the polymesh
  450. /// build step that removes redundant border vertices.
  451. /// (Used during the polymesh and detail polymesh build processes)
  452. /// @see rcPolyMesh::regs
  453. static const unsigned short RC_MULTIPLE_REGS = 0;
  454. /// Border vertex flag.
  455. /// If a region ID has this bit set, then the associated element lies on
  456. /// a tile border. If a contour vertex's region ID has this bit set, the
  457. /// vertex will later be removed in order to match the segments and vertices
  458. /// at tile boundaries.
  459. /// (Used during the build process.)
  460. /// @see rcCompactSpan::reg, #rcContour::verts, #rcContour::rverts
  461. static const int RC_BORDER_VERTEX = 0x10000;
  462. /// Area border flag.
  463. /// If a region ID has this bit set, then the associated element lies on
  464. /// the border of an area.
  465. /// (Used during the region and contour build process.)
  466. /// @see rcCompactSpan::reg, #rcContour::verts, #rcContour::rverts
  467. static const int RC_AREA_BORDER = 0x20000;
  468. /// Contour build flags.
  469. /// @see rcBuildContours
  470. enum rcBuildContoursFlags
  471. {
  472. RC_CONTOUR_TESS_WALL_EDGES = 0x01, ///< Tessellate solid (impassable) edges during contour simplification.
  473. RC_CONTOUR_TESS_AREA_EDGES = 0x02, ///< Tessellate edges between areas during contour simplification.
  474. };
  475. /// Applied to the region id field of contour vertices in order to extract the region id.
  476. /// The region id field of a vertex may have several flags applied to it. So the
  477. /// fields value can't be used directly.
  478. /// @see rcContour::verts, rcContour::rverts
  479. static const int RC_CONTOUR_REG_MASK = 0xffff;
  480. /// An value which indicates an invalid index within a mesh.
  481. /// @note This does not necessarily indicate an error.
  482. /// @see rcPolyMesh::polys
  483. static const unsigned short RC_MESH_NULL_IDX = 0xffff;
  484. /// Represents the null area.
  485. /// When a data element is given this value it is considered to no longer be
  486. /// assigned to a usable area. (E.g. It is unwalkable.)
  487. static const unsigned char RC_NULL_AREA = 0;
  488. /// The default area id used to indicate a walkable polygon.
  489. /// This is also the maximum allowed area id, and the only non-null area id
  490. /// recognized by some steps in the build process.
  491. static const unsigned char RC_WALKABLE_AREA = 63;
  492. /// The value returned by #rcGetCon if the specified direction is not connected
  493. /// to another span. (Has no neighbor.)
  494. static const int RC_NOT_CONNECTED = 0x3f;
  495. /// @name General helper functions
  496. /// @{
  497. /// Used to ignore a function parameter. VS complains about unused parameters
  498. /// and this silences the warning.
  499. /// @param [in] _ Unused parameter
  500. template<class T> void rcIgnoreUnused(const T&) { }
  501. /// Swaps the values of the two parameters.
  502. /// @param[in,out] a Value A
  503. /// @param[in,out] b Value B
  504. template<class T> inline void rcSwap(T& a, T& b) { T t = a; a = b; b = t; }
  505. /// Returns the minimum of two values.
  506. /// @param[in] a Value A
  507. /// @param[in] b Value B
  508. /// @return The minimum of the two values.
  509. template<class T> inline T rcMin(T a, T b) { return a < b ? a : b; }
  510. /// Returns the maximum of two values.
  511. /// @param[in] a Value A
  512. /// @param[in] b Value B
  513. /// @return The maximum of the two values.
  514. template<class T> inline T rcMax(T a, T b) { return a > b ? a : b; }
  515. /// Returns the absolute value.
  516. /// @param[in] a The value.
  517. /// @return The absolute value of the specified value.
  518. template<class T> inline T rcAbs(T a) { return a < 0 ? -a : a; }
  519. /// Returns the square of the value.
  520. /// @param[in] a The value.
  521. /// @return The square of the value.
  522. template<class T> inline T rcSqr(T a) { return a*a; }
  523. /// Clamps the value to the specified range.
  524. /// @param[in] v The value to clamp.
  525. /// @param[in] mn The minimum permitted return value.
  526. /// @param[in] mx The maximum permitted return value.
  527. /// @return The value, clamped to the specified range.
  528. template<class T> inline T rcClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
  529. /// Returns the square root of the value.
  530. /// @param[in] x The value.
  531. /// @return The square root of the vlaue.
  532. float rcSqrt(float x);
  533. /// @}
  534. /// @name Vector helper functions.
  535. /// @{
  536. /// Derives the cross product of two vectors. (@p v1 x @p v2)
  537. /// @param[out] dest The cross product. [(x, y, z)]
  538. /// @param[in] v1 A Vector [(x, y, z)]
  539. /// @param[in] v2 A vector [(x, y, z)]
  540. inline void rcVcross(float* dest, const float* v1, const float* v2)
  541. {
  542. dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
  543. dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
  544. dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
  545. }
  546. /// Derives the dot product of two vectors. (@p v1 . @p v2)
  547. /// @param[in] v1 A Vector [(x, y, z)]
  548. /// @param[in] v2 A vector [(x, y, z)]
  549. /// @return The dot product.
  550. inline float rcVdot(const float* v1, const float* v2)
  551. {
  552. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  553. }
  554. /// Performs a scaled vector addition. (@p v1 + (@p v2 * @p s))
  555. /// @param[out] dest The result vector. [(x, y, z)]
  556. /// @param[in] v1 The base vector. [(x, y, z)]
  557. /// @param[in] v2 The vector to scale and add to @p v1. [(x, y, z)]
  558. /// @param[in] s The amount to scale @p v2 by before adding to @p v1.
  559. inline void rcVmad(float* dest, const float* v1, const float* v2, const float s)
  560. {
  561. dest[0] = v1[0]+v2[0]*s;
  562. dest[1] = v1[1]+v2[1]*s;
  563. dest[2] = v1[2]+v2[2]*s;
  564. }
  565. /// Performs a vector addition. (@p v1 + @p v2)
  566. /// @param[out] dest The result vector. [(x, y, z)]
  567. /// @param[in] v1 The base vector. [(x, y, z)]
  568. /// @param[in] v2 The vector to add to @p v1. [(x, y, z)]
  569. inline void rcVadd(float* dest, const float* v1, const float* v2)
  570. {
  571. dest[0] = v1[0]+v2[0];
  572. dest[1] = v1[1]+v2[1];
  573. dest[2] = v1[2]+v2[2];
  574. }
  575. /// Performs a vector subtraction. (@p v1 - @p v2)
  576. /// @param[out] dest The result vector. [(x, y, z)]
  577. /// @param[in] v1 The base vector. [(x, y, z)]
  578. /// @param[in] v2 The vector to subtract from @p v1. [(x, y, z)]
  579. inline void rcVsub(float* dest, const float* v1, const float* v2)
  580. {
  581. dest[0] = v1[0]-v2[0];
  582. dest[1] = v1[1]-v2[1];
  583. dest[2] = v1[2]-v2[2];
  584. }
  585. /// Selects the minimum value of each element from the specified vectors.
  586. /// @param[in,out] mn A vector. (Will be updated with the result.) [(x, y, z)]
  587. /// @param[in] v A vector. [(x, y, z)]
  588. inline void rcVmin(float* mn, const float* v)
  589. {
  590. mn[0] = rcMin(mn[0], v[0]);
  591. mn[1] = rcMin(mn[1], v[1]);
  592. mn[2] = rcMin(mn[2], v[2]);
  593. }
  594. /// Selects the maximum value of each element from the specified vectors.
  595. /// @param[in,out] mx A vector. (Will be updated with the result.) [(x, y, z)]
  596. /// @param[in] v A vector. [(x, y, z)]
  597. inline void rcVmax(float* mx, const float* v)
  598. {
  599. mx[0] = rcMax(mx[0], v[0]);
  600. mx[1] = rcMax(mx[1], v[1]);
  601. mx[2] = rcMax(mx[2], v[2]);
  602. }
  603. /// Performs a vector copy.
  604. /// @param[out] dest The result. [(x, y, z)]
  605. /// @param[in] v The vector to copy. [(x, y, z)]
  606. inline void rcVcopy(float* dest, const float* v)
  607. {
  608. dest[0] = v[0];
  609. dest[1] = v[1];
  610. dest[2] = v[2];
  611. }
  612. /// Returns the distance between two points.
  613. /// @param[in] v1 A point. [(x, y, z)]
  614. /// @param[in] v2 A point. [(x, y, z)]
  615. /// @return The distance between the two points.
  616. inline float rcVdist(const float* v1, const float* v2)
  617. {
  618. float dx = v2[0] - v1[0];
  619. float dy = v2[1] - v1[1];
  620. float dz = v2[2] - v1[2];
  621. return rcSqrt(dx*dx + dy*dy + dz*dz);
  622. }
  623. /// Returns the square of the distance between two points.
  624. /// @param[in] v1 A point. [(x, y, z)]
  625. /// @param[in] v2 A point. [(x, y, z)]
  626. /// @return The square of the distance between the two points.
  627. inline float rcVdistSqr(const float* v1, const float* v2)
  628. {
  629. float dx = v2[0] - v1[0];
  630. float dy = v2[1] - v1[1];
  631. float dz = v2[2] - v1[2];
  632. return dx*dx + dy*dy + dz*dz;
  633. }
  634. /// Normalizes the vector.
  635. /// @param[in,out] v The vector to normalize. [(x, y, z)]
  636. inline void rcVnormalize(float* v)
  637. {
  638. float d = 1.0f / rcSqrt(rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2]));
  639. v[0] *= d;
  640. v[1] *= d;
  641. v[2] *= d;
  642. }
  643. /// @}
  644. /// @name Heightfield Functions
  645. /// @see rcHeightfield
  646. /// @{
  647. /// Calculates the bounding box of an array of vertices.
  648. /// @ingroup recast
  649. /// @param[in] verts An array of vertices. [(x, y, z) * @p nv]
  650. /// @param[in] nv The number of vertices in the @p verts array.
  651. /// @param[out] bmin The minimum bounds of the AABB. [(x, y, z)] [Units: wu]
  652. /// @param[out] bmax The maximum bounds of the AABB. [(x, y, z)] [Units: wu]
  653. void rcCalcBounds(const float* verts, int nv, float* bmin, float* bmax);
  654. /// Calculates the grid size based on the bounding box and grid cell size.
  655. /// @ingroup recast
  656. /// @param[in] bmin The minimum bounds of the AABB. [(x, y, z)] [Units: wu]
  657. /// @param[in] bmax The maximum bounds of the AABB. [(x, y, z)] [Units: wu]
  658. /// @param[in] cs The xz-plane cell size. [Limit: > 0] [Units: wu]
  659. /// @param[out] w The width along the x-axis. [Limit: >= 0] [Units: vx]
  660. /// @param[out] h The height along the z-axis. [Limit: >= 0] [Units: vx]
  661. void rcCalcGridSize(const float* bmin, const float* bmax, float cs, int* w, int* h);
  662. /// Initializes a new heightfield.
  663. /// @ingroup recast
  664. /// @param[in,out] ctx The build context to use during the operation.
  665. /// @param[in,out] hf The allocated heightfield to initialize.
  666. /// @param[in] width The width of the field along the x-axis. [Limit: >= 0] [Units: vx]
  667. /// @param[in] height The height of the field along the z-axis. [Limit: >= 0] [Units: vx]
  668. /// @param[in] bmin The minimum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  669. /// @param[in] bmax The maximum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  670. /// @param[in] cs The xz-plane cell size to use for the field. [Limit: > 0] [Units: wu]
  671. /// @param[in] ch The y-axis cell size to use for field. [Limit: > 0] [Units: wu]
  672. /// @returns True if the operation completed successfully.
  673. bool rcCreateHeightfield(rcContext* ctx, rcHeightfield& hf, int width, int height,
  674. const float* bmin, const float* bmax,
  675. float cs, float ch);
  676. /// Sets the area id of all triangles with a slope below the specified value
  677. /// to #RC_WALKABLE_AREA.
  678. /// @ingroup recast
  679. /// @param[in,out] ctx The build context to use during the operation.
  680. /// @param[in] walkableSlopeAngle The maximum slope that is considered walkable.
  681. /// [Limits: 0 <= value < 90] [Units: Degrees]
  682. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  683. /// @param[in] nv The number of vertices.
  684. /// @param[in] tris The triangle vertex indices. [(vertA, vertB, vertC) * @p nt]
  685. /// @param[in] nt The number of triangles.
  686. /// @param[out] areas The triangle area ids. [Length: >= @p nt]
  687. void rcMarkWalkableTriangles(rcContext* ctx, const float walkableSlopeAngle, const float* verts, int nv,
  688. const int* tris, int nt, unsigned char* areas);
  689. /// Sets the area id of all triangles with a slope greater than or equal to the specified value to #RC_NULL_AREA.
  690. /// @ingroup recast
  691. /// @param[in,out] ctx The build context to use during the operation.
  692. /// @param[in] walkableSlopeAngle The maximum slope that is considered walkable.
  693. /// [Limits: 0 <= value < 90] [Units: Degrees]
  694. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  695. /// @param[in] nv The number of vertices.
  696. /// @param[in] tris The triangle vertex indices. [(vertA, vertB, vertC) * @p nt]
  697. /// @param[in] nt The number of triangles.
  698. /// @param[out] areas The triangle area ids. [Length: >= @p nt]
  699. void rcClearUnwalkableTriangles(rcContext* ctx, const float walkableSlopeAngle, const float* verts, int nv,
  700. const int* tris, int nt, unsigned char* areas);
  701. /// Adds a span to the specified heightfield.
  702. /// @ingroup recast
  703. /// @param[in,out] ctx The build context to use during the operation.
  704. /// @param[in,out] hf An initialized heightfield.
  705. /// @param[in] x The width index where the span is to be added.
  706. /// [Limits: 0 <= value < rcHeightfield::width]
  707. /// @param[in] y The height index where the span is to be added.
  708. /// [Limits: 0 <= value < rcHeightfield::height]
  709. /// @param[in] smin The minimum height of the span. [Limit: < @p smax] [Units: vx]
  710. /// @param[in] smax The maximum height of the span. [Limit: <= #RC_SPAN_MAX_HEIGHT] [Units: vx]
  711. /// @param[in] area The area id of the span. [Limit: <= #RC_WALKABLE_AREA)
  712. /// @param[in] flagMergeThr The merge theshold. [Limit: >= 0] [Units: vx]
  713. /// @returns True if the operation completed successfully.
  714. bool rcAddSpan(rcContext* ctx, rcHeightfield& hf, const int x, const int y,
  715. const unsigned short smin, const unsigned short smax,
  716. const unsigned char area, const int flagMergeThr);
  717. /// Rasterizes a triangle into the specified heightfield.
  718. /// @ingroup recast
  719. /// @param[in,out] ctx The build context to use during the operation.
  720. /// @param[in] v0 Triangle vertex 0 [(x, y, z)]
  721. /// @param[in] v1 Triangle vertex 1 [(x, y, z)]
  722. /// @param[in] v2 Triangle vertex 2 [(x, y, z)]
  723. /// @param[in] area The area id of the triangle. [Limit: <= #RC_WALKABLE_AREA]
  724. /// @param[in,out] solid An initialized heightfield.
  725. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  726. /// [Limit: >= 0] [Units: vx]
  727. /// @returns True if the operation completed successfully.
  728. bool rcRasterizeTriangle(rcContext* ctx, const float* v0, const float* v1, const float* v2,
  729. const unsigned char area, rcHeightfield& solid,
  730. const int flagMergeThr = 1);
  731. /// Rasterizes an indexed triangle mesh into the specified heightfield.
  732. /// @ingroup recast
  733. /// @param[in,out] ctx The build context to use during the operation.
  734. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  735. /// @param[in] nv The number of vertices.
  736. /// @param[in] tris The triangle indices. [(vertA, vertB, vertC) * @p nt]
  737. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  738. /// @param[in] nt The number of triangles.
  739. /// @param[in,out] solid An initialized heightfield.
  740. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  741. /// [Limit: >= 0] [Units: vx]
  742. /// @returns True if the operation completed successfully.
  743. bool rcRasterizeTriangles(rcContext* ctx, const float* verts, const int nv,
  744. const int* tris, const unsigned char* areas, const int nt,
  745. rcHeightfield& solid, const int flagMergeThr = 1);
  746. /// Rasterizes an indexed triangle mesh into the specified heightfield.
  747. /// @ingroup recast
  748. /// @param[in,out] ctx The build context to use during the operation.
  749. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  750. /// @param[in] nv The number of vertices.
  751. /// @param[in] tris The triangle indices. [(vertA, vertB, vertC) * @p nt]
  752. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  753. /// @param[in] nt The number of triangles.
  754. /// @param[in,out] solid An initialized heightfield.
  755. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  756. /// [Limit: >= 0] [Units: vx]
  757. /// @returns True if the operation completed successfully.
  758. bool rcRasterizeTriangles(rcContext* ctx, const float* verts, const int nv,
  759. const unsigned short* tris, const unsigned char* areas, const int nt,
  760. rcHeightfield& solid, const int flagMergeThr = 1);
  761. /// Rasterizes triangles into the specified heightfield.
  762. /// @ingroup recast
  763. /// @param[in,out] ctx The build context to use during the operation.
  764. /// @param[in] verts The triangle vertices. [(ax, ay, az, bx, by, bz, cx, by, cx) * @p nt]
  765. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  766. /// @param[in] nt The number of triangles.
  767. /// @param[in,out] solid An initialized heightfield.
  768. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  769. /// [Limit: >= 0] [Units: vx]
  770. /// @returns True if the operation completed successfully.
  771. bool rcRasterizeTriangles(rcContext* ctx, const float* verts, const unsigned char* areas, const int nt,
  772. rcHeightfield& solid, const int flagMergeThr = 1);
  773. /// Marks non-walkable spans as walkable if their maximum is within @p walkableClimp of a walkable neihbor.
  774. /// @ingroup recast
  775. /// @param[in,out] ctx The build context to use during the operation.
  776. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  777. /// [Limit: >=0] [Units: vx]
  778. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  779. void rcFilterLowHangingWalkableObstacles(rcContext* ctx, const int walkableClimb, rcHeightfield& solid);
  780. /// Marks spans that are ledges as not-walkable.
  781. /// @ingroup recast
  782. /// @param[in,out] ctx The build context to use during the operation.
  783. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area to
  784. /// be considered walkable. [Limit: >= 3] [Units: vx]
  785. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  786. /// [Limit: >=0] [Units: vx]
  787. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  788. void rcFilterLedgeSpans(rcContext* ctx, const int walkableHeight,
  789. const int walkableClimb, rcHeightfield& solid);
  790. /// Marks walkable spans as not walkable if the clearence above the span is less than the specified height.
  791. /// @ingroup recast
  792. /// @param[in,out] ctx The build context to use during the operation.
  793. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area to
  794. /// be considered walkable. [Limit: >= 3] [Units: vx]
  795. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  796. void rcFilterWalkableLowHeightSpans(rcContext* ctx, int walkableHeight, rcHeightfield& solid);
  797. /// Returns the number of spans contained in the specified heightfield.
  798. /// @ingroup recast
  799. /// @param[in,out] ctx The build context to use during the operation.
  800. /// @param[in] hf An initialized heightfield.
  801. /// @returns The number of spans in the heightfield.
  802. int rcGetHeightFieldSpanCount(rcContext* ctx, rcHeightfield& hf);
  803. /// @}
  804. /// @name Compact Heightfield Functions
  805. /// @see rcCompactHeightfield
  806. /// @{
  807. /// Builds a compact heightfield representing open space, from a heightfield representing solid space.
  808. /// @ingroup recast
  809. /// @param[in,out] ctx The build context to use during the operation.
  810. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area
  811. /// to be considered walkable. [Limit: >= 3] [Units: vx]
  812. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  813. /// [Limit: >=0] [Units: vx]
  814. /// @param[in] hf The heightfield to be compacted.
  815. /// @param[out] chf The resulting compact heightfield. (Must be pre-allocated.)
  816. /// @returns True if the operation completed successfully.
  817. bool rcBuildCompactHeightfield(rcContext* ctx, const int walkableHeight, const int walkableClimb,
  818. rcHeightfield& hf, rcCompactHeightfield& chf);
  819. /// Erodes the walkable area within the heightfield by the specified radius.
  820. /// @ingroup recast
  821. /// @param[in,out] ctx The build context to use during the operation.
  822. /// @param[in] radius The radius of erosion. [Limits: 0 < value < 255] [Units: vx]
  823. /// @param[in,out] chf The populated compact heightfield to erode.
  824. /// @returns True if the operation completed successfully.
  825. bool rcErodeWalkableArea(rcContext* ctx, int radius, rcCompactHeightfield& chf);
  826. /// Applies a median filter to walkable area types (based on area id), removing noise.
  827. /// @ingroup recast
  828. /// @param[in,out] ctx The build context to use during the operation.
  829. /// @param[in,out] chf A populated compact heightfield.
  830. /// @returns True if the operation completed successfully.
  831. bool rcMedianFilterWalkableArea(rcContext* ctx, rcCompactHeightfield& chf);
  832. /// Applies an area id to all spans within the specified bounding box. (AABB)
  833. /// @ingroup recast
  834. /// @param[in,out] ctx The build context to use during the operation.
  835. /// @param[in] bmin The minimum of the bounding box. [(x, y, z)]
  836. /// @param[in] bmax The maximum of the bounding box. [(x, y, z)]
  837. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  838. /// @param[in,out] chf A populated compact heightfield.
  839. void rcMarkBoxArea(rcContext* ctx, const float* bmin, const float* bmax, unsigned char areaId,
  840. rcCompactHeightfield& chf);
  841. /// Applies the area id to the all spans within the specified convex polygon.
  842. /// @ingroup recast
  843. /// @param[in,out] ctx The build context to use during the operation.
  844. /// @param[in] verts The vertices of the polygon [Fomr: (x, y, z) * @p nverts]
  845. /// @param[in] nverts The number of vertices in the polygon.
  846. /// @param[in] hmin The height of the base of the polygon.
  847. /// @param[in] hmax The height of the top of the polygon.
  848. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  849. /// @param[in,out] chf A populated compact heightfield.
  850. void rcMarkConvexPolyArea(rcContext* ctx, const float* verts, const int nverts,
  851. const float hmin, const float hmax, unsigned char areaId,
  852. rcCompactHeightfield& chf);
  853. /// Helper function to offset voncex polygons for rcMarkConvexPolyArea.
  854. /// @ingroup recast
  855. /// @param[in] verts The vertices of the polygon [Form: (x, y, z) * @p nverts]
  856. /// @param[in] nverts The number of vertices in the polygon.
  857. /// @param[out] outVerts The offset vertices (should hold up to 2 * @p nverts) [Form: (x, y, z) * return value]
  858. /// @param[in] maxOutVerts The max number of vertices that can be stored to @p outVerts.
  859. /// @returns Number of vertices in the offset polygon or 0 if too few vertices in @p outVerts.
  860. int rcOffsetPoly(const float* verts, const int nverts, const float offset,
  861. float* outVerts, const int maxOutVerts);
  862. /// Applies the area id to all spans within the specified cylinder.
  863. /// @ingroup recast
  864. /// @param[in,out] ctx The build context to use during the operation.
  865. /// @param[in] pos The center of the base of the cylinder. [Form: (x, y, z)]
  866. /// @param[in] r The radius of the cylinder.
  867. /// @param[in] h The height of the cylinder.
  868. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  869. /// @param[in,out] chf A populated compact heightfield.
  870. void rcMarkCylinderArea(rcContext* ctx, const float* pos,
  871. const float r, const float h, unsigned char areaId,
  872. rcCompactHeightfield& chf);
  873. /// Builds the distance field for the specified compact heightfield.
  874. /// @ingroup recast
  875. /// @param[in,out] ctx The build context to use during the operation.
  876. /// @param[in,out] chf A populated compact heightfield.
  877. /// @returns True if the operation completed successfully.
  878. bool rcBuildDistanceField(rcContext* ctx, rcCompactHeightfield& chf);
  879. /// Builds region data for the heightfield using watershed partitioning.
  880. /// @ingroup recast
  881. /// @param[in,out] ctx The build context to use during the operation.
  882. /// @param[in,out] chf A populated compact heightfield.
  883. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  884. /// [Limit: >=0] [Units: vx]
  885. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  886. /// [Limit: >=0] [Units: vx].
  887. /// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
  888. /// be merged with larger regions. [Limit: >=0] [Units: vx]
  889. /// @returns True if the operation completed successfully.
  890. bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
  891. const int borderSize, const int minRegionArea, const int mergeRegionArea);
  892. /// Builds region data for the heightfield by partitioning the heightfield in non-overlapping layers.
  893. /// @ingroup recast
  894. /// @param[in,out] ctx The build context to use during the operation.
  895. /// @param[in,out] chf A populated compact heightfield.
  896. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  897. /// [Limit: >=0] [Units: vx]
  898. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  899. /// [Limit: >=0] [Units: vx].
  900. /// @returns True if the operation completed successfully.
  901. bool rcBuildLayerRegions(rcContext* ctx, rcCompactHeightfield& chf,
  902. const int borderSize, const int minRegionArea);
  903. /// Builds region data for the heightfield using simple monotone partitioning.
  904. /// @ingroup recast
  905. /// @param[in,out] ctx The build context to use during the operation.
  906. /// @param[in,out] chf A populated compact heightfield.
  907. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  908. /// [Limit: >=0] [Units: vx]
  909. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  910. /// [Limit: >=0] [Units: vx].
  911. /// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
  912. /// be merged with larger regions. [Limit: >=0] [Units: vx]
  913. /// @returns True if the operation completed successfully.
  914. bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
  915. const int borderSize, const int minRegionArea, const int mergeRegionArea);
  916. /// Sets the neighbor connection data for the specified direction.
  917. /// @param[in] s The span to update.
  918. /// @param[in] dir The direction to set. [Limits: 0 <= value < 4]
  919. /// @param[in] i The index of the neighbor span.
  920. inline void rcSetCon(rcCompactSpan& s, int dir, int i)
  921. {
  922. const unsigned int shift = (unsigned int)dir*6;
  923. unsigned int con = s.con;
  924. s.con = (con & ~(0x3f << shift)) | (((unsigned int)i & 0x3f) << shift);
  925. }
  926. /// Gets neighbor connection data for the specified direction.
  927. /// @param[in] s The span to check.
  928. /// @param[in] dir The direction to check. [Limits: 0 <= value < 4]
  929. /// @return The neighbor connection data for the specified direction,
  930. /// or #RC_NOT_CONNECTED if there is no connection.
  931. inline int rcGetCon(const rcCompactSpan& s, int dir)
  932. {
  933. const unsigned int shift = (unsigned int)dir*6;
  934. return (s.con >> shift) & 0x3f;
  935. }
  936. /// Gets the standard width (x-axis) offset for the specified direction.
  937. /// @param[in] dir The direction. [Limits: 0 <= value < 4]
  938. /// @return The width offset to apply to the current cell position to move
  939. /// in the direction.
  940. inline int rcGetDirOffsetX(int dir)
  941. {
  942. static const int offset[4] = { -1, 0, 1, 0, };
  943. return offset[dir&0x03];
  944. }
  945. /// Gets the standard height (z-axis) offset for the specified direction.
  946. /// @param[in] dir The direction. [Limits: 0 <= value < 4]
  947. /// @return The height offset to apply to the current cell position to move
  948. /// in the direction.
  949. inline int rcGetDirOffsetY(int dir)
  950. {
  951. static const int offset[4] = { 0, 1, 0, -1 };
  952. return offset[dir&0x03];
  953. }
  954. /// Gets the direction for the specified offset. One of x and y should be 0.
  955. /// @param[in] x The x offset. [Limits: -1 <= value <= 1]
  956. /// @param[in] y The y offset. [Limits: -1 <= value <= 1]
  957. /// @return The direction that represents the offset.
  958. inline int rcGetDirForOffset(int x, int y)
  959. {
  960. static const int dirs[5] = { 3, 0, -1, 2, 1 };
  961. return dirs[((y+1)<<1)+x];
  962. }
  963. /// @}
  964. /// @name Layer, Contour, Polymesh, and Detail Mesh Functions
  965. /// @see rcHeightfieldLayer, rcContourSet, rcPolyMesh, rcPolyMeshDetail
  966. /// @{
  967. /// Builds a layer set from the specified compact heightfield.
  968. /// @ingroup recast
  969. /// @param[in,out] ctx The build context to use during the operation.
  970. /// @param[in] chf A fully built compact heightfield.
  971. /// @param[in] borderSize The size of the non-navigable border around the heightfield. [Limit: >=0]
  972. /// [Units: vx]
  973. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area
  974. /// to be considered walkable. [Limit: >= 3] [Units: vx]
  975. /// @param[out] lset The resulting layer set. (Must be pre-allocated.)
  976. /// @returns True if the operation completed successfully.
  977. bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf,
  978. const int borderSize, const int walkableHeight,
  979. rcHeightfieldLayerSet& lset);
  980. /// Builds a contour set from the region outlines in the provided compact heightfield.
  981. /// @ingroup recast
  982. /// @param[in,out] ctx The build context to use during the operation.
  983. /// @param[in] chf A fully built compact heightfield.
  984. /// @param[in] maxError The maximum distance a simplfied contour's border edges should deviate
  985. /// the original raw contour. [Limit: >=0] [Units: wu]
  986. /// @param[in] maxEdgeLen The maximum allowed length for contour edges along the border of the mesh.
  987. /// [Limit: >=0] [Units: vx]
  988. /// @param[out] cset The resulting contour set. (Must be pre-allocated.)
  989. /// @param[in] buildFlags The build flags. (See: #rcBuildContoursFlags)
  990. /// @returns True if the operation completed successfully.
  991. bool rcBuildContours(rcContext* ctx, rcCompactHeightfield& chf,
  992. const float maxError, const int maxEdgeLen,
  993. rcContourSet& cset, const int buildFlags = RC_CONTOUR_TESS_WALL_EDGES);
  994. /// Builds a polygon mesh from the provided contours.
  995. /// @ingroup recast
  996. /// @param[in,out] ctx The build context to use during the operation.
  997. /// @param[in] cset A fully built contour set.
  998. /// @param[in] nvp The maximum number of vertices allowed for polygons generated during the
  999. /// contour to polygon conversion process. [Limit: >= 3]
  1000. /// @param[out] mesh The resulting polygon mesh. (Must be re-allocated.)
  1001. /// @returns True if the operation completed successfully.
  1002. bool rcBuildPolyMesh(rcContext* ctx, rcContourSet& cset, const int nvp, rcPolyMesh& mesh);
  1003. /// Merges multiple polygon meshes into a single mesh.
  1004. /// @ingroup recast
  1005. /// @param[in,out] ctx The build context to use during the operation.
  1006. /// @param[in] meshes An array of polygon meshes to merge. [Size: @p nmeshes]
  1007. /// @param[in] nmeshes The number of polygon meshes in the meshes array.
  1008. /// @param[in] mesh The resulting polygon mesh. (Must be pre-allocated.)
  1009. /// @returns True if the operation completed successfully.
  1010. bool rcMergePolyMeshes(rcContext* ctx, rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh);
  1011. /// Builds a detail mesh from the provided polygon mesh.
  1012. /// @ingroup recast
  1013. /// @param[in,out] ctx The build context to use during the operation.
  1014. /// @param[in] mesh A fully built polygon mesh.
  1015. /// @param[in] chf The compact heightfield used to build the polygon mesh.
  1016. /// @param[in] sampleDist Sets the distance to use when samping the heightfield. [Limit: >=0] [Units: wu]
  1017. /// @param[in] sampleMaxError The maximum distance the detail mesh surface should deviate from
  1018. /// heightfield data. [Limit: >=0] [Units: wu]
  1019. /// @param[out] dmesh The resulting detail mesh. (Must be pre-allocated.)
  1020. /// @returns True if the operation completed successfully.
  1021. bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompactHeightfield& chf,
  1022. const float sampleDist, const float sampleMaxError,
  1023. rcPolyMeshDetail& dmesh);
  1024. /// Copies the poly mesh data from src to dst.
  1025. /// @ingroup recast
  1026. /// @param[in,out] ctx The build context to use during the operation.
  1027. /// @param[in] src The source mesh to copy from.
  1028. /// @param[out] dst The resulting detail mesh. (Must be pre-allocated, must be empty mesh.)
  1029. /// @returns True if the operation completed successfully.
  1030. bool rcCopyPolyMesh(rcContext* ctx, const rcPolyMesh& src, rcPolyMesh& dst);
  1031. /// Merges multiple detail meshes into a single detail mesh.
  1032. /// @ingroup recast
  1033. /// @param[in,out] ctx The build context to use during the operation.
  1034. /// @param[in] meshes An array of detail meshes to merge. [Size: @p nmeshes]
  1035. /// @param[in] nmeshes The number of detail meshes in the meshes array.
  1036. /// @param[out] mesh The resulting detail mesh. (Must be pre-allocated.)
  1037. /// @returns True if the operation completed successfully.
  1038. bool rcMergePolyMeshDetails(rcContext* ctx, rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh);
  1039. /// @}
  1040. #endif // RECAST_H
  1041. ///////////////////////////////////////////////////////////////////////////
  1042. // Due to the large amount of detail documentation for this file,
  1043. // the content normally located at the end of the header file has been separated
  1044. // out to a file in /Docs/Extern.