upsample.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <Eigen/Dense>
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedNV,
  15. typename DerivedNF>
  16. IGL_INLINE void igl::upsample(
  17. const Eigen::PlainObjectBase<DerivedV>& V,
  18. const Eigen::PlainObjectBase<DerivedF>& F,
  19. Eigen::PlainObjectBase<DerivedNV>& NV,
  20. Eigen::PlainObjectBase<DerivedNF>& NF)
  21. {
  22. // Use "in place" wrapper instead
  23. assert(&V != &NV);
  24. assert(&F != &NF);
  25. using namespace std;
  26. using namespace Eigen;
  27. Eigen::MatrixXi FF,FFi;
  28. triangle_triangle_adjacency(V,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. int counter = 0;
  33. for(int i=0;i<FF.rows();++i)
  34. {
  35. for(int j=0;j<3;++j)
  36. {
  37. if(NI(i,j) == -1)
  38. {
  39. NI(i,j) = counter;
  40. if (FF(i,j) != -1) // If it is not a border
  41. NI(FF(i,j),FFi(i,j)) = counter;
  42. ++counter;
  43. }
  44. }
  45. }
  46. int n_odd = V.rows();
  47. int n_even = counter;
  48. // Preallocate NV and NF
  49. NV.resize(V.rows()+n_even,V.cols());
  50. NF.resize(F.rows()*4,3);
  51. // Fill the odd vertices position
  52. NV.block(0,0,V.rows(),V.cols()) = V;
  53. // Fill the even vertices position
  54. for(int i=0;i<FF.rows();++i)
  55. {
  56. for(int j=0;j<3;++j)
  57. {
  58. NV.row(NI(i,j) + n_odd) = 0.5 * V.row(F(i,j)) + 0.5 * V.row(F(i,(j+1)%3));
  59. }
  60. }
  61. // Build the new topology (Every face is replaced by four)
  62. for(int i=0; i<F.rows();++i)
  63. {
  64. VectorXi VI(6);
  65. 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;
  66. VectorXi f0(3), f1(3), f2(3), f3(3);
  67. f0 << VI(0), VI(3), VI(5);
  68. f1 << VI(1), VI(4), VI(3);
  69. f2 << VI(3), VI(4), VI(5);
  70. f3 << VI(4), VI(2), VI(5);
  71. NF.row((i*4)+0) = f0;
  72. NF.row((i*4)+1) = f1;
  73. NF.row((i*4)+2) = f2;
  74. NF.row((i*4)+3) = f3;
  75. }
  76. }
  77. template <
  78. typename MatV,
  79. typename MatF>
  80. IGL_INLINE void igl::upsample(
  81. MatV& V,
  82. MatF& F)
  83. {
  84. const MatV V_copy = V;
  85. const MatF F_copy = F;
  86. return upsample(V_copy,F_copy,V,F);
  87. }
  88. #ifdef IGL_STATIC_LIBRARY
  89. // Explicit template specialization
  90. 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>&);
  91. #endif