Recast.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef 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. rcHeightfield();
  247. ~rcHeightfield();
  248. int width; ///< The width of the heightfield. (Along the x-axis in cell units.)
  249. int height; ///< The height of the heightfield. (Along the z-axis in cell units.)
  250. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  251. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  252. float cs; ///< The size of each cell. (On the xz-plane.)
  253. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  254. rcSpan** spans; ///< Heightfield of spans (width*height).
  255. rcSpanPool* pools; ///< Linked list of span pools.
  256. rcSpan* freelist; ///< The next free span.
  257. private:
  258. // Explicitly-disabled copy constructor and copy assignment operator.
  259. rcHeightfield(const rcHeightfield&);
  260. rcHeightfield& operator=(const rcHeightfield&);
  261. };
  262. /// Provides information on the content of a cell column in a compact heightfield.
  263. struct rcCompactCell
  264. {
  265. unsigned int index : 24; ///< Index to the first span in the column.
  266. unsigned int count : 8; ///< Number of spans in the column.
  267. };
  268. /// Represents a span of unobstructed space within a compact heightfield.
  269. struct rcCompactSpan
  270. {
  271. unsigned short y; ///< The lower extent of the span. (Measured from the heightfield's base.)
  272. unsigned short reg; ///< The id of the region the span belongs to. (Or zero if not in a region.)
  273. unsigned int con : 24; ///< Packed neighbor connection data.
  274. unsigned int h : 8; ///< The height of the span. (Measured from #y.)
  275. };
  276. /// A compact, static heightfield representing unobstructed space.
  277. /// @ingroup recast
  278. struct rcCompactHeightfield
  279. {
  280. int width; ///< The width of the heightfield. (Along the x-axis in cell units.)
  281. int height; ///< The height of the heightfield. (Along the z-axis in cell units.)
  282. int spanCount; ///< The number of spans in the heightfield.
  283. int walkableHeight; ///< The walkable height used during the build of the field. (See: rcConfig::walkableHeight)
  284. int walkableClimb; ///< The walkable climb used during the build of the field. (See: rcConfig::walkableClimb)
  285. int borderSize; ///< The AABB border size used during the build of the field. (See: rcConfig::borderSize)
  286. unsigned short maxDistance; ///< The maximum distance value of any span within the field.
  287. unsigned short maxRegions; ///< The maximum region id of any span within the field.
  288. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  289. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  290. float cs; ///< The size of each cell. (On the xz-plane.)
  291. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  292. rcCompactCell* cells; ///< Array of cells. [Size: #width*#height]
  293. rcCompactSpan* spans; ///< Array of spans. [Size: #spanCount]
  294. unsigned short* dist; ///< Array containing border distance data. [Size: #spanCount]
  295. unsigned char* areas; ///< Array containing area id data. [Size: #spanCount]
  296. };
  297. /// Represents a heightfield layer within a layer set.
  298. /// @see rcHeightfieldLayerSet
  299. struct rcHeightfieldLayer
  300. {
  301. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  302. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  303. float cs; ///< The size of each cell. (On the xz-plane.)
  304. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  305. int width; ///< The width of the heightfield. (Along the x-axis in cell units.)
  306. int height; ///< The height of the heightfield. (Along the z-axis in cell units.)
  307. int minx; ///< The minimum x-bounds of usable data.
  308. int maxx; ///< The maximum x-bounds of usable data.
  309. int miny; ///< The minimum y-bounds of usable data. (Along the z-axis.)
  310. int maxy; ///< The maximum y-bounds of usable data. (Along the z-axis.)
  311. int hmin; ///< The minimum height bounds of usable data. (Along the y-axis.)
  312. int hmax; ///< The maximum height bounds of usable data. (Along the y-axis.)
  313. unsigned char* heights; ///< The heightfield. [Size: width * height]
  314. unsigned char* areas; ///< Area ids. [Size: Same as #heights]
  315. unsigned char* cons; ///< Packed neighbor connection information. [Size: Same as #heights]
  316. };
  317. /// Represents a set of heightfield layers.
  318. /// @ingroup recast
  319. /// @see rcAllocHeightfieldLayerSet, rcFreeHeightfieldLayerSet
  320. struct rcHeightfieldLayerSet
  321. {
  322. rcHeightfieldLayer* layers; ///< The layers in the set. [Size: #nlayers]
  323. int nlayers; ///< The number of layers in the set.
  324. };
  325. /// Represents a simple, non-overlapping contour in field space.
  326. struct rcContour
  327. {
  328. int* verts; ///< Simplified contour vertex and connection data. [Size: 4 * #nverts]
  329. int nverts; ///< The number of vertices in the simplified contour.
  330. int* rverts; ///< Raw contour vertex and connection data. [Size: 4 * #nrverts]
  331. int nrverts; ///< The number of vertices in the raw contour.
  332. unsigned short reg; ///< The region id of the contour.
  333. unsigned char area; ///< The area id of the contour.
  334. };
  335. /// Represents a group of related contours.
  336. /// @ingroup recast
  337. struct rcContourSet
  338. {
  339. rcContour* conts; ///< An array of the contours in the set. [Size: #nconts]
  340. int nconts; ///< The number of contours in the set.
  341. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  342. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  343. float cs; ///< The size of each cell. (On the xz-plane.)
  344. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  345. int width; ///< The width of the set. (Along the x-axis in cell units.)
  346. int height; ///< The height of the set. (Along the z-axis in cell units.)
  347. int borderSize; ///< The AABB border size used to generate the source data from which the contours were derived.
  348. float maxError; ///< The max edge error that this contour set was simplified with.
  349. };
  350. /// Represents a polygon mesh suitable for use in building a navigation mesh.
  351. /// @ingroup recast
  352. struct rcPolyMesh
  353. {
  354. unsigned short* verts; ///< The mesh vertices. [Form: (x, y, z) * #nverts]
  355. unsigned short* polys; ///< Polygon and neighbor data. [Length: #maxpolys * 2 * #nvp]
  356. unsigned short* regs; ///< The region id assigned to each polygon. [Length: #maxpolys]
  357. unsigned short* flags; ///< The user defined flags for each polygon. [Length: #maxpolys]
  358. unsigned char* areas; ///< The area id assigned to each polygon. [Length: #maxpolys]
  359. int nverts; ///< The number of vertices.
  360. int npolys; ///< The number of polygons.
  361. int maxpolys; ///< The number of allocated polygons.
  362. int nvp; ///< The maximum number of vertices per polygon.
  363. float bmin[3]; ///< The minimum bounds in world space. [(x, y, z)]
  364. float bmax[3]; ///< The maximum bounds in world space. [(x, y, z)]
  365. float cs; ///< The size of each cell. (On the xz-plane.)
  366. float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
  367. int borderSize; ///< The AABB border size used to generate the source data from which the mesh was derived.
  368. float maxEdgeError; ///< The max error of the polygon edges in the mesh.
  369. };
  370. /// Contains triangle meshes that represent detailed height data associated
  371. /// with the polygons in its associated polygon mesh object.
  372. /// @ingroup recast
  373. struct rcPolyMeshDetail
  374. {
  375. unsigned int* meshes; ///< The sub-mesh data. [Size: 4*#nmeshes]
  376. float* verts; ///< The mesh vertices. [Size: 3*#nverts]
  377. unsigned char* tris; ///< The mesh triangles. [Size: 4*#ntris]
  378. int nmeshes; ///< The number of sub-meshes defined by #meshes.
  379. int nverts; ///< The number of vertices in #verts.
  380. int ntris; ///< The number of triangles in #tris.
  381. };
  382. /// @name Allocation Functions
  383. /// Functions used to allocate and de-allocate Recast objects.
  384. /// @see rcAllocSetCustom
  385. /// @{
  386. /// Allocates a heightfield object using the Recast allocator.
  387. /// @return A heightfield that is ready for initialization, or null on failure.
  388. /// @ingroup recast
  389. /// @see rcCreateHeightfield, rcFreeHeightField
  390. rcHeightfield* rcAllocHeightfield();
  391. /// Frees the specified heightfield object using the Recast allocator.
  392. /// @param[in] hf A heightfield allocated using #rcAllocHeightfield
  393. /// @ingroup recast
  394. /// @see rcAllocHeightfield
  395. void rcFreeHeightField(rcHeightfield* hf);
  396. /// Allocates a compact heightfield object using the Recast allocator.
  397. /// @return A compact heightfield that is ready for initialization, or null on failure.
  398. /// @ingroup recast
  399. /// @see rcBuildCompactHeightfield, rcFreeCompactHeightfield
  400. rcCompactHeightfield* rcAllocCompactHeightfield();
  401. /// Frees the specified compact heightfield object using the Recast allocator.
  402. /// @param[in] chf A compact heightfield allocated using #rcAllocCompactHeightfield
  403. /// @ingroup recast
  404. /// @see rcAllocCompactHeightfield
  405. void rcFreeCompactHeightfield(rcCompactHeightfield* chf);
  406. /// Allocates a heightfield layer set using the Recast allocator.
  407. /// @return A heightfield layer set that is ready for initialization, or null on failure.
  408. /// @ingroup recast
  409. /// @see rcBuildHeightfieldLayers, rcFreeHeightfieldLayerSet
  410. rcHeightfieldLayerSet* rcAllocHeightfieldLayerSet();
  411. /// Frees the specified heightfield layer set using the Recast allocator.
  412. /// @param[in] lset A heightfield layer set allocated using #rcAllocHeightfieldLayerSet
  413. /// @ingroup recast
  414. /// @see rcAllocHeightfieldLayerSet
  415. void rcFreeHeightfieldLayerSet(rcHeightfieldLayerSet* lset);
  416. /// Allocates a contour set object using the Recast allocator.
  417. /// @return A contour set that is ready for initialization, or null on failure.
  418. /// @ingroup recast
  419. /// @see rcBuildContours, rcFreeContourSet
  420. rcContourSet* rcAllocContourSet();
  421. /// Frees the specified contour set using the Recast allocator.
  422. /// @param[in] cset A contour set allocated using #rcAllocContourSet
  423. /// @ingroup recast
  424. /// @see rcAllocContourSet
  425. void rcFreeContourSet(rcContourSet* cset);
  426. /// Allocates a polygon mesh object using the Recast allocator.
  427. /// @return A polygon mesh that is ready for initialization, or null on failure.
  428. /// @ingroup recast
  429. /// @see rcBuildPolyMesh, rcFreePolyMesh
  430. rcPolyMesh* rcAllocPolyMesh();
  431. /// Frees the specified polygon mesh using the Recast allocator.
  432. /// @param[in] pmesh A polygon mesh allocated using #rcAllocPolyMesh
  433. /// @ingroup recast
  434. /// @see rcAllocPolyMesh
  435. void rcFreePolyMesh(rcPolyMesh* pmesh);
  436. /// Allocates a detail mesh object using the Recast allocator.
  437. /// @return A detail mesh that is ready for initialization, or null on failure.
  438. /// @ingroup recast
  439. /// @see rcBuildPolyMeshDetail, rcFreePolyMeshDetail
  440. rcPolyMeshDetail* rcAllocPolyMeshDetail();
  441. /// Frees the specified detail mesh using the Recast allocator.
  442. /// @param[in] dmesh A detail mesh allocated using #rcAllocPolyMeshDetail
  443. /// @ingroup recast
  444. /// @see rcAllocPolyMeshDetail
  445. void rcFreePolyMeshDetail(rcPolyMeshDetail* dmesh);
  446. /// @}
  447. /// Heighfield border flag.
  448. /// If a heightfield region ID has this bit set, then the region is a border
  449. /// region and its spans are considered unwalkable.
  450. /// (Used during the region and contour build process.)
  451. /// @see rcCompactSpan::reg
  452. static const unsigned short RC_BORDER_REG = 0x8000;
  453. /// Polygon touches multiple regions.
  454. /// If a polygon has this region ID it was merged with or created
  455. /// from polygons of different regions during the polymesh
  456. /// build step that removes redundant border vertices.
  457. /// (Used during the polymesh and detail polymesh build processes)
  458. /// @see rcPolyMesh::regs
  459. static const unsigned short RC_MULTIPLE_REGS = 0;
  460. /// Border vertex flag.
  461. /// If a region ID has this bit set, then the associated element lies on
  462. /// a tile border. If a contour vertex's region ID has this bit set, the
  463. /// vertex will later be removed in order to match the segments and vertices
  464. /// at tile boundaries.
  465. /// (Used during the build process.)
  466. /// @see rcCompactSpan::reg, #rcContour::verts, #rcContour::rverts
  467. static const int RC_BORDER_VERTEX = 0x10000;
  468. /// Area border flag.
  469. /// If a region ID has this bit set, then the associated element lies on
  470. /// the border of an area.
  471. /// (Used during the region and contour build process.)
  472. /// @see rcCompactSpan::reg, #rcContour::verts, #rcContour::rverts
  473. static const int RC_AREA_BORDER = 0x20000;
  474. /// Contour build flags.
  475. /// @see rcBuildContours
  476. enum rcBuildContoursFlags
  477. {
  478. RC_CONTOUR_TESS_WALL_EDGES = 0x01, ///< Tessellate solid (impassable) edges during contour simplification.
  479. RC_CONTOUR_TESS_AREA_EDGES = 0x02, ///< Tessellate edges between areas during contour simplification.
  480. };
  481. /// Applied to the region id field of contour vertices in order to extract the region id.
  482. /// The region id field of a vertex may have several flags applied to it. So the
  483. /// fields value can't be used directly.
  484. /// @see rcContour::verts, rcContour::rverts
  485. static const int RC_CONTOUR_REG_MASK = 0xffff;
  486. /// An value which indicates an invalid index within a mesh.
  487. /// @note This does not necessarily indicate an error.
  488. /// @see rcPolyMesh::polys
  489. static const unsigned short RC_MESH_NULL_IDX = 0xffff;
  490. /// Represents the null area.
  491. /// When a data element is given this value it is considered to no longer be
  492. /// assigned to a usable area. (E.g. It is unwalkable.)
  493. static const unsigned char RC_NULL_AREA = 0;
  494. /// The default area id used to indicate a walkable polygon.
  495. /// This is also the maximum allowed area id, and the only non-null area id
  496. /// recognized by some steps in the build process.
  497. static const unsigned char RC_WALKABLE_AREA = 63;
  498. /// The value returned by #rcGetCon if the specified direction is not connected
  499. /// to another span. (Has no neighbor.)
  500. static const int RC_NOT_CONNECTED = 0x3f;
  501. /// @name General helper functions
  502. /// @{
  503. /// Used to ignore a function parameter. VS complains about unused parameters
  504. /// and this silences the warning.
  505. /// @param [in] _ Unused parameter
  506. template<class T> void rcIgnoreUnused(const T&) { }
  507. /// Swaps the values of the two parameters.
  508. /// @param[in,out] a Value A
  509. /// @param[in,out] b Value B
  510. template<class T> inline void rcSwap(T& a, T& b) { T t = a; a = b; b = t; }
  511. /// Returns the minimum of two values.
  512. /// @param[in] a Value A
  513. /// @param[in] b Value B
  514. /// @return The minimum of the two values.
  515. template<class T> inline T rcMin(T a, T b) { return a < b ? a : b; }
  516. /// Returns the maximum of two values.
  517. /// @param[in] a Value A
  518. /// @param[in] b Value B
  519. /// @return The maximum of the two values.
  520. template<class T> inline T rcMax(T a, T b) { return a > b ? a : b; }
  521. /// Returns the absolute value.
  522. /// @param[in] a The value.
  523. /// @return The absolute value of the specified value.
  524. template<class T> inline T rcAbs(T a) { return a < 0 ? -a : a; }
  525. /// Returns the square of the value.
  526. /// @param[in] a The value.
  527. /// @return The square of the value.
  528. template<class T> inline T rcSqr(T a) { return a*a; }
  529. /// Clamps the value to the specified range.
  530. /// @param[in] v The value to clamp.
  531. /// @param[in] mn The minimum permitted return value.
  532. /// @param[in] mx The maximum permitted return value.
  533. /// @return The value, clamped to the specified range.
  534. template<class T> inline T rcClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
  535. /// Returns the square root of the value.
  536. /// @param[in] x The value.
  537. /// @return The square root of the vlaue.
  538. float rcSqrt(float x);
  539. /// @}
  540. /// @name Vector helper functions.
  541. /// @{
  542. /// Derives the cross product of two vectors. (@p v1 x @p v2)
  543. /// @param[out] dest The cross product. [(x, y, z)]
  544. /// @param[in] v1 A Vector [(x, y, z)]
  545. /// @param[in] v2 A vector [(x, y, z)]
  546. inline void rcVcross(float* dest, const float* v1, const float* v2)
  547. {
  548. dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
  549. dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
  550. dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
  551. }
  552. /// Derives the dot product of two vectors. (@p v1 . @p v2)
  553. /// @param[in] v1 A Vector [(x, y, z)]
  554. /// @param[in] v2 A vector [(x, y, z)]
  555. /// @return The dot product.
  556. inline float rcVdot(const float* v1, const float* v2)
  557. {
  558. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  559. }
  560. /// Performs a scaled vector addition. (@p v1 + (@p v2 * @p s))
  561. /// @param[out] dest The result vector. [(x, y, z)]
  562. /// @param[in] v1 The base vector. [(x, y, z)]
  563. /// @param[in] v2 The vector to scale and add to @p v1. [(x, y, z)]
  564. /// @param[in] s The amount to scale @p v2 by before adding to @p v1.
  565. inline void rcVmad(float* dest, const float* v1, const float* v2, const float s)
  566. {
  567. dest[0] = v1[0]+v2[0]*s;
  568. dest[1] = v1[1]+v2[1]*s;
  569. dest[2] = v1[2]+v2[2]*s;
  570. }
  571. /// Performs a vector addition. (@p v1 + @p v2)
  572. /// @param[out] dest The result vector. [(x, y, z)]
  573. /// @param[in] v1 The base vector. [(x, y, z)]
  574. /// @param[in] v2 The vector to add to @p v1. [(x, y, z)]
  575. inline void rcVadd(float* dest, const float* v1, const float* v2)
  576. {
  577. dest[0] = v1[0]+v2[0];
  578. dest[1] = v1[1]+v2[1];
  579. dest[2] = v1[2]+v2[2];
  580. }
  581. /// Performs a vector subtraction. (@p v1 - @p v2)
  582. /// @param[out] dest The result vector. [(x, y, z)]
  583. /// @param[in] v1 The base vector. [(x, y, z)]
  584. /// @param[in] v2 The vector to subtract from @p v1. [(x, y, z)]
  585. inline void rcVsub(float* dest, const float* v1, const float* v2)
  586. {
  587. dest[0] = v1[0]-v2[0];
  588. dest[1] = v1[1]-v2[1];
  589. dest[2] = v1[2]-v2[2];
  590. }
  591. /// Selects the minimum value of each element from the specified vectors.
  592. /// @param[in,out] mn A vector. (Will be updated with the result.) [(x, y, z)]
  593. /// @param[in] v A vector. [(x, y, z)]
  594. inline void rcVmin(float* mn, const float* v)
  595. {
  596. mn[0] = rcMin(mn[0], v[0]);
  597. mn[1] = rcMin(mn[1], v[1]);
  598. mn[2] = rcMin(mn[2], v[2]);
  599. }
  600. /// Selects the maximum value of each element from the specified vectors.
  601. /// @param[in,out] mx A vector. (Will be updated with the result.) [(x, y, z)]
  602. /// @param[in] v A vector. [(x, y, z)]
  603. inline void rcVmax(float* mx, const float* v)
  604. {
  605. mx[0] = rcMax(mx[0], v[0]);
  606. mx[1] = rcMax(mx[1], v[1]);
  607. mx[2] = rcMax(mx[2], v[2]);
  608. }
  609. /// Performs a vector copy.
  610. /// @param[out] dest The result. [(x, y, z)]
  611. /// @param[in] v The vector to copy. [(x, y, z)]
  612. inline void rcVcopy(float* dest, const float* v)
  613. {
  614. dest[0] = v[0];
  615. dest[1] = v[1];
  616. dest[2] = v[2];
  617. }
  618. /// Returns the distance between two points.
  619. /// @param[in] v1 A point. [(x, y, z)]
  620. /// @param[in] v2 A point. [(x, y, z)]
  621. /// @return The distance between the two points.
  622. inline float rcVdist(const float* v1, const float* v2)
  623. {
  624. float dx = v2[0] - v1[0];
  625. float dy = v2[1] - v1[1];
  626. float dz = v2[2] - v1[2];
  627. return rcSqrt(dx*dx + dy*dy + dz*dz);
  628. }
  629. /// Returns the square of the distance between two points.
  630. /// @param[in] v1 A point. [(x, y, z)]
  631. /// @param[in] v2 A point. [(x, y, z)]
  632. /// @return The square of the distance between the two points.
  633. inline float rcVdistSqr(const float* v1, const float* v2)
  634. {
  635. float dx = v2[0] - v1[0];
  636. float dy = v2[1] - v1[1];
  637. float dz = v2[2] - v1[2];
  638. return dx*dx + dy*dy + dz*dz;
  639. }
  640. /// Normalizes the vector.
  641. /// @param[in,out] v The vector to normalize. [(x, y, z)]
  642. inline void rcVnormalize(float* v)
  643. {
  644. float d = 1.0f / rcSqrt(rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2]));
  645. v[0] *= d;
  646. v[1] *= d;
  647. v[2] *= d;
  648. }
  649. /// @}
  650. /// @name Heightfield Functions
  651. /// @see rcHeightfield
  652. /// @{
  653. /// Calculates the bounding box of an array of vertices.
  654. /// @ingroup recast
  655. /// @param[in] verts An array of vertices. [(x, y, z) * @p nv]
  656. /// @param[in] nv The number of vertices in the @p verts array.
  657. /// @param[out] bmin The minimum bounds of the AABB. [(x, y, z)] [Units: wu]
  658. /// @param[out] bmax The maximum bounds of the AABB. [(x, y, z)] [Units: wu]
  659. void rcCalcBounds(const float* verts, int nv, float* bmin, float* bmax);
  660. /// Calculates the grid size based on the bounding box and grid cell size.
  661. /// @ingroup recast
  662. /// @param[in] bmin The minimum bounds of the AABB. [(x, y, z)] [Units: wu]
  663. /// @param[in] bmax The maximum bounds of the AABB. [(x, y, z)] [Units: wu]
  664. /// @param[in] cs The xz-plane cell size. [Limit: > 0] [Units: wu]
  665. /// @param[out] w The width along the x-axis. [Limit: >= 0] [Units: vx]
  666. /// @param[out] h The height along the z-axis. [Limit: >= 0] [Units: vx]
  667. void rcCalcGridSize(const float* bmin, const float* bmax, float cs, int* w, int* h);
  668. /// Initializes a new heightfield.
  669. /// @ingroup recast
  670. /// @param[in,out] ctx The build context to use during the operation.
  671. /// @param[in,out] hf The allocated heightfield to initialize.
  672. /// @param[in] width The width of the field along the x-axis. [Limit: >= 0] [Units: vx]
  673. /// @param[in] height The height of the field along the z-axis. [Limit: >= 0] [Units: vx]
  674. /// @param[in] bmin The minimum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  675. /// @param[in] bmax The maximum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  676. /// @param[in] cs The xz-plane cell size to use for the field. [Limit: > 0] [Units: wu]
  677. /// @param[in] ch The y-axis cell size to use for field. [Limit: > 0] [Units: wu]
  678. /// @returns True if the operation completed successfully.
  679. bool rcCreateHeightfield(rcContext* ctx, rcHeightfield& hf, int width, int height,
  680. const float* bmin, const float* bmax,
  681. float cs, float ch);
  682. /// Sets the area id of all triangles with a slope below the specified value
  683. /// to #RC_WALKABLE_AREA.
  684. /// @ingroup recast
  685. /// @param[in,out] ctx The build context to use during the operation.
  686. /// @param[in] walkableSlopeAngle The maximum slope that is considered walkable.
  687. /// [Limits: 0 <= value < 90] [Units: Degrees]
  688. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  689. /// @param[in] nv The number of vertices.
  690. /// @param[in] tris The triangle vertex indices. [(vertA, vertB, vertC) * @p nt]
  691. /// @param[in] nt The number of triangles.
  692. /// @param[out] areas The triangle area ids. [Length: >= @p nt]
  693. void rcMarkWalkableTriangles(rcContext* ctx, const float walkableSlopeAngle, const float* verts, int nv,
  694. const int* tris, int nt, unsigned char* areas);
  695. /// Sets the area id of all triangles with a slope greater than or equal to the specified value to #RC_NULL_AREA.
  696. /// @ingroup recast
  697. /// @param[in,out] ctx The build context to use during the operation.
  698. /// @param[in] walkableSlopeAngle The maximum slope that is considered walkable.
  699. /// [Limits: 0 <= value < 90] [Units: Degrees]
  700. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  701. /// @param[in] nv The number of vertices.
  702. /// @param[in] tris The triangle vertex indices. [(vertA, vertB, vertC) * @p nt]
  703. /// @param[in] nt The number of triangles.
  704. /// @param[out] areas The triangle area ids. [Length: >= @p nt]
  705. void rcClearUnwalkableTriangles(rcContext* ctx, const float walkableSlopeAngle, const float* verts, int nv,
  706. const int* tris, int nt, unsigned char* areas);
  707. /// Adds a span to the specified heightfield.
  708. /// @ingroup recast
  709. /// @param[in,out] ctx The build context to use during the operation.
  710. /// @param[in,out] hf An initialized heightfield.
  711. /// @param[in] x The width index where the span is to be added.
  712. /// [Limits: 0 <= value < rcHeightfield::width]
  713. /// @param[in] y The height index where the span is to be added.
  714. /// [Limits: 0 <= value < rcHeightfield::height]
  715. /// @param[in] smin The minimum height of the span. [Limit: < @p smax] [Units: vx]
  716. /// @param[in] smax The maximum height of the span. [Limit: <= #RC_SPAN_MAX_HEIGHT] [Units: vx]
  717. /// @param[in] area The area id of the span. [Limit: <= #RC_WALKABLE_AREA)
  718. /// @param[in] flagMergeThr The merge theshold. [Limit: >= 0] [Units: vx]
  719. /// @returns True if the operation completed successfully.
  720. bool rcAddSpan(rcContext* ctx, rcHeightfield& hf, const int x, const int y,
  721. const unsigned short smin, const unsigned short smax,
  722. const unsigned char area, const int flagMergeThr);
  723. /// Rasterizes a triangle into the specified heightfield.
  724. /// @ingroup recast
  725. /// @param[in,out] ctx The build context to use during the operation.
  726. /// @param[in] v0 Triangle vertex 0 [(x, y, z)]
  727. /// @param[in] v1 Triangle vertex 1 [(x, y, z)]
  728. /// @param[in] v2 Triangle vertex 2 [(x, y, z)]
  729. /// @param[in] area The area id of the triangle. [Limit: <= #RC_WALKABLE_AREA]
  730. /// @param[in,out] solid An initialized heightfield.
  731. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  732. /// [Limit: >= 0] [Units: vx]
  733. /// @returns True if the operation completed successfully.
  734. bool rcRasterizeTriangle(rcContext* ctx, const float* v0, const float* v1, const float* v2,
  735. const unsigned char area, rcHeightfield& solid,
  736. const int flagMergeThr = 1);
  737. /// Rasterizes an indexed triangle mesh into the specified heightfield.
  738. /// @ingroup recast
  739. /// @param[in,out] ctx The build context to use during the operation.
  740. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  741. /// @param[in] nv The number of vertices.
  742. /// @param[in] tris The triangle indices. [(vertA, vertB, vertC) * @p nt]
  743. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  744. /// @param[in] nt The number of triangles.
  745. /// @param[in,out] solid An initialized heightfield.
  746. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  747. /// [Limit: >= 0] [Units: vx]
  748. /// @returns True if the operation completed successfully.
  749. bool rcRasterizeTriangles(rcContext* ctx, const float* verts, const int nv,
  750. const int* tris, const unsigned char* areas, const int nt,
  751. rcHeightfield& solid, const int flagMergeThr = 1);
  752. /// Rasterizes an indexed triangle mesh into the specified heightfield.
  753. /// @ingroup recast
  754. /// @param[in,out] ctx The build context to use during the operation.
  755. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  756. /// @param[in] nv The number of vertices.
  757. /// @param[in] tris The triangle indices. [(vertA, vertB, vertC) * @p nt]
  758. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  759. /// @param[in] nt The number of triangles.
  760. /// @param[in,out] solid An initialized heightfield.
  761. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  762. /// [Limit: >= 0] [Units: vx]
  763. /// @returns True if the operation completed successfully.
  764. bool rcRasterizeTriangles(rcContext* ctx, const float* verts, const int nv,
  765. const unsigned short* tris, const unsigned char* areas, const int nt,
  766. rcHeightfield& solid, const int flagMergeThr = 1);
  767. /// Rasterizes triangles into the specified heightfield.
  768. /// @ingroup recast
  769. /// @param[in,out] ctx The build context to use during the operation.
  770. /// @param[in] verts The triangle vertices. [(ax, ay, az, bx, by, bz, cx, by, cx) * @p nt]
  771. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  772. /// @param[in] nt The number of triangles.
  773. /// @param[in,out] solid An initialized heightfield.
  774. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  775. /// [Limit: >= 0] [Units: vx]
  776. /// @returns True if the operation completed successfully.
  777. bool rcRasterizeTriangles(rcContext* ctx, const float* verts, const unsigned char* areas, const int nt,
  778. rcHeightfield& solid, const int flagMergeThr = 1);
  779. /// Marks non-walkable spans as walkable if their maximum is within @p walkableClimp of a walkable neighbor.
  780. /// @ingroup recast
  781. /// @param[in,out] ctx The build context to use during the operation.
  782. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  783. /// [Limit: >=0] [Units: vx]
  784. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  785. void rcFilterLowHangingWalkableObstacles(rcContext* ctx, const int walkableClimb, rcHeightfield& solid);
  786. /// Marks spans that are ledges as not-walkable.
  787. /// @ingroup recast
  788. /// @param[in,out] ctx The build context to use during the operation.
  789. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area to
  790. /// be considered walkable. [Limit: >= 3] [Units: vx]
  791. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  792. /// [Limit: >=0] [Units: vx]
  793. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  794. void rcFilterLedgeSpans(rcContext* ctx, const int walkableHeight,
  795. const int walkableClimb, rcHeightfield& solid);
  796. /// Marks walkable spans as not walkable if the clearence above the span is less than the specified height.
  797. /// @ingroup recast
  798. /// @param[in,out] ctx The build context to use during the operation.
  799. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area to
  800. /// be considered walkable. [Limit: >= 3] [Units: vx]
  801. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  802. void rcFilterWalkableLowHeightSpans(rcContext* ctx, int walkableHeight, rcHeightfield& solid);
  803. /// Returns the number of spans contained in the specified heightfield.
  804. /// @ingroup recast
  805. /// @param[in,out] ctx The build context to use during the operation.
  806. /// @param[in] hf An initialized heightfield.
  807. /// @returns The number of spans in the heightfield.
  808. int rcGetHeightFieldSpanCount(rcContext* ctx, rcHeightfield& hf);
  809. /// @}
  810. /// @name Compact Heightfield Functions
  811. /// @see rcCompactHeightfield
  812. /// @{
  813. /// Builds a compact heightfield representing open space, from a heightfield representing solid space.
  814. /// @ingroup recast
  815. /// @param[in,out] ctx The build context to use during the operation.
  816. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area
  817. /// to be considered walkable. [Limit: >= 3] [Units: vx]
  818. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  819. /// [Limit: >=0] [Units: vx]
  820. /// @param[in] hf The heightfield to be compacted.
  821. /// @param[out] chf The resulting compact heightfield. (Must be pre-allocated.)
  822. /// @returns True if the operation completed successfully.
  823. bool rcBuildCompactHeightfield(rcContext* ctx, const int walkableHeight, const int walkableClimb,
  824. rcHeightfield& hf, rcCompactHeightfield& chf);
  825. /// Erodes the walkable area within the heightfield by the specified radius.
  826. /// @ingroup recast
  827. /// @param[in,out] ctx The build context to use during the operation.
  828. /// @param[in] radius The radius of erosion. [Limits: 0 < value < 255] [Units: vx]
  829. /// @param[in,out] chf The populated compact heightfield to erode.
  830. /// @returns True if the operation completed successfully.
  831. bool rcErodeWalkableArea(rcContext* ctx, int radius, rcCompactHeightfield& chf);
  832. /// Applies a median filter to walkable area types (based on area id), removing noise.
  833. /// @ingroup recast
  834. /// @param[in,out] ctx The build context to use during the operation.
  835. /// @param[in,out] chf A populated compact heightfield.
  836. /// @returns True if the operation completed successfully.
  837. bool rcMedianFilterWalkableArea(rcContext* ctx, rcCompactHeightfield& chf);
  838. /// Applies an area id to all spans within the specified bounding box. (AABB)
  839. /// @ingroup recast
  840. /// @param[in,out] ctx The build context to use during the operation.
  841. /// @param[in] bmin The minimum of the bounding box. [(x, y, z)]
  842. /// @param[in] bmax The maximum of the bounding box. [(x, y, z)]
  843. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  844. /// @param[in,out] chf A populated compact heightfield.
  845. void rcMarkBoxArea(rcContext* ctx, const float* bmin, const float* bmax, unsigned char areaId,
  846. rcCompactHeightfield& chf);
  847. /// Applies the area id to the all spans within the specified convex polygon.
  848. /// @ingroup recast
  849. /// @param[in,out] ctx The build context to use during the operation.
  850. /// @param[in] verts The vertices of the polygon [Fomr: (x, y, z) * @p nverts]
  851. /// @param[in] nverts The number of vertices in the polygon.
  852. /// @param[in] hmin The height of the base of the polygon.
  853. /// @param[in] hmax The height of the top of the polygon.
  854. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  855. /// @param[in,out] chf A populated compact heightfield.
  856. void rcMarkConvexPolyArea(rcContext* ctx, const float* verts, const int nverts,
  857. const float hmin, const float hmax, unsigned char areaId,
  858. rcCompactHeightfield& chf);
  859. /// Helper function to offset voncex polygons for rcMarkConvexPolyArea.
  860. /// @ingroup recast
  861. /// @param[in] verts The vertices of the polygon [Form: (x, y, z) * @p nverts]
  862. /// @param[in] nverts The number of vertices in the polygon.
  863. /// @param[out] outVerts The offset vertices (should hold up to 2 * @p nverts) [Form: (x, y, z) * return value]
  864. /// @param[in] maxOutVerts The max number of vertices that can be stored to @p outVerts.
  865. /// @returns Number of vertices in the offset polygon or 0 if too few vertices in @p outVerts.
  866. int rcOffsetPoly(const float* verts, const int nverts, const float offset,
  867. float* outVerts, const int maxOutVerts);
  868. /// Applies the area id to all spans within the specified cylinder.
  869. /// @ingroup recast
  870. /// @param[in,out] ctx The build context to use during the operation.
  871. /// @param[in] pos The center of the base of the cylinder. [Form: (x, y, z)]
  872. /// @param[in] r The radius of the cylinder.
  873. /// @param[in] h The height of the cylinder.
  874. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  875. /// @param[in,out] chf A populated compact heightfield.
  876. void rcMarkCylinderArea(rcContext* ctx, const float* pos,
  877. const float r, const float h, unsigned char areaId,
  878. rcCompactHeightfield& chf);
  879. /// Builds the distance field for the specified compact heightfield.
  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. /// @returns True if the operation completed successfully.
  884. bool rcBuildDistanceField(rcContext* ctx, rcCompactHeightfield& chf);
  885. /// Builds region data for the heightfield using watershed partitioning.
  886. /// @ingroup recast
  887. /// @param[in,out] ctx The build context to use during the operation.
  888. /// @param[in,out] chf A populated compact heightfield.
  889. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  890. /// [Limit: >=0] [Units: vx]
  891. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  892. /// [Limit: >=0] [Units: vx].
  893. /// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
  894. /// be merged with larger regions. [Limit: >=0] [Units: vx]
  895. /// @returns True if the operation completed successfully.
  896. bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
  897. const int borderSize, const int minRegionArea, const int mergeRegionArea);
  898. /// Builds region data for the heightfield by partitioning the heightfield in non-overlapping layers.
  899. /// @ingroup recast
  900. /// @param[in,out] ctx The build context to use during the operation.
  901. /// @param[in,out] chf A populated compact heightfield.
  902. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  903. /// [Limit: >=0] [Units: vx]
  904. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  905. /// [Limit: >=0] [Units: vx].
  906. /// @returns True if the operation completed successfully.
  907. bool rcBuildLayerRegions(rcContext* ctx, rcCompactHeightfield& chf,
  908. const int borderSize, const int minRegionArea);
  909. /// Builds region data for the heightfield using simple monotone partitioning.
  910. /// @ingroup recast
  911. /// @param[in,out] ctx The build context to use during the operation.
  912. /// @param[in,out] chf A populated compact heightfield.
  913. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  914. /// [Limit: >=0] [Units: vx]
  915. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  916. /// [Limit: >=0] [Units: vx].
  917. /// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
  918. /// be merged with larger regions. [Limit: >=0] [Units: vx]
  919. /// @returns True if the operation completed successfully.
  920. bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
  921. const int borderSize, const int minRegionArea, const int mergeRegionArea);
  922. /// Sets the neighbor connection data for the specified direction.
  923. /// @param[in] s The span to update.
  924. /// @param[in] dir The direction to set. [Limits: 0 <= value < 4]
  925. /// @param[in] i The index of the neighbor span.
  926. inline void rcSetCon(rcCompactSpan& s, int dir, int i)
  927. {
  928. const unsigned int shift = (unsigned int)dir*6;
  929. unsigned int con = s.con;
  930. s.con = (con & ~(0x3f << shift)) | (((unsigned int)i & 0x3f) << shift);
  931. }
  932. /// Gets neighbor connection data for the specified direction.
  933. /// @param[in] s The span to check.
  934. /// @param[in] dir The direction to check. [Limits: 0 <= value < 4]
  935. /// @return The neighbor connection data for the specified direction,
  936. /// or #RC_NOT_CONNECTED if there is no connection.
  937. inline int rcGetCon(const rcCompactSpan& s, int dir)
  938. {
  939. const unsigned int shift = (unsigned int)dir*6;
  940. return (s.con >> shift) & 0x3f;
  941. }
  942. /// Gets the standard width (x-axis) offset for the specified direction.
  943. /// @param[in] dir The direction. [Limits: 0 <= value < 4]
  944. /// @return The width offset to apply to the current cell position to move
  945. /// in the direction.
  946. inline int rcGetDirOffsetX(int dir)
  947. {
  948. static const int offset[4] = { -1, 0, 1, 0, };
  949. return offset[dir&0x03];
  950. }
  951. /// Gets the standard height (z-axis) offset for the specified direction.
  952. /// @param[in] dir The direction. [Limits: 0 <= value < 4]
  953. /// @return The height offset to apply to the current cell position to move
  954. /// in the direction.
  955. inline int rcGetDirOffsetY(int dir)
  956. {
  957. static const int offset[4] = { 0, 1, 0, -1 };
  958. return offset[dir&0x03];
  959. }
  960. /// Gets the direction for the specified offset. One of x and y should be 0.
  961. /// @param[in] x The x offset. [Limits: -1 <= value <= 1]
  962. /// @param[in] y The y offset. [Limits: -1 <= value <= 1]
  963. /// @return The direction that represents the offset.
  964. inline int rcGetDirForOffset(int x, int y)
  965. {
  966. static const int dirs[5] = { 3, 0, -1, 2, 1 };
  967. return dirs[((y+1)<<1)+x];
  968. }
  969. /// @}
  970. /// @name Layer, Contour, Polymesh, and Detail Mesh Functions
  971. /// @see rcHeightfieldLayer, rcContourSet, rcPolyMesh, rcPolyMeshDetail
  972. /// @{
  973. /// Builds a layer set from the specified compact heightfield.
  974. /// @ingroup recast
  975. /// @param[in,out] ctx The build context to use during the operation.
  976. /// @param[in] chf A fully built compact heightfield.
  977. /// @param[in] borderSize The size of the non-navigable border around the heightfield. [Limit: >=0]
  978. /// [Units: vx]
  979. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area
  980. /// to be considered walkable. [Limit: >= 3] [Units: vx]
  981. /// @param[out] lset The resulting layer set. (Must be pre-allocated.)
  982. /// @returns True if the operation completed successfully.
  983. bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf,
  984. const int borderSize, const int walkableHeight,
  985. rcHeightfieldLayerSet& lset);
  986. /// Builds a contour set from the region outlines in the provided compact heightfield.
  987. /// @ingroup recast
  988. /// @param[in,out] ctx The build context to use during the operation.
  989. /// @param[in] chf A fully built compact heightfield.
  990. /// @param[in] maxError The maximum distance a simplfied contour's border edges should deviate
  991. /// the original raw contour. [Limit: >=0] [Units: wu]
  992. /// @param[in] maxEdgeLen The maximum allowed length for contour edges along the border of the mesh.
  993. /// [Limit: >=0] [Units: vx]
  994. /// @param[out] cset The resulting contour set. (Must be pre-allocated.)
  995. /// @param[in] buildFlags The build flags. (See: #rcBuildContoursFlags)
  996. /// @returns True if the operation completed successfully.
  997. bool rcBuildContours(rcContext* ctx, rcCompactHeightfield& chf,
  998. const float maxError, const int maxEdgeLen,
  999. rcContourSet& cset, const int buildFlags = RC_CONTOUR_TESS_WALL_EDGES);
  1000. /// Builds a polygon mesh from the provided contours.
  1001. /// @ingroup recast
  1002. /// @param[in,out] ctx The build context to use during the operation.
  1003. /// @param[in] cset A fully built contour set.
  1004. /// @param[in] nvp The maximum number of vertices allowed for polygons generated during the
  1005. /// contour to polygon conversion process. [Limit: >= 3]
  1006. /// @param[out] mesh The resulting polygon mesh. (Must be re-allocated.)
  1007. /// @returns True if the operation completed successfully.
  1008. bool rcBuildPolyMesh(rcContext* ctx, rcContourSet& cset, const int nvp, rcPolyMesh& mesh);
  1009. /// Merges multiple polygon meshes into a single mesh.
  1010. /// @ingroup recast
  1011. /// @param[in,out] ctx The build context to use during the operation.
  1012. /// @param[in] meshes An array of polygon meshes to merge. [Size: @p nmeshes]
  1013. /// @param[in] nmeshes The number of polygon meshes in the meshes array.
  1014. /// @param[in] mesh The resulting polygon mesh. (Must be pre-allocated.)
  1015. /// @returns True if the operation completed successfully.
  1016. bool rcMergePolyMeshes(rcContext* ctx, rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh);
  1017. /// Builds a detail mesh from the provided polygon mesh.
  1018. /// @ingroup recast
  1019. /// @param[in,out] ctx The build context to use during the operation.
  1020. /// @param[in] mesh A fully built polygon mesh.
  1021. /// @param[in] chf The compact heightfield used to build the polygon mesh.
  1022. /// @param[in] sampleDist Sets the distance to use when samping the heightfield. [Limit: >=0] [Units: wu]
  1023. /// @param[in] sampleMaxError The maximum distance the detail mesh surface should deviate from
  1024. /// heightfield data. [Limit: >=0] [Units: wu]
  1025. /// @param[out] dmesh The resulting detail mesh. (Must be pre-allocated.)
  1026. /// @returns True if the operation completed successfully.
  1027. bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompactHeightfield& chf,
  1028. const float sampleDist, const float sampleMaxError,
  1029. rcPolyMeshDetail& dmesh);
  1030. /// Copies the poly mesh data from src to dst.
  1031. /// @ingroup recast
  1032. /// @param[in,out] ctx The build context to use during the operation.
  1033. /// @param[in] src The source mesh to copy from.
  1034. /// @param[out] dst The resulting detail mesh. (Must be pre-allocated, must be empty mesh.)
  1035. /// @returns True if the operation completed successfully.
  1036. bool rcCopyPolyMesh(rcContext* ctx, const rcPolyMesh& src, rcPolyMesh& dst);
  1037. /// Merges multiple detail meshes into a single detail mesh.
  1038. /// @ingroup recast
  1039. /// @param[in,out] ctx The build context to use during the operation.
  1040. /// @param[in] meshes An array of detail meshes to merge. [Size: @p nmeshes]
  1041. /// @param[in] nmeshes The number of detail meshes in the meshes array.
  1042. /// @param[out] mesh The resulting detail mesh. (Must be pre-allocated.)
  1043. /// @returns True if the operation completed successfully.
  1044. bool rcMergePolyMeshDetails(rcContext* ctx, rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh);
  1045. /// @}
  1046. #endif // RECAST_H
  1047. ///////////////////////////////////////////////////////////////////////////
  1048. // Due to the large amount of detail documentation for this file,
  1049. // the content normally located at the end of the header file has been separated
  1050. // out to a file in /Docs/Extern.