mesh_boolean.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "mesh_boolean.h"
  2. #include <igl/per_face_normals.h>
  3. #include <igl/cgal/peel_outer_hull_layers.h>
  4. #include <igl/cgal/remesh_self_intersections.h>
  5. #include <igl/remove_unreferenced.h>
  6. #include <igl/unique_simplices.h>
  7. #include <iostream>
  8. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  9. //#define IGL_MESH_BOOLEAN_DEBUG
  10. template <
  11. typename DerivedVA,
  12. typename DerivedFA,
  13. typename DerivedVB,
  14. typename DerivedFB,
  15. typename DerivedVC,
  16. typename DerivedFC,
  17. typename DerivedJ>
  18. IGL_INLINE void igl::boolean::mesh_boolean(
  19. const Eigen::PlainObjectBase<DerivedVA > & VA,
  20. const Eigen::PlainObjectBase<DerivedFA > & FA,
  21. const Eigen::PlainObjectBase<DerivedVB > & VB,
  22. const Eigen::PlainObjectBase<DerivedFB > & FB,
  23. const MeshBooleanType & type,
  24. Eigen::PlainObjectBase<DerivedVC > & VC,
  25. Eigen::PlainObjectBase<DerivedFC > & FC,
  26. Eigen::PlainObjectBase<DerivedJ > & J)
  27. {
  28. const std::function<void(
  29. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  30. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  31. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  32. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  33. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  34. empty_fun;
  35. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  36. }
  37. template <
  38. typename DerivedVA,
  39. typename DerivedFA,
  40. typename DerivedVB,
  41. typename DerivedFB,
  42. typename DerivedVC,
  43. typename DerivedFC>
  44. IGL_INLINE void igl::boolean::mesh_boolean(
  45. const Eigen::PlainObjectBase<DerivedVA > & VA,
  46. const Eigen::PlainObjectBase<DerivedFA > & FA,
  47. const Eigen::PlainObjectBase<DerivedVB > & VB,
  48. const Eigen::PlainObjectBase<DerivedFB > & FB,
  49. const MeshBooleanType & type,
  50. Eigen::PlainObjectBase<DerivedVC > & VC,
  51. Eigen::PlainObjectBase<DerivedFC > & FC)
  52. {
  53. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  54. const std::function<void(
  55. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  56. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  57. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  58. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  59. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1>&)>
  60. empty_fun;
  61. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  62. }
  63. template <
  64. typename DerivedVA,
  65. typename DerivedFA,
  66. typename DerivedVB,
  67. typename DerivedFB,
  68. typename DerivedVC,
  69. typename DerivedFC,
  70. typename DerivedJ>
  71. IGL_INLINE void igl::boolean::mesh_boolean(
  72. const Eigen::PlainObjectBase<DerivedVA > & VA,
  73. const Eigen::PlainObjectBase<DerivedFA > & FA,
  74. const Eigen::PlainObjectBase<DerivedVB > & VB,
  75. const Eigen::PlainObjectBase<DerivedFB > & FB,
  76. const MeshBooleanType & type,
  77. const std::function<void(
  78. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  79. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  80. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  81. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  82. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  83. & resolve_fun,
  84. Eigen::PlainObjectBase<DerivedVC > & VC,
  85. Eigen::PlainObjectBase<DerivedFC > & FC,
  86. Eigen::PlainObjectBase<DerivedJ > & J)
  87. {
  88. using namespace Eigen;
  89. using namespace std;
  90. using namespace igl;
  91. using namespace igl::cgal;
  92. MeshBooleanType eff_type = type;
  93. // Concatenate A and B into a single mesh
  94. typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
  95. typedef Kernel::FT ExactScalar;
  96. typedef typename DerivedVC::Scalar Scalar;
  97. typedef typename DerivedFC::Scalar Index;
  98. typedef Matrix<Scalar,Dynamic,3> MatrixX3S;
  99. typedef Matrix<ExactScalar,Dynamic,3> MatrixX3ES;
  100. typedef Matrix<Index,Dynamic,3> MatrixX3I;
  101. typedef Matrix<Index,Dynamic,2> MatrixX2I;
  102. typedef Matrix<Index,Dynamic,1> VectorXI;
  103. typedef Matrix<typename DerivedJ::Scalar,Dynamic,1> VectorXJ;
  104. #ifdef IGL_MESH_BOOLEAN_DEBUG
  105. cout<<"mesh boolean..."<<endl;
  106. #endif
  107. MatrixX3S V(VA.rows()+VB.rows(),3);
  108. MatrixX3I F(FA.rows()+FB.rows(),3);
  109. V.block(0,0,VA.rows(),VA.cols()) = VA;
  110. V.block(VA.rows(),0,VB.rows(),VB.cols()) = VB;
  111. #ifdef IGL_MESH_BOOLEAN_DEBUG
  112. cout<<"prepare selfintersect input..."<<endl;
  113. #endif
  114. switch(type)
  115. {
  116. // Minus is implemented by flipping B and computing union
  117. case MESH_BOOLEAN_TYPE_MINUS:
  118. F.block(0,0,FA.rows(),FA.cols()) = FA.rowwise().reverse();
  119. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  120. //F.block(0,0,FA.rows(),3) = FA;
  121. //F.block(FA.rows(),0,FB.rows(),3) =
  122. // FB.rowwise().reverse().array()+VA.rows();
  123. eff_type = MESH_BOOLEAN_TYPE_INTERSECT;
  124. break;
  125. default:
  126. F.block(0,0,FA.rows(),FA.cols()) = FA;
  127. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  128. break;
  129. }
  130. // Resolve intersections (assumes A and B are solid)
  131. const auto & libigl_resolve = [](
  132. const MatrixX3S & V,
  133. const MatrixX3I & F,
  134. MatrixX3ES & CV,
  135. MatrixX3I & CF,
  136. VectorXJ & J)
  137. {
  138. MatrixX3ES SV;
  139. MatrixX3I SF;
  140. MatrixX2I SIF;
  141. VectorXI SIM,UIM;
  142. RemeshSelfIntersectionsParam params;
  143. remesh_self_intersections(V,F,params,SV,SF,SIF,J,SIM);
  144. for_each(SF.data(),SF.data()+SF.size(),[&SIM](int & a){a=SIM(a);});
  145. {
  146. remove_unreferenced(SV,SF,CV,CF,UIM);
  147. }
  148. };
  149. #ifdef IGL_MESH_BOOLEAN_DEBUG
  150. cout<<"resolve..."<<endl;
  151. #endif
  152. MatrixX3S CV;
  153. MatrixX3ES EV;
  154. MatrixX3I CF;
  155. VectorXJ CJ;
  156. if(resolve_fun)
  157. {
  158. resolve_fun(V,F,CV,CF,CJ);
  159. }else
  160. {
  161. libigl_resolve(V,F,EV,CF,CJ);
  162. CV.resize(EV.rows(), EV.cols());
  163. std::transform(EV.data(), EV.data() + EV.rows()*EV.cols(),
  164. CV.data(), [&](ExactScalar val) {
  165. return CGAL::to_double(val);
  166. });
  167. }
  168. if(type == MESH_BOOLEAN_TYPE_RESOLVE)
  169. {
  170. FC = CF;
  171. VC = CV;
  172. J = CJ;
  173. return;
  174. }
  175. MatrixX3S N,CN;
  176. per_face_normals_stable(V,F,N);
  177. CN.resize(CF.rows(),3);
  178. for(size_t f = 0;f<(size_t)CN.rows();f++)
  179. {
  180. CN.row(f) = N.row(CJ(f));
  181. }
  182. #ifdef IGL_MESH_BOOLEAN_DEBUG
  183. cout<<"peel..."<<endl;
  184. #endif
  185. Matrix<bool,Dynamic,1> from_A(CF.rows());
  186. // peel layers keeping track of odd and even flips
  187. Matrix<bool,Dynamic,1> odd;
  188. Matrix<bool,Dynamic,1> flip;
  189. peel_outer_hull_layers(EV,CF,CN,odd,flip);
  190. #ifdef IGL_MESH_BOOLEAN_DEBUG
  191. cout<<"categorize..."<<endl;
  192. #endif
  193. const Index m = CF.rows();
  194. // Faces of output vG[i] = j means ith face of output should be jth face in F
  195. std::vector<Index> vG;
  196. // Whether faces of output should be flipped, Gflip[i] = true means ith face
  197. // of output should be F.row(vG[i]).reverse() rather than F.row(vG[i])
  198. std::vector<bool> Gflip;
  199. for(Index f = 0;f<m;f++)
  200. {
  201. switch(eff_type)
  202. {
  203. case MESH_BOOLEAN_TYPE_XOR:
  204. case MESH_BOOLEAN_TYPE_UNION:
  205. if((odd(f)&&!flip(f))||(!odd(f)&&flip(f)))
  206. {
  207. vG.push_back(f);
  208. Gflip.push_back(false);
  209. }else if(eff_type == MESH_BOOLEAN_TYPE_XOR)
  210. {
  211. vG.push_back(f);
  212. Gflip.push_back(true);
  213. }
  214. break;
  215. case MESH_BOOLEAN_TYPE_INTERSECT:
  216. if((!odd(f) && !flip(f)) || (odd(f) && flip(f)))
  217. {
  218. vG.push_back(f);
  219. Gflip.push_back(type == MESH_BOOLEAN_TYPE_MINUS);
  220. }
  221. break;
  222. default:
  223. assert(false && "Unknown type");
  224. return;
  225. }
  226. }
  227. const Index gm = vG.size();
  228. MatrixX3I G(gm,3);
  229. VectorXi GJ(gm,1);
  230. for(Index g = 0;g<gm;g++)
  231. {
  232. G.row(g) = Gflip[g] ? CF.row(vG[g]).reverse().eval() : CF.row(vG[g]);
  233. GJ(g) = CJ(vG[g]);
  234. }
  235. #ifdef IGL_MESH_BOOLEAN_DEBUG
  236. cout<<"clean..."<<endl;
  237. #endif
  238. // Deal with duplicate faces
  239. {
  240. VectorXi IA,IC;
  241. MatrixX3I uG;
  242. unique_simplices(G,uG,IA,IC);
  243. assert(IA.rows() == uG.rows());
  244. // faces ontop of each unique face
  245. vector<vector<Index> > uG2G(uG.rows());
  246. // signed counts
  247. VectorXi counts = VectorXi::Zero(uG.rows());
  248. // loop over all faces
  249. for(Index g = 0;g<gm;g++)
  250. {
  251. const int ug = IC(g);
  252. assert(ug < uG2G.size());
  253. uG2G[ug].push_back(g);
  254. // is uG(g,:) just a rotated version of G(g,:) ?
  255. const bool consistent =
  256. (G(g,0) == uG(ug,0) && G(g,1) == uG(ug,1) && G(g,2) == uG(ug,2)) ||
  257. (G(g,0) == uG(ug,1) && G(g,1) == uG(ug,2) && G(g,2) == uG(ug,0)) ||
  258. (G(g,0) == uG(ug,2) && G(g,1) == uG(ug,0) && G(g,2) == uG(ug,1));
  259. counts(ug) += consistent ? 1 : -1;
  260. }
  261. MatrixX3I oldG = G;
  262. // Faces of output vG[i] = j means ith face of output should be jth face in
  263. // oldG
  264. vG.clear();
  265. for(size_t ug = 0;ug < uG2G.size();ug++)
  266. {
  267. // if signed occurrences is zero or ±two then keep none
  268. // else if signed occurrences is ±one then keep just one facet
  269. if(abs(counts(ug)) == 1)
  270. {
  271. assert(uG2G.size() > 0);
  272. vG.push_back(uG2G[ug][0]);
  273. }
  274. #ifdef IGL_MESH_BOOLEAN_DEBUG
  275. else
  276. {
  277. cout<<"Skipping "<<uG2G.size()<<" facets..."<<endl;
  278. }
  279. #endif
  280. }
  281. G.resize(vG.size(),3);
  282. J.resize(vG.size());
  283. for(size_t g = 0;g<vG.size();g++)
  284. {
  285. G.row(g) = oldG.row(vG[g]);
  286. J(g) = GJ(vG[g]);
  287. }
  288. }
  289. // remove unreferenced vertices
  290. VectorXi newIM;
  291. remove_unreferenced(CV,G,VC,FC,newIM);
  292. //cerr<<"warning not removing unref"<<endl;
  293. //VC = CV;
  294. //FC = G;
  295. }
  296. #ifdef IGL_STATIC_LIBRARY
  297. // This is a hack to discuss
  298. #include <igl/remove_unreferenced.cpp>
  299. template void igl::remove_unreferenced<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  300. #include <igl/cgal/peel_outer_hull_layers.cpp>
  301. template unsigned long igl::cgal::peel_outer_hull_layers<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  302. #include <igl/cgal/outer_hull.cpp>
  303. template void igl::cgal::outer_hull<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  304. // Explicit template specialization
  305. template void igl::boolean::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::boolean::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  306. template void igl::boolean::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::boolean::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> >&);
  307. #endif