GetNode() public static method

public static GetNode ( int index, List &_nodes ) : Node
index int
_nodes List
return Node
Ejemplo n.º 1
0
        public bool Evaluate()
        {
            evaluationResult = true;

            // Check smallest angles
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].adjacents.Count != 2)
                {
                    continue;
                }

                Node prevNode = EdgeGraphUtility.GetNode(nodes[i].adjacents[0], ref nodes);
                Node nextNode = EdgeGraphUtility.GetNode(nodes[i].adjacents[1], ref nodes);

                Vector3 dirToPrev = (prevNode.Position - nodes[i].Position).normalized;
                Vector3 dirToNext = (nextNode.Position - nodes[i].Position).normalized;

                if (Vector3.Angle(dirToPrev, dirToNext) < minAcceptAngle)
                {
                    evaluationResult = false;
                }
            }

            return(evaluationResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks if given point is inside the edges. The point must be in the same space than node positions
        /// </summary>
        public static bool PointIsInside(Vector3 point, List <Node> nodes, List <Edge> edges)
        {
            Vector3 position = Vector3.zero;

            for (int i = 0; i < nodes.Count; i++)
            {
                position += nodes[i].Position;
            }

            position /= nodes.Count;

            Vector2 posInXZSpace = new Vector2(position.x, position.z);
            Vector2 pointOutside = posInXZSpace + Vector2.right * 10000f;

            Vector2 pointInXZSpace = new Vector2(point.x, point.z);

            int intersectedEdgeCount = 0;

            foreach (var edge in edges)
            {
                //Node n1 = nodes.Find(x => x.ID == edge.Node1);
                //Node n2 = nodes.Find(x => x.ID == edge.Node2);

                Node node1 = EdgeGraphUtility.GetNode(edge.Node1, ref nodes);
                if (node1 == null)
                {
                    continue;
                }

                Vector3 n1Pos   = node1.Position;
                Vector3 n1PosXZ = new Vector2(n1Pos.x, n1Pos.z);

                Node node2 = EdgeGraphUtility.GetNode(edge.Node2, ref nodes);
                if (node2 == null)
                {
                    continue;
                }

                Vector3 n2Pos   = node2.Position;
                Vector3 n2PosXZ = new Vector2(n2Pos.x, n2Pos.z);

                if (UtilityTools.MathHelper.AreIntersecting(pointOutside, pointInXZSpace, n1PosXZ, n2PosXZ, 0f) == 1)
                {
                    intersectedEdgeCount++;
                }
            }

            return(intersectedEdgeCount % 2 != 0);
        }
Ejemplo n.º 3
0
        public static Node GetCounterClockwiseMostAdjacent(Node prev, Node curr, ref List <Node> _nodes)
        {
            if (curr.adjacents == null || curr.adjacents.Count == 0)
            {
                return(null);
            }

            Vector3 dirCurr = Vector3.zero;

            dirCurr = curr.Position - prev.Position;

            Node next = EdgeGraphUtility.GetNode(curr.GetAdjacent(prev.ID), ref _nodes);

            if (next == null)
            {
                return(null);
            }

            Vector3 dirNext = next.Position - curr.Position;

            bool currIsConvex = DotPerp(dirNext, dirCurr) <= 0;

            foreach (var a in curr.adjacents)
            {
                var     adj    = EdgeGraphUtility.GetNode(a, ref _nodes);
                Vector3 dirAdj = adj.Position - curr.Position;
                if (currIsConvex)
                {
                    if (DotPerp(dirCurr, dirAdj) > 0 && DotPerp(dirNext, dirAdj) > 0)
                    {
                        next         = adj;
                        dirNext      = dirAdj;
                        currIsConvex = DotPerp(dirNext, dirCurr) <= 0;
                    }
                }
                else
                {
                    if (DotPerp(dirCurr, dirAdj) > 0 || DotPerp(dirNext, dirAdj) > 0)
                    {
                        next         = adj;
                        dirNext      = dirAdj;
                        currIsConvex = DotPerp(dirNext, dirCurr) <= 0;
                    }
                }
            }

            return(next);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns closest point on all the given edges. Edge positions are changed to world coordinates in reference to refTransform.
        /// </summary>
        public static Vector3 GetClosestPointOnEdge(Vector3 point, List <Node> nodes, List <Edge> edges, Transform refTransform, out Edge closestEdge)
        {
            Vector3 pointOnEdge = Vector3.zero;

            closestEdge = edges[0];
            float closestDist = Mathf.Infinity;

            for (int i = 0; i < edges.Count; i++)
            {
                Vector2 splitPointXZ;

                Node node1 = EdgeGraphUtility.GetNode(edges[i].Node1, ref nodes);
                if (node1 == null)
                {
                    continue;
                }

                Vector3 n1Pos = node1.Position;
                n1Pos = refTransform.TransformPoint(n1Pos);

                Node node2 = EdgeGraphUtility.GetNode(edges[i].Node2, ref nodes);
                if (node2 == null)
                {
                    continue;
                }

                Vector3 n2Pos = node2.Position;
                n2Pos = refTransform.TransformPoint(n2Pos);

                splitPointXZ = Edge.GetClosestPointOnEdge(point, n1Pos, n2Pos);

                Vector3 splitPoint = new Vector3(splitPointXZ.x, n1Pos.y, splitPointXZ.y);

                if (Vector3.Distance(point, splitPoint) < closestDist)
                {
                    closestEdge = edges[i];
                    pointOnEdge = splitPoint;
                    closestDist = Vector3.Distance(point, splitPoint);
                }
            }

            return(pointOnEdge);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns closest point on the closest point with given point
        /// </summary>
        public static Vector3 GetPointOnClosestEdge(Vector3 point, List <Node> nodes, List <Edge> edges, Transform refTransform)
        {
            Edge closestEdge = GetClosestEdge(point, nodes, edges, refTransform);

            if (closestEdge == null)
            {
                return(Vector3.zero);
            }

            Vector2 splitPointXZ;

            Node node1 = EdgeGraphUtility.GetNode(closestEdge.Node1, ref nodes);

            if (node1 == null)
            {
                return(Vector3.zero);
            }

            Vector3 n1Pos = node1.Position;

            n1Pos = refTransform.TransformPoint(n1Pos);

            Node node2 = EdgeGraphUtility.GetNode(closestEdge.Node2, ref nodes);

            if (node2 == null)
            {
                return(Vector3.zero);
            }

            Vector3 n2Pos = node2.Position;

            n2Pos = refTransform.TransformPoint(n2Pos);

            splitPointXZ = Edge.GetClosestPointOnEdge(point, n1Pos, n2Pos);

            return(new Vector3(splitPointXZ.x, n1Pos.y, splitPointXZ.y));
        }
Ejemplo n.º 6
0
        public void CutAcuteAngles()
        {
            if (type != PrimitiveType.MinimalCycle)
            {
                return;
            }

            List <Node> _nodesToRemove = new List <Node>();
            List <Node> _newNodes      = new List <Node>();
            List <Edge> _newEdges      = new List <Edge>();

            // Old edges are kept in the edges list, but node changes are saved here and refreshed to the edges once all angles are checked
            Dictionary <string, List <NodePair> > nodesToSwitchInEdges = new Dictionary <string, List <NodePair> >();

            for (int i = 0; i < nodes.Count; i++)
            {
                Node prevNode = EdgeGraphUtility.GetNode(nodes[i].adjacents[0], ref nodes);
                Node nextNode = EdgeGraphUtility.GetNode(nodes[i].adjacents[1], ref nodes);

                Vector3 dirToPrev = (prevNode.Position - nodes[i].Position).normalized;
                Vector3 dirToNext = (nextNode.Position - nodes[i].Position).normalized;

                Edge prevEdge = EdgeGraphUtility.FindEdgeByNodes(nodes[i], prevNode, edges);
                Edge nextEdge = EdgeGraphUtility.FindEdgeByNodes(nodes[i], nextNode, edges);

                float angle = Vector3.Angle(dirToPrev, dirToNext);
                if (angle < 45f)
                {
                    // Move nodes so that the cut side is 1f wide
                    float distanceToMove = .55f / Mathf.Sin(Mathf.Deg2Rad * angle / 2f);

                    float distToPrev = Vector3.Distance(prevNode.Position, nodes[i].Position);
                    float distToNext = Vector3.Distance(nextNode.Position, nodes[i].Position);

                    if (distanceToMove > distToPrev)
                    {
                        distanceToMove = distToPrev * .8f;
                    }

                    if (distanceToMove > distToNext)
                    {
                        distanceToMove = distToNext * .8f;
                    }

                    Vector3 newNodePrevPos = (nodes[i].Position + dirToPrev * distanceToMove);
                    Vector3 newNodeNextPos = (nodes[i].Position + dirToNext * distanceToMove);

                    string oldNodeID = nodes[i].ID;

                    // Remove old node
                    _nodesToRemove.Add(nodes[i]);

                    // Make new nodes
                    Node newNodeToPrev = new Node(newNodePrevPos);

                    Node newNodeToNext = new Node(newNodeNextPos);

                    if (!nodesToSwitchInEdges.ContainsKey(prevEdge.ID))
                    {
                        nodesToSwitchInEdges.Add(prevEdge.ID, new List <NodePair>());
                    }

                    nodesToSwitchInEdges[prevEdge.ID].Add(new NodePair(oldNodeID, newNodeToPrev.ID));

                    if (!nodesToSwitchInEdges.ContainsKey(nextEdge.ID))
                    {
                        nodesToSwitchInEdges.Add(nextEdge.ID, new List <NodePair>());
                    }

                    nodesToSwitchInEdges[nextEdge.ID].Add(new NodePair(oldNodeID, newNodeToNext.ID));

                    // Add the new edge
                    Edge newEdge = new Edge(newNodeToNext.ID, newNodeToPrev.ID);

                    _newEdges.Add(newEdge);

                    // Add new nodes to the dict
                    _newNodes.Add(newNodeToPrev);
                    _newNodes.Add(newNodeToNext);
                }
            }

            foreach (var n in _nodesToRemove)
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    if (nodes[i].ID == n.ID)
                    {
                        nodes.RemoveAt(i);
                        i--;
                    }
                }
            }

            foreach (var kvp in nodesToSwitchInEdges)
            {
                for (int i = 0; i < edges.Count; i++)
                {
                    if (edges[i].ID == kvp.Key)
                    {
                        Edge e = edges[i];
                        for (int j = 0; j < kvp.Value.Count; j++)
                        {
                            NodePair pair = kvp.Value[j];
                            if (e.Node1 == pair.oldNode)
                            {
                                e.Node1 = pair.newNode;
                            }
                            if (e.Node2 == pair.oldNode)
                            {
                                e.Node2 = pair.newNode;
                            }
                        }
                    }
                }
            }

            _newEdges.ForEach((e) => edges.Add(e));
            _newNodes.ForEach((n) => nodes.Add(n));

            EdgeGraphUtility.CleanUpEdges(ref nodes, ref edges);
            EdgeGraphUtility.CheckAdjacentNodes(ref nodes, ref edges);
        }
Ejemplo n.º 7
0
        // Algorithms from http://www.geometrictools.com/Documentation/MinimalCycleBasis.pdf
        // The Minimal Cycle Basis for a Planar Graph by David Eberly

        /// <summary>
        /// Attempts to find minimal cycles
        /// </summary>
        public static void ExtractPrimitive(Node _n0, ref List <Node> _nodes, ref List <Edge> _edges, ref List <Primitive> _primitives)
        {
            List <Node> visited  = new List <Node>();
            List <Node> sequence = new List <Node>();

            EdgeGraphUtility.CheckAdjacentNodes(ref _nodes, ref _edges);

            if (_n0.adjacents.Count == 0)
            {
                EdgeGraphUtility.RemoveNodeAndCleanAdjacents(_n0, ref _nodes, ref _edges);
                return;
            }

            sequence.Add(_n0);
            Node _n1  = GetClockwiseMostAdjacent(null, _n0, ref _nodes);
            Node prev = _n0;
            Node curr = _n1;

            while (curr != null && curr != _n0 && !visited.Contains(curr))
            {
                sequence.Add(curr);
                visited.Add(curr);
                Node next = GetCounterClockwiseMostAdjacent(prev, curr, ref _nodes);
                prev = curr;
                curr = next;
            }

            if (curr == null)
            {
                // Filament found, not necessarily rooted at prev
                ExtractFilament(prev, EdgeGraphUtility.GetNode(prev.adjacents[0], ref _nodes), ref _nodes, ref _edges, ref _primitives);
            }
            else if (curr == _n0)
            {
                // Minimal cycle found
                Primitive primitive = new Primitive(Primitive.PrimitiveType.MinimalCycle);
                primitive.nodes.AddRange(sequence);

                for (int i = 0; i < sequence.Count; i++)
                {
                    Node n1;
                    Node n2;
                    if (i == sequence.Count - 1)
                    {
                        n1 = sequence[i];
                        n2 = sequence[0];
                    }
                    else
                    {
                        n1 = sequence[i];
                        n2 = sequence[i + 1];
                    }
                    Edge e = EdgeGraphUtility.FindEdgeByNodes(n1, n2, _edges);
                    if (e != null)
                    {
                        primitive.edges.Add(e);
                        e.isPartOfCycle = true;
                    }
                }

                EdgeGraphUtility.RemoveEdgeAndCleanAdjacents(_n0, _n1, ref _nodes, ref _edges);

                if (_n0.adjacents.Count == 1)
                {
                    // Remove the filament rooted at v0
                    ExtractFilament(_n0, EdgeGraphUtility.GetNode(_n0.adjacents[0], ref _nodes), ref _nodes, ref _edges, ref _primitives);
                }

                if (_n1.adjacents.Count == 1)
                {
                    // Remove the filament rooted at v1
                    ExtractFilament(_n1, EdgeGraphUtility.GetNode(_n1.adjacents[0], ref _nodes), ref _nodes, ref _edges, ref _primitives);
                }

                _primitives.Add(primitive);
            }
            else   // curr was visited earlier
            {
                // A cycle has been found, but is not guaranteed to be a minimal cycle.
                // This implies v0 is part of a filament
                // Locate the starting point for the filament by traversing from v0 away from the initial v1

                while (_n0.adjacents.Count == 2)
                {
                    if (_n0.adjacents[0] != _n1.ID)
                    {
                        _n1 = _n0;
                        _n0 = EdgeGraphUtility.GetNode(_n0.adjacents[0], ref _nodes);
                    }
                    else
                    {
                        _n1 = _n0;
                        _n0 = EdgeGraphUtility.GetNode(_n0.adjacents[1], ref _nodes);
                    }
                }

                ExtractFilament(_n0, _n1, ref _nodes, ref _edges, ref _primitives);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Extracts filament consisting of nodes and edges
        /// </summary>
        public static void ExtractFilament(Node _n0, Node _n1, ref List <Node> _nodes, ref List <Edge> _edges, ref List <Primitive> _primitives)
        {
            Edge e = EdgeGraphUtility.FindEdgeByNodes(_n0, _n1, _edges);

            if (e != null && e.isPartOfCycle)
            {
                if (_n0.adjacents.Count >= 3)
                {
                    EdgeGraphUtility.RemoveEdgeAndCleanAdjacents(_n0, _n1, ref _nodes, ref _edges);
                    _n0 = _n1;
                    if (_n0.adjacents.Count == 1)
                    {
                        _n1 = EdgeGraphUtility.GetNode(_n0.adjacents[0], ref _nodes);
                    }
                }

                while (_n0.adjacents.Count == 1)
                {
                    _n1 = EdgeGraphUtility.GetNode(_n0.adjacents[0], ref _nodes);
                    Edge ee = EdgeGraphUtility.FindEdgeByNodes(_n0, _n1, _edges);
                    if (ee != null && e.isPartOfCycle)
                    {
                        EdgeGraphUtility.RemoveNodeAndCleanAdjacents(_n0, ref _nodes, ref _edges);
                        EdgeGraphUtility.RemoveEdgeAndCleanAdjacents(_n0, _n1, ref _nodes, ref _edges);
                    }
                    else
                    {
                        break;
                    }
                }

                if (_n0.adjacents.Count == 0)
                {
                    EdgeGraphUtility.RemoveNodeAndCleanAdjacents(_n0, ref _nodes, ref _edges);
                }
            }
            else
            {
                Primitive primitive = new Primitive(Primitive.PrimitiveType.Filament);

                if (_n0.adjacents.Count >= 3)
                {
                    primitive.nodes.Add(_n0);
                    primitive.edges.Add(e);
                    EdgeGraphUtility.RemoveEdgeAndCleanAdjacents(_n0, _n1, ref _nodes, ref _edges);
                    _n0 = _n1;
                    if (_n0.adjacents.Count == 1)
                    {
                        _n1 = EdgeGraphUtility.GetNode(_n0.adjacents[0], ref _nodes);
                    }
                }

                while (_n0.adjacents.Count == 1)
                {
                    primitive.nodes.Add(_n0);
                    _n1 = EdgeGraphUtility.GetNode(_n0.adjacents[0], ref _nodes);
                    EdgeGraphUtility.RemoveNodeAndCleanAdjacents(_n0, ref _nodes, ref _edges);
                    Edge _e = EdgeGraphUtility.RemoveEdgeAndCleanAdjacents(_n0, _n1, ref _nodes, ref _edges);
                    if (_e != null)
                    {
                        primitive.edges.Add(_e);
                    }
                    _n0 = _n1;
                }

                primitive.nodes.Add(_n0);
                if (_n0.adjacents.Count == 0)
                {
                    EdgeGraphUtility.RemoveNodeAndCleanAdjacents(_n0, ref _nodes, ref _edges);
                }

                _primitives.Add(primitive);
            }
        }