dual_contouring.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef IGL_DUAL_CONTOURING_H
  2. #define IGL_DUAL_CONTOURING_H
  3. #include "igl_inline.h"
  4. #include <Eigen/Core>
  5. #include <Eigen/Dense>
  6. #include <functional>
  7. namespace igl
  8. {
  9. /// Dual contouring to extract a pure quad mesh from differentiable implicit
  10. /// function using a dense grid.
  11. ///
  12. /// @param[in] f function returning >0 outside, <0 inside and =0 on the surface
  13. /// @param[in] f_grad function returning ∇f/‖∇f‖
  14. /// @param[in] min_corner position of primal grid vertex at minimum corner
  15. /// @param[in] max_corner position of primal grid vertex at maximum corner
  16. /// @param[in] nx number of vertices on x side of primal grid
  17. /// @param[in] ny number of vertices on y side of primal grid
  18. /// @param[in] nz number of vertices on z side of primal grid
  19. /// @param[in] constrained whether to force dual vertices to lie strictly inside
  20. /// corresponding primal cell (prevents self-intersections at cost of
  21. /// surface quality; marginally slower)
  22. /// @param[in] triangles whether to output four triangles instead of one quad per
  23. /// crossing edge (quad mesh usually looks fine)
  24. /// @param[in] root_finding whether to use root finding to identify crossing point on
  25. /// each edge (improves quality a lot at cost of performance). If false,
  26. /// use linear interpolation.
  27. /// @param[out] V #V by 3 list of outputs vertex positions
  28. /// @param[out] Q #Q by 4 (or 3 if triangles=true) face indices into rows of V
  29. template <
  30. typename DerivedV,
  31. typename DerivedQ>
  32. IGL_INLINE void dual_contouring(
  33. const std::function<
  34. typename DerivedV::Scalar(const Eigen::Matrix<typename DerivedV::Scalar,1,3> &)> & f,
  35. const std::function<
  36. Eigen::Matrix<typename DerivedV::Scalar,1,3>(
  37. const Eigen::Matrix<typename DerivedV::Scalar,1,3> &)> & f_grad,
  38. const Eigen::Matrix<typename DerivedV::Scalar,1,3> & min_corner,
  39. const Eigen::Matrix<typename DerivedV::Scalar,1,3> & max_corner,
  40. const int nx,
  41. const int ny,
  42. const int nz,
  43. const bool constrained,
  44. const bool triangles,
  45. const bool root_finding,
  46. Eigen::PlainObjectBase<DerivedV> & V,
  47. Eigen::PlainObjectBase<DerivedQ> & Q);
  48. /// \overload
  49. ///
  50. /// @param[in] Gf nx*ny*nz list of function values so that Gf(k) = f(GV.row(k)) (only
  51. /// needs to be accurate near f=0 and correct sign elsewhere)
  52. /// @param[in] GV nx*ny*nz list of grid positions so that the x,y,z grid position is at
  53. /// GV.row(x+nx*(y+z*ny))
  54. template <
  55. typename DerivedGf,
  56. typename DerivedGV,
  57. typename DerivedV,
  58. typename DerivedQ>
  59. IGL_INLINE void dual_contouring(
  60. const std::function<
  61. typename DerivedV::Scalar(const Eigen::Matrix<typename DerivedV::Scalar,1,3> &)> & f,
  62. const std::function<
  63. Eigen::Matrix<typename DerivedV::Scalar,1,3>(
  64. const Eigen::Matrix<typename DerivedV::Scalar,1,3> &)> & f_grad,
  65. const Eigen::MatrixBase<DerivedGf> & Gf,
  66. const Eigen::MatrixBase<DerivedGV> & GV,
  67. const int nx,
  68. const int ny,
  69. const int nz,
  70. const bool constrained,
  71. const bool triangles,
  72. const bool root_finding,
  73. Eigen::PlainObjectBase<DerivedV> & V,
  74. Eigen::PlainObjectBase<DerivedQ> & Q);
  75. /// \overload
  76. /// \brief Sparse voxel grid
  77. ///
  78. /// @param[in] Gf #GV list of corresponding f values. If using root finding then only the
  79. /// sign needs to be correct.
  80. /// @param[in] GV #GV by 3 list of sparse grid positions referenced by GI
  81. /// @param[in] GI #GI by 2 list of edge indices into rows of GV
  82. template <
  83. typename DerivedGf,
  84. typename DerivedGV,
  85. typename DerivedGI,
  86. typename DerivedV,
  87. typename DerivedQ>
  88. IGL_INLINE void dual_contouring(
  89. const std::function<typename DerivedV::Scalar(const Eigen::Matrix<typename DerivedV::Scalar,1,3> &)> & f,
  90. const std::function<Eigen::Matrix<typename DerivedV::Scalar,1,3>(const Eigen::Matrix<typename DerivedV::Scalar,1,3> &)> & f_grad,
  91. const Eigen::Matrix<typename DerivedV::Scalar,1,3> & step,
  92. const Eigen::MatrixBase<DerivedGf> & Gf,
  93. const Eigen::MatrixBase<DerivedGV> & GV,
  94. const Eigen::MatrixBase<DerivedGI> & GI,
  95. const bool constrained,
  96. const bool triangles,
  97. const bool root_finding,
  98. Eigen::PlainObjectBase<DerivedV> & V,
  99. Eigen::PlainObjectBase<DerivedQ> & Q);
  100. }
  101. #ifndef IGL_STATIC_LIBRARY
  102. # include "dual_contouring.cpp"
  103. #endif
  104. #endif