marching_cubes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*===========================================================================*\
  2. * *
  3. * IsoEx *
  4. * Copyright (C) 2002 by Computer Graphics Group, RWTH Aachen *
  5. * www.rwth-graphics.de *
  6. * *
  7. *---------------------------------------------------------------------------*
  8. * *
  9. * License *
  10. * *
  11. * This library is free software; you can redistribute it and/or modify it *
  12. * under the terms of the GNU Library General Public License as published *
  13. * by the Free Software Foundation, version 2. *
  14. * *
  15. * This library is distributed in the hope that it will be useful, but *
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  18. * Library General Public License for more details. *
  19. * *
  20. * You should have received a copy of the GNU Library General Public *
  21. * License along with this library; if not, write to the Free Software *
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  23. * *
  24. \*===========================================================================*/
  25. #include "marching_cubes.h"
  26. #include <map>
  27. extern int edgeTable[256];
  28. extern int triTable[256][2][17];
  29. extern int polyTable[8][16];
  30. class EdgeKey
  31. {
  32. public:
  33. EdgeKey(unsigned i0, unsigned i1) {
  34. if (i0 < i1) { i0_ = i0; i1_ = i1; }
  35. else { i0_ = i1; i1_ = i0; }
  36. }
  37. bool operator<(const EdgeKey& _rhs) const
  38. {
  39. if (i0_ != _rhs.i0_)
  40. return (i0_ < _rhs.i0_);
  41. else
  42. return (i1_ < _rhs.i1_);
  43. }
  44. private:
  45. unsigned i0_, i1_;
  46. };
  47. template <typename DerivedV, typename DerivedF>
  48. class MarchingCubes
  49. {
  50. typedef Eigen::PlainObjectBase<DerivedV> PointMatrixType;
  51. typedef Eigen::PlainObjectBase<DerivedF> FaceMatrixType;
  52. typedef std::map<EdgeKey, unsigned> MyMap;
  53. typedef typename MyMap::const_iterator MyMapIterator;
  54. public:
  55. MarchingCubes(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
  56. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
  57. const unsigned x_res,
  58. const unsigned y_res,
  59. const unsigned z_res,
  60. PointMatrixType &vertices,
  61. FaceMatrixType &faces)
  62. {
  63. if(x_res <2 || y_res<2 ||z_res<2)
  64. return;
  65. faces.resize(10000,3);
  66. int num_faces = 0;
  67. vertices.resize(10000,3);
  68. int num_vertices = 0;
  69. unsigned n_cubes = (x_res-1) * (y_res-1) * (z_res-1);
  70. assert(unsigned(points.rows()) == x_res * y_res * z_res);
  71. unsigned int offsets_[8];
  72. offsets_[0] = 0;
  73. offsets_[1] = 1;
  74. offsets_[2] = 1 + x_res;
  75. offsets_[3] = x_res;
  76. offsets_[4] = x_res*y_res;
  77. offsets_[5] = 1 + x_res*y_res;
  78. offsets_[6] = 1 + x_res + x_res*y_res;
  79. offsets_[7] = x_res + x_res*y_res;
  80. for (unsigned cube_it =0 ; cube_it < n_cubes; ++cube_it)
  81. {
  82. unsigned corner[8];
  83. typename DerivedF::Scalar samples[12];
  84. unsigned char cubetype(0);
  85. unsigned int i;
  86. // get point indices of corner vertices
  87. for (i=0; i<8; ++i)
  88. {
  89. // get cube coordinates
  90. unsigned int _idx = cube_it;
  91. unsigned int X(x_res-1), Y(y_res-1);
  92. unsigned int x = _idx % X; _idx /= X;
  93. unsigned int y = _idx % Y; _idx /= Y;
  94. unsigned int z = _idx;
  95. // transform to point coordinates
  96. _idx = x + y*x_res + z*x_res*y_res;
  97. // add offset
  98. corner[i] = _idx + offsets_[i];
  99. }
  100. // determine cube type
  101. for (i=0; i<8; ++i)
  102. if (values[corner[i]] > 0.0)
  103. cubetype |= (1<<i);
  104. // trivial reject ?
  105. if (cubetype == 0 || cubetype == 255)
  106. continue;
  107. // compute samples on cube's edges
  108. if (edgeTable[cubetype]&1)
  109. samples[0] = add_vertex(values, points, corner[0], corner[1], vertices, num_vertices, edge2vertex);
  110. if (edgeTable[cubetype]&2)
  111. samples[1] = add_vertex(values, points, corner[1], corner[2], vertices, num_vertices, edge2vertex);
  112. if (edgeTable[cubetype]&4)
  113. samples[2] = add_vertex(values, points, corner[3], corner[2], vertices, num_vertices, edge2vertex);
  114. if (edgeTable[cubetype]&8)
  115. samples[3] = add_vertex(values, points, corner[0], corner[3], vertices, num_vertices, edge2vertex);
  116. if (edgeTable[cubetype]&16)
  117. samples[4] = add_vertex(values, points, corner[4], corner[5], vertices, num_vertices, edge2vertex);
  118. if (edgeTable[cubetype]&32)
  119. samples[5] = add_vertex(values, points, corner[5], corner[6], vertices, num_vertices, edge2vertex);
  120. if (edgeTable[cubetype]&64)
  121. samples[6] = add_vertex(values, points, corner[7], corner[6], vertices, num_vertices, edge2vertex);
  122. if (edgeTable[cubetype]&128)
  123. samples[7] = add_vertex(values, points, corner[4], corner[7], vertices, num_vertices, edge2vertex);
  124. if (edgeTable[cubetype]&256)
  125. samples[8] = add_vertex(values, points, corner[0], corner[4], vertices, num_vertices, edge2vertex);
  126. if (edgeTable[cubetype]&512)
  127. samples[9] = add_vertex(values, points, corner[1], corner[5], vertices, num_vertices, edge2vertex);
  128. if (edgeTable[cubetype]&1024)
  129. samples[10] = add_vertex(values, points, corner[2], corner[6], vertices, num_vertices, edge2vertex);
  130. if (edgeTable[cubetype]&2048)
  131. samples[11] = add_vertex(values, points, corner[3], corner[7], vertices, num_vertices, edge2vertex);
  132. // connect samples by triangles
  133. for (i=0; triTable[cubetype][0][i] != -1; i+=3 )
  134. {
  135. num_faces++;
  136. if (num_faces > faces.rows())
  137. faces.conservativeResize(faces.rows()+10000, Eigen::NoChange);
  138. faces.row(num_faces-1) <<
  139. samples[triTable[cubetype][0][i ]],
  140. samples[triTable[cubetype][0][i+1]],
  141. samples[triTable[cubetype][0][i+2]];
  142. }
  143. }
  144. vertices.conservativeResize(num_vertices, Eigen::NoChange);
  145. faces.conservativeResize(num_faces, Eigen::NoChange);
  146. };
  147. static typename DerivedF::Scalar add_vertex(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
  148. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
  149. unsigned int i0,
  150. unsigned int i1,
  151. PointMatrixType &vertices,
  152. int &num_vertices,
  153. MyMap &edge2vertex)
  154. {
  155. // find vertex if it has been computed already
  156. MyMapIterator it = edge2vertex.find(EdgeKey(i0, i1));
  157. if (it != edge2vertex.end())
  158. return it->second;
  159. ;
  160. // generate new vertex
  161. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> & p0 = points.row(i0);
  162. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> & p1 = points.row(i1);
  163. typename DerivedV::Scalar s0 = fabs(values[i0]);
  164. typename DerivedV::Scalar s1 = fabs(values[i1]);
  165. typename DerivedV::Scalar t = s0 / (s0+s1);
  166. num_vertices++;
  167. if (num_vertices > vertices.rows())
  168. vertices.conservativeResize(vertices.rows()+10000, Eigen::NoChange);
  169. vertices.row(num_vertices-1) = (1.0f-t)*p0 + t*p1;
  170. edge2vertex[EdgeKey(i0, i1)] = num_vertices-1;
  171. return num_vertices-1;
  172. }
  173. ;
  174. // maps an edge to the sample vertex generated on it
  175. MyMap edge2vertex;
  176. };
  177. template <typename DerivedV, typename DerivedF>
  178. IGL_INLINE void igl::marching_cubes(const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 1> &values,
  179. const Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3> &points,
  180. const unsigned x_res,
  181. const unsigned y_res,
  182. const unsigned z_res,
  183. Eigen::PlainObjectBase<DerivedV> &vertices,
  184. Eigen::PlainObjectBase<DerivedF> &faces)
  185. {
  186. MarchingCubes<DerivedV, DerivedF> mc(values,
  187. points,
  188. x_res,
  189. y_res,
  190. z_res,
  191. vertices,
  192. faces);
  193. }
  194. #ifndef IGL_HEADER_ONLY
  195. // Explicit template specialization
  196. template void igl::marching_cubes<Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  197. template void igl::marching_cubes<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  198. template void igl::marching_cubes<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<double, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >&);
  199. template void igl::marching_cubes<Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >(Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 1, 0, -1, 1> const&, Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, -1, 3, 0, -1, 3> const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 0, -1, 3> >&);
  200. #endif