marching_cubes.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "marching_cubes_tables.h"
  27. #include <unordered_map>
  28. extern const int edgeTable[256];
  29. extern const int triTable[256][2][17];
  30. extern const int polyTable[8][16];
  31. struct EdgeKey
  32. {
  33. EdgeKey(unsigned i0, unsigned i1) : i0_(i0), i1_(i1) {}
  34. bool operator==(const EdgeKey& _rhs) const
  35. {
  36. return i0_ == _rhs.i0_ && i1_ == _rhs.i1_;
  37. }
  38. unsigned i0_, i1_;
  39. };
  40. struct EdgeHash
  41. {
  42. std::size_t operator()(const EdgeKey& key) const {
  43. std::size_t seed = 0;
  44. seed ^= key.i0_ + 0x9e3779b9 + (seed<<6) + (seed>>2); // Copied from boost::hash_combine
  45. seed ^= key.i1_ + 0x9e3779b9 + (seed<<6) + (seed>>2);
  46. return std::hash<std::size_t>()(seed);
  47. }
  48. };
  49. template <typename Derivedvalues, typename Derivedpoints,typename Derivedvertices, typename DerivedF>
  50. class MarchingCubes
  51. {
  52. typedef std::unordered_map<EdgeKey, unsigned, EdgeHash> MyMap;
  53. typedef typename MyMap::const_iterator MyMapIterator;
  54. public:
  55. MarchingCubes(
  56. const Eigen::PlainObjectBase<Derivedvalues> &values,
  57. const Eigen::PlainObjectBase<Derivedpoints> &points,
  58. const unsigned x_res,
  59. const unsigned y_res,
  60. const unsigned z_res,
  61. Eigen::PlainObjectBase<Derivedvertices> &vertices,
  62. Eigen::PlainObjectBase<DerivedF> &faces)
  63. {
  64. assert(values.cols() == 1);
  65. assert(points.cols() == 3);
  66. if(x_res <2 || y_res<2 ||z_res<2)
  67. return;
  68. faces.resize(10000,3);
  69. int num_faces = 0;
  70. vertices.resize(10000,3);
  71. int num_vertices = 0;
  72. unsigned n_cubes = (x_res-1) * (y_res-1) * (z_res-1);
  73. assert(unsigned(points.rows()) == x_res * y_res * z_res);
  74. unsigned int offsets_[8];
  75. offsets_[0] = 0;
  76. offsets_[1] = 1;
  77. offsets_[2] = 1 + x_res;
  78. offsets_[3] = x_res;
  79. offsets_[4] = x_res*y_res;
  80. offsets_[5] = 1 + x_res*y_res;
  81. offsets_[6] = 1 + x_res + x_res*y_res;
  82. offsets_[7] = x_res + x_res*y_res;
  83. for (unsigned cube_it =0 ; cube_it < n_cubes; ++cube_it)
  84. {
  85. unsigned corner[8];
  86. typename DerivedF::Scalar samples[12];
  87. unsigned char cubetype(0);
  88. unsigned int i;
  89. // get point indices of corner vertices
  90. for (i=0; i<8; ++i)
  91. {
  92. // get cube coordinates
  93. unsigned int _idx = cube_it;
  94. unsigned int X(x_res-1), Y(y_res-1);
  95. unsigned int x = _idx % X; _idx /= X;
  96. unsigned int y = _idx % Y; _idx /= Y;
  97. unsigned int z = _idx;
  98. // transform to point coordinates
  99. _idx = x + y*x_res + z*x_res*y_res;
  100. // add offset
  101. corner[i] = _idx + offsets_[i];
  102. }
  103. // determine cube type
  104. for (i=0; i<8; ++i)
  105. if (values[corner[i]] > 0.0)
  106. cubetype |= (1<<i);
  107. // trivial reject ?
  108. if (cubetype == 0 || cubetype == 255)
  109. continue;
  110. // compute samples on cube's edges
  111. if (edgeTable[cubetype]&1)
  112. samples[0] = add_vertex(values, points, corner[0], corner[1], vertices, num_vertices, edge2vertex);
  113. if (edgeTable[cubetype]&2)
  114. samples[1] = add_vertex(values, points, corner[1], corner[2], vertices, num_vertices, edge2vertex);
  115. if (edgeTable[cubetype]&4)
  116. samples[2] = add_vertex(values, points, corner[3], corner[2], vertices, num_vertices, edge2vertex);
  117. if (edgeTable[cubetype]&8)
  118. samples[3] = add_vertex(values, points, corner[0], corner[3], vertices, num_vertices, edge2vertex);
  119. if (edgeTable[cubetype]&16)
  120. samples[4] = add_vertex(values, points, corner[4], corner[5], vertices, num_vertices, edge2vertex);
  121. if (edgeTable[cubetype]&32)
  122. samples[5] = add_vertex(values, points, corner[5], corner[6], vertices, num_vertices, edge2vertex);
  123. if (edgeTable[cubetype]&64)
  124. samples[6] = add_vertex(values, points, corner[7], corner[6], vertices, num_vertices, edge2vertex);
  125. if (edgeTable[cubetype]&128)
  126. samples[7] = add_vertex(values, points, corner[4], corner[7], vertices, num_vertices, edge2vertex);
  127. if (edgeTable[cubetype]&256)
  128. samples[8] = add_vertex(values, points, corner[0], corner[4], vertices, num_vertices, edge2vertex);
  129. if (edgeTable[cubetype]&512)
  130. samples[9] = add_vertex(values, points, corner[1], corner[5], vertices, num_vertices, edge2vertex);
  131. if (edgeTable[cubetype]&1024)
  132. samples[10] = add_vertex(values, points, corner[2], corner[6], vertices, num_vertices, edge2vertex);
  133. if (edgeTable[cubetype]&2048)
  134. samples[11] = add_vertex(values, points, corner[3], corner[7], vertices, num_vertices, edge2vertex);
  135. // connect samples by triangles
  136. for (i=0; triTable[cubetype][0][i] != -1; i+=3 )
  137. {
  138. num_faces++;
  139. if (num_faces > faces.rows())
  140. faces.conservativeResize(faces.rows()+10000, Eigen::NoChange);
  141. faces.row(num_faces-1) <<
  142. samples[triTable[cubetype][0][i ]],
  143. samples[triTable[cubetype][0][i+1]],
  144. samples[triTable[cubetype][0][i+2]];
  145. }
  146. }
  147. vertices.conservativeResize(num_vertices, Eigen::NoChange);
  148. faces.conservativeResize(num_faces, Eigen::NoChange);
  149. };
  150. static typename DerivedF::Scalar add_vertex(const Eigen::PlainObjectBase<Derivedvalues> &values,
  151. const Eigen::PlainObjectBase<Derivedpoints> &points,
  152. unsigned int i0,
  153. unsigned int i1,
  154. Eigen::PlainObjectBase<Derivedvertices> &vertices,
  155. int &num_vertices,
  156. MyMap &edge2vertex)
  157. {
  158. // find vertex if it has been computed already
  159. MyMapIterator it = edge2vertex.find(EdgeKey(i0, i1));
  160. if (it != edge2vertex.end())
  161. return it->second;
  162. ;
  163. // generate new vertex
  164. const Eigen::Matrix<typename Derivedpoints::Scalar, 1, 3> & p0 = points.row(i0);
  165. const Eigen::Matrix<typename Derivedpoints::Scalar, 1, 3> & p1 = points.row(i1);
  166. typename Derivedvalues::Scalar s0 = fabs(values[i0]);
  167. typename Derivedvalues::Scalar s1 = fabs(values[i1]);
  168. typename Derivedvalues::Scalar t = s0 / (s0+s1);
  169. num_vertices++;
  170. if (num_vertices > vertices.rows())
  171. vertices.conservativeResize(vertices.rows()+10000, Eigen::NoChange);
  172. vertices.row(num_vertices-1) = ((1.0f-t)*p0 + t*p1).template cast<typename Derivedvertices::Scalar>();
  173. edge2vertex[EdgeKey(i0, i1)] = num_vertices-1;
  174. return num_vertices-1;
  175. }
  176. ;
  177. // maps an edge to the sample vertex generated on it
  178. MyMap edge2vertex;
  179. };
  180. template <typename Derivedvalues, typename Derivedpoints, typename Derivedvertices, typename DerivedF>
  181. IGL_INLINE void igl::copyleft::marching_cubes(
  182. const Eigen::PlainObjectBase<Derivedvalues> &values,
  183. const Eigen::PlainObjectBase<Derivedpoints> &points,
  184. const unsigned x_res,
  185. const unsigned y_res,
  186. const unsigned z_res,
  187. Eigen::PlainObjectBase<Derivedvertices> &vertices,
  188. Eigen::PlainObjectBase<DerivedF> &faces)
  189. {
  190. MarchingCubes<Derivedvalues, Derivedpoints, Derivedvertices, DerivedF> mc(values,
  191. points,
  192. x_res,
  193. y_res,
  194. z_res,
  195. vertices,
  196. faces);
  197. }
  198. #ifdef IGL_STATIC_LIBRARY
  199. // Explicit template instantiation
  200. // generated by autoexplicit.sh
  201. template void igl::copyleft::marching_cubes<Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -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> >&);
  202. // generated by autoexplicit.sh
  203. template void igl::copyleft::marching_cubes<Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&);
  204. // generated by autoexplicit.sh
  205. template void igl::copyleft::marching_cubes<Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&);
  206. // generated by autoexplicit.sh
  207. template void igl::copyleft::marching_cubes<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&);
  208. template void igl::copyleft::marching_cubes< Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, unsigned int, unsigned int, unsigned int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  209. #endif