upsample.cpp 4.4 KB

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