public static void Polyganise(Strucs.GridCell cell, List <Vector3> vertices, float isovalue) { Vector3[] vertlist = new Vector3[12]; int i, ntriang; int cubeindex; cubeindex = 0; if (cell.Points[0].Density < isovalue) { cubeindex |= 1; } if (cell.Points[1].Density < isovalue) { cubeindex |= 2; } if (cell.Points[2].Density < isovalue) { cubeindex |= 4; } if (cell.Points[3].Density < isovalue) { cubeindex |= 8; } if (cell.Points[4].Density < isovalue) { cubeindex |= 16; } if (cell.Points[5].Density < isovalue) { cubeindex |= 32; } if (cell.Points[6].Density < isovalue) { cubeindex |= 64; } if (cell.Points[7].Density < isovalue) { cubeindex |= 128; } /* Cube is entirely in/out of the surface */ if (edgeTable[cubeindex] == 0) { return; } /* Find the vertices where the surface intersects the cube */ if ((edgeTable[cubeindex] & 1) == 1) { vertlist[0] = Utility.Lerp(isovalue, cell.Points[0], cell.Points[1]); } if ((edgeTable[cubeindex] & 2) == 2) { vertlist[1] = Utility.Lerp(isovalue, cell.Points[1], cell.Points[2]); } if ((edgeTable[cubeindex] & 4) == 4) { vertlist[2] = Utility.Lerp(isovalue, cell.Points[2], cell.Points[3]); } if ((edgeTable[cubeindex] & 8) == 8) { vertlist[3] = Utility.Lerp(isovalue, cell.Points[3], cell.Points[0]); } if ((edgeTable[cubeindex] & 16) == 16) { vertlist[4] = Utility.Lerp(isovalue, cell.Points[4], cell.Points[5]); } if ((edgeTable[cubeindex] & 32) == 32) { vertlist[5] = Utility.Lerp(isovalue, cell.Points[5], cell.Points[6]); } if ((edgeTable[cubeindex] & 64) == 64) { vertlist[6] = Utility.Lerp(isovalue, cell.Points[6], cell.Points[7]); } if ((edgeTable[cubeindex] & 128) == 128) { vertlist[7] = Utility.Lerp(isovalue, cell.Points[7], cell.Points[4]); } if ((edgeTable[cubeindex] & 256) == 256) { vertlist[8] = Utility.Lerp(isovalue, cell.Points[0], cell.Points[4]); } if ((edgeTable[cubeindex] & 512) == 512) { vertlist[9] = Utility.Lerp(isovalue, cell.Points[1], cell.Points[5]); } if ((edgeTable[cubeindex] & 1024) == 1024) { vertlist[10] = Utility.Lerp(isovalue, cell.Points[2], cell.Points[6]); } if ((edgeTable[cubeindex] & 2048) == 2048) { vertlist[11] = Utility.Lerp(isovalue, cell.Points[3], cell.Points[7]); } /* Create the triangle */ ntriang = 0; for (i = 0; triTable[cubeindex][i] != -1; i += 3) { ntriang++; } for (i = 0; triTable[cubeindex][i] != -1; i++) { vertices.Add(vertlist[triTable[cubeindex][i]]); } }
public static void PolyganiseTetrahedron(Strucs.GridCell cell, List <Vector3> vertices, float isovalue) { int triangleIndex = 0; if (cell.Points[0].Density < isovalue) { triangleIndex |= 1; } if (cell.Points[1].Density < isovalue) { triangleIndex |= 2; } if (cell.Points[2].Density < isovalue) { triangleIndex |= 4; } if (cell.Points[3].Density < isovalue) { triangleIndex |= 8; } // these cases were taken from https://github.com/Calvin-L/MarchingTetrahedrons/blob/master/Decimate.cpp switch (triangleIndex) { case 0: case 15: break; case 0x01: vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[1])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[2])); break; case 0x02: vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); break; case 0x04: vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[1])); break; case 0x08: vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[1])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); break; case 0x03: vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[1])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); break; case 0x05: vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[3])); break; case 0x09: vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[1])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[2])); break; case 0x06: vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[1])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[2])); break; case 0x0C: vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[1])); break; case 0x0A: vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); break; case 0x07: vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[3], cell.Points[1])); break; case 0x0B: vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[1])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[2], cell.Points[0])); break; case 0x0D: vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[0])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[3])); vertices.Add(Utility.Lerp(isovalue, cell.Points[1], cell.Points[2])); break; case 0x0E: vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[1])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[2])); vertices.Add(Utility.Lerp(isovalue, cell.Points[0], cell.Points[3])); break; } }