1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <math.h>
#include "Recast.h"
extern "C"{
#include "ED_navmesh_conversion.h"
#include "DNA_meshdata_types.h"
#include "BKE_cdderivedmesh.h"
#include "BLI_math.h"
}
int polyNumVerts(const unsigned short* p, const int vertsPerPoly)
{
int nv = 0;
for (int i=0; i<vertsPerPoly; i++)
{
if (p[i]==0xffff)
break;
nv++;
}
return nv;
}
bool polyIsConvex(const unsigned short* p, const int vertsPerPoly, const float* verts)
{
int nv = polyNumVerts(p, vertsPerPoly);
if (nv<3)
return false;
for (int j=0; j<nv; j++)
{
const float* v = &verts[3*p[j]];
const float* v_next = &verts[3*p[(j+1)%nv]];
const float* v_prev = &verts[3*p[(nv+j-1)%nv]];
if (!left(v_prev, v, v_next))
return false;
}
return true;
}
float distPointToSegmentSq(const float* point, const float* a, const float* b)
{
float abx[3], dx[3];
vsub(abx, b,a);
vsub(dx, point,a);
float d = abx[0]*abx[0]+abx[2]*abx[2];
float t = abx[0]*dx[0]+abx[2]*dx[2];
if (d > 0)
t /= d;
if (t < 0)
t = 0;
else if (t > 1)
t = 1;
dx[0] = a[0] + t*abx[0] - point[0];
dx[2] = a[2] + t*abx[2] - point[2];
return dx[0]*dx[0] + dx[2]*dx[2];
}
bool buildRawVertIndicesData(DerivedMesh* dm, int &nverts, float *&verts,
int &ntris, unsigned short *&tris, int *&trisToFacesMap,
int *&recastData)
{
nverts = dm->getNumVerts(dm);
if (nverts>=0xffff)
{
printf("Converting navmesh: Error! Too many vertices. Max number of vertices %d\n", 0xffff);
return false;
}
verts = new float[3*nverts];
dm->getVertCos(dm, (float(*)[3])verts);
//flip coordinates
for (int vi=0; vi<nverts; vi++)
{
SWAP(float, verts[3*vi+1], verts[3*vi+2]);
}
//calculate number of tris
int nfaces = dm->getNumFaces(dm);
MFace *faces = dm->getFaceArray(dm);
ntris = nfaces;
for (int fi=0; fi<nfaces; fi++)
{
MFace* face = &faces[fi];
if (face->v4)
ntris++;
}
//copy and transform to triangles (reorder on the run)
trisToFacesMap = new int[ntris];
tris = new unsigned short[3*ntris];
unsigned short* tri = tris;
int triIdx = 0;
for (int fi=0; fi<nfaces; fi++)
{
MFace* face = &faces[fi];
tri[3*triIdx+0] = (unsigned short) face->v1;
tri[3*triIdx+1] = (unsigned short) face->v3;
tri[3*triIdx+2] = (unsigned short) face->v2;
trisToFacesMap[triIdx++]=fi;
if (face->v4)
{
tri[3*triIdx+0] = (unsigned short) face->v1;
tri[3*triIdx+1] = (unsigned short) face->v4;
tri[3*triIdx+2] = (unsigned short) face->v3;
trisToFacesMap[triIdx++]=fi;
}
}
//carefully, recast data is just reference to data in derived mesh
recastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
return true;
}
bool buildPolygonsByDetailedMeshes(const int vertsPerPoly, const int npolys,
unsigned short* polys, const unsigned short* dmeshes,
const float* verts, const unsigned short* dtris,
const int* dtrisToPolysMap)
{
int capacity = vertsPerPoly;
unsigned short* newPoly = new unsigned short[capacity];
memset(newPoly, 0xff, sizeof(unsigned short)*capacity);
for (int polyidx=0; polyidx<npolys; polyidx++)
{
int nv = 0;
//search border
int btri = -1;
int bedge = -1;
int dtrisNum = dmeshes[polyidx*4+3];
int dtrisBase = dmeshes[polyidx*4+2];
unsigned char *traversedTris = new unsigned char[dtrisNum];
memset(traversedTris, 0, dtrisNum*sizeof(unsigned char));
for (int j=0; j<dtrisNum && btri==-1;j++)
{
int curpolytri = dtrisBase+j;
for (int k=0; k<3; k++)
{
unsigned short neighbortri = dtris[curpolytri*3*2+3+k];
if ( neighbortri==0xffff || dtrisToPolysMap[neighbortri]!=polyidx+1)
{
btri = curpolytri;
bedge = k;
break;
}
}
}
if (btri==-1 || bedge==-1)
{
//can't find triangle with border edge
return false;
}
newPoly[nv++] = dtris[btri*3*2+bedge];
int tri = btri;
int edge = (bedge+1)%3;
traversedTris[tri-dtrisBase] = 1;
while (tri!=btri || edge!=bedge)
{
int neighbortri = dtris[tri*3*2+3+edge];
if (neighbortri==0xffff || dtrisToPolysMap[neighbortri]!=polyidx+1)
{
if (nv==capacity)
{
capacity += vertsPerPoly;
unsigned short* newPolyBig = new unsigned short[capacity];
memset(newPolyBig, 0xff, sizeof(unsigned short)*capacity);
memcpy(newPolyBig, newPoly, sizeof(unsigned short)*nv);
delete newPoly;
newPoly = newPolyBig;
}
newPoly[nv++] = dtris[tri*3*2+edge];
//move to next edge
edge = (edge+1)%3;
}
else
{
//move to next tri
int twinedge = -1;
for (int k=0; k<3; k++)
{
if (dtris[neighbortri*3*2+3+k] == tri)
{
twinedge = k;
break;
}
}
if (twinedge==-1)
{
printf("Converting navmesh: Error! Can't find neighbor edge - invalid adjacency info\n");
goto returnLabel;
}
tri = neighbortri;
edge = (twinedge+1)%3;
traversedTris[tri-dtrisBase] = 1;
}
}
unsigned short* adjustedPoly = new unsigned short[nv];
int adjustedNv = 0;
for (size_t i=0; i<(size_t)nv; i++)
{
unsigned short prev = newPoly[(nv+i-1)%nv];
unsigned short cur = newPoly[i];
unsigned short next = newPoly[(i+1)%nv];
float distSq = distPointToSegmentSq(&verts[3*cur], &verts[3*prev], &verts[3*next]);
static const float tolerance = 0.001f;
if (distSq>tolerance)
adjustedPoly[adjustedNv++] = cur;
}
memcpy(newPoly, adjustedPoly, adjustedNv*sizeof(unsigned short));
delete adjustedPoly;
nv = adjustedNv;
bool allBorderTraversed = true;
for (size_t i=0; i<(size_t)dtrisNum; i++)
{
if (traversedTris[i]==0)
{
//check whether it has border edges
int curpolytri = dtrisBase+i;
for (int k=0; k<3; k++)
{
unsigned short neighbortri = dtris[curpolytri*3*2+3+k];
if ( neighbortri==0xffff || dtrisToPolysMap[neighbortri]!=polyidx+1)
{
allBorderTraversed = false;
break;
}
}
}
}
if (nv<=vertsPerPoly && allBorderTraversed)
{
for (int i=0; i<nv; i++)
{
polys[polyidx*vertsPerPoly*2+i] = newPoly[i];
}
}
}
returnLabel:
delete newPoly;
return true;
}
struct SortContext
{
const int* recastData;
const int* trisToFacesMap;
};
#if defined(_MSC_VER)
static int compareByData(void* data, const void * a, const void * b)
#elif defined(__APPLE__)
static int compareByData(void* data, const void * a, const void * b)
#else
static int compareByData(const void * a, const void * b, void* data)
#endif
{
const SortContext* context = (const SortContext*)data;
return ( context->recastData[context->trisToFacesMap[*(int*)a]] -
context->recastData[context->trisToFacesMap[*(int*)b]] );
}
bool buildNavMeshData(const int nverts, const float* verts,
const int ntris, const unsigned short *tris,
const int* recastData, const int* trisToFacesMap,
int &ndtris, unsigned short *&dtris,
int &npolys, unsigned short *&dmeshes, unsigned short *&polys,
int &vertsPerPoly, int *&dtrisToPolysMap, int *&dtrisToTrisMap)
{
if (!recastData)
{
printf("Converting navmesh: Error! Can't find recast custom data\n");
return false;
}
//sort the triangles by polygon idx
int* trisMapping = new int[ntris];
for (int i=0; i<ntris; i++)
trisMapping[i]=i;
SortContext context;
context.recastData = recastData;
context.trisToFacesMap = trisToFacesMap;
#if defined(_MSC_VER)
qsort_s(trisMapping, ntris, sizeof(int), compareByData, &context);
#elif defined(__APPLE__)
qsort_r(trisMapping, ntris, sizeof(int), &context, compareByData);
#else
qsort_r(trisMapping, ntris, sizeof(int), compareByData, &context);
#endif
//search first valid triangle - triangle of convex polygon
int validTriStart = -1;
for (int i=0; i< ntris; i++)
{
if (recastData[trisToFacesMap[trisMapping[i]]]>0)
{
validTriStart = i;
break;
}
}
if (validTriStart<0)
{
printf("Converting navmesh: Error! No valid polygons in mesh\n");
delete trisMapping;
return false;
}
ndtris = ntris-validTriStart;
//fill dtris to faces mapping
dtrisToTrisMap = new int[ndtris];
memcpy(dtrisToTrisMap, &trisMapping[validTriStart], ndtris*sizeof(int));
delete trisMapping; trisMapping=NULL;
//create detailed mesh triangles - copy only valid triangles
//and reserve memory for adjacency info
dtris = new unsigned short[3*2*ndtris];
memset(dtris, 0xffff, sizeof(unsigned short)*3*2*ndtris);
for (int i=0; i<ndtris; i++)
{
memcpy(dtris+3*2*i, tris+3*dtrisToTrisMap[i], sizeof(unsigned short)*3);
}
//create new recast data corresponded to dtris and renumber for continuous indices
int prevPolyIdx=-1, curPolyIdx, newPolyIdx=0;
dtrisToPolysMap = new int[ndtris];
for (int i=0; i<ndtris; i++)
{
curPolyIdx = recastData[trisToFacesMap[dtrisToTrisMap[i]]];
if (curPolyIdx!=prevPolyIdx)
{
newPolyIdx++;
prevPolyIdx=curPolyIdx;
}
dtrisToPolysMap[i] = newPolyIdx;
}
//build adjacency info for detailed mesh triangles
buildMeshAdjacency(dtris, ndtris, nverts, 3);
//create detailed mesh description for each navigation polygon
npolys = dtrisToPolysMap[ndtris-1];
dmeshes = new unsigned short[npolys*4];
memset(dmeshes, 0, npolys*4*sizeof(unsigned short));
unsigned short *dmesh = NULL;
int prevpolyidx = 0;
for (int i=0; i<ndtris; i++)
{
int curpolyidx = dtrisToPolysMap[i];
if (curpolyidx!=prevpolyidx)
{
if (curpolyidx!=prevpolyidx+1)
{
printf("Converting navmesh: Error! Wrong order of detailed mesh faces\n");
return false;
}
dmesh = dmesh==NULL ? dmeshes : dmesh+4;
dmesh[2] = (unsigned short)i; //tbase
dmesh[3] = 0; //tnum
prevpolyidx = curpolyidx;
}
dmesh[3]++;
}
//create navigation polygons
vertsPerPoly = 6;
polys = new unsigned short[npolys*vertsPerPoly*2];
memset(polys, 0xff, sizeof(unsigned short)*vertsPerPoly*2*npolys);
buildPolygonsByDetailedMeshes(vertsPerPoly, npolys, polys, dmeshes, verts, dtris, dtrisToPolysMap);
return true;
}
bool buildNavMeshDataByDerivedMesh(DerivedMesh *dm, int& vertsPerPoly,
int &nverts, float *&verts,
int &ndtris, unsigned short *&dtris,
int& npolys, unsigned short *&dmeshes,
unsigned short*& polys, int *&dtrisToPolysMap,
int *&dtrisToTrisMap, int *&trisToFacesMap)
{
bool res = true;
int ntris =0, *recastData=NULL;
unsigned short *tris=NULL;
res = buildRawVertIndicesData(dm, nverts, verts, ntris, tris, trisToFacesMap, recastData);
if (!res)
{
printf("Converting navmesh: Error! Can't get vertices and indices from mesh\n");
goto exit;
}
res = buildNavMeshData(nverts, verts, ntris, tris, recastData, trisToFacesMap,
ndtris, dtris, npolys, dmeshes,polys, vertsPerPoly,
dtrisToPolysMap, dtrisToTrisMap);
if (!res)
{
printf("Converting navmesh: Error! Can't get vertices and indices from mesh\n");
goto exit;
}
exit:
if (tris)
delete tris;
return res;
}
int polyFindVertex(const unsigned short* p, const int vertsPerPoly, unsigned short vertexIdx)
{
int res = -1;
for(int i=0; i<vertsPerPoly; i++)
{
if (p[i]==0xffff)
break;
if (p[i]==vertexIdx)
{
res = i;
break;
}
}
return res;
}
|