public Connection(ConvexPolyForm startPoly, EdgeIndex startEdge, ConvexPolyForm endPoly, EdgeIndex endEdge)
 {
     StartPoly = startPoly;
     StartEdgeIndex = startEdge;
     EndPoly = endPoly;
     EndEdgeIndex = endEdge;
 }
Exemple #2
0
 public Connection(ConvexPolyForm startPoly, EdgeIndex startEdge, ConvexPolyForm endPoly, EdgeIndex endEdge)
 {
     StartPoly      = startPoly;
     StartEdgeIndex = startEdge;
     EndPoly        = endPoly;
     EndEdgeIndex   = endEdge;
 }
Exemple #3
0
        private void Render(ConvexPolyForm poly)
        {
            if (poly == _selectedPoly)
            {
                GLUtil.SetColor(new Color(0, 0, 0, 1));
            }
            else
            {
                GLUtil.SetColor(new Color(0.1f, 0.1f, 0.1f, 0.5f));
            }
            PolyDrawer.RenderPoly(poly);

            RenderNormals(poly);
            RenderCentroid(poly);


            if (poly == _selectedPoly)
            {
                RenderNearestEdge(poly);
                RenderVerts(poly);
            }

            if (_editState == EditState.ConnectPressed)
            {
                GLUtil.SetColor(new Color(0, 0, 0, 1));
                PrimitiveDrawer.DrawLine2d(_connectStart, _input.Mouse.Position);
                RenderCrossedEdges();
            }
        }
Exemple #4
0
        private void CheckForConnection()
        {
            if (_connectIntersectedEdges.Count != 2)
            {
                // Connections should be between two edges.
                return;
            }

            var start = _connectIntersectedEdges[0];
            var end   = _connectIntersectedEdges[1];

            ConvexPolyForm startPoly = start.Item1;
            ConvexPolyForm endPoly   = end.Item1;

            if (startPoly == endPoly)
            {
                // Edges should be between two different polygons
                return;
            }

            // ok it's a valid connection.
            Connection connection = new Connection(startPoly, start.Item2, endPoly, end.Item2);

            _connections.Add(connection);
        }
Exemple #5
0
        public List <Point> GetPath(Point from, Point to)
        {
            List <Point> path = new List <Point>();
            // First find the polygon they're in
            // !* May want a little overlapping so all screen can be clicked.
            //    In that case this should check if all intersected options contain as same poly start/end
            ConvexPolyForm polyStart = FindPoly(from);
            ConvexPolyForm polyEnd   = FindPoly(to);

            if (polyStart == null || polyEnd == null)
            {
                return(path);
            }
            else if (polyStart == polyEnd)
            {
                path.Add(from);
                path.Add(to);
            }
            else if (polyStart != polyEnd)
            {
                // This does not need doing every time but it's easier to code if is recreated.
                var startEndNodes      = CreateNodeNetwork(polyStart, polyEnd, from, to);
                AStar <PathNode> astar = new AStar <PathNode>();
                astar.FindPath(startEndNodes.Item1, startEndNodes.Item2);
                astar.Path.Reverse();
                foreach (var node in astar.Path)
                {
                    path.Add(node.Position);
                }
            }


            return(path);
        }
Exemple #6
0
        private Tuple <PathNode, PathNode> CreateNodeNetwork(ConvexPolyForm polyStart, ConvexPolyForm polyEnd, Point from, Point to)
        {
            _nodeGraph.Clear();
            // Store a map poly -> node to make it simple to work out the connection nodes.
            Dictionary <ConvexPolyForm, PathNode> polyToNodeMap = new Dictionary <ConvexPolyForm, PathNode>();

            PathNode startNode = null;
            PathNode endNode   = null;

            // Create a node for the centroid of each polygon
            // Replace the postion of the start and end polygon.
            foreach (ConvexPolyForm poly in _polygons)
            {
                Point    position;
                PathNode node;
                if (polyStart == poly)
                {
                    position  = from;
                    node      = new PathNode(position);
                    startNode = node;
                }
                else if (polyEnd == poly)
                {
                    position = to;
                    node     = new PathNode(position);
                    endNode  = node;
                }
                else
                {
                    position = poly.GetCentroid();
                    node     = new PathNode(position);
                }


                _nodeGraph.Add(node);
                polyToNodeMap.Add(poly, node);
            }

            // Create the edge nodes and add the links
            // !* This is where you'd add several nodes per edge, if you wanted.
            foreach (Connection connection in _connections)
            {
                LineSegment line     = connection.GetShortestEdge();
                Point       midPoint = line.GetMiddle();

                PathNode connectionNode = new PathNode(midPoint);

                // Add bidirectional links to connected polys and edge polys.
                polyToNodeMap[connection.StartPoly].Neighbours.Add(connectionNode);
                connectionNode.Neighbours.Add(polyToNodeMap[connection.StartPoly]);

                polyToNodeMap[connection.EndPoly].Neighbours.Add(connectionNode);
                connectionNode.Neighbours.Add(polyToNodeMap[connection.EndPoly]);

                _nodeGraph.Add(connectionNode);
            }

            return(new Tuple <PathNode, PathNode>(startNode, endNode));
        }
 internal static void RenderPoly(ConvexPolyForm poly)
 {
     Gl.glBegin(Gl.GL_LINE_STRIP);
     {
         foreach (Point p in poly.Vertices)
         {
             GLUtil.DrawPointVertex(p);
         }
         GLUtil.DrawPointVertex(poly.Vertices.First());
     }
     Gl.glEnd();
 }
 internal static void RenderPoly(ConvexPolyForm poly)
 {
     Gl.glBegin(Gl.GL_LINE_STRIP);
     {
         foreach (Point p in poly.Vertices)
         {
             GLUtil.DrawPointVertex(p);
         }
         GLUtil.DrawPointVertex(poly.Vertices.First());
     }
     Gl.glEnd();
 }
Exemple #9
0
        private void FindNearestPolygon(Point mousePos)
        {
            float minDistance = float.MaxValue;

            foreach (ConvexPolyForm poly in _convexPolyForm)
            {
                float distance = poly.GetClosestEdgeDistance(mousePos);
                if (distance < minDistance)
                {
                    minDistance   = distance;
                    _selectedPoly = poly;
                }
            }
        }
Exemple #10
0
        private void RenderCrossedEdges()
        {
            foreach (var tuple in _connectIntersectedEdges)
            {
                ConvexPolyForm poly = tuple.Item1;
                EdgeIndex      edge = tuple.Item2;

                Point start = poly.Vertices[edge.Start];
                Point end   = poly.Vertices[edge.End];

                GLUtil.SetColor(new Color(0, 0, 1, 1));
                PrimitiveDrawer.DrawLine2d(start, end);
            }
        }
Exemple #11
0
        private void RenderVerts(ConvexPolyForm poly)
        {
            for (int i = 0; i < poly.Vertices.Count; i++)
            {
                Point p = poly.Vertices[i];
                PrimitiveDrawer.DrawCircle(new Circle(p.X, p.Y, 16), new Color(0, 0, 0, 1));

                if (i == _selectedVert && _editState == EditState.VertexPressed)
                {
                    PrimitiveDrawer.DrawFilledCircle(p, 15, new Color(0, 1, 1, 1));
                }
                else
                {
                    PrimitiveDrawer.DrawFilledCircle(p, 15, new Color(1, 1, 1, 1));
                }
            }
        }
Exemple #12
0
        private void RenderNormals(ConvexPolyForm poly)
        {
            GLUtil.SetColor(new Color(0, 1, 0, 1));

            foreach (EdgeIndex edge in poly.Edges)
            {
                Point start  = poly.Vertices[edge.Start];
                Point end    = poly.Vertices[edge.End];
                Point middle = LineSegment.GetMiddle(start, end);
                Point normal = EdgeIndex.GetLineNormal(start, end);
                Gl.glBegin(Gl.GL_LINES);
                {
                    GLUtil.DrawPointVertex(middle);
                    GLUtil.DrawPointVertex(new Point(middle.X + (normal.X * 50), middle.Y + (normal.Y * 50)));
                }
                Gl.glEnd();
            }
        }
Exemple #13
0
        private void RenderNearestEdge(ConvexPolyForm poly)
        {
            EdgeIndex edge = poly.GetClosestEdge(_input.Mouse.Position);

            GLUtil.SetColor(new Color(1, 0, 0, 1));
            Gl.glBegin(Gl.GL_LINE_STRIP);
            {
                GLUtil.DrawPointVertex(poly.Vertices[edge.Start]);
                GLUtil.DrawPointVertex(poly.Vertices[edge.End]);
            }
            Gl.glEnd();

            // Render closest point
            Point p = EdgeIndex.GetClosestPoint(poly.Vertices[edge.Start], poly.Vertices[edge.End], _input.Mouse.Position);

            Gl.glBegin(Gl.GL_POINTS);
            {
                GLUtil.DrawPointVertex(p);
            }
            Gl.glEnd();
        }
        private void Render(ConvexPolyForm poly)
        {
            if (poly == _selectedPoly)
            {
                GLUtil.SetColor(new Color(0, 0, 0, 1));
            }
            else
            {
                GLUtil.SetColor(new Color(0.1f, 0.1f, 0.1f, 0.5f));
            }
            PolyDrawer.RenderPoly(poly);

            RenderNormals(poly);
            RenderCentroid(poly);

            if (poly == _selectedPoly)
            {
                RenderNearestEdge(poly);
                RenderVerts(poly);
            }

            if (_editState == EditState.ConnectPressed)
            {
                GLUtil.SetColor(new Color(0, 0,0,1));
                PrimitiveDrawer.DrawLine2d(_connectStart, _input.Mouse.Position);
                RenderCrossedEdges();
            }
        }
Exemple #15
0
 private void RenderCentroid(ConvexPolyForm poly)
 {
     PrimitiveDrawer.DrawFilledCircle(poly.GetCentroid(), 15, new Color(1, 1, 0, 1));
 }
        private void RenderVerts(ConvexPolyForm poly)
        {
            for (int i = 0; i < poly.Vertices.Count; i++)
            {
                Point p = poly.Vertices[i];
                PrimitiveDrawer.DrawCircle(new Circle(p.X, p.Y, 16), new Color(0, 0, 0, 1));

                if (i == _selectedVert && _editState == EditState.VertexPressed)
                {

                    PrimitiveDrawer.DrawFilledCircle(p, 15, new Color(0, 1, 1, 1));
                }
                else
                {
                    PrimitiveDrawer.DrawFilledCircle(p, 15, new Color(1, 1, 1, 1));
                }
            }
        }
        private void RenderNormals(ConvexPolyForm poly)
        {
            GLUtil.SetColor(new Color(0, 1, 0, 1));

            foreach (EdgeIndex edge in poly.Edges)
            {
                Point start = poly.Vertices[edge.Start];
                Point end = poly.Vertices[edge.End];
                Point middle = LineSegment.GetMiddle(start, end);
                Point normal = EdgeIndex.GetLineNormal(start, end);
                Gl.glBegin(Gl.GL_LINES);
                {
                    GLUtil.DrawPointVertex(middle);
                    GLUtil.DrawPointVertex(new Point(middle.X + (normal.X*50), middle.Y + (normal.Y*50)));
                }
                Gl.glEnd();
            }
        }
        private void RenderNearestEdge(ConvexPolyForm poly)
        {
            EdgeIndex edge = poly.GetClosestEdge(_input.Mouse.Position);
            GLUtil.SetColor(new Color(1, 0, 0, 1));
            Gl.glBegin(Gl.GL_LINE_STRIP);
            {
                GLUtil.DrawPointVertex(poly.Vertices[edge.Start]);
                GLUtil.DrawPointVertex(poly.Vertices[edge.End]);
            }
            Gl.glEnd();

            // Render closest point
            Point p = EdgeIndex.GetClosestPoint(poly.Vertices[edge.Start], poly.Vertices[edge.End], _input.Mouse.Position);
            Gl.glBegin(Gl.GL_POINTS);
            {
                GLUtil.DrawPointVertex(p);
            }
            Gl.glEnd();
        }
 private void RenderCentroid(ConvexPolyForm poly)
 {
     PrimitiveDrawer.DrawFilledCircle(poly.GetCentroid(), 15, new Color(1,1, 0,1 ));
 }
 private void FindNearestPolygon(Point mousePos)
 {
     float minDistance = float.MaxValue;
     foreach (ConvexPolyForm poly in _convexPolyForm)
     {
         float distance = poly.GetClosestEdgeDistance(mousePos);
         if (distance < minDistance)
         {
             minDistance = distance;
             _selectedPoly = poly;
         }
     }
 }