Пример #1
0
        ///<summary>
        /// Given a grid cell and an isolevel, calculate the triangular facets required to represent the isosurface through the cell.
        /// Return the number of triangular facets, the array "triangles" will be loaded up with the vertices at most 5 triangular facets.
        /// 0 will be returned if the grid cell is either totally above of totally below the isolevel.
        ///</summary>
        public static int Polygonise( GRIDCELL grid, float isolevel, TRIANGLE[] triangles )
        {
            int i, ntriang;
            int cubeindex;
            Vector3[] vertlist = new Vector3[ 12 ];

            // Determine the index into the edge table which tells us which vertices are inside of the surface
            cubeindex = 0;
            if( grid.val[ 0 ] < isolevel )
            {
                cubeindex |= 1;
            }
            if( grid.val[ 1 ] < isolevel )
            {
                cubeindex |= 2;
            }
            if( grid.val[ 2 ] < isolevel )
            {
                cubeindex |= 4;
            }
            if( grid.val[ 3 ] < isolevel )
            {
                cubeindex |= 8;
            }
            if( grid.val[ 4 ] < isolevel )
            {
                cubeindex |= 16;
            }
            if( grid.val[ 5 ] < isolevel )
            {
                cubeindex |= 32;
            }
            if( grid.val[ 6 ] < isolevel )
            {
                cubeindex |= 64;
            }
            if( grid.val[ 7 ] < isolevel )
            {
                cubeindex |= 128;
            }

            // Cube is entirely in/out of the surface
            if( edgeTable[ cubeindex ] == 0 )
            {
                return 0;
            }

            // Find the vertices where the surface intersects the cube
            if( ( edgeTable[ cubeindex ] & 1 ) != 0x0 )
            {
                vertlist[ 0 ] = VertexInterp( isolevel, grid.p[ 0 ], grid.p[ 1 ], grid.val[ 0 ], grid.val[ 1 ] );
            }
            if( ( edgeTable[ cubeindex ] & 2 ) != 0x0 )
            {
                vertlist[ 1 ] = VertexInterp( isolevel, grid.p[ 1 ], grid.p[ 2 ], grid.val[ 1 ], grid.val[ 2 ] );
            }
            if( ( edgeTable[ cubeindex ] & 4 ) != 0x0 )
            {
                vertlist[ 2 ] = VertexInterp( isolevel, grid.p[ 2 ], grid.p[ 3 ], grid.val[ 2 ], grid.val[ 3 ] );
            }
            if( ( edgeTable[ cubeindex ] & 8 ) != 0x0 )
            {
                vertlist[ 3 ] = VertexInterp( isolevel, grid.p[ 3 ], grid.p[ 0 ], grid.val[ 3 ], grid.val[ 0 ] );
            }
            if( ( edgeTable[ cubeindex ] & 16 ) != 0x0 )
            {
                vertlist[ 4 ] = VertexInterp( isolevel, grid.p[ 4 ], grid.p[ 5 ], grid.val[ 4 ], grid.val[ 5 ] );
            }
            if( ( edgeTable[ cubeindex ] & 32 ) != 0x0 )
            {
                vertlist[ 5 ] = VertexInterp( isolevel, grid.p[ 5 ], grid.p[ 6 ], grid.val[ 5 ], grid.val[ 6 ] );
            }
            if( ( edgeTable[ cubeindex ] & 64 ) != 0x0 )
            {
                vertlist[ 6 ] = VertexInterp( isolevel, grid.p[ 6 ], grid.p[ 7 ], grid.val[ 6 ], grid.val[ 7 ] );
            }
            if( ( edgeTable[ cubeindex ] & 128 ) != 0x0 )
            {
                vertlist[ 7 ] = VertexInterp( isolevel, grid.p[ 7 ], grid.p[ 4 ], grid.val[ 7 ], grid.val[ 4 ] );
            }
            if( ( edgeTable[ cubeindex ] & 256 ) != 0x0 )
            {
                vertlist[ 8 ] = VertexInterp( isolevel, grid.p[ 0 ], grid.p[ 4 ], grid.val[ 0 ], grid.val[ 4 ] );
            }
            if( ( edgeTable[ cubeindex ] & 512 ) != 0x0 )
            {
                vertlist[ 9 ] = VertexInterp( isolevel, grid.p[ 1 ], grid.p[ 5 ], grid.val[ 1 ], grid.val[ 5 ] );
            }
            if( ( edgeTable[ cubeindex ] & 1024 ) != 0x0 )
            {
                vertlist[ 10 ] = VertexInterp( isolevel, grid.p[ 2 ], grid.p[ 6 ], grid.val[ 2 ], grid.val[ 6 ] );
            }
            if( ( edgeTable[ cubeindex ] & 2048 ) != 0x0 )
            {
                vertlist[ 11 ] = VertexInterp( isolevel, grid.p[ 3 ], grid.p[ 7 ], grid.val[ 3 ], grid.val[ 7 ] );
            }

            // Create the triangle
            ntriang = 0;
            for( i = 0; triTable[ cubeindex ][ i ] != -1; i += 3 )
            {
                triangles[ ntriang ].p[ 2 ] = vertlist[ triTable[ cubeindex ][ i ] ];
                triangles[ ntriang ].p[ 1 ] = vertlist[ triTable[ cubeindex ][ i + 1 ] ];
                triangles[ ntriang ].p[ 0 ] = vertlist[ triTable[ cubeindex ][ i + 2 ] ];
                ( ntriang )++;
            }
            return ntriang;
        }
Пример #2
0
        ///<summary>
        /// Given a grid cell and an isolevel, calculate the triangular facets required to represent the isosurface through the cell.
        /// Return the number of triangular facets, the array "triangles" will be loaded up with the vertices at most 5 triangular facets.
        /// 0 will be returned if the grid cell is either totally above of totally below the isolevel.
        ///</summary>
        public static int Polygonise(GRIDCELL grid, float isolevel, TRIANGLE[] triangles)
        {
            int i, ntriang;
            int cubeindex;

            Vector3[] vertlist = new Vector3[12];

            // Determine the index into the edge table which tells us which vertices are inside of the surface
            cubeindex = 0;
            if (grid.val[0] < isolevel)
            {
                cubeindex |= 1;
            }
            if (grid.val[1] < isolevel)
            {
                cubeindex |= 2;
            }
            if (grid.val[2] < isolevel)
            {
                cubeindex |= 4;
            }
            if (grid.val[3] < isolevel)
            {
                cubeindex |= 8;
            }
            if (grid.val[4] < isolevel)
            {
                cubeindex |= 16;
            }
            if (grid.val[5] < isolevel)
            {
                cubeindex |= 32;
            }
            if (grid.val[6] < isolevel)
            {
                cubeindex |= 64;
            }
            if (grid.val[7] < isolevel)
            {
                cubeindex |= 128;
            }

            // Cube is entirely in/out of the surface
            if (edgeTable[cubeindex] == 0)
            {
                return(0);
            }

            // Find the vertices where the surface intersects the cube
            if ((edgeTable[cubeindex] & 1) != 0x0)
            {
                vertlist[0] = VertexInterp(isolevel, grid.p[0], grid.p[1], grid.val[0], grid.val[1]);
            }
            if ((edgeTable[cubeindex] & 2) != 0x0)
            {
                vertlist[1] = VertexInterp(isolevel, grid.p[1], grid.p[2], grid.val[1], grid.val[2]);
            }
            if ((edgeTable[cubeindex] & 4) != 0x0)
            {
                vertlist[2] = VertexInterp(isolevel, grid.p[2], grid.p[3], grid.val[2], grid.val[3]);
            }
            if ((edgeTable[cubeindex] & 8) != 0x0)
            {
                vertlist[3] = VertexInterp(isolevel, grid.p[3], grid.p[0], grid.val[3], grid.val[0]);
            }
            if ((edgeTable[cubeindex] & 16) != 0x0)
            {
                vertlist[4] = VertexInterp(isolevel, grid.p[4], grid.p[5], grid.val[4], grid.val[5]);
            }
            if ((edgeTable[cubeindex] & 32) != 0x0)
            {
                vertlist[5] = VertexInterp(isolevel, grid.p[5], grid.p[6], grid.val[5], grid.val[6]);
            }
            if ((edgeTable[cubeindex] & 64) != 0x0)
            {
                vertlist[6] = VertexInterp(isolevel, grid.p[6], grid.p[7], grid.val[6], grid.val[7]);
            }
            if ((edgeTable[cubeindex] & 128) != 0x0)
            {
                vertlist[7] = VertexInterp(isolevel, grid.p[7], grid.p[4], grid.val[7], grid.val[4]);
            }
            if ((edgeTable[cubeindex] & 256) != 0x0)
            {
                vertlist[8] = VertexInterp(isolevel, grid.p[0], grid.p[4], grid.val[0], grid.val[4]);
            }
            if ((edgeTable[cubeindex] & 512) != 0x0)
            {
                vertlist[9] = VertexInterp(isolevel, grid.p[1], grid.p[5], grid.val[1], grid.val[5]);
            }
            if ((edgeTable[cubeindex] & 1024) != 0x0)
            {
                vertlist[10] = VertexInterp(isolevel, grid.p[2], grid.p[6], grid.val[2], grid.val[6]);
            }
            if ((edgeTable[cubeindex] & 2048) != 0x0)
            {
                vertlist[11] = VertexInterp(isolevel, grid.p[3], grid.p[7], grid.val[3], grid.val[7]);
            }

            // Create the triangle
            ntriang = 0;
            for (i = 0; triTable[cubeindex][i] != -1; i += 3)
            {
                triangles[ntriang].p[2] = vertlist[triTable[cubeindex][i]];
                triangles[ntriang].p[1] = vertlist[triTable[cubeindex][i + 1]];
                triangles[ntriang].p[0] = vertlist[triTable[cubeindex][i + 2]];
                (ntriang)++;
            }
            return(ntriang);
        }