Exemple #1
0
        /// <summary>
        /// Creates a Graph by a set of boundary and internal polygons.
        /// </summary>
        /// <param name="boundaries">Boundary polygons</param>
        /// <param name="internals">Internal polygons</param>
        /// <returns name="baseGraph">Base graph</returns>
        public static BaseGraph ByBoundaryAndInternalPolygons(List <Polygon> boundaries, [DefaultArgument("[]")] List <Polygon> internals)
        {
            if (boundaries == null)
            {
                throw new NullReferenceException("boundaryPolygons");
            }
            if (internals == null)
            {
                throw new NullReferenceException("internalPolygons");
            }
            List <gPolygon> input = new List <gPolygon>();

            foreach (Polygon pol in boundaries)
            {
                var      vertices = pol.Points.Select(pt => gVertex.ByCoordinates(pt.X, pt.Y, pt.Z)).ToList();
                gPolygon gPol     = gPolygon.ByVertices(vertices, true);
                input.Add(gPol);
            }

            foreach (Polygon pol in internals)
            {
                var      vertices = pol.Points.Select(pt => gVertex.ByCoordinates(pt.X, pt.Y, pt.Z)).ToList();
                gPolygon gPol     = gPolygon.ByVertices(vertices, false);
                input.Add(gPol);
            }

            return(new BaseGraph(input));
        }
        public void VisibilityFromPointColinearVerticesXAxis()
        {
            var a = gVertex.ByCoordinates(-20, -20);
            var b = gVertex.ByCoordinates(a.X, a.Y - 5);
            var c = gVertex.ByCoordinates(b.X, b.X - 10);
            var d = gVertex.ByCoordinates(c.X + 10, c.Y);
            var e = gVertex.ByCoordinates(d.X, b.Y);
            var f = gVertex.ByCoordinates(e.X + 5, e.Y);
            var g = gVertex.ByCoordinates(f.X, d.Y);
            var h = gVertex.ByCoordinates(g.X + 10, g.Y);
            var i = gVertex.ByCoordinates(h.X, f.Y);
            var j = gVertex.ByCoordinates(i.X, a.Y);

            gPolygon polygon = gPolygon.ByVertices(new List <gVertex>()
            {
                a, b, c, d, e, f, g, h, i, j
            }, true);
            Graph baseGraph = new Graph(new List <gPolygon>()
            {
                polygon
            });
            List <gVertex> vertices = VisibilityGraph.VertexVisibility(i, baseGraph);

            Assert.NotNull(vertices);
        }
Exemple #3
0
        /// <summary>
        /// Method to check if a polygon contains a point.
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public static bool ContainsPoint(Polygon polygon, Point point)
        {
            gPolygon gPol   = gPolygon.ByVertices(polygon.Points.Select(p => gVertex.ByCoordinates(p.X, p.Y, p.Z)).ToList());
            gVertex  vertex = gVertex.ByCoordinates(point.X, point.Y, point.Z);

            return(gPol.ContainsVertex(vertex));
        }
        internal static bool PolygonContainsPoint(Polygon polygon, DSPoint point)
        {
            gVertex  vertex   = Points.ToVertex(point);
            var      vertices = polygon.Points.Select(p => Points.ToVertex(p)).ToList();
            gPolygon gPolygon = gPolygon.ByVertices(vertices, false);

            return(gPolygon.ContainsVertex(vertex));
        }
Exemple #5
0
        // TODO: Seems that Q gets corrupted somehow. Check with Tests
        internal SweepLine(gPolygon subject, gPolygon clip, SweepLineType type)
        {
            this.sweepLineType = type;
            this.subject       = subject;
            this.clip          = clip;
            var totalEdges = subject.Edges.Count + clip.Edges.Count;

            this.eventsList   = new List <SweepEvent>(totalEdges * 2);
            this.activeEvents = new List <SweepEvent>(totalEdges);

            subject.Edges.ForEach(e => this.AddNewEvent(e, PolygonType.Subject));
            clip.Edges.ForEach(e => this.AddNewEvent(e, PolygonType.Clip));
        }
Exemple #6
0
        /// <summary>
        /// Creates a graph by a set of closed polygons
        /// </summary>
        /// <param name="polygons">Polygons</param>
        /// <returns name="baseGraph">Base graph</returns>
        public static BaseGraph ByPolygons(List <Polygon> polygons)
        {
            if (polygons == null)
            {
                throw new NullReferenceException("polygons");
            }
            List <gPolygon> input = new List <gPolygon>();

            foreach (Polygon pol in polygons)
            {
                var      vertices = pol.Points.Select(pt => gVertex.ByCoordinates(pt.X, pt.Y, pt.Z)).ToList();
                gPolygon gPol     = gPolygon.ByVertices(vertices, false);
                input.Add(gPol);
            }

            return(new BaseGraph(input));
        }
Exemple #7
0
        /// <summary>
        /// Computes edges and creates polygons from those connected by vertices.
        /// </summary>
        public void BuildPolygons()
        {
            var computedVertices = new List <gVertex>();

            foreach (gVertex v in vertices)
            {
                // If already belongs to a polygon or is not a polygon vertex or already computed
                if (computedVertices.Contains(v) || v.polygonId >= 0 || graph[v].Count > 2)
                {
                    continue;
                }

                computedVertices.Add(v);
                gPolygon polygon = new gPolygon(GetNextId(), false);

                polygon.AddVertex(v);
                foreach (gEdge edge in GetVertexEdges(v))
                {
                    gEdge   nextEdge   = edge;
                    gVertex nextVertex = edge.GetVertexPair(v);
                    while (!polygon.vertices.Contains(nextVertex))
                    {
                        computedVertices.Add(nextVertex);
                        polygon.AddVertex(nextVertex);
                        polygon.edges.Add(nextEdge);

                        //It is extreme vertex, polygon not closed
                        if (graph[nextVertex].Count < 2)
                        {
                            break;
                        }

                        nextEdge   = graph[nextVertex].Where(e => !e.Equals(nextEdge)).First();
                        nextVertex = nextEdge.GetVertexPair(nextVertex);
                    }
                    if (!polygon.edges.Last().Equals(nextEdge))
                    {
                        polygon.edges.Add(nextEdge);
                    }
                }
                this.polygons.Add(polygon.id, polygon);
            }
        }
        public static VisibilityGraph Merge(List <VisibilityGraph> graphs)
        {
            Graph        graph = new Graph();
            List <gEdge> edges = new List <gEdge>();

            foreach (VisibilityGraph g in graphs)
            {
                Dictionary <int, int> oldNewIds = new Dictionary <int, int>();
                foreach (gPolygon p in g.baseGraph.polygons.Values)
                {
                    int nextId = graph.GetNextId();
                    oldNewIds.Add(p.id, nextId);
                    gPolygon polygon = (gPolygon)p.Clone();
                    polygon.id = nextId;
                    graph.polygons.Add(nextId, polygon);
                }

                foreach (gEdge e in g.edges)
                {
                    gVertex start = (gVertex)e.StartVertex.Clone();
                    gVertex end   = (gVertex)e.EndVertex.Clone();
                    //start.polygonId = oldNewIds[start.polygonId];
                    //end.polygonId = oldNewIds[end.polygonId];
                    edges.Add(gEdge.ByStartVertexEndVertex(start, end));
                }
            }

            VisibilityGraph visibilityGraph = new VisibilityGraph()
            {
                baseGraph = new Graph(graph.polygons.Values.ToList()),
            };

            foreach (gEdge edge in edges)
            {
                visibilityGraph.AddEdge(edge);
            }

            return(visibilityGraph);
        }
        public void VisibilityFromPointColinearVerticesYAxis()
        {
            var a = gVertex.ByCoordinates(0, 0);
            var b = gVertex.ByCoordinates(a.X + 20, a.Y);
            var c = gVertex.ByCoordinates(b.X, b.Y + 10);
            var d = gVertex.ByCoordinates(c.X - 10, c.Y);
            var e = gVertex.ByCoordinates(d.X, d.Y + 10);
            var f = gVertex.ByCoordinates(c.X, e.Y);
            var g = gVertex.ByCoordinates(f.X, d.Y + 10);
            var h = gVertex.ByCoordinates(a.X, g.Y);

            gPolygon polygon = gPolygon.ByVertices(new List <gVertex>()
            {
                a, b, c, d, e, f, g, h
            }, true);
            Graph baseGraph = new Graph(new List <gPolygon>()
            {
                polygon
            });

            List <gVertex> vertices = VisibilityGraph.VertexVisibility(b, baseGraph);

            Assert.NotNull(vertices);
        }
Exemple #10
0
 public static SweepLine BySubjectClipPolygons(gPolygon subject, gPolygon clip)
 {
     return(new SweepLine(subject, clip, SweepLineType.Boolean));
 }
Exemple #11
0
        public Graph(List <gPolygon> gPolygonsSet)
        {
            edges   = new List <gEdge>();
            graphID = Guid.NewGuid();
            //Setting up Graph instance by adding vertices, edges and polygons
            foreach (gPolygon gPolygon in gPolygonsSet)
            {
                List <gVertex> vertices = gPolygon.vertices;

                // Clear pre-existing edges in the case this is an updating process.
                gPolygon.edges.Clear();

                //If there is only one polygon, treat it as boundary
                if (gPolygonsSet.Count() == 1)
                {
                    gPolygon.isBoundary = true;
                }

                //If first and last point of vertices list are the same, remove last.
                if (vertices.First().Equals(vertices.Last()) && vertices.Count() > 1)
                {
                    vertices = vertices.Take(vertices.Count() - 1).ToList();
                }

                //For each point, creates vertex and associated edge and adds them
                //to the polygons Dictionary
                int vertexCount = vertices.Count();

                // If valid polygon
                if (vertexCount >= 3)
                {
                    int newId = GetNextId();
                    for (var j = 0; j < vertexCount; j++)
                    {
                        int     next_index  = (j + 1) % vertexCount;
                        gVertex vertex      = vertices[j];
                        gVertex next_vertex = vertices[next_index];
                        gEdge   edge        = new gEdge(vertex, next_vertex);

                        //If is a valid polygon, add id to vertex and
                        //edge to vertices dictionary
                        if (vertexCount > 2)
                        {
                            vertex.polygonId      = newId;
                            next_vertex.polygonId = newId;
                            gPolygon gPol = new gPolygon();
                            if (polygons.TryGetValue(newId, out gPol))
                            {
                                gPol.edges.Add(edge);
                            }
                            else
                            {
                                gPolygon.edges.Add(edge);
                                gPolygon.id = newId;
                                polygons.Add(newId, gPolygon);
                            }
                        }
                        AddEdge(edge);
                    }
                }
            }
        }