mesh_boolean.cpp 13 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/unique_simplices.h>
  14. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  15. #include <iostream>
  16. //#define IGL_MESH_BOOLEAN_DEBUG
  17. template <
  18. typename DerivedVA,
  19. typename DerivedFA,
  20. typename DerivedVB,
  21. typename DerivedFB,
  22. typename DerivedVC,
  23. typename DerivedFC,
  24. typename DerivedJ>
  25. IGL_INLINE void igl::boolean::mesh_boolean(
  26. const Eigen::PlainObjectBase<DerivedVA > & VA,
  27. const Eigen::PlainObjectBase<DerivedFA > & FA,
  28. const Eigen::PlainObjectBase<DerivedVB > & VB,
  29. const Eigen::PlainObjectBase<DerivedFB > & FB,
  30. const MeshBooleanType & type,
  31. Eigen::PlainObjectBase<DerivedVC > & VC,
  32. Eigen::PlainObjectBase<DerivedFC > & FC,
  33. Eigen::PlainObjectBase<DerivedJ > & J)
  34. {
  35. const std::function<void(
  36. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  37. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  38. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  39. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  40. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  41. empty_fun;
  42. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  43. }
  44. template <
  45. typename DerivedVA,
  46. typename DerivedFA,
  47. typename DerivedVB,
  48. typename DerivedFB,
  49. typename DerivedVC,
  50. typename DerivedFC>
  51. IGL_INLINE void igl::boolean::mesh_boolean(
  52. const Eigen::PlainObjectBase<DerivedVA > & VA,
  53. const Eigen::PlainObjectBase<DerivedFA > & FA,
  54. const Eigen::PlainObjectBase<DerivedVB > & VB,
  55. const Eigen::PlainObjectBase<DerivedFB > & FB,
  56. const MeshBooleanType & type,
  57. Eigen::PlainObjectBase<DerivedVC > & VC,
  58. Eigen::PlainObjectBase<DerivedFC > & FC)
  59. {
  60. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  61. const std::function<void(
  62. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  63. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  64. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  65. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  66. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1>&)>
  67. empty_fun;
  68. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  69. }
  70. template <
  71. typename DerivedVA,
  72. typename DerivedFA,
  73. typename DerivedVB,
  74. typename DerivedFB,
  75. typename DerivedVC,
  76. typename DerivedFC,
  77. typename DerivedJ>
  78. IGL_INLINE void igl::boolean::mesh_boolean(
  79. const Eigen::PlainObjectBase<DerivedVA > & VA,
  80. const Eigen::PlainObjectBase<DerivedFA > & FA,
  81. const Eigen::PlainObjectBase<DerivedVB > & VB,
  82. const Eigen::PlainObjectBase<DerivedFB > & FB,
  83. const MeshBooleanType & type,
  84. const std::function<void(
  85. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  86. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  87. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  88. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  89. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  90. & resolve_fun,
  91. Eigen::PlainObjectBase<DerivedVC > & VC,
  92. Eigen::PlainObjectBase<DerivedFC > & FC,
  93. Eigen::PlainObjectBase<DerivedJ > & J)
  94. {
  95. using namespace Eigen;
  96. using namespace std;
  97. using namespace igl;
  98. using namespace igl::cgal;
  99. MeshBooleanType eff_type = type;
  100. // Concatenate A and B into a single mesh
  101. typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
  102. typedef Kernel::FT ExactScalar;
  103. typedef typename DerivedVC::Scalar Scalar;
  104. typedef typename DerivedFC::Scalar Index;
  105. typedef Matrix<Scalar,Dynamic,3> MatrixX3S;
  106. typedef Matrix<ExactScalar,Dynamic,3> MatrixX3ES;
  107. typedef Matrix<Index,Dynamic,3> MatrixX3I;
  108. typedef Matrix<Index,Dynamic,2> MatrixX2I;
  109. typedef Matrix<Index,Dynamic,1> VectorXI;
  110. typedef Matrix<typename DerivedJ::Scalar,Dynamic,1> VectorXJ;
  111. #ifdef IGL_MESH_BOOLEAN_DEBUG
  112. cout<<"mesh boolean..."<<endl;
  113. #endif
  114. MatrixX3S V(VA.rows()+VB.rows(),3);
  115. MatrixX3I F(FA.rows()+FB.rows(),3);
  116. V.block(0,0,VA.rows(),VA.cols()) = VA;
  117. V.block(VA.rows(),0,VB.rows(),VB.cols()) = VB;
  118. #ifdef IGL_MESH_BOOLEAN_DEBUG
  119. cout<<"prepare selfintersect input..."<<endl;
  120. #endif
  121. switch(type)
  122. {
  123. // Minus is implemented by flipping B and computing union
  124. case MESH_BOOLEAN_TYPE_MINUS:
  125. F.block(0,0,FA.rows(),FA.cols()) = FA.rowwise().reverse();
  126. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  127. //F.block(0,0,FA.rows(),3) = FA;
  128. //F.block(FA.rows(),0,FB.rows(),3) =
  129. // FB.rowwise().reverse().array()+VA.rows();
  130. eff_type = MESH_BOOLEAN_TYPE_INTERSECT;
  131. break;
  132. default:
  133. F.block(0,0,FA.rows(),FA.cols()) = FA;
  134. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  135. break;
  136. }
  137. // Resolve intersections (assumes A and B are solid)
  138. const auto & libigl_resolve = [](
  139. const MatrixX3S & V,
  140. const MatrixX3I & F,
  141. MatrixX3ES & CV,
  142. MatrixX3I & CF,
  143. VectorXJ & J)
  144. {
  145. MatrixX3ES SV;
  146. MatrixX3I SF;
  147. MatrixX2I SIF;
  148. VectorXI SIM,UIM;
  149. igl::cgal::RemeshSelfIntersectionsParam params;
  150. remesh_self_intersections(V,F,params,SV,SF,SIF,J,SIM);
  151. for_each(SF.data(),SF.data()+SF.size(),[&SIM](int & a){a=SIM(a);});
  152. {
  153. remove_unreferenced(SV,SF,CV,CF,UIM);
  154. }
  155. };
  156. #ifdef IGL_MESH_BOOLEAN_DEBUG
  157. cout<<"resolve..."<<endl;
  158. #endif
  159. MatrixX3S CV;
  160. MatrixX3ES EV;
  161. MatrixX3I CF;
  162. VectorXJ CJ;
  163. if(resolve_fun)
  164. {
  165. resolve_fun(V,F,CV,CF,CJ);
  166. }else
  167. {
  168. libigl_resolve(V,F,EV,CF,CJ);
  169. CV.resize(EV.rows(), EV.cols());
  170. std::transform(EV.data(), EV.data() + EV.rows()*EV.cols(),
  171. CV.data(), [&](ExactScalar val) {
  172. return CGAL::to_double(val);
  173. });
  174. }
  175. if(type == MESH_BOOLEAN_TYPE_RESOLVE)
  176. {
  177. FC = CF;
  178. VC = CV;
  179. J = CJ;
  180. return;
  181. }
  182. MatrixX3S N,CN;
  183. per_face_normals_stable(V,F,N);
  184. CN.resize(CF.rows(),3);
  185. for(size_t f = 0;f<(size_t)CN.rows();f++)
  186. {
  187. CN.row(f) = N.row(CJ(f));
  188. }
  189. #ifdef IGL_MESH_BOOLEAN_DEBUG
  190. cout<<"peel..."<<endl;
  191. #endif
  192. Matrix<bool,Dynamic,1> from_A(CF.rows());
  193. // peel layers keeping track of odd and even flips
  194. Matrix<bool,Dynamic,1> odd;
  195. Matrix<bool,Dynamic,1> flip;
  196. peel_outer_hull_layers(EV,CF,CN,odd,flip);
  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. cout<<"clean..."<<endl;
  244. #endif
  245. // Deal with duplicate faces
  246. {
  247. VectorXi IA,IC;
  248. MatrixX3I uG;
  249. unique_simplices(G,uG,IA,IC);
  250. assert(IA.rows() == uG.rows());
  251. // faces ontop of each unique face
  252. vector<vector<Index> > uG2G(uG.rows());
  253. // signed counts
  254. VectorXi counts = VectorXi::Zero(uG.rows());
  255. // loop over all faces
  256. for(Index g = 0;g<gm;g++)
  257. {
  258. const int ug = IC(g);
  259. assert(ug < uG2G.size());
  260. uG2G[ug].push_back(g);
  261. // is uG(g,:) just a rotated version of G(g,:) ?
  262. const bool consistent =
  263. (G(g,0) == uG(ug,0) && G(g,1) == uG(ug,1) && G(g,2) == uG(ug,2)) ||
  264. (G(g,0) == uG(ug,1) && G(g,1) == uG(ug,2) && G(g,2) == uG(ug,0)) ||
  265. (G(g,0) == uG(ug,2) && G(g,1) == uG(ug,0) && G(g,2) == uG(ug,1));
  266. counts(ug) += consistent ? 1 : -1;
  267. }
  268. MatrixX3I oldG = G;
  269. // Faces of output vG[i] = j means ith face of output should be jth face in
  270. // oldG
  271. vG.clear();
  272. for(size_t ug = 0;ug < uG2G.size();ug++)
  273. {
  274. // if signed occurrences is zero or ±two then keep none
  275. // else if signed occurrences is ±one then keep just one facet
  276. if(abs(counts(ug)) == 1)
  277. {
  278. assert(uG2G.size() > 0);
  279. vG.push_back(uG2G[ug][0]);
  280. }
  281. #ifdef IGL_MESH_BOOLEAN_DEBUG
  282. else
  283. {
  284. cout<<"Skipping "<<uG2G.size()<<" facets..."<<endl;
  285. }
  286. #endif
  287. }
  288. G.resize(vG.size(),3);
  289. J.resize(vG.size());
  290. for(size_t g = 0;g<vG.size();g++)
  291. {
  292. G.row(g) = oldG.row(vG[g]);
  293. J(g) = GJ(vG[g]);
  294. }
  295. }
  296. // remove unreferenced vertices
  297. VectorXi newIM;
  298. remove_unreferenced(CV,G,VC,FC,newIM);
  299. //cerr<<"warning not removing unref"<<endl;
  300. //VC = CV;
  301. //FC = G;
  302. }
  303. #ifdef IGL_STATIC_LIBRARY
  304. // This is a hack to discuss
  305. #include <igl/remove_unreferenced.cpp>
  306. 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> >&);
  307. #include <igl/cgal/peel_outer_hull_layers.cpp>
  308. 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> >&);
  309. #include <igl/cgal/outer_hull.cpp>
  310. 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> >&);
  311. // Explicit template specialization
  312. 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> >&);
  313. 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> >&);
  314. #endif