// Given a reference to a GridCell and an isolevel, calculate the // triangulation representing the intersection of the GridCell and the // isosurface public static void Triangulate(ref GridCell cell, float isolevel) { // Clear any calculations made in the GridCell cell.Clear(); // Index into tables representing configuration number of GridCell int index = 0; // Calculate configuration number by determining which vertices are // below the isosurface // // Note: The "|=" operator sets the bit at the index given, // essentially adding that value to the index if it not already set for (int i = 0; i < 8; i++) { if (cell.p[i].Value < isolevel) { index |= 1 << i; } } // Return if the GridCell does not intersect the isosurface at all if (edgeTable[index] == 0) { return; } // Calculate interpolated edge position for each edge in the GridCell // // Note: The expression "edgeTable[index] & (1 << position)) != 0" // returns true if the bit at the given position is set for (int i = 0; i < 12; i++) { if ((edgeTable[index] & (1 << i)) != 0) { cell.edgepoints[i] = Interpolate(cell.p[vertexTable[i, 0]], cell.p[vertexTable[i, 1]], isolevel); } } // Determine triangles for GridCell configuration for (int i = 0; triangleTable[index, i] != -1; i += 3) { cell.triangles[cell.numTriangles].p[0] = cell.edgepoints[triangleTable[index, i]]; cell.triangles[cell.numTriangles].p[1] = cell.edgepoints[triangleTable[index, i + 1]]; cell.triangles[cell.numTriangles].p[2] = cell.edgepoints[triangleTable[index, i + 2]]; cell.numTriangles++; } }