3 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
19 * The Original Code is Copyright (C) 2011 Blender Foundation.
20 * All rights reserved.
22 * Contributor(s): Sergey Sharybin
24 * ***** END GPL LICENSE BLOCK *****
27 #include "recast-capi.h"
32 static rcContext *sctx;
35 if (sctx == NULL) sctx = new rcContext(false)
37 int recast_buildMeshAdjacency(unsigned short* polys, const int npolys,
38 const int nverts, const int vertsPerPoly)
40 return (int) buildMeshAdjacency(polys, npolys, nverts, vertsPerPoly);
43 void recast_calcBounds(const float *verts, int nv, float *bmin, float *bmax)
45 rcCalcBounds(verts, nv, bmin, bmax);
48 void recast_calcGridSize(const float *bmin, const float *bmax, float cs, int *w, int *h)
50 rcCalcGridSize(bmin, bmax, cs, w, h);
53 struct recast_heightfield *recast_newHeightfield(void)
55 return (struct recast_heightfield *) rcAllocHeightfield();
58 void recast_destroyHeightfield(struct recast_heightfield *heightfield)
60 rcFreeHeightField((rcHeightfield *) heightfield);
63 int recast_createHeightfield(struct recast_heightfield *hf, int width, int height,
64 const float *bmin, const float* bmax, float cs, float ch)
67 return rcCreateHeightfield(sctx, *(rcHeightfield *)hf, width, height, bmin, bmax, cs, ch);
70 void recast_markWalkableTriangles(const float walkableSlopeAngle,const float *verts, int nv,
71 const int *tris, int nt, unsigned char *flags)
74 rcMarkWalkableTriangles(sctx, walkableSlopeAngle, verts, nv, tris, nt, flags);
77 void recast_rasterizeTriangles(const float *verts, int nv, const int *tris,
78 const unsigned char *flags, int nt, struct recast_heightfield *solid)
81 rcRasterizeTriangles(sctx, verts, nv, tris, flags, nt, *(rcHeightfield *) solid);
84 void recast_filterLedgeSpans(const int walkableHeight, const int walkableClimb,
85 struct recast_heightfield *solid)
88 rcFilterLedgeSpans(sctx, walkableHeight, walkableClimb, *(rcHeightfield *) solid);
91 void recast_filterWalkableLowHeightSpans(int walkableHeight, struct recast_heightfield *solid)
94 rcFilterWalkableLowHeightSpans(sctx, walkableHeight, *(rcHeightfield *) solid);
97 void recast_filterLowHangingWalkableObstacles(const int walkableClimb, struct recast_heightfield *solid)
100 rcFilterLowHangingWalkableObstacles(sctx, walkableClimb, *(rcHeightfield *) solid);
103 struct recast_compactHeightfield *recast_newCompactHeightfield(void)
105 return (struct recast_compactHeightfield *) rcAllocCompactHeightfield();
108 void recast_destroyCompactHeightfield(struct recast_compactHeightfield *compactHeightfield)
110 rcFreeCompactHeightfield( (rcCompactHeightfield *) compactHeightfield);
113 int recast_buildCompactHeightfield(const int walkableHeight, const int walkableClimb,
114 struct recast_heightfield *hf, struct recast_compactHeightfield *chf)
117 return rcBuildCompactHeightfield(sctx, walkableHeight, walkableClimb,
118 *(rcHeightfield *) hf, *(rcCompactHeightfield *) chf);
121 int recast_erodeWalkableArea(int radius, struct recast_compactHeightfield *chf)
124 return rcErodeWalkableArea(sctx, radius, *(rcCompactHeightfield *) chf);
127 int recast_buildDistanceField(struct recast_compactHeightfield *chf)
130 return rcBuildDistanceField(sctx, *(rcCompactHeightfield *) chf);
133 int recast_buildRegions(struct recast_compactHeightfield *chf, int borderSize,
134 int minRegionSize, int mergeRegionSize)
137 return rcBuildRegions(sctx, *(rcCompactHeightfield *) chf, borderSize,
138 minRegionSize, mergeRegionSize);
141 struct recast_contourSet *recast_newContourSet(void)
143 return (struct recast_contourSet *) rcAllocContourSet();
146 void recast_destroyContourSet(struct recast_contourSet *contourSet)
148 rcFreeContourSet((rcContourSet *) contourSet);
151 int recast_buildContours(struct recast_compactHeightfield *chf,
152 const float maxError, const int maxEdgeLen, struct recast_contourSet *cset)
155 return rcBuildContours(sctx, *(rcCompactHeightfield *) chf, maxError, maxEdgeLen, *(rcContourSet *) cset);
158 struct recast_polyMesh *recast_newPolyMesh(void)
160 return (recast_polyMesh *) rcAllocPolyMesh();
163 void recast_destroyPolyMesh(struct recast_polyMesh *polyMesh)
165 rcFreePolyMesh((rcPolyMesh *) polyMesh);
168 int recast_buildPolyMesh(struct recast_contourSet *cset, int nvp, struct recast_polyMesh *mesh)
171 return rcBuildPolyMesh(sctx, *(rcContourSet *) cset, nvp, * (rcPolyMesh *) mesh);
174 unsigned short *recast_polyMeshGetVerts(struct recast_polyMesh *mesh, int *nverts)
176 rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
179 *nverts = pmesh->nverts;
184 void recast_polyMeshGetBoundbox(struct recast_polyMesh *mesh, float *bmin, float *bmax)
186 rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
189 bmin[0] = pmesh->bmin[0];
190 bmin[1] = pmesh->bmin[1];
191 bmin[2] = pmesh->bmin[2];
195 bmax[0] = pmesh->bmax[0];
196 bmax[1] = pmesh->bmax[1];
197 bmax[2] = pmesh->bmax[2];
201 void recast_polyMeshGetCell(struct recast_polyMesh *mesh, float *cs, float *ch)
203 rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
212 unsigned short *recast_polyMeshGetPolys(struct recast_polyMesh *mesh, int *npolys, int *nvp)
214 rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
217 *npolys = pmesh->npolys;
225 struct recast_polyMeshDetail *recast_newPolyMeshDetail(void)
227 return (struct recast_polyMeshDetail *) rcAllocPolyMeshDetail();
230 void recast_destroyPolyMeshDetail(struct recast_polyMeshDetail *polyMeshDetail)
232 rcFreePolyMeshDetail((rcPolyMeshDetail *) polyMeshDetail);
235 int recast_buildPolyMeshDetail(const struct recast_polyMesh *mesh, const struct recast_compactHeightfield *chf,
236 const float sampleDist, const float sampleMaxError, struct recast_polyMeshDetail *dmesh)
239 return rcBuildPolyMeshDetail(sctx, *(rcPolyMesh *) mesh, *(rcCompactHeightfield *) chf,
240 sampleDist, sampleMaxError, *(rcPolyMeshDetail *) dmesh);
243 float *recast_polyMeshDetailGetVerts(struct recast_polyMeshDetail *mesh, int *nverts)
245 rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
248 *nverts = dmesh->nverts;
253 unsigned char *recast_polyMeshDetailGetTris(struct recast_polyMeshDetail *mesh, int *ntris)
255 rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
258 *ntris = dmesh->ntris;
263 unsigned int *recast_polyMeshDetailGetMeshes(struct recast_polyMeshDetail *mesh, int *nmeshes)
265 rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
268 *nmeshes = dmesh->nmeshes;
270 return dmesh->meshes;
273 // qsort based on FreeBSD source (libkern\qsort.c)
274 typedef int cmp_t(void *, const void *, const void *);
275 static inline char *med3(char *, char *, char *, cmp_t *, void *);
276 static inline void swapfunc(char *, char *, int, int);
278 #define min(a, b) (a) < (b) ? a : b
279 #define swapcode(TYPE, parmi, parmj, n) \
281 long i = (n) / sizeof (TYPE); \
282 TYPE *pi = (TYPE *) (parmi); \
283 TYPE *pj = (TYPE *) (parmj); \
290 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
291 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
293 static inline void swapfunc(char* a, char* b, int n, int swaptype)
296 swapcode(long, a, b, n)
298 swapcode(char, a, b, n)
302 if (swaptype == 0) { \
303 long t = *(long *)(a); \
304 *(long *)(a) = *(long *)(b);\
307 swapfunc(a, b, es, swaptype)
309 #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
310 #define CMP(t, x, y) (cmp((t), (x), (y)))
312 static inline char * med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk)
314 return CMP(thunk, a, b) < 0 ?
315 (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
316 :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
319 void recast_qsort(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
321 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
322 int d, r, swaptype, swap_cnt;
328 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
330 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
335 pm = (char *)a + (n / 2) * es;
338 pn = (char *)a + (n - 1) * es;
341 pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
342 pm = med3(pm - d, pm, pm + d, cmp, thunk);
343 pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
345 pm = med3(pl, pm, pn, cmp, thunk);
348 pa = pb = (char *)a + es;
350 pc = pd = (char *)a + (n - 1) * es;
352 while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) {
360 while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) {
375 if (swap_cnt == 0) { /* Switch to insertion sort */
376 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
378 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
384 pn = (char *)a + n * es;
385 r = min(pa - (char *)a, pb - pa);
386 vecswap((char *)a, pb - r, r);
387 r = min(pd - pc, pn - pd - es);
388 vecswap(pb, pn - r, r);
389 if ((r = pb - pa) > es)
390 recast_qsort(a, r / es, es, thunk, cmp);
391 if ((r = pd - pc) > es) {
392 /* Iterate rather than recurse to save stack space */