upsample.cpp 3.2 KB

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