Merging r51923 through r52851 from trunk into soc-2011-tomato
[blender.git] / extern / recastnavigation / recast-capi.cpp
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
27 #include "recast-capi.h"
28
29 #include <math.h>
30 #include "Recast.h"
31
32 static rcContext *sctx;
33
34 #define INIT_SCTX()                     \
35         if (sctx == NULL) sctx = new rcContext(false)
36
37 int recast_buildMeshAdjacency(unsigned short* polys, const int npolys,
38                         const int nverts, const int vertsPerPoly)
39 {
40         return (int) buildMeshAdjacency(polys, npolys, nverts, vertsPerPoly);
41 }
42
43 void recast_calcBounds(const float *verts, int nv, float *bmin, float *bmax)
44 {
45         rcCalcBounds(verts, nv, bmin, bmax);
46 }
47
48 void recast_calcGridSize(const float *bmin, const float *bmax, float cs, int *w, int *h)
49 {
50         rcCalcGridSize(bmin, bmax, cs, w, h);
51 }
52
53 struct recast_heightfield *recast_newHeightfield(void)
54 {
55         return (struct recast_heightfield *) rcAllocHeightfield();
56 }
57
58 void recast_destroyHeightfield(struct recast_heightfield *heightfield)
59 {
60         rcFreeHeightField((rcHeightfield *) heightfield);
61 }
62
63 int recast_createHeightfield(struct recast_heightfield *hf, int width, int height,
64                         const float *bmin, const float* bmax, float cs, float ch)
65 {
66         INIT_SCTX();
67         return rcCreateHeightfield(sctx, *(rcHeightfield *)hf, width, height, bmin, bmax, cs, ch);
68 }
69
70 void recast_markWalkableTriangles(const float walkableSlopeAngle,const float *verts, int nv,
71                         const int *tris, int nt, unsigned char *flags)
72 {
73         INIT_SCTX();
74         rcMarkWalkableTriangles(sctx, walkableSlopeAngle, verts, nv, tris, nt, flags);
75 }
76
77 void recast_rasterizeTriangles(const float *verts, int nv, const int *tris,
78                         const unsigned char *flags, int nt, struct recast_heightfield *solid)
79 {
80         INIT_SCTX();
81         rcRasterizeTriangles(sctx, verts, nv, tris, flags, nt, *(rcHeightfield *) solid);
82 }
83
84 void recast_filterLedgeSpans(const int walkableHeight, const int walkableClimb,
85                         struct recast_heightfield *solid)
86 {
87         INIT_SCTX();
88         rcFilterLedgeSpans(sctx, walkableHeight, walkableClimb, *(rcHeightfield *) solid);
89 }
90
91 void recast_filterWalkableLowHeightSpans(int walkableHeight, struct recast_heightfield *solid)
92 {
93         INIT_SCTX();
94         rcFilterWalkableLowHeightSpans(sctx, walkableHeight, *(rcHeightfield *) solid);
95 }
96
97 void recast_filterLowHangingWalkableObstacles(const int walkableClimb, struct recast_heightfield *solid)
98 {
99         INIT_SCTX();
100         rcFilterLowHangingWalkableObstacles(sctx, walkableClimb, *(rcHeightfield *) solid);
101 }
102
103 struct recast_compactHeightfield *recast_newCompactHeightfield(void)
104 {
105         return (struct recast_compactHeightfield *) rcAllocCompactHeightfield();
106 }
107
108 void recast_destroyCompactHeightfield(struct recast_compactHeightfield *compactHeightfield)
109 {
110         rcFreeCompactHeightfield( (rcCompactHeightfield *) compactHeightfield);
111 }
112
113 int recast_buildCompactHeightfield(const int walkableHeight, const int walkableClimb,
114                         struct recast_heightfield *hf, struct recast_compactHeightfield *chf)
115 {
116         INIT_SCTX();
117         return rcBuildCompactHeightfield(sctx, walkableHeight, walkableClimb, 
118                 *(rcHeightfield *) hf, *(rcCompactHeightfield *) chf);
119 }
120
121 int recast_erodeWalkableArea(int radius, struct recast_compactHeightfield *chf)
122 {
123         INIT_SCTX();
124         return rcErodeWalkableArea(sctx, radius, *(rcCompactHeightfield *) chf);
125 }
126
127 int recast_buildDistanceField(struct recast_compactHeightfield *chf)
128 {
129         INIT_SCTX();
130         return rcBuildDistanceField(sctx, *(rcCompactHeightfield *) chf);
131 }
132
133 int recast_buildRegions(struct recast_compactHeightfield *chf, int borderSize,
134         int minRegionSize, int mergeRegionSize)
135 {
136         INIT_SCTX();
137         return rcBuildRegions(sctx, *(rcCompactHeightfield *) chf, borderSize,
138                                 minRegionSize, mergeRegionSize);
139 }
140
141 struct recast_contourSet *recast_newContourSet(void)
142 {
143         return (struct recast_contourSet *) rcAllocContourSet();
144 }
145
146 void recast_destroyContourSet(struct recast_contourSet *contourSet)
147 {
148         rcFreeContourSet((rcContourSet *) contourSet);
149 }
150
151 int recast_buildContours(struct recast_compactHeightfield *chf,
152                         const float maxError, const int maxEdgeLen, struct recast_contourSet *cset)
153 {
154         INIT_SCTX();
155         return rcBuildContours(sctx, *(rcCompactHeightfield *) chf, maxError, maxEdgeLen, *(rcContourSet *) cset);
156 }
157
158 struct recast_polyMesh *recast_newPolyMesh(void)
159 {
160         return (recast_polyMesh *) rcAllocPolyMesh();
161 }
162
163 void recast_destroyPolyMesh(struct recast_polyMesh *polyMesh)
164 {
165         rcFreePolyMesh((rcPolyMesh *) polyMesh);
166 }
167
168 int recast_buildPolyMesh(struct recast_contourSet *cset, int nvp, struct recast_polyMesh *mesh)
169 {
170         INIT_SCTX();
171         return rcBuildPolyMesh(sctx, *(rcContourSet *) cset, nvp, * (rcPolyMesh *) mesh);
172 }
173
174 unsigned short *recast_polyMeshGetVerts(struct recast_polyMesh *mesh, int *nverts)
175 {
176         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
177
178         if (nverts)
179                 *nverts = pmesh->nverts;
180
181         return pmesh->verts;
182 }
183
184 void recast_polyMeshGetBoundbox(struct recast_polyMesh *mesh, float *bmin, float *bmax)
185 {
186         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
187
188         if (bmin) {
189                 bmin[0] = pmesh->bmin[0];
190                 bmin[1] = pmesh->bmin[1];
191                 bmin[2] = pmesh->bmin[2];
192         }
193
194         if (bmax) {
195                 bmax[0] = pmesh->bmax[0];
196                 bmax[1] = pmesh->bmax[1];
197                 bmax[2] = pmesh->bmax[2];
198         }
199 }
200
201 void recast_polyMeshGetCell(struct recast_polyMesh *mesh, float *cs, float *ch)
202 {
203         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
204
205         if (cs)
206                 *cs = pmesh->cs;
207
208         if (ch)
209                 *ch = pmesh->ch;
210 }
211
212 unsigned short *recast_polyMeshGetPolys(struct recast_polyMesh *mesh, int *npolys, int *nvp)
213 {
214         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
215
216         if (npolys)
217                 *npolys = pmesh->npolys;
218
219         if (nvp)
220                 *nvp = pmesh->nvp;
221
222         return pmesh->polys;
223 }
224
225 struct recast_polyMeshDetail *recast_newPolyMeshDetail(void)
226 {
227         return (struct recast_polyMeshDetail *) rcAllocPolyMeshDetail();
228 }
229
230 void recast_destroyPolyMeshDetail(struct recast_polyMeshDetail *polyMeshDetail)
231 {
232         rcFreePolyMeshDetail((rcPolyMeshDetail *) polyMeshDetail);
233 }
234
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)
237 {
238         INIT_SCTX();
239         return rcBuildPolyMeshDetail(sctx, *(rcPolyMesh *) mesh, *(rcCompactHeightfield *) chf,
240                         sampleDist, sampleMaxError, *(rcPolyMeshDetail *) dmesh);
241 }
242
243 float *recast_polyMeshDetailGetVerts(struct recast_polyMeshDetail *mesh, int *nverts)
244 {
245         rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
246
247         if (nverts)
248                 *nverts = dmesh->nverts;
249
250         return dmesh->verts;
251 }
252
253 unsigned char *recast_polyMeshDetailGetTris(struct recast_polyMeshDetail *mesh, int *ntris)
254 {
255         rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
256
257         if (ntris)
258                 *ntris = dmesh->ntris;
259
260         return dmesh->tris;
261 }
262
263 unsigned int *recast_polyMeshDetailGetMeshes(struct recast_polyMeshDetail *mesh, int *nmeshes)
264 {
265         rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
266
267         if (nmeshes)
268                 *nmeshes = dmesh->nmeshes;
269
270         return dmesh->meshes;
271 }
272
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);
277
278 #define min(a, b)       (a) < (b) ? a : b
279 #define swapcode(TYPE, parmi, parmj, n)         \
280 {                                                                                       \
281         long i = (n) / sizeof(TYPE);                    \
282         TYPE *pi = (TYPE *) (parmi);                    \
283         TYPE *pj = (TYPE *) (parmj);                    \
284         do {                                                                    \
285                 TYPE    t = *pi;                                        \
286                 *pi++ = *pj;                                            \
287                 *pj++ = t;                                                      \
288         } while (--i > 0);                                              \
289 }
290 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
291         es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
292
293 static inline void swapfunc(char* a, char* b, int n, int swaptype)
294 {
295         if(swaptype <= 1)
296                 swapcode(long, a, b, n)
297         else
298         swapcode(char, a, b, n)
299 }
300
301 #define swap(a, b)                                      \
302         if (swaptype == 0) {                    \
303                 long t = *(long *)(a);          \
304                 *(long *)(a) = *(long *)(b);\
305                 *(long *)(b) = t;                       \
306         } else                                                  \
307                 swapfunc(a, b, es, swaptype)
308
309 #define vecswap(a, b, n)        if ((n) > 0) swapfunc(a, b, n, swaptype)
310 #define CMP(t, x, y) (cmp((t), (x), (y)))
311
312 static inline char * med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk)
313 {
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 ));
317 }
318
319 void recast_qsort(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
320 {
321         char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
322         int d, r, swaptype, swap_cnt;
323
324 loop:   
325         SWAPINIT(a, es);
326         swap_cnt = 0;
327         if (n < 7) {
328                 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
329                         for (pl = pm; 
330                                 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
331                                 pl -= es)
332                                 swap(pl, pl - es);
333                 return;
334         }
335         pm = (char *)a + (n / 2) * es;
336         if (n > 7) {
337                 pl = (char *)a;
338                 pn = (char *)a + (n - 1) * es;
339                 if (n > 40) {
340                         d = (n / 8) * 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);
344                 }
345                 pm = med3(pl, pm, pn, cmp, thunk);
346         }
347         swap((char *)a, pm);
348         pa = pb = (char *)a + es;
349
350         pc = pd = (char *)a + (n - 1) * es;
351         for (;;) {
352                 while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) {
353                         if (r == 0) {
354                                 swap_cnt = 1;
355                                 swap(pa, pb);
356                                 pa += es;
357                         }
358                         pb += es;
359                 }
360                 while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) {
361                         if (r == 0) {
362                                 swap_cnt = 1;
363                                 swap(pc, pd);
364                                 pd -= es;
365                         }
366                         pc -= es;
367                 }
368                 if (pb > pc)
369                         break;
370                 swap(pb, pc);
371                 swap_cnt = 1;
372                 pb += es;
373                 pc -= es;
374         }
375         if (swap_cnt == 0) {  /* Switch to insertion sort */
376                 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
377                         for (pl = pm; 
378                                 pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
379                                 pl -= es)
380                                 swap(pl, pl - es);
381                 return;
382         }
383
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 */
393                 a = pn - r;
394                 n = r / es;
395                 goto loop;
396         }
397 }
398