public CenterEdge(Center a, Center b) { this.a = a; this.b = b; }
public List <DelaunayTriangle> adjoinTriangle; //邻接三角形 public DelaunayTriangle(Site site1, Site site2, Site site3, Center center1, Center center2, Center center3) { centerPoint = new Site(); this.site1 = site1; this.site2 = site2; this.site3 = site3; this.center1 = center1; this.center2 = center2; this.center3 = center3; //构造外接圆圆心以及半径 voronoi.circle_center(centerPoint, site1, site2, site3, ref radius); }
//对新形成的三角形进行局部优化 public List <DelaunayTriangle> LOP(List <DelaunayTriangle> newTriList) { List <DelaunayTriangle> resultTriList = new List <DelaunayTriangle>(); //拷贝新形成的三角 for (int i = 0; i < newTriList.Count; i++) { resultTriList.Add(newTriList[i]); } for (int i = 0; i < newTriList.Count; i++) { for (int j = i + 1; j < newTriList.Count; j++) { Edge commonEdge; //需要调整对角线的的三角形的公共边 Site anotherPoint = new Site(); //新对角线的另一点 Center anotherCenter = new Center(); if (isInCircle(newTriList[j], newTriList[i].site1)) //三角形点在外接圆内 { //找出两个三角形的公共边 commonEdge = findCommonEdge(newTriList[i], newTriList[j]); if (commonEdge != null) { //移除需要调整的三角形 resultTriList.Remove(newTriList[i]); resultTriList.Remove(newTriList[j]); //找出对角线的另一点 if (siteIsEqual(newTriList[j].site1, commonEdge.a) == false && siteIsEqual(newTriList[j].site1, commonEdge.b) == false) { anotherPoint = newTriList[j].site1; anotherCenter = newTriList[j].center1; } if (siteIsEqual(newTriList[j].site2, commonEdge.a) == false && siteIsEqual(newTriList[j].site2, commonEdge.b) == false) { anotherPoint = newTriList[j].site2; anotherCenter = newTriList[j].center2; } if (siteIsEqual(newTriList[j].site3, commonEdge.a) == false && siteIsEqual(newTriList[j].site3, commonEdge.b) == false) { anotherPoint = newTriList[j].site3; anotherCenter = newTriList[j].center3; } //形成两个新的三角形 resultTriList.Add(new DelaunayTriangle(newTriList[i].site1, anotherPoint, commonEdge.a, newTriList[i].center1, anotherCenter, commonEdge.ca)); resultTriList.Add(new DelaunayTriangle(newTriList[i].site1, anotherPoint, commonEdge.b, newTriList[i].center1, anotherCenter, commonEdge.cb)); } } if (isInCircle(newTriList[j], newTriList[i].site2))//三角形点在外接圆内 { //找出两个三角形的公共边 commonEdge = findCommonEdge(newTriList[i], newTriList[j]); if (commonEdge != null) { //移除需要调整的三角形 resultTriList.Remove(newTriList[i]); resultTriList.Remove(newTriList[j]); //找出对角线的另一点 if (siteIsEqual(newTriList[j].site1, commonEdge.a) == false && siteIsEqual(newTriList[j].site1, commonEdge.b) == false) { anotherPoint = newTriList[j].site1; } if (siteIsEqual(newTriList[j].site2, commonEdge.a) == false && siteIsEqual(newTriList[j].site2, commonEdge.b) == false) { anotherPoint = newTriList[j].site2; } if (siteIsEqual(newTriList[j].site3, commonEdge.a) == false && siteIsEqual(newTriList[j].site3, commonEdge.b) == false) { anotherPoint = newTriList[j].site3; } //形成两个新的三角形 resultTriList.Add(new DelaunayTriangle(newTriList[i].site2, anotherPoint, commonEdge.a, newTriList[i].center2, anotherCenter, commonEdge.ca)); resultTriList.Add(new DelaunayTriangle(newTriList[i].site2, anotherPoint, commonEdge.a, newTriList[i].center2, anotherCenter, commonEdge.cb)); } } if (isInCircle(newTriList[j], newTriList[i].site3))//三角形点在外接圆内 { //找出两个三角形的公共边 commonEdge = findCommonEdge(newTriList[i], newTriList[j]); if (commonEdge != null) { //移除需要调整的三角形 resultTriList.Remove(newTriList[i]); resultTriList.Remove(newTriList[j]); //找出对角线的另一点 if (siteIsEqual(newTriList[j].site1, commonEdge.a) == false && siteIsEqual(newTriList[j].site1, commonEdge.b) == false) { anotherPoint = newTriList[j].site1; } if (siteIsEqual(newTriList[j].site2, commonEdge.a) == false && siteIsEqual(newTriList[j].site2, commonEdge.b) == false) { anotherPoint = newTriList[j].site2; } if (siteIsEqual(newTriList[j].site3, commonEdge.a) == false && siteIsEqual(newTriList[j].site3, commonEdge.b) == false) { anotherPoint = newTriList[j].site3; } //形成两个新的三角形 resultTriList.Add(new DelaunayTriangle(newTriList[i].site3, anotherPoint, commonEdge.a, newTriList[i].center3, anotherCenter, commonEdge.ca)); resultTriList.Add(new DelaunayTriangle(newTriList[i].site3, anotherPoint, commonEdge.a, newTriList[i].center3, anotherCenter, commonEdge.cb)); } } } } newTriList = resultTriList; return(resultTriList);//返回优化后的新形成的三角形 }
//根据Delaunay三角形网构造Voronoi图的边 public List <Edge> returnVoronoiEdgesFromDelaunayTriangles(List <DelaunayTriangle> allTriangle, List <Edge> voronoiRayEdgeList, List <Center> voronoiCenterList, Dictionary <Site, Corner> voronoiCornerList) { List <Edge> voronoiEdgeList = new List <Edge>(); //List<Edge> voronoiRayEdgeList = new List<Edge>(); for (int i = 0; i < allTriangle.Count; i++) { List <Edge> neighborEdgeList = new List <Edge>(); //三角形邻接边集合 for (int j = 0; j < allTriangle.Count; j++) //为了找出邻接三角形数为2的三角形,即最外边的三角形,循环只能从0开始 { if (j != i) //不与自身比较 { Edge neighborEdge = findCommonEdge(allTriangle[i], allTriangle[j]); if (neighborEdge != null) { neighborEdgeList.Add(neighborEdge); //构造Voronoi边 Corner c1, c2; if (!voronoiCornerList.TryGetValue(allTriangle[i].centerPoint, out c1)) { c1 = new Corner(); c1.point = allTriangle[i].centerPoint; c1.index = voronoiCornerList.Count; voronoiCornerList.Add(allTriangle[i].centerPoint, c1); } if (!voronoiCornerList.TryGetValue(allTriangle[j].centerPoint, out c2)) { c2 = new Corner(); c2.point = allTriangle[j].centerPoint; c2.index = voronoiCornerList.Count; voronoiCornerList.Add(allTriangle[j].centerPoint, c2); } Edge voronoiEdge = new Edge(allTriangle[i].centerPoint, allTriangle[j].centerPoint, c1, c2); if (!voronoiEdgeList.Contains(voronoiEdge)) { voronoiEdgeList.Add(voronoiEdge); //c1的touches添加 try { c1.touches.Add(neighborEdge.ca.index, neighborEdge.ca); } catch (ArgumentException) { } try { c1.touches.Add(neighborEdge.cb.index, neighborEdge.cb); } catch (ArgumentException) { } //c2的touches添加 try { c2.touches.Add(neighborEdge.ca.index, neighborEdge.ca); } catch (ArgumentException) { } try { c2.touches.Add(neighborEdge.cb.index, neighborEdge.cb); } catch (ArgumentException) { } //c1的protrudes添加 try { c1.protrudes.Add(voronoiEdge, voronoiEdge); } catch (ArgumentException) { } //c2的protrudes添加 try { c2.protrudes.Add(voronoiEdge, voronoiEdge); } catch (ArgumentException) { } //c1的adjacent添加 try { c1.adjacent.Add(c2.index, c2); } catch (ArgumentException) { } //c2的adjacent添加 try { c2.adjacent.Add(c1.index, c1); } catch (ArgumentException) { } //A的corners添加 try { neighborEdge.ca.corners.Add(c1.point, c1); } catch (ArgumentException) { } try { neighborEdge.ca.corners.Add(c2.point, c2); } catch (ArgumentException) { } //B的corners添加 try { neighborEdge.cb.corners.Add(c1.point, c1); } catch (ArgumentException) { } try { neighborEdge.cb.corners.Add(c2.point, c2); } catch (ArgumentException) { } //A的borders添加 try { neighborEdge.ca.borders.Add(voronoiEdge, voronoiEdge); } catch (ArgumentException) { } //B的borders添加 try { neighborEdge.cb.borders.Add(voronoiEdge, voronoiEdge); } catch (ArgumentException) { } } } } } if (neighborEdgeList.Count == 2)//表示此三角形是外围三角形,Voronoi边需要射线 { Site midpoint; Edge rayEdge = null; Corner c1, c2 = null; Center a = null, b = null; if (!voronoiCornerList.TryGetValue(allTriangle[i].centerPoint, out c1)) { c1 = new Corner(); c1.point = allTriangle[i].centerPoint; c1.index = voronoiCornerList.Count; voronoiCornerList.Add(allTriangle[i].centerPoint, c1); } //找出最外边并寻找中点构造Voronoi射线边 if (isPointOnEdge(neighborEdgeList[0], allTriangle[i].site1) && isPointOnEdge(neighborEdgeList[1], allTriangle[i].site1)) { midpoint = findMidPoint(allTriangle[i].site2, allTriangle[i].site3); rayEdge = produceRayEdge(allTriangle[i].centerPoint, midpoint);//产生较长的射线,原理实现还是线段画出的线 voronoiRayEdgeList.Add(rayEdge); a = allTriangle[i].center2; b = allTriangle[i].center3; } if (isPointOnEdge(neighborEdgeList[0], allTriangle[i].site2) && isPointOnEdge(neighborEdgeList[1], allTriangle[i].site2)) { midpoint = findMidPoint(allTriangle[i].site1, allTriangle[i].site3); rayEdge = produceRayEdge(allTriangle[i].centerPoint, midpoint); voronoiRayEdgeList.Add(rayEdge); a = allTriangle[i].center1; b = allTriangle[i].center3; } if (isPointOnEdge(neighborEdgeList[0], allTriangle[i].site3) && isPointOnEdge(neighborEdgeList[1], allTriangle[i].site3)) { midpoint = findMidPoint(allTriangle[i].site1, allTriangle[i].site2); rayEdge = produceRayEdge(allTriangle[i].centerPoint, midpoint); voronoiRayEdgeList.Add(rayEdge); a = allTriangle[i].center1; b = allTriangle[i].center2; } if (rayEdge != null && !voronoiCornerList.TryGetValue(rayEdge.b, out c2)) { c2 = new Corner(); c2.point = rayEdge.b; c2.index = voronoiCornerList.Count; voronoiCornerList.Add(rayEdge.b, c2); } //c2的touches添加 try { c2.touches.Add(a.index, a); } catch (ArgumentException) { } try { c2.touches.Add(b.index, b); } catch (ArgumentException) { } Edge borderEdge = new Edge(a.point, b.point, a, b); //c1的protrudes添加 try { c1.protrudes.Add(borderEdge, borderEdge); } catch (ArgumentException) { } //c2的protrudes添加 try { c2.protrudes.Add(borderEdge, borderEdge); } catch (ArgumentException) { } //c1的adjacent添加 try { c1.adjacent.Add(c2.index, c2); } catch (ArgumentException) { } //c2的adjacent添加 try { c2.adjacent.Add(c1.index, c1); } catch (ArgumentException) { } //A的corners添加 try { a.corners.Add(c2.point, c2); } catch (ArgumentException) { } //B的corners添加 try { b.corners.Add(c2.point, c2); } catch (ArgumentException) { } //A的borders添加 try { a.borders.Add(borderEdge, borderEdge); } catch (ArgumentException) { } //B的borders添加 try { b.borders.Add(borderEdge, borderEdge); } catch (ArgumentException) { } } } return(voronoiEdgeList); }
//将点与受影响的三角形三点连接,形成新的三个三角形添加到三角形链中 public void addNewDelaunayTriangle(List <DelaunayTriangle> allTriangles, DelaunayTriangle influenedTri, Site point, Center centerPoint) { allTriangles.Add(new DelaunayTriangle(influenedTri.site1, influenedTri.site2, point, influenedTri.center1, influenedTri.center2, centerPoint)); allTriangles.Add(new DelaunayTriangle(influenedTri.site1, influenedTri.site3, point, influenedTri.center1, influenedTri.center3, centerPoint)); allTriangles.Add(new DelaunayTriangle(influenedTri.site2, influenedTri.site3, point, influenedTri.center2, influenedTri.center3, centerPoint)); }