Recast.h 54 KB

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