/// <summary> /// Creates a Voronoi DCEL from a triangulation. Triangulation should be Delaunay /// </summary> /// <param name="m_Delaunay"></param> /// <returns></returns> public static DCEL Create(Triangulation m_Delaunay) { if (!Delaunay.IsValid(m_Delaunay)) { throw new GeomException("Triangulation should be delaunay for the Voronoi diagram."); } var dcel = new DCEL(); // create vertices for each triangles circumcenter and store them in a dictionary Dictionary <Triangle, DCELVertex> vertexMap = new Dictionary <Triangle, DCELVertex>(); foreach (var triangle in m_Delaunay.Triangles) { // degenerate triangle, just ignore if (!triangle.Circumcenter.HasValue) { continue; } var vertex = new DCELVertex(triangle.Circumcenter.Value); dcel.AddVertex(vertex); vertexMap.Add(triangle, vertex); } // remember which edges where visited // since each edge has a twin var edgesVisited = new HashSet <TriangleEdge>(); foreach (var edge in m_Delaunay.Edges) { // either already visited twin edge or edge is outer triangle if (edgesVisited.Contains(edge) || edge.IsOuter) { continue; } // add edge between the two adjacent triangles vertices // vertices at circumcenter of triangle if (edge.T != null && edge.Twin.T != null) { var v1 = vertexMap[edge.T]; var v2 = vertexMap[edge.Twin.T]; HalfEdge e1 = dcel.AddEdge(v1, v2); e1.Twin.Face.owner = m_Delaunay.GetOwner(edge.Point1); e1.Face.owner = m_Delaunay.GetOwner(edge.Point2); edgesVisited.Add(edge); edgesVisited.Add(edge.Twin); } } return(dcel); }
public void DelaunayTriangulation2() { var m_delaunay = Delaunay.Create(m_arrowVertices); Assert.IsTrue(Delaunay.IsValid(m_delaunay)); var triangles = (System.Collections.ICollection)m_delaunay.Triangles; Assert.Contains(new Triangle(m_topVertex, m_rightVertex, m_botVertex), triangles); Assert.Contains(new Triangle(m_topVertex, m_farRightVertex, m_rightVertex), triangles); Assert.Contains(new Triangle(m_rightVertex, m_farRightVertex, m_botVertex), triangles); }
public void DelaunayTriangulation1() { var m_delaunay = Delaunay.Create(m_diamondVertices); Assert.IsTrue(Delaunay.IsValid(m_delaunay)); Assert.AreEqual(m_diamond.Area, m_delaunay.Area, MathUtil.EPS); var triangles = (System.Collections.ICollection)m_delaunay.Triangles; Assert.Contains(new Triangle(m_topVertex, m_rightVertex, m_leftVertex), triangles); Assert.Contains(new Triangle(m_leftVertex, m_rightVertex, m_botVertex), triangles); }
public void InValidTriangulationTest() { var m_delaunay = new Triangulation(); var t1 = new Triangle(m_topVertex, m_rightVertex, m_botVertex); var t2 = new Triangle(m_topVertex, m_botVertex, m_leftVertex); t1.E2.Twin = t2.E0; t2.E0.Twin = t1.E2; m_delaunay.AddTriangle(t1); m_delaunay.AddTriangle(t2); Assert.IsFalse(Delaunay.IsValid(m_delaunay)); }
public void DelaunayAddVertexTest() { var m_delaunay = Delaunay.Create(); Delaunay.AddVertex(m_delaunay, m_topVertex); Delaunay.AddVertex(m_delaunay, m_botVertex); Delaunay.AddVertex(m_delaunay, m_leftVertex); Delaunay.AddVertex(m_delaunay, m_farRightVertex); Delaunay.AddVertex(m_delaunay, m_rightVertex); m_delaunay.RemoveInitialTriangle(); Assert.IsTrue(Delaunay.IsValid(m_delaunay)); // check for triangles var triangles = (System.Collections.ICollection)m_delaunay.Triangles; Assert.Contains(new Triangle(m_topVertex, m_rightVertex, m_leftVertex), triangles); Assert.Contains(new Triangle(m_leftVertex, m_rightVertex, m_botVertex), triangles); Assert.Contains(new Triangle(m_topVertex, m_farRightVertex, m_rightVertex), triangles); Assert.Contains(new Triangle(m_rightVertex, m_farRightVertex, m_botVertex), triangles); }