marching_cubes.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. template <
  52. typename DerivedValue,
  53. typename DerivedPoint,
  54. typename DerivedPoints,
  55. typename DerivedVertices,
  56. typename DerivedFaces>
  57. IGL_INLINE void marching_cubes(
  58. const std::function< DerivedValue(const DerivedPoint & ) > & value_fun,
  59. const Eigen::MatrixBase<DerivedPoints> &points,
  60. const unsigned x_res,
  61. const unsigned y_res,
  62. const unsigned z_res,
  63. const double isovalue,
  64. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  65. Eigen::PlainObjectBase<DerivedFaces> &faces);
  66. // Overload of the above function where the isovalue defaults to 0.0
  67. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedFaces>
  68. IGL_INLINE void marching_cubes(
  69. const Eigen::MatrixBase<DerivedValues> &values,
  70. const Eigen::MatrixBase<DerivedPoints> &points,
  71. const unsigned x_res,
  72. const unsigned y_res,
  73. const unsigned z_res,
  74. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  75. Eigen::PlainObjectBase<DerivedFaces> &faces);
  76. // marching_cubes( values, points, indices, vertices, faces )
  77. //
  78. // Perform marching cubes reconstruction on the sparse grid cells defined by (indices, points).
  79. // The indices parameter is an nx8 dense array of index values into the points and values arrays.
  80. // Each row of indices represents a cube for which to generate vertices and faces over.
  81. //
  82. // Input:
  83. // values #number_of_grid_points x 1 array -- the scalar values of an
  84. // implicit function defined on the grid points (<0 in the inside of the
  85. // surface, 0 on the border, >0 outside)
  86. // points #number_of_grid_points x 3 array -- 3-D positions of the grid
  87. // points, ordered in x,y,z order:
  88. // indices #cubes x 8 array -- one row for each cube where each value is
  89. // the index of a vertex in points and a scalar in values.
  90. // i.e. points[indices[i, j]] = the position of the j'th vertex of the i'th cube
  91. // Output:
  92. // vertices #V by 3 list of mesh vertex positions
  93. // faces #F by 3 list of mesh triangle indices
  94. // Note: The winding direction of the cube indices will affect the output winding of the faces
  95. //
  96. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
  97. IGL_INLINE void marching_cubes(
  98. const Eigen::MatrixBase<DerivedValues> &values,
  99. const Eigen::MatrixBase<DerivedPoints> &points,
  100. const Eigen::MatrixBase<DerivedIndices> &indices,
  101. const double isovalue,
  102. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  103. Eigen::PlainObjectBase<DerivedFaces> &faces);
  104. // Overload of the above function where the isovalue defaults to 0.0
  105. template <typename DerivedValues, typename DerivedPoints, typename DerivedVertices, typename DerivedIndices, typename DerivedFaces>
  106. IGL_INLINE void marching_cubes(
  107. const Eigen::MatrixBase<DerivedValues> &values,
  108. const Eigen::MatrixBase<DerivedPoints> &points,
  109. const Eigen::MatrixBase<DerivedIndices> &indices,
  110. Eigen::PlainObjectBase<DerivedVertices> &vertices,
  111. Eigen::PlainObjectBase<DerivedFaces> &faces);
  112. }
  113. }
  114. #ifndef IGL_STATIC_LIBRARY
  115. # include "marching_cubes.cpp"
  116. #endif
  117. #endif