Recast_api.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. // This file contains the detail API documentation for
  2. // elements defined in the Recast.h.
  3. /**
  4. @defgroup recast Recast
  5. Members in this module are used to create mesh data that is then
  6. used to create Detour navigation meshes.
  7. The are a large number of possible ways to building navigation mesh data.
  8. One of the simple piplines is as follows:
  9. -# Prepare the input triangle mesh.
  10. -# Build a #rcHeightfield.
  11. -# Build a #rcCompactHeightfield.
  12. -# Build a #rcContourSet.
  13. -# Build a #rcPolyMesh.
  14. -# Build a #rcPolyMeshDetail.
  15. -# Use the rcPolyMesh and rcPolyMeshDetail to build a Detour navigation mesh
  16. tile.
  17. The general life-cycle of the main classes is as follows:
  18. -# Allocate the object using the Recast allocator. (E.g. #rcAllocHeightfield)
  19. -# Initialize or build the object. (E.g. #rcCreateHeightfield)
  20. -# Update the object as needed. (E.g. #rcRasterizeTriangles)
  21. -# Use the object as part of the pipeline.
  22. -# Free the object using the Recast allocator. (E.g. #rcFreeHeightField)
  23. @note This is a summary list of members. Use the index or search
  24. feature to find minor members.
  25. @struct rcConfig
  26. @par
  27. The is a convenience structure that represents an aggregation of parameters
  28. used at different stages in the Recast build process. Some
  29. values are derived during the build process. Not all parameters
  30. are used for all build processes.
  31. Units are usually in voxels (vx) or world units (wu). The units for voxels,
  32. grid size, and cell size are all based on the values of #cs and #ch.
  33. In this documentation, the term 'field' refers to heightfield and
  34. contour data structures that define spacial information using an integer
  35. grid.
  36. The upper and lower limits for the various parameters often depend on
  37. the platform's floating point accuraccy as well as interdependencies between
  38. the values of multiple parameters. See the individual parameter
  39. documentation for details.
  40. @var rcConfig::borderSize
  41. @par
  42. This value represents the the closest the walkable area of the heightfield
  43. should come to the xz-plane AABB of the field. It does not have any
  44. impact on the borders around internal obstructions.
  45. @var rcConfig::tileSize
  46. @par
  47. This field is only used when building multi-tile meshes.
  48. @var rcConfig::cs
  49. @par
  50. @p cs and #ch define voxel/grid/cell size. So their values have significant
  51. side effects on all parameters defined in voxel units.
  52. The minimum value for this parameter depends on the platform's floating point
  53. accuracy, with the practical minimum usually around 0.05.
  54. @var rcConfig::ch
  55. @par
  56. #cs and @p ch define voxel/grid/cell size. So their values have significant
  57. side effects on all parameters defined in voxel units.
  58. The minimum value for this parameter depends on the platform's floating point
  59. accuracy, with the practical minimum usually around 0.05.
  60. @var rcConfig::walkableSlopeAngle
  61. @par
  62. The practical upper limit for this parameter is usually around 85 degrees.
  63. @var rcConfig::walkableHeight
  64. @par
  65. Permits detection of overhangs in the source geometry that make the geometry
  66. below un-walkable. The value is usually set to the maximum agent height.
  67. @var rcConfig::walkableClimb
  68. @par
  69. Allows the mesh to flow over low lying obstructions such as curbs and
  70. up/down stairways. The value is usually set to how far up/down an agent can step.
  71. @var rcConfig::walkableRadius
  72. @par
  73. In general, this is the closest any part of the final mesh should get to an
  74. obstruction in the source geometry. It is usually set to the maximum
  75. agent radius.
  76. While a value of zero is legal, it is not recommended and can result in
  77. odd edge case issues.
  78. @var rcConfig::maxEdgeLen
  79. @par
  80. Extra vertices will be inserted as needed to keep contour edges below this
  81. length. A value of zero effectively disables this feature.
  82. @var rcConfig::maxSimplificationError
  83. @par
  84. The effect of this parameter only applies to the xz-plane.
  85. @var rcConfig::minRegionArea
  86. @par
  87. Any regions that are smaller than this area will be marked as unwalkable.
  88. This is useful in removing useless regions that can sometimes form on
  89. geometry such as table tops, box tops, etc.
  90. @var rcConfig::maxVertsPerPoly
  91. @par
  92. If the mesh data is to be used to construct a Detour navigation mesh, then the upper limit
  93. is limited to <= #DT_VERTS_PER_POLYGON.
  94. @struct rcHeightfield
  95. @par
  96. The grid of a heightfield is layed out on the xz-plane based on the
  97. value of #cs. Spans exist within the grid columns with the span
  98. min/max values at increments of #ch from the base of the grid. The smallest
  99. possible span size is <tt>(#cs width) * (#cs depth) * (#ch height)</tt>. (Which is a single voxel.)
  100. The standard process for buidling a heightfield is to allocate it using
  101. #rcAllocHeightfield, initialize it using #rcCreateHeightfield, then
  102. add spans using the various helper functions such as #rcRasterizeTriangle.
  103. Building a heightfield is one of the first steps in creating a polygon mesh
  104. from source geometry. After it is populated, it is used to build a
  105. rcCompactHeightfield.
  106. Example of iterating the spans in a heightfield:
  107. @code
  108. // Where hf is a reference to an heightfield object.
  109. const float* orig = hf.bmin;
  110. const float cs = hf.cs;
  111. const float ch = hf.ch;
  112. const int w = hf.width;
  113. const int h = hf.height;
  114. for (int y = 0; y < h; ++y)
  115. {
  116. for (int x = 0; x < w; ++x)
  117. {
  118. // Deriving the minimum corner of the grid location.
  119. float fx = orig[0] + x*cs;
  120. float fz = orig[2] + y*cs;
  121. // The base span in the column. (May be null.)
  122. const rcSpan* s = hf.spans[x + y*w];
  123. while (s)
  124. {
  125. // Detriving the minium and maximum world position of the span.
  126. float fymin = orig[1]+s->smin*ch;
  127. float fymax = orig[1] + s->smax*ch;
  128. // Do other things with the span before moving up the column.
  129. s = s->next;
  130. }
  131. }
  132. }
  133. @endcode
  134. @see rcAllocHeightfield, rcFreeHeightField, rcCreateHeightfield
  135. @struct rcCompactCell
  136. @par
  137. See the rcCompactHeightfield documentation for an example of how compact cells
  138. are used to iterate the heightfield.
  139. Useful instances of this type can only by obtained from a #rcCompactHeightfield object.
  140. @see rcCompactHeightfield
  141. @struct rcCompactSpan
  142. @par
  143. The span represents open, unobstructed space within a compact heightfield column.
  144. See the rcCompactHeightfield documentation for an example of iterating spans and searching
  145. span connections.
  146. Useful instances of this type can only by obtained from a #rcCompactHeightfield object.
  147. @see rcCompactHeightfield
  148. @struct rcCompactHeightfield
  149. @par
  150. For this type of heightfield, the spans represent the open (unobstructed)
  151. space above the solid surfaces of a voxel field. It is usually created from
  152. a #rcHeightfield object. Data is stored in a compact, efficient manner,
  153. but the structure is not condusive to adding and removing spans.
  154. The standard process for buidling a compact heightfield is to allocate it
  155. using #rcAllocCompactHeightfield, build it using #rcBuildCompactHeightfield,
  156. then run it through the various helper functions to generate neighbor
  157. and region data.
  158. Connected neighbor spans form non-overlapping surfaces. When neighbor
  159. information is generated, spans will include data that can be used to
  160. locate axis-neighbors. Axis-neighbors are connected
  161. spans that are offset from the current cell column as follows:
  162. <pre>
  163. Direction 0 = (-1, 0)
  164. Direction 1 = (0, 1)
  165. Direction 2 = (1, 0)
  166. Direction 3 = (0, -1)
  167. </pre>
  168. Example of iterating and inspecting spans, including connected neighbors:
  169. @code
  170. // Where chf is an instance of a rcCompactHeightfield.
  171. const float cs = chf.cs;
  172. const float ch = chf.ch;
  173. for (int y = 0; y < chf.height; ++y)
  174. {
  175. for (int x = 0; x < chf.width; ++x)
  176. {
  177. // Deriving the minimum corner of the grid location.
  178. const float fx = chf.bmin[0] + x*cs;
  179. const float fz = chf.bmin[2] + y*cs;
  180. // Get the cell for the grid location then iterate
  181. // up the column.
  182. const rcCompactCell& c = chf.cells[x+y*chf.width];
  183. for (unsigned i = c.index, ni = c.index+c.count; i < ni; ++i)
  184. {
  185. const rcCompactSpan& s = chf.spans[i];
  186. Deriving the minimum (floor) of the span.
  187. const float fy = chf.bmin[1] + (s.y+1)*ch;
  188. // Testing the area assignment of the span.
  189. if (chf.areas[i] == RC_WALKABLE_AREA)
  190. {
  191. // The span is in the default 'walkable area'.
  192. }
  193. else if (chf.areas[i] == RC_NULL_AREA)
  194. {
  195. // The surface is not considered walkable.
  196. // E.g. It was filtered out during the build processes.
  197. }
  198. else
  199. {
  200. // Do something. (Only applicable for custom build
  201. // build processes.)
  202. }
  203. // Iterating the connected axis-neighbor spans.
  204. for (int dir = 0; dir < 4; ++dir)
  205. {
  206. if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
  207. {
  208. // There is a neighbor in this direction.
  209. const int nx = x + rcGetDirOffsetX(dir);
  210. const int ny = y + rcGetDirOffsetY(dir);
  211. const int ni = (int)chf.cells[nx+ny*w].index + rcGetCon(s, 0);
  212. const rcCompactSpan& ns = chf.spans[ni];
  213. // Do something with the neighbor span.
  214. }
  215. }
  216. }
  217. }
  218. }
  219. @endcode
  220. @see rcAllocCompactHeightfield, rcFreeCompactHeightfield, rcBuildCompactHeightfield
  221. @struct rcContour
  222. @par
  223. A contour only exists within the context of a #rcContourSet object.
  224. While the height of the contour's border may vary, the contour will always
  225. form a simple polygon when projected onto the xz-plane.
  226. Example of converting vertices into world space:
  227. @code
  228. // Where cset is the rcContourSet object to which the contour belongs.
  229. float worldX = cset.bmin[0] + vertX * cset.cs;
  230. float worldY = cset.bmin[1] + vertY * cset.ch;
  231. float worldZ = cset.bmin[2] + vertZ * cset.cs;
  232. @endcode
  233. @see rcContourSet
  234. @var rcContour::verts
  235. @par
  236. The simplified contour is a version of the raw contour with all
  237. 'unnecessary' vertices removed. Whether a vertex is
  238. considered unnecessary depends on the contour build process.
  239. The data format is as follows: (x, y, z, r) * #nverts
  240. A contour edge is formed by the current and next vertex. The r-value
  241. represents region and connection information for the edge. For example:
  242. @code
  243. int r = verts[i*4+3];
  244. int regionId = r & RC_CONTOUR_REG_MASK;
  245. if (r & RC_BORDER_VERTEX)
  246. {
  247. // The edge represents a solid border.
  248. }
  249. if (r & RC_AREA_BORDER)
  250. {
  251. // The edge represents a transition between different areas.
  252. }
  253. @endcode
  254. @var rcContour::rverts
  255. @par
  256. See #verts for information on element layout.
  257. @struct rcContourSet
  258. @par
  259. All contours within the set share the minimum bounds and cell sizes of the set.
  260. The standard process for building a contour set is to allocate it
  261. using #rcAllocContourSet, then initialize it using #rcBuildContours.
  262. @see rcAllocContourSet, rcFreeContourSet, rcBuildContours
  263. @struct rcPolyMesh
  264. @par
  265. A mesh of potentially overlapping convex polygons of between three
  266. and #nvp vertices. The mesh exists within the context of an axis-aligned
  267. bounding box (AABB) with vertices laid out in an evenly spaced grid, based
  268. on the values of #cs and #ch.
  269. The standard process for building a contour set is to allocate it using
  270. #rcAllocPolyMesh, the initialize it using #rcBuildPolyMesh
  271. Example of iterating the polygons:
  272. @code
  273. // Where mesh is a reference to a rcPolyMesh object.
  274. const int nvp = mesh.nvp;
  275. const float cs = mesh.cs;
  276. const float ch = mesh.ch;
  277. const float* orig = mesh.bmin;
  278. for (int i = 0; i < mesh.npolys; ++i)
  279. {
  280. const unsigned short* p = &mesh.polys[i*nvp*2];
  281. // Iterate the vertices.
  282. unsigned short vi[3]; // The vertex indices.
  283. for (int j = 0; j < nvp; ++j)
  284. {
  285. if (p[j] == RC_MESH_NULL_IDX)
  286. break; // End of vertices.
  287. if (p[j + nvp] == RC_MESH_NULL_IDX)
  288. {
  289. // The edge beginning with this vertex is a solid border.
  290. }
  291. else
  292. {
  293. // The edge beginning with this vertex connects to
  294. // polygon p[j + nvp].
  295. }
  296. // Convert to world space.
  297. const unsigned short* v = &mesh.verts[p[j]*3];
  298. const float x = orig[0] + v[0]*cs;
  299. const float y = orig[1] + v[1]*ch;
  300. const float z = orig[2] + v[2]*cs;
  301. // Do something with the vertices.
  302. }
  303. }
  304. @endcode
  305. @see rcAllocPolyMesh, rcFreePolyMesh, rcBuildPolyMesh
  306. @var rcPolyMesh::verts
  307. @par
  308. The values of #bmin ,#cs, and #ch are used to convert vertex coordinates
  309. to world space as follows:
  310. @code
  311. float worldX = bmin[0] + verts[i*3+0] * cs
  312. float worldY = bmin[1] + verts[i*3+1] * ch
  313. float worldZ = bmin[2] + verts[i*3+2] * cs
  314. @endcode
  315. @var rcPolyMesh::polys
  316. @par
  317. Each entry is <tt>2 * #nvp</tt> in length. The first half of the entry
  318. contains the indices of the polygon. The first instance of #RC_MESH_NULL_IDX
  319. indicates the end of the indices for the entry. The second half contains
  320. indices to neighbor polygons. A value of #RC_MESH_NULL_IDX indicates no
  321. connection for the associated edge. (I.e. The edge is a solid border.)
  322. For example:
  323. <pre>
  324. nvp = 6
  325. For the entry: (1, 3, 4, 8, RC_MESH_NULL_IDX, RC_MESH_NULL_IDX,
  326. 18, RC_MESH_NULL_IDX , 21, RC_MESH_NULL_IDX, RC_MESH_NULL_IDX, RC_MESH_NULL_IDX)
  327. (1, 3, 4, 8) defines a polygon with 4 vertices.
  328. Edge 1->3 is shared with polygon 18.
  329. Edge 4->8 is shared with polygon 21.
  330. Edges 3->4 and 4->8 are border edges not shared with any other polygon.
  331. </pre>
  332. @var rcPolyMesh::areas
  333. @par
  334. The standard build process assigns the value of #RC_WALKABLE_AREA to all walkable polygons.
  335. This value can then be changed to meet user requirements.
  336. @struct rcPolyMeshDetail
  337. @par
  338. The detail mesh is made up of triangle sub-meshes that provide extra
  339. height detail for each polygon in its assoicated polygon mesh.
  340. The standard process for building a detail mesh is to allocate it
  341. using #rcAllocPolyMeshDetail, then build it using #rcBuildPolyMeshDetail.
  342. See the individual field definitions for details realted to the structure
  343. the mesh.
  344. @see rcAllocPolyMeshDetail, rcFreePolyMeshDetail, rcBuildPolyMeshDetail, rcPolyMesh
  345. @var rcPolyMeshDetail::meshes
  346. @par
  347. [(baseVertIndex, vertCount, baseTriIndex, triCount) * #nmeshes]
  348. Maximum number of vertices per sub-mesh: 127<br/>
  349. Maximum number of triangles per sub-mesh: 255
  350. The sub-meshes are stored in the same order as the polygons from the
  351. rcPolyMesh they represent. E.g. rcPolyMeshDetail sub-mesh 5 is associated
  352. with #rcPolyMesh polygon 5.
  353. Example of iterating the triangles in a sub-mesh.
  354. @code
  355. // Where dmesh is a reference to a rcPolyMeshDetail object.
  356. // Iterate the sub-meshes. (One for each source polygon.)
  357. for (int i = 0; i < dmesh.nmeshes; ++i)
  358. {
  359. const unsigned int* meshDef = &dmesh.meshes[i*4];
  360. const unsigned int baseVerts = meshDef[0];
  361. const unsigned int baseTri = meshDef[2];
  362. const int ntris = (int)meshDef[3];
  363. const float* verts = &dmesh.verts[baseVerts*3];
  364. const unsigned char* tris = &dmesh.tris[baseTri*4];
  365. // Iterate the sub-mesh's triangles.
  366. for (int j = 0; j < ntris; ++j)
  367. {
  368. const float x = verts[tris[j*4+0]*3];
  369. const float y = verts[tris[j*4+1]*3];
  370. const float z = verts[tris[j*4+2]*3];
  371. // Do something with the vertex.
  372. }
  373. }
  374. @endcode
  375. @var rcPolyMeshDetail::verts
  376. @par
  377. [(x, y, z) * #nverts]
  378. The vertices are grouped by sub-mesh and will contain duplicates since
  379. each sub-mesh is independently defined.
  380. The first group of vertices for each sub-mesh are in the same order as
  381. the vertices for the sub-mesh's associated PolyMesh polygon. These
  382. vertices are followed by any additional detail vertices. So it the
  383. associated polygon has 5 vertices, the sub-mesh will have a minimum
  384. of 5 vertices and the first 5 vertices will be equivalent to the 5
  385. polygon vertices.
  386. @var rcPolyMeshDetail::tris
  387. @par
  388. [(vertIndexA, vertIndexB, vertIndexC, flags) * #ntris]
  389. The triangles are grouped by sub-mesh.
  390. <b>Vertex Indices</b>
  391. The vertex indices in the triangle array are local to the sub-mesh, not global.
  392. To translate into an global index in the vertices array, the values must be
  393. offset by the sub-mesh's base vertex index.
  394. Example: If the baseVertexIndex for the sub-mesh is 5 and the triangle entry
  395. is (4, 8, 7, 0), then the actual indices for the vertices are (4 + 5, 8 + 5, 7 + 5).
  396. @b Flags
  397. The flags entry indicates which edges are internal and which are external to
  398. the sub-mesh. Internal edges connect to other triangles within the same sub-mesh.
  399. External edges represent portals to other sub-meshes or the null region.
  400. Each flag is stored in a 2-bit position. Where position 0 is the lowest 2-bits
  401. and position 4 is the highest 2-bits:
  402. <tt>
  403. Position 0: Edge AB (>> 0)<br/>
  404. Position 1: Edge BC (>> 2)<br/>
  405. Position 2: Edge CA (>> 4)<br/>
  406. Position 4: Unused<br/>
  407. </tt>
  408. Testing can be performed as follows:
  409. @code
  410. if (((flags >> 2) & 0x3) != 0)
  411. {
  412. // Edge BC is an external edge.
  413. }
  414. @endcode
  415. @fn void rcSetCon(rcCompactSpan &s, int dir, int i)
  416. @par
  417. This function is used by the build process. It is rarely of use to end users.
  418. @see #rcCompactHeightfield, #rcCompactSpan
  419. @fn int rcGetCon(const rcCompactSpan &s, int dir)
  420. @par
  421. Can be used to locate neighbor spans in a compact heightfield. See the
  422. #rcCompactHeightfield documentation for details on its use.
  423. @see #rcCompactHeightfield, #rcCompactSpan
  424. @fn int rcGetDirOffsetX(int dir)
  425. @par
  426. The value of @p dir will be automatically wrapped. So a value of 6 will be interpreted as 2.
  427. See the #rcCompactHeightfield documentation for usage details.
  428. @fn int rcGetDirOffsetY(int dir)
  429. @par
  430. The value of @p dir will be automatically wrapped. So a value of 6 will be interpreted as 2.
  431. See the #rcCompactHeightfield documentation for usage details.
  432. */