mesh_boolean.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "mesh_boolean.h"
  2. #include <igl/peel_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. //#define IGL_MESH_BOOLEAN_DEBUG
  8. template <
  9. typename DerivedVA,
  10. typename DerivedFA,
  11. typename DerivedVB,
  12. typename DerivedFB,
  13. typename DerivedVC,
  14. typename DerivedFC,
  15. typename DerivedJ>
  16. IGL_INLINE void igl::mesh_boolean(
  17. const Eigen::PlainObjectBase<DerivedVA > & VA,
  18. const Eigen::PlainObjectBase<DerivedFA > & FA,
  19. const Eigen::PlainObjectBase<DerivedVB > & VB,
  20. const Eigen::PlainObjectBase<DerivedFB > & FB,
  21. const MeshBooleanType & type,
  22. Eigen::PlainObjectBase<DerivedVC > & VC,
  23. Eigen::PlainObjectBase<DerivedFC > & FC,
  24. Eigen::PlainObjectBase<DerivedJ > & J)
  25. {
  26. const std::function<void(
  27. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  28. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  29. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  30. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  31. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  32. empty_fun;
  33. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  34. }
  35. template <
  36. typename DerivedVA,
  37. typename DerivedFA,
  38. typename DerivedVB,
  39. typename DerivedFB,
  40. typename DerivedVC,
  41. typename DerivedFC>
  42. IGL_INLINE void igl::mesh_boolean(
  43. const Eigen::PlainObjectBase<DerivedVA > & VA,
  44. const Eigen::PlainObjectBase<DerivedFA > & FA,
  45. const Eigen::PlainObjectBase<DerivedVB > & VB,
  46. const Eigen::PlainObjectBase<DerivedFB > & FB,
  47. const MeshBooleanType & type,
  48. Eigen::PlainObjectBase<DerivedVC > & VC,
  49. Eigen::PlainObjectBase<DerivedFC > & FC)
  50. {
  51. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  52. const std::function<void(
  53. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  54. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  55. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  56. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  57. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1>&)>
  58. empty_fun;
  59. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  60. }
  61. template <
  62. typename DerivedVA,
  63. typename DerivedFA,
  64. typename DerivedVB,
  65. typename DerivedFB,
  66. typename DerivedVC,
  67. typename DerivedFC,
  68. typename DerivedJ>
  69. IGL_INLINE void igl::mesh_boolean(
  70. const Eigen::PlainObjectBase<DerivedVA > & VA,
  71. const Eigen::PlainObjectBase<DerivedFA > & FA,
  72. const Eigen::PlainObjectBase<DerivedVB > & VB,
  73. const Eigen::PlainObjectBase<DerivedFB > & FB,
  74. const MeshBooleanType & type,
  75. const std::function<void(
  76. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  77. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  78. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  79. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  80. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  81. & resolve_fun,
  82. Eigen::PlainObjectBase<DerivedVC > & VC,
  83. Eigen::PlainObjectBase<DerivedFC > & FC,
  84. Eigen::PlainObjectBase<DerivedJ > & J)
  85. {
  86. using namespace Eigen;
  87. using namespace std;
  88. using namespace igl;
  89. MeshBooleanType eff_type = type;
  90. // Concatenate A and B into a single mesh
  91. typedef typename DerivedVC::Scalar Scalar;
  92. typedef typename DerivedFC::Scalar Index;
  93. typedef Matrix<Scalar,Dynamic,3> MatrixX3S;
  94. typedef Matrix<Index,Dynamic,3> MatrixX3I;
  95. typedef Matrix<Index,Dynamic,2> MatrixX2I;
  96. typedef Matrix<Index,Dynamic,1> VectorXI;
  97. typedef Matrix<typename DerivedJ::Scalar,Dynamic,1> VectorXJ;
  98. #ifdef IGL_MESH_BOOLEAN_DEBUG
  99. cout<<"mesh boolean..."<<endl;
  100. #endif
  101. MatrixX3S V(VA.rows()+VB.rows(),3);
  102. MatrixX3I F(FA.rows()+FB.rows(),3);
  103. V.block(0,0,VA.rows(),VA.cols()) = VA;
  104. V.block(VA.rows(),0,VB.rows(),VB.cols()) = VB;
  105. #ifdef IGL_MESH_BOOLEAN_DEBUG
  106. cout<<"prepare selfintersect input..."<<endl;
  107. #endif
  108. switch(type)
  109. {
  110. // Minus is implemented by flipping B and computing union
  111. case MESH_BOOLEAN_TYPE_MINUS:
  112. F.block(0,0,FA.rows(),FA.cols()) = FA.rowwise().reverse();
  113. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  114. //F.block(0,0,FA.rows(),3) = FA;
  115. //F.block(FA.rows(),0,FB.rows(),3) =
  116. // FB.rowwise().reverse().array()+VA.rows();
  117. eff_type = MESH_BOOLEAN_TYPE_INTERSECT;
  118. break;
  119. default:
  120. F.block(0,0,FA.rows(),FA.cols()) = FA;
  121. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  122. break;
  123. }
  124. // Resolve intersections (assumes A and B are solid)
  125. const auto & libigl_resolve = [](
  126. const MatrixX3S & V,
  127. const MatrixX3I & F,
  128. MatrixX3S & CV,
  129. MatrixX3I & CF,
  130. VectorXJ & J)
  131. {
  132. MatrixX3S SV;
  133. MatrixX3I SF;
  134. MatrixX2I SIF;
  135. VectorXI SIM,UIM;
  136. RemeshSelfIntersectionsParam params;
  137. remesh_self_intersections(V,F,params,SV,SF,SIF,J,SIM);
  138. for_each(SF.data(),SF.data()+SF.size(),[&SIM](int & a){a=SIM(a);});
  139. {
  140. remove_unreferenced(SV,SF,CV,CF,UIM);
  141. }
  142. };
  143. #ifdef IGL_MESH_BOOLEAN_DEBUG
  144. cout<<"resolve..."<<endl;
  145. #endif
  146. MatrixX3S CV;
  147. MatrixX3I CF;
  148. VectorXJ CJ;
  149. if(resolve_fun)
  150. {
  151. resolve_fun(V,F,CV,CF,CJ);
  152. }else
  153. {
  154. libigl_resolve(V,F,CV,CF,CJ);
  155. }
  156. if(type == MESH_BOOLEAN_TYPE_RESOLVE)
  157. {
  158. FC = CF;
  159. VC = CV;
  160. J = CJ;
  161. return;
  162. }
  163. #ifdef IGL_MESH_BOOLEAN_DEBUG
  164. cout<<"peel..."<<endl;
  165. #endif
  166. Matrix<bool,Dynamic,1> from_A(CF.rows());
  167. // peel layers keeping track of odd and even flips
  168. Matrix<bool,Dynamic,1> odd;
  169. Matrix<bool,Dynamic,1> flip;
  170. peel_outer_hull_layers(CV,CF,odd,flip);
  171. #ifdef IGL_MESH_BOOLEAN_DEBUG
  172. cout<<"categorize..."<<endl;
  173. #endif
  174. const Index m = CF.rows();
  175. // Faces of output vG[i] = j means ith face of output should be jth face in F
  176. std::vector<Index> vG;
  177. // Whether faces of output should be flipped, Gflip[i] = true means ith face
  178. // of output should be F.row(vG[i]).reverse() rather than F.row(vG[i])
  179. std::vector<bool> Gflip;
  180. for(Index f = 0;f<m;f++)
  181. {
  182. switch(eff_type)
  183. {
  184. case MESH_BOOLEAN_TYPE_XOR:
  185. case MESH_BOOLEAN_TYPE_UNION:
  186. if((odd(f)&&!flip(f))||(!odd(f)&&flip(f)))
  187. {
  188. vG.push_back(f);
  189. Gflip.push_back(false);
  190. }else if(eff_type == MESH_BOOLEAN_TYPE_XOR)
  191. {
  192. vG.push_back(f);
  193. Gflip.push_back(true);
  194. }
  195. break;
  196. case MESH_BOOLEAN_TYPE_INTERSECT:
  197. if((!odd(f) && !flip(f)) || (odd(f) && flip(f)))
  198. {
  199. vG.push_back(f);
  200. Gflip.push_back(type == MESH_BOOLEAN_TYPE_MINUS);
  201. }
  202. break;
  203. default:
  204. assert(false && "Unknown type");
  205. return;
  206. }
  207. }
  208. const Index gm = vG.size();
  209. MatrixX3I G(gm,3);
  210. VectorXi GJ(gm,1);
  211. for(Index g = 0;g<gm;g++)
  212. {
  213. G.row(g) = Gflip[g] ? CF.row(vG[g]).reverse().eval() : CF.row(vG[g]);
  214. GJ(g) = CJ(vG[g]);
  215. }
  216. #ifdef IGL_MESH_BOOLEAN_DEBUG
  217. cout<<"clean..."<<endl;
  218. #endif
  219. // remove duplicates: cancel out in all cases? assertion in others?
  220. {
  221. MatrixXi oldG = G;
  222. VectorXi IA,_1;
  223. unique_simplices(oldG,G,IA,_1);
  224. assert(IA.rows() == G.rows());
  225. J.resize(IA.rows(),1);
  226. for(size_t j = 0;j<(size_t)J.size();j++)
  227. {
  228. J(j) = GJ(IA(j));
  229. }
  230. }
  231. // remove unreferenced vertices
  232. VectorXi newIM;
  233. remove_unreferenced(CV,G,VC,FC,newIM);
  234. //cerr<<"warning not removing unref"<<endl;
  235. //VC = CV;
  236. //FC = G;
  237. }
  238. #ifdef IGL_STATIC_LIBRARY
  239. // Explicit template specialization
  240. 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&, igl::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  241. 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::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&, igl::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  242. #endif