upsample.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "upsample.h"
  2. #include "tt.h"
  3. #include "adjacency_list.h"
  4. #include <Eigen/Dense>
  5. template <typename MatV, typename MatF>
  6. IGL_INLINE void igl::upsample( const MatV & V, const MatF & F, MatV & NV, MatF & NF)
  7. {
  8. // Use "in place" wrapper instead
  9. assert(&V != &NV);
  10. assert(&F != &NF);
  11. using namespace igl;
  12. using namespace std;
  13. using namespace Eigen;
  14. MatF FF, FFi;
  15. tt<double>(V,F,FF,FFi);
  16. // TODO: Cache optimization missing from here, it is a mess
  17. // Compute the number and positions of the vertices to insert (on edges)
  18. MatF NI = MatF::Constant(FF.rows(),FF.cols(),-1);
  19. int counter = 0;
  20. for(int i=0;i<FF.rows();++i)
  21. {
  22. for(int j=0;j<3;++j)
  23. {
  24. if(NI(i,j) == -1)
  25. {
  26. NI(i,j) = counter;
  27. if (FF(i,j) != -1) // If it is not a border
  28. NI(FF(i,j),FFi(i,j)) = counter;
  29. ++counter;
  30. }
  31. }
  32. }
  33. int n_odd = V.rows();
  34. int n_even = counter;
  35. Eigen::DynamicSparseMatrix<double> SUBD(V.rows()+n_even,V.rows());
  36. SUBD.reserve(15 * (V.rows()+n_even));
  37. // Preallocate NV and NF
  38. NV = MatV(V.rows()+n_even,V.cols());
  39. NF = MatF(F.rows()*4,3);
  40. // Fill the odd vertices position
  41. NV.block(0,0,V.rows(),V.cols()) = V;
  42. // Fill the even vertices position
  43. for(int i=0;i<FF.rows();++i)
  44. {
  45. for(int j=0;j<3;++j)
  46. {
  47. NV.row(NI(i,j) + n_odd) = 0.5 * V.row(F(i,j)) + 0.5 * V.row(F(i,(j+1)%3));
  48. }
  49. }
  50. // Build the new topology (Every face is replaced by four)
  51. for(int i=0; i<F.rows();++i)
  52. {
  53. VectorXi VI(6);
  54. 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;
  55. VectorXi f0(3), f1(3), f2(3), f3(3);
  56. f0 << VI(0), VI(3), VI(5);
  57. f1 << VI(1), VI(4), VI(3);
  58. f2 << VI(3), VI(4), VI(5);
  59. f3 << VI(4), VI(2), VI(5);
  60. NF.row((i*4)+0) = f0;
  61. NF.row((i*4)+1) = f1;
  62. NF.row((i*4)+2) = f2;
  63. NF.row((i*4)+3) = f3;
  64. }
  65. }
  66. template <typename MatV, typename MatF>
  67. IGL_INLINE void igl::upsample( MatV & V,MatF & F)
  68. {
  69. const MatV V_copy = V;
  70. const MatF F_copy = F;
  71. return upsample(V_copy,F_copy,V,F);
  72. }
  73. #ifndef IGL_HEADER_ONLY
  74. // Explicit template specialization
  75. 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>&);
  76. #endif