public Triangle(HalfEdge _twin, int _newVert, Delaunay _d) { D = _d; var v0 = _twin.OriginIdx; var v1 = _newVert; var v2 = _twin.NextEdge.OriginIdx; Edge0 = new HalfEdge(this, v0, D); Edge1 = new HalfEdge(this, v1, D); Edge2 = new HalfEdge(this, v2, D); Edge0.NextEdge = Edge1; Edge1.NextEdge = Edge2; Edge2.NextEdge = Edge0; Edge2.Twin = _twin; _twin.Twin = Edge2; var pts = D.Points; Geom.Circumcircle(pts[v0], pts[v1], pts[v2], out CCX, out CCY, out CCRSq); PrevTri = _d.LastTri; _d.LastTri = this; _d.m_TriCount++; }
/// <summary> /// Create new Delaunay object /// </summary> /// <param name="_points">Points to be triangulated</param> /// <typeparam name="T">Type of triangulator to use for triangulation</typeparam> /// <returns>Delaunay object ready for triangulation</returns> public static Delaunay Create <T>(Vector2[] _points) where T : Triangulator, new() { var del = new Delaunay(_points); del.triangulator = new T(); ((ITriangulator)del.triangulator).SetTarget(del); return(del); }
public Triangle(int _v0, int _v1, int _v2, Delaunay _d) { D = _d; Edge0 = new HalfEdge(this, _v0, D); Edge1 = new HalfEdge(this, _v1, D); Edge2 = new HalfEdge(this, _v2, D); Edge0.NextEdge = Edge1; Edge1.NextEdge = Edge2; Edge2.NextEdge = Edge0; var pts = D.Points; Geom.Circumcircle(pts[_v0], pts[_v1], pts[_v2], out CCX, out CCY, out CCRSq); PrevTri = _d.LastTri; _d.LastTri = this; _d.m_TriCount++; }
void ITriangulator.SetTarget(Delaunay _d) { D = _d; }
public HalfEdge(Triangle _triangle, int _originIdx, Delaunay _d) { D = _d; Triangle = _triangle; OriginIdx = _originIdx; }
public Voronoi(Delaunay _d) { D = _d; Bounds = _d.BoundsRect; }
public static void Visualize(Delaunay _d, Voronoi _v = null, string _fileName = "debugMesh") { if (!Enabled) { return; } //var bitmap = new Bitmap((int) (_d.BoundsRect.width * 1.2f), (int) (_d.BoundsRect.height * 1.2f)); var bitmap = new Bitmap(2000, 2000); var originOffset = _d.BoundsRect.min; originOffset.x += 0 - bitmap.Width / 2f; originOffset.y += 0 - bitmap.Height / 2f; if (!float.IsInfinity(OriginOffsetOverride.x)) { originOffset = _d.BoundsRect.min; originOffset.x += OriginOffsetOverride.x - bitmap.Width / 2f; originOffset.y += OriginOffsetOverride.y - bitmap.Height / 2f; } //Draw Mesh var tris = _d.Triangles(); foreach (var tri in tris) { for (var idx = 0; idx < 3; ++idx) { var edge = tri.Edge(idx); var x1 = edge.OriginPos.x - originOffset.x; var y1 = edge.OriginPos.y - originOffset.y; var x2 = edge.NextEdge.OriginPos.x - originOffset.x; var y2 = edge.NextEdge.OriginPos.y - originOffset.y; using (var g = Graphics.FromImage(bitmap)) { g.SmoothingMode = SmoothingMode.None; g.InterpolationMode = InterpolationMode.Low; g.PixelOffsetMode = PixelOffsetMode.None; var pen = new Pen(m_ColorMesh); g.DrawLine(pen, x1, y1, x2, y2); if (!showVertIdxs) { continue; } var textRect = new RectangleF(new PointF(x1, y1), new SizeF(45, 20)); g.DrawString(edge.OriginIdx.ToString(), new Font("Small Fonts", fontSize), Brushes.BurlyWood, textRect); } } } //Draw Frontier /* * var cs = (CircleSweep) _d.triangulator; * var fPt = cs.frontier.LastAddedFPt; * var fpStart = fPt; * var firstMoveDone = false; * while (fPt.VertIdx != fpStart.VertIdx || firstMoveDone == false) * { * var pts = _d.Points; * var x1 = pts[fPt.VertIdx].x - originOffset.x; * var y1 = pts[fPt.VertIdx].y - originOffset.y; * var x2 = pts[fPt.Right.VertIdx].x - originOffset.x; * var y2 = pts[fPt.Right.VertIdx].y - originOffset.y; * * //var x1 = fPt.Pos.x - originOffset.x; * //var y1 = fPt.Pos.y - originOffset.y; * //var x2 = fPt.Right.Pos.x - originOffset.x; * //var y2 = fPt.Right.Pos.y - originOffset.y; * * using (var g = Graphics.FromImage(bitmap)) * { * g.SmoothingMode = SmoothingMode.None; * g.InterpolationMode = InterpolationMode.Low; * g.PixelOffsetMode = PixelOffsetMode.None; * var pen = new Pen(m_ColorFront); * g.DrawLine(pen, x1, y1, x2, y2); * } * * fPt = fPt.Right; * firstMoveDone = true; * } */ //Draw Circumcircles /* * foreach (var tri in _d.Triangles) * { * float r; * Vector2f center; * tri.CircumCircle(out center, out r); * var rul = new Vector2f(center.x - r, center.y - r); * var rect = new Rectangle((int)rul.x, (int)rul.y, (int)(2 * r), (int)(2 * r)); * using (var g = Graphics.FromImage(bitmap)) * { * * * g.SmoothingMode = SmoothingMode.None; * g.InterpolationMode = InterpolationMode.Low; * g.PixelOffsetMode = PixelOffsetMode.None; * * var pen = new Pen(m_ColorCircles); * g.DrawEllipse(pen, (center.x - r) - originOffset.x, (center.y - r) - originOffset.y, r * 2, r * 2); * //g.DrawRectangle(pen, rect); * //g.DrawLine(pen, center.x, center.y, rect.Left, rect.Top); * } * } */ //Draw Origin /* * using (var g = Graphics.FromImage(bitmap)) * { * g.SmoothingMode = SmoothingMode.None; * g.InterpolationMode = InterpolationMode.Low; * g.PixelOffsetMode = PixelOffsetMode.None; * var pen = new Pen(m_ColorFront); * var x1 = cs.Origin.x - originOffset.x; * var y1 = cs.Origin.y - originOffset.y; * g.FillRectangle(Brushes.Cyan, x1, y1, 2, 2); * } */ //Voronoi on top if (_v != null) { var sites = _v.SitesByDIdx.Values; foreach (var site in sites) { var edges = site.Edges; for (var idx = 0; idx < edges.Count; ++idx) { if (idx == edges.Count - 1 && !site.Closed) { break; } var edge = edges[idx]; var x1 = edge.Origin.x - originOffset.x; var y1 = edge.Origin.y - originOffset.y; var x2 = edge.NextEdge.Origin.x - originOffset.x; var y2 = edge.NextEdge.Origin.y - originOffset.y; using (var g = Graphics.FromImage(bitmap)) { g.SmoothingMode = SmoothingMode.None; g.InterpolationMode = InterpolationMode.Low; g.PixelOffsetMode = PixelOffsetMode.None; var pen = new Pen(m_ColorVor); g.DrawLine(pen, x1, y1, x2, y2); } } } } //Draw Voronoi Bounds Rect if (_v != null) { var dBnds = new Rect(_v.Bounds); dBnds.center = dBnds.center - originOffset; using (var g = Graphics.FromImage(bitmap)) { g.SmoothingMode = SmoothingMode.None; g.InterpolationMode = InterpolationMode.Low; g.PixelOffsetMode = PixelOffsetMode.None; var pen = new Pen(Color.Orange); //g.DrawRectangle(pen, dBnds.xMin, dBnds.yMin, dBnds.width, dBnds.height); } } //Temp /* * { * var cent = ((CircleSweep)(_d.triangulator)).Origin - originOffset; * var v1 = _d.Points[326757] - originOffset; * * using (var g = Graphics.FromImage(bitmap)) * { * g.SmoothingMode = SmoothingMode.None; * g.InterpolationMode = InterpolationMode.Low; * g.PixelOffsetMode = PixelOffsetMode.None; * var pen = new Pen(Color.Yellow); * g.DrawLine(pen, cent.x, cent.y, v1.x, v1.y); * * } * } */ var path = AppDomain.CurrentDomain.BaseDirectory + _fileName + ".png"; bitmap.Save(path); }