mesh_boolean.cpp 14 KB

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