sample_mesh.cpp 2.8 KB

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