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"); }
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"); }
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; } }
//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); } } }
/// <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); }
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; } }