Example #1
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);
            }
        }