collapse_small_triangles.cpp 3.6 KB

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