Example #1
0
        public static void CreateSubdivision(int rows, int cols, int pointCount, out Triangle2DF[] delaunayTriangles, out VoronoiFacet[] voronoiFacets)
        {
            var rand = new Random();
            var pts  = Enumerable.Range(0, pointCount).Select(_ => new PointF(rand.Next(0, cols), rand.Next(0, rows))).ToArray();

            using (Subdiv2D subdivision = new Subdiv2D(pts))
            {
                delaunayTriangles = subdivision.GetDelaunayTriangles();
                voronoiFacets     = subdivision.GetVoronoiFacets();
            }
        }
Example #2
0
 // Draws the Delaunay triangualtion into an image using the Subdiv2D
 private void DrawDelaunay(ref Mat img1D, ref Subdiv2D subdiv, MCvScalar mCvScalar)
 {
     Triangle2DF[] delaunayTriangles = subdiv.GetDelaunayTriangles();
     foreach (Triangle2DF triangle in delaunayTriangles)
     {
         System.Drawing.Point[] vertices = Array.ConvertAll <PointF, System.Drawing.Point>(triangle.GetVertices(), System.Drawing.Point.Round);
         using (VectorOfPoint vp = new VectorOfPoint(vertices))
         {
             CvInvoke.Polylines(img1D, vp, true, new Bgr(255, 255, 255).MCvScalar);
         }
     }
     img1D.ConvertTo(img1D, Emgu.CV.CvEnum.DepthType.Cv8U);
     CvInvoke.Imshow("Delaunay Triangulation", img1D);
 }
Example #3
0
 private void CreateSubdivision(float maxValue, int pointCount, out Triangle2DF[] delaunayTriangles, out VoronoiFacet[] voronoiFacets)
 {
     #region 在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 (Subdiv2D subDivision = new Subdiv2D(pts))
     {
         delaunayTriangles = subDivision.GetDelaunayTriangles();
         voronoiFacets     = subDivision.GetVoronoiFacets();
     }
 }
Example #4
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 (Subdiv2D subdivision = new Subdiv2D(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();
            }
        }
Example #5
0
        private void CreateDelaunay()
        {
            // Delaunay
            if (facesArrNext != null && facesArrCurr != null)
            {
                using (VectorOfPointF vpfCurr = ffpCurr)
                    using (VectorOfPointF vpfNext = ffpNext)
                    {
                        ptsCurr = vpfCurr.ToArray();
                        ptsNext = vpfNext.ToArray();

                        using (Subdiv2D subdivisionLeft = new Subdiv2D(ptsCurr))
                            using (Subdiv2D subdivisionRight = new Subdiv2D(ptsNext))
                            {
                                //Obtain the delaunay's triangulation from the set of points;
                                delaunayTrianglesCurr = subdivisionLeft.GetDelaunayTriangles();
                                delaunayTrianglesNext = subdivisionRight.GetDelaunayTriangles();
                            }
                    }
            }
        }
Example #6
0
        private void CreateDelaunay(ref Mat img, ref Subdiv2D subdiv, ref VectorOfPointF points,
                                    bool drawAnimated, ref VectorOfVectorOfInt triangleIndexes)
        {
            PointF[] pointsArr = points.ToArray();
            foreach (PointF p in pointsArr)
            {
                subdiv.Insert(p);
                if (drawAnimated)
                {
                    Mat imgCopy = img.Clone();
                    DrawDelaunay(ref imgCopy, ref subdiv, new MCvScalar(255, 255, 255));
                    CvInvoke.Imshow("Delaunay Triangulation", imgCopy);
                }
            }

            // Unfortunately we don't get the triangles by there original point indexes.
            // We only get them with their vertex coordinates.
            // So we have to map them again to get the triangles with their point indexes.

            Size      size = img.Size;
            Rectangle rect = new Rectangle(0, 0, size.Width, size.Height);

            VectorOfInt ind = new VectorOfInt();

            int[]         indArr       = new int[3];
            Triangle2DF[] triangleList = subdiv.GetDelaunayTriangles();
            for (int i = 0; i < triangleList.Length; i++)
            {
                Triangle2DF t = triangleList[i];

                PointF ptzero = new PointF {
                    X = t.V0.X, Y = t.V0.Y
                };
                PointF[] PZero = new PointF[] { ptzero };

                PointF ptone = new PointF {
                    X = t.V1.X, Y = t.V1.Y
                };
                PointF[] POne = new PointF[] { ptone };

                PointF pttwo = new PointF {
                    X = t.V2.X, Y = t.V2.Y
                };
                PointF[] PTwo = new PointF[] { pttwo };

                VectorOfPointF pt = new VectorOfPointF();

                pt.Push(PZero);

                pt.Push(POne);
                pt.Push(PTwo);

                if (rect.Contains(new Point((int)pt[0].X, (int)pt[0].Y)) &&
                    rect.Contains(new Point((int)pt[1].X, (int)pt[1].Y)) &&
                    rect.Contains(new Point((int)pt[2].X, (int)pt[2].Y)))
                {
                    for (int j = 0; j < 3; j++)
                    {
                        for (int k = 0; k < points.Size; k++)
                        {
                            if (Math.Abs(pt[j].X - points[k].X) < 1.0 &&
                                Math.Abs(pt[j].Y - points[k].Y) < 1)
                            {
                                indArr[j] = k;
                            }
                        }
                    }
                }
                ind = new VectorOfInt(indArr);
                triangleIndexes.Push(ind);
            }
        }