Beispiel #1
0
        static void Run()
        {
            float maxValue = 600;

            #region create random points in the range of [0, maxValue]
            PointF[] pts = new PointF[20];
            Random   r   = new Random((int)(DateTime.Now.Ticks & 0x0000ffff));
            for (int i = 0; i < pts.Length; i++)
            {
                pts[i] = new PointF((float)r.NextDouble() * maxValue, (float)r.NextDouble() * maxValue);
            }
            #endregion

            Triangle2DF[]  delaunayTriangles;
            VoronoiFacet[] voronoiFacets;
            using (PlanarSubdivision subdivision = new PlanarSubdivision(pts))
            {
                //Obtain the delaunay's triangulation from the set of points;
                delaunayTriangles = subdivision.GetDelaunayTriangles();

                //Obtain the voronoi facets from the set of points
                voronoiFacets = subdivision.GetVoronoiFacets();
            }

            //create an image for display purpose
            Image <Bgr, Byte> img = new Image <Bgr, byte>((int)maxValue, (int)maxValue);

            //Draw the voronoi Facets
            foreach (VoronoiFacet facet in voronoiFacets)
            {
                Point[] polyline = Array.ConvertAll <PointF, Point>(facet.Vertices, Point.Round);

                //Draw the facet in color
                img.FillConvexPoly(
                    polyline,
                    new Bgr(r.NextDouble() * 120, r.NextDouble() * 120, r.NextDouble() * 120)
                    );

                //highlight the edge of the facet in black
                img.DrawPolyline(polyline, true, new Bgr(Color.Black), 2);

                //draw the points associated with each facet in red
                img.Draw(new CircleF(facet.Point, 5.0f), new Bgr(Color.Red), 0);
            }

            //Draw the Delaunay triangulation
            foreach (Triangle2DF triangles in delaunayTriangles)
            {
                img.Draw(triangles, new Bgr(Color.White), 1);
            }

            //display the image
            ImageViewer.Show(img, "Plannar Subdivision");
        }
Beispiel #2
0
      static void Run()
      {
         float maxValue = 600;

         #region create random points in the range of [0, maxValue]
         PointF[] pts = new PointF[20];
         Random r = new Random((int)(DateTime.Now.Ticks & 0x0000ffff));
         for (int i = 0; i < pts.Length; i++)
            pts[i] = new PointF((float)r.NextDouble() * maxValue, (float)r.NextDouble() * maxValue);
         #endregion

         Triangle2DF[] delaunayTriangles;
         VoronoiFacet[] voronoiFacets;
         using (PlanarSubdivision subdivision = new PlanarSubdivision(pts))
         {
            //Obtain the delaunay's triangulation from the set of points;
            delaunayTriangles = subdivision.GetDelaunayTriangles();

            //Obtain the voronoi facets from the set of points
            voronoiFacets = subdivision.GetVoronoiFacets();
         }

         //create an image for display purpose
         Image<Bgr, Byte> img = new Image<Bgr, byte>((int)maxValue, (int) maxValue);

         //Draw the voronoi Facets
         foreach (VoronoiFacet facet in voronoiFacets)
         {
            Point[] points = Array.ConvertAll<PointF, Point>(facet.Vertices, Point.Round);

            //Draw the facet in color
            img.FillConvexPoly(
                points,
                new Bgr(r.NextDouble() * 120, r.NextDouble() * 120, r.NextDouble() * 120)
                );

            //highlight the edge of the facet in black
            img.DrawPolyline(points, true, new Bgr(Color.Black), 2);

            //draw the points associated with each facet in red
            img.Draw(new CircleF(facet.Point, 5.0f), new Bgr(Color.Red), 0);
         }

         //Draw the Delaunay triangulation
         foreach (Triangle2DF triangles in delaunayTriangles)
         {
            img.Draw(triangles, new Bgr(Color.White), 1);
         }

         //display the image
         ImageViewer.Show(img, "Plannar Subdivision");
      }
        /// <summary>
        /// Create planar subdivision for random points
        /// </summary>
        /// <param name="maxValue">The points contains values between [0, maxValue)</param>
        /// <param name="pointCount">The total number of points to create</param>
        public static void CreateSubdivision(float maxValue, int pointCount, out Triangle2DF[] delaunayTriangles, out VoronoiFacet[] voronoiFacets)
        {
            #region create random points in the range of [0, maxValue]
            PointF[] pts = new PointF[pointCount];
            Random   r   = new Random((int)(DateTime.Now.Ticks & 0x0000ffff));
            for (int i = 0; i < pts.Length; i++)
            {
                pts[i] = new PointF((float)r.NextDouble() * maxValue, (float)r.NextDouble() * maxValue);
            }
            #endregion

            using (PlanarSubdivision subdivision = new PlanarSubdivision(pts))
            {
                //Obtain the delaunay's triangulation from the set of points;
                delaunayTriangles = subdivision.GetDelaunayTriangles();

                //Obtain the voronoi facets from the set of points
                voronoiFacets = subdivision.GetVoronoiFacets();
            }
        }
Beispiel #4
0
        public void TestPlanarSubdivision2()
        {
            PointF[] pts = new PointF[33];

             pts[0] = new PointF(224, 432);
             pts[1] = new PointF(368, 596);
             pts[2] = new PointF(316, 428);
             pts[3] = new PointF(244, 596);
             pts[4] = new PointF(224, 436);
             pts[5] = new PointF(224, 552);
             pts[6] = new PointF(276, 568);
             pts[7] = new PointF(308, 472);
             pts[8] = new PointF(316, 588);
             pts[9] = new PointF(368, 536);
             pts[10] = new PointF(332, 428);
             pts[11] = new PointF(124, 380);
             pts[12] = new PointF(180, 400);
             pts[13] = new PointF(148, 360);
             pts[14] = new PointF(148, 416);
             pts[15] = new PointF(128, 372);
             pts[16] = new PointF(124, 392);
             pts[17] = new PointF(136, 412);
             pts[18] = new PointF(156, 416);
             pts[19] = new PointF(176, 404);
             pts[20] = new PointF(180, 384);
             pts[21] = new PointF(168, 364);
             pts[22] = new PointF(260, 104);
             pts[23] = new PointF(428, 124);
             pts[24] = new PointF(328, 32);
             pts[25] = new PointF(320, 200);
             pts[26] = new PointF(268, 76);
             pts[27] = new PointF(264, 144);
             pts[28] = new PointF(316, 196);
             pts[29] = new PointF(384, 196);
             pts[30] = new PointF(424, 136);
             pts[31] = new PointF(412, 68);
             pts[32] = new PointF(348, 32);

             PlanarSubdivision subdiv = new PlanarSubdivision(pts);
             for (int i = 0; i < pts.Length; i++)
             {
            MCvSubdiv2DEdge? edge;
            MCvSubdiv2DPoint? point;
            CvEnum.Subdiv2DPointLocationType location = subdiv.Locate(ref pts[i], out edge, out point);
            if (location == Emgu.CV.CvEnum.Subdiv2DPointLocationType.ON_EDGE)
            {
               //you might want to store the points which is not inserted here.
               //or alternatively, add some random noise to the point and re-insert it again.
               continue;
            }
            subdiv.Insert(pts[i]);
             }

             VoronoiFacet[] facets = subdiv.GetVoronoiFacets();
        }
Beispiel #5
0
        public void TestPlanarSubdivision1()
        {
            int pointCount = 10000;

             #region generate random points
             PointF[] points = new PointF[pointCount];
             Random r = new Random((int)DateTime.Now.Ticks);
             for (int i = 0; i < points.Length; i++)
             {
            points[i] = new PointF((float)(r.NextDouble() * 20), (float)(r.NextDouble() * 20));
             }
             #endregion

             PlanarSubdivision division;

             Stopwatch watch = Stopwatch.StartNew();
             division = new PlanarSubdivision(points, true);
             Triangle2DF[] triangles = division.GetDelaunayTriangles(false);
             watch.Stop();
             Trace.WriteLine(String.Format("{0} milli-seconds, {1} triangles", watch.ElapsedMilliseconds, triangles.Length));
             watch.Reset();

             Assert.IsTrue(CvInvoke.icvSubdiv2DCheck(division) == 1);

             watch.Start();
             division = new PlanarSubdivision(points);
             VoronoiFacet[] facets = division.GetVoronoiFacets();
             watch.Stop();
             Trace.WriteLine(String.Format("{0} milli-seconds, {1} facets", watch.ElapsedMilliseconds, facets.Length));

             //foreach (Triangle2DF t in triangles)
             //{
             //int equalCount = triangles.FindAll(delegate(Triangle2DF t2) { return t2.Equals(t); }).Count;
             //Assert.AreEqual(1, equalCount, "Triangle duplicates");

             //int overlapCount = triangles.FindAll(delegate(Triangle2D t2) { return Util.IsConvexPolygonInConvexPolygon(t2, t);}).Count;
             //Assert.AreEqual(1, overlapCount, "Triangle overlaps");
             //}
        }