/** Special case: only use this when you have DISCONTINUOUS edges
and need to jump between them.

For all other cases, use AddEdge to have it auatomatically add the ends
intelligently as needed with no dupes
*/
    public void AddJumpToVertex( VoronoiCellVertex vertex )
    {
      if( pathNodes == null )
      {
        pathNodes = new List<VoronoiEdgePathNode>();
      }
      
      pathNodes.Add ( new VoronoiEdgePathNode(vertex));
      }
Пример #2
0
/** Special case: only use this when you have DISCONTINUOUS edges
 * and need to jump between them.
 *
 * For all other cases, use AddEdge to have it auatomatically add the ends
 * intelligently as needed with no dupes
 */
        public void AddJumpToVertex(VoronoiCellVertex vertex)
        {
            if (pathNodes == null)
            {
                pathNodes = new List <VoronoiEdgePathNode>();
            }

            pathNodes.Add(new VoronoiEdgePathNode(vertex));
        }
		public void AddVertex( VoronoiCellVertex vertex )
		{
		if( edgeVertexA == null )
		edgeVertexA = vertex;
		else if( edgeVertexB == null )
		edgeVertexB = vertex;
		else
		{
		Debug.LogError("Impossible: an edge can only have 2 vertices, not 3!");
		return;
		}
		
		if( !vertex.ContainsEdge( this ))
				vertex.AddEdge( this );
		}
Пример #4
0
        public void AddVertex(VoronoiCellVertex vertex)
        {
            if (edgeVertexA == null)
            {
                edgeVertexA = vertex;
            }
            else if (edgeVertexB == null)
            {
                edgeVertexB = vertex;
            }
            else
            {
                Debug.LogError("Impossible: an edge can only have 2 vertices, not 3!");
                return;
            }

            if (!vertex.ContainsEdge(this))
            {
                vertex.AddEdge(this);
            }
        }
Пример #5
0
        /** Required because of major bugs in the AS3Delaunay algorithm where it fails
         * to maintain identity of Vector2 co-ordinates (unlike Unity, which maintains them
         * effectively).
         *
         * Any data that came from the AS3Delaunay core ported code will return "randomly varying"
         * float co-ords, that we have to manually error-correct before using as a key in dictionary,
         * Ugh.
         */
        private static VoronoiCellVertex FetchVertexOrAddToObject(Vector2 voronoiSite, Dictionary <Vector2, VoronoiCellVertex> verticesByVector2, GameObject go, bool useUnity2DCoordsNot3D)
        {
            foreach (KeyValuePair <Vector2, VoronoiCellVertex> item in verticesByVector2)
            {
                if (Site.CloseEnough(item.Key, voronoiSite))
                {
                    return(item.Value);
                }
            }

            /** no match found; create, add, return */
            GameObject        goVertex  = new GameObject("Vertex-#" + (verticesByVector2.Count));
            VoronoiCellVertex newVertex = goVertex.AddComponent <VoronoiCellVertex>();

            newVertex.positionInDiagram = voronoiSite;
            verticesByVector2.Add(voronoiSite, newVertex);
            goVertex.transform.parent        = go.transform;
            goVertex.transform.localPosition = new Vector3(voronoiSite.x, useUnity2DCoordsNot3D ? voronoiSite.y : 0, useUnity2DCoordsNot3D ? 0 : voronoiSite.y); // easier to find it when debugging!

            return(newVertex);
        }
Пример #6
0
        /** AS3Delaunay library automatically gives us this, and its useless for most things, but great when
         *          trying to make a simple mesh.
         *
         *           Complex meshes would need to know the original Edge objects, so they can set vertex-colouring,
         *           vertex-texture-blending etc (which this CANNOT provide), but we might as well make the simple
         *           version easy while we can!
         *
         *           TODO: border cells will randomly miss some of their edges because this
         *           method stops when it hits an outside / infinite edge, and does NOT scan
         *           through the dictionary to try and find a resume point
         */
        //public List<Vector2> orderedPointsOnCircumference;
        public VoronoiEdgePath CalculateOrderedPathAroundEdges()
        {
            VoronoiEdgePath path = new VoronoiEdgePath();

            /** pick a random edge */
            VoronoiCellEdge firstEdge = neighbours.Keys.ToList().First();

            VoronoiCellEdge currentEdge = firstEdge;

            path.AddEdge(currentEdge, true); // true means nextVertex will be B, false means A; you can choose, just implement next few lines appropriately!

            /** Trace the edge, and when yuo reach a vertex, find the only OTHER edge from
             * that vertex that leads to a vertex that is ALSO on this cell */
            VoronoiCellVertex nextVertex = currentEdge.edgeVertexB;
            VoronoiCellEdge   nextEdge   = nextVertex.GetOtherEdgeContainingCell(currentEdge, this);

            while (nextEdge != firstEdge)
            {
                if (nextEdge == null)
                {
                    /** We hit the end prematurely; means this Cell is on the outer edge of diagram
                     * and is UNCLOSED.
                     *
                     * So we need to scan through neighbours looking for a vertex that
                     * only has ONE edge in this cell, but is NOT the last edge (which meets that criteria too)
                     */
                    foreach (KeyValuePair <VoronoiCellEdge, VoronoiCell> item in neighbours)
                    {
                        if (item.Key == currentEdge)
                        {
                            continue; // found the edge we're already on
                        }
                        VoronoiCellVertex newNextVertex = null;
                        if (item.Key.edgeVertexA.GetEdgesBorderingCell(this).Count < 2)
                        {
                            /** we've found the other one. Now need to start from that vertex.
                             */
                            newNextVertex = item.Key.edgeVertexA;
                        }
                        else if (item.Key.edgeVertexB.GetEdgesBorderingCell(this).Count < 2)
                        {
                            /** we've found the other one. Now need to start from that vertex.
                             */
                            newNextVertex = item.Key.edgeVertexB;
                        }

                        if (newNextVertex != null)
                        {
                            /** Need to FORCE-ADD that vertex onto the path too, since we're jumping
                             * the gap */
                            path.AddJumpToVertex(newNextVertex);

                            nextVertex = newNextVertex;
                            nextEdge   = item.Key;
                        }
                    }

                    if (nextEdge == firstEdge) // rare, but can happen!
                    {
                        break;                 // we added the forced jump to vertex, but have no more edges to add, so stop
                    }
                    if (nextEdge == null)
                    {
                        /** Something has gone badly wrong */
                        Debug.LogError("Major error: was trying to close the loop of an outer Cell, but couldn't find a restart point");
                        break;
                    }
                }

                /** ... normal body of while loop starts here ... */
                bool isNextEdgeAToB = (nextVertex == nextEdge.edgeVertexA);
                path.AddEdge(nextEdge, isNextEdgeAToB);

                if (isNextEdgeAToB)
                {
                    nextVertex = nextEdge.edgeVertexB;
                }
                else
                {
                    nextVertex = nextEdge.edgeVertexA;
                }

                currentEdge = nextEdge;
                nextEdge    = nextVertex.GetOtherEdgeContainingCell(currentEdge, this);
            }

            return(path);
        }
 public VoronoiEdgePathNode( VoronoiCellVertex v )
 {
   nodeType = VoronoiEdgePathNodeType.VERTEX;
   vertex = v;
 }
Пример #8
0
 public VoronoiEdgePathNode(VoronoiCellVertex v)
 {
     nodeType = VoronoiEdgePathNodeType.VERTEX;
     vertex   = v;
 }