upsample.cpp 2.9 KB

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