Exemplo n.º 1
0
        /// <summary>
        /// Creates IObstacle objects of this figure.
        /// </summary>
        /// <returns></returns>
        public virtual void Tokenize(ObstacleCollection obstacles)
        {
            if (Lines.Count == 0)
            {
                return;
            }
            GetForlorn(obstacles);
            if (Lines.Count == 0)
            {
                return;
            }

            if (SwitchPoints.Count > 0)
            {
                var polygons = SplitPolygon(Lines, obstacles);
                MergePolygons(polygons, obstacles);
            }
            else
            {
                var polygon = CreatePolygon(Lines);
                if (polygon.CheckIfFilled(Map, Layer, obstacles))
                {
                    AddPolygonObstacle(polygon, VerticesEx.IsRectangle(polygon), obstacles, Layer);
                }
                else
                {
                    AddLineObstacles(polygon, obstacles, Layer);
                }
            }
        }
Exemplo n.º 2
0
 public bool Contains(Vector2 point)
 {
     return(VerticesEx.IsPointInPolygonOrEdge(Vertices, point));
 }
Exemplo n.º 3
0
        protected virtual void MergePolygons(IEnumerable <Polygon> polygons, ObstacleCollection obstacles)
        {
            var filledPolygons = new List <Vertices>();
            var mergedPolygons = new List <Vertices>();

            foreach (var polygon in polygons)
            {
                if (polygon.CheckIfFilled(Map, Layer, obstacles))
                {
                    filledPolygons.Add(polygon);
                }
                else
                {
                    AddLineObstacles(polygon, obstacles, Layer);
                }
            }

            while (filledPolygons.Count > 0)
            {
                var polygon = filledPolygons.First();
                var changed = true;
                while (changed)
                {
                    changed = false;
                    var index = 0;
                    while (index < filledPolygons.Count)
                    {
                        var polygon2         = filledPolygons[index];
                        var error            = PolyClipError.None;
                        var combinedPolygons = polygon == polygon2 ? new List <Vertices> {
                            polygon
                        } : YuPengClipper.Union(polygon2, polygon, out error);
                        if (combinedPolygons.Count == 0)
                        {
                            //sometimes a polygon is a subset of the other polygon. Try to check that...
                            Vertices biggerPolygon;
                            if (VerticesEx.IsPolygonSubsetOf(polygon, polygon2, out biggerPolygon))
                            {
                                combinedPolygons = new List <Vertices> {
                                    biggerPolygon
                                };
                                error = PolyClipError.None; //we could recover it...
                            }
                        }
                        //if (combinedPolygons.Count > 2)
                        //    //the polygons intersect at several points, we ignore them as this would lead to holes...
                        //if (error != PolyClipError.None)
                        //    //some error occurred, so we igore this union process...
                        if (combinedPolygons.Count == 1) //they intersect
                        {
                            filledPolygons.Remove(polygon2);
                            changed = true;
                            polygon = SimplifyTools.CollinearSimplify(combinedPolygons[0]);
                        }
                        else //combinedPolygons.Count > 1 --> they don't intersect (or may do at several points, but we ignore it )
                        {
                            index++;
                        }
                    }
                }
                mergedPolygons.Add(polygon);
            }

            foreach (var mergedPolygon in mergedPolygons)
            {
                AddPolygonObstacle(mergedPolygon, VerticesEx.IsRectangle(mergedPolygon), obstacles, Layer);
            }
        }
Exemplo n.º 4
0
        public override void Tokenize(ObstacleCollection obstacles)
        {
            var polygons     = SplitPolygon(Lines, obstacles);
            var blocks       = Polygon.GetAssociatedBlocks(polygons, Map, Layer).ToList();
            var polygonEdges = new List <LineSegment>();

            foreach (var polygon in polygons)
            {
                for (int i = 0, j = polygon.Count - 1; i < polygon.Count; j = i++)
                {
                    polygonEdges.Add(GetLine(polygon[i], polygon[j], false, Lines));
                }
            }
            var intersectionLines = polygonEdges.GroupBy(x => x).Where(group => group.Count() > 1).Select(group => group.Key).ToList(); //finds duplicated items

            var blockLines = new Dictionary <Block, List <LineSegment> >();                                                             //create a dictionary of block which intersection line they belong to. Because if the intersection line does not match,
                                                                                                                                        //all intersecting lines of that block must be set invalid. Otherwise they engine would not not create a separate polygon.
            var mismatchBlocks = new List <Block>();

            foreach (var intersectionLine in intersectionLines)
            {
                Block       blockX;
                Block       blockY;
                Orientation orientation;
                GetNeighborBlocks(intersectionLine, blocks, out blockX, out blockY, out orientation);
                if (BlocksMatch(blockX, blockY, orientation))
                {
                    Lines.Remove(intersectionLine);
                }
                else
                {
                    if (blockX != null & !mismatchBlocks.Contains(blockX))
                    {
                        mismatchBlocks.Add(blockX);
                    }
                    if (blockY != null & !mismatchBlocks.Contains(blockY))
                    {
                        mismatchBlocks.Add(blockY);
                    }
                }
                AddBlockLines(blockLines, blockX, intersectionLine);
                AddBlockLines(blockLines, blockY, intersectionLine);
            }
            foreach (var mismatchBlock in mismatchBlocks)
            {
                //Restore all lines of the mismatched blocks
                List <LineSegment> blockLineSegments;
                if (!blockLines.TryGetValue(mismatchBlock, out blockLineSegments))
                {
                    continue;
                }
                foreach (var blockSegment in blockLineSegments.Where(blockSegment => !Lines.Contains(blockSegment)))
                {
                    Lines.Add(blockSegment);
                }
            }

            polygons.Clear();
            polygons = SplitPolygon(Lines, obstacles);
            foreach (var polygon in polygons)
            {
                var simplePolygon = SimplifyTools.CollinearSimplify(polygon);
                AddSlopeObstacle(simplePolygon, VerticesEx.IsRectangle(simplePolygon), obstacles, Layer);
            }
        }