loop.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Oded Stein <[email protected]>
  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 "loop.h"
  9. #include "adjacency_list.h"
  10. #include "triangle_triangle_adjacency.h"
  11. #include "unique.h"
  12. #include <vector>
  13. template <
  14. typename DerivedF,
  15. typename SType,
  16. typename DerivedNF>
  17. IGL_INLINE void igl::loop(
  18. const int n_verts,
  19. const Eigen::MatrixBase<DerivedF> & F,
  20. Eigen::SparseMatrix<SType>& S,
  21. Eigen::PlainObjectBase<DerivedNF> & NF)
  22. {
  23. typedef Eigen::Triplet<SType> Triplet_t;
  24. //Ref. https://graphics.stanford.edu/~mdfisher/subdivision.html
  25. //Heavily borrowing from igl::upsample
  26. Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, Eigen::Dynamic> FF, FFi;
  27. triangle_triangle_adjacency(F, FF, FFi);
  28. std::vector<std::vector<typename DerivedF::Scalar>> adjacencyList;
  29. adjacency_list(F, adjacencyList, true);
  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. Eigen::MatrixXi NIdoubles = Eigen::MatrixXi::Zero(FF.rows(), FF.cols());
  33. Eigen::VectorXi vertIsOnBdry = Eigen::VectorXi::Zero(n_verts);
  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. NIdoubles(i,j) = 0;
  43. if (FF(i,j) != -1)
  44. {
  45. //If it is not a boundary
  46. NI(FF(i,j), FFi(i,j)) = counter;
  47. NIdoubles(i,j) = 1;
  48. } else
  49. {
  50. //Mark boundary vertices for later
  51. vertIsOnBdry(F(i,j)) = 1;
  52. vertIsOnBdry(F(i,(j+1)%3)) = 1;
  53. }
  54. ++counter;
  55. }
  56. }
  57. }
  58. const int& n_odd = n_verts;
  59. const int& n_even = counter;
  60. const int n_newverts = n_odd + n_even;
  61. //Construct vertex positions
  62. std::vector<Triplet_t> tripletList;
  63. for(int i=0; i<n_odd; ++i)
  64. {
  65. //Old vertices
  66. const auto& localAdjList = adjacencyList[i];
  67. if(vertIsOnBdry(i)==1)
  68. {
  69. //Boundary vertex
  70. tripletList.emplace_back(i, localAdjList.front(), 1./8.);
  71. tripletList.emplace_back(i, localAdjList.back(), 1./8.);
  72. tripletList.emplace_back(i, i, 3./4.);
  73. } else
  74. {
  75. const int n = localAdjList.size();
  76. const SType dn = n;
  77. SType beta;
  78. if(n==3)
  79. {
  80. beta = 3./16.;
  81. } else
  82. {
  83. beta = 3./8./dn;
  84. }
  85. for(int j=0; j<n; ++j)
  86. {
  87. tripletList.emplace_back(i, localAdjList[j], beta);
  88. }
  89. tripletList.emplace_back(i, i, 1.-dn*beta);
  90. }
  91. }
  92. for(int i=0; i<FF.rows(); ++i)
  93. {
  94. //New vertices
  95. for(int j=0; j<3; ++j)
  96. {
  97. if(NIdoubles(i,j)==0)
  98. {
  99. if(FF(i,j)==-1)
  100. {
  101. //Boundary vertex
  102. tripletList.emplace_back(NI(i,j) + n_odd, F(i,j), 1./2.);
  103. tripletList.emplace_back(NI(i,j) + n_odd, F(i, (j+1)%3), 1./2.);
  104. } else
  105. {
  106. tripletList.emplace_back(NI(i,j) + n_odd, F(i,j), 3./8.);
  107. tripletList.emplace_back(NI(i,j) + n_odd, F(i, (j+1)%3), 3./8.);
  108. tripletList.emplace_back(NI(i,j) + n_odd, F(i, (j+2)%3), 1./8.);
  109. tripletList.emplace_back(NI(i,j) + n_odd, F(FF(i,j), (FFi(i,j)+2)%3), 1./8.);
  110. }
  111. }
  112. }
  113. }
  114. S.resize(n_newverts, n_verts);
  115. S.setFromTriplets(tripletList.begin(), tripletList.end());
  116. // Build the new topology (Every face is replaced by four)
  117. NF.resize(F.rows()*4, 3);
  118. for(int i=0; i<F.rows();++i)
  119. {
  120. Eigen::Matrix<typename DerivedF::Scalar, 6, 1> VI(6);
  121. 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;
  122. Eigen::Matrix<typename DerivedF::Scalar, 3, 1> f0(3), f1(3), f2(3), f3(3);
  123. f0 << VI(0), VI(3), VI(5);
  124. f1 << VI(1), VI(4), VI(3);
  125. f2 << VI(3), VI(4), VI(5);
  126. f3 << VI(4), VI(2), VI(5);
  127. NF.row((i*4)+0) = f0;
  128. NF.row((i*4)+1) = f1;
  129. NF.row((i*4)+2) = f2;
  130. NF.row((i*4)+3) = f3;
  131. }
  132. }
  133. template <
  134. typename DerivedV,
  135. typename DerivedF,
  136. typename DerivedNV,
  137. typename DerivedNF>
  138. IGL_INLINE void igl::loop(
  139. const Eigen::MatrixBase<DerivedV>& V,
  140. const Eigen::MatrixBase<DerivedF>& F,
  141. Eigen::PlainObjectBase<DerivedNV>& NV,
  142. Eigen::PlainObjectBase<DerivedNF>& NF,
  143. const int number_of_subdivs)
  144. {
  145. NV = V;
  146. NF = F;
  147. for(int i=0; i<number_of_subdivs; ++i)
  148. {
  149. DerivedNF tempF = NF;
  150. Eigen::SparseMatrix<typename DerivedV::Scalar> S;
  151. loop(NV.rows(), tempF, S, NF);
  152. // This .eval is super important
  153. NV = (S*NV).eval();
  154. }
  155. }
  156. #ifdef IGL_STATIC_LIBRARY
  157. template void igl::loop<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>>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1>> const &, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>> const &, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1>> &, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>> &, int);
  158. #endif