private void PolygonizeCubes() { this.NumTriangles = 0; MarchingCubesVertex[] vertlist = new MarchingCubesVertex[12]; for (int k = 0; k < this.Z - 1; k++) { for (int j = 0; j < this.Y - 1; j++) { for (int i = 0; i < this.X - 1; i++) { this.PolygonizeCube(i, j, k, vertlist); } } } }
private void PolygonizeCube(int i, int j, int k, MarchingCubesVertex[] vertlist) { int index = 0; if (this.isInside[(k * this.Y + j) * this.X + i] < 0f) { index = 1; //|= 1; } //x+1 if (this.isInside[(k * this.Y + j) * this.X + i + 1] < 0f) { index |= 2; } //z+1, x+1 if (this.isInside[((k + 1) * this.Y + j) * this.X + i + 1] < 0f) { index |= 4; } //z+1 if (this.isInside[((k + 1) * this.Y + j) * this.X + i] < 0f) { index |= 8; } //y+1 if (this.isInside[(k * this.Y + j + 1) * this.X + i] < 0f) { index |= 16; } //y+1, x+1 if (this.isInside[(k * this.Y + j + 1) * this.X + i + 1] < 0f) { index |= 32; } //z+1, y+1, x+1 if (this.isInside[((k + 1) * this.Y + j + 1) * this.X + i + 1] < 0f) { index |= 64; } //z+1, y+1 if (this.isInside[((k + 1) * this.Y + j + 1) * this.X + i] < 0f) { index |= 128; } //index fungerar som ett gäng flaggor, 255 = 11111111 = alla if-satser kördes //om index tex = 21 = 10101 betyder det att if-sats nr 1, 3 och 5 kördes if (Data.edgeTable[index] != 0) { if ((Data.edgeTable[index] & 1) != 0) { //x-1? vertlist[0] = this.xv[(k * this.Y + j) * (this.X - 1) + i]; } if ((Data.edgeTable[index] & 2) != 0) { vertlist[1] = this.zv[((k * this.Y + j) * this.X) + i + 1]; } if ((Data.edgeTable[index] & 4) != 0) { vertlist[2] = this.xv[((((k + 1) * this.Y) + j) * (this.X - 1)) + i]; } if ((Data.edgeTable[index] & 8) != 0) { vertlist[3] = this.zv[(((k * this.Y) + j) * this.X) + i]; } if ((Data.edgeTable[index] & 16) != 0) { vertlist[4] = this.xv[(((k * this.Y) + (j + 1)) * (this.X - 1)) + i]; } if ((Data.edgeTable[index] & 32) != 0) { vertlist[5] = this.zv[(((k * this.Y) + (j + 1)) * this.X) + (i + 1)]; } if ((Data.edgeTable[index] & 64) != 0) { vertlist[6] = this.xv[((((k + 1) * this.Y) + (j + 1)) * (this.X - 1)) + i]; } if ((Data.edgeTable[index] & 128) != 0) { vertlist[7] = this.zv[(((k * this.Y) + (j + 1)) * this.X) + i]; } if ((Data.edgeTable[index] & 256) != 0) { vertlist[8] = this.yv[(((k * (this.Y - 1)) + j) * this.X) + i]; } if ((Data.edgeTable[index] & 512) != 0) { vertlist[9] = this.yv[(((k * (this.Y - 1)) + j) * this.X) + (i + 1)]; } if ((Data.edgeTable[index] & 1024) != 0) { vertlist[10] = this.yv[((((k + 1) * (this.Y - 1)) + j) * this.X) + (i + 1)]; } if ((Data.edgeTable[index] & 2048) != 0) { vertlist[11] = this.yv[((((k + 1) * (this.Y - 1)) + j) * this.X) + i]; } for (int m = 0; Data.triTable[index, m] != -1; m += 3) { this.Triangles[this.NumTriangles].v0 = vertlist[Data.triTable[index, m]]; this.Triangles[this.NumTriangles].v1 = vertlist[Data.triTable[index, m + 1]]; this.Triangles[this.NumTriangles].v2 = vertlist[Data.triTable[index, m + 2]]; this.NumTriangles++; } } }