public static SoftBody CreateFromTriMesh(SoftBodyWorldInfo worldInfo, Vector3[] vertices, int[] triangles, bool randomizeConstraints = true) { int numTriangleIndices = triangles.Length; int numTriangles = numTriangleIndices / 3; int maxIndex = 0; // triangles.Max() + 1; for (int i = 0; i < numTriangleIndices; i++) { if (triangles[i] > maxIndex) { maxIndex = triangles[i]; } } maxIndex++; var psb = new SoftBody(worldInfo, maxIndex, vertices, null); BitArray chks = new BitArray(maxIndex * maxIndex); for (int i = 0; i < numTriangleIndices; i += 3) { int[] idx = new int[] { triangles[i], triangles[i + 1], triangles[i + 2] }; for (int j = 2, k = 0; k < 3; j = k++) { int chkIndex = maxIndex * idx[k] + idx[j]; if (!chks[chkIndex]) { chks[chkIndex] = true; chks[maxIndex * idx[j] + idx[k]] = true; psb.AppendLink(idx[j], idx[k]); } } psb.AppendFace(idx[0], idx[1], idx[2]); } if (randomizeConstraints) { psb.RandomizeConstraints(); } return(psb); }
SoftBody CreateFromIndexedMesh(Vector3Array vertexArray, IntArray triangleVertexIndexArray, bool createBendLinks) { SoftBody softBody = new SoftBody(SoftWorld.WorldInfo, vertexArray, null); Material structuralMaterial = softBody.AppendMaterial(); Material bendMaterial; if (createBendLinks) { bendMaterial = softBody.AppendMaterial(); bendMaterial.Lst = 0.7f; } else { bendMaterial = null; } structuralMaterial.Lst = 1.0f; int numVertices = vertexArray.Count; int numTriangles = triangleVertexIndexArray.Count / 3; // List of values for each link saying which triangle is associated with that link // -1 to start. Once a value is entered we know the "other" triangle // and can add a link across the link AlignedIntArray triangleForLinks = new AlignedIntArray(); triangleForLinks.Resize(numVertices * numVertices, -1); for (int triangle = 0; triangle < numTriangles; ++triangle) { int[] index = new int[] { triangleVertexIndexArray[triangle * 3], triangleVertexIndexArray[triangle * 3 + 1], triangleVertexIndexArray[triangle * 3 + 2] }; softBody.AppendFace(index[0], index[1], index[2]); // Generate the structural links directly from the triangles TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[0], index[1], index[2], structuralMaterial, createBendLinks, bendMaterial); TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[1], index[2], index[0], structuralMaterial, createBendLinks, bendMaterial); TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[2], index[0], index[1], structuralMaterial, createBendLinks, bendMaterial); } return(softBody); }
public static SoftBody CreatePatch(SoftBodyWorldInfo worldInfo, Vector3 corner00, Vector3 corner10, Vector3 corner01, Vector3 corner11, int resolutionX, int resolutionY, int fixedCorners, bool generateDiagonals) { // Create nodes if ((resolutionX < 2) || (resolutionY < 2)) { return(null); } int rx = resolutionX; int ry = resolutionY; int total = rx * ry; var positions = new Vector3[total]; var masses = new double[total]; for (int y = 0; y < ry; y++) { double ty = y / (double)(ry - 1); Vector3 py0, py1; Vector3.Lerp(ref corner00, ref corner01, ty, out py0); Vector3.Lerp(ref corner10, ref corner11, ty, out py1); for (int ix = 0; ix < rx; ix++) { double tx = ix / (double)(rx - 1); int index = rx * y + ix; Vector3.Lerp(ref py0, ref py1, tx, out positions[index]); masses[index] = 1; } } var body = new SoftBody(worldInfo, total, positions, masses); if ((fixedCorners & 1) != 0) { body.SetMass(0, 0); } if ((fixedCorners & 2) != 0) { body.SetMass(rx - 1, 0); } if ((fixedCorners & 4) != 0) { body.SetMass(rx * (ry - 1), 0); } if ((fixedCorners & 8) != 0) { body.SetMass(rx * (ry - 1) + rx - 1, 0); } // Create links and faces for (int y = 0; y < ry; ++y) { for (int x = 0; x < rx; ++x) { int ixy = rx * y + x; int ix1y = ixy + 1; int ixy1 = rx * (y + 1) + x; bool mdx = (x + 1) < rx; bool mdy = (y + 1) < ry; if (mdx) { body.AppendLink(ixy, ix1y); } if (mdy) { body.AppendLink(ixy, ixy1); } if (mdx && mdy) { int ix1y1 = ixy1 + 1; if (((x + y) & 1) != 0) { body.AppendFace(ixy, ix1y, ix1y1); body.AppendFace(ixy, ix1y1, ixy1); if (generateDiagonals) { body.AppendLink(ixy, ix1y1); } } else { body.AppendFace(ixy1, ixy, ix1y); body.AppendFace(ixy1, ix1y, ix1y1); if (generateDiagonals) { body.AppendLink(ix1y, ixy1); } } } } } return(body); }
public static SoftBody CreatePatch(SoftBodyWorldInfo worldInfo, Vector3 corner00, Vector3 corner10, Vector3 corner01, Vector3 corner11, int resx, int resy, int fixeds, bool gendiags) { // Create nodes if ((resx < 2) || (resy < 2)) { return(null); } int rx = resx; int ry = resy; int tot = rx * ry; Vector3[] x = new Vector3[tot]; float[] m = new float[tot]; for (int iy = 0; iy < ry; iy++) { float ty = iy / (float)(ry - 1); Vector3 py0, py1; Vector3.Lerp(ref corner00, ref corner01, ty, out py0); Vector3.Lerp(ref corner10, ref corner11, ty, out py1); for (int ix = 0; ix < rx; ix++) { float tx = ix / (float)(rx - 1); int index = rx * iy + ix; Vector3.Lerp(ref py0, ref py1, tx, out x[index]); m[index] = 1; } } SoftBody psb = new SoftBody(worldInfo, tot, x, m); if ((fixeds & 1) != 0) { psb.SetMass(0, 0); } if ((fixeds & 2) != 0) { psb.SetMass(rx - 1, 0); } if ((fixeds & 4) != 0) { psb.SetMass(rx * (ry - 1), 0); } if ((fixeds & 8) != 0) { psb.SetMass(rx * (ry - 1) + rx - 1, 0); } // Create links and faces for (int iy = 0; iy < ry; ++iy) { for (int ix = 0; ix < rx; ++ix) { int ixy = rx * iy + ix; int ix1y = ixy + 1; int ixy1 = rx * (iy + 1) + ix; bool mdx = (ix + 1) < rx; bool mdy = (iy + 1) < ry; if (mdx) { psb.AppendLink(ixy, ix1y); } if (mdy) { psb.AppendLink(ixy, ixy1); } if (mdx && mdy) { int ix1y1 = ixy1 + 1; if (((ix + iy) & 1) != 0) { psb.AppendFace(ixy, ix1y, ix1y1); psb.AppendFace(ixy, ix1y1, ixy1); if (gendiags) { psb.AppendLink(ixy, ix1y1); } } else { psb.AppendFace(ixy1, ixy, ix1y); psb.AppendFace(ixy1, ix1y, ix1y1); if (gendiags) { psb.AppendLink(ix1y, ixy1); } } } } } return(psb); }
private static void GenerateBoundaryFaces(SoftBody body) { int counter = 0; foreach (Node node in body.Nodes) { node.Index = counter++; } List <int[]> indices = new List <int[]>(body.Tetras.Count); foreach (Tetra tetra in body.Tetras) { NodePtrArray nodes = tetra.Nodes; int[] index = new int[] { nodes[0].Index, nodes[1].Index, nodes[2].Index, nodes[3].Index }; indices.Add(index); } var faceMap = new List <KeyValuePair <int[], int[]> >(); foreach (int[] tetraIndices in indices) { for (int i = 0; i < 4; i++) { int[] face; switch (i) { case 0: face = new int[] { tetraIndices[1], tetraIndices[0], tetraIndices[2] }; break; case 1: face = new int[] { tetraIndices[3], tetraIndices[0], tetraIndices[1] }; break; case 2: face = new int[] { tetraIndices[3], tetraIndices[1], tetraIndices[2] }; break; default: face = new int[] { tetraIndices[2], tetraIndices[0], tetraIndices[3] }; break; } List <int> faceSorted = new List <int>(face); faceSorted.Sort(); bool removed = false; for (int j = 0; j < faceMap.Count; j++) { KeyValuePair <int[], int[]> f = faceMap[j]; int[] faceInMap = f.Key; if (faceInMap[0] == faceSorted[0] && faceInMap[1] == faceSorted[1] && faceInMap[2] == faceSorted[2]) { faceMap.RemoveAt(j); j--; removed = true; break; } } if (!removed) { faceMap.Add(new KeyValuePair <int[], int[]>(faceSorted.ToArray(), face)); } } } foreach (var faceInMap in faceMap) { int[] face = faceInMap.Value; body.AppendFace(face[0], face[1], face[2]); } }