mesh_boolean.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // 0 is "first" iteration, so it's odd
  199. Array<bool,Dynamic,1> odd = igl::mod(I,2).array()==0;
  200. #ifdef IGL_MESH_BOOLEAN_DEBUG
  201. cout<<"categorize..."<<endl;
  202. #endif
  203. const Index m = CF.rows();
  204. // Faces of output vG[i] = j means ith face of output should be jth face in F
  205. std::vector<Index> vG;
  206. // Whether faces of output should be flipped, Gflip[i] = true means ith face
  207. // of output should be F.row(vG[i]).reverse() rather than F.row(vG[i])
  208. std::vector<bool> Gflip;
  209. for(Index f = 0;f<m;f++)
  210. {
  211. switch(eff_type)
  212. {
  213. case MESH_BOOLEAN_TYPE_XOR:
  214. case MESH_BOOLEAN_TYPE_UNION:
  215. if((odd(f)&&!flip(f))||(!odd(f)&&flip(f)))
  216. {
  217. vG.push_back(f);
  218. Gflip.push_back(false);
  219. }else if(eff_type == MESH_BOOLEAN_TYPE_XOR)
  220. {
  221. vG.push_back(f);
  222. Gflip.push_back(true);
  223. }
  224. break;
  225. case MESH_BOOLEAN_TYPE_INTERSECT:
  226. if((!odd(f) && !flip(f)) || (odd(f) && flip(f)))
  227. {
  228. vG.push_back(f);
  229. Gflip.push_back(type == MESH_BOOLEAN_TYPE_MINUS);
  230. }
  231. break;
  232. default:
  233. assert(false && "Unknown type");
  234. return;
  235. }
  236. }
  237. const Index gm = vG.size();
  238. MatrixX3I G(gm,3);
  239. VectorXi GJ(gm,1);
  240. for(Index g = 0;g<gm;g++)
  241. {
  242. G.row(g) = Gflip[g] ? CF.row(vG[g]).reverse().eval() : CF.row(vG[g]);
  243. GJ(g) = CJ(vG[g]);
  244. }
  245. #ifdef IGL_MESH_BOOLEAN_DEBUG
  246. cout<<"clean..."<<endl;
  247. #endif
  248. // Deal with duplicate faces
  249. {
  250. VectorXi IA,IC;
  251. MatrixX3I uG;
  252. unique_simplices(G,uG,IA,IC);
  253. assert(IA.rows() == uG.rows());
  254. // faces ontop of each unique face
  255. vector<vector<Index> > uG2G(uG.rows());
  256. // signed counts
  257. VectorXi counts = VectorXi::Zero(uG.rows());
  258. // loop over all faces
  259. for(Index g = 0;g<gm;g++)
  260. {
  261. const int ug = IC(g);
  262. assert(ug < uG2G.size());
  263. uG2G[ug].push_back(g);
  264. // is uG(g,:) just a rotated version of G(g,:) ?
  265. const bool consistent =
  266. (G(g,0) == uG(ug,0) && G(g,1) == uG(ug,1) && G(g,2) == uG(ug,2)) ||
  267. (G(g,0) == uG(ug,1) && G(g,1) == uG(ug,2) && G(g,2) == uG(ug,0)) ||
  268. (G(g,0) == uG(ug,2) && G(g,1) == uG(ug,0) && G(g,2) == uG(ug,1));
  269. counts(ug) += consistent ? 1 : -1;
  270. }
  271. MatrixX3I oldG = G;
  272. // Faces of output vG[i] = j means ith face of output should be jth face in
  273. // oldG
  274. vG.clear();
  275. for(size_t ug = 0;ug < uG2G.size();ug++)
  276. {
  277. // if signed occurrences is zero or ±two then keep none
  278. // else if signed occurrences is ±one then keep just one facet
  279. if(abs(counts(ug)) == 1)
  280. {
  281. assert(uG2G.size() > 0);
  282. vG.push_back(uG2G[ug][0]);
  283. }
  284. #ifdef IGL_MESH_BOOLEAN_DEBUG
  285. else
  286. {
  287. cout<<"Skipping "<<uG2G.size()<<" facets..."<<endl;
  288. }
  289. #endif
  290. }
  291. G.resize(vG.size(),3);
  292. J.resize(vG.size());
  293. for(size_t g = 0;g<vG.size();g++)
  294. {
  295. G.row(g) = oldG.row(vG[g]);
  296. J(g) = GJ(vG[g]);
  297. }
  298. }
  299. // remove unreferenced vertices
  300. VectorXi newIM;
  301. remove_unreferenced(CV,G,VC,FC,newIM);
  302. //cerr<<"warning not removing unref"<<endl;
  303. //VC = CV;
  304. //FC = G;
  305. }
  306. #ifdef IGL_STATIC_LIBRARY
  307. // This is a hack to discuss. I'm not sure why this _doesn't_ create
  308. // duplicate symbols.
  309. #include <igl/remove_unreferenced.cpp>
  310. 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> >&);
  311. #include <igl/cgal/peel_outer_hull_layers.cpp>
  312. 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> >&);
  313. #include <igl/cgal/outer_hull.cpp>
  314. 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> >&);
  315. // Explicit template specialization
  316. 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> >&);
  317. 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> >&);
  318. #endif