upsample.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "upsample.h"
  9. #include "triangle_triangle_adjacency.h"
  10. template <
  11. typename DerivedF,
  12. typename DerivedS,
  13. typename DerivedNF>
  14. IGL_INLINE void igl::upsample(const int n_verts,
  15. const Eigen::PlainObjectBase<DerivedF>& F,
  16. Eigen::SparseMatrix<DerivedS>& S,
  17. Eigen::PlainObjectBase<DerivedNF>& NF)
  18. {
  19. // Use "in place" wrapper instead
  20. assert(&V != &NV);
  21. assert(&F != &NF);
  22. using namespace std;
  23. using namespace Eigen;
  24. typedef Eigen::Triplet<DerivedS> Triplet_t;
  25. Eigen::Matrix<
  26. typename DerivedF::Scalar,Eigen::Dynamic,Eigen::Dynamic>
  27. FF,FFi;
  28. triangle_triangle_adjacency(F,FF,FFi);
  29. // TODO: Cache optimization missing from here, it is a mess
  30. // Compute the number and positions of the vertices to insert (on edges)
  31. Eigen::MatrixXi NI = Eigen::MatrixXi::Constant(FF.rows(),FF.cols(),-1);
  32. Eigen::MatrixXi NIdoubles = Eigen::MatrixXi::Zero(FF.rows(), FF.cols());
  33. int counter = 0;
  34. for(int i=0;i<FF.rows();++i)
  35. {
  36. for(int j=0;j<3;++j)
  37. {
  38. if(NI(i,j) == -1)
  39. {
  40. NI(i,j) = counter;
  41. NIdoubles(i,j) = 0;
  42. if (FF(i,j) != -1) {
  43. //If it is not a boundary
  44. NI(FF(i,j), FFi(i,j)) = counter;
  45. NIdoubles(i,j) = 1;
  46. }
  47. ++counter;
  48. }
  49. }
  50. }
  51. const int& n_odd = n_verts;
  52. const int& n_even = counter;
  53. const int n_newverts = n_odd + n_even;
  54. //Construct vertex positions
  55. std::vector<Triplet_t> tripletList;
  56. // Fill the odd vertices position
  57. for (int i=0; i<n_odd; ++i)
  58. {
  59. tripletList.emplace_back(i, i, 1.);
  60. }
  61. for(int i=0;i<FF.rows();++i)
  62. {
  63. for(int j=0;j<3;++j)
  64. {
  65. if(NIdoubles(i,j)==0) {
  66. tripletList.emplace_back(NI(i,j) + n_odd, F(i,j), 1./2.);
  67. tripletList.emplace_back(NI(i,j) + n_odd, F(i,(j+1)%3), 1./2.);
  68. }
  69. }
  70. }
  71. S.resize(n_newverts, n_verts);
  72. S.setFromTriplets(tripletList.begin(), tripletList.end());
  73. // Build the new topology (Every face is replaced by four)
  74. NF.resize(F.rows()*4,3);
  75. for(int i=0; i<F.rows();++i)
  76. {
  77. VectorXi VI(6);
  78. VI << F(i,0), F(i,1), F(i,2), NI(i,0) + n_odd, NI(i,1) + n_odd, NI(i,2) + n_odd;
  79. VectorXi f0(3), f1(3), f2(3), f3(3);
  80. f0 << VI(0), VI(3), VI(5);
  81. f1 << VI(1), VI(4), VI(3);
  82. f2 << VI(3), VI(4), VI(5);
  83. f3 << VI(4), VI(2), VI(5);
  84. NF.row((i*4)+0) = f0;
  85. NF.row((i*4)+1) = f1;
  86. NF.row((i*4)+2) = f2;
  87. NF.row((i*4)+3) = f3;
  88. }
  89. }
  90. template <
  91. typename DerivedV,
  92. typename DerivedF,
  93. typename DerivedNV,
  94. typename DerivedNF>
  95. IGL_INLINE void igl::upsample(
  96. const Eigen::PlainObjectBase<DerivedV>& V,
  97. const Eigen::PlainObjectBase<DerivedF>& F,
  98. Eigen::PlainObjectBase<DerivedNV>& NV,
  99. Eigen::PlainObjectBase<DerivedNF>& NF,
  100. const int number_of_subdivs)
  101. {
  102. typedef Eigen::SparseMatrix<double> SparseMat;
  103. typedef Eigen::Triplet<double> Triplet_t;
  104. NV = V;
  105. NF = F;
  106. for(int i=0; i<number_of_subdivs; ++i) {
  107. DerivedNF tempF = NF;
  108. SparseMat S;
  109. upsample(NV.rows(), tempF, S, NF);
  110. NV = S*NV;
  111. }
  112. }
  113. template <
  114. typename MatV,
  115. typename MatF>
  116. IGL_INLINE void igl::upsample(MatV& V,
  117. MatF& F,
  118. const int number_of_subdivs)
  119. {
  120. const MatV V_copy = V;
  121. const MatF F_copy = F;
  122. return upsample(V_copy,F_copy,V,F,number_of_subdivs);
  123. }
  124. #ifdef IGL_STATIC_LIBRARY
  125. // Explicit template specialization
  126. // generated by autoexplicit.sh
  127. template void igl::upsample<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  128. template void igl::upsample<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<int, -1, -1, 0, -1, -1>&);
  129. #endif