void marchCubes(vec3 position,vec3 size,int numCubes,std::vector<vec3>& vertices,std::vector<sphere>* spheres){ _spheres = spheres; vec3 currentPosition = position; vec3 step = size;step/=float(numCubes); const int runs = numCubes*numCubes*numCubes; int nextX = numCubes - 1; int nextY = numCubes*numCubes - 1; std::cout<<"Running marching cubes( "<<runs<<" runs)"<<std::endl; //instead of for(x for(y for(z run single loop for increased efficiency (hopefully) for(int i = 0;i<runs;i++){ marchCube(currentPosition,step,vertices); currentPosition.x += step.x; if( i == nextX ){ currentPosition.x = position.x; nextX += numCubes; currentPosition.y += step.y; if( i == nextY ){ std::cout<<" "<<((float)nextY/(float)runs)*100.0<<"% done.."<<std::endl; currentPosition.y = position.y; nextY+=numCubes*numCubes; currentPosition.z+=step.z; } } } std::cout<<" "<<vertices.size()/3<<" triangles generated!"<<std::endl; }
void mbChargeLattice::recurseCube(mbLatticeCube *cube) { mbLatticeCube *adjacentCube; int xIndex, yIndex, zIndex; int *latticeLocation = cube->latticePosition; xIndex = latticeLocation[0]; yIndex = latticeLocation[1]; zIndex = latticeLocation[2]; float axisCases[6][3] = { {xIndex + 1, yIndex, zIndex}, {xIndex - 1, yIndex, zIndex}, {xIndex, yIndex + 1, zIndex}, {xIndex, yIndex - 1, zIndex}, {xIndex, yIndex, zIndex + 1}, {xIndex, yIndex, zIndex - 1}, }; float* currentCase; int i; // Test 6 axis cases. for (i = 0; i < 6; i++) { currentCase = axisCases[i]; adjacentCube = getCube(currentCase[0], currentCase[1], currentCase[2]); if (adjacentCube != NULL && adjacentCube->lastFrameVisited != currentFrame) { adjacentCube->lastFrameVisited = currentFrame; if (marchCube(adjacentCube)) recurseCube(adjacentCube); } } }
// marchingCubes iterates over the entire dataset, calling marchCube on each cube void marchingCubes() { for (int x = 0; x < dataSetSize; x++) { for (int y = 0; y < dataSetSize; y++) { for (int z = 0; z < dataSetSize; z++) { marchCube(x*stepSize, y*stepSize, z*stepSize, stepSize); } } } }
void mbChargeLattice::march() { int i, xIndex, yIndex, zIndex; mbChargeNode *node; mbLatticeCube *cube; for (i = 0; i < chargeNodes.size(); i++) { node = &chargeNodes[i]; //std::cout << "Searching node " << i << std::endl; xIndex = (int)((node->x + .5f) * xDim); yIndex = (int)((node->y + .5f) * yDim); zIndex = (int)((node->z + .5f) * zDim); //std::cout << xIndex << ',' << yIndex << ',' << zIndex << std::endl; while (zIndex >= 0) { cube = getCube(xIndex, yIndex, zIndex); //std::cout << "Cube is null " << (cube == NULL) << std::endl; if (cube != NULL && cube->lastFrameVisited != currentFrame) { //std::cout << "marching cube" << xIndex << yIndex << zIndex << std::endl; if (marchCube(cube)) { //std::cout << "recursing cube" << xIndex << yIndex << zIndex << std::endl; recurseCube(cube); zIndex--; } cube->lastFrameVisited = currentFrame; } else { zIndex = -1; } zIndex--; } } }
//Draw function void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -15.0f); glRotatef(0.0f, 1.0f, 0.0f, 0.0f); glRotatef(0.0f, 0.0f, 1.0f, 0.0f); GLfloat ambientColor[] = {0.4f, 0.4f, 0.4f, 1.0f}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor); GLfloat lightColor0[] = {0.6f, 0.6f, 0.6f, 1.0f}; GLfloat lightPos0[] = {-0.5f, 0.8f, 0.1f, 0.0f}; glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0); glLightfv(GL_LIGHT0, GL_POSITION, lightPos0); float scale = 10.0f / 341.0; glScalef(scale, scale, scale); glTranslatef(-170.0f, -170.0f, -170.0f); //marching cube implementation for isosurface extraction. marchCube(); }