random_points_on_mesh_intrinsic.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "random_points_on_mesh_intrinsic.h"
  2. #include "cumsum.h"
  3. #include "histc.h"
  4. #include <cassert>
  5. #include <random>
  6. template <
  7. typename DeriveddblA,
  8. typename DerivedB,
  9. typename DerivedFI,
  10. typename URBG>
  11. IGL_INLINE void igl::random_points_on_mesh_intrinsic(
  12. const int n,
  13. const Eigen::MatrixBase<DeriveddblA > & dblA,
  14. Eigen::PlainObjectBase<DerivedB > & B,
  15. Eigen::PlainObjectBase<DerivedFI > & FI,
  16. URBG && urbg)
  17. {
  18. typedef typename DeriveddblA::Scalar Scalar;
  19. typedef Eigen::Matrix<Scalar ,Eigen::Dynamic,1> VectorXs;
  20. VectorXs C;
  21. VectorXs A0(dblA.size()+1);
  22. A0(0) = 0;
  23. A0.bottomRightCorner(dblA.size(),1) = dblA;
  24. // Even faster would be to use the "Alias Table Method"
  25. cumsum(A0,1,C);
  26. const Scalar Cmax = C(C.size()-1);
  27. assert(Cmax > 0 && "Total surface area should be positive");
  28. // Why is this more accurate than `C /= C(C.size()-1)` ?
  29. for(int i = 0;i<C.size();i++) { C(i) = C(i)/Cmax; }
  30. std::uniform_real_distribution<Scalar> dis(-1.0, 1.0);
  31. const VectorXs R = (VectorXs::NullaryExpr(n,1,[&](){return dis(urbg);}).array() + 1.)/2.;
  32. assert(R.minCoeff() >= 0);
  33. assert(R.maxCoeff() <= 1);
  34. histc(R,C,FI);
  35. // fix the bin when R(i) == 1 exactly
  36. // Gross cast to deal with Windows
  37. FI = FI.array().min(static_cast<typename DerivedFI::Scalar>(dblA.rows() - 1));
  38. const VectorXs S = (VectorXs::NullaryExpr(n,1,[&](){return dis(urbg);}).array() + 1.)/2.;
  39. const VectorXs T = (VectorXs::NullaryExpr(n,1,[&](){return dis(urbg);}).array() + 1.)/2.;
  40. B.resize(n,3);
  41. B.col(0) = 1.-T.array().sqrt();
  42. B.col(1) = (1.-S.array()) * T.array().sqrt();
  43. B.col(2) = S.array() * T.array().sqrt();
  44. }
  45. template <
  46. typename DeriveddblA,
  47. typename DerivedF,
  48. typename ScalarB,
  49. typename DerivedFI,
  50. typename URBG>
  51. IGL_INLINE void igl::random_points_on_mesh_intrinsic(
  52. const int n,
  53. const Eigen::MatrixBase<DeriveddblA > & dblA,
  54. const int num_vertices,
  55. const Eigen::MatrixBase<DerivedF> & F,
  56. Eigen::SparseMatrix<ScalarB > & B,
  57. Eigen::PlainObjectBase<DerivedFI > & FI,
  58. URBG && urbg)
  59. {
  60. Eigen::Matrix<ScalarB ,Eigen::Dynamic,3> BC;
  61. // Should be traingle mesh. Although Turk's method 1 generalizes...
  62. assert(F.cols() == 3);
  63. random_points_on_mesh_intrinsic(n,dblA,BC,FI,urbg);
  64. std::vector<Eigen::Triplet<ScalarB> > BIJV;
  65. BIJV.reserve(n*3);
  66. for(int s = 0;s<n;s++)
  67. {
  68. for(int c = 0;c<3;c++)
  69. {
  70. assert(FI(s) < dblA.rows());
  71. assert(FI(s) >= 0);
  72. const int v = F(FI(s),c);
  73. BIJV.push_back(Eigen::Triplet<ScalarB>(s,v,BC(s,c)));
  74. }
  75. }
  76. B.resize(n,num_vertices);
  77. B.reserve(n*3);
  78. B.setFromTriplets(BIJV.begin(),BIJV.end());
  79. }
  80. #ifdef IGL_STATIC_LIBRARY
  81. // Explicit template instantiation
  82. template void igl::random_points_on_mesh_intrinsic<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::minstd_rand&>(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::minstd_rand&);
  83. template void igl::random_points_on_mesh_intrinsic<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::minstd_rand0&>(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::minstd_rand0&);
  84. template void igl::random_points_on_mesh_intrinsic<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::mt19937&>(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::mt19937&);
  85. template void igl::random_points_on_mesh_intrinsic<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::mt19937_64&>(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::mt19937_64&);
  86. template void igl::random_points_on_mesh_intrinsic<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::mt19937&>(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::mt19937&);
  87. template void igl::random_points_on_mesh_intrinsic<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::mt19937&>(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::mt19937&);
  88. template void igl::random_points_on_mesh_intrinsic<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, std::mt19937_64&>(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::mt19937_64&);
  89. #endif