SCons: Don't compile libmv stub when libmv is actually enabled
[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