random_points_on_mesh.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "random_points_on_mesh.h"
  2. #include "doublearea.h"
  3. #include "cumsum.h"
  4. #include "histc.h"
  5. #include "matlab_format.h"
  6. #include <iostream>
  7. #include <cassert>
  8. template <typename DerivedV, typename DerivedF, typename DerivedB, typename DerivedFI>
  9. IGL_INLINE void igl::random_points_on_mesh(
  10. const int n,
  11. const Eigen::PlainObjectBase<DerivedV > & V,
  12. const Eigen::PlainObjectBase<DerivedF > & F,
  13. Eigen::PlainObjectBase<DerivedB > & B,
  14. Eigen::PlainObjectBase<DerivedFI > & FI)
  15. {
  16. using namespace Eigen;
  17. using namespace std;
  18. typedef typename DerivedV::Scalar Scalar;
  19. typedef Matrix<Scalar,Dynamic,1> VectorXs;
  20. VectorXs A;
  21. doublearea(V,F,A);
  22. A /= A.array().sum();
  23. // Should be traingle mesh. Although Turk's method 1 generalizes...
  24. assert(F.cols() == 3);
  25. VectorXs C;
  26. VectorXs A0(A.size()+1);
  27. A0(0) = 0;
  28. A0.bottomRightCorner(A.size(),1) = A;
  29. cumsum(A0,1,C);
  30. const VectorXs R = (VectorXs::Random(n,1).array() + 1.)/2.;
  31. assert(R.minCoeff() >= 0);
  32. assert(R.maxCoeff() <= 1);
  33. histc(R,C,FI);
  34. const VectorXs S = (VectorXs::Random(n,1).array() + 1.)/2.;
  35. const VectorXs T = (VectorXs::Random(n,1).array() + 1.)/2.;
  36. B.resize(n,3);
  37. B.col(0) = 1.-T.array().sqrt();
  38. B.col(1) = (1.-S.array()) * T.array().sqrt();
  39. B.col(2) = S.array() * T.array().sqrt();
  40. }
  41. template <typename DerivedV, typename DerivedF, typename ScalarB, typename DerivedFI>
  42. IGL_INLINE void igl::random_points_on_mesh(
  43. const int n,
  44. const Eigen::PlainObjectBase<DerivedV > & V,
  45. const Eigen::PlainObjectBase<DerivedF > & F,
  46. Eigen::SparseMatrix<ScalarB > & B,
  47. Eigen::PlainObjectBase<DerivedFI > & FI)
  48. {
  49. using namespace Eigen;
  50. using namespace std;
  51. Matrix<ScalarB,Dynamic,3> BC;
  52. random_points_on_mesh(n,V,F,BC,FI);
  53. vector<Triplet<ScalarB> > BIJV;
  54. BIJV.reserve(n*3);
  55. for(int s = 0;s<n;s++)
  56. {
  57. for(int c = 0;c<3;c++)
  58. {
  59. assert(FI(s) < F.rows());
  60. assert(FI(s) >= 0);
  61. const int v = F(FI(s),c);
  62. BIJV.push_back(Triplet<ScalarB>(s,v,BC(s,c)));
  63. }
  64. }
  65. B.resize(n,V.rows());
  66. B.reserve(n*3);
  67. B.setFromTriplets(BIJV.begin(),BIJV.end());
  68. }
  69. #ifndef IGL_HEADER_ONLY
  70. // Explicit template specialization
  71. template void igl::random_points_on_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  72. template void igl::random_points_on_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  73. #endif