public Mesh GenerateSurface() { d = bmp.depth; h = bmp.height; w = bmp.width; wh = w * h; Int16Triple[] temp = new Int16Triple[8]; Mesh m = new Mesh(); OriginalTriangle[] tempTriangles = new OriginalTriangle[4]; SMCTriangleNetHashTable hash = new SMCTriangleNetHashTable(0, 0, w, h); for (int k = 0; k <= d - 1; k++) { for (int j = 0; j <= h - 1; j++) { for (int i = 0; i <= w - 1; i++) { byte value = GetConfig(temp, bmp, i, j, k); if (value == 0 || value == 255) { continue; } int tcount = ExtractTriangles(temp, value, i, j, k, tempTriangles); for (int tindex = 0; tindex < tcount; tindex++) { MergeTriangleIntoMesh(m, hash, tempTriangles[tindex]); } } } hash.IncreaseIndex(); } return(m); }
private void MergeTriangleIntoMesh(Mesh mesh, SMCTriangleNetHashTable hashMap, OriginalTriangle ot) { int p0x = ot.P0.X; int p0y = ot.P0.Y; int p0z = ot.P0.Z; int p1x = ot.P1.X; int p1y = ot.P1.Y; int p1z = ot.P1.Z; int p2x = ot.P2.X; int p2y = ot.P2.Y; int p2z = ot.P2.Z; int p0i; int p1i; int p2i; int index = 0; index = hashMap.GetHashValue(p0x, p0y, p0z); if (index == -1) { p0i = mesh.AddVertex(new Point3d(p0x, p0y, p0z)); hashMap.SetHashValue(p0x, p0y, p0z, p0i); } else { p0i = index; } index = hashMap.GetHashValue(p1x, p1y, p1z); if (index == -1) { p1i = mesh.AddVertex(new Point3d(p1x, p1y, p1z)); hashMap.SetHashValue(p1x, p1y, p1z, p1i); } else { p1i = index; } index = hashMap.GetHashValue(p2x, p2y, p2z); if (index == -1) { p2i = mesh.AddVertex(new Point3d(p2x, p2y, p2z)); hashMap.SetHashValue(p2x, p2y, p2z, p2i); } else { p2i = index; } Triangle t = new Triangle(p0i, p1i, p2i); mesh.AddFace(t); }