collapse_small_triangles.cpp 3.2 KB

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