upsample.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef IGL_UPSAMPLE_H
  2. #define IGL_UPSAMPLE_H
  3. namespace igl
  4. {
  5. // Subdivide a mesh without moving vertices: loop subdivision but odd
  6. // vertices stay put and even vertices are just edge midpoints
  7. //
  8. // Templates:
  9. // MatV matrix for vertex positions, e.g. MatrixXd
  10. // MatF matrix for vertex positions, e.g. MatrixXi
  11. // Inputs:
  12. // V #V by dim mesh vertices
  13. // F #F by 3 mesh triangles
  14. // Outputs:
  15. // NV new vertex positions, V is guaranteed to be at top
  16. // NF new list of face indices
  17. //
  18. // NOTE: V should not be the same as NV,
  19. // NOTE: F should not be the same as NF, use other proto
  20. template <typename MatV, typename MatF>
  21. inline void upsample( const MatV & V, const MatF & F, MatV & NV, MatF & NF);
  22. // Virtually in place wrapper
  23. template <typename MatV, typename MatF>
  24. inline void upsample( MatV & V,MatF & F);
  25. }
  26. // Implementation
  27. #include <tt.h>
  28. #include <adjacency_list.h>
  29. #include <Eigen/Dense>
  30. template <typename MatV, typename MatF>
  31. inline void igl::upsample( const MatV & V, const MatF & F, MatV & NV, MatF & NF)
  32. {
  33. // Use "in place" wrapper instead
  34. assert(&V != &NV);
  35. assert(&F != &NF);
  36. using namespace igl;
  37. using namespace std;
  38. using namespace Eigen;
  39. MatF FF, FFi;
  40. tt<double>(V,F,FF,FFi);
  41. // TODO: Cache optimization missing from here, it is a mess
  42. // Compute the number and positions of the vertices to insert (on edges)
  43. MatF NI = MatF::Constant(FF.rows(),FF.cols(),-1);
  44. int counter = 0;
  45. for(int i=0;i<FF.rows();++i)
  46. {
  47. for(int j=0;j<3;++j)
  48. {
  49. if(NI(i,j) == -1)
  50. {
  51. NI(i,j) = counter;
  52. if (FF(i,j) != -1) // If it is not a border
  53. NI(FF(i,j),FFi(i,j)) = counter;
  54. ++counter;
  55. }
  56. }
  57. }
  58. int n_odd = V.rows();
  59. int n_even = counter;
  60. Eigen::DynamicSparseMatrix<double> SUBD(V.rows()+n_even,V.rows());
  61. SUBD.reserve(15 * (V.rows()+n_even));
  62. // Preallocate NV and NF
  63. NV = MatV(V.rows()+n_even,V.cols());
  64. NF = MatF(F.rows()*4,3);
  65. // Fill the odd vertices position
  66. NV.block(0,0,V.rows(),V.cols()) = V;
  67. // Fill the even vertices position
  68. for(int i=0;i<FF.rows();++i)
  69. {
  70. for(int j=0;j<3;++j)
  71. {
  72. NV.row(NI(i,j) + n_odd) = 0.5 * V.row(F(i,j)) + 0.5 * V.row(F(i,(j+1)%3));
  73. }
  74. }
  75. // Build the new topology (Every face is replaced by four)
  76. for(int i=0; i<F.rows();++i)
  77. {
  78. VectorXi VI(6);
  79. 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;
  80. VectorXi f0(3), f1(3), f2(3), f3(3);
  81. f0 << VI(0), VI(3), VI(5);
  82. f1 << VI(1), VI(4), VI(3);
  83. f2 << VI(3), VI(4), VI(5);
  84. f3 << VI(4), VI(2), VI(5);
  85. NF.row((i*4)+0) = f0;
  86. NF.row((i*4)+1) = f1;
  87. NF.row((i*4)+2) = f2;
  88. NF.row((i*4)+3) = f3;
  89. }
  90. }
  91. template <typename MatV, typename MatF>
  92. inline void igl::upsample( MatV & V,MatF & F)
  93. {
  94. const MatV V_copy = V;
  95. const MatF F_copy = F;
  96. return upsample(V_copy,F_copy,V,F);
  97. }
  98. #endif