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.

PlanarGraph.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using Unity.Collections;
  2. using Unity.Mathematics;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace UnityEngine.U2D.Common.UTess
  5. {
  6. // Ensures that there are no duplicate Points, Overlapping Edges.
  7. struct PlanarGraph
  8. {
  9. private static readonly double kEpsilon = 0.00001;
  10. private static readonly int kMaxIntersectionTolerance = 4; // Maximum Intersection Tolerance per Intersection Loop Check.
  11. internal static void RemoveDuplicateEdges(ref Array<int2> edges, ref int edgeCount, Array<int> duplicates, int duplicateCount)
  12. {
  13. if (duplicateCount == 0)
  14. {
  15. for (var i = 0; i < edgeCount; ++i)
  16. {
  17. var e = edges[i];
  18. e.x = math.min(edges[i].x, edges[i].y);
  19. e.y = math.max(edges[i].x, edges[i].y);
  20. edges[i] = e;
  21. }
  22. }
  23. else
  24. {
  25. for (var i = 0; i < edgeCount; ++i)
  26. {
  27. var e = edges[i];
  28. var a = duplicates[e.x];
  29. var b = duplicates[e.y];
  30. e.x = math.min(a, b);
  31. e.y = math.max(a, b);
  32. edges[i] = e;
  33. }
  34. }
  35. unsafe
  36. {
  37. ModuleHandle.InsertionSort<int2, TessEdgeCompare>(edges.UnsafePtr, 0, edgeCount - 1,new TessEdgeCompare());
  38. }
  39. var n = 1;
  40. for (var i = 1; i < edgeCount; ++i)
  41. {
  42. var prev = edges[i - 1];
  43. var next = edges[i];
  44. if (next.x == prev.x && next.y == prev.y)
  45. continue;
  46. if (next.x == next.y)
  47. continue;
  48. edges[n++] = next;
  49. }
  50. edgeCount = n;
  51. }
  52. internal static bool CheckCollinear(double2 a0, double2 a1, double2 b0, double2 b1)
  53. {
  54. double2 a = a0;
  55. double2 b = a1;
  56. double2 c = b0;
  57. double2 d = b1;
  58. double x = (b.y - a.y) / (b.x - a.x);
  59. double y = (c.y - a.y) / (c.x - a.x);
  60. double z = (d.y - a.y) / (d.x - a.x);
  61. return ((!math.isinf(x) || !math.isinf(y) || !math.isinf(z)) && math.abs(x - y) > kEpsilon && math.abs(x - z) > kEpsilon);
  62. }
  63. internal static bool LineLineIntersection(double2 a0, double2 a1, double2 b0, double2 b1)
  64. {
  65. var x0 = ModuleHandle.OrientFastDouble(a0, b0, b1);
  66. var y0 = ModuleHandle.OrientFastDouble(a1, b0, b1);
  67. if ((x0 > kEpsilon && y0 > kEpsilon) || (x0 < -kEpsilon && y0 < -kEpsilon))
  68. {
  69. return false;
  70. }
  71. var x1 = ModuleHandle.OrientFastDouble(b0, a0, a1);
  72. var y1 = ModuleHandle.OrientFastDouble(b1, a0, a1);
  73. if ((x1 > kEpsilon && y1 > kEpsilon) || (x1 < -kEpsilon && y1 < -kEpsilon))
  74. {
  75. return false;
  76. }
  77. // Check for degenerate collinear case
  78. if (math.abs(x0) < kEpsilon && math.abs(y0) < kEpsilon && math.abs(x1) < kEpsilon && math.abs(y1) < kEpsilon)
  79. {
  80. return CheckCollinear(a0, a1, b0, b1);
  81. }
  82. return true;
  83. }
  84. internal static bool LineLineIntersection(double2 p1, double2 p2, double2 p3, double2 p4, ref double2 result)
  85. {
  86. double bx = p2.x - p1.x;
  87. double by = p2.y - p1.y;
  88. double dx = p4.x - p3.x;
  89. double dy = p4.y - p3.y;
  90. double bDotDPerp = bx * dy - by * dx;
  91. if (math.abs(bDotDPerp) < kEpsilon)
  92. {
  93. return false;
  94. }
  95. double cx = p3.x - p1.x;
  96. double cy = p3.y - p1.y;
  97. double t = (cx * dy - cy * dx) / bDotDPerp;
  98. if ((t >= -kEpsilon) && (t <= 1.0f + kEpsilon))
  99. {
  100. result.x = p1.x + t * bx;
  101. result.y = p1.y + t * by;
  102. return true;
  103. }
  104. return false;
  105. }
  106. internal static bool CalculateEdgeIntersections(Array<int2> edges, int edgeCount, Array<double2> points, int pointCount, ref Array<int2> results, ref Array<double2> intersects, ref int resultCount)
  107. {
  108. resultCount = 0;
  109. for (int i = 0; i < edgeCount; ++i)
  110. {
  111. for (int j = i + 1; j < edgeCount; ++j)
  112. {
  113. var e = edges[i];
  114. var f = edges[j];
  115. if (e.x == f.x || e.x == f.y || e.y == f.x || e.y == f.y)
  116. continue;
  117. var a = points[e.x];
  118. var b = points[e.y];
  119. var c = points[f.x];
  120. var d = points[f.y];
  121. var g = double2.zero;
  122. if (LineLineIntersection(a, b, c, d))
  123. {
  124. if (LineLineIntersection(a, b, c, d, ref g))
  125. {
  126. // Until we ensure Outline is generated properly, we need this useless Check every correction.
  127. if (resultCount >= intersects.Length)
  128. return false;
  129. intersects[resultCount] = g;
  130. results[resultCount++] = new int2(i, j);
  131. }
  132. }
  133. }
  134. }
  135. // Basically we have self intersections all over (eg. gobo_tree_04). Better don't generate any Mesh as even though this will eventually succeed, error correction will take long time.
  136. if (resultCount > (edgeCount * kMaxIntersectionTolerance))
  137. {
  138. return false;
  139. }
  140. var tjc = new IntersectionCompare();
  141. tjc.edges = edges;
  142. tjc.points = points;
  143. unsafe
  144. {
  145. ModuleHandle.InsertionSort<int2, IntersectionCompare>(results.UnsafePtr, 0, resultCount - 1, tjc);
  146. }
  147. return true;
  148. }
  149. internal static bool CalculateTJunctions(Array<int2> edges, int edgeCount, Array<double2> points, int pointCount, Array<int2> results, ref int resultCount)
  150. {
  151. resultCount = 0;
  152. for (int i = 0; i < edgeCount; ++i)
  153. {
  154. for (int j = 0; j < pointCount; ++j)
  155. {
  156. var e = edges[i];
  157. if (e.x == j || e.y == j)
  158. continue;
  159. var a = points[e.x];
  160. var b = points[e.y];
  161. var c = points[j];
  162. var d = points[j];
  163. if (LineLineIntersection(a, b, c, d))
  164. {
  165. // Until we ensure Outline is generated properly, we need this useless Check every correction.
  166. if (resultCount >= results.Length)
  167. return false;
  168. results[resultCount++] = new int2(i, j);
  169. }
  170. }
  171. }
  172. return true;
  173. }
  174. internal static bool CutEdges(ref Array<double2> points, ref int pointCount, ref Array<int2> edges, ref int edgeCount, ref Array<int2> tJunctions, ref int tJunctionCount, Array<int2> intersections, Array<double2> intersects, int intersectionCount)
  175. {
  176. for (int i = 0; i < intersectionCount; ++i)
  177. {
  178. var intr = intersections[i];
  179. var e = intr.x;
  180. var f = intr.y;
  181. int2 j1 = int2.zero;
  182. j1.x = e;
  183. j1.y = pointCount;
  184. tJunctions[tJunctionCount++] = j1;
  185. int2 j2 = int2.zero;
  186. j2.x = f;
  187. j2.y = pointCount;
  188. tJunctions[tJunctionCount++] = j2;
  189. // Until we ensure Outline is generated properly, we need this useless Check every correction.
  190. if (pointCount >= points.Length)
  191. return false;
  192. points[pointCount++] = intersects[i];
  193. }
  194. unsafe
  195. {
  196. ModuleHandle.InsertionSort<int2, TessJunctionCompare>( tJunctions.UnsafePtr, 0, tJunctionCount - 1, new TessJunctionCompare());
  197. }
  198. // Split edges along junctions
  199. for (int i = tJunctionCount - 1; i >= 0; --i)
  200. {
  201. var tJunction = tJunctions[i];
  202. var e = tJunction.x;
  203. var edge = edges[e];
  204. var s = edge.x;
  205. var t = edge.y;
  206. // Check if edge is not lexicographically sorted
  207. var a = points[s];
  208. var b = points[t];
  209. if (((a.x - b.x) < 0) || (a.x == b.x && (a.y - b.y) < 0))
  210. {
  211. var tmp = s;
  212. s = t;
  213. t = tmp;
  214. }
  215. // Split leading edge
  216. edge.x = s;
  217. var last = edge.y = tJunction.y;
  218. edges[e] = edge;
  219. // If we are grouping edges by color, remember to track data
  220. // Split other edges
  221. while (i > 0 && tJunctions[i - 1].x == e)
  222. {
  223. var next = tJunctions[--i].y;
  224. int2 te = new int2();
  225. te.x = last;
  226. te.y = next;
  227. edges[edgeCount++] = te;
  228. last = next;
  229. }
  230. int2 le = new int2();
  231. le.x = last;
  232. le.y = t;
  233. edges[edgeCount++] = le;
  234. }
  235. return true;
  236. }
  237. internal static void RemoveDuplicatePoints(ref Array<double2> points, ref int pointCount, ref Array<int> duplicates, ref int duplicateCount, Allocator allocator)
  238. {
  239. TessLink link = TessLink.CreateLink(pointCount, allocator);
  240. for (int i = 0; i < pointCount; ++i)
  241. {
  242. for (int j = i + 1; j < pointCount; ++j)
  243. {
  244. if (math.distance(points[i], points[j]) < kEpsilon)
  245. {
  246. link.Link(i, j);
  247. }
  248. }
  249. }
  250. duplicateCount = 0;
  251. for (var i = 0; i < pointCount; ++i)
  252. {
  253. var j = link.Find(i);
  254. if (j != i)
  255. {
  256. duplicateCount++;
  257. points[j] = math.min(points[i], points[j]);
  258. }
  259. }
  260. // Find Duplicates.
  261. if (duplicateCount != 0)
  262. {
  263. var prevPointCount = pointCount;
  264. pointCount = 0;
  265. for (var i = 0; i < prevPointCount; ++i)
  266. {
  267. var j = link.Find(i);
  268. if (j == i)
  269. {
  270. duplicates[i] = pointCount;
  271. points[pointCount++] = points[i];
  272. }
  273. else
  274. {
  275. duplicates[i] = -1;
  276. }
  277. }
  278. // Update Duplicates.
  279. for (int i = 0; i < prevPointCount; ++i)
  280. {
  281. if (duplicates[i] < 0)
  282. {
  283. duplicates[i] = duplicates[link.Find(i)];
  284. }
  285. }
  286. }
  287. TessLink.DestroyLink(link);
  288. }
  289. // Validate the Input Points ane Edges.
  290. internal static bool Validate(Allocator allocator, in NativeArray<float2> inputPoints, int pointCount, in NativeArray<int2> inputEdges, int edgeCount, ref NativeArray<float2> outputPoints, out int outputPointCount, ref NativeArray<int2> outputEdges, out int outputEdgeCount)
  291. {
  292. outputPointCount = 0;
  293. outputEdgeCount = 0;
  294. // Outline generated inputs can have differences in the range of 0.00001f.. See TwoLayers.psb sample.
  295. // Since PlanarGraph operates on double, scaling up and down does not result in loss of data.
  296. var precisionFudge = 10000.0f;
  297. var protectLoop = edgeCount;
  298. var requiresFix = true;
  299. var validGraph = false;
  300. // Processing Arrays.
  301. int startEdgeCount = edgeCount;
  302. Array<int> duplicates = new Array<int>(startEdgeCount, ModuleHandle.kMaxEdgeCount, allocator, NativeArrayOptions.UninitializedMemory);
  303. Array<int2> edges = new Array<int2>(startEdgeCount, ModuleHandle.kMaxEdgeCount, allocator, NativeArrayOptions.UninitializedMemory);
  304. Array<int2> tJunctions = new Array<int2>(startEdgeCount, ModuleHandle.kMaxEdgeCount, allocator, NativeArrayOptions.UninitializedMemory);
  305. Array<int2> edgeIntersections = new Array<int2>(startEdgeCount, ModuleHandle.kMaxEdgeCount, allocator, NativeArrayOptions.UninitializedMemory);
  306. Array<double2> points = new Array<double2>(pointCount * 2, pointCount * 8, allocator, NativeArrayOptions.UninitializedMemory);
  307. Array<double2> intersects = new Array<double2>(pointCount * 2, pointCount * 8, allocator, NativeArrayOptions.UninitializedMemory);
  308. // Initialize.
  309. for (int i = 0; i < pointCount; ++i)
  310. points[i] = inputPoints[i] * precisionFudge;
  311. unsafe
  312. {
  313. UnsafeUtility.MemCpy(edges.UnsafeReadOnlyPtr, inputEdges.GetUnsafePtr(), edgeCount * sizeof(int2));
  314. }
  315. // Pre-clear duplicates, otherwise the following will simply fail.
  316. RemoveDuplicateEdges(ref edges, ref edgeCount, duplicates, 0);
  317. // While PSG is clean.
  318. while (requiresFix && --protectLoop > 0)
  319. {
  320. // Edge Edge Intersection.
  321. int intersectionCount = 0;
  322. validGraph = CalculateEdgeIntersections(edges, edgeCount, points, pointCount, ref edgeIntersections, ref intersects, ref intersectionCount);
  323. if (!validGraph)
  324. break;
  325. // Edge Point Intersection. T-Junction.
  326. int tJunctionCount = 0;
  327. validGraph = CalculateTJunctions(edges, edgeCount, points, pointCount, tJunctions, ref tJunctionCount);
  328. if (!validGraph)
  329. break;
  330. // Cut Overlapping Edges.
  331. validGraph = CutEdges(ref points, ref pointCount, ref edges, ref edgeCount, ref tJunctions, ref tJunctionCount, edgeIntersections, intersects, intersectionCount);
  332. if (!validGraph)
  333. break;
  334. // Remove Duplicate Points.
  335. int duplicateCount = 0;
  336. RemoveDuplicatePoints(ref points, ref pointCount, ref duplicates, ref duplicateCount, allocator);
  337. RemoveDuplicateEdges(ref edges, ref edgeCount, duplicates, duplicateCount);
  338. requiresFix = intersectionCount != 0 || tJunctionCount != 0;
  339. }
  340. if (validGraph)
  341. {
  342. // Finalize Output.
  343. outputEdgeCount = edgeCount;
  344. outputPointCount = pointCount;
  345. unsafe
  346. {
  347. UnsafeUtility.MemCpy(outputEdges.GetUnsafePtr(), edges.UnsafeReadOnlyPtr, edgeCount * sizeof(int2));
  348. }
  349. for (int i = 0; i < pointCount; ++i)
  350. outputPoints[i] = new float2((float)(points[i].x / precisionFudge), (float)(points[i].y / precisionFudge));
  351. }
  352. edges.Dispose();
  353. points.Dispose();
  354. intersects.Dispose();
  355. duplicates.Dispose();
  356. tJunctions.Dispose();
  357. edgeIntersections.Dispose();
  358. return (validGraph && protectLoop > 0);
  359. }
  360. }
  361. }