public CHull canMerge(CHull a, CHull b) { if (!a.overlap(b)) // if their AABB's (with a little slop) don't overlap, then return. { return(null); } CHull ret = null; // ok..we are going to combine both meshes into a single mesh // and then we are going to compute the concavity... VertexPool vc = new VertexPool(); List <int> indices = new List <int>(); getMesh(a.mResult, vc, indices); getMesh(b.mResult, vc, indices); int vcount = vc.GetSize(); List <float3> vertices = vc.GetVertices(); int tcount = indices.Count / 3; //don't do anything if hull is empty if (tcount == 0) { vc.Clear(); return(null); } HullResult hresult = new HullResult(); HullDesc desc = new HullDesc(); desc.SetHullFlag(HullFlag.QF_TRIANGLES); desc.Vertices = vertices; HullError hret = HullUtils.CreateConvexHull(desc, ref hresult); if (hret == HullError.QE_OK) { float combineVolume = Concavity.computeMeshVolume(hresult.OutputVertices, hresult.Indices); float sumVolume = a.mVolume + b.mVolume; float percent = (sumVolume * 100) / combineVolume; if (percent >= (100.0f - MERGE_PERCENT)) { ConvexResult cr = new ConvexResult(hresult.OutputVertices, hresult.Indices); ret = new CHull(cr); } } vc.Clear(); return(ret); }
// compute's how 'concave' this object is and returns the total volume of the // convex hull as well as the volume of the 'concavity' which was found. public static float computeConcavity(List <float3> vertices, List <int> indices, ref float4 plane, ref float volume) { float cret = 0f; volume = 1f; HullResult result = new HullResult(); HullDesc desc = new HullDesc(); desc.MaxFaces = 256; desc.MaxVertices = 256; desc.SetHullFlag(HullFlag.QF_TRIANGLES); desc.Vertices = vertices; HullError ret = HullUtils.CreateConvexHull(desc, ref result); if (ret == HullError.QE_OK) { volume = computeMeshVolume2(result.OutputVertices, result.Indices); // ok..now..for each triangle on the original mesh.. // we extrude the points to the nearest point on the hull. List <CTri> tris = new List <CTri>(); for (int i = 0; i < result.Indices.Count / 3; i++) { int i1 = result.Indices[i * 3 + 0]; int i2 = result.Indices[i * 3 + 1]; int i3 = result.Indices[i * 3 + 2]; float3 p1 = result.OutputVertices[i1]; float3 p2 = result.OutputVertices[i2]; float3 p3 = result.OutputVertices[i3]; CTri t = new CTri(p1, p2, p3, i1, i2, i3); tris.Add(t); } // we have not pre-computed the plane equation for each triangle in the convex hull.. float totalVolume = 0; List <CTri> ftris = new List <CTri>(); // 'feature' triangles. List <CTri> input_mesh = new List <CTri>(); for (int i = 0; i < indices.Count / 3; i++) { int i1 = indices[i * 3 + 0]; int i2 = indices[i * 3 + 1]; int i3 = indices[i * 3 + 2]; float3 p1 = vertices[i1]; float3 p2 = vertices[i2]; float3 p3 = vertices[i3]; CTri t = new CTri(p1, p2, p3, i1, i2, i3); input_mesh.Add(t); } for (int i = 0; i < indices.Count / 3; i++) { int i1 = indices[i * 3 + 0]; int i2 = indices[i * 3 + 1]; int i3 = indices[i * 3 + 2]; float3 p1 = vertices[i1]; float3 p2 = vertices[i2]; float3 p3 = vertices[i3]; CTri t = new CTri(p1, p2, p3, i1, i2, i3); featureMatch(t, tris, input_mesh); if (t.mConcavity > 0.05f) { float v = t.getVolume(); totalVolume += v; ftris.Add(t); } } SplitPlane.computeSplitPlane(vertices, indices, ref plane); cret = totalVolume; } return(cret); }
public int process(DecompDesc desc) { int ret = 0; MAXDEPTH = (int)desc.mDepth; CONCAVE_PERCENT = desc.mCpercent; MERGE_PERCENT = desc.mPpercent; ConvexDecomposition.calcConvexDecomposition(desc.mVertices, desc.mIndices, ConvexDecompResult, 0f, 0, MAXDEPTH, CONCAVE_PERCENT, MERGE_PERCENT); while (combineHulls()) // keep combinging hulls until I can't combine any more... { ; } int i; for (i = 0; i < mChulls.Count; i++) { CHull cr = mChulls[i]; // before we hand it back to the application, we need to regenerate the hull based on the // limits given by the user. ConvexResult c = cr.mResult; // the high resolution hull... HullResult result = new HullResult(); HullDesc hdesc = new HullDesc(); hdesc.SetHullFlag(HullFlag.QF_TRIANGLES); hdesc.Vertices = c.HullVertices; hdesc.MaxVertices = desc.mMaxVertices; // maximum number of vertices allowed in the output if (desc.mSkinWidth != 0f) { hdesc.SkinWidth = desc.mSkinWidth; hdesc.SetHullFlag(HullFlag.QF_SKIN_WIDTH); // do skin width computation. } HullError ret2 = HullUtils.CreateConvexHull(hdesc, ref result); if (ret2 == HullError.QE_OK) { ConvexResult r = new ConvexResult(result.OutputVertices, result.Indices); r.mHullVolume = Concavity.computeMeshVolume(result.OutputVertices, result.Indices); // the volume of the hull. mCallback(r); } result = null; cr.Dispose(); } ret = mChulls.Count; mChulls.Clear(); return(ret); }