diff options
author | Benoit Bolsee <benoit.bolsee@online.be> | 2011-09-30 01:38:57 +0400 |
---|---|---|
committer | Benoit Bolsee <benoit.bolsee@online.be> | 2011-09-30 01:38:57 +0400 |
commit | e6a9b68c798457430698afbfc7dfcdd8b71ca596 (patch) | |
tree | 64cc1ff496d8b3df6a04dfac078905a312952d8e /extern/recastnavigation/recast-capi.cpp | |
parent | e21e7895071eaa7484679639f39bb7f413bb4dcc (diff) |
Recast: upgrade library.
- Upgrade Recast library to latest portable version
- Implement recast_qsort based on FreeBSD qsort.c to have
portable thread safe quick sort for use in conversion routine.
- Better default value for the Build Navigation Mesh operator
Diffstat (limited to 'extern/recastnavigation/recast-capi.cpp')
-rw-r--r-- | extern/recastnavigation/recast-capi.cpp | 211 |
1 files changed, 179 insertions, 32 deletions
diff --git a/extern/recastnavigation/recast-capi.cpp b/extern/recastnavigation/recast-capi.cpp index 2348497b0d7..42a9a4b82c8 100644 --- a/extern/recastnavigation/recast-capi.cpp +++ b/extern/recastnavigation/recast-capi.cpp @@ -30,6 +30,11 @@ #include <math.h> #include "Recast.h" +static rcContext *sctx; + +#define INIT_SCTX() \ + if (sctx == NULL) sctx = new rcContext(false) + int recast_buildMeshAdjacency(unsigned short* polys, const int npolys, const int nverts, const int vertsPerPoly) { @@ -48,109 +53,123 @@ void recast_calcGridSize(const float *bmin, const float *bmax, float cs, int *w, struct recast_heightfield *recast_newHeightfield(void) { - return (struct recast_heightfield *) (new rcHeightfield); + return (struct recast_heightfield *) rcAllocHeightfield(); } void recast_destroyHeightfield(struct recast_heightfield *heightfield) { - delete (rcHeightfield *) heightfield; + rcFreeHeightField((rcHeightfield *) heightfield); } int recast_createHeightfield(struct recast_heightfield *hf, int width, int height, const float *bmin, const float* bmax, float cs, float ch) { - return rcCreateHeightfield(*(rcHeightfield *)hf, width, height, bmin, bmax, cs, ch); + INIT_SCTX(); + return rcCreateHeightfield(sctx, *(rcHeightfield *)hf, width, height, bmin, bmax, cs, ch); } void recast_markWalkableTriangles(const float walkableSlopeAngle,const float *verts, int nv, const int *tris, int nt, unsigned char *flags) { - rcMarkWalkableTriangles(walkableSlopeAngle, verts, nv, tris, nt, flags); + INIT_SCTX(); + rcMarkWalkableTriangles(sctx, walkableSlopeAngle, verts, nv, tris, nt, flags); } void recast_rasterizeTriangles(const float *verts, int nv, const int *tris, const unsigned char *flags, int nt, struct recast_heightfield *solid) { - rcRasterizeTriangles(verts, nv, tris, flags, nt, *(rcHeightfield *) solid); + INIT_SCTX(); + rcRasterizeTriangles(sctx, verts, nv, tris, flags, nt, *(rcHeightfield *) solid); } void recast_filterLedgeSpans(const int walkableHeight, const int walkableClimb, struct recast_heightfield *solid) { - rcFilterLedgeSpans(walkableHeight, walkableClimb, *(rcHeightfield *) solid); + INIT_SCTX(); + rcFilterLedgeSpans(sctx, walkableHeight, walkableClimb, *(rcHeightfield *) solid); } void recast_filterWalkableLowHeightSpans(int walkableHeight, struct recast_heightfield *solid) { - rcFilterWalkableLowHeightSpans(walkableHeight, *(rcHeightfield *) solid); + INIT_SCTX(); + rcFilterWalkableLowHeightSpans(sctx, walkableHeight, *(rcHeightfield *) solid); +} + +void recast_filterLowHangingWalkableObstacles(const int walkableClimb, struct recast_heightfield *solid) +{ + INIT_SCTX(); + rcFilterLowHangingWalkableObstacles(sctx, walkableClimb, *(rcHeightfield *) solid); } struct recast_compactHeightfield *recast_newCompactHeightfield(void) { - return (struct recast_compactHeightfield *) (new rcCompactHeightfield); + return (struct recast_compactHeightfield *) rcAllocCompactHeightfield(); } void recast_destroyCompactHeightfield(struct recast_compactHeightfield *compactHeightfield) { - delete (rcCompactHeightfield *) compactHeightfield; + rcFreeCompactHeightfield( (rcCompactHeightfield *) compactHeightfield); } int recast_buildCompactHeightfield(const int walkableHeight, const int walkableClimb, - unsigned char flags, struct recast_heightfield *hf, struct recast_compactHeightfield *chf) + struct recast_heightfield *hf, struct recast_compactHeightfield *chf) { - int rcFlags = 0; - - if(flags & RECAST_WALKABLE) - rcFlags |= RC_WALKABLE; - - if(flags & RECAST_REACHABLE) - rcFlags |= RC_REACHABLE; + INIT_SCTX(); + return rcBuildCompactHeightfield(sctx, walkableHeight, walkableClimb, + *(rcHeightfield *) hf, *(rcCompactHeightfield *) chf); +} - return rcBuildCompactHeightfield(walkableHeight, walkableClimb, rcFlags, - *(rcHeightfield *) hf, *(rcCompactHeightfield *) chf); +int recast_erodeWalkableArea(int radius, struct recast_compactHeightfield *chf) +{ + INIT_SCTX(); + return rcErodeWalkableArea(sctx, radius, *(rcCompactHeightfield *) chf); } int recast_buildDistanceField(struct recast_compactHeightfield *chf) { - return rcBuildDistanceField(*(rcCompactHeightfield *) chf); + INIT_SCTX(); + return rcBuildDistanceField(sctx, *(rcCompactHeightfield *) chf); } -int recast_buildRegions(struct recast_compactHeightfield *chf, int walkableRadius, int borderSize, +int recast_buildRegions(struct recast_compactHeightfield *chf, int borderSize, int minRegionSize, int mergeRegionSize) { - return rcBuildRegions(*(rcCompactHeightfield *) chf, walkableRadius, borderSize, + INIT_SCTX(); + return rcBuildRegions(sctx, *(rcCompactHeightfield *) chf, borderSize, minRegionSize, mergeRegionSize); } struct recast_contourSet *recast_newContourSet(void) { - return (struct recast_contourSet *) (new rcContourSet); + return (struct recast_contourSet *) rcAllocContourSet(); } void recast_destroyContourSet(struct recast_contourSet *contourSet) { - delete (rcContourSet *) contourSet; + rcFreeContourSet((rcContourSet *) contourSet); } int recast_buildContours(struct recast_compactHeightfield *chf, const float maxError, const int maxEdgeLen, struct recast_contourSet *cset) { - return rcBuildContours(*(rcCompactHeightfield *) chf, maxError, maxEdgeLen, *(rcContourSet *) cset); + INIT_SCTX(); + return rcBuildContours(sctx, *(rcCompactHeightfield *) chf, maxError, maxEdgeLen, *(rcContourSet *) cset); } struct recast_polyMesh *recast_newPolyMesh(void) { - return (recast_polyMesh *) (new rcPolyMesh); + return (recast_polyMesh *) rcAllocPolyMesh(); } void recast_destroyPolyMesh(struct recast_polyMesh *polyMesh) { - delete (rcPolyMesh *) polyMesh; + rcFreePolyMesh((rcPolyMesh *) polyMesh); } int recast_buildPolyMesh(struct recast_contourSet *cset, int nvp, struct recast_polyMesh *mesh) { - return rcBuildPolyMesh(*(rcContourSet *) cset, nvp, * (rcPolyMesh *) mesh); + INIT_SCTX(); + return rcBuildPolyMesh(sctx, *(rcContourSet *) cset, nvp, * (rcPolyMesh *) mesh); } unsigned short *recast_polyMeshGetVerts(struct recast_polyMesh *mesh, int *nverts) @@ -206,18 +225,19 @@ unsigned short *recast_polyMeshGetPolys(struct recast_polyMesh *mesh, int *npoly struct recast_polyMeshDetail *recast_newPolyMeshDetail(void) { - return (struct recast_polyMeshDetail *) (new rcPolyMeshDetail); + return (struct recast_polyMeshDetail *) rcAllocPolyMeshDetail(); } void recast_destroyPolyMeshDetail(struct recast_polyMeshDetail *polyMeshDetail) { - delete (rcPolyMeshDetail *) polyMeshDetail; + rcFreePolyMeshDetail((rcPolyMeshDetail *) polyMeshDetail); } int recast_buildPolyMeshDetail(const struct recast_polyMesh *mesh, const struct recast_compactHeightfield *chf, const float sampleDist, const float sampleMaxError, struct recast_polyMeshDetail *dmesh) { - return rcBuildPolyMeshDetail(*(rcPolyMesh *) mesh, *(rcCompactHeightfield *) chf, + INIT_SCTX(); + return rcBuildPolyMeshDetail(sctx, *(rcPolyMesh *) mesh, *(rcCompactHeightfield *) chf, sampleDist, sampleMaxError, *(rcPolyMeshDetail *) dmesh); } @@ -241,7 +261,7 @@ unsigned char *recast_polyMeshDetailGetTris(struct recast_polyMeshDetail *mesh, return dmesh->tris; } -unsigned short *recast_polyMeshDetailGetMeshes(struct recast_polyMeshDetail *mesh, int *nmeshes) +unsigned int *recast_polyMeshDetailGetMeshes(struct recast_polyMeshDetail *mesh, int *nmeshes) { rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh; @@ -250,3 +270,130 @@ unsigned short *recast_polyMeshDetailGetMeshes(struct recast_polyMeshDetail *mes return dmesh->meshes; } + +// qsort based on FreeBSD source (libkern\qsort.c) +typedef int cmp_t(void *, const void *, const void *); +static inline char *med3(char *, char *, char *, cmp_t *, void *); +static inline void swapfunc(char *, char *, int, int); + +#define min(a, b) (a) < (b) ? a : b +#define swapcode(TYPE, parmi, parmj, n) \ +{ \ + long i = (n) / sizeof (TYPE); \ + TYPE *pi = (TYPE *) (parmi); \ + TYPE *pj = (TYPE *) (parmj); \ + do { \ + TYPE t = *pi; \ + *pi++ = *pj; \ + *pj++ = t; \ + } while (--i > 0); \ +} +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; + +static inline void swapfunc(char* a, char* b, int n, int swaptype) +{ + if(swaptype <= 1) + swapcode(long, a, b, n) + else + swapcode(char, a, b, n) +} + +#define swap(a, b) \ + if (swaptype == 0) { \ + long t = *(long *)(a); \ + *(long *)(a) = *(long *)(b);\ + *(long *)(b) = t; \ + } else \ + swapfunc(a, b, es, swaptype) + +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) +#define CMP(t, x, y) (cmp((t), (x), (y))) + +static inline char * med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk) +{ + return CMP(thunk, a, b) < 0 ? + (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a )) + :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); +} + +void recast_qsort(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) +{ + char *pa, *pb, *pc, *pd, *pl, *pm, *pn; + int d, r, swaptype, swap_cnt; + +loop: + SWAPINIT(a, es); + swap_cnt = 0; + if (n < 7) { + for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) + for (pl = pm; + pl > (char *)a && CMP(thunk, pl - es, pl) > 0; + pl -= es) + swap(pl, pl - es); + return; + } + pm = (char *)a + (n / 2) * es; + if (n > 7) { + pl = (char *)a; + pn = (char *)a + (n - 1) * es; + if (n > 40) { + d = (n / 8) * es; + pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk); + pm = med3(pm - d, pm, pm + d, cmp, thunk); + pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk); + } + pm = med3(pl, pm, pn, cmp, thunk); + } + swap((char *)a, pm); + pa = pb = (char *)a + es; + + pc = pd = (char *)a + (n - 1) * es; + for (;;) { + while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) { + if (r == 0) { + swap_cnt = 1; + swap(pa, pb); + pa += es; + } + pb += es; + } + while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) { + if (r == 0) { + swap_cnt = 1; + swap(pc, pd); + pd -= es; + } + pc -= es; + } + if (pb > pc) + break; + swap(pb, pc); + swap_cnt = 1; + pb += es; + pc -= es; + } + if (swap_cnt == 0) { /* Switch to insertion sort */ + for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) + for (pl = pm; + pl > (char *)a && CMP(thunk, pl - es, pl) > 0; + pl -= es) + swap(pl, pl - es); + return; + } + + pn = (char *)a + n * es; + r = min(pa - (char *)a, pb - pa); + vecswap((char *)a, pb - r, r); + r = min(pd - pc, pn - pd - es); + vecswap(pb, pn - r, r); + if ((r = pb - pa) > es) + recast_qsort(a, r / es, es, thunk, cmp); + if ((r = pd - pc) > es) { + /* Iterate rather than recurse to save stack space */ + a = pn - r; + n = r / es; + goto loop; + } +} + |