MarchingCubeUtility.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 }
00133
00134 }
00135
00136 #endif
00137