collapse_small_triangles.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[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 "collapse_small_triangles.h"
  9. #include "bounding_box_diagonal.h"
  10. #include "doublearea.h"
  11. #include "edge_lengths.h"
  12. #include "colon.h"
  13. #include "faces_first.h"
  14. #include <limits>
  15. #include <iostream>
  16. template <
  17. typename DerivedV,
  18. typename DerivedF,
  19. typename DerivedFF>
  20. void igl::collapse_small_triangles(
  21. const Eigen::MatrixBase<DerivedV> & V,
  22. const Eigen::MatrixBase<DerivedF> & F,
  23. const double eps,
  24. Eigen::PlainObjectBase<DerivedFF> & FF)
  25. {
  26. using namespace Eigen;
  27. using namespace std;
  28. // Compute bounding box diagonal length
  29. double bbd = bounding_box_diagonal(V);
  30. MatrixXd l;
  31. edge_lengths(V,F,l);
  32. VectorXd dblA;
  33. doublearea(l,0.,dblA);
  34. // Minimum area tolerance
  35. const double min_dblarea = 2.0*eps*bbd*bbd;
  36. Eigen::VectorXi FIM = colon<int>(0,V.rows()-1);
  37. int num_edge_collapses = 0;
  38. // Loop over triangles
  39. for(int f = 0;f<F.rows();f++)
  40. {
  41. if(dblA(f) < min_dblarea)
  42. {
  43. double minl = 0;
  44. int minli = -1;
  45. // Find shortest edge
  46. for(int e = 0;e<3;e++)
  47. {
  48. if(minli==-1 || l(f,e)<minl)
  49. {
  50. minli = e;
  51. minl = l(f,e);
  52. }
  53. }
  54. double maxl = 0;
  55. int maxli = -1;
  56. // Find longest edge
  57. for(int e = 0;e<3;e++)
  58. {
  59. if(maxli==-1 || l(f,e)>maxl)
  60. {
  61. maxli = e;
  62. maxl = l(f,e);
  63. }
  64. }
  65. // Be sure that min and max aren't the same
  66. maxli = (minli==maxli?(minli+1)%3:maxli);
  67. // Collapse min edge maintaining max edge: i-->j
  68. // Q: Why this direction?
  69. int i = maxli;
  70. int j = ((minli+1)%3 == maxli ? (minli+2)%3: (minli+1)%3);
  71. assert(i != minli);
  72. assert(j != minli);
  73. assert(i != j);
  74. FIM(F(f,i)) = FIM(F(f,j));
  75. num_edge_collapses++;
  76. }
  77. }
  78. // Reindex faces
  79. MatrixXi rF = F;
  80. // Loop over triangles
  81. for(int f = 0;f<rF.rows();f++)
  82. {
  83. for(int i = 0;i<rF.cols();i++)
  84. {
  85. rF(f,i) = FIM(rF(f,i));
  86. }
  87. }
  88. FF.resizeLike(rF);
  89. #ifndef NDEBUG
  90. int num_face_collapses=0;
  91. #endif
  92. // Only keep uncollapsed faces
  93. {
  94. int ff = 0;
  95. // Loop over triangles
  96. for(int f = 0;f<rF.rows();f++)
  97. {
  98. bool collapsed = false;
  99. // Check if any indices are the same
  100. for(int i = 0;i<rF.cols();i++)
  101. {
  102. for(int j = i+1;j<rF.cols();j++)
  103. {
  104. if(rF(f,i)==rF(f,j))
  105. {
  106. collapsed = true;
  107. #ifndef NDEBUG
  108. num_face_collapses++;
  109. #endif
  110. break;
  111. }
  112. }
  113. }
  114. if(!collapsed)
  115. {
  116. FF.row(ff++) = rF.row(f);
  117. }
  118. }
  119. // Use conservative resize
  120. FF.conservativeResize(ff,FF.cols());
  121. }
  122. //cout<<"num_edge_collapses: "<<num_edge_collapses<<endl;
  123. //cout<<"num_face_collapses: "<<num_face_collapses<<endl;
  124. if(num_edge_collapses == 0)
  125. {
  126. // There must have been a "collapsed edge" in the input
  127. assert(num_face_collapses==0);
  128. // Base case
  129. return;
  130. }
  131. //// force base case
  132. //return;
  133. MatrixXi recFF = FF;
  134. return collapse_small_triangles(V,recFF,eps,FF);
  135. }
  136. #ifdef IGL_STATIC_LIBRARY
  137. // Explicit template instantiation
  138. template void igl::collapse_small_triangles<Eigen::MatrixXd, Eigen::MatrixXi, Eigen::MatrixXi> ( const Eigen::MatrixBase<Eigen::MatrixXd> &, const Eigen::MatrixBase<Eigen::MatrixXi> &, const double , Eigen::PlainObjectBase<Eigen::MatrixXi> & );
  139. #endif