internal static bool CalculateEdgeIntersections(NativeArray <int2> edges, int edgeCount, NativeArray <double2> points, int pointCount, ref NativeArray <int2> results, ref NativeArray <double2> intersects, ref int resultCount) { resultCount = 0; for (int i = 0; i < edgeCount; ++i) { for (int j = i + 1; j < edgeCount; ++j) { var e = edges[i]; var f = edges[j]; if (e.x == f.x || e.x == f.y || e.y == f.x || e.y == f.y) { continue; } var a = points[e.x]; var b = points[e.y]; var c = points[f.x]; var d = points[f.y]; var g = double2.zero; if (LineLineIntersection(a, b, c, d)) { if (LineLineIntersection(a, b, c, d, ref g)) { // Until we ensure Outline is generated properly, we need this useless Check every correction. if (resultCount >= intersects.Length) { return(false); } intersects[resultCount] = g; results[resultCount++] = new int2(i, j); } } } } // 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. if (resultCount > (edgeCount * kMaxIntersectionTolerance)) { return(false); } unsafe { var tjc = new IntersectionCompare(); tjc.edges = edges; tjc.points = points; ModuleHandle.InsertionSort <int2, IntersectionCompare>( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(results), 0, resultCount - 1, tjc); } return(true); }
private static readonly int kMaxIntersectionTolerance = 4; // Maximum Intersection Tolerance per Intersection Loop Check. internal static void RemoveDuplicateEdges(ref NativeArray <int2> edges, ref int edgeCount, NativeArray <int> duplicates, int duplicateCount) { if (duplicateCount == 0) { for (var i = 0; i < edgeCount; ++i) { var e = edges[i]; e.x = math.min(edges[i].x, edges[i].y); e.y = math.max(edges[i].x, edges[i].y); edges[i] = e; } } else { for (var i = 0; i < edgeCount; ++i) { var e = edges[i]; var a = duplicates[e.x]; var b = duplicates[e.y]; e.x = math.min(a, b); e.y = math.max(a, b); edges[i] = e; } } unsafe { ModuleHandle.InsertionSort <int2, TessEdgeCompare>( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(edges), 0, edgeCount - 1, new TessEdgeCompare()); } var n = 1; for (var i = 1; i < edgeCount; ++i) { var prev = edges[i - 1]; var next = edges[i]; if (next.x == prev.x && next.y == prev.y) { continue; } if (next.x == next.y) { continue; } edges[n++] = next; } edgeCount = n; }
public int Compare(int2 a, int2 b) { var e1a = edges[a.x]; var e1b = edges[a.y]; var e2a = edges[b.x]; var e2b = edges[b.y]; xvasort[0] = points[e1a.x].x; xvasort[1] = points[e1a.y].x; xvasort[2] = points[e1b.x].x; xvasort[3] = points[e1b.y].x; xvbsort[0] = points[e2a.x].x; xvbsort[1] = points[e2a.y].x; xvbsort[2] = points[e2b.x].x; xvbsort[3] = points[e2b.y].x; fixed(double *xvasortPtr = xvasort) { ModuleHandle.InsertionSort <double, XCompare>(xvasortPtr, 0, 3, new XCompare()); } fixed(double *xvbsortPtr = xvbsort) { ModuleHandle.InsertionSort <double, XCompare>(xvbsortPtr, 0, 3, new XCompare()); } for (int i = 0; i < 4; ++i) { if (xvasort[i] - xvbsort[i] != 0) { return(xvasort[i] < xvbsort[i] ? -1 : 1); } } return(points[e1a.x].y < points[e1a.x].y ? -1 : 1); }
internal static bool CutEdges(ref NativeArray <double2> points, ref int pointCount, ref NativeArray <int2> edges, ref int edgeCount, ref NativeArray <int2> tJunctions, ref int tJunctionCount, NativeArray <int2> intersections, NativeArray <double2> intersects, int intersectionCount) { for (int i = 0; i < intersectionCount; ++i) { var intr = intersections[i]; var e = intr.x; var f = intr.y; int2 j1 = int2.zero; j1.x = e; j1.y = pointCount; tJunctions[tJunctionCount++] = j1; int2 j2 = int2.zero; j2.x = f; j2.y = pointCount; tJunctions[tJunctionCount++] = j2; // Until we ensure Outline is generated properly, we need this useless Check every correction. if (pointCount >= points.Length) { return(false); } points[pointCount++] = intersects[i]; } unsafe { ModuleHandle.InsertionSort <int2, TessJunctionCompare>( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(tJunctions), 0, tJunctionCount - 1, new TessJunctionCompare()); } // Split edges along junctions for (int i = tJunctionCount - 1; i >= 0; --i) { var tJunction = tJunctions[i]; var e = tJunction.x; var edge = edges[e]; var s = edge.x; var t = edge.y; // Check if edge is not lexicographically sorted var a = points[s]; var b = points[t]; if (((a.x - b.x) < 0) || (a.x == b.x && (a.y - b.y) < 0)) { var tmp = s; s = t; t = tmp; } // Split leading edge edge.x = s; var last = edge.y = tJunction.y; edges[e] = edge; // If we are grouping edges by color, remember to track data // Split other edges while (i > 0 && tJunctions[i - 1].x == e) { var next = tJunctions[--i].y; int2 te = new int2(); te.x = last; te.y = next; edges[edgeCount++] = te; last = next; } int2 le = new int2(); le.x = last; le.y = t; edges[edgeCount++] = le; } return(true); }
// Perform Voronoi based Smoothing. Does not add/remove points but merely relocates internal vertices so they are uniform distributed. internal static bool Condition(Allocator allocator, ref NativeArray <float2> pgPoints, int pgPointCount, NativeArray <int2> pgEdges, int pgEdgeCount, ref NativeArray <float2> vertices, ref int vertexCount, ref NativeArray <int> indices, ref int indexCount) { // Build Triangles and Edges. float maxArea = 0, cmxArea = 0, minArea = 0, cmnArea = 0, avgArea = 0, minEdge = 0, maxEdge = 0, avgEdge = 0; bool polygonCentroid = true, validGraph = true; int triangleCount = 0, delaEdgeCount = 0, affectingEdgeCount = 0; var triangles = new NativeArray <UTriangle>(indexCount, allocator); // Intentionally added more room than actual Triangles needed here. var delaEdges = new NativeArray <int4>(indexCount, allocator); var voronoiEdges = new NativeArray <int4>(indexCount, allocator); var connectedTri = new NativeArray <int4>(vertexCount, allocator); var voronoiCheck = new NativeArray <int>(indexCount, allocator); var affectsEdges = new NativeArray <int>(indexCount, allocator); var triCentroids = new NativeArray <int>(vertexCount, allocator); ModuleHandle.BuildTrianglesAndEdges(vertices, vertexCount, indices, indexCount, ref triangles, ref triangleCount, ref delaEdges, ref delaEdgeCount, ref maxArea, ref avgArea, ref minArea); var refinedEdges = new NativeArray <int4>(delaEdgeCount, allocator); // Sort the Delaunay Edges. unsafe { ModuleHandle.InsertionSort <int4, DelaEdgeCompare>( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(delaEdges), 0, delaEdgeCount - 1, new DelaEdgeCompare()); } // TrimEdges. Update Triangle Info for Shared Edges and remove Duplicates. RefineEdges(ref refinedEdges, ref delaEdges, ref delaEdgeCount, ref voronoiEdges); // Now for each point, generate Voronoi diagram. for (int i = 0; i < vertexCount; ++i) { // Try moving this to Centroid of the Voronoi Polygon. GetAffectingEdges(i, delaEdges, delaEdgeCount, ref affectsEdges, ref voronoiCheck, ref affectingEdgeCount); var bounded = affectingEdgeCount != 0; // Check for Boundedness for (int j = 0; j < affectingEdgeCount; ++j) { // Edge Index. var ei = affectsEdges[j]; if (delaEdges[ei].z == -1 || delaEdges[ei].w == -1) { bounded = false; break; } } // If this is bounded point, relocate to Voronoi Diagram's Centroid if (bounded) { polygonCentroid = ConnectTriangles(ref connectedTri, ref affectsEdges, ref voronoiCheck, voronoiEdges, affectingEdgeCount); if (!polygonCentroid) { break; } float2 point = float2.zero; float area = 0, distance = 0; for (int k = 0; k < affectingEdgeCount; ++k) { CentroidByPolygon(connectedTri[k], triangles, ref point, ref area, ref distance); } point /= (3 * area); pgPoints[i] = point; } } // Do Delaunay Again. int srcIndexCount = indexCount, srcVertexCount = vertexCount; indexCount = 0; vertexCount = 0; triangleCount = 0; if (polygonCentroid) { validGraph = Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref vertices, ref vertexCount, ref indices, ref indexCount); if (validGraph) { ModuleHandle.BuildTriangles(vertices, vertexCount, indices, indexCount, ref triangles, ref triangleCount, ref cmxArea, ref avgArea, ref cmnArea, ref maxEdge, ref avgEdge, ref minEdge); } // This Edge validation prevents artifacts by forcing a fallback. todo: Fix the actual bug in Outline generation. validGraph = validGraph && (cmxArea < maxArea * kMaxAreaTolerance) && (maxEdge < avgEdge * kMaxEdgeTolerance); } // Cleanup. triangles.Dispose(); delaEdges.Dispose(); refinedEdges.Dispose(); voronoiCheck.Dispose(); voronoiEdges.Dispose(); affectsEdges.Dispose(); triCentroids.Dispose(); connectedTri.Dispose(); return(validGraph && srcIndexCount == indexCount && srcVertexCount == vertexCount); }
internal bool Triangulate(NativeArray <float2> points, int pointCount, NativeArray <int2> edges, int edgeCount) { m_NumEdges = edgeCount; m_NumHulls = edgeCount * 2; m_NumPoints = pointCount; m_CellCount = 0; m_Cells = new NativeArray <int3>(ModuleHandle.kMaxTriangleCount, m_Allocator); m_ILArray = new NativeArray <int>(m_NumHulls * (m_NumHulls + 1), m_Allocator); // Make room for -1 node. m_IUArray = new NativeArray <int>(m_NumHulls * (m_NumHulls + 1), m_Allocator); // Make room for -1 node. NativeArray <UHull> hulls = new NativeArray <UHull>(m_NumPoints * 8, m_Allocator); int hullCount = 0; NativeArray <UEvent> events = new NativeArray <UEvent>(m_NumPoints + (m_NumEdges * 2), m_Allocator); int eventCount = 0; for (int i = 0; i < m_NumPoints; ++i) { UEvent evt = new UEvent(); evt.a = points[i]; evt.b = new float2(); evt.idx = i; evt.type = (int)UEventType.EVENT_POINT; events[eventCount++] = evt; } for (int i = 0; i < m_NumEdges; ++i) { int2 e = edges[i]; float2 a = points[e.x]; float2 b = points[e.y]; if (a.x < b.x) { UEvent _s = new UEvent(); _s.a = a; _s.b = b; _s.idx = i; _s.type = (int)UEventType.EVENT_START; UEvent _e = new UEvent(); _e.a = b; _e.b = a; _e.idx = i; _e.type = (int)UEventType.EVENT_END; events[eventCount++] = _s; events[eventCount++] = _e; } else if (a.x > b.x) { UEvent _s = new UEvent(); _s.a = b; _s.b = a; _s.idx = i; _s.type = (int)UEventType.EVENT_START; UEvent _e = new UEvent(); _e.a = a; _e.b = b; _e.idx = i; _e.type = (int)UEventType.EVENT_END; events[eventCount++] = _s; events[eventCount++] = _e; } } unsafe { ModuleHandle.InsertionSort <UEvent, TessEventCompare>( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(events), 0, eventCount - 1, new TessEventCompare()); ; } var hullOp = true; float minX = events[0].a.x - (1 + math.abs(events[0].a.x)) * math.pow(2.0f, -16.0f); UHull hull; hull.a.x = minX; hull.a.y = 1; hull.b.x = minX; hull.b.y = 0; hull.idx = -1; hull.ilarray = new ArraySlice <int>(m_ILArray, m_NumHulls * m_NumHulls, m_NumHulls); // Last element hull.iuarray = new ArraySlice <int>(m_IUArray, m_NumHulls * m_NumHulls, m_NumHulls); hull.ilcount = 0; hull.iucount = 0; hulls[hullCount++] = hull; for (int i = 0, numEvents = eventCount; i < numEvents; ++i) { switch (events[i].type) { case (int)UEventType.EVENT_POINT: { hullOp = AddPoint(hulls, hullCount, points, events[i].a, events[i].idx); } break; case (int)UEventType.EVENT_START: { hullOp = SplitHulls(hulls, ref hullCount, points, events[i]); } break; default: { hullOp = MergeHulls(hulls, ref hullCount, points, events[i]); } break; } if (!hullOp) { break; } } events.Dispose(); hulls.Dispose(); return(hullOp); }
NativeArray <int3> Constrain(ref int count) { var cells = GetCells(ref count); int nc = count; for (int i = 0; i < nc; ++i) { int3 c = cells[i]; int x = c.x, y = c.y, z = c.z; if (y < z) { if (y < x) { c.x = y; c.y = z; c.z = x; } } else if (z < x) { c.x = z; c.y = x; c.z = y; } cells[i] = c; } unsafe { ModuleHandle.InsertionSort <int3, TessCellCompare>( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(cells), 0, m_CellCount - 1, new TessCellCompare()); } // Out m_Flags = new NativeArray <int>(nc, m_Allocator); m_Neighbors = new NativeArray <int>(nc * 3, m_Allocator); m_Constraints = new NativeArray <int>(nc * 3, m_Allocator); var next = new NativeArray <int>(nc * 3, m_Allocator); var active = new NativeArray <int>(nc * 3, m_Allocator); int side = 1, nextCount = 0, activeCount = 0; for (int i = 0; i < nc; ++i) { int3 c = cells[i]; for (int j = 0; j < 3; ++j) { int x = j, y = (j + 1) % 3; x = (x == 0) ? c.x : (j == 1) ? c.y : c.z; y = (y == 0) ? c.x : (y == 1) ? c.y : c.z; int o = OppositeOf(y, x); int a = m_Neighbors[3 * i + j] = FindNeighbor(cells, count, y, x, o); int b = m_Constraints[3 * i + j] = (-1 != FindConstraint(x, y)) ? 1 : 0; if (a < 0) { if (0 != b) { next[nextCount++] = i; } else { active[activeCount++] = i; m_Flags[i] = 1; } } } } while (activeCount > 0 || nextCount > 0) { while (activeCount > 0) { int t = active[activeCount - 1]; activeCount--; if (m_Flags[t] == -side) { continue; } m_Flags[t] = side; int3 c = cells[t]; for (int j = 0; j < 3; ++j) { int f = m_Neighbors[3 * t + j]; if (f >= 0 && m_Flags[f] == 0) { if (0 != m_Constraints[3 * t + j]) { next[nextCount++] = f; } else { active[activeCount++] = f; m_Flags[f] = side; } } } } for (int e = 0; e < nextCount; e++) { active[e] = next[e]; } activeCount = nextCount; nextCount = 0; side = -side; } active.Dispose(); next.Dispose(); return(cells); }
void PrepareDelaunay(NativeArray <int2> edges, int edgeCount) { m_StarCount = m_CellCount * 3; m_Stars = new NativeArray <UStar>(m_StarCount, m_Allocator); m_SPArray = new NativeArray <int>(m_StarCount * m_StarCount, m_Allocator); var UEdgeCount = 0; var UEdges = new NativeArray <int2>(m_StarCount, m_Allocator); // Input Edges. for (int i = 0; i < edgeCount; ++i) { int2 e = edges[i]; e.x = (edges[i].x < edges[i].y) ? edges[i].x : edges[i].y; e.y = (edges[i].x > edges[i].y) ? edges[i].x : edges[i].y; edges[i] = e; InsertUniqueEdge(UEdges, e, ref UEdgeCount); } m_Edges = new NativeArray <int2>(UEdgeCount, m_Allocator); for (int i = 0; i < UEdgeCount; ++i) { m_Edges[i] = UEdges[i]; } UEdges.Dispose(); unsafe { ModuleHandle.InsertionSort <int2, TessEdgeCompare>( NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Edges), 0, m_Edges.Length - 1, new TessEdgeCompare()); } // Init Stars. for (int i = 0; i < m_StarCount; ++i) { UStar s = m_Stars[i]; s.points = new ArraySlice <int>(m_SPArray, i * m_StarCount, m_StarCount); s.pointCount = 0; m_Stars[i] = s; } // Fill stars. for (int i = 0; i < m_CellCount; ++i) { int a = m_Cells[i].x; int b = m_Cells[i].y; int c = m_Cells[i].z; UStar sa = m_Stars[a]; UStar sb = m_Stars[b]; UStar sc = m_Stars[c]; sa.points[sa.pointCount++] = b; sa.points[sa.pointCount++] = c; sb.points[sb.pointCount++] = c; sb.points[sb.pointCount++] = a; sc.points[sc.pointCount++] = a; sc.points[sc.pointCount++] = b; m_Stars[a] = sa; m_Stars[b] = sb; m_Stars[c] = sc; } }