예제 #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");
        }
예제 #2
0
        public void ThreadedFunction()
        {
            while (true)
            {
                while (!FApply)
                {
                    Thread.Sleep(5);
                }

                int SliceCount;
                Spread <PointF[]>      points;
                Spread <Triangle2DF[]> triangles;

                lock (FLockPoints)
                {
                    SliceCount = FPoints.SliceCount;
                    points     = new Spread <PointF[]>(SliceCount);

                    for (int i = 0; i < SliceCount; i++)
                    {
                        points[i] = new PointF[FPoints[i].Length];
                        Array.Copy(FPoints[i], points[i], FPoints[i].Length);
                    }
                }

                triangles = new Spread <Triangle2DF[]>(SliceCount);

                for (int i = 0; i < SliceCount; i++)
                {
                    PlanarSubdivision subdivision = new PlanarSubdivision(points[i] as PointF[]);
                    triangles[i] = subdivision.GetDelaunayTriangles(false);
                }

                lock (FTriangles)
                {
                    FTriangles.SliceCount = SliceCount;

                    Triangle2DF t;
                    for (int i = 0; i < SliceCount; i++)
                    {
                        FTriangles[i] = new Triangle2DF[triangles[i].Length];
                        for (int j = 0; j < triangles[i].Length; j++)
                        {
                            t = triangles[i][j];
                            FTriangles[i][j] = new Triangle2DF(t.V0, t.V1, t.V2);
                        }
                    }
                }

                FResults = true;
            }
        }
예제 #3
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");
      }
		//called when data for any output pin is requested
		public void Evaluate(int SpreadMax)
		{
			if (FPinInInput[0] == null)
			{
				FPinOutPosition.SliceCount = 0;
				FPinOutArea.SliceCount = 0;
				FPinOutCentroid.SliceCount = 0;
				return;
			}

			FPinOutPosition.SliceCount = SpreadMax;
			FPinOutArea.SliceCount = SpreadMax;
			FPinOutCentroid.SliceCount = SpreadMax;

			Triangle2DF[] delaunayTriangles;
			PlanarSubdivision subdivision;
			for (int i = 0; i < SpreadMax; i++)
			{
				if (!FPinInApply[i])
					continue;

				if (FPinInInput[i].Points.Length > 1000)
					continue;

				subdivision = new PlanarSubdivision(FPinInInput[i].Points.Clone() as PointF[]);
				delaunayTriangles = subdivision.GetDelaunayTriangles();

				FPinOutPosition[i].SliceCount = delaunayTriangles.Length * 3;
				FPinOutArea[i].SliceCount = delaunayTriangles.Length;
				FPinOutCentroid[i].SliceCount = delaunayTriangles.Length;

				for (int j = 0; j < delaunayTriangles.Length; j++)
				{
					FPinOutPosition[i][j * 3 + 0] = new Vector2D(delaunayTriangles[j].V0.X, delaunayTriangles[j].V0.Y);
					FPinOutPosition[i][j * 3 + 1] = new Vector2D(delaunayTriangles[j].V1.X, delaunayTriangles[j].V1.Y);
					FPinOutPosition[i][j * 3 + 2] = new Vector2D(delaunayTriangles[j].V2.X, delaunayTriangles[j].V2.Y);

					FPinOutArea[i][j] = delaunayTriangles[j].Area;
					FPinOutCentroid[i][j] = new Vector2D(delaunayTriangles[j].Centeroid.X, delaunayTriangles[j].Centeroid.Y);
				}

			}
		}
예제 #5
0
        /// <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();
            }
        }
        /// <summary>
        /// Function inserts points into poly (numInsert points) and triangulates while set of points.
        /// Result is lsit of triangles representing the poly.
        /// </summary>
        /// <param name="poly">Seq. of points representing the poly</param>
        /// <param name="numInsert">Number of points to insert into poly</param>
        /// <returns>Triangle's list</returns>
        public /*Triangle2DF[]*/ PolyFromTris triangulatePoly(Seq <Point> poly, int numInsert, int iw, int ih)
        {
            //Triangle2DF[] trisList; // Triangles list
            PolyFromTris trisPoly = new PolyFromTris();

            if (poly.Total + numInsert <= 24)
            {
                insertPoints(ref poly, ref trisPoly, numInsert, iw, ih); // Insert random points into the poly
            }

            //Array.ConvertAll(convexHull.ToArray(), new Converter<Point, PointF>(PointToPointF));

            using (PlanarSubdivision subdiv = new PlanarSubdivision(Array.ConvertAll(poly.ToArray(), new Converter <Point, PointF>(PointToPointF))))   //(poly.ToArray()))
            {
                Console.WriteLine(" ply size: " + poly.Total.ToString());
                //trisList = subdiv.GetDelaunayTriangles(); // Do triangulation
                trisPoly.setTris(subdiv.GetDelaunayTriangles());
            }


            return(trisPoly);
        }
예제 #7
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();
        }
예제 #8
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");
             //}
        }
        /// <summary>
        /// Function inserts points into poly (numInsert points) and triangulates while set of points. 
        /// Result is lsit of triangles representing the poly.
        /// </summary>
        /// <param name="poly">Seq. of points representing the poly</param>
        /// <param name="numInsert">Number of points to insert into poly</param>
        /// <returns>Triangle's list</returns>
        public /*Triangle2DF[]*/ PolyFromTris triangulatePoly(Seq<Point> poly, int numInsert, int iw, int ih)
        {
            //Triangle2DF[] trisList; // Triangles list
            PolyFromTris trisPoly = new PolyFromTris();

            if (poly.Total + numInsert <= 24)
            {

                insertPoints(ref poly, ref trisPoly, numInsert, iw, ih); // Insert random points into the poly
            }

            //Array.ConvertAll(convexHull.ToArray(), new Converter<Point, PointF>(PointToPointF));

            using (PlanarSubdivision subdiv = new PlanarSubdivision(Array.ConvertAll(poly.ToArray(), new Converter<Point, PointF>(PointToPointF))))   //(poly.ToArray()))
            {
                 Console.WriteLine(" ply size: " + poly.Total.ToString());
                 //trisList = subdiv.GetDelaunayTriangles(); // Do triangulation
                 trisPoly.setTris(subdiv.GetDelaunayTriangles());
            }
            
            
            return trisPoly;
        }
		public void ThreadedFunction()
		{
			while (true)
			{
				while (!FApply)
					Thread.Sleep(5);

				int SliceCount;
				Spread<PointF[]> points;
				Spread<Triangle2DF[]> triangles;

				lock (FLockPoints)
				{
					SliceCount = FPoints.SliceCount;
					points = new Spread<PointF[]>(SliceCount);

					for (int i = 0; i < SliceCount; i++)
					{
						points[i] = new PointF[FPoints[i].Length];
						Array.Copy(FPoints[i], points[i], FPoints[i].Length);
					}
	
				}

				triangles = new Spread<Triangle2DF[]>(SliceCount);

				for (int i = 0; i < SliceCount; i++)
				{
					PlanarSubdivision subdivision = new PlanarSubdivision(points[i] as PointF[]);
					triangles[i] = subdivision.GetDelaunayTriangles(false);
				}

				lock (FTriangles)
				{
					FTriangles.SliceCount = SliceCount;

					Triangle2DF t;
					for (int i = 0; i < SliceCount; i++)
					{
						FTriangles[i] = new Triangle2DF[triangles[i].Length];
						for (int j = 0; j < triangles[i].Length; j++ )
						{
							t = triangles[i][j];
							FTriangles[i][j] = new Triangle2DF(t.V0, t.V1, t.V2);
						}
					}
				}

				FResults = true;
			}
		}