Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UTess.cs 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Unity.Profiling;
  5. using Unity.Collections;
  6. using Unity.Mathematics;
  7. using Unity.Collections.LowLevel.Unsafe;
  8. namespace UnityEngine.U2D.Common.UTess
  9. {
  10. enum UEventType
  11. {
  12. EVENT_POINT = 0,
  13. EVENT_END = 1,
  14. EVENT_START = 2,
  15. };
  16. struct UEvent
  17. {
  18. public float2 a;
  19. public float2 b;
  20. public int idx;
  21. public int type;
  22. };
  23. struct UHull
  24. {
  25. public float2 a;
  26. public float2 b;
  27. public int idx;
  28. public ArraySlice<int> ilarray;
  29. public int ilcount;
  30. public ArraySlice<int> iuarray;
  31. public int iucount;
  32. };
  33. struct UStar
  34. {
  35. public ArraySlice<int> points;
  36. public int pointCount;
  37. };
  38. struct UBounds
  39. {
  40. public double2 min;
  41. public double2 max;
  42. };
  43. struct UCircle
  44. {
  45. public float2 center;
  46. public float radius;
  47. };
  48. struct UTriangle
  49. {
  50. public float2 va;
  51. public float2 vb;
  52. public float2 vc;
  53. public UCircle c;
  54. public float area;
  55. public int3 indices;
  56. };
  57. struct UEncroachingSegment
  58. {
  59. public float2 a;
  60. public float2 b;
  61. public int index;
  62. }
  63. internal interface ICondition2<in T, in U>
  64. {
  65. bool Test(T x, U y, ref float t);
  66. }
  67. struct XCompare : IComparer<double>
  68. {
  69. public int Compare(double a, double b)
  70. {
  71. return (a < b) ? -1 : 1;
  72. }
  73. }
  74. unsafe struct IntersectionCompare : IComparer<int2>
  75. {
  76. public Array<double2> points;
  77. public Array<int2> edges;
  78. public fixed double xvasort[4];
  79. public fixed double xvbsort[4];
  80. public int Compare(int2 a, int2 b)
  81. {
  82. var e1a = edges[a.x];
  83. var e1b = edges[a.y];
  84. var e2a = edges[b.x];
  85. var e2b = edges[b.y];
  86. xvasort[0] = points[e1a.x].x;
  87. xvasort[1] = points[e1a.y].x;
  88. xvasort[2] = points[e1b.x].x;
  89. xvasort[3] = points[e1b.y].x;
  90. xvbsort[0] = points[e2a.x].x;
  91. xvbsort[1] = points[e2a.y].x;
  92. xvbsort[2] = points[e2b.x].x;
  93. xvbsort[3] = points[e2b.y].x;
  94. fixed (double* xvasortPtr = xvasort)
  95. {
  96. ModuleHandle.InsertionSort<double, XCompare>(xvasortPtr, 0, 3, new XCompare());
  97. }
  98. fixed (double* xvbsortPtr = xvbsort)
  99. {
  100. ModuleHandle.InsertionSort<double, XCompare>(xvbsortPtr, 0, 3, new XCompare());
  101. }
  102. for (int i = 0; i < 4; ++i)
  103. if (xvasort[i] - xvbsort[i] != 0)
  104. return xvasort[i] < xvbsort[i] ? -1 : 1;
  105. return points[e1a.x].y < points[e1a.x].y ? -1 : 1;
  106. }
  107. }
  108. struct TessEventCompare : IComparer<UEvent>
  109. {
  110. public int Compare(UEvent a, UEvent b)
  111. {
  112. float f = (a.a.x - b.a.x);
  113. if (0 != f)
  114. return (f > 0) ? 1 : -1;
  115. f = (a.a.y - b.a.y);
  116. if (0 != f)
  117. return (f > 0) ? 1 : -1;
  118. int i = a.type - b.type;
  119. if (0 != i)
  120. return i;
  121. if (a.type != (int)UEventType.EVENT_POINT)
  122. {
  123. float o = ModuleHandle.OrientFast(a.a, a.b, b.b);
  124. if (0 != o)
  125. {
  126. return (o > 0) ? 1 : -1;
  127. }
  128. }
  129. return a.idx - b.idx;
  130. }
  131. }
  132. struct TessEdgeCompare : IComparer<int2>
  133. {
  134. public int Compare(int2 a, int2 b)
  135. {
  136. int i = a.x - b.x;
  137. if (0 != i)
  138. return i;
  139. i = a.y - b.y;
  140. return i;
  141. }
  142. }
  143. struct TessCellCompare : IComparer<int3>
  144. {
  145. public int Compare(int3 a, int3 b)
  146. {
  147. int i = a.x - b.x;
  148. if (0 != i)
  149. return i;
  150. i = a.y - b.y;
  151. if (0 != i)
  152. return i;
  153. i = a.z - b.z;
  154. return i;
  155. }
  156. }
  157. struct TessJunctionCompare : IComparer<int2>
  158. {
  159. public int Compare(int2 a, int2 b)
  160. {
  161. int i = a.x - b.x;
  162. if (0 != i)
  163. return i;
  164. i = a.y - b.y;
  165. return i;
  166. }
  167. }
  168. struct DelaEdgeCompare : IComparer<int4>
  169. {
  170. public int Compare(int4 a, int4 b)
  171. {
  172. int i = a.x - b.x;
  173. if (0 != i)
  174. return i;
  175. i = a.y - b.y;
  176. if (0 != i)
  177. return i;
  178. i = a.z - b.z;
  179. if (0 != i)
  180. return i;
  181. i = a.w - b.w;
  182. return i;
  183. }
  184. }
  185. struct TessLink
  186. {
  187. internal NativeArray<int> roots;
  188. internal NativeArray<int> ranks;
  189. internal static TessLink CreateLink(int count, Allocator allocator)
  190. {
  191. TessLink link = new TessLink();
  192. link.roots = new NativeArray<int>(count, allocator);
  193. link.ranks = new NativeArray<int>(count, allocator);
  194. for (int i = 0; i < count; ++i)
  195. {
  196. link.roots[i] = i;
  197. link.ranks[i] = 0;
  198. }
  199. return link;
  200. }
  201. internal static void DestroyLink(TessLink link)
  202. {
  203. link.ranks.Dispose();
  204. link.roots.Dispose();
  205. }
  206. internal int Find(int x)
  207. {
  208. var x0 = x;
  209. while (roots[x] != x)
  210. {
  211. x = roots[x];
  212. }
  213. while (roots[x0] != x)
  214. {
  215. var y = roots[x0];
  216. roots[x0] = x;
  217. x0 = y;
  218. }
  219. return x;
  220. }
  221. internal void Link(int x, int y)
  222. {
  223. var xr = Find(x);
  224. var yr = Find(y);
  225. if (xr == yr)
  226. {
  227. return;
  228. }
  229. var xd = ranks[xr];
  230. var yd = ranks[yr];
  231. if (xd < yd)
  232. {
  233. roots[xr] = yr;
  234. }
  235. else if (yd < xd)
  236. {
  237. roots[yr] = xr;
  238. }
  239. else
  240. {
  241. roots[yr] = xr;
  242. ++ranks[xr];
  243. }
  244. }
  245. };
  246. internal struct ModuleHandle
  247. {
  248. // Max Edge Count with Subdivision allowed. This is already a very relaxed limit
  249. // and anything beyond are basically littered with numerous paths.
  250. internal static readonly int kMaxArea = 65536;
  251. internal static readonly int kMaxEdgeCount = 65536;
  252. internal static readonly int kMaxIndexCount = 65536;
  253. internal static readonly int kMaxVertexCount = 65536;
  254. internal static readonly int kMaxTriangleCount = kMaxIndexCount / 3;
  255. internal static readonly int kMaxRefineIterations = 48;
  256. internal static readonly int kMaxSmoothenIterations = 256;
  257. internal static readonly float kIncrementAreaFactor = 1.2f;
  258. internal static void Copy<T>(NativeArray<T> src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
  259. where T : struct
  260. {
  261. NativeArray<T>.Copy(src, srcIndex, dst, dstIndex, length);
  262. }
  263. internal static void Copy<T>(NativeArray<T> src, NativeArray<T> dst, int length)
  264. where T : struct
  265. {
  266. Copy(src, 0, dst, 0, length);
  267. }
  268. internal static unsafe void InsertionSort<T, U>(void* array, int lo, int hi, U comp)
  269. where T : struct where U : IComparer<T>
  270. {
  271. int i, j;
  272. T t;
  273. for (i = lo; i < hi; i++)
  274. {
  275. j = i;
  276. t = UnsafeUtility.ReadArrayElement<T>(array, i + 1);
  277. while (j >= lo && comp.Compare(t, UnsafeUtility.ReadArrayElement<T>(array, j)) < 0)
  278. {
  279. UnsafeUtility.WriteArrayElement<T>(array, j + 1, UnsafeUtility.ReadArrayElement<T>(array, j));
  280. j--;
  281. }
  282. UnsafeUtility.WriteArrayElement<T>(array, j + 1, t);
  283. }
  284. }
  285. // Search Lower Bounds
  286. internal static int GetLower<T, U, X>(NativeArray<T> values, int count, U check, X condition)
  287. where T : struct where U : struct where X : ICondition2<T, U>
  288. {
  289. int l = 0;
  290. int h = count - 1;
  291. int i = l - 1;
  292. while (l <= h)
  293. {
  294. int m = ((int)(l + h)) >> 1;
  295. float t = 0;
  296. if (condition.Test(values[m], check, ref t))
  297. {
  298. i = m;
  299. l = m + 1;
  300. }
  301. else
  302. {
  303. h = m - 1;
  304. }
  305. }
  306. return i;
  307. }
  308. // Search Upper Bounds
  309. internal static int GetUpper<T, U, X>(NativeArray<T> values, int count, U check, X condition)
  310. where T : struct where U : struct where X : ICondition2<T, U>
  311. {
  312. int l = 0;
  313. int h = count - 1;
  314. int i = h + 1;
  315. while (l <= h)
  316. {
  317. int m = ((int)(l + h)) >> 1;
  318. float t = 0;
  319. if (condition.Test(values[m], check, ref t))
  320. {
  321. i = m;
  322. h = m - 1;
  323. }
  324. else
  325. {
  326. l = m + 1;
  327. }
  328. }
  329. return i;
  330. }
  331. // Search for Equal
  332. internal static int GetEqual<T, U, X>(Array<T> values, int count, U check, X condition)
  333. where T : struct where U : struct where X : ICondition2<T, U>
  334. {
  335. int l = 0;
  336. int h = count - 1;
  337. while (l <= h)
  338. {
  339. int m = ((int)(l + h)) >> 1;
  340. float t = 0;
  341. condition.Test(values[m], check, ref t);
  342. if (t == 0)
  343. {
  344. return m;
  345. }
  346. else if (t <= 0)
  347. {
  348. l = m + 1;
  349. }
  350. else
  351. {
  352. h = m - 1;
  353. }
  354. }
  355. return -1;
  356. }
  357. // Search for Equal
  358. internal static int GetEqual<T, U, X>(NativeArray<T> values, int count, U check, X condition)
  359. where T : struct where U : struct where X : ICondition2<T, U>
  360. {
  361. int l = 0;
  362. int h = count - 1;
  363. while (l <= h)
  364. {
  365. int m = ((int)(l + h)) >> 1;
  366. float t = 0;
  367. condition.Test(values[m], check, ref t);
  368. if (t == 0)
  369. {
  370. return m;
  371. }
  372. else if (t <= 0)
  373. {
  374. l = m + 1;
  375. }
  376. else
  377. {
  378. h = m - 1;
  379. }
  380. }
  381. return -1;
  382. }
  383. // Simple Orientation test.
  384. internal static float OrientFast(float2 a, float2 b, float2 c)
  385. {
  386. float epsilon = 1.1102230246251565e-16f;
  387. float det = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
  388. if (math.abs(det) < epsilon) return 0;
  389. return det;
  390. }
  391. // This is needed when doing PlanarGraph as it requires high precision separation of points.
  392. internal static double OrientFastDouble(double2 a, double2 b, double2 c)
  393. {
  394. double epsilon = 1.1102230246251565e-16f;
  395. double det = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
  396. if (math.abs(det) < epsilon) return 0;
  397. return det;
  398. }
  399. internal static UCircle CircumCircle(UTriangle tri)
  400. {
  401. float xa = tri.va.x * tri.va.x;
  402. float xb = tri.vb.x * tri.vb.x;
  403. float xc = tri.vc.x * tri.vc.x;
  404. float ya = tri.va.y * tri.va.y;
  405. float yb = tri.vb.y * tri.vb.y;
  406. float yc = tri.vc.y * tri.vc.y;
  407. float c = 2f * ((tri.vb.x - tri.va.x) * (tri.vc.y - tri.va.y) - (tri.vb.y - tri.va.y) * (tri.vc.x - tri.va.x));
  408. float x = ((tri.vc.y - tri.va.y) * (xb - xa + yb - ya) + (tri.va.y - tri.vb.y) * (xc - xa + yc - ya)) / c;
  409. float y = ((tri.va.x - tri.vc.x) * (xb - xa + yb - ya) + (tri.vb.x - tri.va.x) * (xc - xa + yc - ya)) / c;
  410. float vx = (tri.va.x - x);
  411. float vy = (tri.va.y - y);
  412. return new UCircle { center = new float2(x, y), radius = math.sqrt((vx * vx) + (vy * vy)) };
  413. }
  414. internal static bool IsInsideCircle(UCircle c, float2 v)
  415. {
  416. return math.distance(v, c.center) < c.radius;
  417. }
  418. internal static float TriangleArea(float2 va, float2 vb, float2 vc)
  419. {
  420. float3 a = new float3(va.x, va.y, 0);
  421. float3 b = new float3(vb.x, vb.y, 0);
  422. float3 c = new float3(vc.x, vc.y, 0);
  423. float3 v = math.cross(a - b, a - c);
  424. return math.abs(v.z) * 0.5f;
  425. }
  426. internal static float Sign(float2 p1, float2 p2, float2 p3)
  427. {
  428. return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
  429. }
  430. internal static bool IsInsideTriangle(float2 pt, float2 v1, float2 v2, float2 v3)
  431. {
  432. float d1, d2, d3;
  433. bool has_neg, has_pos;
  434. d1 = Sign(pt, v1, v2);
  435. d2 = Sign(pt, v2, v3);
  436. d3 = Sign(pt, v3, v1);
  437. has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
  438. has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
  439. return !(has_neg && has_pos);
  440. }
  441. internal static bool IsInsideTriangleApproximate(float2 pt, float2 v1, float2 v2, float2 v3)
  442. {
  443. float d0, d1, d2, d3;
  444. d0 = TriangleArea(v1, v2, v3);
  445. d1 = TriangleArea(pt, v1, v2);
  446. d2 = TriangleArea(pt, v2, v3);
  447. d3 = TriangleArea(pt, v3, v1);
  448. float epsilon = 1.1102230246251565e-16f;
  449. return Mathf.Abs(d0 - (d1 + d2 + d3)) < epsilon;
  450. }
  451. internal static bool IsInsideCircle(float2 a, float2 b, float2 c, float2 p)
  452. {
  453. float ab = math.dot(a, a);
  454. float cd = math.dot(b, b);
  455. float ef = math.dot(c, c);
  456. float ax = a.x;
  457. float ay = a.y;
  458. float bx = b.x;
  459. float by = b.y;
  460. float cx = c.x;
  461. float cy = c.y;
  462. float circum_x = (ab * (cy - by) + cd * (ay - cy) + ef * (by - ay)) /
  463. (ax * (cy - by) + bx * (ay - cy) + cx * (by - ay));
  464. float circum_y = (ab * (cx - bx) + cd * (ax - cx) + ef * (bx - ax)) /
  465. (ay * (cx - bx) + by * (ax - cx) + cy * (bx - ax));
  466. float2 circum = new float2();
  467. circum.x = circum_x / 2;
  468. circum.y = circum_y / 2;
  469. float circum_radius = math.distance(a, circum);
  470. float dist = math.distance(p, circum);
  471. return circum_radius - dist > 0.00001f;
  472. }
  473. internal static void BuildTriangles(NativeArray<float2> vertices, int vertexCount, NativeArray<int> indices, int indexCount, ref NativeArray<UTriangle> triangles, ref int triangleCount, ref float maxArea, ref float avgArea, ref float minArea)
  474. {
  475. // Check if there are invalid triangles or segments.
  476. for (int i = 0; i < indexCount; i += 3)
  477. {
  478. UTriangle tri = new UTriangle();
  479. var i0 = indices[i + 0];
  480. var i1 = indices[i + 1];
  481. var i2 = indices[i + 2];
  482. tri.va = vertices[i0];
  483. tri.vb = vertices[i1];
  484. tri.vc = vertices[i2];
  485. tri.c = CircumCircle(tri);
  486. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  487. maxArea = math.max(tri.area, maxArea);
  488. minArea = math.min(tri.area, minArea);
  489. avgArea = avgArea + tri.area;
  490. triangles[triangleCount++] = tri;
  491. }
  492. avgArea = avgArea / triangleCount;
  493. }
  494. internal static void BuildTriangles(NativeArray<float2> vertices, int vertexCount, NativeArray<int> indices, int indexCount, ref Array<UTriangle> triangles, ref int triangleCount, ref float maxArea, ref float avgArea, ref float minArea)
  495. {
  496. // Check if there are invalid triangles or segments.
  497. for (int i = 0; i < indexCount; i += 3)
  498. {
  499. UTriangle tri = new UTriangle();
  500. var i0 = indices[i + 0];
  501. var i1 = indices[i + 1];
  502. var i2 = indices[i + 2];
  503. tri.va = vertices[i0];
  504. tri.vb = vertices[i1];
  505. tri.vc = vertices[i2];
  506. tri.c = CircumCircle(tri);
  507. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  508. maxArea = math.max(tri.area, maxArea);
  509. minArea = math.min(tri.area, minArea);
  510. avgArea = avgArea + tri.area;
  511. triangles[triangleCount++] = tri;
  512. }
  513. avgArea = avgArea / triangleCount;
  514. }
  515. internal static void BuildTriangles(NativeArray<float2> vertices, int vertexCount, NativeArray<int> indices, int indexCount, ref NativeArray<UTriangle> triangles, ref int triangleCount, ref float maxArea, ref float avgArea, ref float minArea, ref float maxEdge, ref float avgEdge, ref float minEdge)
  516. {
  517. // Check if there are invalid triangles or segments.
  518. for (int i = 0; i < indexCount; i += 3)
  519. {
  520. UTriangle tri = new UTriangle();
  521. var i0 = indices[i + 0];
  522. var i1 = indices[i + 1];
  523. var i2 = indices[i + 2];
  524. tri.va = vertices[i0];
  525. tri.vb = vertices[i1];
  526. tri.vc = vertices[i2];
  527. tri.c = CircumCircle(tri);
  528. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  529. maxArea = math.max(tri.area, maxArea);
  530. minArea = math.min(tri.area, minArea);
  531. avgArea = avgArea + tri.area;
  532. var e1 = math.distance(tri.va, tri.vb);
  533. var e2 = math.distance(tri.vb, tri.vc);
  534. var e3 = math.distance(tri.vc, tri.va);
  535. maxEdge = math.max(e1, maxEdge);
  536. maxEdge = math.max(e2, maxEdge);
  537. maxEdge = math.max(e3, maxEdge);
  538. minEdge = math.min(e1, minEdge);
  539. minEdge = math.min(e2, minEdge);
  540. minEdge = math.min(e3, minEdge);
  541. avgEdge = avgEdge + e1;
  542. avgEdge = avgEdge + e2;
  543. avgEdge = avgEdge + e3;
  544. triangles[triangleCount++] = tri;
  545. }
  546. avgArea = avgArea / triangleCount;
  547. avgEdge = avgEdge / indexCount;
  548. }
  549. internal static void BuildTrianglesAndEdges(NativeArray<float2> vertices, int vertexCount, NativeArray<int> indices, int indexCount, ref NativeArray<UTriangle> triangles, ref int triangleCount, ref NativeArray<int4> delaEdges, ref int delaEdgeCount, ref float maxArea, ref float avgArea, ref float minArea)
  550. {
  551. // Check if there are invalid triangles or segments.
  552. for (int i = 0; i < indexCount; i += 3)
  553. {
  554. UTriangle tri = new UTriangle();
  555. var i0 = indices[i + 0];
  556. var i1 = indices[i + 1];
  557. var i2 = indices[i + 2];
  558. tri.va = vertices[i0];
  559. tri.vb = vertices[i1];
  560. tri.vc = vertices[i2];
  561. tri.c = CircumCircle(tri);
  562. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  563. maxArea = math.max(tri.area, maxArea);
  564. minArea = math.min(tri.area, minArea);
  565. avgArea = avgArea + tri.area;
  566. tri.indices = new int3(i0, i1, i2);
  567. // Outputs.
  568. delaEdges[delaEdgeCount++] = new int4(math.min(i0, i1), math.max(i0, i1), triangleCount, -1);
  569. delaEdges[delaEdgeCount++] = new int4(math.min(i1, i2), math.max(i1, i2), triangleCount, -1);
  570. delaEdges[delaEdgeCount++] = new int4(math.min(i2, i0), math.max(i2, i0), triangleCount, -1);
  571. triangles[triangleCount++] = tri;
  572. }
  573. avgArea = avgArea / triangleCount;
  574. }
  575. static void CopyGraph(NativeArray<float2> srcPoints, int srcPointCount, ref NativeArray<float2> dstPoints, ref int dstPointCount, NativeArray<int2> srcEdges, int srcEdgeCount, ref NativeArray<int2> dstEdges, ref int dstEdgeCount)
  576. {
  577. dstEdgeCount = srcEdgeCount;
  578. dstPointCount = srcPointCount;
  579. Copy(srcEdges, dstEdges, srcEdgeCount);
  580. Copy(srcPoints, dstPoints, srcPointCount);
  581. }
  582. static void CopyGeometry(NativeArray<int> srcIndices, int srcIndexCount, ref NativeArray<int> dstIndices, ref int dstIndexCount, NativeArray<float2> srcVertices, int srcVertexCount, ref NativeArray<float2> dstVertices, ref int dstVertexCount)
  583. {
  584. dstIndexCount = srcIndexCount;
  585. dstVertexCount = srcVertexCount;
  586. Copy(srcIndices, dstIndices, srcIndexCount);
  587. Copy(srcVertices, dstVertices, srcVertexCount);
  588. }
  589. static void TransferOutput(NativeArray<int2> srcEdges, int srcEdgeCount, ref NativeArray<int2> dstEdges, ref int dstEdgeCount, NativeArray<int> srcIndices, int srcIndexCount, ref NativeArray<int> dstIndices, ref int dstIndexCount, NativeArray<float2> srcVertices, int srcVertexCount, ref NativeArray<float2> dstVertices, ref int dstVertexCount)
  590. {
  591. dstEdgeCount = srcEdgeCount;
  592. dstIndexCount = srcIndexCount;
  593. dstVertexCount = srcVertexCount;
  594. Copy(srcEdges, dstEdges, srcEdgeCount);
  595. Copy(srcIndices, dstIndices, srcIndexCount);
  596. Copy(srcVertices, dstVertices, srcVertexCount);
  597. }
  598. static void GraphConditioner(NativeArray<float2> points, ref NativeArray<float2> pgPoints, ref int pgPointCount, ref NativeArray<int2> pgEdges, ref int pgEdgeCount, bool resetTopology)
  599. {
  600. var min = new float2(math.INFINITY, math.INFINITY);
  601. var max = float2.zero;
  602. for (int i = 0; i < points.Length; ++i)
  603. {
  604. min = math.min(points[i], min);
  605. max = math.max(points[i], max);
  606. }
  607. var ext = (max - min);
  608. var mid = ext * 0.5f;
  609. var kNonRect = 0.0001f;
  610. // Construct a simple convex hull rect!.
  611. pgPointCount = resetTopology ? 0 : pgPointCount;
  612. var pc = pgPointCount;
  613. pgPoints[pgPointCount++] = new float2(min.x, min.y); pgPoints[pgPointCount++] = new float2(min.x - kNonRect, min.y + mid.y); pgPoints[pgPointCount++] = new float2(min.x, max.y); pgPoints[pgPointCount++] = new float2(min.x + mid.x, max.y + kNonRect);
  614. pgPoints[pgPointCount++] = new float2(max.x, max.y); pgPoints[pgPointCount++] = new float2(max.x + kNonRect, min.y + mid.y); pgPoints[pgPointCount++] = new float2(max.x, min.y); pgPoints[pgPointCount++] = new float2(min.x + mid.x, min.y - kNonRect);
  615. pgEdgeCount = 8;
  616. pgEdges[0] = new int2(pc + 0, pc + 1); pgEdges[1] = new int2(pc + 1, pc + 2); pgEdges[2] = new int2(pc + 2, pc + 3); pgEdges[3] = new int2(pc + 3, pc + 4);
  617. pgEdges[4] = new int2(pc + 4, pc + 5); pgEdges[5] = new int2(pc + 5, pc + 6); pgEdges[6] = new int2(pc + 6, pc + 7); pgEdges[7] = new int2(pc + 7, pc + 0);
  618. }
  619. // Reorder vertices.
  620. static void Reorder(int startVertexCount, int index, ref NativeArray<int> indices, ref int indexCount, ref NativeArray<float2> vertices, ref int vertexCount)
  621. {
  622. var found = false;
  623. for (var i = 0; i < indexCount; ++i)
  624. {
  625. if (indices[i] != index) continue;
  626. found = true;
  627. break;
  628. }
  629. if (!found)
  630. {
  631. vertexCount--;
  632. vertices[index] = vertices[vertexCount];
  633. for (var i = 0; i < indexCount; ++i)
  634. if (indices[i] == vertexCount)
  635. indices[i] = index;
  636. }
  637. }
  638. // Perform Sanitization.
  639. internal static void VertexCleanupConditioner(int startVertexCount, ref NativeArray<int> indices, ref int indexCount, ref NativeArray<float2> vertices, ref int vertexCount)
  640. {
  641. for (int i = startVertexCount; i < vertexCount; ++i)
  642. {
  643. Reorder(startVertexCount,i, ref indices, ref indexCount, ref vertices, ref vertexCount);
  644. }
  645. }
  646. public static float4 ConvexQuad(Allocator allocator, NativeArray<float2> points, NativeArray<int2> edges, ref NativeArray<float2> outVertices, ref int outVertexCount, ref NativeArray<int> outIndices, ref int outIndexCount, ref NativeArray<int2> outEdges, ref int outEdgeCount)
  647. {
  648. // Inputs are garbage, just early out.
  649. float4 ret = float4.zero;
  650. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  651. if (points.Length < 3 || points.Length >= kMaxVertexCount)
  652. return ret;
  653. // Ensure inputs form a proper PlanarGraph.
  654. int pgEdgeCount = 0, pgPointCount = 0;
  655. NativeArray<int2> pgEdges = new NativeArray<int2>(kMaxEdgeCount, allocator);
  656. NativeArray<float2> pgPoints = new NativeArray<float2>(kMaxVertexCount, allocator);
  657. // Valid Edges and Paths, correct the Planar Graph. If invalid create a simple convex hull rect.
  658. GraphConditioner(points, ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount, true);
  659. Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref outVertices, ref outVertexCount, ref outIndices, ref outIndexCount);
  660. // Dispose Temp Memory.
  661. pgPoints.Dispose();
  662. pgEdges.Dispose();
  663. return ret;
  664. }
  665. public static float4 Tessellate(Allocator allocator, in NativeArray<float2> points, in NativeArray<int2> edges, ref NativeArray<float2> outVertices, out int outVertexCount, ref NativeArray<int> outIndices, out int outIndexCount, ref NativeArray<int2> outEdges, out int outEdgeCount, bool runPlanarGraph)
  666. {
  667. // Inputs are garbage, just early out.
  668. float4 ret = float4.zero;
  669. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  670. if (points.Length < 3 || points.Length >= kMaxVertexCount)
  671. return ret;
  672. // Ensure inputs form a proper PlanarGraph.
  673. bool validGraph = false, handleEdgeCase = false;
  674. int pgEdgeCount = 0, pgPointCount = 0;
  675. NativeArray<int2> pgEdges = new NativeArray<int2>(edges.Length * 8, allocator);
  676. NativeArray<float2> pgPoints = new NativeArray<float2>(points.Length * 4, allocator);
  677. // Valid Edges and Paths, correct the Planar Graph. If invalid create a simple convex hull rect.
  678. if (runPlanarGraph)
  679. {
  680. if (0 != edges.Length)
  681. {
  682. validGraph = PlanarGraph.Validate(allocator, in points, points.Length, in edges, edges.Length, ref pgPoints, out pgPointCount, ref pgEdges, out pgEdgeCount);
  683. }
  684. }
  685. else
  686. {
  687. // Just copy Stage.
  688. pgEdgeCount = edges.Length;
  689. pgPointCount = points.Length;
  690. Copy(edges, pgEdges, pgEdgeCount);
  691. Copy(points, pgPoints, pgPointCount);
  692. }
  693. // Fallbacks are now handled by the Higher level packages. Enable if UTess needs to handle it.
  694. // #if UTESS_QUAD_FALLBACK
  695. // if (!validGraph)
  696. // {
  697. // pgPointCount = 0;
  698. // handleEdgeCase = true;
  699. // ModuleHandle.Copy(points, pgPoints, points.Length);
  700. // GraphConditioner(points, ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount, false);
  701. // }
  702. // #else
  703. // If its not a valid Graph simply return back input Data without triangulation instead of going through UTess (pointless wasted cpu cycles).
  704. if (!validGraph)
  705. {
  706. outEdgeCount = edges.Length;
  707. outVertexCount = points.Length;
  708. ModuleHandle.Copy(edges, outEdges, edges.Length);
  709. ModuleHandle.Copy(points, outVertices, points.Length);
  710. }
  711. // Do a proper Delaunay Triangulation if Inputs are valid.
  712. if (pgPointCount > 2 && pgEdgeCount > 2)
  713. {
  714. // Tessellate does not add new points, only PG and SD does. Assuming each point creates a degenerate triangle, * 4 is more than enough.
  715. NativeArray<int> tsIndices = new NativeArray<int>(pgPointCount * 8, allocator);
  716. NativeArray<float2> tsVertices = new NativeArray<float2>(pgPointCount * 4, allocator);
  717. int tsIndexCount = 0, tsVertexCount = 0;
  718. validGraph = Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref tsVertices, ref tsVertexCount, ref tsIndices, ref tsIndexCount);
  719. if (validGraph)
  720. {
  721. // Copy Out
  722. TransferOutput(pgEdges, pgEdgeCount, ref outEdges, ref outEdgeCount, tsIndices, tsIndexCount, ref outIndices, ref outIndexCount, tsVertices, tsVertexCount, ref outVertices, ref outVertexCount);
  723. if (handleEdgeCase == true)
  724. outEdgeCount = 0;
  725. }
  726. tsVertices.Dispose();
  727. tsIndices.Dispose();
  728. }
  729. // Dispose Temp Memory.
  730. pgPoints.Dispose();
  731. pgEdges.Dispose();
  732. return ret;
  733. }
  734. public static float4 Subdivide(Allocator allocator, NativeArray<float2> points, NativeArray<int2> edges, ref NativeArray<float2> outVertices, ref int outVertexCount, ref NativeArray<int> outIndices, ref int outIndexCount, ref NativeArray<int2> outEdges, ref int outEdgeCount, float areaFactor, float targetArea, int refineIterations, int smoothenIterations)
  735. {
  736. // Inputs are garbage, just early out.
  737. float4 ret = float4.zero;
  738. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  739. if (points.Length < 3 || points.Length >= kMaxVertexCount || 0 == edges.Length)
  740. return ret;
  741. // Do a proper Delaunay Triangulation.
  742. int tsIndexCount = 0, tsVertexCount = 0;
  743. NativeArray<int> tsIndices = new NativeArray<int>(kMaxIndexCount, allocator);
  744. NativeArray<float2> tsVertices = new NativeArray<float2>(kMaxVertexCount, allocator);
  745. var validGraph = Tessellator.Tessellate(allocator, points, points.Length, edges, edges.Length, ref tsVertices, ref tsVertexCount, ref tsIndices, ref tsIndexCount);
  746. // Refinement and Smoothing.
  747. bool refined = false;
  748. bool refinementRequired = (targetArea != 0 || areaFactor != 0);
  749. if (validGraph && refinementRequired)
  750. {
  751. // Do Refinement until success.
  752. float maxArea = 0;
  753. float incArea = 0;
  754. int rfEdgeCount = 0, rfPointCount = 0, rfIndexCount = 0, rfVertexCount = 0;
  755. NativeArray<int2> rfEdges = new NativeArray<int2>(kMaxEdgeCount, allocator);
  756. NativeArray<float2> rfPoints = new NativeArray<float2>(kMaxVertexCount, allocator);
  757. NativeArray<int> rfIndices = new NativeArray<int>(kMaxIndexCount, allocator);
  758. NativeArray<float2> rfVertices = new NativeArray<float2>(kMaxVertexCount, allocator);
  759. ret.x = 0;
  760. refineIterations = Math.Min(refineIterations, kMaxRefineIterations);
  761. if (targetArea != 0)
  762. {
  763. // Increment for Iterations.
  764. incArea = (targetArea / 10);
  765. while (targetArea < kMaxArea && refineIterations > 0)
  766. {
  767. // Do Mesh Refinement.
  768. CopyGraph(points, points.Length, ref rfPoints, ref rfPointCount, edges, edges.Length, ref rfEdges, ref rfEdgeCount);
  769. CopyGeometry(tsIndices, tsIndexCount, ref rfIndices, ref rfIndexCount, tsVertices, tsVertexCount, ref rfVertices, ref rfVertexCount);
  770. refined = Refinery.Condition(allocator, areaFactor, targetArea, ref rfPoints, ref rfPointCount, ref rfEdges, ref rfEdgeCount, ref rfVertices, ref rfVertexCount, ref rfIndices, ref rfIndexCount, ref maxArea);
  771. if (refined && rfIndexCount > rfPointCount)
  772. {
  773. // Copy Out
  774. ret.x = areaFactor;
  775. TransferOutput(rfEdges, rfEdgeCount, ref outEdges, ref outEdgeCount, rfIndices, rfIndexCount, ref outIndices, ref outIndexCount, rfVertices, rfVertexCount, ref outVertices, ref outVertexCount);
  776. break;
  777. }
  778. refined = false;
  779. targetArea = targetArea + incArea;
  780. refineIterations--;
  781. }
  782. }
  783. else if (areaFactor != 0)
  784. {
  785. // Increment for Iterations.
  786. areaFactor = math.lerp(0.1f, 0.54f, (areaFactor - 0.05f) / 0.45f); // Specific to Animation.
  787. incArea = (areaFactor / 10);
  788. while (areaFactor < 0.8f && refineIterations > 0)
  789. {
  790. // Do Mesh Refinement.
  791. CopyGraph(points, points.Length, ref rfPoints, ref rfPointCount, edges, edges.Length, ref rfEdges, ref rfEdgeCount);
  792. CopyGeometry(tsIndices, tsIndexCount, ref rfIndices, ref rfIndexCount, tsVertices, tsVertexCount, ref rfVertices, ref rfVertexCount);
  793. refined = Refinery.Condition(allocator, areaFactor, targetArea, ref rfPoints, ref rfPointCount, ref rfEdges, ref rfEdgeCount, ref rfVertices, ref rfVertexCount, ref rfIndices, ref rfIndexCount, ref maxArea);
  794. if (refined && rfIndexCount > rfPointCount)
  795. {
  796. // Copy Out
  797. ret.x = areaFactor;
  798. TransferOutput(rfEdges, rfEdgeCount, ref outEdges, ref outEdgeCount, rfIndices, rfIndexCount, ref outIndices, ref outIndexCount, rfVertices, rfVertexCount, ref outVertices, ref outVertexCount);
  799. break;
  800. }
  801. refined = false;
  802. areaFactor = areaFactor + incArea;
  803. refineIterations--;
  804. }
  805. }
  806. if (refined)
  807. {
  808. // Sanitize generated geometry data.
  809. var preSmoothen = outVertexCount;
  810. if (ret.x != 0)
  811. VertexCleanupConditioner(tsVertexCount, ref rfIndices, ref rfIndexCount, ref rfVertices, ref rfVertexCount);
  812. // Smoothen. At this point only vertex relocation is allowed, not vertex addition/removal.
  813. // Note: Only refined mesh contains Steiner points and we only smoothen these points.
  814. ret.y = 0;
  815. smoothenIterations = math.clamp(smoothenIterations, 0, kMaxSmoothenIterations);
  816. while (smoothenIterations > 0)
  817. {
  818. var smoothen = Smoothen.Condition(allocator, ref rfPoints, rfPointCount, rfEdges, rfEdgeCount, ref rfVertices, ref rfVertexCount, ref rfIndices, ref rfIndexCount);
  819. if (!smoothen)
  820. break;
  821. // Copy Out
  822. ret.y = (float)(smoothenIterations);
  823. TransferOutput(rfEdges, rfEdgeCount, ref outEdges, ref outEdgeCount, rfIndices, rfIndexCount, ref outIndices, ref outIndexCount, rfVertices, rfVertexCount, ref outVertices, ref outVertexCount);
  824. smoothenIterations--;
  825. }
  826. // Sanitize generated geometry data.
  827. var postSmoothen = outVertexCount;
  828. if (ret.y != 0)
  829. VertexCleanupConditioner(tsVertexCount, ref outIndices, ref outIndexCount, ref outVertices, ref outVertexCount);
  830. }
  831. rfVertices.Dispose();
  832. rfIndices.Dispose();
  833. rfPoints.Dispose();
  834. rfEdges.Dispose();
  835. }
  836. // Refinement failed but Graph succeeded.
  837. if (validGraph && !refined)
  838. {
  839. // Copy Out
  840. TransferOutput(edges, edges.Length, ref outEdges, ref outEdgeCount, tsIndices, tsIndexCount, ref outIndices, ref outIndexCount, tsVertices, tsVertexCount, ref outVertices, ref outVertexCount);
  841. }
  842. // Dispose Temp Memory.
  843. tsVertices.Dispose();
  844. tsIndices.Dispose();
  845. return ret;
  846. }
  847. }
  848. }