Recast.h 60 KB

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