Exemple #1
0
    public EdgeLoop(Vector2[] points, ILinkedGraphEdgeFactory <EdgeType> factory, Object[] factoryParams)
    {
        LinkedGraphVertex[] verts = new LinkedGraphVertex[points.Length];
        for (int i = 0; i < verts.Length; i++)
        {
            verts[i] = new LinkedGraphVertex(points[i]);
        }
        EdgeType[] newEdges = new EdgeType[points.Length];
        for (int i = 0; i < verts.Length; i++)
        {
            newEdges[i] = factory.GetEdge(verts[i], verts[(i + 1) % points.Length], factoryParams);//new EdgeLoopEdge(verts[i], verts[(i + 1) % points.Length]);
        }

        this.edges = new List <EdgeType>(newEdges);
        RecalculateBounds();

        if (!Verify())
        {
            Debug.LogWarning("Edge loop edges do not form a loop.");
        }

        foreach (EdgeType edge in edges)
        {
            edge.AddEdgeSplitListener(this);
        }
    }
 public DividingEdge(Vector2 p1, Vector2 p2, ILinkedGraphEdgeFactory <EdgeType> factory, System.Object[] factoryParams)
 {
     this.p1            = p1;
     this.p2            = p2;
     this.factory       = factory;
     this.factoryParams = factoryParams;
 }
Exemple #3
0
    public static EdgeType AddEdge(LinkedGraphVertex aVert, LinkedGraphVertex bVert, ILinkedGraphEdgeFactory <EdgeType> edgeFactory, System.Object[] factoryParams, List <EdgeType> knownEdges)
    {
        EdgeType newEdge = edgeFactory.GetEdge(aVert, bVert, factoryParams);

        if (knownEdges != null)
        {
            knownEdges.Add(newEdge);
        }

        return(newEdge);
    }
Exemple #4
0
    public static void SubdivideEdge(EdgeType edge, LinkedGraphVertex midPoint, ILinkedGraphEdgeFactory <EdgeType> edgeFactory, List <EdgeType> knownEdges)
    {
        LinkedGraphVertex aVert = edge.a;
        LinkedGraphVertex bVert = edge.b;

        EdgeType aEdge = AddEdge(aVert, midPoint, edgeFactory, null, knownEdges);
        EdgeType bEdge = AddEdge(midPoint, bVert, edgeFactory, null, knownEdges);

        edge.OnEdgeSplit(aEdge, bEdge);

        knownEdges.Remove(edge);
        Detach(edge);
    }
    protected List <EdgeType[]> CollectChildLoops(SubdividableEdgeLoop <EdgeType> parent, List <DividingEdge> dividingEdges)
    {
        List <EdgeType> knownEdges    = new List <EdgeType>(parent.GetEdgesEnumerable());
        Polygon         parentPoly    = parent.GetPolygon();
        Path            polygonAsClip = parentPoly.ClipperPath(HelperFunctions.clipperScale);

        Vector2[] parentPoints = parent.GetPoints();

        //kinda ugly, these two variables are implicitly paired together
        Paths edgePaths = new Paths();
        List <ILinkedGraphEdgeFactory <EdgeType> > edgePathFactories = new List <ILinkedGraphEdgeFactory <EdgeType> >();
        List <System.Object[]> edgePathFactoriesParams = new List <System.Object[]>();

        foreach (DividingEdge edge in dividingEdges)
        {
            Path edgePath = new Path();
            edgePath.Add(HelperFunctions.GetIntPoint(edge.p1));
            edgePath.Add(HelperFunctions.GetIntPoint(edge.p2));

            PolyTree clippedResults = new PolyTree();
            Clipper  clipper        = new Clipper();

            clipper.AddPath(edgePath, PolyType.ptSubject, false);
            clipper.AddPath(polygonAsClip, PolyType.ptClip, true);
            clipper.Execute(ClipType.ctIntersection, clippedResults);

            Paths subPaths = Clipper.OpenPathsFromPolyTree(clippedResults);
            edgePaths.AddRange(subPaths);
            //if this edge was split into multiple paths when intersecting with parent poly, note that each subpath has the same factory
            foreach (Path path in subPaths)
            {
                edgePathFactories.Add(edge.factory);
                edgePathFactoriesParams.Add(edge.factoryParams);
            }
        }

        DebugLines debug = GameObject.FindObjectOfType <DebugLines>();


        int totalAddedEdges = 0;

        for (int j = 0; j < edgePaths.Count; j++)
        {
            Path edgePath = edgePaths[j];
            ILinkedGraphEdgeFactory <EdgeType> edgeFactory = edgePathFactories[j];
            System.Object[] edgeParams = edgePathFactoriesParams[j];
            //this is almost always just 2 elements in which case it runs once
            for (int i = 0; i < edgePath.Count - 1; i++)
            {
                //convert path back into regular coordinates. Watch out that there is high enough resolution
                //that when this conversion happens, the linkedGraph still thinks points/edges are adjacent and connects them
                Vector2 p1 = HelperFunctions.GetPoint(edgePath[i]);
                Vector2 p2 = HelperFunctions.GetPoint(edgePath[i + 1]);

                LinkedGraph <EdgeType> .ConnectNewEdge(p1, p2, edgeFactory, edgeParams, knownEdges);

                totalAddedEdges++;
            }
        }

        List <EdgeType[]> formedChildLoops = parent.GetInteriorEdgeLoops();

        if (formedChildLoops.Count == 0)
        {
            Debug.Log("No Children " + parent.GetHashCode());
        }
        return(formedChildLoops);
    }
Exemple #6
0
    //given a list of edges that may intersect with the new edge, connect the new edge
    public static void ConnectNewEdge(Vector2 a, Vector2 b, ILinkedGraphEdgeFactory <EdgeType> edgeFactory, System.Object[] factoryParams, List <EdgeType> knownEdges)
    {
        LinkedGraphVertex aVert = HasVertex(knownEdges, a);
        LinkedGraphVertex bVert = HasVertex(knownEdges, b);

        if (aVert != null && bVert != null && aVert == bVert || (a - b).sqrMagnitude < VERT_MERGE_DIST_SQR)
        {
            return;
        }

        if (aVert == null)
        {
            aVert = new LinkedGraphVertex(a);
        }
        if (bVert == null)
        {
            bVert = new LinkedGraphVertex(b);
        }

        if (knownEdges == null || knownEdges.Count == 0)
        {
            AddEdge(aVert, bVert, edgeFactory, factoryParams, knownEdges);
            return;
        }

        //use for checking intersections
        Segment testingSegment   = Segment.SegmentWithPoints(a, b);
        Rect    testingSegBounds = testingSegment.ExpandedBounds(VERT_MERGE_DIST_SQR);

        List <LinkedGraphVertex> intersectionVertices = new List <LinkedGraphVertex>();

        intersectionVertices.Add(aVert);//these will be sorted later
        intersectionVertices.Add(bVert);

        EdgeType[] edgesCopy = new EdgeType[knownEdges.Count];
        knownEdges.CopyTo(edgesCopy);

        //check if new vertices are on another line
        foreach (EdgeType seg in edgesCopy)
        {
            if (!seg.segRef.bounds.Overlaps(testingSegBounds))
            {
                continue;
            }
            if (seg.a == aVert || seg.a == bVert || seg.b == aVert || seg.b == bVert)
            {
                //do nothing in this case.
                //this is captured in the base case of finding existing verts
            }
            else if (seg.segRef.ContainsPoint(a, VERT_MERGE_DIST_SQR))
            {
                SubdivideEdge(seg, aVert, edgeFactory, knownEdges);
            }
            else if (seg.segRef.ContainsPoint(b, VERT_MERGE_DIST_SQR))
            {
                SubdivideEdge(seg, bVert, edgeFactory, knownEdges);
            }
            else if (testingSegment.ContainsPoint(seg.a.pt, VERT_MERGE_DIST_SQR))
            {
                intersectionVertices.Add(seg.a);
            }
            else if (testingSegment.ContainsPoint(seg.b.pt, VERT_MERGE_DIST_SQR))
            {
                intersectionVertices.Add(seg.b);
            }
            else
            {
                Vector2 intersectionPoint = Vector2.zero;
                if (seg.segRef.IntersectionWithSegment(testingSegment, out intersectionPoint))
                {
                    LinkedGraphVertex midPtVert = new LinkedGraphVertex(intersectionPoint);
                    SubdivideEdge(seg, midPtVert, edgeFactory, knownEdges);
                    intersectionVertices.Add(midPtVert);
                }
            }
        }
        Vector2 sortDirection = b - a;

        intersectionVertices.Sort((first, second) =>
                                  (first.pt - a).sqrMagnitude.CompareTo(
                                      (second.pt - a).sqrMagnitude));

        for (int i = 0; i < intersectionVertices.Count - 1; i++)
        {
            AddEdge(intersectionVertices[i], intersectionVertices[i + 1], edgeFactory, factoryParams, knownEdges);
        }
    }
Exemple #7
0
 public static void ConnectNewEdge(Vector2 a, Vector2 b, ILinkedGraphEdgeFactory <EdgeType> edgeFactory, List <EdgeType> knownEdges)
 {
     ConnectNewEdge(a, b, edgeFactory, null, knownEdges);
 }
Exemple #8
0
 public static EdgeType AddEdge(LinkedGraphVertex aVert, LinkedGraphVertex bVert, ILinkedGraphEdgeFactory <EdgeType> edgeFactory, List <EdgeType> knownEdges)
 {
     return(AddEdge(aVert, bVert, edgeFactory, null, knownEdges));
 }
 public CircularCenter(ILinkedGraphEdgeFactory <EdgeType> factory, System.Object[] factoryParams)
 {
     this.factory       = factory;
     this.factoryParams = factoryParams;
 }
Exemple #10
0
    public static EdgeType[] GetPolygonEdges(int sides, float radius, float radiusRandomness, float randomnessRangeDetail, ILinkedGraphEdgeFactory <EdgeType> factory, System.Object[] factoryParams)
    {
        LinkedGraphVertex[] verts = new LinkedGraphVertex[sides];
        EdgeType[]          edges = new EdgeType[sides];

        float noiseOffset = Random.value * 10000f;

        for (int i = 0; i < sides; i++)
        {
            float t     = (i / (float)sides);
            float angle = t * Mathf.PI * 2;

            Vector2 unitCircle  = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
            Vector2 position    = unitCircle * radius;
            float   noise       = Mathf.PerlinNoise(position.x / randomnessRangeDetail + noiseOffset, position.y / randomnessRangeDetail) - 0.5f;
            Vector2 modPosition = unitCircle * (radius + noise * radiusRandomness);
            verts[i] = new LinkedGraphVertex(modPosition);
        }

        for (int i = 0; i < sides; i++)
        {
            int firstVertInd  = i;
            int secondVertInd = (i + 1) % sides;
            edges[i] = factory.GetEdge(verts[firstVertInd], verts[secondVertInd], factoryParams);
        }
        return(edges);
    }
Exemple #11
0
 public Divide(ILinkedGraphEdgeFactory <EdgeType> factory, System.Object[] factoryParams)
 {
     this.factory       = factory;
     this.factoryParams = factoryParams;
 }
Exemple #12
0
 public GetPieSections(ILinkedGraphEdgeFactory <EdgeType> factory, System.Object[] factoryParams)
 {
     this.factory       = factory;
     this.factoryParams = factoryParams;
 }