Beispiel #1
0
        /// <summary>
        /// Create a polygon with vertex id's and next chain. Can contain holes.
        /// </summary>
        /// <param name="vertexCoordinates">the coordinates</param>
        /// <param name="vertexIds">the vertex ids</param>
        /// <param name="nextIndices">the next index in vertexIds. Must be same length as vertexIds</param>
        /// <param name="fusionVertices">Vertices that are used in more than one subpolygon. Can be null.</param>
        /// <returns>a polygon</returns>
        public static Polygon FromVertexList(Vertex[] vertexCoordinates, IEnumerable <int> vertexIds, IEnumerable <int> nextIndices, IEnumerable <int> polygonIds, IReadOnlyList <int> fusionVertices)
        {
            var vertexIdCollection  = vertexIds as IReadOnlyCollection <int> ?? vertexIds.ToArray();
            var polygonIdCollection = polygonIds as IList <int> ?? polygonIds.ToArray();
            var vertexToChain       = Enumerable.Repeat(-1, vertexCoordinates.Length).ToArray();
            var vertexChain         = new VertexChain[vertexIdCollection.Count];
            var polygonStartIndex   = new List <int>();

            var i = 0;

            foreach (var(vertexId, nextId) in vertexIdCollection.Zip(nextIndices, Tuple.Create))
            {
                vertexChain[i].VertexId        = vertexId;
                vertexChain[i].SameVertexChain = vertexToChain[vertexId];
                vertexChain[i].SubPolygonId    = polygonIdCollection[i];
                SetNext(vertexChain, i, nextId);

                if (polygonIdCollection[i] >= polygonStartIndex.Count)
                {
                    polygonStartIndex.Add(i);
                }

                vertexToChain[vertexId] = i;
                i++;
            }

            var polygon = new Polygon(vertexCoordinates, vertexChain, vertexToChain, polygonStartIndex);

            polygon.FusionVerticesIntoChain(fusionVertices);
            return(polygon);
        }
Beispiel #2
0
        /// <summary>
        /// create a polygon from multiple polygon lines
        /// </summary>
        /// <param name="vertexCoordinates">the vertex coordinates</param>
        /// <param name="lines">The polygon lines. Each vertex Id must be unique inside a polygon line.</param>
        /// <param name="fusionVertices">vertices that are used in more than one subpolygon</param>
        /// <returns>a polygon</returns>
        public static Polygon FromPolygonLines(Vertex[] vertexCoordinates, IReadOnlyCollection <int>[] lines, IReadOnlyList <int> fusionVertices)
        {
            var vertexChain   = new VertexChain[lines.Sum(x => x.Count)];
            var id            = 0;
            var first         = id;
            var vertexToChain = Enumerable.Repeat(-1, vertexCoordinates.Length).ToArray();
            var subPolygones  = new List <int>();

            foreach (var line in lines)
            {
                var polygonId = subPolygones.Count;
                subPolygones.Add(id);

                foreach (var vertexId in line)
                {
                    vertexChain[id].VertexId     = vertexId;
                    vertexChain[id].SubPolygonId = polygonId;

                    vertexChain[id].SameVertexChain = vertexToChain[vertexId];
                    vertexToChain[vertexId]         = id;

                    SetNext(vertexChain, id, id == vertexChain.Length - 1 ? first : id + 1);
                    id++;
                }

                SetNext(vertexChain, id - 1, first);
                first = id;
            }

            var polygon = new Polygon(vertexCoordinates, vertexChain, vertexToChain, subPolygones);

            polygon.FusionVerticesIntoChain(fusionVertices);
            return(polygon);
        }