MarchingCubeUtility.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #ifndef MARCHINGCUBEUTILITY_H
00028 #define MARCHINGCUBEUTILITY_H
00029 
00030 #include <sofa/defaulttype/Vec.h>
00031 #include <sofa/helper/vector.h>
00032 #include <sofa/helper/io/Mesh.h>
00033 #include <map>
00034 
00035 
00036 namespace sofa
00037 {
00038 
00039 namespace helper
00040 {
00041     using sofa::defaulttype::Vec;
00042     using sofa::defaulttype::Vector3;
00043     using sofa::helper::vector;
00044 
00045     class MarchingCubeUtility
00046     {    
00047     public:
00048         typedef unsigned int PointID;
00049         typedef Vec<3, int> Vec3i;
00050 
00051     public:
00052         MarchingCubeUtility();
00053 
00054         ~MarchingCubeUtility(){};
00055 
00056         void setDataResolution(const Vec3i   &resolution)     
00057         {
00058             dataResolution = resolution;
00059         }
00060 
00061         void setDataVoxelSize(const Vector3 &voxelSize)
00062         {
00063             dataVoxelSize = voxelSize;
00064         }
00065 
00066         void setStep(const unsigned int step)
00067         {
00068             cubeStep = step;
00069         }
00070 
00071         void setConvolutionSize(const unsigned int convolutionSize)
00072         {
00073             this->convolutionSize = convolutionSize;
00074         }
00075 
00080         void run( const unsigned char *data,  const float isolevel, 
00081                 sofa::helper::vector< PointID > &triangles,
00082                 sofa::helper::vector< Vector3>  &vertices) const;
00083 
00086         void run(  const unsigned char *data,  const float isolevel, sofa::helper::io::Mesh &m) const;
00087 
00088     private:
00089 
00090         struct GridCell
00091         {  
00092             float val[8];
00093             Vector3 pos[8];
00094         };
00095 
00096         inline void vertexInterp(const float isolevel, const Vector3 &p1, const Vector3 &p2, 
00097                                 const float valp1, const float valp2, Vector3 &p) const ;    
00098         
00099         inline bool testGrid(const float v, const float isolevel) const;
00100 
00101         int polygonise(const GridCell &grid, const float isolevel, 
00102                         sofa::helper::vector< PointID > &triangles,
00103                         std::map< Vector3, PointID> &map_vertices,
00104                         std::map< PointID, Vector3> &map_indices,
00105                         unsigned int &ID) const ;
00106 
00107         bool getVoxel(unsigned int index, const unsigned char *dataVoxels) const
00108         {
00109             const int i = index%8;
00110             return ((dataVoxels[index>>3]&((int)(pow(2.0f, i)))) >> i) == 1;
00111         }
00112 
00113         void createConvolutionKernel(vector< float >  &convolutionKernel) const;
00114 
00115         void applyConvolution(unsigned int x, unsigned int y, unsigned int z, 
00116                                 const float *original_data, float *data, 
00117                                 const vector< float >  &convolutionKernel) const;
00118 
00119         void smoothData( float *data) const;
00120 
00121     private:
00122         unsigned int    cubeStep;
00123         unsigned int    convolutionSize;
00124         Vec3i           dataResolution;
00125         Vector3         dataVoxelSize;
00126 
00127     };
00128 
00129     extern const int MarchingCubeEdgeTable[256];
00130     extern const int MarchingCubeTriTable[256][16];
00131 
00132 } // namespace helper
00133 
00134 } // namespace sofa
00135 
00136 #endif
00137 

Generated on Sun Nov 23 02:29:30 2008 for Sofa by  doxygen 1.5.7.1