RcChunkyTriMesh.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using DotRecast.Core;
  22. namespace DotRecast.Recast.Geom
  23. {
  24. public class RcChunkyTriMesh
  25. {
  26. private List<RcChunkyTriMeshNode> nodes;
  27. private int ntris;
  28. private int maxTrisPerChunk;
  29. private void CalcExtends(BoundsItem[] items, int imin, int imax, ref RcVec2f bmin, ref RcVec2f bmax)
  30. {
  31. bmin.x = items[imin].bmin.x;
  32. bmin.y = items[imin].bmin.y;
  33. bmax.x = items[imin].bmax.x;
  34. bmax.y = items[imin].bmax.y;
  35. for (int i = imin + 1; i < imax; ++i)
  36. {
  37. BoundsItem it = items[i];
  38. if (it.bmin.x < bmin.x)
  39. {
  40. bmin.x = it.bmin.x;
  41. }
  42. if (it.bmin.y < bmin.y)
  43. {
  44. bmin.y = it.bmin.y;
  45. }
  46. if (it.bmax.x > bmax.x)
  47. {
  48. bmax.x = it.bmax.x;
  49. }
  50. if (it.bmax.y > bmax.y)
  51. {
  52. bmax.y = it.bmax.y;
  53. }
  54. }
  55. }
  56. private int LongestAxis(float x, float y)
  57. {
  58. return y > x ? 1 : 0;
  59. }
  60. private void Subdivide(BoundsItem[] items, int imin, int imax, int trisPerChunk, List<RcChunkyTriMeshNode> nodes, int[] inTris)
  61. {
  62. int inum = imax - imin;
  63. RcChunkyTriMeshNode node = new RcChunkyTriMeshNode();
  64. nodes.Add(node);
  65. if (inum <= trisPerChunk)
  66. {
  67. // Leaf
  68. CalcExtends(items, imin, imax, ref node.bmin, ref node.bmax);
  69. // Copy triangles.
  70. node.i = nodes.Count;
  71. node.tris = new int[inum * 3];
  72. int dst = 0;
  73. for (int i = imin; i < imax; ++i)
  74. {
  75. int src = items[i].i * 3;
  76. node.tris[dst++] = inTris[src];
  77. node.tris[dst++] = inTris[src + 1];
  78. node.tris[dst++] = inTris[src + 2];
  79. }
  80. }
  81. else
  82. {
  83. // Split
  84. CalcExtends(items, imin, imax, ref node.bmin, ref node.bmax);
  85. int axis = LongestAxis(node.bmax.x - node.bmin.x, node.bmax.y - node.bmin.y);
  86. if (axis == 0)
  87. {
  88. Array.Sort(items, imin, imax - imin, BoundsItemXComparer.Shared);
  89. // Sort along x-axis
  90. }
  91. else if (axis == 1)
  92. {
  93. Array.Sort(items, imin, imax - imin, BoundsItemYComparer.Shared);
  94. // Sort along y-axis
  95. }
  96. int isplit = imin + inum / 2;
  97. // Left
  98. Subdivide(items, imin, isplit, trisPerChunk, nodes, inTris);
  99. // Right
  100. Subdivide(items, isplit, imax, trisPerChunk, nodes, inTris);
  101. // Negative index means escape.
  102. node.i = -nodes.Count;
  103. }
  104. }
  105. public RcChunkyTriMesh(float[] verts, int[] tris, int ntris, int trisPerChunk)
  106. {
  107. int nchunks = (ntris + trisPerChunk - 1) / trisPerChunk;
  108. nodes = new List<RcChunkyTriMeshNode>(nchunks);
  109. this.ntris = ntris;
  110. // Build tree
  111. BoundsItem[] items = new BoundsItem[ntris];
  112. for (int i = 0; i < ntris; i++)
  113. {
  114. int t = i * 3;
  115. BoundsItem it = items[i] = new BoundsItem();
  116. it.i = i;
  117. // Calc triangle XZ bounds.
  118. it.bmin.x = it.bmax.x = verts[tris[t] * 3 + 0];
  119. it.bmin.y = it.bmax.y = verts[tris[t] * 3 + 2];
  120. for (int j = 1; j < 3; ++j)
  121. {
  122. int v = tris[t + j] * 3;
  123. if (verts[v] < it.bmin.x)
  124. {
  125. it.bmin.x = verts[v];
  126. }
  127. if (verts[v + 2] < it.bmin.y)
  128. {
  129. it.bmin.y = verts[v + 2];
  130. }
  131. if (verts[v] > it.bmax.x)
  132. {
  133. it.bmax.x = verts[v];
  134. }
  135. if (verts[v + 2] > it.bmax.y)
  136. {
  137. it.bmax.y = verts[v + 2];
  138. }
  139. }
  140. }
  141. Subdivide(items, 0, ntris, trisPerChunk, nodes, tris);
  142. // Calc max tris per node.
  143. maxTrisPerChunk = 0;
  144. foreach (RcChunkyTriMeshNode node in nodes)
  145. {
  146. bool isLeaf = node.i >= 0;
  147. if (!isLeaf)
  148. {
  149. continue;
  150. }
  151. if (node.tris.Length / 3 > maxTrisPerChunk)
  152. {
  153. maxTrisPerChunk = node.tris.Length / 3;
  154. }
  155. }
  156. }
  157. private bool CheckOverlapRect(float[] amin, float[] amax, RcVec2f bmin, RcVec2f bmax)
  158. {
  159. bool overlap = true;
  160. overlap = (amin[0] > bmax.x || amax[0] < bmin.x) ? false : overlap;
  161. overlap = (amin[1] > bmax.y || amax[1] < bmin.y) ? false : overlap;
  162. return overlap;
  163. }
  164. public List<RcChunkyTriMeshNode> GetChunksOverlappingRect(float[] bmin, float[] bmax)
  165. {
  166. // Traverse tree
  167. List<RcChunkyTriMeshNode> ids = new List<RcChunkyTriMeshNode>();
  168. int i = 0;
  169. while (i < nodes.Count)
  170. {
  171. RcChunkyTriMeshNode node = nodes[i];
  172. bool overlap = CheckOverlapRect(bmin, bmax, node.bmin, node.bmax);
  173. bool isLeafNode = node.i >= 0;
  174. if (isLeafNode && overlap)
  175. {
  176. ids.Add(node);
  177. }
  178. if (overlap || isLeafNode)
  179. {
  180. i++;
  181. }
  182. else
  183. {
  184. i = -node.i;
  185. }
  186. }
  187. return ids;
  188. }
  189. public List<RcChunkyTriMeshNode> GetChunksOverlappingSegment(float[] p, float[] q)
  190. {
  191. // Traverse tree
  192. List<RcChunkyTriMeshNode> ids = new List<RcChunkyTriMeshNode>();
  193. int i = 0;
  194. while (i < nodes.Count)
  195. {
  196. RcChunkyTriMeshNode node = nodes[i];
  197. bool overlap = CheckOverlapSegment(p, q, node.bmin, node.bmax);
  198. bool isLeafNode = node.i >= 0;
  199. if (isLeafNode && overlap)
  200. {
  201. ids.Add(node);
  202. }
  203. if (overlap || isLeafNode)
  204. {
  205. i++;
  206. }
  207. else
  208. {
  209. i = -node.i;
  210. }
  211. }
  212. return ids;
  213. }
  214. private bool CheckOverlapSegment(float[] p, float[] q, RcVec2f bmin, RcVec2f bmax)
  215. {
  216. float EPSILON = 1e-6f;
  217. float tmin = 0;
  218. float tmax = 1;
  219. float[] d = new float[2];
  220. d[0] = q[0] - p[0];
  221. d[1] = q[1] - p[1];
  222. for (int i = 0; i < 2; i++)
  223. {
  224. if (Math.Abs(d[i]) < EPSILON)
  225. {
  226. // Ray is parallel to slab. No hit if origin not within slab
  227. if (p[i] < bmin.Get(i) || p[i] > bmax.Get(i))
  228. return false;
  229. }
  230. else
  231. {
  232. // Compute intersection t value of ray with near and far plane of slab
  233. float ood = 1.0f / d[i];
  234. float t1 = (bmin.Get(i) - p[i]) * ood;
  235. float t2 = (bmax.Get(i) - p[i]) * ood;
  236. if (t1 > t2)
  237. {
  238. (t1, t2) = (t2, t1);
  239. }
  240. if (t1 > tmin)
  241. tmin = t1;
  242. if (t2 < tmax)
  243. tmax = t2;
  244. if (tmin > tmax)
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. }
  251. }