ChunkyTriMesh.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  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. #include "ChunkyTriMesh.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. struct BoundsItem
  23. {
  24. float bmin[2];
  25. float bmax[2];
  26. int i;
  27. };
  28. static int compareItemX(const void* va, const void* vb)
  29. {
  30. const BoundsItem* a = (const BoundsItem*)va;
  31. const BoundsItem* b = (const BoundsItem*)vb;
  32. if (a->bmin[0] < b->bmin[0])
  33. return -1;
  34. if (a->bmin[0] > b->bmin[0])
  35. return 1;
  36. return 0;
  37. }
  38. static int compareItemY(const void* va, const void* vb)
  39. {
  40. const BoundsItem* a = (const BoundsItem*)va;
  41. const BoundsItem* b = (const BoundsItem*)vb;
  42. if (a->bmin[1] < b->bmin[1])
  43. return -1;
  44. if (a->bmin[1] > b->bmin[1])
  45. return 1;
  46. return 0;
  47. }
  48. static void calcExtends(const BoundsItem* items, const int /*nitems*/,
  49. const int imin, const int imax,
  50. float* bmin, float* bmax)
  51. {
  52. bmin[0] = items[imin].bmin[0];
  53. bmin[1] = items[imin].bmin[1];
  54. bmax[0] = items[imin].bmax[0];
  55. bmax[1] = items[imin].bmax[1];
  56. for (int i = imin+1; i < imax; ++i)
  57. {
  58. const BoundsItem& it = items[i];
  59. if (it.bmin[0] < bmin[0]) bmin[0] = it.bmin[0];
  60. if (it.bmin[1] < bmin[1]) bmin[1] = it.bmin[1];
  61. if (it.bmax[0] > bmax[0]) bmax[0] = it.bmax[0];
  62. if (it.bmax[1] > bmax[1]) bmax[1] = it.bmax[1];
  63. }
  64. }
  65. inline int longestAxis(float x, float y)
  66. {
  67. return y > x ? 1 : 0;
  68. }
  69. static void subdivide(BoundsItem* items, int nitems, int imin, int imax, int trisPerChunk,
  70. int& curNode, rcChunkyTriMeshNode* nodes, const int maxNodes,
  71. int& curTri, int* outTris, const int* inTris)
  72. {
  73. int inum = imax - imin;
  74. int icur = curNode;
  75. if (curNode > maxNodes)
  76. return;
  77. rcChunkyTriMeshNode& node = nodes[curNode++];
  78. if (inum <= trisPerChunk)
  79. {
  80. // Leaf
  81. calcExtends(items, nitems, imin, imax, node.bmin, node.bmax);
  82. // Copy triangles.
  83. node.i = curTri;
  84. node.n = inum;
  85. for (int i = imin; i < imax; ++i)
  86. {
  87. const int* src = &inTris[items[i].i*3];
  88. int* dst = &outTris[curTri*3];
  89. curTri++;
  90. dst[0] = src[0];
  91. dst[1] = src[1];
  92. dst[2] = src[2];
  93. }
  94. }
  95. else
  96. {
  97. // Split
  98. calcExtends(items, nitems, imin, imax, node.bmin, node.bmax);
  99. int axis = longestAxis(node.bmax[0] - node.bmin[0],
  100. node.bmax[1] - node.bmin[1]);
  101. if (axis == 0)
  102. {
  103. // Sort along x-axis
  104. qsort(items+imin, static_cast<size_t>(inum), sizeof(BoundsItem), compareItemX);
  105. }
  106. else if (axis == 1)
  107. {
  108. // Sort along y-axis
  109. qsort(items+imin, static_cast<size_t>(inum), sizeof(BoundsItem), compareItemY);
  110. }
  111. int isplit = imin+inum/2;
  112. // Left
  113. subdivide(items, nitems, imin, isplit, trisPerChunk, curNode, nodes, maxNodes, curTri, outTris, inTris);
  114. // Right
  115. subdivide(items, nitems, isplit, imax, trisPerChunk, curNode, nodes, maxNodes, curTri, outTris, inTris);
  116. int iescape = curNode - icur;
  117. // Negative index means escape.
  118. node.i = -iescape;
  119. }
  120. }
  121. bool rcCreateChunkyTriMesh(const float* verts, const int* tris, int ntris,
  122. int trisPerChunk, rcChunkyTriMesh* cm)
  123. {
  124. int nchunks = (ntris + trisPerChunk-1) / trisPerChunk;
  125. cm->nodes = new rcChunkyTriMeshNode[nchunks*4];
  126. if (!cm->nodes)
  127. return false;
  128. cm->tris = new int[ntris*3];
  129. if (!cm->tris)
  130. return false;
  131. cm->ntris = ntris;
  132. // Build tree
  133. BoundsItem* items = new BoundsItem[ntris];
  134. if (!items)
  135. return false;
  136. for (int i = 0; i < ntris; i++)
  137. {
  138. const int* t = &tris[i*3];
  139. BoundsItem& it = items[i];
  140. it.i = i;
  141. // Calc triangle XZ bounds.
  142. it.bmin[0] = it.bmax[0] = verts[t[0]*3+0];
  143. it.bmin[1] = it.bmax[1] = verts[t[0]*3+2];
  144. for (int j = 1; j < 3; ++j)
  145. {
  146. const float* v = &verts[t[j]*3];
  147. if (v[0] < it.bmin[0]) it.bmin[0] = v[0];
  148. if (v[2] < it.bmin[1]) it.bmin[1] = v[2];
  149. if (v[0] > it.bmax[0]) it.bmax[0] = v[0];
  150. if (v[2] > it.bmax[1]) it.bmax[1] = v[2];
  151. }
  152. }
  153. int curTri = 0;
  154. int curNode = 0;
  155. subdivide(items, ntris, 0, ntris, trisPerChunk, curNode, cm->nodes, nchunks*4, curTri, cm->tris, tris);
  156. delete [] items;
  157. cm->nnodes = curNode;
  158. // Calc max tris per node.
  159. cm->maxTrisPerChunk = 0;
  160. for (int i = 0; i < cm->nnodes; ++i)
  161. {
  162. rcChunkyTriMeshNode& node = cm->nodes[i];
  163. const bool isLeaf = node.i >= 0;
  164. if (!isLeaf) continue;
  165. if (node.n > cm->maxTrisPerChunk)
  166. cm->maxTrisPerChunk = node.n;
  167. }
  168. return true;
  169. }
  170. inline bool checkOverlapRect(const float amin[2], const float amax[2],
  171. const float bmin[2], const float bmax[2])
  172. {
  173. bool overlap = true;
  174. overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
  175. overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
  176. return overlap;
  177. }
  178. int rcGetChunksOverlappingRect(const rcChunkyTriMesh* cm,
  179. float bmin[2], float bmax[2],
  180. int* ids, const int maxIds)
  181. {
  182. // Traverse tree
  183. int i = 0;
  184. int n = 0;
  185. while (i < cm->nnodes)
  186. {
  187. const rcChunkyTriMeshNode* node = &cm->nodes[i];
  188. const bool overlap = checkOverlapRect(bmin, bmax, node->bmin, node->bmax);
  189. const bool isLeafNode = node->i >= 0;
  190. if (isLeafNode && overlap)
  191. {
  192. if (n < maxIds)
  193. {
  194. ids[n] = i;
  195. n++;
  196. }
  197. }
  198. if (overlap || isLeafNode)
  199. i++;
  200. else
  201. {
  202. const int escapeIndex = -node->i;
  203. i += escapeIndex;
  204. }
  205. }
  206. return n;
  207. }
  208. static bool checkOverlapSegment(const float p[2], const float q[2],
  209. const float bmin[2], const float bmax[2])
  210. {
  211. static const float EPSILON = 1e-6f;
  212. float tmin = 0;
  213. float tmax = 1;
  214. float d[2];
  215. d[0] = q[0] - p[0];
  216. d[1] = q[1] - p[1];
  217. for (int i = 0; i < 2; i++)
  218. {
  219. if (fabsf(d[i]) < EPSILON)
  220. {
  221. // Ray is parallel to slab. No hit if origin not within slab
  222. if (p[i] < bmin[i] || p[i] > bmax[i])
  223. return false;
  224. }
  225. else
  226. {
  227. // Compute intersection t value of ray with near and far plane of slab
  228. float ood = 1.0f / d[i];
  229. float t1 = (bmin[i] - p[i]) * ood;
  230. float t2 = (bmax[i] - p[i]) * ood;
  231. if (t1 > t2) { float tmp = t1; t1 = t2; t2 = tmp; }
  232. if (t1 > tmin) tmin = t1;
  233. if (t2 < tmax) tmax = t2;
  234. if (tmin > tmax) return false;
  235. }
  236. }
  237. return true;
  238. }
  239. int rcGetChunksOverlappingSegment(const rcChunkyTriMesh* cm,
  240. float p[2], float q[2],
  241. int* ids, const int maxIds)
  242. {
  243. // Traverse tree
  244. int i = 0;
  245. int n = 0;
  246. while (i < cm->nnodes)
  247. {
  248. const rcChunkyTriMeshNode* node = &cm->nodes[i];
  249. const bool overlap = checkOverlapSegment(p, q, node->bmin, node->bmax);
  250. const bool isLeafNode = node->i >= 0;
  251. if (isLeafNode && overlap)
  252. {
  253. if (n < maxIds)
  254. {
  255. ids[n] = i;
  256. n++;
  257. }
  258. }
  259. if (overlap || isLeafNode)
  260. i++;
  261. else
  262. {
  263. const int escapeIndex = -node->i;
  264. i += escapeIndex;
  265. }
  266. }
  267. return n;
  268. }