mesh_boolean.cpp 10 KB

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