mesh_boolean.cpp 14 KB

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