remesh_along_isoline.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2018 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 "remesh_along_isoline.h"
  9. #include "list_to_matrix.h"
  10. template <
  11. typename DerivedV,
  12. typename DerivedF,
  13. typename DerivedS,
  14. typename DerivedU,
  15. typename DerivedG,
  16. typename DerivedJ,
  17. typename BCtype,
  18. typename DerivedSU,
  19. typename DerivedL>
  20. IGL_INLINE void igl::remesh_along_isoline(
  21. const Eigen::MatrixBase<DerivedV> & V,
  22. const Eigen::MatrixBase<DerivedF> & F,
  23. const Eigen::MatrixBase<DerivedS> & S,
  24. const typename DerivedS::Scalar val,
  25. Eigen::PlainObjectBase<DerivedU> & U,
  26. Eigen::PlainObjectBase<DerivedG> & G,
  27. Eigen::PlainObjectBase<DerivedSU> & SU,
  28. Eigen::PlainObjectBase<DerivedJ> & J,
  29. Eigen::SparseMatrix<BCtype> & BC,
  30. Eigen::PlainObjectBase<DerivedL> & L)
  31. {
  32. igl::remesh_along_isoline(V.rows(),F,S,val,G,SU,J,BC,L);
  33. U = BC * V;
  34. }
  35. template <
  36. typename DerivedF,
  37. typename DerivedS,
  38. typename DerivedG,
  39. typename DerivedJ,
  40. typename BCtype,
  41. typename DerivedSU,
  42. typename DerivedL>
  43. IGL_INLINE void igl::remesh_along_isoline(
  44. const int num_vertices,
  45. const Eigen::MatrixBase<DerivedF> & F,
  46. const Eigen::MatrixBase<DerivedS> & S,
  47. const typename DerivedS::Scalar val,
  48. Eigen::PlainObjectBase<DerivedG> & G,
  49. Eigen::PlainObjectBase<DerivedSU> & SU,
  50. Eigen::PlainObjectBase<DerivedJ> & J,
  51. Eigen::SparseMatrix<BCtype> & BC,
  52. Eigen::PlainObjectBase<DerivedL> & L)
  53. {
  54. // Lazy implementation using vectors
  55. //assert(val.size() == 1);
  56. const int isoval_i = 0;
  57. //auto isoval = val(isoval_i);
  58. auto isoval = val;
  59. std::vector<std::vector<typename DerivedG::Scalar> > vG;
  60. std::vector<typename DerivedJ::Scalar> vJ;
  61. std::vector<typename DerivedL::Scalar> vL;
  62. std::vector<Eigen::Triplet<BCtype> > vBC;
  63. int Ucount = 0;
  64. for(int i = 0;i<num_vertices;i++)
  65. {
  66. vBC.emplace_back(Ucount,i,1.0);
  67. Ucount++;
  68. }
  69. // Loop over each face
  70. for(int f = 0;f<F.rows();f++)
  71. {
  72. bool Psign[2];
  73. int P[2];
  74. int count = 0;
  75. for(int p = 0;p<3;p++)
  76. {
  77. const bool psign = S(F(f,p)) > isoval;
  78. // Find crossings
  79. const int n = (p+1)%3;
  80. const bool nsign = S(F(f,n)) > isoval;
  81. if(psign != nsign)
  82. {
  83. P[count] = p;
  84. Psign[count] = psign;
  85. // record crossing
  86. count++;
  87. }
  88. }
  89. assert(count == 0 || count == 2);
  90. switch(count)
  91. {
  92. case 0:
  93. {
  94. // Easy case
  95. std::vector<typename DerivedG::Scalar> row = {F(f,0),F(f,1),F(f,2)};
  96. vG.push_back(row);
  97. vJ.push_back(f);
  98. vL.push_back( S(F(f,0))>isoval ? isoval_i+1 : isoval_i );
  99. break;
  100. }
  101. case 2:
  102. {
  103. // Cut case
  104. // flip so that P[1] is the one-off vertex
  105. if(P[0] == 0 && P[1] == 2)
  106. {
  107. std::swap(P[0],P[1]);
  108. std::swap(Psign[0],Psign[1]);
  109. }
  110. assert(Psign[0] != Psign[1]);
  111. // Create two new vertices
  112. for(int i = 0;i<2;i++)
  113. {
  114. const double bci = (isoval - S(F(f,(P[i]+1)%3)))/
  115. (S(F(f,P[i]))-S(F(f,(P[i]+1)%3)));
  116. vBC.emplace_back(Ucount,F(f,P[i]),bci);
  117. vBC.emplace_back(Ucount,F(f,(P[i]+1)%3),1.0-bci);
  118. Ucount++;
  119. }
  120. const int v0 = F(f,P[0]);
  121. const int v01 = Ucount-2;
  122. assert(((P[0]+1)%3) == P[1]);
  123. const int v1 = F(f,P[1]);
  124. const int v12 = Ucount-1;
  125. const int v2 = F(f,(P[1]+1)%3);
  126. // v0
  127. // | \
  128. // | \
  129. // | \
  130. // v01 \
  131. // | \
  132. // | \
  133. // | \
  134. // v1--v12---v2
  135. typedef std::vector<typename DerivedG::Scalar> Row;
  136. {Row row = {v01,v1,v12}; vG.push_back(row);vJ.push_back(f);vL.push_back(Psign[0]?isoval_i:isoval_i+1);}
  137. {Row row = {v12,v2,v01}; vG.push_back(row);vJ.push_back(f);vL.push_back(Psign[1]?isoval_i:isoval_i+1);}
  138. {Row row = {v2,v0,v01}; vG.push_back(row) ;vJ.push_back(f);vL.push_back(Psign[1]?isoval_i:isoval_i+1);}
  139. break;
  140. }
  141. default: assert(false);
  142. }
  143. }
  144. igl::list_to_matrix(vG,G);
  145. igl::list_to_matrix(vJ,J);
  146. igl::list_to_matrix(vL,L);
  147. BC.resize(Ucount,num_vertices);
  148. BC.setFromTriplets(vBC.begin(),vBC.end());
  149. SU = BC * S;
  150. }
  151. #ifdef IGL_STATIC_LIBRARY
  152. // Explicit template instantiation
  153. template void igl::remesh_along_isoline<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<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -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::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<double, -1, 1, 0, -1, 1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::SparseMatrix<double, 0, int>&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  154. #endif