示例#1
0
        /// <summary>
        /// Obtains the list of Voronoi Facets
        /// </summary>
        /// <returns>The list of Voronoi Facets</returns>
        private IEnumerable <VoronoiFacet> GetVoronoiFacetsHelper()
        {
            if (_isVoronoiDirty)
            {
                CvInvoke.cvCalcSubdivVoronoi2D(Ptr);
                _isVoronoiDirty = false;
            }

            int size = MCvSubdiv2D.total;

            PointF[]          points      = new PointF[size];
            MCvSubdiv2DEdge[] edges       = new MCvSubdiv2DEdge[size];
            GCHandle          pointHandle = GCHandle.Alloc(points, GCHandleType.Pinned);
            GCHandle          edgeHandle  = GCHandle.Alloc(edges, GCHandleType.Pinned);

            CvInvoke.PlanarSubdivisionGetSubdiv2DPoints(_ptr, pointHandle.AddrOfPinnedObject(), edgeHandle.AddrOfPinnedObject(), ref size);
            pointHandle.Free();
            edgeHandle.Free();
            using (MemStorage stor = new MemStorage())
            {
                Seq <PointF> ptSeq = new Seq <PointF>(stor);
                for (int i = 0; i < size; i++)
                {
                    CvInvoke.PlanarSubdivisionEdgeToPoly(edges[i], ptSeq);
                    PointF[] polygon = ptSeq.ToArray();
                    if (polygon.Length > 0)
                    {
                        yield return(new VoronoiFacet(points[i], polygon));
                    }
                }
            }
        }
        /// <summary>
        /// Obtains the list of Voronoi Facets
        /// </summary>
        /// <returns>The list of Voronoi Facets</returns>
        public VoronoiFacet[] GetVoronoiFacets()
        {
            if (_isVoronoiDirty)
            {
                CvInvoke.cvCalcSubdivVoronoi2D(Ptr);
                _isVoronoiDirty = false;
            }

            int size = MCvSubdiv2D.total;

            PointF[]          points      = new PointF[size];
            MCvSubdiv2DEdge[] edges       = new MCvSubdiv2DEdge[size];
            GCHandle          pointHandle = GCHandle.Alloc(points, GCHandleType.Pinned);
            GCHandle          edgeHandle  = GCHandle.Alloc(edges, GCHandleType.Pinned);

            PlanarSubdivisionGetSubdiv2DPoints(_ptr, pointHandle.AddrOfPinnedObject(), edgeHandle.AddrOfPinnedObject(), ref size);
            pointHandle.Free();
            edgeHandle.Free();

            List <VoronoiFacet> facets = new List <VoronoiFacet>();

            PointF[] buffer       = new PointF[256];
            GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr   bufferPtr    = bufferHandle.AddrOfPinnedObject();

            int count = 0;

            for (int i = 0; i < size; i++)
            {
                PlanarSubdivisionEdgeToPoly(edges[i], bufferPtr, ref count);
                if (count > 0)
                {
                    PointF[] polygon = new PointF[count];
                    Array.Copy(buffer, polygon, count);
                    facets.Add(new VoronoiFacet(points[i], polygon));
                }
            }
            bufferHandle.Free();
            return(facets.ToArray());
        }
示例#3
0
 internal static extern void PlanarSubdivisionEdgeToPoly(MCvSubdiv2DEdge edge, IntPtr buffer);
 private static extern void PlanarSubdivisionEdgeToPoly(MCvSubdiv2DEdge edge, IntPtr buffer, ref int count);