/* Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Johns Hopkins University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "MarchingCubes.h" //////////// // Square // //////////// int Square::AntipodalCornerIndex(int idx){ int x,y; FactorCornerIndex(idx,x,y); return CornerIndex( (x+1)%2 , (y+1)%2 ); } int Square::CornerIndex( int x , int y ){ return (y<<1)|x; } void Square::FactorCornerIndex( int idx , int& x , int& y ){ x=(idx>>0)&1 , y=(idx>>1)&1; } int Square::EdgeIndex( int orientation , int i ) { switch( orientation ) { case 0: // x if( !i ) return 0; // (0,0) -> (1,0) else return 2; // (0,1) -> (1,1) case 1: // y if( !i ) return 3; // (0,0) -> (0,1) else return 1; // (1,0) -> (1,1) }; return -1; } void Square::FactorEdgeIndex(int idx,int& orientation,int& i){ switch(idx){ case 0: case 2: orientation=0; i=idx/2; return; case 1: case 3: orientation=1; i=((idx/2)+1)%2; return; }; } void Square::EdgeCorners(int idx,int& c1,int& c2){ int orientation,i; FactorEdgeIndex(idx,orientation,i); switch(orientation){ case 0: c1 = CornerIndex(0,i); c2 = CornerIndex(1,i); break; case 1: c1 = CornerIndex(i,0); c2 = CornerIndex(i,1); break; }; } int Square::ReflectEdgeIndex(int idx,int edgeIndex){ int orientation=edgeIndex%2; int o,i; FactorEdgeIndex(idx,o,i); if(o!=orientation){return idx;} else{return EdgeIndex(o,(i+1)%2);} } int Square::ReflectCornerIndex(int idx,int edgeIndex){ int orientation=edgeIndex%2; int x,y; FactorCornerIndex(idx,x,y); switch(orientation){ case 0: return CornerIndex((x+1)%2,y); case 1: return CornerIndex(x,(y+1)%2); }; return -1; } ////////// // Cube // ////////// int Cube::CornerIndex( int x , int y , int z ){ return (z<<2)|(y<<1)|x; } void Cube::FactorCornerIndex( int idx , int& x , int& y , int& z ){ x = (idx>>0)&1 , y = (idx>>1)&1 , z = (idx>>2)&1; } int Cube::EdgeIndex(int orientation,int i,int j){return (i | (j<<1))|(orientation<<2);} void Cube::FactorEdgeIndex( int idx , int& orientation , int& i , int &j ) { orientation=idx>>2; i = (idx&1); j = (idx&2)>>1; } int Cube::FaceIndex( int x , int y , int z ) { if ( x<0 ) return 0; else if( x>0 ) return 1; else if( y<0 ) return 2; else if( y>0 ) return 3; else if( z<0 ) return 4; else if( z>0 ) return 5; else return -1; } int Cube::FaceIndex( int dir , int offSet ){ return (dir<<1)|offSet; } void Cube::FactorFaceIndex( int idx , int& x , int& y , int& z ) { x=y=z=0; switch( idx ) { case 0: x=-1; break; case 1: x= 1; break; case 2: y=-1; break; case 3: y= 1; break; case 4: z=-1; break; case 5: z= 1; break; }; } void Cube::FactorFaceIndex( int idx , int& dir , int& offSet ) { dir = idx>>1; offSet=idx &1; } bool Cube::IsEdgeCorner( int cIndex , int e ) { int o , i , j; FactorEdgeIndex( e , o , i , j ); switch( o ) { case 0: return (cIndex && 2)==(i<<1) && (cIndex && 4)==(j<<2); case 1: return (cIndex && 1)==(i<<0) && (cIndex && 4)==(j<<2); case 2: return (cIndex && 4)==(i<<2) && (cIndex && 2)==(j<<1); default: return false; } } bool Cube::IsFaceCorner( int cIndex , int f ) { int dir , off; FactorFaceIndex( f , dir , off ); return ( cIndex & (1< (1,0) 1} // (1,0) -> (1,1) 2} // (0,1) -> (1,1) 3} // (0,0) -> (0,1) */ const int MarchingSquares::edgeMask[1< -> -> 9, // 1 -> 0 -> (0,0) -> 0,3 -> 9 3, // 2 -> 1 -> (1,0) -> 0,1 -> 3 10, // 3 -> 0,1 -> (0,0) (1,0) -> 1,3 -> 10 12, // 4 -> 2 -> (0,1) -> 2,3 -> 12 5, // 5 -> 0,2 -> (0,0) (0,1) -> 0,2 -> 5 15, // 6 -> 1,2 -> (1,0) (0,1) -> 0,1,2,3 -> 15 6, // 7 -> 0,1,2 -> (0,0) (1,0) (0,1) -> 1,2 -> 6 6, // 8 -> 3 -> (1,1) -> 1,2 -> 6 15, // 9 -> 0,3 -> (0,0) (1,1) -> 0,1,2,3 -> 15 5, // 10 -> 1,3 -> (1,0) (1,1) -> 0,2 -> 5 12, // 11 -> 0,1,3 -> (0,0) (1,0) (1,1) -> 2,3 -> 12 10, // 12 -> 2,3 -> (0,1) (1,1) -> 1,3 -> 10 3, // 13 -> 0,2,3 -> (0,0) (0,1) (1,1) -> 0,1 -> 3 9, // 14 -> 1,2,3 -> (1,0) (0,1) (1,1) -> 0,3 -> 9 0, // 15 -> 0,1,2,3 -> (0,0) (1,0) (0,1) (1,1) -> }; #if NEW_ORDERING /* 0} // (0,0) -> (1,0) 1} // (1,0) -> (1,1) 2} // (0,1) -> (1,1) 3} // (0,0) -> (0,1) */ const int MarchingSquares::edges[1<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(1,i,j)];}}} else if (y<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,0,j)];}}} else if (y>0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,1,j)];}}} else if (z<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,j,0)];}}} else if (z>0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,j,1)];}}} if (v[0][0] < iso) idx |= 1; if (v[1][0] < iso) idx |= 2; if (v[1][1] < iso) idx |= 4; if (v[0][1] < iso) idx |= 8; return idx; } bool MarchingCubes::IsAmbiguous( const double v[Cube::CORNERS] , double isoValue , int faceIndex ){ return MarchingSquares::IsAmbiguous( GetFaceIndex( v , isoValue , faceIndex ) ); } bool MarchingCubes::HasRoots( const double v[Cube::CORNERS] , double isoValue , int faceIndex ){ return MarchingSquares::HasRoots( GetFaceIndex( v , isoValue , faceIndex ) ); } bool MarchingCubes::HasRoots( const double v[Cube::CORNERS] , double isoValue ){ return HasRoots( GetIndex( v , isoValue ) ); } bool MarchingCubes::HasRoots( unsigned char mcIndex ){ return !(mcIndex==0 || mcIndex==255); } int MarchingCubes::AddTriangles( const double v[Cube::CORNERS] , double iso , Triangle* isoTriangles ) { unsigned char idx; int ntriang=0; Triangle tri; idx=GetIndex(v,iso); /* Cube is entirely in/out of the surface */ if (!edgeMask[idx]) return 0; /* Find the vertices where the surface intersects the cube */ int i,j,ii=1; for(i=0;i<12;i++){ if(edgeMask[idx] & ii){SetVertex(i,v,iso);} ii<<=1; } /* Create the triangle */ for( i=0 ; triangles[idx][i]!=-1 ; i+=3 ) { for(j=0;j<3;j++){ tri.p[0][j]=vertexList[triangles[idx][i+0]][j]; tri.p[1][j]=vertexList[triangles[idx][i+1]][j]; tri.p[2][j]=vertexList[triangles[idx][i+2]][j]; } isoTriangles[ntriang++]=tri; } return ntriang; } int MarchingCubes::AddTriangleIndices(const double v[Cube::CORNERS],double iso,int* isoIndices){ unsigned char idx; int ntriang=0; idx=GetIndex(v,iso); /* Cube is entirely in/out of the surface */ if (!edgeMask[idx]) return 0; /* Create the triangle */ for(int i=0;triangles[idx][i]!=-1;i+=3){ for(int j=0;j<3;j++){isoIndices[i+j]=triangles[idx][i+j];} ntriang++; } return ntriang; } void MarchingCubes::SetVertex( int e , const double values[Cube::CORNERS] , double iso ) { double t; int o , i1 , i2; Cube::FactorEdgeIndex( e , o , i1 , i2 ); switch( o ) { case 0: t = Interpolate( values[ Cube::CornerIndex( 0 , i1 , i2 ) ] - iso , values[ Cube::CornerIndex( 1 , i1 , i2 ) ] - iso ); vertexList[e][0] = t , vertexList[e][1] = i1 , vertexList[e][2] = i2; break; case 1: t = Interpolate( values[ Cube::CornerIndex( i1 , 0 , i2 ) ] - iso , values[ Cube::CornerIndex( i1 , 1 , i2 ) ] - iso ); vertexList[e][0] = i1 , vertexList[e][1] = t , vertexList[e][2] = i2; break; case 2: t = Interpolate( values[ Cube::CornerIndex( i1 , i2 , 0 ) ] - iso , values[ Cube::CornerIndex( i1 , i2 , 1 ) ] - iso ); vertexList[e][0] = i1 , vertexList[e][1] = i2 , vertexList[e][2] = t; break; } } double MarchingCubes::Interpolate( double v1 , double v2 ) { return v1/(v1-v2); } /////////////////////////////////// unsigned char MarchingCubes::GetIndex(const float v[Cube::CORNERS],float iso){ unsigned char idx=0; if (v[Cube::CornerIndex(0,0,0)] < iso) idx |= 1; if (v[Cube::CornerIndex(1,0,0)] < iso) idx |= 2; if (v[Cube::CornerIndex(1,1,0)] < iso) idx |= 4; if (v[Cube::CornerIndex(0,1,0)] < iso) idx |= 8; if (v[Cube::CornerIndex(0,0,1)] < iso) idx |= 16; if (v[Cube::CornerIndex(1,0,1)] < iso) idx |= 32; if (v[Cube::CornerIndex(1,1,1)] < iso) idx |= 64; if (v[Cube::CornerIndex(0,1,1)] < iso) idx |= 128; return idx; } unsigned char MarchingCubes::GetFaceIndex( const float values[Cube::CORNERS] , float iso , int faceIndex ) { int i,j,x,y,z; unsigned char idx=0; double v[2][2]; Cube::FactorFaceIndex(faceIndex,x,y,z); if (x<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(0,i,j)];}}} else if (x>0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(1,i,j)];}}} else if (y<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,0,j)];}}} else if (y>0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,1,j)];}}} else if (z<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,j,0)];}}} else if (z>0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=values[Cube::CornerIndex(i,j,1)];}}} if (v[0][0] < iso) idx |= 1; if (v[1][0] < iso) idx |= 2; if (v[1][1] < iso) idx |= 4; if (v[0][1] < iso) idx |= 8; return idx; } unsigned char MarchingCubes::GetFaceIndex( unsigned char mcIndex , int faceIndex ) { int i,j,x,y,z; unsigned char idx=0; int v[2][2]; Cube::FactorFaceIndex(faceIndex,x,y,z); if (x<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=mcIndex&(1<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=mcIndex&(1<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=mcIndex&(1<0){for(i=0;i<2;i++){for(j=0;j<2;j++){v[i][j]=mcIndex&(1<