Recast.h 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef RECAST_H
  19. #define RECAST_H
  20. /// The value of PI used by Recast.
  21. static const float RC_PI = 3.14159265f;
  22. /// Recast log categories.
  23. /// @see rcContext
  24. enum rcLogCategory
  25. {
  26. RC_LOG_PROGRESS = 1, ///< A progress log entry.
  27. RC_LOG_WARNING, ///< A warning log entry.
  28. RC_LOG_ERROR, ///< An error log entry.
  29. };
  30. /// Recast performance timer categories.
  31. /// @see rcContext
  32. enum rcTimerLabel
  33. {
  34. /// The user defined total time of the build.
  35. RC_TIMER_TOTAL,
  36. /// A user defined build time.
  37. RC_TIMER_TEMP,
  38. /// The time to rasterize the triangles. (See: #rcRasterizeTriangle)
  39. RC_TIMER_RASTERIZE_TRIANGLES,
  40. /// The time to build the compact heightfield. (See: #rcBuildCompactHeightfield)
  41. RC_TIMER_BUILD_COMPACTHEIGHTFIELD,
  42. /// The total time to build the contours. (See: #rcBuildContours)
  43. RC_TIMER_BUILD_CONTOURS,
  44. /// The time to trace the boundaries of the contours. (See: #rcBuildContours)
  45. RC_TIMER_BUILD_CONTOURS_TRACE,
  46. /// The time to simplify the contours. (See: #rcBuildContours)
  47. RC_TIMER_BUILD_CONTOURS_SIMPLIFY,
  48. /// The time to filter ledge spans. (See: #rcFilterLedgeSpans)
  49. RC_TIMER_FILTER_BORDER,
  50. /// The time to filter low height spans. (See: #rcFilterWalkableLowHeightSpans)
  51. RC_TIMER_FILTER_WALKABLE,
  52. /// The time to apply the median filter. (See: #rcMedianFilterWalkableArea)
  53. RC_TIMER_MEDIAN_AREA,
  54. /// The time to filter low obstacles. (See: #rcFilterLowHangingWalkableObstacles)
  55. RC_TIMER_FILTER_LOW_OBSTACLES,
  56. /// The time to build the polygon mesh. (See: #rcBuildPolyMesh)
  57. RC_TIMER_BUILD_POLYMESH,
  58. /// The time to merge polygon meshes. (See: #rcMergePolyMeshes)
  59. RC_TIMER_MERGE_POLYMESH,
  60. /// The time to erode the walkable area. (See: #rcErodeWalkableArea)
  61. RC_TIMER_ERODE_AREA,
  62. /// The time to mark a box area. (See: #rcMarkBoxArea)
  63. RC_TIMER_MARK_BOX_AREA,
  64. /// The time to mark a cylinder area. (See: #rcMarkCylinderArea)
  65. RC_TIMER_MARK_CYLINDER_AREA,
  66. /// The time to mark a convex polygon area. (See: #rcMarkConvexPolyArea)
  67. RC_TIMER_MARK_CONVEXPOLY_AREA,
  68. /// The total time to build the distance field. (See: #rcBuildDistanceField)
  69. RC_TIMER_BUILD_DISTANCEFIELD,
  70. /// The time to build the distances of the distance field. (See: #rcBuildDistanceField)
  71. RC_TIMER_BUILD_DISTANCEFIELD_DIST,
  72. /// The time to blur the distance field. (See: #rcBuildDistanceField)
  73. RC_TIMER_BUILD_DISTANCEFIELD_BLUR,
  74. /// The total time to build the regions. (See: #rcBuildRegions, #rcBuildRegionsMonotone)
  75. RC_TIMER_BUILD_REGIONS,
  76. /// The total time to apply the watershed algorithm. (See: #rcBuildRegions)
  77. RC_TIMER_BUILD_REGIONS_WATERSHED,
  78. /// The time to expand regions while applying the watershed algorithm. (See: #rcBuildRegions)
  79. RC_TIMER_BUILD_REGIONS_EXPAND,
  80. /// The time to flood regions while applying the watershed algorithm. (See: #rcBuildRegions)
  81. RC_TIMER_BUILD_REGIONS_FLOOD,
  82. /// The time to filter out small regions. (See: #rcBuildRegions, #rcBuildRegionsMonotone)
  83. RC_TIMER_BUILD_REGIONS_FILTER,
  84. /// The time to build heightfield layers. (See: #rcBuildHeightfieldLayers)
  85. RC_TIMER_BUILD_LAYERS,
  86. /// The time to build the polygon mesh detail. (See: #rcBuildPolyMeshDetail)
  87. RC_TIMER_BUILD_POLYMESHDETAIL,
  88. /// The time to merge polygon mesh details. (See: #rcMergePolyMeshDetails)
  89. RC_TIMER_MERGE_POLYMESHDETAIL,
  90. /// The maximum number of timers. (Used for iterating timers.)
  91. RC_MAX_TIMERS
  92. };
  93. /// Provides an interface for optional logging and performance tracking of the Recast
  94. /// build process.
  95. /// @ingroup recast
  96. class rcContext
  97. {
  98. public:
  99. /// Contructor.
  100. /// @param[in] state TRUE if the logging and performance timers should be enabled. [Default: true]
  101. inline rcContext(bool state = true) : m_logEnabled(state), m_timerEnabled(state) {}
  102. virtual ~rcContext() {}
  103. /// Enables or disables logging.
  104. /// @param[in] state TRUE if logging should be enabled.
  105. inline void enableLog(bool state) { m_logEnabled = state; }
  106. /// Clears all log entries.
  107. inline void resetLog() { if (m_logEnabled) doResetLog(); }
  108. /// Logs a message.
  109. /// @param[in] category The category of the message.
  110. /// @param[in] format The message.
  111. void log(const rcLogCategory category, const char* format, ...);
  112. /// Enables or disables the performance timers.
  113. /// @param[in] state TRUE if timers should be enabled.
  114. inline void enableTimer(bool state) { m_timerEnabled = state; }
  115. /// Clears all peformance timers. (Resets all to unused.)
  116. inline void resetTimers() { if (m_timerEnabled) doResetTimers(); }
  117. /// Starts the specified performance timer.
  118. /// @param label The category of 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: wu]
  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 - borderSize*2) * (h - borderSize*2)]
  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. /// Swaps the values of the two parameters.
  472. /// @param[in,out] a Value A
  473. /// @param[in,out] b Value B
  474. template<class T> inline void rcSwap(T& a, T& b) { T t = a; a = b; b = t; }
  475. /// Returns the minimum of two values.
  476. /// @param[in] a Value A
  477. /// @param[in] b Value B
  478. /// @return The minimum of the two values.
  479. template<class T> inline T rcMin(T a, T b) { return a < b ? a : b; }
  480. /// Returns the maximum of two values.
  481. /// @param[in] a Value A
  482. /// @param[in] b Value B
  483. /// @return The maximum of the two values.
  484. template<class T> inline T rcMax(T a, T b) { return a > b ? a : b; }
  485. /// Returns the absolute value.
  486. /// @param[in] a The value.
  487. /// @return The absolute value of the specified value.
  488. template<class T> inline T rcAbs(T a) { return a < 0 ? -a : a; }
  489. /// Returns the square of the value.
  490. /// @param[in] a The value.
  491. /// @return The square of the value.
  492. template<class T> inline T rcSqr(T a) { return a*a; }
  493. /// Clamps the value to the specified range.
  494. /// @param[in] v The value to clamp.
  495. /// @param[in] mn The minimum permitted return value.
  496. /// @param[in] mx The maximum permitted return value.
  497. /// @return The value, clamped to the specified range.
  498. template<class T> inline T rcClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
  499. /// Returns the square root of the value.
  500. /// @param[in] x The value.
  501. /// @return The square root of the vlaue.
  502. float rcSqrt(float x);
  503. /// @}
  504. /// @name Vector helper functions.
  505. /// @{
  506. /// Derives the cross product of two vectors. (@p v1 x @p v2)
  507. /// @param[out] dest The cross product. [(x, y, z)]
  508. /// @param[in] v1 A Vector [(x, y, z)]
  509. /// @param[in] v2 A vector [(x, y, z)]
  510. inline void rcVcross(float* dest, const float* v1, const float* v2)
  511. {
  512. dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
  513. dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
  514. dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
  515. }
  516. /// Derives the dot product of two vectors. (@p v1 . @p v2)
  517. /// @param[in] v1 A Vector [(x, y, z)]
  518. /// @param[in] v2 A vector [(x, y, z)]
  519. /// @return The dot product.
  520. inline float rcVdot(const float* v1, const float* v2)
  521. {
  522. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  523. }
  524. /// Performs a scaled vector addition. (@p v1 + (@p v2 * @p s))
  525. /// @param[out] dest The result vector. [(x, y, z)]
  526. /// @param[in] v1 The base vector. [(x, y, z)]
  527. /// @param[in] v2 The vector to scale and add to @p v1. [(x, y, z)]
  528. /// @param[in] s The amount to scale @p v2 by before adding to @p v1.
  529. inline void rcVmad(float* dest, const float* v1, const float* v2, const float s)
  530. {
  531. dest[0] = v1[0]+v2[0]*s;
  532. dest[1] = v1[1]+v2[1]*s;
  533. dest[2] = v1[2]+v2[2]*s;
  534. }
  535. /// Performs a vector addition. (@p v1 + @p v2)
  536. /// @param[out] dest The result vector. [(x, y, z)]
  537. /// @param[in] v1 The base vector. [(x, y, z)]
  538. /// @param[in] v2 The vector to add to @p v1. [(x, y, z)]
  539. inline void rcVadd(float* dest, const float* v1, const float* v2)
  540. {
  541. dest[0] = v1[0]+v2[0];
  542. dest[1] = v1[1]+v2[1];
  543. dest[2] = v1[2]+v2[2];
  544. }
  545. /// Performs a vector subtraction. (@p v1 - @p v2)
  546. /// @param[out] dest The result vector. [(x, y, z)]
  547. /// @param[in] v1 The base vector. [(x, y, z)]
  548. /// @param[in] v2 The vector to subtract from @p v1. [(x, y, z)]
  549. inline void rcVsub(float* dest, const float* v1, const float* v2)
  550. {
  551. dest[0] = v1[0]-v2[0];
  552. dest[1] = v1[1]-v2[1];
  553. dest[2] = v1[2]-v2[2];
  554. }
  555. /// Selects the minimum value of each element from the specified vectors.
  556. /// @param[in,out] mn A vector. (Will be updated with the result.) [(x, y, z)]
  557. /// @param[in] v A vector. [(x, y, z)]
  558. inline void rcVmin(float* mn, const float* v)
  559. {
  560. mn[0] = rcMin(mn[0], v[0]);
  561. mn[1] = rcMin(mn[1], v[1]);
  562. mn[2] = rcMin(mn[2], v[2]);
  563. }
  564. /// Selects the maximum value of each element from the specified vectors.
  565. /// @param[in,out] mx A vector. (Will be updated with the result.) [(x, y, z)]
  566. /// @param[in] v A vector. [(x, y, z)]
  567. inline void rcVmax(float* mx, const float* v)
  568. {
  569. mx[0] = rcMax(mx[0], v[0]);
  570. mx[1] = rcMax(mx[1], v[1]);
  571. mx[2] = rcMax(mx[2], v[2]);
  572. }
  573. /// Performs a vector copy.
  574. /// @param[out] dest The result. [(x, y, z)]
  575. /// @param[in] v The vector to copy. [(x, y, z)]
  576. inline void rcVcopy(float* dest, const float* v)
  577. {
  578. dest[0] = v[0];
  579. dest[1] = v[1];
  580. dest[2] = v[2];
  581. }
  582. /// Returns the distance between two points.
  583. /// @param[in] v1 A point. [(x, y, z)]
  584. /// @param[in] v2 A point. [(x, y, z)]
  585. /// @return The distance between the two points.
  586. inline float rcVdist(const float* v1, const float* v2)
  587. {
  588. float dx = v2[0] - v1[0];
  589. float dy = v2[1] - v1[1];
  590. float dz = v2[2] - v1[2];
  591. return rcSqrt(dx*dx + dy*dy + dz*dz);
  592. }
  593. /// Returns the square of the distance between two points.
  594. /// @param[in] v1 A point. [(x, y, z)]
  595. /// @param[in] v2 A point. [(x, y, z)]
  596. /// @return The square of the distance between the two points.
  597. inline float rcVdistSqr(const float* v1, const float* v2)
  598. {
  599. float dx = v2[0] - v1[0];
  600. float dy = v2[1] - v1[1];
  601. float dz = v2[2] - v1[2];
  602. return dx*dx + dy*dy + dz*dz;
  603. }
  604. /// Normalizes the vector.
  605. /// @param[in,out] v The vector to normalize. [(x, y, z)]
  606. inline void rcVnormalize(float* v)
  607. {
  608. float d = 1.0f / rcSqrt(rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2]));
  609. v[0] *= d;
  610. v[1] *= d;
  611. v[2] *= d;
  612. }
  613. /// @}
  614. /// @name Heightfield Functions
  615. /// @see rcHeightfield
  616. /// @{
  617. /// Calculates the bounding box of an array of vertices.
  618. /// @ingroup recast
  619. /// @param[in] verts An array of vertices. [(x, y, z) * @p nv]
  620. /// @param[in] nv The number of vertices in the @p verts array.
  621. /// @param[out] bmin The minimum bounds of the AABB. [(x, y, z)] [Units: wu]
  622. /// @param[out] bmax The maximum bounds of the AABB. [(x, y, z)] [Units: wu]
  623. void rcCalcBounds(const float* verts, int nv, float* bmin, float* bmax);
  624. /// Calculates the grid size based on the bounding box and grid cell size.
  625. /// @ingroup recast
  626. /// @param[in] bmin The minimum bounds of the AABB. [(x, y, z)] [Units: wu]
  627. /// @param[in] bmax The maximum bounds of the AABB. [(x, y, z)] [Units: wu]
  628. /// @param[in] cs The xz-plane cell size. [Limit: > 0] [Units: wu]
  629. /// @param[out] w The width along the x-axis. [Limit: >= 0] [Units: vx]
  630. /// @param[out] h The height along the z-axis. [Limit: >= 0] [Units: vx]
  631. void rcCalcGridSize(const float* bmin, const float* bmax, float cs, int* w, int* h);
  632. /// Initializes a new heightfield.
  633. /// @ingroup recast
  634. /// @param[in,out] ctx The build context to use during the operation.
  635. /// @param[in,out] hf The allocated heightfield to initialize.
  636. /// @param[in] width The width of the field along the x-axis. [Limit: >= 0] [Units: vx]
  637. /// @param[in] height The height of the field along the z-axis. [Limit: >= 0] [Units: vx]
  638. /// @param[in] bmin The minimum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  639. /// @param[in] bmax The maximum bounds of the field's AABB. [(x, y, z)] [Units: wu]
  640. /// @param[in] cs The xz-plane cell size to use for the field. [Limit: > 0] [Units: wu]
  641. /// @param[in] ch The y-axis cell size to use for field. [Limit: > 0] [Units: wu]
  642. bool rcCreateHeightfield(rcContext* ctx, rcHeightfield& hf, int width, int height,
  643. const float* bmin, const float* bmax,
  644. float cs, float ch);
  645. /// Sets the area id of all triangles with a slope below the specified value
  646. /// to #RC_WALKABLE_AREA.
  647. /// @ingroup recast
  648. /// @param[in,out] ctx The build context to use during the operation.
  649. /// @param[in] walkableSlopeAngle The maximum slope that is considered walkable.
  650. /// [Limits: 0 <= value < 90] [Units: Degrees]
  651. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  652. /// @param[in] nv The number of vertices.
  653. /// @param[in] tris The triangle vertex indices. [(vertA, vertB, vertC) * @p nt]
  654. /// @param[in] nt The number of triangles.
  655. /// @param[out] areas The triangle area ids. [Length: >= @p nt]
  656. void rcMarkWalkableTriangles(rcContext* ctx, const float walkableSlopeAngle, const float* verts, int nv,
  657. const int* tris, int nt, unsigned char* areas);
  658. /// Sets the area id of all triangles with a slope greater than or equal to the specified value to #RC_NULL_AREA.
  659. /// @ingroup recast
  660. /// @param[in,out] ctx The build context to use during the operation.
  661. /// @param[in] walkableSlopeAngle The maximum slope that is considered walkable.
  662. /// [Limits: 0 <= value < 90] [Units: Degrees]
  663. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  664. /// @param[in] nv The number of vertices.
  665. /// @param[in] tris The triangle vertex indices. [(vertA, vertB, vertC) * @p nt]
  666. /// @param[in] nt The number of triangles.
  667. /// @param[out] areas The triangle area ids. [Length: >= @p nt]
  668. void rcClearUnwalkableTriangles(rcContext* ctx, const float walkableSlopeAngle, const float* verts, int nv,
  669. const int* tris, int nt, unsigned char* areas);
  670. /// Adds a span to the specified heightfield.
  671. /// @ingroup recast
  672. /// @param[in,out] ctx The build context to use during the operation.
  673. /// @param[in,out] hf An initialized heightfield.
  674. /// @param[in] x The width index where the span is to be added.
  675. /// [Limits: 0 <= value < rcHeightfield::width]
  676. /// @param[in] y The height index where the span is to be added.
  677. /// [Limits: 0 <= value < rcHeightfield::height]
  678. /// @param[in] smin The minimum height of the span. [Limit: < @p smax] [Units: vx]
  679. /// @param[in] smax The maximum height of the span. [Limit: <= #RC_SPAN_MAX_HEIGHT] [Units: vx]
  680. /// @param[in] area The area id of the span. [Limit: <= #RC_WALKABLE_AREA)
  681. /// @param[in] flagMergeThr The merge theshold. [Limit: >= 0] [Units: vx]
  682. void rcAddSpan(rcContext* ctx, rcHeightfield& hf, const int x, const int y,
  683. const unsigned short smin, const unsigned short smax,
  684. const unsigned char area, const int flagMergeThr);
  685. /// Rasterizes a triangle into the specified heightfield.
  686. /// @ingroup recast
  687. /// @param[in,out] ctx The build context to use during the operation.
  688. /// @param[in] v0 Triangle vertex 0 [(x, y, z)]
  689. /// @param[in] v1 Triangle vertex 1 [(x, y, z)]
  690. /// @param[in] v2 Triangle vertex 2 [(x, y, z)]
  691. /// @param[in] area The area id of the triangle. [Limit: <= #RC_WALKABLE_AREA]
  692. /// @param[in,out] solid An initialized heightfield.
  693. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  694. /// [Limit: >= 0] [Units: vx]
  695. void rcRasterizeTriangle(rcContext* ctx, const float* v0, const float* v1, const float* v2,
  696. const unsigned char area, rcHeightfield& solid,
  697. const int flagMergeThr = 1);
  698. /// Rasterizes an indexed triangle mesh into the specified heightfield.
  699. /// @ingroup recast
  700. /// @param[in,out] ctx The build context to use during the operation.
  701. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  702. /// @param[in] nv The number of vertices.
  703. /// @param[in] tris The triangle indices. [(vertA, vertB, vertC) * @p nt]
  704. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  705. /// @param[in] nt The number of triangles.
  706. /// @param[in,out] solid An initialized heightfield.
  707. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  708. /// [Limit: >= 0] [Units: vx]
  709. void rcRasterizeTriangles(rcContext* ctx, const float* verts, const int nv,
  710. const int* tris, const unsigned char* areas, const int nt,
  711. rcHeightfield& solid, const int flagMergeThr = 1);
  712. /// Rasterizes an indexed triangle mesh into the specified heightfield.
  713. /// @ingroup recast
  714. /// @param[in,out] ctx The build context to use during the operation.
  715. /// @param[in] verts The vertices. [(x, y, z) * @p nv]
  716. /// @param[in] nv The number of vertices.
  717. /// @param[in] tris The triangle indices. [(vertA, vertB, vertC) * @p nt]
  718. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  719. /// @param[in] nt The number of triangles.
  720. /// @param[in,out] solid An initialized heightfield.
  721. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  722. /// [Limit: >= 0] [Units: vx]
  723. void rcRasterizeTriangles(rcContext* ctx, const float* verts, const int nv,
  724. const unsigned short* tris, const unsigned char* areas, const int nt,
  725. rcHeightfield& solid, const int flagMergeThr = 1);
  726. /// Rasterizes triangles into the specified heightfield.
  727. /// @ingroup recast
  728. /// @param[in,out] ctx The build context to use during the operation.
  729. /// @param[in] verts The triangle vertices. [(ax, ay, az, bx, by, bz, cx, by, cx) * @p nt]
  730. /// @param[in] areas The area id's of the triangles. [Limit: <= #RC_WALKABLE_AREA] [Size: @p nt]
  731. /// @param[in] nt The number of triangles.
  732. /// @param[in,out] solid An initialized heightfield.
  733. /// @param[in] flagMergeThr The distance where the walkable flag is favored over the non-walkable flag.
  734. /// [Limit: >= 0] [Units: vx]
  735. void rcRasterizeTriangles(rcContext* ctx, const float* verts, const unsigned char* areas, const int nt,
  736. rcHeightfield& solid, const int flagMergeThr = 1);
  737. /// Marks non-walkable spans as walkable if their maximum is within @p walkableClimp of a walkable neihbor.
  738. /// @ingroup recast
  739. /// @param[in,out] ctx The build context to use during the operation.
  740. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  741. /// [Limit: >=0] [Units: vx]
  742. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  743. void rcFilterLowHangingWalkableObstacles(rcContext* ctx, const int walkableClimb, rcHeightfield& solid);
  744. /// Marks spans that are ledges as not-walkable.
  745. /// @ingroup recast
  746. /// @param[in,out] ctx The build context to use during the operation.
  747. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area to
  748. /// be considered walkable. [Limit: >= 3] [Units: vx]
  749. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  750. /// [Limit: >=0] [Units: vx]
  751. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  752. void rcFilterLedgeSpans(rcContext* ctx, const int walkableHeight,
  753. const int walkableClimb, rcHeightfield& solid);
  754. /// Marks walkable spans as not walkable if the clearence above the span is less than the specified height.
  755. /// @ingroup recast
  756. /// @param[in,out] ctx The build context to use during the operation.
  757. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area to
  758. /// be considered walkable. [Limit: >= 3] [Units: vx]
  759. /// @param[in,out] solid A fully built heightfield. (All spans have been added.)
  760. void rcFilterWalkableLowHeightSpans(rcContext* ctx, int walkableHeight, rcHeightfield& solid);
  761. /// Returns the number of spans contained in the specified heightfield.
  762. /// @ingroup recast
  763. /// @param[in,out] ctx The build context to use during the operation.
  764. /// @param[in] hf An initialized heightfield.
  765. /// @returns The number of spans in the heightfield.
  766. int rcGetHeightFieldSpanCount(rcContext* ctx, rcHeightfield& hf);
  767. /// @}
  768. /// @name Compact Heightfield Functions
  769. /// @see rcCompactHeightfield
  770. /// @{
  771. /// Builds a compact heightfield representing open space, from a heightfield representing solid space.
  772. /// @ingroup recast
  773. /// @param[in,out] ctx The build context to use during the operation.
  774. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area
  775. /// to be considered walkable. [Limit: >= 3] [Units: vx]
  776. /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
  777. /// [Limit: >=0] [Units: vx]
  778. /// @param[in] hf The heightfield to be compacted.
  779. /// @param[out] chf The resulting compact heightfield. (Must be pre-allocated.)
  780. /// @returns True if the operation completed successfully.
  781. bool rcBuildCompactHeightfield(rcContext* ctx, const int walkableHeight, const int walkableClimb,
  782. rcHeightfield& hf, rcCompactHeightfield& chf);
  783. /// Erodes the walkable area within the heightfield by the specified radius.
  784. /// @ingroup recast
  785. /// @param[in,out] ctx The build context to use during the operation.
  786. /// @param[in] radius The radius of erosion. [Limits: 0 < value < 255] [Units: vx]
  787. /// @param[in,out] chf The populated compact heightfield to erode.
  788. /// @returns True if the operation completed successfully.
  789. bool rcErodeWalkableArea(rcContext* ctx, int radius, rcCompactHeightfield& chf);
  790. /// Applies a median filter to walkable area types (based on area id), removing noise.
  791. /// @ingroup recast
  792. /// @param[in,out] ctx The build context to use during the operation.
  793. /// @param[in,out] chf A populated compact heightfield.
  794. /// @returns True if the operation completed successfully.
  795. bool rcMedianFilterWalkableArea(rcContext* ctx, rcCompactHeightfield& chf);
  796. /// Applies an area id to all spans within the specified bounding box. (AABB)
  797. /// @ingroup recast
  798. /// @param[in,out] ctx The build context to use during the operation.
  799. /// @param[in] bmin The minimum of the bounding box. [(x, y, z)]
  800. /// @param[in] bmax The maximum of the bounding box. [(x, y, z)]
  801. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  802. /// @param[in,out] chf A populated compact heightfield.
  803. void rcMarkBoxArea(rcContext* ctx, const float* bmin, const float* bmax, unsigned char areaId,
  804. rcCompactHeightfield& chf);
  805. /// Applies the area id to the all spans within the specified convex polygon.
  806. /// @ingroup recast
  807. /// @param[in,out] ctx The build context to use during the operation.
  808. /// @param[in] verts The vertices of the polygon [Fomr: (x, y, z) * @p nverts]
  809. /// @param[in] nverts The number of vertices in the polygon.
  810. /// @param[in] hmin The height of the base of the polygon.
  811. /// @param[in] hmax The height of the top of the polygon.
  812. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  813. /// @param[in,out] chf A populated compact heightfield.
  814. void rcMarkConvexPolyArea(rcContext* ctx, const float* verts, const int nverts,
  815. const float hmin, const float hmax, unsigned char areaId,
  816. rcCompactHeightfield& chf);
  817. /// Helper function to offset voncex polygons for rcMarkConvexPolyArea.
  818. /// @ingroup recast
  819. /// @param[in] verts The vertices of the polygon [Form: (x, y, z) * @p nverts]
  820. /// @param[in] nverts The number of vertices in the polygon.
  821. /// @param[out] outVerts The offset vertices (should hold up to 2 * @p nverts) [Form: (x, y, z) * return value]
  822. /// @param[in] maxOutVerts The max number of vertices that can be stored to @p outVerts.
  823. /// @returns Number of vertices in the offset polygon or 0 if too few vertices in @p outVerts.
  824. int rcOffsetPoly(const float* verts, const int nverts, const float offset,
  825. float* outVerts, const int maxOutVerts);
  826. /// Applies the area id to all spans within the specified cylinder.
  827. /// @ingroup recast
  828. /// @param[in,out] ctx The build context to use during the operation.
  829. /// @param[in] pos The center of the base of the cylinder. [Form: (x, y, z)]
  830. /// @param[in] r The radius of the cylinder.
  831. /// @param[in] h The height of the cylinder.
  832. /// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
  833. /// @param[in,out] chf A populated compact heightfield.
  834. void rcMarkCylinderArea(rcContext* ctx, const float* pos,
  835. const float r, const float h, unsigned char areaId,
  836. rcCompactHeightfield& chf);
  837. /// Builds the distance field for the specified compact heightfield.
  838. /// @ingroup recast
  839. /// @param[in,out] ctx The build context to use during the operation.
  840. /// @param[in,out] chf A populated compact heightfield.
  841. /// @returns True if the operation completed successfully.
  842. bool rcBuildDistanceField(rcContext* ctx, rcCompactHeightfield& chf);
  843. /// Builds region data for the heightfield using watershed partitioning.
  844. /// @ingroup recast
  845. /// @param[in,out] ctx The build context to use during the operation.
  846. /// @param[in,out] chf A populated compact heightfield.
  847. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  848. /// [Limit: >=0] [Units: vx]
  849. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  850. /// [Limit: >=0] [Units: vx].
  851. /// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
  852. /// be merged with larger regions. [Limit: >=0] [Units: vx]
  853. /// @returns True if the operation completed successfully.
  854. bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
  855. const int borderSize, const int minRegionArea, const int mergeRegionArea);
  856. /// Builds region data for the heightfield using simple monotone partitioning.
  857. /// @ingroup recast
  858. /// @param[in,out] ctx The build context to use during the operation.
  859. /// @param[in,out] chf A populated compact heightfield.
  860. /// @param[in] borderSize The size of the non-navigable border around the heightfield.
  861. /// [Limit: >=0] [Units: vx]
  862. /// @param[in] minRegionArea The minimum number of cells allowed to form isolated island areas.
  863. /// [Limit: >=0] [Units: vx].
  864. /// @param[in] mergeRegionArea Any regions with a span count smaller than this value will, if possible,
  865. /// be merged with larger regions. [Limit: >=0] [Units: vx]
  866. /// @returns True if the operation completed successfully.
  867. bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
  868. const int borderSize, const int minRegionArea, const int mergeRegionArea);
  869. /// Sets the neighbor connection data for the specified direction.
  870. /// @param[in] s The span to update.
  871. /// @param[in] dir The direction to set. [Limits: 0 <= value < 4]
  872. /// @param[in] i The index of the neighbor span.
  873. inline void rcSetCon(rcCompactSpan& s, int dir, int i)
  874. {
  875. const unsigned int shift = (unsigned int)dir*6;
  876. unsigned int con = s.con;
  877. s.con = (con & ~(0x3f << shift)) | (((unsigned int)i & 0x3f) << shift);
  878. }
  879. /// Gets neighbor connection data for the specified direction.
  880. /// @param[in] s The span to check.
  881. /// @param[in] dir The direction to check. [Limits: 0 <= value < 4]
  882. /// @return The neighbor connection data for the specified direction,
  883. /// or #RC_NOT_CONNECTED if there is no connection.
  884. inline int rcGetCon(const rcCompactSpan& s, int dir)
  885. {
  886. const unsigned int shift = (unsigned int)dir*6;
  887. return (s.con >> shift) & 0x3f;
  888. }
  889. /// Gets the standard width (x-axis) offset for the specified direction.
  890. /// @param[in] dir The direction. [Limits: 0 <= value < 4]
  891. /// @return The width offset to apply to the current cell position to move
  892. /// in the direction.
  893. inline int rcGetDirOffsetX(int dir)
  894. {
  895. const int offset[4] = { -1, 0, 1, 0, };
  896. return offset[dir&0x03];
  897. }
  898. /// Gets the standard height (z-axis) offset for the specified direction.
  899. /// @param[in] dir The direction. [Limits: 0 <= value < 4]
  900. /// @return The height offset to apply to the current cell position to move
  901. /// in the direction.
  902. inline int rcGetDirOffsetY(int dir)
  903. {
  904. const int offset[4] = { 0, 1, 0, -1 };
  905. return offset[dir&0x03];
  906. }
  907. /// @}
  908. /// @name Layer, Contour, Polymesh, and Detail Mesh Functions
  909. /// @see rcHeightfieldLayer, rcContourSet, rcPolyMesh, rcPolyMeshDetail
  910. /// @{
  911. /// Builds a layer set from the specified compact heightfield.
  912. /// @ingroup recast
  913. /// @param[in,out] ctx The build context to use during the operation.
  914. /// @param[in] chf A fully built compact heightfield.
  915. /// @param[in] borderSize The size of the non-navigable border around the heightfield. [Limit: >=0]
  916. /// [Units: vx]
  917. /// @param[in] walkableHeight Minimum floor to 'ceiling' height that will still allow the floor area
  918. /// to be considered walkable. [Limit: >= 3] [Units: vx]
  919. /// @param[out] lset The resulting layer set. (Must be pre-allocated.)
  920. /// @returns True if the operation completed successfully.
  921. bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf,
  922. const int borderSize, const int walkableHeight,
  923. rcHeightfieldLayerSet& lset);
  924. /// Builds a contour set from the region outlines in the provided compact heightfield.
  925. /// @ingroup recast
  926. /// @param[in,out] ctx The build context to use during the operation.
  927. /// @param[in] chf A fully built compact heightfield.
  928. /// @param[in] maxError The maximum distance a simplfied contour's border edges should deviate
  929. /// the original raw contour. [Limit: >=0] [Units: wu]
  930. /// @param[in] maxEdgeLen The maximum allowed length for contour edges along the border of the mesh.
  931. /// [Limit: >=0] [Units: vx]
  932. /// @param[out] cset The resulting contour set. (Must be pre-allocated.)
  933. /// @param[in] buildFlags The build flags. (See: #rcBuildContoursFlags)
  934. /// @returns True if the operation completed successfully.
  935. bool rcBuildContours(rcContext* ctx, rcCompactHeightfield& chf,
  936. const float maxError, const int maxEdgeLen,
  937. rcContourSet& cset, const int flags = RC_CONTOUR_TESS_WALL_EDGES);
  938. /// Builds a polygon mesh from the provided contours.
  939. /// @ingroup recast
  940. /// @param[in,out] ctx The build context to use during the operation.
  941. /// @param[in] cset A fully built contour set.
  942. /// @param[in] nvp The maximum number of vertices allowed for polygons generated during the
  943. /// contour to polygon conversion process. [Limit: >= 3]
  944. /// @param[out] mesh The resulting polygon mesh. (Must be re-allocated.)
  945. /// @returns True if the operation completed successfully.
  946. bool rcBuildPolyMesh(rcContext* ctx, rcContourSet& cset, const int nvp, rcPolyMesh& mesh);
  947. /// Merges multiple polygon meshes into a single mesh.
  948. /// @ingroup recast
  949. /// @param[in,out] ctx The build context to use during the operation.
  950. /// @param[in] meshes An array of polygon meshes to merge. [Size: @p nmeshes]
  951. /// @param[in] nmeshes The number of polygon meshes in the meshes array.
  952. /// @param[in] mesh The resulting polygon mesh. (Must be pre-allocated.)
  953. /// @returns True if the operation completed successfully.
  954. bool rcMergePolyMeshes(rcContext* ctx, rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh);
  955. /// Builds a detail mesh from the provided polygon mesh.
  956. /// @ingroup recast
  957. /// @param[in,out] ctx The build context to use during the operation.
  958. /// @param[in] mesh A fully built polygon mesh.
  959. /// @param[in] chf The compact heightfield used to build the polygon mesh.
  960. /// @param[in] sampleDist Sets the distance to use when samping the heightfield. [Limit: >=0] [Units: wu]
  961. /// @param[in] sampleMaxError The maximum distance the detail mesh surface should deviate from
  962. /// heightfield data. [Limit: >=0] [Units: wu]
  963. /// @param[out] dmesh The resulting detail mesh. (Must be pre-allocated.)
  964. /// @returns True if the operation completed successfully.
  965. bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompactHeightfield& chf,
  966. const float sampleDist, const float sampleMaxError,
  967. rcPolyMeshDetail& dmesh);
  968. /// Copies the poly mesh data from src to dst.
  969. /// @ingroup recast
  970. /// @param[in,out] ctx The build context to use during the operation.
  971. /// @param[in] src The source mesh to copy from.
  972. /// @param[out] dst The resulting detail mesh. (Must be pre-allocated, must be empty mesh.)
  973. /// @returns True if the operation completed successfully.
  974. bool rcCopyPolyMesh(rcContext* ctx, const rcPolyMesh& src, rcPolyMesh& dst);
  975. /// Merges multiple detail meshes into a single detail mesh.
  976. /// @ingroup recast
  977. /// @param[in,out] ctx The build context to use during the operation.
  978. /// @param[in] meshes An array of detail meshes to merge. [Size: @p nmeshes]
  979. /// @param[in] nmeshes The number of detail meshes in the meshes array.
  980. /// @param[out] mesh The resulting detail mesh. (Must be pre-allocated.)
  981. /// @returns True if the operation completed successfully.
  982. bool rcMergePolyMeshDetails(rcContext* ctx, rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh);
  983. /// @}
  984. #endif // RECAST_H
  985. ///////////////////////////////////////////////////////////////////////////
  986. // Due to the large amount of detail documentation for this file,
  987. // the content normally located at the end of the header file has been separated
  988. // out to a file in /Docs/Extern.