mesh_boolean.cpp 13 KB

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