recast-capi.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. *
  3. * ***** BEGIN GPL LICENSE BLOCK *****
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. *
  19. * The Original Code is Copyright (C) 2011 Blender Foundation.
  20. * All rights reserved.
  21. *
  22. * Contributor(s): Sergey Sharybin
  23. *
  24. * ***** END GPL LICENSE BLOCK *****
  25. */
  26. #include "recast-capi.h"
  27. #include <math.h>
  28. #include "Recast.h"
  29. static rcContext *sctx;
  30. #define INIT_SCTX() \
  31. if (sctx == NULL) sctx = new rcContext(false)
  32. int recast_buildMeshAdjacency(unsigned short* polys, const int npolys,
  33. const int nverts, const int vertsPerPoly)
  34. {
  35. return (int) buildMeshAdjacency(polys, npolys, nverts, vertsPerPoly);
  36. }
  37. void recast_calcBounds(const float *verts, int nv, float *bmin, float *bmax)
  38. {
  39. rcCalcBounds(verts, nv, bmin, bmax);
  40. }
  41. void recast_calcGridSize(const float *bmin, const float *bmax, float cs, int *w, int *h)
  42. {
  43. rcCalcGridSize(bmin, bmax, cs, w, h);
  44. }
  45. struct recast_heightfield *recast_newHeightfield(void)
  46. {
  47. return (struct recast_heightfield *) rcAllocHeightfield();
  48. }
  49. void recast_destroyHeightfield(struct recast_heightfield *heightfield)
  50. {
  51. rcFreeHeightField((rcHeightfield *) heightfield);
  52. }
  53. int recast_createHeightfield(struct recast_heightfield *hf, int width, int height,
  54. const float *bmin, const float* bmax, float cs, float ch)
  55. {
  56. INIT_SCTX();
  57. return rcCreateHeightfield(sctx, *(rcHeightfield *)hf, width, height, bmin, bmax, cs, ch);
  58. }
  59. void recast_markWalkableTriangles(const float walkableSlopeAngle,const float *verts, int nv,
  60. const int *tris, int nt, unsigned char *areas)
  61. {
  62. INIT_SCTX();
  63. rcMarkWalkableTriangles(sctx, walkableSlopeAngle, verts, nv, tris, nt, areas);
  64. }
  65. void recast_clearUnwalkableTriangles(const float walkableSlopeAngle, const float* verts, int nv,
  66. const int* tris, int nt, unsigned char* areas)
  67. {
  68. INIT_SCTX();
  69. rcClearUnwalkableTriangles(sctx, walkableSlopeAngle, verts, nv, tris, nt, areas);
  70. }
  71. int recast_addSpan(struct recast_heightfield *hf, const int x, const int y,
  72. const unsigned short smin, const unsigned short smax,
  73. const unsigned char area, const int flagMergeThr)
  74. {
  75. INIT_SCTX();
  76. return rcAddSpan(sctx, *(rcHeightfield *) hf, x, y, smin, smax, area, flagMergeThr);
  77. }
  78. int recast_rasterizeTriangle(const float *v0, const float *v1, const float *v2,
  79. const unsigned char area, struct recast_heightfield *solid,
  80. const int flagMergeThr)
  81. {
  82. INIT_SCTX();
  83. return rcRasterizeTriangle(sctx, v0, v1, v2, area, *(rcHeightfield *) solid, flagMergeThr);
  84. }
  85. int recast_rasterizeTriangles(const float *verts, const int nv, const int *tris,
  86. const unsigned char *areas, const int nt, struct recast_heightfield *solid,
  87. const int flagMergeThr)
  88. {
  89. INIT_SCTX();
  90. return rcRasterizeTriangles(sctx, verts, nv, tris, areas, nt, *(rcHeightfield *) solid, flagMergeThr);
  91. }
  92. void recast_filterLedgeSpans(const int walkableHeight, const int walkableClimb,
  93. struct recast_heightfield *solid)
  94. {
  95. INIT_SCTX();
  96. rcFilterLedgeSpans(sctx, walkableHeight, walkableClimb, *(rcHeightfield *) solid);
  97. }
  98. void recast_filterWalkableLowHeightSpans(int walkableHeight, struct recast_heightfield *solid)
  99. {
  100. INIT_SCTX();
  101. rcFilterWalkableLowHeightSpans(sctx, walkableHeight, *(rcHeightfield *) solid);
  102. }
  103. void recast_filterLowHangingWalkableObstacles(const int walkableClimb, struct recast_heightfield *solid)
  104. {
  105. INIT_SCTX();
  106. rcFilterLowHangingWalkableObstacles(sctx, walkableClimb, *(rcHeightfield *) solid);
  107. }
  108. int recast_getHeightFieldSpanCount(struct recast_heightfield *hf)
  109. {
  110. INIT_SCTX();
  111. return rcGetHeightFieldSpanCount(sctx, *(rcHeightfield *) hf);
  112. }
  113. struct recast_heightfieldLayerSet *recast_newHeightfieldLayerSet(void)
  114. {
  115. return (struct recast_heightfieldLayerSet *) rcAllocHeightfieldLayerSet();
  116. }
  117. void recast_destroyHeightfieldLayerSet(struct recast_heightfieldLayerSet *lset)
  118. {
  119. rcFreeHeightfieldLayerSet( (rcHeightfieldLayerSet *) lset);
  120. }
  121. struct recast_compactHeightfield *recast_newCompactHeightfield(void)
  122. {
  123. return (struct recast_compactHeightfield *) rcAllocCompactHeightfield();
  124. }
  125. void recast_destroyCompactHeightfield(struct recast_compactHeightfield *compactHeightfield)
  126. {
  127. rcFreeCompactHeightfield( (rcCompactHeightfield *) compactHeightfield);
  128. }
  129. int recast_buildCompactHeightfield(const int walkableHeight, const int walkableClimb,
  130. struct recast_heightfield *hf, struct recast_compactHeightfield *chf)
  131. {
  132. INIT_SCTX();
  133. return rcBuildCompactHeightfield(sctx, walkableHeight, walkableClimb,
  134. *(rcHeightfield *) hf, *(rcCompactHeightfield *) chf);
  135. }
  136. int recast_erodeWalkableArea(int radius, struct recast_compactHeightfield *chf)
  137. {
  138. INIT_SCTX();
  139. return rcErodeWalkableArea(sctx, radius, *(rcCompactHeightfield *) chf);
  140. }
  141. int recast_medianFilterWalkableArea(struct recast_compactHeightfield *chf)
  142. {
  143. INIT_SCTX();
  144. return rcMedianFilterWalkableArea(sctx, *(rcCompactHeightfield *) chf);
  145. }
  146. void recast_markBoxArea(const float *bmin, const float *bmax, unsigned char areaId,
  147. struct recast_compactHeightfield *chf)
  148. {
  149. INIT_SCTX();
  150. rcMarkBoxArea(sctx, bmin, bmax, areaId, *(rcCompactHeightfield *) chf);
  151. }
  152. void recast_markConvexPolyArea(const float* verts, const int nverts,
  153. const float hmin, const float hmax, unsigned char areaId,
  154. struct recast_compactHeightfield *chf)
  155. {
  156. INIT_SCTX();
  157. rcMarkConvexPolyArea(sctx, verts, nverts, hmin, hmax, areaId, *(rcCompactHeightfield *) chf);
  158. }
  159. int recast_offsetPoly(const float* verts, const int nverts,
  160. const float offset, float *outVerts, const int maxOutVerts)
  161. {
  162. return rcOffsetPoly(verts, nverts, offset, outVerts, maxOutVerts);
  163. }
  164. void recast_markCylinderArea(const float* pos, const float r, const float h,
  165. unsigned char areaId, struct recast_compactHeightfield *chf)
  166. {
  167. INIT_SCTX();
  168. rcMarkCylinderArea(sctx, pos, r, h, areaId, *(rcCompactHeightfield *) chf);
  169. }
  170. int recast_buildDistanceField(struct recast_compactHeightfield *chf)
  171. {
  172. INIT_SCTX();
  173. return rcBuildDistanceField(sctx, *(rcCompactHeightfield *) chf);
  174. }
  175. int recast_buildRegions(struct recast_compactHeightfield *chf,
  176. const int borderSize, const int minRegionArea, const int mergeRegionArea)
  177. {
  178. INIT_SCTX();
  179. return rcBuildRegions(sctx, *(rcCompactHeightfield *) chf, borderSize,
  180. minRegionArea, mergeRegionArea);
  181. }
  182. int recast_buildLayerRegions(struct recast_compactHeightfield *chf,
  183. const int borderSize, const int minRegionArea)
  184. {
  185. INIT_SCTX();
  186. return rcBuildLayerRegions(sctx, *(rcCompactHeightfield *) chf, borderSize,
  187. minRegionArea);
  188. }
  189. int recast_buildRegionsMonotone(struct recast_compactHeightfield *chf,
  190. const int borderSize, const int minRegionArea, const int mergeRegionArea)
  191. {
  192. INIT_SCTX();
  193. return rcBuildRegionsMonotone(sctx, *(rcCompactHeightfield *) chf, borderSize,
  194. minRegionArea, mergeRegionArea);
  195. }
  196. struct recast_contourSet *recast_newContourSet(void)
  197. {
  198. return (struct recast_contourSet *) rcAllocContourSet();
  199. }
  200. void recast_destroyContourSet(struct recast_contourSet *contourSet)
  201. {
  202. rcFreeContourSet((rcContourSet *) contourSet);
  203. }
  204. int recast_buildContours(struct recast_compactHeightfield *chf,
  205. const float maxError, const int maxEdgeLen, struct recast_contourSet *cset,
  206. const int buildFlags)
  207. {
  208. INIT_SCTX();
  209. return rcBuildContours(sctx, *(rcCompactHeightfield *) chf, maxError, maxEdgeLen, *(rcContourSet *) cset, buildFlags);
  210. }
  211. struct recast_polyMesh *recast_newPolyMesh(void)
  212. {
  213. return (recast_polyMesh *) rcAllocPolyMesh();
  214. }
  215. void recast_destroyPolyMesh(struct recast_polyMesh *polyMesh)
  216. {
  217. rcFreePolyMesh((rcPolyMesh *) polyMesh);
  218. }
  219. int recast_buildPolyMesh(struct recast_contourSet *cset, const int nvp, struct recast_polyMesh *mesh)
  220. {
  221. INIT_SCTX();
  222. return rcBuildPolyMesh(sctx, *(rcContourSet *) cset, nvp, *(rcPolyMesh *) mesh);
  223. }
  224. int recast_mergePolyMeshes(struct recast_polyMesh **meshes, const int nmeshes, struct recast_polyMesh *mesh)
  225. {
  226. INIT_SCTX();
  227. return rcMergePolyMeshes(sctx, (rcPolyMesh **) meshes, nmeshes, *(rcPolyMesh *) mesh);
  228. }
  229. int recast_copyPolyMesh(const struct recast_polyMesh *src, struct recast_polyMesh *dst)
  230. {
  231. INIT_SCTX();
  232. return rcCopyPolyMesh(sctx, *(const rcPolyMesh *) src, *(rcPolyMesh *) dst);
  233. }
  234. unsigned short *recast_polyMeshGetVerts(struct recast_polyMesh *mesh, int *nverts)
  235. {
  236. rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
  237. if (nverts)
  238. *nverts = pmesh->nverts;
  239. return pmesh->verts;
  240. }
  241. void recast_polyMeshGetBoundbox(struct recast_polyMesh *mesh, float *bmin, float *bmax)
  242. {
  243. rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
  244. if (bmin) {
  245. bmin[0] = pmesh->bmin[0];
  246. bmin[1] = pmesh->bmin[1];
  247. bmin[2] = pmesh->bmin[2];
  248. }
  249. if (bmax) {
  250. bmax[0] = pmesh->bmax[0];
  251. bmax[1] = pmesh->bmax[1];
  252. bmax[2] = pmesh->bmax[2];
  253. }
  254. }
  255. void recast_polyMeshGetCell(struct recast_polyMesh *mesh, float *cs, float *ch)
  256. {
  257. rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
  258. if (cs)
  259. *cs = pmesh->cs;
  260. if (ch)
  261. *ch = pmesh->ch;
  262. }
  263. unsigned short *recast_polyMeshGetPolys(struct recast_polyMesh *mesh, int *npolys, int *nvp)
  264. {
  265. rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
  266. if (npolys)
  267. *npolys = pmesh->npolys;
  268. if (nvp)
  269. *nvp = pmesh->nvp;
  270. return pmesh->polys;
  271. }
  272. struct recast_polyMeshDetail *recast_newPolyMeshDetail(void)
  273. {
  274. return (struct recast_polyMeshDetail *) rcAllocPolyMeshDetail();
  275. }
  276. void recast_destroyPolyMeshDetail(struct recast_polyMeshDetail *polyMeshDetail)
  277. {
  278. rcFreePolyMeshDetail((rcPolyMeshDetail *) polyMeshDetail);
  279. }
  280. int recast_buildPolyMeshDetail(const struct recast_polyMesh *mesh, const struct recast_compactHeightfield *chf,
  281. const float sampleDist, const float sampleMaxError, struct recast_polyMeshDetail *dmesh)
  282. {
  283. INIT_SCTX();
  284. return rcBuildPolyMeshDetail(sctx, *(rcPolyMesh *) mesh, *(rcCompactHeightfield *) chf,
  285. sampleDist, sampleMaxError, *(rcPolyMeshDetail *) dmesh);
  286. }
  287. int recast_mergePolyMeshDetails(struct recast_polyMeshDetail **meshes, const int nmeshes, struct recast_polyMeshDetail *mesh)
  288. {
  289. INIT_SCTX();
  290. return rcMergePolyMeshDetails(sctx, (rcPolyMeshDetail **) meshes, nmeshes, *(rcPolyMeshDetail *) mesh);
  291. }
  292. float *recast_polyMeshDetailGetVerts(struct recast_polyMeshDetail *mesh, int *nverts)
  293. {
  294. rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
  295. if (nverts)
  296. *nverts = dmesh->nverts;
  297. return dmesh->verts;
  298. }
  299. unsigned char *recast_polyMeshDetailGetTris(struct recast_polyMeshDetail *mesh, int *ntris)
  300. {
  301. rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
  302. if (ntris)
  303. *ntris = dmesh->ntris;
  304. return dmesh->tris;
  305. }
  306. unsigned int *recast_polyMeshDetailGetMeshes(struct recast_polyMeshDetail *mesh, int *nmeshes)
  307. {
  308. rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
  309. if (nmeshes)
  310. *nmeshes = dmesh->nmeshes;
  311. return dmesh->meshes;
  312. }