mesh_boolean.cpp 14 KB

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