暫無描述
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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.Rendering.Universal.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 NativeArray<double2> points;
  77. public NativeArray<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>(NativeArray<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. // Simple Orientation test.
  358. internal static float OrientFast(float2 a, float2 b, float2 c)
  359. {
  360. float epsilon = 1.1102230246251565e-16f;
  361. float det = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
  362. if (math.abs(det) < epsilon) return 0;
  363. return det;
  364. }
  365. // This is needed when doing PlanarGraph as it requires high precision separation of points.
  366. internal static double OrientFastDouble(double2 a, double2 b, double2 c)
  367. {
  368. double epsilon = 1.1102230246251565e-16f;
  369. double det = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
  370. if (math.abs(det) < epsilon) return 0;
  371. return det;
  372. }
  373. internal static UCircle CircumCircle(UTriangle tri)
  374. {
  375. float xa = tri.va.x * tri.va.x;
  376. float xb = tri.vb.x * tri.vb.x;
  377. float xc = tri.vc.x * tri.vc.x;
  378. float ya = tri.va.y * tri.va.y;
  379. float yb = tri.vb.y * tri.vb.y;
  380. float yc = tri.vc.y * tri.vc.y;
  381. 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));
  382. float x = ((tri.vc.y - tri.va.y) * (xb - xa + yb - ya) + (tri.va.y - tri.vb.y) * (xc - xa + yc - ya)) / c;
  383. float y = ((tri.va.x - tri.vc.x) * (xb - xa + yb - ya) + (tri.vb.x - tri.va.x) * (xc - xa + yc - ya)) / c;
  384. float vx = (tri.va.x - x);
  385. float vy = (tri.va.y - y);
  386. return new UCircle { center = new float2(x, y), radius = math.sqrt((vx * vx) + (vy * vy)) };
  387. }
  388. internal static bool IsInsideCircle(UCircle c, float2 v)
  389. {
  390. return math.distance(v, c.center) < c.radius;
  391. }
  392. internal static float TriangleArea(float2 va, float2 vb, float2 vc)
  393. {
  394. float3 a = new float3(va.x, va.y, 0);
  395. float3 b = new float3(vb.x, vb.y, 0);
  396. float3 c = new float3(vc.x, vc.y, 0);
  397. float3 v = math.cross(a - b, a - c);
  398. return math.abs(v.z) * 0.5f;
  399. }
  400. internal static float Sign(float2 p1, float2 p2, float2 p3)
  401. {
  402. return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
  403. }
  404. internal static bool IsInsideTriangle(float2 pt, float2 v1, float2 v2, float2 v3)
  405. {
  406. float d1, d2, d3;
  407. bool has_neg, has_pos;
  408. d1 = Sign(pt, v1, v2);
  409. d2 = Sign(pt, v2, v3);
  410. d3 = Sign(pt, v3, v1);
  411. has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
  412. has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
  413. return !(has_neg && has_pos);
  414. }
  415. internal static bool IsInsideTriangleApproximate(float2 pt, float2 v1, float2 v2, float2 v3)
  416. {
  417. float d0, d1, d2, d3;
  418. d0 = TriangleArea(v1, v2, v3);
  419. d1 = TriangleArea(pt, v1, v2);
  420. d2 = TriangleArea(pt, v2, v3);
  421. d3 = TriangleArea(pt, v3, v1);
  422. float epsilon = 1.1102230246251565e-16f;
  423. return Mathf.Abs(d0 - (d1 + d2 + d3)) < epsilon;
  424. }
  425. internal static bool IsInsideCircle(float2 a, float2 b, float2 c, float2 p)
  426. {
  427. float ab = math.dot(a, a);
  428. float cd = math.dot(b, b);
  429. float ef = math.dot(c, c);
  430. float ax = a.x;
  431. float ay = a.y;
  432. float bx = b.x;
  433. float by = b.y;
  434. float cx = c.x;
  435. float cy = c.y;
  436. float circum_x = (ab * (cy - by) + cd * (ay - cy) + ef * (by - ay)) /
  437. (ax * (cy - by) + bx * (ay - cy) + cx * (by - ay));
  438. float circum_y = (ab * (cx - bx) + cd * (ax - cx) + ef * (bx - ax)) /
  439. (ay * (cx - bx) + by * (ax - cx) + cy * (bx - ax));
  440. float2 circum = new float2();
  441. circum.x = circum_x / 2;
  442. circum.y = circum_y / 2;
  443. float circum_radius = math.distance(a, circum);
  444. float dist = math.distance(p, circum);
  445. return circum_radius - dist > 0.00001f;
  446. }
  447. 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)
  448. {
  449. // Check if there are invalid triangles or segments.
  450. for (int i = 0; i < indexCount; i += 3)
  451. {
  452. UTriangle tri = new UTriangle();
  453. var i0 = indices[i + 0];
  454. var i1 = indices[i + 1];
  455. var i2 = indices[i + 2];
  456. tri.va = vertices[i0];
  457. tri.vb = vertices[i1];
  458. tri.vc = vertices[i2];
  459. tri.c = CircumCircle(tri);
  460. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  461. maxArea = math.max(tri.area, maxArea);
  462. minArea = math.min(tri.area, minArea);
  463. avgArea = avgArea + tri.area;
  464. triangles[triangleCount++] = tri;
  465. }
  466. avgArea = avgArea / triangleCount;
  467. }
  468. 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)
  469. {
  470. // Check if there are invalid triangles or segments.
  471. for (int i = 0; i < indexCount; i += 3)
  472. {
  473. UTriangle tri = new UTriangle();
  474. var i0 = indices[i + 0];
  475. var i1 = indices[i + 1];
  476. var i2 = indices[i + 2];
  477. tri.va = vertices[i0];
  478. tri.vb = vertices[i1];
  479. tri.vc = vertices[i2];
  480. tri.c = CircumCircle(tri);
  481. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  482. maxArea = math.max(tri.area, maxArea);
  483. minArea = math.min(tri.area, minArea);
  484. avgArea = avgArea + tri.area;
  485. var e1 = math.distance(tri.va, tri.vb);
  486. var e2 = math.distance(tri.vb, tri.vc);
  487. var e3 = math.distance(tri.vc, tri.va);
  488. maxEdge = math.max(e1, maxEdge);
  489. maxEdge = math.max(e2, maxEdge);
  490. maxEdge = math.max(e3, maxEdge);
  491. minEdge = math.min(e1, minEdge);
  492. minEdge = math.min(e2, minEdge);
  493. minEdge = math.min(e3, minEdge);
  494. avgEdge = avgEdge + e1;
  495. avgEdge = avgEdge + e2;
  496. avgEdge = avgEdge + e3;
  497. triangles[triangleCount++] = tri;
  498. }
  499. avgArea = avgArea / triangleCount;
  500. avgEdge = avgEdge / indexCount;
  501. }
  502. 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)
  503. {
  504. // Check if there are invalid triangles or segments.
  505. for (int i = 0; i < indexCount; i += 3)
  506. {
  507. UTriangle tri = new UTriangle();
  508. var i0 = indices[i + 0];
  509. var i1 = indices[i + 1];
  510. var i2 = indices[i + 2];
  511. tri.va = vertices[i0];
  512. tri.vb = vertices[i1];
  513. tri.vc = vertices[i2];
  514. tri.c = CircumCircle(tri);
  515. tri.area = TriangleArea(tri.va, tri.vb, tri.vc);
  516. maxArea = math.max(tri.area, maxArea);
  517. minArea = math.min(tri.area, minArea);
  518. avgArea = avgArea + tri.area;
  519. tri.indices = new int3(i0, i1, i2);
  520. // Outputs.
  521. delaEdges[delaEdgeCount++] = new int4(math.min(i0, i1), math.max(i0, i1), triangleCount, -1);
  522. delaEdges[delaEdgeCount++] = new int4(math.min(i1, i2), math.max(i1, i2), triangleCount, -1);
  523. delaEdges[delaEdgeCount++] = new int4(math.min(i2, i0), math.max(i2, i0), triangleCount, -1);
  524. triangles[triangleCount++] = tri;
  525. }
  526. avgArea = avgArea / triangleCount;
  527. }
  528. 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)
  529. {
  530. dstEdgeCount = srcEdgeCount;
  531. dstPointCount = srcPointCount;
  532. Copy(srcEdges, dstEdges, srcEdgeCount);
  533. Copy(srcPoints, dstPoints, srcPointCount);
  534. }
  535. 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)
  536. {
  537. dstIndexCount = srcIndexCount;
  538. dstVertexCount = srcVertexCount;
  539. Copy(srcIndices, dstIndices, srcIndexCount);
  540. Copy(srcVertices, dstVertices, srcVertexCount);
  541. }
  542. 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)
  543. {
  544. dstEdgeCount = srcEdgeCount;
  545. dstIndexCount = srcIndexCount;
  546. dstVertexCount = srcVertexCount;
  547. Copy(srcEdges, dstEdges, srcEdgeCount);
  548. Copy(srcIndices, dstIndices, srcIndexCount);
  549. Copy(srcVertices, dstVertices, srcVertexCount);
  550. }
  551. static void GraphConditioner(NativeArray<float2> points, ref NativeArray<float2> pgPoints, ref int pgPointCount, ref NativeArray<int2> pgEdges, ref int pgEdgeCount, bool resetTopology)
  552. {
  553. var min = new float2(math.INFINITY, math.INFINITY);
  554. var max = float2.zero;
  555. for (int i = 0; i < points.Length; ++i)
  556. {
  557. min = math.min(points[i], min);
  558. max = math.max(points[i], max);
  559. }
  560. var ext = (max - min);
  561. var mid = ext * 0.5f;
  562. var kNonRect = 0.0001f;
  563. // Construct a simple convex hull rect!.
  564. pgPointCount = resetTopology ? 0 : pgPointCount;
  565. var pc = pgPointCount;
  566. 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);
  567. 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);
  568. pgEdgeCount = 8;
  569. 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);
  570. 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);
  571. }
  572. // Reorder vertices.
  573. static void Reorder(int startVertexCount, int index, ref NativeArray<int> indices, ref int indexCount, ref NativeArray<float2> vertices, ref int vertexCount)
  574. {
  575. var found = false;
  576. for (var i = 0; i < indexCount; ++i)
  577. {
  578. if (indices[i] != index) continue;
  579. found = true;
  580. break;
  581. }
  582. if (!found)
  583. {
  584. vertexCount--;
  585. vertices[index] = vertices[vertexCount];
  586. for (var i = 0; i < indexCount; ++i)
  587. if (indices[i] == vertexCount)
  588. indices[i] = index;
  589. }
  590. }
  591. // Perform Sanitization.
  592. internal static void VertexCleanupConditioner(int startVertexCount, ref NativeArray<int> indices, ref int indexCount, ref NativeArray<float2> vertices, ref int vertexCount)
  593. {
  594. for (int i = startVertexCount; i < vertexCount; ++i)
  595. {
  596. Reorder(startVertexCount, i, ref indices, ref indexCount, ref vertices, ref vertexCount);
  597. }
  598. }
  599. 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)
  600. {
  601. // Inputs are garbage, just early out.
  602. float4 ret = float4.zero;
  603. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  604. if (points.Length < 3 || points.Length >= kMaxVertexCount)
  605. return ret;
  606. // Ensure inputs form a proper PlanarGraph.
  607. int pgEdgeCount = 0, pgPointCount = 0;
  608. NativeArray<int2> pgEdges = new NativeArray<int2>(kMaxEdgeCount, allocator);
  609. NativeArray<float2> pgPoints = new NativeArray<float2>(kMaxVertexCount, allocator);
  610. // Valid Edges and Paths, correct the Planar Graph. If invalid create a simple convex hull rect.
  611. GraphConditioner(points, ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount, true);
  612. Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref outVertices, ref outVertexCount, ref outIndices, ref outIndexCount);
  613. // Dispose Temp Memory.
  614. pgPoints.Dispose();
  615. pgEdges.Dispose();
  616. return ret;
  617. }
  618. public static float4 Tessellate(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)
  619. {
  620. // Inputs are garbage, just early out.
  621. float4 ret = float4.zero;
  622. outEdgeCount = 0; outIndexCount = 0; outVertexCount = 0;
  623. if (points.Length < 3 || points.Length >= kMaxVertexCount)
  624. return ret;
  625. // Ensure inputs form a proper PlanarGraph.
  626. bool validGraph = false, handleEdgeCase = false;
  627. int pgEdgeCount = 0, pgPointCount = 0;
  628. NativeArray<int2> pgEdges = new NativeArray<int2>(edges.Length * 8, allocator);
  629. NativeArray<float2> pgPoints = new NativeArray<float2>(points.Length * 4, allocator);
  630. // Valid Edges and Paths, correct the Planar Graph. If invalid create a simple convex hull rect.
  631. if (0 != edges.Length)
  632. {
  633. validGraph = PlanarGraph.Validate(allocator, points, points.Length, edges, edges.Length, ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount);
  634. }
  635. // Fallbacks are now handled by the Higher level packages. Enable if UTess needs to handle it.
  636. // #if UTESS_QUAD_FALLBACK
  637. // if (!validGraph)
  638. // {
  639. // pgPointCount = 0;
  640. // handleEdgeCase = true;
  641. // ModuleHandle.Copy(points, pgPoints, points.Length);
  642. // GraphConditioner(points, ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount, false);
  643. // }
  644. // #else
  645. // If its not a valid Graph simply return back input Data without triangulation instead of going through UTess (pointless wasted cpu cycles).
  646. if (!validGraph)
  647. {
  648. outEdgeCount = edges.Length;
  649. outVertexCount = points.Length;
  650. ModuleHandle.Copy(edges, outEdges, edges.Length);
  651. ModuleHandle.Copy(points, outVertices, points.Length);
  652. }
  653. // Do a proper Delaunay Triangulation if Inputs are valid.
  654. if (pgPointCount > 2 && pgEdgeCount > 2)
  655. {
  656. // Tessellate does not add new points, only PG and SD does. Assuming each point creates a degenerate triangle, * 4 is more than enough.
  657. NativeArray<int> tsIndices = new NativeArray<int>(pgPointCount * 8, allocator);
  658. NativeArray<float2> tsVertices = new NativeArray<float2>(pgPointCount * 4, allocator);
  659. int tsIndexCount = 0, tsVertexCount = 0;
  660. validGraph = Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref tsVertices, ref tsVertexCount, ref tsIndices, ref tsIndexCount);
  661. if (validGraph)
  662. {
  663. // Copy Out
  664. TransferOutput(pgEdges, pgEdgeCount, ref outEdges, ref outEdgeCount, tsIndices, tsIndexCount, ref outIndices, ref outIndexCount, tsVertices, tsVertexCount, ref outVertices, ref outVertexCount);
  665. if (handleEdgeCase == true)
  666. outEdgeCount = 0;
  667. }
  668. tsVertices.Dispose();
  669. tsIndices.Dispose();
  670. }
  671. // Dispose Temp Memory.
  672. pgPoints.Dispose();
  673. pgEdges.Dispose();
  674. return ret;
  675. }
  676. }
  677. }