marching_cubes.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_COPYLEFT_MARCHINGCUBES_H
  9. #define IGL_COPYLEFT_MARCHINGCUBES_H
  10. #include "../igl_inline.h"
  11. #include <Eigen/Core>
  12. namespace igl
  13. {
  14. namespace copyleft
  15. {
  16. // marching_cubes( values, points, x_res, y_res, z_res, isovalue, vertices, faces )
  17. //
  18. // performs marching cubes reconstruction on a grid defined by values, and
  19. // points, and generates a mesh defined by vertices and faces
  20. //
  21. // Input:
  22. // values #number_of_grid_points x 1 array -- the scalar values of an
  23. // implicit function defined on the grid points (<0 in the inside of the
  24. // surface, 0 on the border, >0 outside)
  25. // points #number_of_grid_points x 3 array -- 3-D positions of the grid
  26. // points, ordered in x,y,z order:
  27. // points[index] = the point at (x,y,z) where :
  28. // x = (index % (xres -1),
  29. // y = (index / (xres-1)) %(yres-1),
  30. // z = index / (xres -1) / (yres -1) ).
  31. // where x,y,z index x, y, z dimensions
  32. // i.e. index = x + y*xres + z*xres*yres
  33. // xres resolutions of the grid in x dimension
  34. // yres resolutions of the grid in y dimension
  35. // zres resolutions of the grid in z dimension
  36. // isovalue the isovalue of the surface to reconstruct
  37. // Output:
  38. // vertices #V by 3 list of mesh vertex positions
  39. // faces #F by 3 list of mesh triangle indices
  40. //
  41. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
  42. IGL_INLINE void marching_cubes(
  43. const Eigen::MatrixBase<DerivedValues> &values,
  44. const Eigen::MatrixBase<DerivedPoints> &points,
  45. const unsigned x_res,
  46. const unsigned y_res,
  47. const unsigned z_res,
  48. const double isovalue,
  49. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  50. Eigen::PlainObjectBase<DerivedFaces> &faces);
  51. // Overload of the above function where the isovalue defaults to 0.0
  52. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
  53. IGL_INLINE void marching_cubes(
  54. const Eigen::MatrixBase<DerivedValues> &values,
  55. const Eigen::MatrixBase<DerivedPoints> &points,
  56. const unsigned x_res,
  57. const unsigned y_res,
  58. const unsigned z_res,
  59. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  60. Eigen::PlainObjectBase<DerivedFaces> &faces);
  61. // marching_cubes( values, points, indices, vertices, faces )
  62. //
  63. // Perform marching cubes reconstruction on the sparse grid cells defined by (indices, points).
  64. // The indices parameter is an nx8 dense array of index values into the points and values arrays.
  65. // Each row of indices represents a cube for which to generate vertices and faces over.
  66. //
  67. // Input:
  68. // values #number_of_grid_points x 1 array -- the scalar values of an
  69. // implicit function defined on the grid points (<0 in the inside of the
  70. // surface, 0 on the border, >0 outside)
  71. // points #number_of_grid_points x 3 array -- 3-D positions of the grid
  72. // points, ordered in x,y,z order:
  73. // indices #cubes x 8 array -- one row for each cube where each value is
  74. // the index of a vertex in points and a scalar in values.
  75. // i.e. points[indices[i, j]] = the position of the j'th vertex of the i'th cube
  76. // Output:
  77. // vertices #V by 3 list of mesh vertex positions
  78. // faces #F by 3 list of mesh triangle indices
  79. // Note: The winding direction of the cube indices will affect the output winding of the faces
  80. //
  81. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
  82. IGL_INLINE void marching_cubes(
  83. const Eigen::MatrixBase<DerivedValues> &values,
  84. const Eigen::MatrixBase<DerivedPoints> &points,
  85. const Eigen::MatrixBase<DerivedIndices> &indices,
  86. const double isovalue,
  87. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  88. Eigen::PlainObjectBase<DerivedFaces> &faces);
  89. // Overload of the above function where the isovalue defaults to 0.0
  90. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
  91. IGL_INLINE void marching_cubes(
  92. const Eigen::MatrixBase<DerivedValues> &values,
  93. const Eigen::MatrixBase<DerivedPoints> &points,
  94. const Eigen::MatrixBase<DerivedIndices> &indices,
  95. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  96. Eigen::PlainObjectBase<DerivedFaces> &faces);
  97. }
  98. }
  99. #ifndef IGL_STATIC_LIBRARY
  100. # include "marching_cubes.cpp"
  101. #endif
  102. #endif