No Description
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 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. // From https://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c and is public domain. Can't find one within Unity.
  384. internal static float OrientFast(float2 a, float2 b, float2 c)
  385. {
  386. float epsilon = 1.1102230246251565e-16f;
  387. float errbound3 = (3.0f + 16.0f * epsilon) * epsilon;
  388. float l = (a.y - c.y) * (b.x - c.x);
  389. float r = (a.x - c.x) * (b.y - c.y);
  390. float det = l - r;
  391. float s = 0;
  392. if (l > 0)
  393. {
  394. if (r <= 0)
  395. {
  396. return det;
  397. }
  398. else
  399. {
  400. s = l + r;
  401. }
  402. }
  403. else if (l < 0)
  404. {
  405. if (r >= 0)
  406. {
  407. return det;
  408. }
  409. else
  410. {
  411. s = -(l + r);
  412. }
  413. }
  414. else
  415. {
  416. return det;
  417. }
  418. float tol = errbound3 * s;
  419. if (det >= tol || det <= -tol)
  420. {
  421. return det;
  422. }
  423. return epsilon;
  424. }
  425. // This is needed when doing PlanarGraph as it requires high precision separation of points.
  426. internal static double OrientFastDouble(double2 a, double2 b, double2 c)
  427. {
  428. double epsilon = 1.1102230246251565e-16f;
  429. double errbound3 = (3.0 + 16.0 * epsilon) * epsilon;
  430. double l = (a.y - c.y) * (b.x - c.x);
  431. double r = (a.x - c.x) * (b.y - c.y);
  432. double det = l - r;
  433. double s = 0;
  434. if (l > 0)
  435. {
  436. if (r <= 0)
  437. {
  438. return det;
  439. }
  440. else
  441. {
  442. s = l + r;
  443. }
  444. }
  445. else if (l < 0)
  446. {
  447. if (r >= 0)
  448. {
  449. return det;
  450. }
  451. else
  452. {
  453. s = -(l + r);
  454. }
  455. }
  456. else
  457. {
  458. return det;
  459. }
  460. double tol = errbound3 * s;
  461. if (det >= tol || det <= -tol)
  462. {
  463. return det;
  464. }
  465. return epsilon;
  466. }
  467. internal static UCircle CircumCircle(UTriangle tri)
  468. {
  469. float xa = tri.va.x * tri.va.x;
  470. float xb = tri.vb.x * tri.vb.x;
  471. float xc = tri.vc.x * tri.vc.x;
  472. float ya = tri.va.y * tri.va.y;
  473. float yb = tri.vb.y * tri.vb.y;
  474. float yc = tri.vc.y * tri.vc.y;
  475. 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));
  476. float x = ((tri.vc.y - tri.va.y) * (xb - xa + yb - ya) + (tri.va.y - tri.vb.y) * (xc - xa + yc - ya)) / c;
  477. float y = ((tri.va.x - tri.vc.x) * (xb - xa + yb - ya) + (tri.vb.x - tri.va.x) * (xc - xa + yc - ya)) / c;
  478. float vx = (tri.va.x - x);
  479. float vy = (tri.va.y - y);
  480. return new UCircle { center = new float2(x, y), radius = math.sqrt((vx * vx) + (vy * vy)) };
  481. }
  482. internal static bool IsInsideCircle(UCircle c, float2 v)
  483. {
  484. return math.distance(v, c.center) < c.radius;
  485. }
  486. internal static float TriangleArea(float2 va, float2 vb, float2 vc)
  487. {
  488. float3 a = new float3(va.x, va.y, 0);
  489. float3 b = new float3(vb.x, vb.y, 0);
  490. float3 c = new float3(vc.x, vc.y, 0);
  491. float3 v = math.cross(a - b, a - c);
  492. return math.abs(v.z) * 0.5f;
  493. }
  494. internal static float Sign(float2 p1, float2 p2, float2 p3)
  495. {
  496. return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
  497. }
  498. internal static bool IsInsideTriangle(float2 pt, float2 v1, float2 v2, float2 v3)
  499. {
  500. float d1, d2, d3;
  501. bool has_neg, has_pos;
  502. d1 = Sign(pt, v1, v2);
  503. d2 = Sign(pt, v2, v3);
  504. d3 = Sign(pt, v3, v1);
  505. has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
  506. has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
  507. return !(has_neg && has_pos);
  508. }
  509. internal static bool IsInsideTriangleApproximate(float2 pt, float2 v1, float2 v2, float2 v3)
  510. {
  511. float d0, d1, d2, d3;
  512. d0 = TriangleArea(v1, v2, v3);
  513. d1 = TriangleArea(pt, v1, v2);
  514. d2 = TriangleArea(pt, v2, v3);
  515. d3 = TriangleArea(pt, v3, v1);
  516. float epsilon = 1.1102230246251565e-16f;
  517. return Mathf.Abs(d0 - (d1 + d2 + d3)) < epsilon;
  518. }
  519. internal static bool IsInsideCircle(float2 a, float2 b, float2 c, float2 p)
  520. {
  521. float ab = math.dot(a, a);
  522. float cd = math.dot(b, b);
  523. float ef = math.dot(c, c);
  524. float ax = a.x;
  525. float ay = a.y;
  526. float bx = b.x;
  527. float by = b.y;
  528. float cx = c.x;
  529. float cy = c.y;
  530. float circum_x = (ab * (cy - by) + cd * (ay - cy) + ef * (by - ay)) /
  531. (ax * (cy - by) + bx * (ay - cy) + cx * (by - ay));
  532. float circum_y = (ab * (cx - bx) + cd * (ax - cx) + ef * (bx - ax)) /
  533. (ay * (cx - bx) + by * (ax - cx) + cy * (bx - ax));
  534. float2 circum = new float2();
  535. circum.x = circum_x / 2;
  536. circum.y = circum_y / 2;
  537. float circum_radius = math.distance(a, circum);
  538. float dist = math.distance(p, circum);
  539. return circum_radius - dist > 0.00001f;
  540. }
  541. 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)
  542. {
  543. // Check if there are invalid triangles or segments.
  544. for (int i = 0; i < indexCount; i += 3)
  545. {
  546. UTriangle tri = new UTriangle();
  547. var i0 = indices[i + 0];
  548. var i1 = indices[i + 1];
  549. var i2 = indices[i + 2];
  550. tri.va = vertices[i0];
  551. tri.vb = vertices[i1];
  552. tri.vc = vertices[i2];
  553. tri.c = CircumCircle(tri);
  554. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  555. maxArea = math.max(tri.area, maxArea);
  556. minArea = math.min(tri.area, minArea);
  557. avgArea = avgArea + tri.area;
  558. triangles[triangleCount++] = tri;
  559. }
  560. avgArea = avgArea / triangleCount;
  561. }
  562. 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)
  563. {
  564. // Check if there are invalid triangles or segments.
  565. for (int i = 0; i < indexCount; i += 3)
  566. {
  567. UTriangle tri = new UTriangle();
  568. var i0 = indices[i + 0];
  569. var i1 = indices[i + 1];
  570. var i2 = indices[i + 2];
  571. tri.va = vertices[i0];
  572. tri.vb = vertices[i1];
  573. tri.vc = vertices[i2];
  574. tri.c = CircumCircle(tri);
  575. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  576. maxArea = math.max(tri.area, maxArea);
  577. minArea = math.min(tri.area, minArea);
  578. avgArea = avgArea + tri.area;
  579. triangles[triangleCount++] = tri;
  580. }
  581. avgArea = avgArea / triangleCount;
  582. }
  583. 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)
  584. {
  585. // Check if there are invalid triangles or segments.
  586. for (int i = 0; i < indexCount; i += 3)
  587. {
  588. UTriangle tri = new UTriangle();
  589. var i0 = indices[i + 0];
  590. var i1 = indices[i + 1];
  591. var i2 = indices[i + 2];
  592. tri.va = vertices[i0];
  593. tri.vb = vertices[i1];
  594. tri.vc = vertices[i2];
  595. tri.c = CircumCircle(tri);
  596. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  597. maxArea = math.max(tri.area, maxArea);
  598. minArea = math.min(tri.area, minArea);
  599. avgArea = avgArea + tri.area;
  600. var e1 = math.distance(tri.va, tri.vb);
  601. var e2 = math.distance(tri.vb, tri.vc);
  602. var e3 = math.distance(tri.vc, tri.va);
  603. maxEdge = math.max(e1, maxEdge);
  604. maxEdge = math.max(e2, maxEdge);
  605. maxEdge = math.max(e3, maxEdge);
  606. minEdge = math.min(e1, minEdge);
  607. minEdge = math.min(e2, minEdge);
  608. minEdge = math.min(e3, minEdge);
  609. avgEdge = avgEdge + e1;
  610. avgEdge = avgEdge + e2;
  611. avgEdge = avgEdge + e3;
  612. triangles[triangleCount++] = tri;
  613. }
  614. avgArea = avgArea / triangleCount;
  615. avgEdge = avgEdge / indexCount;
  616. }
  617. 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)
  618. {
  619. // Check if there are invalid triangles or segments.
  620. for (int i = 0; i < indexCount; i += 3)
  621. {
  622. UTriangle tri = new UTriangle();
  623. var i0 = indices[i + 0];
  624. var i1 = indices[i + 1];
  625. var i2 = indices[i + 2];
  626. tri.va = vertices[i0];
  627. tri.vb = vertices[i1];
  628. tri.vc = vertices[i2];
  629. tri.c = CircumCircle(tri);
  630. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  631. maxArea = math.max(tri.area, maxArea);
  632. minArea = math.min(tri.area, minArea);
  633. avgArea = avgArea + tri.area;
  634. tri.indices = new int3(i0, i1, i2);
  635. // Outputs.
  636. delaEdges[delaEdgeCount++] = new int4(math.min(i0, i1), math.max(i0, i1), triangleCount, -1);
  637. delaEdges[delaEdgeCount++] = new int4(math.min(i1, i2), math.max(i1, i2), triangleCount, -1);
  638. delaEdges[delaEdgeCount++] = new int4(math.min(i2, i0), math.max(i2, i0), triangleCount, -1);
  639. triangles[triangleCount++] = tri;
  640. }
  641. avgArea = avgArea / triangleCount;
  642. }
  643. 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)
  644. {
  645. dstEdgeCount = srcEdgeCount;
  646. dstPointCount = srcPointCount;
  647. Copy(srcEdges, dstEdges, srcEdgeCount);
  648. Copy(srcPoints, dstPoints, srcPointCount);
  649. }
  650. 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)
  651. {
  652. dstIndexCount = srcIndexCount;
  653. dstVertexCount = srcVertexCount;
  654. Copy(srcIndices, dstIndices, srcIndexCount);
  655. Copy(srcVertices, dstVertices, srcVertexCount);
  656. }
  657. 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)
  658. {
  659. dstEdgeCount = srcEdgeCount;
  660. dstIndexCount = srcIndexCount;
  661. dstVertexCount = srcVertexCount;
  662. Copy(srcEdges, dstEdges, srcEdgeCount);
  663. Copy(srcIndices, dstIndices, srcIndexCount);
  664. Copy(srcVertices, dstVertices, srcVertexCount);
  665. }
  666. static void GraphConditioner(NativeArray<float2> points, ref NativeArray<float2> pgPoints, ref int pgPointCount, ref NativeArray<int2> pgEdges, ref int pgEdgeCount, bool resetTopology)
  667. {
  668. var min = new float2(math.INFINITY, math.INFINITY);
  669. var max = float2.zero;
  670. for (int i = 0; i < points.Length; ++i)
  671. {
  672. min = math.min(points[i], min);
  673. max = math.max(points[i], max);
  674. }
  675. var ext = (max - min);
  676. var mid = ext * 0.5f;
  677. var kNonRect = 0.0001f;
  678. // Construct a simple convex hull rect!.
  679. pgPointCount = resetTopology ? 0 : pgPointCount;
  680. var pc = pgPointCount;
  681. 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);
  682. 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);
  683. pgEdgeCount = 8;
  684. 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);
  685. 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);
  686. }
  687. // Reorder vertices.
  688. static void Reorder(int startVertexCount, int index, ref NativeArray<int> indices, ref int indexCount, ref NativeArray<float2> vertices, ref int vertexCount)
  689. {
  690. var found = false;
  691. for (var i = 0; i < indexCount; ++i)
  692. {
  693. if (indices[i] != index) continue;
  694. found = true;
  695. break;
  696. }
  697. if (!found)
  698. {
  699. vertexCount--;
  700. vertices[index] = vertices[vertexCount];
  701. for (var i = 0; i < indexCount; ++i)
  702. if (indices[i] == vertexCount)
  703. indices[i] = index;
  704. }
  705. }
  706. // Perform Sanitization.
  707. internal static void VertexCleanupConditioner(int startVertexCount, ref NativeArray<int> indices, ref int indexCount, ref NativeArray<float2> vertices, ref int vertexCount)
  708. {
  709. for (int i = startVertexCount; i < vertexCount; ++i)
  710. {
  711. Reorder(startVertexCount,i, ref indices, ref indexCount, ref vertices, ref vertexCount);
  712. }
  713. }
  714. 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)
  715. {
  716. // Inputs are garbage, just early out.
  717. float4 ret = float4.zero;
  718. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  719. if (points.Length < 3 || points.Length >= kMaxVertexCount)
  720. return ret;
  721. // Ensure inputs form a proper PlanarGraph.
  722. int pgEdgeCount = 0, pgPointCount = 0;
  723. NativeArray<int2> pgEdges = new NativeArray<int2>(kMaxEdgeCount, allocator);
  724. NativeArray<float2> pgPoints = new NativeArray<float2>(kMaxVertexCount, allocator);
  725. // Valid Edges and Paths, correct the Planar Graph. If invalid create a simple convex hull rect.
  726. GraphConditioner(points, ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount, true);
  727. Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref outVertices, ref outVertexCount, ref outIndices, ref outIndexCount);
  728. // Dispose Temp Memory.
  729. pgPoints.Dispose();
  730. pgEdges.Dispose();
  731. return ret;
  732. }
  733. 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)
  734. {
  735. // Inputs are garbage, just early out.
  736. float4 ret = float4.zero;
  737. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  738. if (points.Length < 3 || points.Length >= kMaxVertexCount)
  739. return ret;
  740. // Ensure inputs form a proper PlanarGraph.
  741. bool validGraph = false, handleEdgeCase = false;
  742. int pgEdgeCount = 0, pgPointCount = 0;
  743. NativeArray<int2> pgEdges = new NativeArray<int2>(edges.Length * 8, allocator);
  744. NativeArray<float2> pgPoints = new NativeArray<float2>(points.Length * 4, allocator);
  745. // Valid Edges and Paths, correct the Planar Graph. If invalid create a simple convex hull rect.
  746. if (0 != edges.Length)
  747. {
  748. validGraph = PlanarGraph.Validate(allocator, in points, points.Length, in edges, edges.Length, ref pgPoints,out pgPointCount, ref pgEdges, out pgEdgeCount);
  749. }
  750. // Fallbacks are now handled by the Higher level packages. Enable if UTess needs to handle it.
  751. // #if UTESS_QUAD_FALLBACK
  752. // if (!validGraph)
  753. // {
  754. // pgPointCount = 0;
  755. // handleEdgeCase = true;
  756. // ModuleHandle.Copy(points, pgPoints, points.Length);
  757. // GraphConditioner(points, ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount, false);
  758. // }
  759. // #else
  760. // If its not a valid Graph simply return back input Data without triangulation instead of going through UTess (pointless wasted cpu cycles).
  761. if (!validGraph)
  762. {
  763. outEdgeCount = edges.Length;
  764. outVertexCount = points.Length;
  765. ModuleHandle.Copy(edges, outEdges, edges.Length);
  766. ModuleHandle.Copy(points, outVertices, points.Length);
  767. }
  768. // Do a proper Delaunay Triangulation if Inputs are valid.
  769. if (pgPointCount > 2 && pgEdgeCount > 2)
  770. {
  771. // Tessellate does not add new points, only PG and SD does. Assuming each point creates a degenerate triangle, * 4 is more than enough.
  772. NativeArray<int> tsIndices = new NativeArray<int>(pgPointCount * 8, allocator);
  773. NativeArray<float2> tsVertices = new NativeArray<float2>(pgPointCount * 4, allocator);
  774. int tsIndexCount = 0, tsVertexCount = 0;
  775. validGraph = Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref tsVertices, ref tsVertexCount, ref tsIndices, ref tsIndexCount);
  776. if (validGraph)
  777. {
  778. // Copy Out
  779. TransferOutput(pgEdges, pgEdgeCount, ref outEdges, ref outEdgeCount, tsIndices, tsIndexCount, ref outIndices, ref outIndexCount, tsVertices, tsVertexCount, ref outVertices, ref outVertexCount);
  780. if (handleEdgeCase == true)
  781. outEdgeCount = 0;
  782. }
  783. tsVertices.Dispose();
  784. tsIndices.Dispose();
  785. }
  786. // Dispose Temp Memory.
  787. pgPoints.Dispose();
  788. pgEdges.Dispose();
  789. return ret;
  790. }
  791. 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)
  792. {
  793. // Inputs are garbage, just early out.
  794. float4 ret = float4.zero;
  795. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  796. if (points.Length < 3 || points.Length >= kMaxVertexCount || 0 == edges.Length)
  797. return ret;
  798. // Do a proper Delaunay Triangulation.
  799. int tsIndexCount = 0, tsVertexCount = 0;
  800. NativeArray<int> tsIndices = new NativeArray<int>(kMaxIndexCount, allocator);
  801. NativeArray<float2> tsVertices = new NativeArray<float2>(kMaxVertexCount, allocator);
  802. var validGraph = Tessellator.Tessellate(allocator, points, points.Length, edges, edges.Length, ref tsVertices, ref tsVertexCount, ref tsIndices, ref tsIndexCount);
  803. // Refinement and Smoothing.
  804. bool refined = false;
  805. bool refinementRequired = (targetArea != 0 || areaFactor != 0);
  806. if (validGraph && refinementRequired)
  807. {
  808. // Do Refinement until success.
  809. float maxArea = 0;
  810. float incArea = 0;
  811. int rfEdgeCount = 0, rfPointCount = 0, rfIndexCount = 0, rfVertexCount = 0;
  812. NativeArray<int2> rfEdges = new NativeArray<int2>(kMaxEdgeCount, allocator);
  813. NativeArray<float2> rfPoints = new NativeArray<float2>(kMaxVertexCount, allocator);
  814. NativeArray<int> rfIndices = new NativeArray<int>(kMaxIndexCount, allocator);
  815. NativeArray<float2> rfVertices = new NativeArray<float2>(kMaxVertexCount, allocator);
  816. ret.x = 0;
  817. refineIterations = Math.Min(refineIterations, kMaxRefineIterations);
  818. if (targetArea != 0)
  819. {
  820. // Increment for Iterations.
  821. incArea = (targetArea / 10);
  822. while (targetArea < kMaxArea && refineIterations > 0)
  823. {
  824. // Do Mesh Refinement.
  825. CopyGraph(points, points.Length, ref rfPoints, ref rfPointCount, edges, edges.Length, ref rfEdges, ref rfEdgeCount);
  826. CopyGeometry(tsIndices, tsIndexCount, ref rfIndices, ref rfIndexCount, tsVertices, tsVertexCount, ref rfVertices, ref rfVertexCount);
  827. refined = Refinery.Condition(allocator, areaFactor, targetArea, ref rfPoints, ref rfPointCount, ref rfEdges, ref rfEdgeCount, ref rfVertices, ref rfVertexCount, ref rfIndices, ref rfIndexCount, ref maxArea);
  828. if (refined && rfIndexCount > rfPointCount)
  829. {
  830. // Copy Out
  831. ret.x = areaFactor;
  832. TransferOutput(rfEdges, rfEdgeCount, ref outEdges, ref outEdgeCount, rfIndices, rfIndexCount, ref outIndices, ref outIndexCount, rfVertices, rfVertexCount, ref outVertices, ref outVertexCount);
  833. break;
  834. }
  835. refined = false;
  836. targetArea = targetArea + incArea;
  837. refineIterations--;
  838. }
  839. }
  840. else if (areaFactor != 0)
  841. {
  842. // Increment for Iterations.
  843. areaFactor = math.lerp(0.1f, 0.54f, (areaFactor - 0.05f) / 0.45f); // Specific to Animation.
  844. incArea = (areaFactor / 10);
  845. while (areaFactor < 0.8f && refineIterations > 0)
  846. {
  847. // Do Mesh Refinement.
  848. CopyGraph(points, points.Length, ref rfPoints, ref rfPointCount, edges, edges.Length, ref rfEdges, ref rfEdgeCount);
  849. CopyGeometry(tsIndices, tsIndexCount, ref rfIndices, ref rfIndexCount, tsVertices, tsVertexCount, ref rfVertices, ref rfVertexCount);
  850. refined = Refinery.Condition(allocator, areaFactor, targetArea, ref rfPoints, ref rfPointCount, ref rfEdges, ref rfEdgeCount, ref rfVertices, ref rfVertexCount, ref rfIndices, ref rfIndexCount, ref maxArea);
  851. if (refined && rfIndexCount > rfPointCount)
  852. {
  853. // Copy Out
  854. ret.x = areaFactor;
  855. TransferOutput(rfEdges, rfEdgeCount, ref outEdges, ref outEdgeCount, rfIndices, rfIndexCount, ref outIndices, ref outIndexCount, rfVertices, rfVertexCount, ref outVertices, ref outVertexCount);
  856. break;
  857. }
  858. refined = false;
  859. areaFactor = areaFactor + incArea;
  860. refineIterations--;
  861. }
  862. }
  863. if (refined)
  864. {
  865. // Sanitize generated geometry data.
  866. var preSmoothen = outVertexCount;
  867. if (ret.x != 0)
  868. VertexCleanupConditioner(tsVertexCount, ref rfIndices, ref rfIndexCount, ref rfVertices, ref rfVertexCount);
  869. // Smoothen. At this point only vertex relocation is allowed, not vertex addition/removal.
  870. // Note: Only refined mesh contains Steiner points and we only smoothen these points.
  871. ret.y = 0;
  872. smoothenIterations = math.clamp(smoothenIterations, 0, kMaxSmoothenIterations);
  873. while (smoothenIterations > 0)
  874. {
  875. var smoothen = Smoothen.Condition(allocator, ref rfPoints, rfPointCount, rfEdges, rfEdgeCount, ref rfVertices, ref rfVertexCount, ref rfIndices, ref rfIndexCount);
  876. if (!smoothen)
  877. break;
  878. // Copy Out
  879. ret.y = (float)(smoothenIterations);
  880. TransferOutput(rfEdges, rfEdgeCount, ref outEdges, ref outEdgeCount, rfIndices, rfIndexCount, ref outIndices, ref outIndexCount, rfVertices, rfVertexCount, ref outVertices, ref outVertexCount);
  881. smoothenIterations--;
  882. }
  883. // Sanitize generated geometry data.
  884. var postSmoothen = outVertexCount;
  885. if (ret.y != 0)
  886. VertexCleanupConditioner(tsVertexCount, ref outIndices, ref outIndexCount, ref outVertices, ref outVertexCount);
  887. }
  888. rfVertices.Dispose();
  889. rfIndices.Dispose();
  890. rfPoints.Dispose();
  891. rfEdges.Dispose();
  892. }
  893. // Refinement failed but Graph succeeded.
  894. if (validGraph && !refined)
  895. {
  896. // Copy Out
  897. TransferOutput(edges, edges.Length, ref outEdges, ref outEdgeCount, tsIndices, tsIndexCount, ref outIndices, ref outIndexCount, tsVertices, tsVertexCount, ref outVertices, ref outVertexCount);
  898. }
  899. // Dispose Temp Memory.
  900. tsVertices.Dispose();
  901. tsIndices.Dispose();
  902. return ret;
  903. }
  904. }
  905. }