mesh_boolean.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "mesh_boolean.h"
  2. #include <igl/peal_outer_hull_layers.h>
  3. #include <igl/cgal/remesh_self_intersections.h>
  4. #include <igl/remove_unreferenced.h>
  5. #include <igl/unique_simplices.h>
  6. #include <iostream>
  7. template <
  8. typename DerivedVA,
  9. typename DerivedFA,
  10. typename DerivedVB,
  11. typename DerivedFB,
  12. typename DerivedVC,
  13. typename DerivedFC>
  14. IGL_INLINE void igl::mesh_boolean(
  15. const Eigen::PlainObjectBase<DerivedVA > & VA,
  16. const Eigen::PlainObjectBase<DerivedFA > & FA,
  17. const Eigen::PlainObjectBase<DerivedVB > & VB,
  18. const Eigen::PlainObjectBase<DerivedFB > & FB,
  19. const MeshBooleanType & type,
  20. Eigen::PlainObjectBase<DerivedVC > & VC,
  21. Eigen::PlainObjectBase<DerivedFC > & FC)
  22. {
  23. const std::function<void(
  24. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  25. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  26. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  27. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&)>
  28. empty_fun;
  29. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC);
  30. }
  31. template <
  32. typename DerivedVA,
  33. typename DerivedFA,
  34. typename DerivedVB,
  35. typename DerivedFB,
  36. typename DerivedVC,
  37. typename DerivedFC>
  38. IGL_INLINE void igl::mesh_boolean(
  39. const Eigen::PlainObjectBase<DerivedVA > & VA,
  40. const Eigen::PlainObjectBase<DerivedFA > & FA,
  41. const Eigen::PlainObjectBase<DerivedVB > & VB,
  42. const Eigen::PlainObjectBase<DerivedFB > & FB,
  43. const MeshBooleanType & type,
  44. const std::function<void(
  45. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  46. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  47. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  48. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&)>
  49. & resolve_fun,
  50. Eigen::PlainObjectBase<DerivedVC > & VC,
  51. Eigen::PlainObjectBase<DerivedFC > & FC)
  52. {
  53. using namespace Eigen;
  54. using namespace std;
  55. using namespace igl;
  56. MeshBooleanType eff_type = type;
  57. // Concatenate A and B into a single mesh
  58. typedef typename DerivedVC::Scalar Scalar;
  59. typedef typename DerivedFC::Scalar Index;
  60. typedef Matrix<Scalar,Dynamic,3> MatrixX3S;
  61. typedef Matrix<Index,Dynamic,3> MatrixX3I;
  62. typedef Matrix<Index,Dynamic,2> MatrixX2I;
  63. typedef Matrix<Index,Dynamic,1> VectorXI;
  64. MatrixX3S V(VA.rows()+VB.rows(),3);
  65. MatrixX3I F(FA.rows()+FB.rows(),3);
  66. V.block(0,0,VA.rows(),VA.cols()) = VA;
  67. V.block(VA.rows(),0,VB.rows(),VB.cols()) = VB;
  68. switch(type)
  69. {
  70. // Minus is implemented by flipping B and computing union
  71. case MESH_BOOLEAN_TYPE_MINUS:
  72. F.block(0,0,FA.rows(),FA.cols()) = FA.rowwise().reverse();
  73. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  74. //F.block(0,0,FA.rows(),3) = FA;
  75. //F.block(FA.rows(),0,FB.rows(),3) =
  76. // FB.rowwise().reverse().array()+VA.rows();
  77. eff_type = MESH_BOOLEAN_TYPE_INTERSECT;
  78. break;
  79. default:
  80. F.block(0,0,FA.rows(),FA.cols()) = FA;
  81. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  82. break;
  83. }
  84. // Resolve intersections (assumes A and B are solid)
  85. const auto & libigl_resolve = [](
  86. const MatrixX3S & V,
  87. const MatrixX3I & F,
  88. MatrixX3S & CV,
  89. MatrixX3I & CF)
  90. {
  91. MatrixX3S SV;
  92. MatrixX3I SF;
  93. MatrixX2I SIF;
  94. VectorXI SJ,SIM,UIM;
  95. RemeshSelfIntersectionsParam params;
  96. remesh_self_intersections(V,F,params,SV,SF,SIF,SJ,SIM);
  97. for_each(SF.data(),SF.data()+SF.size(),[&SIM](int & a){a=SIM(a);});
  98. {
  99. remove_unreferenced(SV,SF,CV,CF,UIM);
  100. }
  101. };
  102. MatrixX3S CV;
  103. MatrixX3I CF;
  104. if(resolve_fun)
  105. {
  106. resolve_fun(V,F,CV,CF);
  107. }else
  108. {
  109. libigl_resolve(V,F,CV,CF);
  110. }
  111. if(type == MESH_BOOLEAN_TYPE_RESOLVE)
  112. {
  113. FC = CF;
  114. VC = CV;
  115. return;
  116. }
  117. Matrix<bool,Dynamic,1> from_A(CF.rows());
  118. // Peal layers keeping track of odd and even flips
  119. Matrix<bool,Dynamic,1> odd;
  120. Matrix<bool,Dynamic,1> flip;
  121. peal_outer_hull_layers(CV,CF,odd,flip);
  122. const Index m = CF.rows();
  123. // Faces of output vG[i] = j means ith face of output should be jth face in F
  124. std::vector<Index> vG;
  125. // Whether faces of output should be flipped, Gflip[i] = true means ith face
  126. // of output should be F.row(vG[i]).reverse() rather than F.row(vG[i])
  127. std::vector<bool> Gflip;
  128. for(Index f = 0;f<m;f++)
  129. {
  130. switch(eff_type)
  131. {
  132. case MESH_BOOLEAN_TYPE_XOR:
  133. case MESH_BOOLEAN_TYPE_UNION:
  134. if((odd(f)&&!flip(f))||(!odd(f)&&flip(f)))
  135. {
  136. vG.push_back(f);
  137. Gflip.push_back(false);
  138. }else if(eff_type == MESH_BOOLEAN_TYPE_XOR)
  139. {
  140. vG.push_back(f);
  141. Gflip.push_back(true);
  142. }
  143. break;
  144. case MESH_BOOLEAN_TYPE_INTERSECT:
  145. if((!odd(f) && !flip(f)) || (odd(f) && flip(f)))
  146. {
  147. vG.push_back(f);
  148. Gflip.push_back(false);
  149. }
  150. break;
  151. default:
  152. assert(false && "Unknown type");
  153. return;
  154. }
  155. }
  156. const Index gm = vG.size();
  157. MatrixX3I G(gm,3);
  158. for(Index g = 0;g<gm;g++)
  159. {
  160. G.row(g) = Gflip[g] ? CF.row(vG[g]).reverse().eval() : CF.row(vG[g]);
  161. }
  162. // remove duplicates: cancel out in all cases? assertion in others?
  163. {
  164. MatrixXi oldG = G;
  165. unique_simplices(oldG,G);
  166. }
  167. // remove unreferenced vertices
  168. VectorXi newIM;
  169. remove_unreferenced(CV,G,VC,FC,newIM);
  170. //cerr<<"warning not removing unref"<<endl;
  171. //VC = CV;
  172. //FC = G;
  173. }
  174. #ifdef IGL_STATIC_LIBRARY
  175. // Explicit template specialization
  176. template void igl::mesh_boolean<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::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  177. #endif