swept_volume_signed_distance.cpp 3.5 KB

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