mesh_boolean.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <iostream>
  15. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  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::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::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::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. MeshBooleanType eff_type = type;
  99. // Concatenate A and B into a single mesh
  100. typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
  101. typedef Kernel::FT ExactScalar;
  102. typedef typename DerivedVC::Scalar Scalar;
  103. typedef typename DerivedFC::Scalar Index;
  104. typedef Matrix<Scalar,Dynamic,3> MatrixX3S;
  105. typedef Matrix<ExactScalar,Dynamic,3> MatrixX3ES;
  106. typedef Matrix<Index,Dynamic,3> MatrixX3I;
  107. typedef Matrix<Index,Dynamic,2> MatrixX2I;
  108. typedef Matrix<Index,Dynamic,1> VectorXI;
  109. typedef Matrix<typename DerivedJ::Scalar,Dynamic,1> VectorXJ;
  110. #ifdef IGL_MESH_BOOLEAN_DEBUG
  111. cout<<"mesh boolean..."<<endl;
  112. #endif
  113. MatrixX3S V(VA.rows()+VB.rows(),3);
  114. MatrixX3I F(FA.rows()+FB.rows(),3);
  115. V.block(0,0,VA.rows(),VA.cols()) = VA;
  116. V.block(VA.rows(),0,VB.rows(),VB.cols()) = VB;
  117. #ifdef IGL_MESH_BOOLEAN_DEBUG
  118. cout<<"prepare selfintersect input..."<<endl;
  119. #endif
  120. switch(type)
  121. {
  122. // Minus is implemented by flipping B and computing union
  123. case MESH_BOOLEAN_TYPE_MINUS:
  124. F.block(0,0,FA.rows(),FA.cols()) = FA.rowwise().reverse();
  125. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  126. //F.block(0,0,FA.rows(),3) = FA;
  127. //F.block(FA.rows(),0,FB.rows(),3) =
  128. // FB.rowwise().reverse().array()+VA.rows();
  129. eff_type = MESH_BOOLEAN_TYPE_INTERSECT;
  130. break;
  131. default:
  132. F.block(0,0,FA.rows(),FA.cols()) = FA;
  133. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  134. break;
  135. }
  136. // Resolve intersections (assumes A and B are solid)
  137. const auto & libigl_resolve = [](
  138. const MatrixX3S & V,
  139. const MatrixX3I & F,
  140. MatrixX3ES & CV,
  141. MatrixX3I & CF,
  142. VectorXJ & J)
  143. {
  144. MatrixX3ES SV;
  145. MatrixX3I SF;
  146. MatrixX2I SIF;
  147. VectorXI SIM,UIM;
  148. RemeshSelfIntersectionsParam params;
  149. remesh_self_intersections(V,F,params,SV,SF,SIF,J,SIM);
  150. for_each(SF.data(),SF.data()+SF.size(),[&SIM](int & a){a=SIM(a);});
  151. {
  152. remove_unreferenced(SV,SF,CV,CF,UIM);
  153. }
  154. };
  155. #ifdef IGL_MESH_BOOLEAN_DEBUG
  156. cout<<"resolve..."<<endl;
  157. #endif
  158. MatrixX3S CV;
  159. MatrixX3ES EV;
  160. MatrixX3I CF;
  161. VectorXJ CJ;
  162. if(resolve_fun)
  163. {
  164. resolve_fun(V,F,CV,CF,CJ);
  165. }else
  166. {
  167. libigl_resolve(V,F,EV,CF,CJ);
  168. CV.resize(EV.rows(), EV.cols());
  169. std::transform(EV.data(), EV.data() + EV.rows()*EV.cols(),
  170. CV.data(), [&](ExactScalar val) {
  171. return CGAL::to_double(val);
  172. });
  173. }
  174. if(type == MESH_BOOLEAN_TYPE_RESOLVE)
  175. {
  176. FC = CF;
  177. VC = CV;
  178. J = CJ;
  179. return;
  180. }
  181. MatrixX3S N,CN;
  182. per_face_normals_stable(V,F,N);
  183. CN.resize(CF.rows(),3);
  184. for(size_t f = 0;f<(size_t)CN.rows();f++)
  185. {
  186. CN.row(f) = N.row(CJ(f));
  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. Matrix<bool,Dynamic,1> odd;
  194. Matrix<bool,Dynamic,1> flip;
  195. peel_outer_hull_layers(EV,CF,CN,odd,flip);
  196. #ifdef IGL_MESH_BOOLEAN_DEBUG
  197. cout<<"categorize..."<<endl;
  198. #endif
  199. const Index m = CF.rows();
  200. // Faces of output vG[i] = j means ith face of output should be jth face in F
  201. std::vector<Index> vG;
  202. // Whether faces of output should be flipped, Gflip[i] = true means ith face
  203. // of output should be F.row(vG[i]).reverse() rather than F.row(vG[i])
  204. std::vector<bool> Gflip;
  205. for(Index f = 0;f<m;f++)
  206. {
  207. switch(eff_type)
  208. {
  209. case MESH_BOOLEAN_TYPE_XOR:
  210. case MESH_BOOLEAN_TYPE_UNION:
  211. if((odd(f)&&!flip(f))||(!odd(f)&&flip(f)))
  212. {
  213. vG.push_back(f);
  214. Gflip.push_back(false);
  215. }else if(eff_type == MESH_BOOLEAN_TYPE_XOR)
  216. {
  217. vG.push_back(f);
  218. Gflip.push_back(true);
  219. }
  220. break;
  221. case MESH_BOOLEAN_TYPE_INTERSECT:
  222. if((!odd(f) && !flip(f)) || (odd(f) && flip(f)))
  223. {
  224. vG.push_back(f);
  225. Gflip.push_back(type == MESH_BOOLEAN_TYPE_MINUS);
  226. }
  227. break;
  228. default:
  229. assert(false && "Unknown type");
  230. return;
  231. }
  232. }
  233. const Index gm = vG.size();
  234. MatrixX3I G(gm,3);
  235. VectorXi GJ(gm,1);
  236. for(Index g = 0;g<gm;g++)
  237. {
  238. G.row(g) = Gflip[g] ? CF.row(vG[g]).reverse().eval() : CF.row(vG[g]);
  239. GJ(g) = CJ(vG[g]);
  240. }
  241. #ifdef IGL_MESH_BOOLEAN_DEBUG
  242. cout<<"clean..."<<endl;
  243. #endif
  244. // Deal with duplicate faces
  245. {
  246. VectorXi IA,IC;
  247. MatrixX3I uG;
  248. unique_simplices(G,uG,IA,IC);
  249. assert(IA.rows() == uG.rows());
  250. // faces ontop of each unique face
  251. vector<vector<Index> > uG2G(uG.rows());
  252. // signed counts
  253. VectorXi counts = VectorXi::Zero(uG.rows());
  254. // loop over all faces
  255. for(Index g = 0;g<gm;g++)
  256. {
  257. const int ug = IC(g);
  258. assert(ug < uG2G.size());
  259. uG2G[ug].push_back(g);
  260. // is uG(g,:) just a rotated version of G(g,:) ?
  261. const bool consistent =
  262. (G(g,0) == uG(ug,0) && G(g,1) == uG(ug,1) && G(g,2) == uG(ug,2)) ||
  263. (G(g,0) == uG(ug,1) && G(g,1) == uG(ug,2) && G(g,2) == uG(ug,0)) ||
  264. (G(g,0) == uG(ug,2) && G(g,1) == uG(ug,0) && G(g,2) == uG(ug,1));
  265. counts(ug) += consistent ? 1 : -1;
  266. }
  267. MatrixX3I oldG = G;
  268. // Faces of output vG[i] = j means ith face of output should be jth face in
  269. // oldG
  270. vG.clear();
  271. for(size_t ug = 0;ug < uG2G.size();ug++)
  272. {
  273. // if signed occurrences is zero or ±two then keep none
  274. // else if signed occurrences is ±one then keep just one facet
  275. if(abs(counts(ug)) == 1)
  276. {
  277. assert(uG2G.size() > 0);
  278. vG.push_back(uG2G[ug][0]);
  279. }
  280. #ifdef IGL_MESH_BOOLEAN_DEBUG
  281. else
  282. {
  283. cout<<"Skipping "<<uG2G.size()<<" facets..."<<endl;
  284. }
  285. #endif
  286. }
  287. G.resize(vG.size(),3);
  288. J.resize(vG.size());
  289. for(size_t g = 0;g<vG.size();g++)
  290. {
  291. G.row(g) = oldG.row(vG[g]);
  292. J(g) = GJ(vG[g]);
  293. }
  294. }
  295. // remove unreferenced vertices
  296. VectorXi newIM;
  297. remove_unreferenced(CV,G,VC,FC,newIM);
  298. //cerr<<"warning not removing unref"<<endl;
  299. //VC = CV;
  300. //FC = G;
  301. }
  302. #ifdef IGL_STATIC_LIBRARY
  303. // This is a hack to discuss
  304. #include <igl/remove_unreferenced.cpp>
  305. 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> >&);
  306. #include <igl/cgal/peel_outer_hull_layers.cpp>
  307. template unsigned long igl::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> >&);
  308. #include <igl/cgal/outer_hull.cpp>
  309. template void igl::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> >&);
  310. // Explicit template specialization
  311. template void igl::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::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  312. template void igl::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::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> >&);
  313. #endif