random_points_on_mesh_intrinsic.cpp 5.5 KB

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