swept_volume_signed_distance.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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. #include "swept_volume_signed_distance.h"
  9. #include "flood_fill.h"
  10. #include "signed_distance.h"
  11. #include "AABB.h"
  12. #include "pseudonormal_test.h"
  13. #include "per_face_normals.h"
  14. #include "per_vertex_normals.h"
  15. #include "per_edge_normals.h"
  16. #include <Eigen/Geometry>
  17. #include <cmath>
  18. IGL_INLINE void igl::swept_volume_signed_distance(
  19. const Eigen::MatrixXd & V,
  20. const Eigen::MatrixXi & F,
  21. const std::function<Eigen::Affine3d(const double t)> & transform,
  22. const size_t & steps,
  23. const Eigen::MatrixXd & GV,
  24. const Eigen::RowVector3i & res,
  25. const double h,
  26. const double isolevel,
  27. const Eigen::VectorXd & S0,
  28. Eigen::VectorXd & S)
  29. {
  30. using namespace std;
  31. using namespace igl;
  32. using namespace Eigen;
  33. S = S0;
  34. const VectorXd t = VectorXd::LinSpaced(steps,0,1);
  35. const bool finite_iso = isfinite(isolevel);
  36. const double extension = (finite_iso ? isolevel : 0) + sqrt(3.0)*h;
  37. Eigen::AlignedBox3d box(
  38. V.colwise().minCoeff().array()-extension,
  39. V.colwise().maxCoeff().array()+extension);
  40. // Precomputation
  41. Eigen::MatrixXd FN,VN,EN;
  42. Eigen::MatrixXi E;
  43. Eigen::VectorXi EMAP;
  44. per_face_normals(V,F,FN);
  45. per_vertex_normals(V,F,PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE,FN,VN);
  46. per_edge_normals(
  47. V,F,PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM,FN,EN,E,EMAP);
  48. AABB<MatrixXd,3> tree;
  49. tree.init(V,F);
  50. for(int ti = 0;ti<t.size();ti++)
  51. {
  52. const Affine3d At = transform(t(ti));
  53. for(int g = 0;g<GV.rows();g++)
  54. {
  55. // Don't bother finding out how deep inside points are.
  56. if(finite_iso && S(g)==S(g) && S(g)<isolevel-sqrt(3.0)*h)
  57. {
  58. continue;
  59. }
  60. const RowVector3d gv =
  61. (GV.row(g) - At.translation().transpose())*At.linear();
  62. // If outside of extended box, then consider it "far away enough"
  63. if(finite_iso && !box.contains(gv.transpose()))
  64. {
  65. continue;
  66. }
  67. RowVector3d c,n;
  68. int i;
  69. double sqrd,s;
  70. //signed_distance_pseudonormal(tree,V,F,FN,VN,EN,EMAP,gv,s,sqrd,i,c,n);
  71. const double min_sqrd =
  72. finite_iso ?
  73. pow(sqrt(3.)*h+isolevel,2) :
  74. numeric_limits<double>::infinity();
  75. sqrd = tree.squared_distance(V,F,gv,min_sqrd,i,c);
  76. if(sqrd<min_sqrd)
  77. {
  78. pseudonormal_test(V,F,FN,VN,EN,EMAP,gv,i,c,s,n);
  79. if(S(g) == S(g))
  80. {
  81. S(g) = min(S(g),s*sqrt(sqrd));
  82. }else
  83. {
  84. S(g) = s*sqrt(sqrd);
  85. }
  86. }
  87. }
  88. }
  89. if(finite_iso)
  90. {
  91. flood_fill(res,S);
  92. }else
  93. {
  94. #ifndef NDEBUG
  95. // Check for nans
  96. for_each(S.data(),S.data()+S.size(),[](const double s){assert(s==s);});
  97. #endif
  98. }
  99. }
  100. IGL_INLINE void igl::swept_volume_signed_distance(
  101. const Eigen::MatrixXd & V,
  102. const Eigen::MatrixXi & F,
  103. const std::function<Eigen::Affine3d(const double t)> & transform,
  104. const size_t & steps,
  105. const Eigen::MatrixXd & GV,
  106. const Eigen::RowVector3i & res,
  107. const double h,
  108. const double isolevel,
  109. Eigen::VectorXd & S)
  110. {
  111. using namespace std;
  112. using namespace igl;
  113. using namespace Eigen;
  114. S = VectorXd::Constant(GV.rows(),1,numeric_limits<double>::quiet_NaN());
  115. return
  116. swept_volume_signed_distance(V,F,transform,steps,GV,res,h,isolevel,S,S);
  117. }