mesh_boolean.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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::Epeck 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. Scalar c;
  176. assign_scalar(val,c);
  177. return c;
  178. });
  179. }
  180. if(type == MESH_BOOLEAN_TYPE_RESOLVE)
  181. {
  182. FC = CF;
  183. VC = CV;
  184. J = CJ;
  185. return;
  186. }
  187. #ifdef IGL_MESH_BOOLEAN_DEBUG
  188. cout<<"peel..."<<endl;
  189. #endif
  190. Matrix<bool,Dynamic,1> from_A(CF.rows());
  191. // peel layers keeping track of odd and even flips
  192. VectorXi I;
  193. Matrix<bool,Dynamic,1> flip;
  194. peel_outer_hull_layers(EV,CF,I,flip);
  195. // 0 is "first" iteration, so it's odd
  196. Array<bool,Dynamic,1> odd = igl::mod(I,2).array()==0;
  197. #ifdef IGL_MESH_BOOLEAN_DEBUG
  198. cout<<"categorize..."<<endl;
  199. #endif
  200. const Index m = CF.rows();
  201. // Faces of output vG[i] = j means ith face of output should be jth face in F
  202. std::vector<Index> vG;
  203. // Whether faces of output should be flipped, Gflip[i] = true means ith face
  204. // of output should be F.row(vG[i]).reverse() rather than F.row(vG[i])
  205. std::vector<bool> Gflip;
  206. for(Index f = 0;f<m;f++)
  207. {
  208. switch(eff_type)
  209. {
  210. case MESH_BOOLEAN_TYPE_XOR:
  211. case MESH_BOOLEAN_TYPE_UNION:
  212. if((odd(f)&&!flip(f))||(!odd(f)&&flip(f)))
  213. {
  214. vG.push_back(f);
  215. Gflip.push_back(false);
  216. }else if(eff_type == MESH_BOOLEAN_TYPE_XOR)
  217. {
  218. vG.push_back(f);
  219. Gflip.push_back(true);
  220. }
  221. break;
  222. case MESH_BOOLEAN_TYPE_INTERSECT:
  223. if((!odd(f) && !flip(f)) || (odd(f) && flip(f)))
  224. {
  225. vG.push_back(f);
  226. Gflip.push_back(type == MESH_BOOLEAN_TYPE_MINUS);
  227. }
  228. break;
  229. default:
  230. assert(false && "Unknown type");
  231. return;
  232. }
  233. }
  234. const Index gm = vG.size();
  235. MatrixX3I G(gm,3);
  236. VectorXi GJ(gm,1);
  237. for(Index g = 0;g<gm;g++)
  238. {
  239. G.row(g) = Gflip[g] ? CF.row(vG[g]).reverse().eval() : CF.row(vG[g]);
  240. GJ(g) = CJ(vG[g]);
  241. }
  242. #ifdef IGL_MESH_BOOLEAN_DEBUG
  243. {
  244. MatrixXi O;
  245. boundary_facets(FC,O);
  246. cout<<"# boundary: "<<O.rows()<<endl;
  247. }
  248. cout<<"# exterior: "<<exterior_edges(FC).rows()<<endl;
  249. #endif
  250. #ifdef IGL_MESH_BOOLEAN_DEBUG
  251. cout<<"clean..."<<endl;
  252. #endif
  253. // Deal with duplicate faces
  254. {
  255. VectorXi IA,IC;
  256. MatrixX3I uG;
  257. unique_simplices(G,uG,IA,IC);
  258. assert(IA.rows() == uG.rows());
  259. // faces ontop of each unique face
  260. vector<vector<Index> > uG2G(uG.rows());
  261. // signed counts
  262. VectorXi counts = VectorXi::Zero(uG.rows());
  263. VectorXi ucounts = VectorXi::Zero(uG.rows());
  264. // loop over all faces
  265. for(Index g = 0;g<gm;g++)
  266. {
  267. const int ug = IC(g);
  268. assert(ug < uG2G.size());
  269. uG2G[ug].push_back(g);
  270. // is uG(g,:) just a rotated version of G(g,:) ?
  271. const bool consistent =
  272. (G(g,0) == uG(ug,0) && G(g,1) == uG(ug,1) && G(g,2) == uG(ug,2)) ||
  273. (G(g,0) == uG(ug,1) && G(g,1) == uG(ug,2) && G(g,2) == uG(ug,0)) ||
  274. (G(g,0) == uG(ug,2) && G(g,1) == uG(ug,0) && G(g,2) == uG(ug,1));
  275. counts(ug) += consistent ? 1 : -1;
  276. ucounts(ug)++;
  277. }
  278. MatrixX3I oldG = G;
  279. // Faces of output vG[i] = j means ith face of output should be jth face in
  280. // oldG
  281. vG.clear();
  282. for(size_t ug = 0;ug < uG2G.size();ug++)
  283. {
  284. // if signed occurrences is zero or ±two then keep none
  285. // else if signed occurrences is ±one then keep just one facet
  286. switch(abs(counts(ug)))
  287. {
  288. case 1:
  289. assert(uG2G[ug].size() > 0);
  290. vG.push_back(uG2G[ug][0]);
  291. #ifdef IGL_MESH_BOOLEAN_DEBUG
  292. if(abs(ucounts(ug)) != 1)
  293. {
  294. cout<<"count,ucount of "<<counts(ug)<<","<<ucounts(ug)<<endl;
  295. }
  296. #endif
  297. break;
  298. case 0:
  299. #ifdef IGL_MESH_BOOLEAN_DEBUG
  300. cout<<"Skipping "<<uG2G[ug].size()<<" facets..."<<endl;
  301. if(abs(ucounts(ug)) != 0)
  302. {
  303. cout<<"count,ucount of "<<counts(ug)<<","<<ucounts(ug)<<endl;
  304. }
  305. #endif
  306. break;
  307. default:
  308. #ifdef IGL_MESH_BOOLEAN_DEBUG
  309. cout<<"Didn't expect to be here."<<endl;
  310. #endif
  311. assert(false && "Shouldn't count be -1/0/1 ?");
  312. }
  313. }
  314. G.resize(vG.size(),3);
  315. J.resize(vG.size());
  316. for(size_t g = 0;g<vG.size();g++)
  317. {
  318. G.row(g) = oldG.row(vG[g]);
  319. J(g) = GJ(vG[g]);
  320. }
  321. }
  322. // remove unreferenced vertices
  323. VectorXi newIM;
  324. remove_unreferenced(CV,G,VC,FC,newIM);
  325. //cerr<<"warning not removing unref"<<endl;
  326. //VC = CV;
  327. //FC = G;
  328. #ifdef IGL_MESH_BOOLEAN_DEBUG
  329. {
  330. MatrixXi O;
  331. boundary_facets(FC,O);
  332. cout<<"# boundary: "<<O.rows()<<endl;
  333. }
  334. cout<<"# exterior: "<<exterior_edges(FC).rows()<<endl;
  335. #endif
  336. }
  337. #ifdef IGL_STATIC_LIBRARY
  338. // This is a hack to discuss. I'm not sure why this _doesn't_ create
  339. // duplicate symbols.
  340. #include <igl/remove_unreferenced.cpp>
  341. 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> >&);
  342. #include <igl/cgal/peel_outer_hull_layers.cpp>
  343. template unsigned long
  344. 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> >&);
  345. #include <igl/cgal/outer_hull.cpp>
  346. 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> >&);
  347. // Explicit template specialization
  348. 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> >&);
  349. 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> >&);
  350. #endif