Exemplo n.º 1
0
        public List <Vertex> GetBoundary()
        {
            BinaryMap bm     = new BinaryMap(this.Settings);
            var       result = new List <Vertex>();

            for (int i = 0; i < SizeX; i++)
            {
                for (int j = 0; j < SizeY; j++)
                {
                    int value = Get4Neighbours(i, j);
                    bm.SetBinaryValue(i, j, value);
                }
            }

            // bm.WriteToFile(@"c:\temp\contour.txt");

            GetContinousBoundaryPoints(bm);

            double stepX = Math.Abs((Max.x - Min.x) / SizeX);
            double stepY = Math.Abs((Max.y - Min.y) / SizeY);

            foreach (Vertex vertex in BoundaryVertices)
            {
                double x = Min.x + stepX * vertex.I;
                double y = Min.y + stepY * vertex.J;
                result.Add(new Vertex(x, y));
            }

            return(result);
        }
Exemplo n.º 2
0
        public static List <Vertex> GetOuterContour(IReadOnlyCollection <Contour> contours,
                                                    int gridResolution)
        {
            List <Vertex> outerVertices = new List <Vertex>();

            var convexHull  = GetConvexHull(contours);
            var hullPolygon = convexHull.GetHullPolygon();

            ConcurrentBag <BinaryMap> Maps     = new ConcurrentBag <BinaryMap>();
            BinaryMapSettings         settings = new BinaryMapSettings
            {
                SizeX       = gridResolution,
                SizeY       = gridResolution,
                BoundingBox = hullPolygon.GetBounds()
            };

            for (int i = 0; i < contours.Count; i++)
            {
                BinaryMap bm = new BinaryMap(settings);
                bm.SetContour(contours.ElementAt(i).ToVector2dList());
                bm.Id = i;
                Maps.Add(bm);
            }

            BinaryMap refMap = Maps.FirstOrDefault();

            foreach (BinaryMap map in Maps)
            {
                refMap?.OR(map);
            }
            outerVertices = refMap?.GetBoundary();
            return(outerVertices);
        }
Exemplo n.º 3
0
 /// <summary>
 /// OR operation on Binarymaps
 /// </summary>
 /// <param name="map"></param>
 public void OR(BinaryMap map)
 {
     for (int i = 0; i < SizeX; i++)
     {
         for (int j = 0; j < SizeY; j++)
         {
             _binaryMap[i, j] = _binaryMap[i, j] | map._binaryMap[i, j];
         }
     }
 }
Exemplo n.º 4
0
        private Vertex SearchForNextNeighbour(Vertex currentVertex, BinaryMap bm)
        {
            try
            {
                int i0 = currentVertex.I;
                int j0 = currentVertex.J;

                foreach (Offset offset in this._offsets)
                {
                    var value = bm.GetValue(i0 + offset.DeltaI, j0 + offset.DeltaJ);
                    if (value == 1 || value == 2)
                    {
                        if (_trackingMap[i0 + offset.DeltaI, j0 + offset.DeltaJ] == 0)
                        {
                            _trackingMap[i0 + offset.DeltaI, j0 + offset.DeltaJ] = 1;
                        }
                        else
                        {
                            continue;
                        }

                        var vertex = new Vertex(i0 + offset.DeltaI, j0 + offset.DeltaJ);
                        //if (BoundaryVertices.Contains(vertex))
                        //    continue;
                        //Trace.WriteLine($"BPoint = ({i0 + offset.DeltaI}, {j0 + offset.DeltaJ}");
                        BoundaryVertices.Add(vertex);

                        return(vertex);
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }
            return(new Vertex(0, 0));
        }
Exemplo n.º 5
0
        void GetContinousBoundaryPoints(BinaryMap bm)
        {
            //  +----------+----------+----------+
            //  |          |          |          |
            //  |(x-1,y-1) | (x,y-1)  |(x+1,y-1) |
            //  +----------+----------+----------+
            //  |(x-1,y)   |  (x,y)   |(x+1,y)   |
            //  |          |          |          |
            //  +----------+----------+----------+
            //  |          | (x,y+1)  |(x+1,y+1) |
            //  |(x-1,y+1) |          |          |
            //  +----------+----------+----------+

            this._offsets = new List <Offset>
            {
                new Offset()
                {
                    DeltaI = -1, DeltaJ = 1
                },
                new Offset()
                {
                    DeltaI = -1, DeltaJ = 0
                },
                new Offset()
                {
                    DeltaI = -1, DeltaJ = -1
                },
                new Offset()
                {
                    DeltaI = 0, DeltaJ = -1
                },
                new Offset()
                {
                    DeltaI = 0, DeltaJ = 1
                },
                new Offset()
                {
                    DeltaI = 1, DeltaJ = 1
                },
                new Offset()
                {
                    DeltaI = 1, DeltaJ = 0
                },
                new Offset()
                {
                    DeltaI = 1, DeltaJ = -1
                },
            };

            StartVertex = bm.FindStart();
            BoundaryVertices.Add(StartVertex);
            _trackingMap[StartVertex.I, StartVertex.J] = 1;
            Vertex tmp   = null;
            bool   bStop = false;

            while (bStop == false)
            {
                if (tmp == null)
                {
                    tmp = StartVertex;
                }
                var vertex = SearchForNextNeighbour(tmp, bm);

                if (vertex.I == 0 && vertex.J == 0)
                {
                    bStop = true;
                    BoundaryVertices.Add(StartVertex);
                }
                tmp = vertex;
            }
        }