marching_cubes.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <[email protected]>
  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. /// Performs marching cubes reconstruction on a grid defined by values, and
  17. /// points, and generates a mesh defined by vertices and faces
  18. ///
  19. /// @param[in] values #number_of_grid_points x 1 array -- the scalar values of an
  20. /// implicit function defined on the grid points (<0 in the inside of the
  21. /// surface, 0 on the border, >0 outside)
  22. /// @param[in] points #number_of_grid_points x 3 array -- 3-D positions of the grid
  23. /// points, ordered in x,y,z order:
  24. /// points[index] = the point at (x,y,z) where :
  25. /// x = (index % (xres -1),
  26. /// y = (index / (xres-1)) %(yres-1),
  27. /// z = index / (xres -1) / (yres -1) ).
  28. /// where x,y,z index x, y, z dimensions
  29. /// i.e. index = x + y*xres + z*xres*yres
  30. /// @param[in] xres resolutions of the grid in x dimension
  31. /// @param[in] yres resolutions of the grid in y dimension
  32. /// @param[in] zres resolutions of the grid in z dimension
  33. /// @param[in] isovalue the isovalue of the surface to reconstruct
  34. /// @param[out] vertices #V by 3 list of mesh vertex positions
  35. /// @param[out] faces #F by 3 list of mesh triangle indices
  36. ///
  37. /// \see igl::marching_cubes
  38. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
  39. IGL_INLINE void marching_cubes(
  40. const Eigen::MatrixBase<DerivedValues> &values,
  41. const Eigen::MatrixBase<DerivedPoints> &points,
  42. const unsigned x_res,
  43. const unsigned y_res,
  44. const unsigned z_res,
  45. const double isovalue,
  46. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  47. Eigen::PlainObjectBase<DerivedFaces> &faces);
  48. /// \overload
  49. /// Overload of the above function where the isovalue defaults to 0.0
  50. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
  51. IGL_INLINE void marching_cubes(
  52. const Eigen::MatrixBase<DerivedValues> &values,
  53. const Eigen::MatrixBase<DerivedPoints> &points,
  54. const unsigned x_res,
  55. const unsigned y_res,
  56. const unsigned z_res,
  57. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  58. Eigen::PlainObjectBase<DerivedFaces> &faces);
  59. /// \overload
  60. /// @param[in] value_fun a function that takes a 3D point and returns a scalar value
  61. template <
  62. typename DerivedValue,
  63. typename DerivedPoint,
  64. typename DerivedPoints,
  65. typename DerivedVertices,
  66. typename DerivedFaces>
  67. IGL_INLINE void marching_cubes(
  68. const std::function< DerivedValue(const DerivedPoint & ) > & value_fun,
  69. const Eigen::MatrixBase<DerivedPoints> &points,
  70. const unsigned x_res,
  71. const unsigned y_res,
  72. const unsigned z_res,
  73. const double isovalue,
  74. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  75. Eigen::PlainObjectBase<DerivedFaces> &faces);
  76. /// Perform marching cubes reconstruction on the sparse grid cells defined by (indices, points).
  77. /// The indices parameter is an nx8 dense array of index values into the points and values arrays.
  78. /// Each row of indices represents a cube for which to generate vertices and faces over.
  79. ///
  80. /// @param[in] values #number_of_grid_points x 1 array -- the scalar values of an
  81. /// implicit function defined on the grid points (<0 in the inside of the
  82. /// surface, 0 on the border, >0 outside)
  83. /// @param[in] points #number_of_grid_points x 3 array -- 3-D positions of the grid
  84. /// points, ordered in x,y,z order:
  85. /// @param[in] indices #cubes x 8 array -- one row for each cube where each value is
  86. /// the index of a vertex in points and a scalar in values.
  87. /// i.e. points[indices[i, j]] = the position of the j'th vertex of the i'th cube
  88. /// @param[out] vertices #V by 3 list of mesh vertex positions
  89. /// @param[out] faces #F by 3 list of mesh triangle indices
  90. ///
  91. /// \note The winding direction of the cube indices will affect the output winding of the faces
  92. ///
  93. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
  94. IGL_INLINE void marching_cubes(
  95. const Eigen::MatrixBase<DerivedValues> &values,
  96. const Eigen::MatrixBase<DerivedPoints> &points,
  97. const Eigen::MatrixBase<DerivedIndices> &indices,
  98. const double isovalue,
  99. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  100. Eigen::PlainObjectBase<DerivedFaces> &faces);
  101. /// \overload
  102. /// \brief isovalue defaults to 0.0
  103. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
  104. IGL_INLINE void marching_cubes(
  105. const Eigen::MatrixBase<DerivedValues> &values,
  106. const Eigen::MatrixBase<DerivedPoints> &points,
  107. const Eigen::MatrixBase<DerivedIndices> &indices,
  108. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  109. Eigen::PlainObjectBase<DerivedFaces> &faces);
  110. }
  111. }
  112. #ifndef IGL_STATIC_LIBRARY
  113. # include "marching_cubes.cpp"
  114. #endif
  115. #endif