public static bool MakeHull(Mesh mesh, Transform tr, string path, ref ConvexData data) { GiftWrap gw = new GiftWrap(); gw.Reset(); List <Vector3> vecVertex = new List <Vector3>(); Vector3 vmin = Vector3.zero; Vector3 vmax = Vector3.zero; Bounds bound = mesh.bounds; vmin = bound.min; vmax = bound.max; Vector3[] verts = mesh.vertices; for (int j = 0; j < mesh.vertexCount; ++j) { Vector3 wp = verts[j]; if (null != tr) { wp = tr.TransformPoint(wp); } bool bnear = false; for (int k = 0; k < (int)vecVertex.Count; k++) { if ((wp - vecVertex[k]).magnitude < CLOSE_VERTEX_DISTANCE_THRESH) { bnear = true; break; } } if (!bnear) { vecVertex.Add(wp); } } int numallvert = vecVertex.Count; gw.SetVertexes(vecVertex); gw.ComputeConvexHull(); path += "/" + System.DateTime.Now.ToString("dd-MM-yy-HH-mm-ss") + "_vts.txt"; if (gw.ExceptionOccur()) { gw.SaveVerticesToFile(path); gw.Reset(); return(false); } ConvexPolytope cp = new ConvexPolytope(); cp.Init(gw, (vmax - vmin).magnitude); if (cp.ExceptionOccur && cp.MinPatchNum > 20) { gw.SaveVerticesToFile(path); return(false); } int patchnum = Mathf.Min(cp.OriginPatchNum, Mathf.Max(10, cp.MinPatchNum)); if (patchnum > MAX_FACE_IN_HULL) { gw.Reset(); return(false); } else { cp.Goto(Mathf.Min(patchnum, MAX_FACE_IN_HULL)); } gw.Reset(); data.Reset(); cp.ExportCHData(data); return(true); }
public bool Init(GiftWrap gw) { if (gw.ExceptionOccur()) { return(false); } Reset(); //重置为初始状态 Centroid = gw.Centroid; //质心置为初始化gw的质心 //分情况考虑构造CConvexPolytope if (gw.HullType == GiftWrap.HULL_TYPE.HULL_3D) //3D Hull的情况 { int[] map = new int[gw.LstVertex.Count]; //构造一个映射表 int count = 0; //添加有效顶点&构造映射表 int i; for (i = 0; i < gw.LstVertex.Count; i++) { map[i] = -1; if (gw.LstExtremeVertex[i]) { //当前点有效,用其初始化v,并将其插入到顶点列表 Vector3 v = gw.LstVertex[i]; LstVertecies.Add(v); LstVertexInfo.Add(new VertexInfo()); map[i] = count++; } } //构造面片 //添加三角形面片 for (i = 0; i < gw.LstFaces.Count; i++) { Face f = gw.LstFaces[i]; if (!f.InPolygon) //不属于任何多边形,添加 { Vector3 v1 = gw.LstVertex[f.v1]; Vector3 v2 = gw.LstVertex[f.v2]; Vector3 v3 = gw.LstVertex[f.v3]; Patch pat = new Patch(this); pat.Set(v1, v2, v3); //几何信息 //依次向Neighbors中添加元素 VPNeighbor vpn = new VPNeighbor(); List <VPNeighbor> lstNeighbor = pat.LstNeighbors; vpn.Vid = map[f.v1]; //这里必须作一个映射 lstNeighbor.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = map[f.v2]; lstNeighbor.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = map[f.v3]; lstNeighbor.Add(vpn); //各顶点度数增1 LstVertexInfo[map[f.v1]].Degree++; LstVertexInfo[map[f.v2]].Degree++; LstVertexInfo[map[f.v3]].Degree++; //添加到链表 LstPatches.Add(pat); } } //添加多边形面片 for (i = 0; i < gw.LstPlanes.Count; i++) { List <int> planes = gw.LstPlanes[i]; //取前三个点构造平面几何信息 Vector3 v1 = gw.LstVertex[planes[0]]; Vector3 v2 = gw.LstVertex[planes[1]]; Vector3 v3 = gw.LstVertex[planes[2]]; Patch pat = new Patch(this); pat.Set(v1, v2, v3); //几何信息 //依次向Neighbors中添加元素 VPNeighbor vpn = new VPNeighbor(); List <VPNeighbor> lstNeighbors = pat.LstNeighbors; for (int j = 0; j < planes.Count; j++) { vpn = new VPNeighbor(); vpn.Vid = map[planes[j]]; //这里必须作一个映射 lstNeighbors.Add(vpn); LstVertexInfo[vpn.Vid].Degree++; //顶点度数增1 } //添加到链表 LstPatches.Add(pat); } } else { //说明是2D Hull的情况 List <int> lstCHVs = gw.GetCHVertecies(); if (lstCHVs == null) { return(false); } if (lstCHVs.Count < 3) //至少是一个三角形 { return(false); } //顶点信息 VertexInfo vInfo = new VertexInfo(); vInfo.Degree = 3; //直棱拄所有顶点的面度数均为3 HalfSpace planeOut = new HalfSpace(); HalfSpace planeIn = new HalfSpace(); //取前三个点构造平面几何信息 Vector3 v1 = gw.LstVertex[lstCHVs[0]]; Vector3 v2 = gw.LstVertex[lstCHVs[1]]; Vector3 v3 = gw.LstVertex[lstCHVs[2]]; //构造两个平面PlaneOut和PlaneIn,分别表示顶面和底面 planeOut.Set(v1, v2, v3); planeIn.Normal = -planeOut.Normal; planeIn.Dist = -planeOut.Dist; planeIn.Translate(HULL2D_HALF_THICKNESS); planeOut.Translate(HULL2D_HALF_THICKNESS); Vector3 vOutNormal = planeOut.Normal; Vector3 vInNormal = planeIn.Normal; //分别求出PlaneOut,PlaneIn上的两点 Vector3 vOut = v1 + HULL2D_HALF_THICKNESS * vOutNormal; Vector3 vIn = v1 + HULL2D_HALF_THICKNESS * vInNormal; //构造顶点及顶点信息 int i; for (i = 0; i < lstCHVs.Count; i++) { //同时添加底面和顶面的一个顶点 Vector3 vec1 = gw.LstVertex[lstCHVs[i]]; Vector3 vec2 = vec1; if (i < 3) { vec1 += HULL2D_HALF_THICKNESS * vOutNormal; vec2 += HULL2D_HALF_THICKNESS * vInNormal; } else { Vector3 vDiff = vec1 - vOut; vec1 -= Vector3.Dot(vDiff, vOutNormal) * vOutNormal; vDiff = vec2 - vIn; vec2 -= Vector3.Dot(vDiff, vInNormal) * vInNormal; } LstVertecies.Add(vec1); LstVertecies.Add(vec2); //相应的,添加两个顶点信息 LstVertexInfo.Add(vInfo); LstVertexInfo.Add(vInfo); } //开始构造平面面片 //向外的面 Patch pat = new Patch(this); pat.Normal = vOutNormal; //几何信息 pat.Dist = planeOut.Dist; //依次向Neighbors中添加元素 VPNeighbor vpn = new VPNeighbor(); List <VPNeighbor> lstNeighbors1 = pat.LstNeighbors; for (i = 0; i < lstCHVs.Count; i++) { vpn = new VPNeighbor(); vpn.Vid = 2 * i; lstNeighbors1.Add(vpn); } //添加到链表 LstPatches.Add(pat); //向内的面 pat = new Patch(this); pat.Normal = vInNormal; //几何信息 pat.Dist = planeIn.Dist; //依次向Neighbors中添加元素 List <VPNeighbor> lstNeighbors2 = pat.LstNeighbors; //顶面按照逆序添加 for (i = lstCHVs.Count - 1; i >= 0; i--) { vpn = new VPNeighbor(); vpn.Vid = 2 * i + 1; lstNeighbors2.Add(vpn); } //添加到链表 LstPatches.Add(pat); //开始添加各个侧面 for (i = 0; i < lstCHVs.Count; i++) { pat = new Patch(this); List <VPNeighbor> lstNeighbors = pat.LstNeighbors; //每个侧面都是一个矩形 if (i < lstCHVs.Count - 1) { v1 = LstVertecies[2 * i + 2]; v2 = LstVertecies[2 * i]; v3 = LstVertecies[2 * i + 1]; pat.Set(v1, v2, v3); vpn = new VPNeighbor(); vpn.Vid = 2 * i; lstNeighbors.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = 2 * i + 1; lstNeighbors.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = 2 * i + 3; lstNeighbors.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = 2 * i + 2; lstNeighbors.Add(vpn); } else { //最后一个矩形的情况比较特殊 v1 = LstVertecies[0]; v2 = LstVertecies[2 * i]; v3 = LstVertecies[2 * i + 1]; pat.Set(v1, v2, v3); vpn = new VPNeighbor(); vpn.Vid = 2 * i; lstNeighbors.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = 2 * i + 1; lstNeighbors.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = 1; lstNeighbors.Add(vpn); vpn = new VPNeighbor(); vpn.Vid = 0; lstNeighbors.Add(vpn); } LstPatches.Add(pat); } } OriginPatchNum = LstPatches.Count; CurVNum = LstVertecies.Count; //初始化删除误差 if (mLstRemovedError == null) { mLstRemovedError = new List <float>(OriginPatchNum + 1); } mLstRemovedError.Clear(); for (int i = 0; i < OriginPatchNum + 1; i++) { mLstRemovedError.Add(-1.0f); //0,1,2,3都置为无效值 } ExceptionOccur = false; //计算每个patch的邻域patch ComputePatchNeighbors(); //HalfSpace::SetDistThresh(1e-3); //恢复到缺省的阈值 //寻找最小删除误差对应的面片 SearchLeastErrorPatch(); //开始简化,简化到头 ReduceAll(); return(true); }