RecastMesh.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #define _USE_MATH_DEFINES
  19. #include <math.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include "Recast.h"
  23. #include "RecastAlloc.h"
  24. #include "RecastAssert.h"
  25. struct rcEdge
  26. {
  27. unsigned short vert[2];
  28. unsigned short polyEdge[2];
  29. unsigned short poly[2];
  30. };
  31. static bool buildMeshAdjacency(unsigned short* polys, const int npolys,
  32. const int nverts, const int vertsPerPoly)
  33. {
  34. // Based on code by Eric Lengyel from:
  35. // http://www.terathon.com/code/edges.php
  36. int maxEdgeCount = npolys*vertsPerPoly;
  37. unsigned short* firstEdge = (unsigned short*)rcAlloc(sizeof(unsigned short)*(nverts + maxEdgeCount), RC_ALLOC_TEMP);
  38. if (!firstEdge)
  39. return false;
  40. unsigned short* nextEdge = firstEdge + nverts;
  41. int edgeCount = 0;
  42. rcEdge* edges = (rcEdge*)rcAlloc(sizeof(rcEdge)*maxEdgeCount, RC_ALLOC_TEMP);
  43. if (!edges)
  44. {
  45. rcFree(firstEdge);
  46. return false;
  47. }
  48. for (int i = 0; i < nverts; i++)
  49. firstEdge[i] = RC_MESH_NULL_IDX;
  50. for (int i = 0; i < npolys; ++i)
  51. {
  52. unsigned short* t = &polys[i*vertsPerPoly*2];
  53. for (int j = 0; j < vertsPerPoly; ++j)
  54. {
  55. if (t[j] == RC_MESH_NULL_IDX) break;
  56. unsigned short v0 = t[j];
  57. unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RC_MESH_NULL_IDX) ? t[0] : t[j+1];
  58. if (v0 < v1)
  59. {
  60. rcEdge& edge = edges[edgeCount];
  61. edge.vert[0] = v0;
  62. edge.vert[1] = v1;
  63. edge.poly[0] = (unsigned short)i;
  64. edge.polyEdge[0] = (unsigned short)j;
  65. edge.poly[1] = (unsigned short)i;
  66. edge.polyEdge[1] = 0;
  67. // Insert edge
  68. nextEdge[edgeCount] = firstEdge[v0];
  69. firstEdge[v0] = (unsigned short)edgeCount;
  70. edgeCount++;
  71. }
  72. }
  73. }
  74. for (int i = 0; i < npolys; ++i)
  75. {
  76. unsigned short* t = &polys[i*vertsPerPoly*2];
  77. for (int j = 0; j < vertsPerPoly; ++j)
  78. {
  79. if (t[j] == RC_MESH_NULL_IDX) break;
  80. unsigned short v0 = t[j];
  81. unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RC_MESH_NULL_IDX) ? t[0] : t[j+1];
  82. if (v0 > v1)
  83. {
  84. for (unsigned short e = firstEdge[v1]; e != RC_MESH_NULL_IDX; e = nextEdge[e])
  85. {
  86. rcEdge& edge = edges[e];
  87. if (edge.vert[1] == v0 && edge.poly[0] == edge.poly[1])
  88. {
  89. edge.poly[1] = (unsigned short)i;
  90. edge.polyEdge[1] = (unsigned short)j;
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. // Store adjacency
  98. for (int i = 0; i < edgeCount; ++i)
  99. {
  100. const rcEdge& e = edges[i];
  101. if (e.poly[0] != e.poly[1])
  102. {
  103. unsigned short* p0 = &polys[e.poly[0]*vertsPerPoly*2];
  104. unsigned short* p1 = &polys[e.poly[1]*vertsPerPoly*2];
  105. p0[vertsPerPoly + e.polyEdge[0]] = e.poly[1];
  106. p1[vertsPerPoly + e.polyEdge[1]] = e.poly[0];
  107. }
  108. }
  109. rcFree(firstEdge);
  110. rcFree(edges);
  111. return true;
  112. }
  113. static const int VERTEX_BUCKET_COUNT = (1<<12);
  114. inline int computeVertexHash(int x, int y, int z)
  115. {
  116. const unsigned int h1 = 0x8da6b343; // Large multiplicative constants;
  117. const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes
  118. const unsigned int h3 = 0xcb1ab31f;
  119. unsigned int n = h1 * x + h2 * y + h3 * z;
  120. return (int)(n & (VERTEX_BUCKET_COUNT-1));
  121. }
  122. static unsigned short addVertex(unsigned short x, unsigned short y, unsigned short z,
  123. unsigned short* verts, int* firstVert, int* nextVert, int& nv)
  124. {
  125. int bucket = computeVertexHash(x, 0, z);
  126. int i = firstVert[bucket];
  127. while (i != -1)
  128. {
  129. const unsigned short* v = &verts[i*3];
  130. if (v[0] == x && (rcAbs(v[1] - y) <= 2) && v[2] == z)
  131. return (unsigned short)i;
  132. i = nextVert[i]; // next
  133. }
  134. // Could not find, create new.
  135. i = nv; nv++;
  136. unsigned short* v = &verts[i*3];
  137. v[0] = x;
  138. v[1] = y;
  139. v[2] = z;
  140. nextVert[i] = firstVert[bucket];
  141. firstVert[bucket] = i;
  142. return (unsigned short)i;
  143. }
  144. inline int prev(int i, int n) { return i-1 >= 0 ? i-1 : n-1; }
  145. inline int next(int i, int n) { return i+1 < n ? i+1 : 0; }
  146. inline int area2(const int* a, const int* b, const int* c)
  147. {
  148. return (b[0] - a[0]) * (c[2] - a[2]) - (c[0] - a[0]) * (b[2] - a[2]);
  149. }
  150. // Exclusive or: true iff exactly one argument is true.
  151. // The arguments are negated to ensure that they are 0/1
  152. // values. Then the bitwise Xor operator may apply.
  153. // (This idea is due to Michael Baldwin.)
  154. inline bool xorb(bool x, bool y)
  155. {
  156. return !x ^ !y;
  157. }
  158. // Returns true iff c is strictly to the left of the directed
  159. // line through a to b.
  160. inline bool left(const int* a, const int* b, const int* c)
  161. {
  162. return area2(a, b, c) < 0;
  163. }
  164. inline bool leftOn(const int* a, const int* b, const int* c)
  165. {
  166. return area2(a, b, c) <= 0;
  167. }
  168. inline bool collinear(const int* a, const int* b, const int* c)
  169. {
  170. return area2(a, b, c) == 0;
  171. }
  172. // Returns true iff ab properly intersects cd: they share
  173. // a point interior to both segments. The properness of the
  174. // intersection is ensured by using strict leftness.
  175. static bool intersectProp(const int* a, const int* b, const int* c, const int* d)
  176. {
  177. // Eliminate improper cases.
  178. if (collinear(a,b,c) || collinear(a,b,d) ||
  179. collinear(c,d,a) || collinear(c,d,b))
  180. return false;
  181. return xorb(left(a,b,c), left(a,b,d)) && xorb(left(c,d,a), left(c,d,b));
  182. }
  183. // Returns T iff (a,b,c) are collinear and point c lies
  184. // on the closed segement ab.
  185. static bool between(const int* a, const int* b, const int* c)
  186. {
  187. if (!collinear(a, b, c))
  188. return false;
  189. // If ab not vertical, check betweenness on x; else on y.
  190. if (a[0] != b[0])
  191. return ((a[0] <= c[0]) && (c[0] <= b[0])) || ((a[0] >= c[0]) && (c[0] >= b[0]));
  192. else
  193. return ((a[2] <= c[2]) && (c[2] <= b[2])) || ((a[2] >= c[2]) && (c[2] >= b[2]));
  194. }
  195. // Returns true iff segments ab and cd intersect, properly or improperly.
  196. static bool intersect(const int* a, const int* b, const int* c, const int* d)
  197. {
  198. if (intersectProp(a, b, c, d))
  199. return true;
  200. else if (between(a, b, c) || between(a, b, d) ||
  201. between(c, d, a) || between(c, d, b))
  202. return true;
  203. else
  204. return false;
  205. }
  206. static bool vequal(const int* a, const int* b)
  207. {
  208. return a[0] == b[0] && a[2] == b[2];
  209. }
  210. // Returns T iff (v_i, v_j) is a proper internal *or* external
  211. // diagonal of P, *ignoring edges incident to v_i and v_j*.
  212. static bool diagonalie(int i, int j, int n, const int* verts, int* indices)
  213. {
  214. const int* d0 = &verts[(indices[i] & 0x0fffffff) * 4];
  215. const int* d1 = &verts[(indices[j] & 0x0fffffff) * 4];
  216. // For each edge (k,k+1) of P
  217. for (int k = 0; k < n; k++)
  218. {
  219. int k1 = next(k, n);
  220. // Skip edges incident to i or j
  221. if (!((k == i) || (k1 == i) || (k == j) || (k1 == j)))
  222. {
  223. const int* p0 = &verts[(indices[k] & 0x0fffffff) * 4];
  224. const int* p1 = &verts[(indices[k1] & 0x0fffffff) * 4];
  225. if (vequal(d0, p0) || vequal(d1, p0) || vequal(d0, p1) || vequal(d1, p1))
  226. continue;
  227. if (intersect(d0, d1, p0, p1))
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. // Returns true iff the diagonal (i,j) is strictly internal to the
  234. // polygon P in the neighborhood of the i endpoint.
  235. static bool inCone(int i, int j, int n, const int* verts, int* indices)
  236. {
  237. const int* pi = &verts[(indices[i] & 0x0fffffff) * 4];
  238. const int* pj = &verts[(indices[j] & 0x0fffffff) * 4];
  239. const int* pi1 = &verts[(indices[next(i, n)] & 0x0fffffff) * 4];
  240. const int* pin1 = &verts[(indices[prev(i, n)] & 0x0fffffff) * 4];
  241. // If P[i] is a convex vertex [ i+1 left or on (i-1,i) ].
  242. if (leftOn(pin1, pi, pi1))
  243. return left(pi, pj, pin1) && left(pj, pi, pi1);
  244. // Assume (i-1,i,i+1) not collinear.
  245. // else P[i] is reflex.
  246. return !(leftOn(pi, pj, pi1) && leftOn(pj, pi, pin1));
  247. }
  248. // Returns T iff (v_i, v_j) is a proper internal
  249. // diagonal of P.
  250. static bool diagonal(int i, int j, int n, const int* verts, int* indices)
  251. {
  252. return inCone(i, j, n, verts, indices) && diagonalie(i, j, n, verts, indices);
  253. }
  254. static int triangulate(int n, const int* verts, int* indices, int* tris)
  255. {
  256. int ntris = 0;
  257. int* dst = tris;
  258. // The last bit of the index is used to indicate if the vertex can be removed.
  259. for (int i = 0; i < n; i++)
  260. {
  261. int i1 = next(i, n);
  262. int i2 = next(i1, n);
  263. if (diagonal(i, i2, n, verts, indices))
  264. indices[i1] |= 0x80000000;
  265. }
  266. while (n > 3)
  267. {
  268. int minLen = -1;
  269. int mini = -1;
  270. for (int i = 0; i < n; i++)
  271. {
  272. int i1 = next(i, n);
  273. if (indices[i1] & 0x80000000)
  274. {
  275. const int* p0 = &verts[(indices[i] & 0x0fffffff) * 4];
  276. const int* p2 = &verts[(indices[next(i1, n)] & 0x0fffffff) * 4];
  277. int dx = p2[0] - p0[0];
  278. int dy = p2[2] - p0[2];
  279. int len = dx*dx + dy*dy;
  280. if (minLen < 0 || len < minLen)
  281. {
  282. minLen = len;
  283. mini = i;
  284. }
  285. }
  286. }
  287. if (mini == -1)
  288. {
  289. // Should not happen.
  290. /* printf("mini == -1 ntris=%d n=%d\n", ntris, n);
  291. for (int i = 0; i < n; i++)
  292. {
  293. printf("%d ", indices[i] & 0x0fffffff);
  294. }
  295. printf("\n");*/
  296. return -ntris;
  297. }
  298. int i = mini;
  299. int i1 = next(i, n);
  300. int i2 = next(i1, n);
  301. *dst++ = indices[i] & 0x0fffffff;
  302. *dst++ = indices[i1] & 0x0fffffff;
  303. *dst++ = indices[i2] & 0x0fffffff;
  304. ntris++;
  305. // Removes P[i1] by copying P[i+1]...P[n-1] left one index.
  306. n--;
  307. for (int k = i1; k < n; k++)
  308. indices[k] = indices[k+1];
  309. if (i1 >= n) i1 = 0;
  310. i = prev(i1,n);
  311. // Update diagonal flags.
  312. if (diagonal(prev(i, n), i1, n, verts, indices))
  313. indices[i] |= 0x80000000;
  314. else
  315. indices[i] &= 0x0fffffff;
  316. if (diagonal(i, next(i1, n), n, verts, indices))
  317. indices[i1] |= 0x80000000;
  318. else
  319. indices[i1] &= 0x0fffffff;
  320. }
  321. // Append the remaining triangle.
  322. *dst++ = indices[0] & 0x0fffffff;
  323. *dst++ = indices[1] & 0x0fffffff;
  324. *dst++ = indices[2] & 0x0fffffff;
  325. ntris++;
  326. return ntris;
  327. }
  328. static int countPolyVerts(const unsigned short* p, const int nvp)
  329. {
  330. for (int i = 0; i < nvp; ++i)
  331. if (p[i] == RC_MESH_NULL_IDX)
  332. return i;
  333. return nvp;
  334. }
  335. inline bool uleft(const unsigned short* a, const unsigned short* b, const unsigned short* c)
  336. {
  337. return ((int)b[0] - (int)a[0]) * ((int)c[2] - (int)a[2]) -
  338. ((int)c[0] - (int)a[0]) * ((int)b[2] - (int)a[2]) < 0;
  339. }
  340. static int getPolyMergeValue(unsigned short* pa, unsigned short* pb,
  341. const unsigned short* verts, int& ea, int& eb,
  342. const int nvp)
  343. {
  344. const int na = countPolyVerts(pa, nvp);
  345. const int nb = countPolyVerts(pb, nvp);
  346. // If the merged polygon would be too big, do not merge.
  347. if (na+nb-2 > nvp)
  348. return -1;
  349. // Check if the polygons share an edge.
  350. ea = -1;
  351. eb = -1;
  352. for (int i = 0; i < na; ++i)
  353. {
  354. unsigned short va0 = pa[i];
  355. unsigned short va1 = pa[(i+1) % na];
  356. if (va0 > va1)
  357. rcSwap(va0, va1);
  358. for (int j = 0; j < nb; ++j)
  359. {
  360. unsigned short vb0 = pb[j];
  361. unsigned short vb1 = pb[(j+1) % nb];
  362. if (vb0 > vb1)
  363. rcSwap(vb0, vb1);
  364. if (va0 == vb0 && va1 == vb1)
  365. {
  366. ea = i;
  367. eb = j;
  368. break;
  369. }
  370. }
  371. }
  372. // No common edge, cannot merge.
  373. if (ea == -1 || eb == -1)
  374. return -1;
  375. // Check to see if the merged polygon would be convex.
  376. unsigned short va, vb, vc;
  377. va = pa[(ea+na-1) % na];
  378. vb = pa[ea];
  379. vc = pb[(eb+2) % nb];
  380. if (!uleft(&verts[va*3], &verts[vb*3], &verts[vc*3]))
  381. return -1;
  382. va = pb[(eb+nb-1) % nb];
  383. vb = pb[eb];
  384. vc = pa[(ea+2) % na];
  385. if (!uleft(&verts[va*3], &verts[vb*3], &verts[vc*3]))
  386. return -1;
  387. va = pa[ea];
  388. vb = pa[(ea+1)%na];
  389. int dx = (int)verts[va*3+0] - (int)verts[vb*3+0];
  390. int dy = (int)verts[va*3+2] - (int)verts[vb*3+2];
  391. return dx*dx + dy*dy;
  392. }
  393. static void mergePolys(unsigned short* pa, unsigned short* pb, int ea, int eb,
  394. unsigned short* tmp, const int nvp)
  395. {
  396. const int na = countPolyVerts(pa, nvp);
  397. const int nb = countPolyVerts(pb, nvp);
  398. // Merge polygons.
  399. memset(tmp, 0xff, sizeof(unsigned short)*nvp);
  400. int n = 0;
  401. // Add pa
  402. for (int i = 0; i < na-1; ++i)
  403. tmp[n++] = pa[(ea+1+i) % na];
  404. // Add pb
  405. for (int i = 0; i < nb-1; ++i)
  406. tmp[n++] = pb[(eb+1+i) % nb];
  407. memcpy(pa, tmp, sizeof(unsigned short)*nvp);
  408. }
  409. static void pushFront(int v, int* arr, int& an)
  410. {
  411. an++;
  412. for (int i = an-1; i > 0; --i) arr[i] = arr[i-1];
  413. arr[0] = v;
  414. }
  415. static void pushBack(int v, int* arr, int& an)
  416. {
  417. arr[an] = v;
  418. an++;
  419. }
  420. static bool canRemoveVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned short rem)
  421. {
  422. const int nvp = mesh.nvp;
  423. // Count number of polygons to remove.
  424. int numRemovedVerts = 0;
  425. int numTouchedVerts = 0;
  426. int numRemainingEdges = 0;
  427. for (int i = 0; i < mesh.npolys; ++i)
  428. {
  429. unsigned short* p = &mesh.polys[i*nvp*2];
  430. const int nv = countPolyVerts(p, nvp);
  431. int numRemoved = 0;
  432. int numVerts = 0;
  433. for (int j = 0; j < nv; ++j)
  434. {
  435. if (p[j] == rem)
  436. {
  437. numTouchedVerts++;
  438. numRemoved++;
  439. }
  440. numVerts++;
  441. }
  442. if (numRemoved)
  443. {
  444. numRemovedVerts += numRemoved;
  445. numRemainingEdges += numVerts-(numRemoved+1);
  446. }
  447. }
  448. // There would be too few edges remaining to create a polygon.
  449. // This can happen for example when a tip of a triangle is marked
  450. // as deletion, but there are no other polys that share the vertex.
  451. // In this case, the vertex should not be removed.
  452. if (numRemainingEdges <= 2)
  453. return false;
  454. // Find edges which share the removed vertex.
  455. const int maxEdges = numTouchedVerts*2;
  456. int nedges = 0;
  457. rcScopedDelete<int> edges = (int*)rcAlloc(sizeof(int)*maxEdges*3, RC_ALLOC_TEMP);
  458. if (!edges)
  459. {
  460. ctx->log(RC_LOG_WARNING, "canRemoveVertex: Out of memory 'edges' (%d).", maxEdges*3);
  461. return false;
  462. }
  463. for (int i = 0; i < mesh.npolys; ++i)
  464. {
  465. unsigned short* p = &mesh.polys[i*nvp*2];
  466. const int nv = countPolyVerts(p, nvp);
  467. // Collect edges which touches the removed vertex.
  468. for (int j = 0, k = nv-1; j < nv; k = j++)
  469. {
  470. if (p[j] == rem || p[k] == rem)
  471. {
  472. // Arrange edge so that a=rem.
  473. int a = p[j], b = p[k];
  474. if (b == rem)
  475. rcSwap(a,b);
  476. // Check if the edge exists
  477. bool exists = false;
  478. for (int m = 0; m < nedges; ++m)
  479. {
  480. int* e = &edges[m*3];
  481. if (e[1] == b)
  482. {
  483. // Exists, increment vertex share count.
  484. e[2]++;
  485. exists = true;
  486. }
  487. }
  488. // Add new edge.
  489. if (!exists)
  490. {
  491. int* e = &edges[nedges*3];
  492. e[0] = a;
  493. e[1] = b;
  494. e[2] = 1;
  495. nedges++;
  496. }
  497. }
  498. }
  499. }
  500. // There should be no more than 2 open edges.
  501. // This catches the case that two non-adjacent polygons
  502. // share the removed vertex. In that case, do not remove the vertex.
  503. int numOpenEdges = 0;
  504. for (int i = 0; i < nedges; ++i)
  505. {
  506. if (edges[i*3+2] < 2)
  507. numOpenEdges++;
  508. }
  509. if (numOpenEdges > 2)
  510. return false;
  511. return true;
  512. }
  513. static bool removeVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned short rem, const int maxTris)
  514. {
  515. const int nvp = mesh.nvp;
  516. // Count number of polygons to remove.
  517. int numRemovedVerts = 0;
  518. for (int i = 0; i < mesh.npolys; ++i)
  519. {
  520. unsigned short* p = &mesh.polys[i*nvp*2];
  521. const int nv = countPolyVerts(p, nvp);
  522. for (int j = 0; j < nv; ++j)
  523. {
  524. if (p[j] == rem)
  525. numRemovedVerts++;
  526. }
  527. }
  528. int nedges = 0;
  529. rcScopedDelete<int> edges = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp*4, RC_ALLOC_TEMP);
  530. if (!edges)
  531. {
  532. ctx->log(RC_LOG_WARNING, "removeVertex: Out of memory 'edges' (%d).", numRemovedVerts*nvp*4);
  533. return false;
  534. }
  535. int nhole = 0;
  536. rcScopedDelete<int> hole = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp, RC_ALLOC_TEMP);
  537. if (!hole)
  538. {
  539. ctx->log(RC_LOG_WARNING, "removeVertex: Out of memory 'hole' (%d).", numRemovedVerts*nvp);
  540. return false;
  541. }
  542. int nhreg = 0;
  543. rcScopedDelete<int> hreg = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp, RC_ALLOC_TEMP);
  544. if (!hreg)
  545. {
  546. ctx->log(RC_LOG_WARNING, "removeVertex: Out of memory 'hreg' (%d).", numRemovedVerts*nvp);
  547. return false;
  548. }
  549. int nharea = 0;
  550. rcScopedDelete<int> harea = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp, RC_ALLOC_TEMP);
  551. if (!harea)
  552. {
  553. ctx->log(RC_LOG_WARNING, "removeVertex: Out of memory 'harea' (%d).", numRemovedVerts*nvp);
  554. return false;
  555. }
  556. for (int i = 0; i < mesh.npolys; ++i)
  557. {
  558. unsigned short* p = &mesh.polys[i*nvp*2];
  559. const int nv = countPolyVerts(p, nvp);
  560. bool hasRem = false;
  561. for (int j = 0; j < nv; ++j)
  562. if (p[j] == rem) hasRem = true;
  563. if (hasRem)
  564. {
  565. // Collect edges which does not touch the removed vertex.
  566. for (int j = 0, k = nv-1; j < nv; k = j++)
  567. {
  568. if (p[j] != rem && p[k] != rem)
  569. {
  570. int* e = &edges[nedges*4];
  571. e[0] = p[k];
  572. e[1] = p[j];
  573. e[2] = mesh.regs[i];
  574. e[3] = mesh.areas[i];
  575. nedges++;
  576. }
  577. }
  578. // Remove the polygon.
  579. unsigned short* p2 = &mesh.polys[(mesh.npolys-1)*nvp*2];
  580. memcpy(p,p2,sizeof(unsigned short)*nvp);
  581. memset(p+nvp,0xff,sizeof(unsigned short)*nvp);
  582. mesh.regs[i] = mesh.regs[mesh.npolys-1];
  583. mesh.areas[i] = mesh.areas[mesh.npolys-1];
  584. mesh.npolys--;
  585. --i;
  586. }
  587. }
  588. // Remove vertex.
  589. for (int i = (int)rem; i < mesh.nverts; ++i)
  590. {
  591. mesh.verts[i*3+0] = mesh.verts[(i+1)*3+0];
  592. mesh.verts[i*3+1] = mesh.verts[(i+1)*3+1];
  593. mesh.verts[i*3+2] = mesh.verts[(i+1)*3+2];
  594. }
  595. mesh.nverts--;
  596. // Adjust indices to match the removed vertex layout.
  597. for (int i = 0; i < mesh.npolys; ++i)
  598. {
  599. unsigned short* p = &mesh.polys[i*nvp*2];
  600. const int nv = countPolyVerts(p, nvp);
  601. for (int j = 0; j < nv; ++j)
  602. if (p[j] > rem) p[j]--;
  603. }
  604. for (int i = 0; i < nedges; ++i)
  605. {
  606. if (edges[i*4+0] > rem) edges[i*4+0]--;
  607. if (edges[i*4+1] > rem) edges[i*4+1]--;
  608. }
  609. if (nedges == 0)
  610. return true;
  611. // Start with one vertex, keep appending connected
  612. // segments to the start and end of the hole.
  613. pushBack(edges[0], hole, nhole);
  614. pushBack(edges[2], hreg, nhreg);
  615. pushBack(edges[3], harea, nharea);
  616. while (nedges)
  617. {
  618. bool match = false;
  619. for (int i = 0; i < nedges; ++i)
  620. {
  621. const int ea = edges[i*4+0];
  622. const int eb = edges[i*4+1];
  623. const int r = edges[i*4+2];
  624. const int a = edges[i*4+3];
  625. bool add = false;
  626. if (hole[0] == eb)
  627. {
  628. // The segment matches the beginning of the hole boundary.
  629. pushFront(ea, hole, nhole);
  630. pushFront(r, hreg, nhreg);
  631. pushFront(a, harea, nharea);
  632. add = true;
  633. }
  634. else if (hole[nhole-1] == ea)
  635. {
  636. // The segment matches the end of the hole boundary.
  637. pushBack(eb, hole, nhole);
  638. pushBack(r, hreg, nhreg);
  639. pushBack(a, harea, nharea);
  640. add = true;
  641. }
  642. if (add)
  643. {
  644. // The edge segment was added, remove it.
  645. edges[i*4+0] = edges[(nedges-1)*4+0];
  646. edges[i*4+1] = edges[(nedges-1)*4+1];
  647. edges[i*4+2] = edges[(nedges-1)*4+2];
  648. edges[i*4+3] = edges[(nedges-1)*4+3];
  649. --nedges;
  650. match = true;
  651. --i;
  652. }
  653. }
  654. if (!match)
  655. break;
  656. }
  657. rcScopedDelete<int> tris = (int*)rcAlloc(sizeof(int)*nhole*3, RC_ALLOC_TEMP);
  658. if (!tris)
  659. {
  660. ctx->log(RC_LOG_WARNING, "removeVertex: Out of memory 'tris' (%d).", nhole*3);
  661. return false;
  662. }
  663. rcScopedDelete<int> tverts = (int*)rcAlloc(sizeof(int)*nhole*4, RC_ALLOC_TEMP);
  664. if (!tverts)
  665. {
  666. ctx->log(RC_LOG_WARNING, "removeVertex: Out of memory 'tverts' (%d).", nhole*4);
  667. return false;
  668. }
  669. rcScopedDelete<int> thole = (int*)rcAlloc(sizeof(int)*nhole, RC_ALLOC_TEMP);
  670. if (!tverts)
  671. {
  672. ctx->log(RC_LOG_WARNING, "removeVertex: Out of memory 'thole' (%d).", nhole);
  673. return false;
  674. }
  675. // Generate temp vertex array for triangulation.
  676. for (int i = 0; i < nhole; ++i)
  677. {
  678. const int pi = hole[i];
  679. tverts[i*4+0] = mesh.verts[pi*3+0];
  680. tverts[i*4+1] = mesh.verts[pi*3+1];
  681. tverts[i*4+2] = mesh.verts[pi*3+2];
  682. tverts[i*4+3] = 0;
  683. thole[i] = i;
  684. }
  685. // Triangulate the hole.
  686. int ntris = triangulate(nhole, &tverts[0], &thole[0], tris);
  687. if (ntris < 0)
  688. {
  689. ntris = -ntris;
  690. ctx->log(RC_LOG_WARNING, "removeVertex: triangulate() returned bad results.");
  691. }
  692. // Merge the hole triangles back to polygons.
  693. rcScopedDelete<unsigned short> polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*(ntris+1)*nvp, RC_ALLOC_TEMP);
  694. if (!polys)
  695. {
  696. ctx->log(RC_LOG_ERROR, "removeVertex: Out of memory 'polys' (%d).", (ntris+1)*nvp);
  697. return false;
  698. }
  699. rcScopedDelete<unsigned short> pregs = (unsigned short*)rcAlloc(sizeof(unsigned short)*ntris, RC_ALLOC_TEMP);
  700. if (!pregs)
  701. {
  702. ctx->log(RC_LOG_ERROR, "removeVertex: Out of memory 'pregs' (%d).", ntris);
  703. return false;
  704. }
  705. rcScopedDelete<unsigned char> pareas = (unsigned char*)rcAlloc(sizeof(unsigned char)*ntris, RC_ALLOC_TEMP);
  706. if (!pregs)
  707. {
  708. ctx->log(RC_LOG_ERROR, "removeVertex: Out of memory 'pareas' (%d).", ntris);
  709. return false;
  710. }
  711. unsigned short* tmpPoly = &polys[ntris*nvp];
  712. // Build initial polygons.
  713. int npolys = 0;
  714. memset(polys, 0xff, ntris*nvp*sizeof(unsigned short));
  715. for (int j = 0; j < ntris; ++j)
  716. {
  717. int* t = &tris[j*3];
  718. if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2])
  719. {
  720. polys[npolys*nvp+0] = (unsigned short)hole[t[0]];
  721. polys[npolys*nvp+1] = (unsigned short)hole[t[1]];
  722. polys[npolys*nvp+2] = (unsigned short)hole[t[2]];
  723. pregs[npolys] = (unsigned short)hreg[t[0]];
  724. pareas[npolys] = (unsigned char)harea[t[0]];
  725. npolys++;
  726. }
  727. }
  728. if (!npolys)
  729. return true;
  730. // Merge polygons.
  731. if (nvp > 3)
  732. {
  733. for (;;)
  734. {
  735. // Find best polygons to merge.
  736. int bestMergeVal = 0;
  737. int bestPa = 0, bestPb = 0, bestEa = 0, bestEb = 0;
  738. for (int j = 0; j < npolys-1; ++j)
  739. {
  740. unsigned short* pj = &polys[j*nvp];
  741. for (int k = j+1; k < npolys; ++k)
  742. {
  743. unsigned short* pk = &polys[k*nvp];
  744. int ea, eb;
  745. int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb, nvp);
  746. if (v > bestMergeVal)
  747. {
  748. bestMergeVal = v;
  749. bestPa = j;
  750. bestPb = k;
  751. bestEa = ea;
  752. bestEb = eb;
  753. }
  754. }
  755. }
  756. if (bestMergeVal > 0)
  757. {
  758. // Found best, merge.
  759. unsigned short* pa = &polys[bestPa*nvp];
  760. unsigned short* pb = &polys[bestPb*nvp];
  761. mergePolys(pa, pb, bestEa, bestEb, tmpPoly, nvp);
  762. memcpy(pb, &polys[(npolys-1)*nvp], sizeof(unsigned short)*nvp);
  763. pregs[bestPb] = pregs[npolys-1];
  764. pareas[bestPb] = pareas[npolys-1];
  765. npolys--;
  766. }
  767. else
  768. {
  769. // Could not merge any polygons, stop.
  770. break;
  771. }
  772. }
  773. }
  774. // Store polygons.
  775. for (int i = 0; i < npolys; ++i)
  776. {
  777. if (mesh.npolys >= maxTris) break;
  778. unsigned short* p = &mesh.polys[mesh.npolys*nvp*2];
  779. memset(p,0xff,sizeof(unsigned short)*nvp*2);
  780. for (int j = 0; j < nvp; ++j)
  781. p[j] = polys[i*nvp+j];
  782. mesh.regs[mesh.npolys] = pregs[i];
  783. mesh.areas[mesh.npolys] = pareas[i];
  784. mesh.npolys++;
  785. if (mesh.npolys > maxTris)
  786. {
  787. ctx->log(RC_LOG_ERROR, "removeVertex: Too many polygons %d (max:%d).", mesh.npolys, maxTris);
  788. return false;
  789. }
  790. }
  791. return true;
  792. }
  793. /// @par
  794. ///
  795. /// @note If the mesh data is to be used to construct a Detour navigation mesh, then the upper
  796. /// limit must be retricted to <= #DT_VERTS_PER_POLYGON.
  797. ///
  798. /// @see rcAllocPolyMesh, rcContourSet, rcPolyMesh, rcConfig
  799. bool rcBuildPolyMesh(rcContext* ctx, rcContourSet& cset, const int nvp, rcPolyMesh& mesh)
  800. {
  801. rcAssert(ctx);
  802. ctx->startTimer(RC_TIMER_BUILD_POLYMESH);
  803. rcVcopy(mesh.bmin, cset.bmin);
  804. rcVcopy(mesh.bmax, cset.bmax);
  805. mesh.cs = cset.cs;
  806. mesh.ch = cset.ch;
  807. mesh.borderSize = cset.borderSize;
  808. int maxVertices = 0;
  809. int maxTris = 0;
  810. int maxVertsPerCont = 0;
  811. for (int i = 0; i < cset.nconts; ++i)
  812. {
  813. // Skip null contours.
  814. if (cset.conts[i].nverts < 3) continue;
  815. maxVertices += cset.conts[i].nverts;
  816. maxTris += cset.conts[i].nverts - 2;
  817. maxVertsPerCont = rcMax(maxVertsPerCont, cset.conts[i].nverts);
  818. }
  819. if (maxVertices >= 0xfffe)
  820. {
  821. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Too many vertices %d.", maxVertices);
  822. return false;
  823. }
  824. rcScopedDelete<unsigned char> vflags = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxVertices, RC_ALLOC_TEMP);
  825. if (!vflags)
  826. {
  827. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'vflags' (%d).", maxVertices);
  828. return false;
  829. }
  830. memset(vflags, 0, maxVertices);
  831. mesh.verts = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertices*3, RC_ALLOC_PERM);
  832. if (!mesh.verts)
  833. {
  834. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.verts' (%d).", maxVertices);
  835. return false;
  836. }
  837. mesh.polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxTris*nvp*2, RC_ALLOC_PERM);
  838. if (!mesh.polys)
  839. {
  840. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.polys' (%d).", maxTris*nvp*2);
  841. return false;
  842. }
  843. mesh.regs = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxTris, RC_ALLOC_PERM);
  844. if (!mesh.regs)
  845. {
  846. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.regs' (%d).", maxTris);
  847. return false;
  848. }
  849. mesh.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxTris, RC_ALLOC_PERM);
  850. if (!mesh.areas)
  851. {
  852. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.areas' (%d).", maxTris);
  853. return false;
  854. }
  855. mesh.nverts = 0;
  856. mesh.npolys = 0;
  857. mesh.nvp = nvp;
  858. mesh.maxpolys = maxTris;
  859. memset(mesh.verts, 0, sizeof(unsigned short)*maxVertices*3);
  860. memset(mesh.polys, 0xff, sizeof(unsigned short)*maxTris*nvp*2);
  861. memset(mesh.regs, 0, sizeof(unsigned short)*maxTris);
  862. memset(mesh.areas, 0, sizeof(unsigned char)*maxTris);
  863. rcScopedDelete<int> nextVert = (int*)rcAlloc(sizeof(int)*maxVertices, RC_ALLOC_TEMP);
  864. if (!nextVert)
  865. {
  866. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'nextVert' (%d).", maxVertices);
  867. return false;
  868. }
  869. memset(nextVert, 0, sizeof(int)*maxVertices);
  870. rcScopedDelete<int> firstVert = (int*)rcAlloc(sizeof(int)*VERTEX_BUCKET_COUNT, RC_ALLOC_TEMP);
  871. if (!firstVert)
  872. {
  873. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'firstVert' (%d).", VERTEX_BUCKET_COUNT);
  874. return false;
  875. }
  876. for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i)
  877. firstVert[i] = -1;
  878. rcScopedDelete<int> indices = (int*)rcAlloc(sizeof(int)*maxVertsPerCont, RC_ALLOC_TEMP);
  879. if (!indices)
  880. {
  881. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'indices' (%d).", maxVertsPerCont);
  882. return false;
  883. }
  884. rcScopedDelete<int> tris = (int*)rcAlloc(sizeof(int)*maxVertsPerCont*3, RC_ALLOC_TEMP);
  885. if (!tris)
  886. {
  887. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'tris' (%d).", maxVertsPerCont*3);
  888. return false;
  889. }
  890. rcScopedDelete<unsigned short> polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*(maxVertsPerCont+1)*nvp, RC_ALLOC_TEMP);
  891. if (!polys)
  892. {
  893. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'polys' (%d).", maxVertsPerCont*nvp);
  894. return false;
  895. }
  896. unsigned short* tmpPoly = &polys[maxVertsPerCont*nvp];
  897. for (int i = 0; i < cset.nconts; ++i)
  898. {
  899. rcContour& cont = cset.conts[i];
  900. // Skip null contours.
  901. if (cont.nverts < 3)
  902. continue;
  903. // Triangulate contour
  904. for (int j = 0; j < cont.nverts; ++j)
  905. indices[j] = j;
  906. int ntris = triangulate(cont.nverts, cont.verts, &indices[0], &tris[0]);
  907. if (ntris <= 0)
  908. {
  909. // Bad triangulation, should not happen.
  910. /* printf("\tconst float bmin[3] = {%ff,%ff,%ff};\n", cset.bmin[0], cset.bmin[1], cset.bmin[2]);
  911. printf("\tconst float cs = %ff;\n", cset.cs);
  912. printf("\tconst float ch = %ff;\n", cset.ch);
  913. printf("\tconst int verts[] = {\n");
  914. for (int k = 0; k < cont.nverts; ++k)
  915. {
  916. const int* v = &cont.verts[k*4];
  917. printf("\t\t%d,%d,%d,%d,\n", v[0], v[1], v[2], v[3]);
  918. }
  919. printf("\t};\n\tconst int nverts = sizeof(verts)/(sizeof(int)*4);\n");*/
  920. ctx->log(RC_LOG_WARNING, "rcBuildPolyMesh: Bad triangulation Contour %d.", i);
  921. ntris = -ntris;
  922. }
  923. // Add and merge vertices.
  924. for (int j = 0; j < cont.nverts; ++j)
  925. {
  926. const int* v = &cont.verts[j*4];
  927. indices[j] = addVertex((unsigned short)v[0], (unsigned short)v[1], (unsigned short)v[2],
  928. mesh.verts, firstVert, nextVert, mesh.nverts);
  929. if (v[3] & RC_BORDER_VERTEX)
  930. {
  931. // This vertex should be removed.
  932. vflags[indices[j]] = 1;
  933. }
  934. }
  935. // Build initial polygons.
  936. int npolys = 0;
  937. memset(polys, 0xff, maxVertsPerCont*nvp*sizeof(unsigned short));
  938. for (int j = 0; j < ntris; ++j)
  939. {
  940. int* t = &tris[j*3];
  941. if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2])
  942. {
  943. polys[npolys*nvp+0] = (unsigned short)indices[t[0]];
  944. polys[npolys*nvp+1] = (unsigned short)indices[t[1]];
  945. polys[npolys*nvp+2] = (unsigned short)indices[t[2]];
  946. npolys++;
  947. }
  948. }
  949. if (!npolys)
  950. continue;
  951. // Merge polygons.
  952. if (nvp > 3)
  953. {
  954. for(;;)
  955. {
  956. // Find best polygons to merge.
  957. int bestMergeVal = 0;
  958. int bestPa = 0, bestPb = 0, bestEa = 0, bestEb = 0;
  959. for (int j = 0; j < npolys-1; ++j)
  960. {
  961. unsigned short* pj = &polys[j*nvp];
  962. for (int k = j+1; k < npolys; ++k)
  963. {
  964. unsigned short* pk = &polys[k*nvp];
  965. int ea, eb;
  966. int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb, nvp);
  967. if (v > bestMergeVal)
  968. {
  969. bestMergeVal = v;
  970. bestPa = j;
  971. bestPb = k;
  972. bestEa = ea;
  973. bestEb = eb;
  974. }
  975. }
  976. }
  977. if (bestMergeVal > 0)
  978. {
  979. // Found best, merge.
  980. unsigned short* pa = &polys[bestPa*nvp];
  981. unsigned short* pb = &polys[bestPb*nvp];
  982. mergePolys(pa, pb, bestEa, bestEb, tmpPoly, nvp);
  983. memcpy(pb, &polys[(npolys-1)*nvp], sizeof(unsigned short)*nvp);
  984. npolys--;
  985. }
  986. else
  987. {
  988. // Could not merge any polygons, stop.
  989. break;
  990. }
  991. }
  992. }
  993. // Store polygons.
  994. for (int j = 0; j < npolys; ++j)
  995. {
  996. unsigned short* p = &mesh.polys[mesh.npolys*nvp*2];
  997. unsigned short* q = &polys[j*nvp];
  998. for (int k = 0; k < nvp; ++k)
  999. p[k] = q[k];
  1000. mesh.regs[mesh.npolys] = cont.reg;
  1001. mesh.areas[mesh.npolys] = cont.area;
  1002. mesh.npolys++;
  1003. if (mesh.npolys > maxTris)
  1004. {
  1005. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Too many polygons %d (max:%d).", mesh.npolys, maxTris);
  1006. return false;
  1007. }
  1008. }
  1009. }
  1010. // Remove edge vertices.
  1011. for (int i = 0; i < mesh.nverts; ++i)
  1012. {
  1013. if (vflags[i])
  1014. {
  1015. if (!canRemoveVertex(ctx, mesh, (unsigned short)i))
  1016. continue;
  1017. if (!removeVertex(ctx, mesh, (unsigned short)i, maxTris))
  1018. {
  1019. // Failed to remove vertex
  1020. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Failed to remove edge vertex %d.", i);
  1021. return false;
  1022. }
  1023. // Remove vertex
  1024. // Note: mesh.nverts is already decremented inside removeVertex()!
  1025. // Fixup vertex flags
  1026. for (int j = i; j < mesh.nverts; ++j)
  1027. vflags[j] = vflags[j+1];
  1028. --i;
  1029. }
  1030. }
  1031. // Calculate adjacency.
  1032. if (!buildMeshAdjacency(mesh.polys, mesh.npolys, mesh.nverts, nvp))
  1033. {
  1034. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Adjacency failed.");
  1035. return false;
  1036. }
  1037. // Find portal edges
  1038. if (mesh.borderSize > 0)
  1039. {
  1040. const int w = cset.width;
  1041. const int h = cset.height;
  1042. for (int i = 0; i < mesh.npolys; ++i)
  1043. {
  1044. unsigned short* p = &mesh.polys[i*2*nvp];
  1045. for (int j = 0; j < nvp; ++j)
  1046. {
  1047. if (p[j] == RC_MESH_NULL_IDX) break;
  1048. // Skip connected edges.
  1049. if (p[nvp+j] != RC_MESH_NULL_IDX)
  1050. continue;
  1051. int nj = j+1;
  1052. if (nj >= nvp || p[nj] == RC_MESH_NULL_IDX) nj = 0;
  1053. const unsigned short* va = &mesh.verts[p[j]*3];
  1054. const unsigned short* vb = &mesh.verts[p[nj]*3];
  1055. if ((int)va[0] == 0 && (int)vb[0] == 0)
  1056. p[nvp+j] = 0x8000 | 0;
  1057. else if ((int)va[2] == h && (int)vb[2] == h)
  1058. p[nvp+j] = 0x8000 | 1;
  1059. else if ((int)va[0] == w && (int)vb[0] == w)
  1060. p[nvp+j] = 0x8000 | 2;
  1061. else if ((int)va[2] == 0 && (int)vb[2] == 0)
  1062. p[nvp+j] = 0x8000 | 3;
  1063. }
  1064. }
  1065. }
  1066. // Just allocate the mesh flags array. The user is resposible to fill it.
  1067. mesh.flags = (unsigned short*)rcAlloc(sizeof(unsigned short)*mesh.npolys, RC_ALLOC_PERM);
  1068. if (!mesh.flags)
  1069. {
  1070. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.flags' (%d).", mesh.npolys);
  1071. return false;
  1072. }
  1073. memset(mesh.flags, 0, sizeof(unsigned short) * mesh.npolys);
  1074. if (mesh.nverts > 0xffff)
  1075. {
  1076. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: The resulting mesh has too many vertices %d (max %d). Data can be corrupted.", mesh.nverts, 0xffff);
  1077. }
  1078. if (mesh.npolys > 0xffff)
  1079. {
  1080. ctx->log(RC_LOG_ERROR, "rcBuildPolyMesh: The resulting mesh has too many polygons %d (max %d). Data can be corrupted.", mesh.npolys, 0xffff);
  1081. }
  1082. ctx->stopTimer(RC_TIMER_BUILD_POLYMESH);
  1083. return true;
  1084. }
  1085. /// @see rcAllocPolyMesh, rcPolyMesh
  1086. bool rcMergePolyMeshes(rcContext* ctx, rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
  1087. {
  1088. rcAssert(ctx);
  1089. if (!nmeshes || !meshes)
  1090. return true;
  1091. ctx->startTimer(RC_TIMER_MERGE_POLYMESH);
  1092. mesh.nvp = meshes[0]->nvp;
  1093. mesh.cs = meshes[0]->cs;
  1094. mesh.ch = meshes[0]->ch;
  1095. rcVcopy(mesh.bmin, meshes[0]->bmin);
  1096. rcVcopy(mesh.bmax, meshes[0]->bmax);
  1097. int maxVerts = 0;
  1098. int maxPolys = 0;
  1099. int maxVertsPerMesh = 0;
  1100. for (int i = 0; i < nmeshes; ++i)
  1101. {
  1102. rcVmin(mesh.bmin, meshes[i]->bmin);
  1103. rcVmax(mesh.bmax, meshes[i]->bmax);
  1104. maxVertsPerMesh = rcMax(maxVertsPerMesh, meshes[i]->nverts);
  1105. maxVerts += meshes[i]->nverts;
  1106. maxPolys += meshes[i]->npolys;
  1107. }
  1108. mesh.nverts = 0;
  1109. mesh.verts = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVerts*3, RC_ALLOC_PERM);
  1110. if (!mesh.verts)
  1111. {
  1112. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.verts' (%d).", maxVerts*3);
  1113. return false;
  1114. }
  1115. mesh.npolys = 0;
  1116. mesh.polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxPolys*2*mesh.nvp, RC_ALLOC_PERM);
  1117. if (!mesh.polys)
  1118. {
  1119. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.polys' (%d).", maxPolys*2*mesh.nvp);
  1120. return false;
  1121. }
  1122. memset(mesh.polys, 0xff, sizeof(unsigned short)*maxPolys*2*mesh.nvp);
  1123. mesh.regs = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxPolys, RC_ALLOC_PERM);
  1124. if (!mesh.regs)
  1125. {
  1126. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.regs' (%d).", maxPolys);
  1127. return false;
  1128. }
  1129. memset(mesh.regs, 0, sizeof(unsigned short)*maxPolys);
  1130. mesh.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxPolys, RC_ALLOC_PERM);
  1131. if (!mesh.areas)
  1132. {
  1133. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.areas' (%d).", maxPolys);
  1134. return false;
  1135. }
  1136. memset(mesh.areas, 0, sizeof(unsigned char)*maxPolys);
  1137. mesh.flags = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxPolys, RC_ALLOC_PERM);
  1138. if (!mesh.flags)
  1139. {
  1140. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.flags' (%d).", maxPolys);
  1141. return false;
  1142. }
  1143. memset(mesh.flags, 0, sizeof(unsigned short)*maxPolys);
  1144. rcScopedDelete<int> nextVert = (int*)rcAlloc(sizeof(int)*maxVerts, RC_ALLOC_TEMP);
  1145. if (!nextVert)
  1146. {
  1147. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'nextVert' (%d).", maxVerts);
  1148. return false;
  1149. }
  1150. memset(nextVert, 0, sizeof(int)*maxVerts);
  1151. rcScopedDelete<int> firstVert = (int*)rcAlloc(sizeof(int)*VERTEX_BUCKET_COUNT, RC_ALLOC_TEMP);
  1152. if (!firstVert)
  1153. {
  1154. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'firstVert' (%d).", VERTEX_BUCKET_COUNT);
  1155. return false;
  1156. }
  1157. for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i)
  1158. firstVert[i] = -1;
  1159. rcScopedDelete<unsigned short> vremap = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertsPerMesh, RC_ALLOC_PERM);
  1160. if (!vremap)
  1161. {
  1162. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'vremap' (%d).", maxVertsPerMesh);
  1163. return false;
  1164. }
  1165. memset(vremap, 0, sizeof(unsigned short)*maxVertsPerMesh);
  1166. for (int i = 0; i < nmeshes; ++i)
  1167. {
  1168. const rcPolyMesh* pmesh = meshes[i];
  1169. const unsigned short ox = (unsigned short)floorf((pmesh->bmin[0]-mesh.bmin[0])/mesh.cs+0.5f);
  1170. const unsigned short oz = (unsigned short)floorf((pmesh->bmin[2]-mesh.bmin[2])/mesh.cs+0.5f);
  1171. for (int j = 0; j < pmesh->nverts; ++j)
  1172. {
  1173. unsigned short* v = &pmesh->verts[j*3];
  1174. vremap[j] = addVertex(v[0]+ox, v[1], v[2]+oz,
  1175. mesh.verts, firstVert, nextVert, mesh.nverts);
  1176. }
  1177. for (int j = 0; j < pmesh->npolys; ++j)
  1178. {
  1179. unsigned short* tgt = &mesh.polys[mesh.npolys*2*mesh.nvp];
  1180. unsigned short* src = &pmesh->polys[j*2*mesh.nvp];
  1181. mesh.regs[mesh.npolys] = pmesh->regs[j];
  1182. mesh.areas[mesh.npolys] = pmesh->areas[j];
  1183. mesh.flags[mesh.npolys] = pmesh->flags[j];
  1184. mesh.npolys++;
  1185. for (int k = 0; k < mesh.nvp; ++k)
  1186. {
  1187. if (src[k] == RC_MESH_NULL_IDX) break;
  1188. tgt[k] = vremap[src[k]];
  1189. }
  1190. }
  1191. }
  1192. // Calculate adjacency.
  1193. if (!buildMeshAdjacency(mesh.polys, mesh.npolys, mesh.nverts, mesh.nvp))
  1194. {
  1195. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: Adjacency failed.");
  1196. return false;
  1197. }
  1198. if (mesh.nverts > 0xffff)
  1199. {
  1200. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: The resulting mesh has too many vertices %d (max %d). Data can be corrupted.", mesh.nverts, 0xffff);
  1201. }
  1202. if (mesh.npolys > 0xffff)
  1203. {
  1204. ctx->log(RC_LOG_ERROR, "rcMergePolyMeshes: The resulting mesh has too many polygons %d (max %d). Data can be corrupted.", mesh.npolys, 0xffff);
  1205. }
  1206. ctx->stopTimer(RC_TIMER_MERGE_POLYMESH);
  1207. return true;
  1208. }
  1209. bool rcCopyPolyMesh(rcContext* ctx, const rcPolyMesh& src, rcPolyMesh& dst)
  1210. {
  1211. rcAssert(ctx);
  1212. // Destination must be empty.
  1213. rcAssert(dst.verts == 0);
  1214. rcAssert(dst.polys == 0);
  1215. rcAssert(dst.regs == 0);
  1216. rcAssert(dst.areas == 0);
  1217. rcAssert(dst.flags == 0);
  1218. dst.nverts = src.nverts;
  1219. dst.npolys = src.npolys;
  1220. dst.maxpolys = src.npolys;
  1221. dst.nvp = src.nvp;
  1222. rcVcopy(dst.bmin, src.bmin);
  1223. rcVcopy(dst.bmax, src.bmax);
  1224. dst.cs = src.cs;
  1225. dst.ch = src.ch;
  1226. dst.borderSize = src.borderSize;
  1227. dst.verts = (unsigned short*)rcAlloc(sizeof(unsigned short)*src.nverts*3, RC_ALLOC_PERM);
  1228. if (!dst.verts)
  1229. {
  1230. ctx->log(RC_LOG_ERROR, "rcCopyPolyMesh: Out of memory 'dst.verts' (%d).", src.nverts*3);
  1231. return false;
  1232. }
  1233. memcpy(dst.verts, src.verts, sizeof(unsigned short)*src.nverts*3);
  1234. dst.polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*src.npolys*2*src.nvp, RC_ALLOC_PERM);
  1235. if (!dst.polys)
  1236. {
  1237. ctx->log(RC_LOG_ERROR, "rcCopyPolyMesh: Out of memory 'dst.polys' (%d).", src.npolys*2*src.nvp);
  1238. return false;
  1239. }
  1240. memcpy(dst.polys, src.polys, sizeof(unsigned short)*src.npolys*2*src.nvp);
  1241. dst.regs = (unsigned short*)rcAlloc(sizeof(unsigned short)*src.npolys, RC_ALLOC_PERM);
  1242. if (!dst.regs)
  1243. {
  1244. ctx->log(RC_LOG_ERROR, "rcCopyPolyMesh: Out of memory 'dst.regs' (%d).", src.npolys);
  1245. return false;
  1246. }
  1247. memcpy(dst.regs, src.regs, sizeof(unsigned short)*src.npolys);
  1248. dst.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*src.npolys, RC_ALLOC_PERM);
  1249. if (!dst.areas)
  1250. {
  1251. ctx->log(RC_LOG_ERROR, "rcCopyPolyMesh: Out of memory 'dst.areas' (%d).", src.npolys);
  1252. return false;
  1253. }
  1254. memcpy(dst.areas, src.areas, sizeof(unsigned char)*src.npolys);
  1255. dst.flags = (unsigned short*)rcAlloc(sizeof(unsigned short)*src.npolys, RC_ALLOC_PERM);
  1256. if (!dst.flags)
  1257. {
  1258. ctx->log(RC_LOG_ERROR, "rcCopyPolyMesh: Out of memory 'dst.flags' (%d).", src.npolys);
  1259. return false;
  1260. }
  1261. memcpy(dst.flags, src.flags, sizeof(unsigned char)*src.npolys);
  1262. return true;
  1263. }