double GetArea3D(gPoint gp1, gPoint gp2, gPoint gp3) { try { gPoints gps = new gPoints(); gps.Add(gp1); gps.Add(gp2); gps.Add(gp3); return(Math.Abs(gps.Area3D(gps.GetNormal()))); } catch { } return(0.0); }
bool Merge3DFace(vdPolyface vdp) { int II = int.MaxValue; Int32Array i32 = vdp.FaceList; Dictionary <int, int> deleteID = new Dictionary <int, int>(); Dictionary <int, int> kk1 = new Dictionary <int, int>(); Dictionary <int, int> kk2 = new Dictionary <int, int>(); //合并三角形 #region 合并三角形 for (int i = 0; i < i32.Count; i = i + 5) { if (i32[i + 4] != II && i32[i] == i32[i + 3]) //一定是没有被合并过的,说明是三角形 { int i1 = i32[i]; int i2 = i32[i + 1]; int i3 = i32[i + 2]; kk1.Clear(); kk1.Add(i1, i1); kk1.Add(i2, i2); kk1.Add(i3, i3); for (int j = i + 5; j < i32.Count; j = j + 5) { if (i32[j + 4] != II && i32[j] == i32[j + 3]) //剔除已经处理过的 { int j1 = i32[j]; int j2 = i32[j + 1]; int j3 = i32[j + 2]; //kk2.Clear(); //kk2.Add(j1, j1); //kk2.Add(j2, j2); //kk2.Add(j3, j3); int k1 = 0; int k2 = 0; int k3 = 0; int k4 = 0; //找到公共边 if ((i1 == j1 || i1 == j2 || i1 == j3) && (i2 == j1 || i2 == j2 || i2 == j3)) { k1 = i3; k2 = i1; k4 = i2; } else if ((i2 == j1 || i2 == j2 || i2 == j3) && (i3 == j1 || i3 == j2 || i3 == j3)) { k1 = i1; k2 = i2; k4 = i3; } else if ((i1 == j1 || i1 == j2 || i1 == j3) && (i3 == j1 || i3 == j2 || i3 == j3)) { k1 = i2; k2 = i3; k4 = i1; } else { continue; } if (!kk1.ContainsKey(j1)) { k3 = j1; } if (!kk1.ContainsKey(j2)) { k3 = j2; } if (!kk1.ContainsKey(j3)) { k3 = j3; } //检查是否共面 gPoints gps3 = new gPoints(); //gps3.Add(vdp.VertexList[i1 - 1]); //gps3.Add(vdp.VertexList[i2 - 1]); //gps3.Add(vdp.VertexList[i3 - 1]); gps3.Add(vdp.VertexList[k1 - 1]); gps3.Add(vdp.VertexList[k2 - 1]); gps3.Add(vdp.VertexList[k4 - 1]); Vector vec = gps3.GetNormal(); // 导出时,下面计算dist的方法有异常OutOfRange异常抛出,特加以下判断 if (k3 > vdp.VertexList.Count || k1 > vdp.VertexList.Count || k3 <= 0 || k1 <= 0) { continue; } double dist = Globals.DistancePointToPlane(vdp.VertexList[k3 - 1], vec, vdp.VertexList[k1 - 1]); if (Math.Abs(dist) > Globals.VD_ZERO6) { continue; } //判断是否是凹多边形 凹多边形有显示问题 ////判断是否构成了三角形 gps3.InsertAt(3, vdp.VertexList[k3 - 1]); //gPoints gps4=gps3.Clone() as gPoints; //gps3.makeClosed(); //gps4.makeClosed(); //gps4.RemoveInLinePoints(); //if (gps3.Count != gps4.Count) continue; double area1 = GetArea3D(gps3[0], gps3[1], gps3[2]); double area2 = GetArea3D(gps3[3], gps3[1], gps3[2]); double area3 = GetArea3D(gps3[0], gps3[1], gps3[3]); double area4 = GetArea3D(gps3[0], gps3[2], gps3[3]); double area = Math.Max(area3, area4); if (area1 + area2 <= area) //凹多边形 { continue; } i32[i] = k4; i32[i + 1] = k1; i32[i + 2] = k2; i32[i + 3] = k3;//这里放到第3个点经常出现问题 i32[j + 4] = II; break; } } } } #endregion #region 生成新的polyface Int32Array iii32 = new Int32Array(); for (int i = 0; i < vdp.FaceList.Count; i = i + 5) { if (vdp.FaceList[i + 4] != II) { iii32.Add(i32[i]); iii32.Add(i32[i + 1]); iii32.Add(i32[i + 2]); iii32.Add(i32[i + 3]); iii32.Add(i32[i + 4]); } } vdp.FaceList = iii32; vdp.Invalidate(); #endregion return(true); }