Recast.h 52 KB

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