shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/marching_cube.hpp
Go to the documentation of this file.
00001 # ifndef shape_marching_cube_hpp
00002 # define shape_marching_cube_hpp
00003 # define ULONG unsigned long 
00004 # define TMPL template<class K>
00005 
00006 namespace mmx { namespace shape {
00007 
00008 extern int marching_cube_edge_table[256];
00009 extern int marching_cube_tri_table[256][16] ;
00010 
00011 struct marching_cube {
00012 
00013 public:
00014 
00015   marching_cube(void) {};
00016  ~marching_cube(void){};
00017 
00018   template<class Mesh, class Cell>
00019   static bool polygonise(Mesh& res, const Cell& grid);
00020   template<class Mesh, class Cell>
00021   static bool centralise(Mesh& res, const Cell& grid);
00022 
00023 };
00024 
00025 //************************************************************************
00026 // Polygonise the cell in which isosurface pass through
00027 //************************************************************************
00028 template<class Mesh, class Cell> bool 
00029 marching_cube::polygonise(Mesh& res, const Cell& cell) {
00030 
00031   typedef typename Mesh::Point       Point;
00032   typedef typename Mesh::Face        Face;
00033   
00034   int CubeIndex=cell.mc_index();
00035 
00036   if (marching_cube_edge_table[CubeIndex] == 0) return false;
00037 
00038   Point* CELL[12];
00039   if(cell.edge_point(CELL, marching_cube_edge_table[CubeIndex])) {
00040     for (int i=0;marching_cube_tri_table[CubeIndex][i]!=-1;i+=3) {
00041       Face* F= new Face;
00042       for(int j=0;j<3;j++) {
00043         Point* p=CELL[marching_cube_tri_table[CubeIndex][i+j]];
00044         res.insert(p);
00045         F->insert(p);
00046       }
00047       res.insert(F);
00048     }
00049     return true;
00050   } else
00051     return false;
00052 }
00053 
00054 //--------------------------------------------------------------------
00055 // Compute a "center" for the cell
00056 //--------------------------------------------------------------------
00057 template<class Mesh, class Cell> bool 
00058 marching_cube::centralise(Mesh& res, const Cell& cell) {
00059 
00060   typedef typename Mesh::Point       Point;
00061   typedef typename Mesh::Face        Face;
00062 
00063   int CubeIndex=cell.mc_index();
00064   //  std::cout<<CubeIndex<<std::endl;
00065   if (marching_cube_edge_table[CubeIndex] == 0) return false;
00066 
00067   Point* CELL[12];
00068 
00069   if(cell.edge_point(CELL, marching_cube_edge_table[CubeIndex])) {
00070     Point* c=new Point(0,0,0); 
00071     unsigned ntr=0;
00072     for (int i=0;marching_cube_tri_table[CubeIndex][i]!=-1;i+=3)
00073       ntr++;
00074     ntr/=2;
00075 
00076     for(int j=0;j<3;j++) {
00077       Point* p=CELL[marching_cube_tri_table[CubeIndex][3*ntr+j]];
00078       c->x()+= p->x();
00079       c->y()+= p->y();
00080       c->z()+= p->z();
00081     }
00082     c->x()/=3.;
00083     c->y()/=3.;
00084     c->z()/=3.;
00085     //   c->x()=(cell.xmin()+cell.xmax())/2;
00086     //   c->y()=(cell.ymin()+cell.ymax())/2;
00087     //   c->z()=(cell.zmin()+cell.zmax())/2;
00088     //std::cout<<*c<<std::endl;
00089     res.insert(c);
00090     return true;
00091   } else
00092     return false;
00093 }
00094 
00095 //====================================================================
00096   } //namespace shape
00097 }//namespace mmx
00098 # undef TMPL
00099 # undef ULONG
00100 # endif //shape_marching_cube_hpp