CMake: cleanup
[blender-staging.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 *areas)
72 {
73         INIT_SCTX();
74         rcMarkWalkableTriangles(sctx, walkableSlopeAngle, verts, nv, tris, nt, areas);
75 }
76
77 void recast_clearUnwalkableTriangles(const float walkableSlopeAngle, const float* verts, int nv,
78                         const int* tris, int nt, unsigned char* areas)
79 {
80         INIT_SCTX();
81         rcClearUnwalkableTriangles(sctx, walkableSlopeAngle, verts, nv, tris, nt, areas);
82 }
83
84 int recast_addSpan(struct recast_heightfield *hf, const int x, const int y,
85                         const unsigned short smin, const unsigned short smax,
86                         const unsigned char area, const int flagMergeThr)
87 {
88         INIT_SCTX();
89         return rcAddSpan(sctx, *(rcHeightfield *) hf, x, y, smin, smax, area, flagMergeThr);
90 }
91
92 int recast_rasterizeTriangle(const float *v0, const float *v1, const float *v2,
93                         const unsigned char area, struct recast_heightfield *solid,
94                         const int flagMergeThr)
95 {
96         INIT_SCTX();
97         return rcRasterizeTriangle(sctx, v0, v1, v2, area, *(rcHeightfield *) solid, flagMergeThr);
98 }
99
100 int recast_rasterizeTriangles(const float *verts, const int nv, const int *tris,
101                         const unsigned char *areas, const int nt, struct recast_heightfield *solid,
102                         const int flagMergeThr)
103 {
104         INIT_SCTX();
105         return rcRasterizeTriangles(sctx, verts, nv, tris, areas, nt, *(rcHeightfield *) solid, flagMergeThr);
106 }
107
108 void recast_filterLedgeSpans(const int walkableHeight, const int walkableClimb,
109                         struct recast_heightfield *solid)
110 {
111         INIT_SCTX();
112         rcFilterLedgeSpans(sctx, walkableHeight, walkableClimb, *(rcHeightfield *) solid);
113 }
114
115 void recast_filterWalkableLowHeightSpans(int walkableHeight, struct recast_heightfield *solid)
116 {
117         INIT_SCTX();
118         rcFilterWalkableLowHeightSpans(sctx, walkableHeight, *(rcHeightfield *) solid);
119 }
120
121 void recast_filterLowHangingWalkableObstacles(const int walkableClimb, struct recast_heightfield *solid)
122 {
123         INIT_SCTX();
124         rcFilterLowHangingWalkableObstacles(sctx, walkableClimb, *(rcHeightfield *) solid);
125 }
126
127 int recast_getHeightFieldSpanCount(struct recast_heightfield *hf)
128 {
129         INIT_SCTX();
130         return rcGetHeightFieldSpanCount(sctx, *(rcHeightfield *) hf);
131 }
132
133 struct recast_heightfieldLayerSet *recast_newHeightfieldLayerSet(void)
134 {
135         return (struct recast_heightfieldLayerSet *) rcAllocHeightfieldLayerSet();
136 }
137
138 void recast_destroyHeightfieldLayerSet(struct recast_heightfieldLayerSet *lset)
139 {
140         rcFreeHeightfieldLayerSet( (rcHeightfieldLayerSet *) lset);
141 }
142
143 struct recast_compactHeightfield *recast_newCompactHeightfield(void)
144 {
145         return (struct recast_compactHeightfield *) rcAllocCompactHeightfield();
146 }
147
148 void recast_destroyCompactHeightfield(struct recast_compactHeightfield *compactHeightfield)
149 {
150         rcFreeCompactHeightfield( (rcCompactHeightfield *) compactHeightfield);
151 }
152
153 int recast_buildCompactHeightfield(const int walkableHeight, const int walkableClimb,
154                         struct recast_heightfield *hf, struct recast_compactHeightfield *chf)
155 {
156         INIT_SCTX();
157         return rcBuildCompactHeightfield(sctx, walkableHeight, walkableClimb, 
158                 *(rcHeightfield *) hf, *(rcCompactHeightfield *) chf);
159 }
160
161 int recast_erodeWalkableArea(int radius, struct recast_compactHeightfield *chf)
162 {
163         INIT_SCTX();
164         return rcErodeWalkableArea(sctx, radius, *(rcCompactHeightfield *) chf);
165 }
166
167 int recast_medianFilterWalkableArea(struct recast_compactHeightfield *chf)
168 {
169         INIT_SCTX();
170         return rcMedianFilterWalkableArea(sctx, *(rcCompactHeightfield *) chf);
171 }
172
173 void recast_markBoxArea(const float *bmin, const float *bmax, unsigned char areaId,
174                         struct recast_compactHeightfield *chf)
175 {
176         INIT_SCTX();
177         rcMarkBoxArea(sctx, bmin, bmax, areaId, *(rcCompactHeightfield *) chf);
178 }
179
180 void recast_markConvexPolyArea(const float* verts, const int nverts,
181                         const float hmin, const float hmax, unsigned char areaId,
182                         struct recast_compactHeightfield *chf)
183 {
184         INIT_SCTX();
185         rcMarkConvexPolyArea(sctx, verts, nverts, hmin, hmax, areaId, *(rcCompactHeightfield *) chf);
186 }
187
188 int recast_offsetPoly(const float* verts, const int nverts,
189                         const float offset, float *outVerts, const int maxOutVerts)
190 {
191         return rcOffsetPoly(verts, nverts, offset, outVerts, maxOutVerts);
192 }
193
194 void recast_markCylinderArea(const float* pos, const float r, const float h,
195                         unsigned char areaId, struct recast_compactHeightfield *chf)
196 {
197         INIT_SCTX();
198         rcMarkCylinderArea(sctx, pos, r, h, areaId, *(rcCompactHeightfield *) chf);
199 }
200
201 int recast_buildDistanceField(struct recast_compactHeightfield *chf)
202 {
203         INIT_SCTX();
204         return rcBuildDistanceField(sctx, *(rcCompactHeightfield *) chf);
205 }
206
207 int recast_buildRegions(struct recast_compactHeightfield *chf,
208                         const int borderSize, const int minRegionArea, const int mergeRegionArea)
209 {
210         INIT_SCTX();
211         return rcBuildRegions(sctx, *(rcCompactHeightfield *) chf, borderSize,
212                                 minRegionArea, mergeRegionArea);
213 }
214
215 int recast_buildLayerRegions(struct recast_compactHeightfield *chf,
216                         const int borderSize, const int minRegionArea)
217 {
218         INIT_SCTX();
219         return rcBuildLayerRegions(sctx, *(rcCompactHeightfield *) chf, borderSize,
220                                 minRegionArea);
221 }
222
223 int recast_buildRegionsMonotone(struct recast_compactHeightfield *chf,
224                         const int borderSize, const int minRegionArea, const int mergeRegionArea)
225 {
226         INIT_SCTX();
227         return rcBuildRegionsMonotone(sctx, *(rcCompactHeightfield *) chf, borderSize,
228                                 minRegionArea, mergeRegionArea);
229 }
230
231 struct recast_contourSet *recast_newContourSet(void)
232 {
233         return (struct recast_contourSet *) rcAllocContourSet();
234 }
235
236 void recast_destroyContourSet(struct recast_contourSet *contourSet)
237 {
238         rcFreeContourSet((rcContourSet *) contourSet);
239 }
240
241 int recast_buildContours(struct recast_compactHeightfield *chf,
242                         const float maxError, const int maxEdgeLen, struct recast_contourSet *cset,
243                         const int buildFlags)
244 {
245         INIT_SCTX();
246         return rcBuildContours(sctx, *(rcCompactHeightfield *) chf, maxError, maxEdgeLen, *(rcContourSet *) cset, buildFlags);
247 }
248
249 struct recast_polyMesh *recast_newPolyMesh(void)
250 {
251         return (recast_polyMesh *) rcAllocPolyMesh();
252 }
253
254 void recast_destroyPolyMesh(struct recast_polyMesh *polyMesh)
255 {
256         rcFreePolyMesh((rcPolyMesh *) polyMesh);
257 }
258
259 int recast_buildPolyMesh(struct recast_contourSet *cset, const int nvp, struct recast_polyMesh *mesh)
260 {
261         INIT_SCTX();
262         return rcBuildPolyMesh(sctx, *(rcContourSet *) cset, nvp, *(rcPolyMesh *) mesh);
263 }
264
265 int recast_mergePolyMeshes(struct recast_polyMesh **meshes, const int nmeshes, struct recast_polyMesh *mesh)
266 {
267         INIT_SCTX();
268         return rcMergePolyMeshes(sctx, (rcPolyMesh **) meshes, nmeshes, *(rcPolyMesh *) mesh);
269 }
270
271 int recast_copyPolyMesh(const struct recast_polyMesh *src, struct recast_polyMesh *dst)
272 {
273         INIT_SCTX();
274         return rcCopyPolyMesh(sctx, *(const rcPolyMesh *) src, *(rcPolyMesh *) dst);
275 }
276
277 unsigned short *recast_polyMeshGetVerts(struct recast_polyMesh *mesh, int *nverts)
278 {
279         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
280
281         if (nverts)
282                 *nverts = pmesh->nverts;
283
284         return pmesh->verts;
285 }
286
287 void recast_polyMeshGetBoundbox(struct recast_polyMesh *mesh, float *bmin, float *bmax)
288 {
289         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
290
291         if (bmin) {
292                 bmin[0] = pmesh->bmin[0];
293                 bmin[1] = pmesh->bmin[1];
294                 bmin[2] = pmesh->bmin[2];
295         }
296
297         if (bmax) {
298                 bmax[0] = pmesh->bmax[0];
299                 bmax[1] = pmesh->bmax[1];
300                 bmax[2] = pmesh->bmax[2];
301         }
302 }
303
304 void recast_polyMeshGetCell(struct recast_polyMesh *mesh, float *cs, float *ch)
305 {
306         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
307
308         if (cs)
309                 *cs = pmesh->cs;
310
311         if (ch)
312                 *ch = pmesh->ch;
313 }
314
315 unsigned short *recast_polyMeshGetPolys(struct recast_polyMesh *mesh, int *npolys, int *nvp)
316 {
317         rcPolyMesh *pmesh = (rcPolyMesh *)mesh;
318
319         if (npolys)
320                 *npolys = pmesh->npolys;
321
322         if (nvp)
323                 *nvp = pmesh->nvp;
324
325         return pmesh->polys;
326 }
327
328 struct recast_polyMeshDetail *recast_newPolyMeshDetail(void)
329 {
330         return (struct recast_polyMeshDetail *) rcAllocPolyMeshDetail();
331 }
332
333 void recast_destroyPolyMeshDetail(struct recast_polyMeshDetail *polyMeshDetail)
334 {
335         rcFreePolyMeshDetail((rcPolyMeshDetail *) polyMeshDetail);
336 }
337
338 int recast_buildPolyMeshDetail(const struct recast_polyMesh *mesh, const struct recast_compactHeightfield *chf,
339                         const float sampleDist, const float sampleMaxError, struct recast_polyMeshDetail *dmesh)
340 {
341         INIT_SCTX();
342         return rcBuildPolyMeshDetail(sctx, *(rcPolyMesh *) mesh, *(rcCompactHeightfield *) chf,
343                         sampleDist, sampleMaxError, *(rcPolyMeshDetail *) dmesh);
344 }
345
346 int recast_mergePolyMeshDetails(struct recast_polyMeshDetail **meshes, const int nmeshes, struct recast_polyMeshDetail *mesh)
347 {
348         INIT_SCTX();
349         return rcMergePolyMeshDetails(sctx, (rcPolyMeshDetail **) meshes, nmeshes, *(rcPolyMeshDetail *) mesh);
350 }
351
352 float *recast_polyMeshDetailGetVerts(struct recast_polyMeshDetail *mesh, int *nverts)
353 {
354         rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
355
356         if (nverts)
357                 *nverts = dmesh->nverts;
358
359         return dmesh->verts;
360 }
361
362 unsigned char *recast_polyMeshDetailGetTris(struct recast_polyMeshDetail *mesh, int *ntris)
363 {
364         rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
365
366         if (ntris)
367                 *ntris = dmesh->ntris;
368
369         return dmesh->tris;
370 }
371
372 unsigned int *recast_polyMeshDetailGetMeshes(struct recast_polyMeshDetail *mesh, int *nmeshes)
373 {
374         rcPolyMeshDetail *dmesh = (rcPolyMeshDetail *)mesh;
375
376         if (nmeshes)
377                 *nmeshes = dmesh->nmeshes;
378
379         return dmesh->meshes;
380 }
381