mesh_boolean.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "mesh_boolean.h"
  2. #include <igl/peal_outer_hull_layers.h>
  3. #include <igl/cgal/remesh_self_intersections.h>
  4. #include <igl/remove_unreferenced.h>
  5. #include <igl/unique_simplices.h>
  6. #include <iostream>
  7. template <
  8. typename DerivedVA,
  9. typename DerivedFA,
  10. typename DerivedVB,
  11. typename DerivedFB,
  12. typename DerivedVC,
  13. typename DerivedFC,
  14. typename DerivedJ>
  15. IGL_INLINE void igl::mesh_boolean(
  16. const Eigen::PlainObjectBase<DerivedVA > & VA,
  17. const Eigen::PlainObjectBase<DerivedFA > & FA,
  18. const Eigen::PlainObjectBase<DerivedVB > & VB,
  19. const Eigen::PlainObjectBase<DerivedFB > & FB,
  20. const MeshBooleanType & type,
  21. Eigen::PlainObjectBase<DerivedVC > & VC,
  22. Eigen::PlainObjectBase<DerivedFC > & FC,
  23. Eigen::PlainObjectBase<DerivedJ > & J)
  24. {
  25. const std::function<void(
  26. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  27. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  28. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  29. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  30. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  31. empty_fun;
  32. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  33. }
  34. template <
  35. typename DerivedVA,
  36. typename DerivedFA,
  37. typename DerivedVB,
  38. typename DerivedFB,
  39. typename DerivedVC,
  40. typename DerivedFC>
  41. IGL_INLINE void igl::mesh_boolean(
  42. const Eigen::PlainObjectBase<DerivedVA > & VA,
  43. const Eigen::PlainObjectBase<DerivedFA > & FA,
  44. const Eigen::PlainObjectBase<DerivedVB > & VB,
  45. const Eigen::PlainObjectBase<DerivedFB > & FB,
  46. const MeshBooleanType & type,
  47. Eigen::PlainObjectBase<DerivedVC > & VC,
  48. Eigen::PlainObjectBase<DerivedFC > & FC)
  49. {
  50. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  51. const std::function<void(
  52. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  53. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  54. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3> &,
  55. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  56. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1>&)>
  57. empty_fun;
  58. return mesh_boolean(VA,FA,VB,FB,type,empty_fun,VC,FC,J);
  59. }
  60. template <
  61. typename DerivedVA,
  62. typename DerivedFA,
  63. typename DerivedVB,
  64. typename DerivedFB,
  65. typename DerivedVC,
  66. typename DerivedFC,
  67. typename DerivedJ>
  68. IGL_INLINE void igl::mesh_boolean(
  69. const Eigen::PlainObjectBase<DerivedVA > & VA,
  70. const Eigen::PlainObjectBase<DerivedFA > & FA,
  71. const Eigen::PlainObjectBase<DerivedVB > & VB,
  72. const Eigen::PlainObjectBase<DerivedFB > & FB,
  73. const MeshBooleanType & type,
  74. const std::function<void(
  75. const Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  76. const Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  77. Eigen::Matrix<typename DerivedVC::Scalar,Eigen::Dynamic,3>&,
  78. Eigen::Matrix<typename DerivedFC::Scalar, Eigen::Dynamic,3>&,
  79. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1>&)>
  80. & resolve_fun,
  81. Eigen::PlainObjectBase<DerivedVC > & VC,
  82. Eigen::PlainObjectBase<DerivedFC > & FC,
  83. Eigen::PlainObjectBase<DerivedJ > & J)
  84. {
  85. using namespace Eigen;
  86. using namespace std;
  87. using namespace igl;
  88. MeshBooleanType eff_type = type;
  89. // Concatenate A and B into a single mesh
  90. typedef typename DerivedVC::Scalar Scalar;
  91. typedef typename DerivedFC::Scalar Index;
  92. typedef Matrix<Scalar,Dynamic,3> MatrixX3S;
  93. typedef Matrix<Index,Dynamic,3> MatrixX3I;
  94. typedef Matrix<Index,Dynamic,2> MatrixX2I;
  95. typedef Matrix<Index,Dynamic,1> VectorXI;
  96. typedef Matrix<typename DerivedJ::Scalar,Dynamic,1> VectorXJ;
  97. MatrixX3S V(VA.rows()+VB.rows(),3);
  98. MatrixX3I F(FA.rows()+FB.rows(),3);
  99. V.block(0,0,VA.rows(),VA.cols()) = VA;
  100. V.block(VA.rows(),0,VB.rows(),VB.cols()) = VB;
  101. switch(type)
  102. {
  103. // Minus is implemented by flipping B and computing union
  104. case MESH_BOOLEAN_TYPE_MINUS:
  105. F.block(0,0,FA.rows(),FA.cols()) = FA.rowwise().reverse();
  106. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  107. //F.block(0,0,FA.rows(),3) = FA;
  108. //F.block(FA.rows(),0,FB.rows(),3) =
  109. // FB.rowwise().reverse().array()+VA.rows();
  110. eff_type = MESH_BOOLEAN_TYPE_INTERSECT;
  111. break;
  112. default:
  113. F.block(0,0,FA.rows(),FA.cols()) = FA;
  114. F.block(FA.rows(),0,FB.rows(),FB.cols()) = FB.array()+VA.rows();
  115. break;
  116. }
  117. // Resolve intersections (assumes A and B are solid)
  118. const auto & libigl_resolve = [](
  119. const MatrixX3S & V,
  120. const MatrixX3I & F,
  121. MatrixX3S & CV,
  122. MatrixX3I & CF,
  123. VectorXJ & J)
  124. {
  125. MatrixX3S SV;
  126. MatrixX3I SF;
  127. MatrixX2I SIF;
  128. VectorXI SIM,UIM;
  129. RemeshSelfIntersectionsParam params;
  130. remesh_self_intersections(V,F,params,SV,SF,SIF,J,SIM);
  131. for_each(SF.data(),SF.data()+SF.size(),[&SIM](int & a){a=SIM(a);});
  132. {
  133. remove_unreferenced(SV,SF,CV,CF,UIM);
  134. }
  135. };
  136. MatrixX3S CV;
  137. MatrixX3I CF;
  138. VectorXJ CJ;
  139. if(resolve_fun)
  140. {
  141. resolve_fun(V,F,CV,CF,CJ);
  142. }else
  143. {
  144. libigl_resolve(V,F,CV,CF,CJ);
  145. }
  146. if(type == MESH_BOOLEAN_TYPE_RESOLVE)
  147. {
  148. FC = CF;
  149. VC = CV;
  150. J = CJ;
  151. return;
  152. }
  153. Matrix<bool,Dynamic,1> from_A(CF.rows());
  154. // Peal layers keeping track of odd and even flips
  155. Matrix<bool,Dynamic,1> odd;
  156. Matrix<bool,Dynamic,1> flip;
  157. peal_outer_hull_layers(CV,CF,odd,flip);
  158. const Index m = CF.rows();
  159. // Faces of output vG[i] = j means ith face of output should be jth face in F
  160. std::vector<Index> vG;
  161. // Whether faces of output should be flipped, Gflip[i] = true means ith face
  162. // of output should be F.row(vG[i]).reverse() rather than F.row(vG[i])
  163. std::vector<bool> Gflip;
  164. for(Index f = 0;f<m;f++)
  165. {
  166. switch(eff_type)
  167. {
  168. case MESH_BOOLEAN_TYPE_XOR:
  169. case MESH_BOOLEAN_TYPE_UNION:
  170. if((odd(f)&&!flip(f))||(!odd(f)&&flip(f)))
  171. {
  172. vG.push_back(f);
  173. Gflip.push_back(false);
  174. }else if(eff_type == MESH_BOOLEAN_TYPE_XOR)
  175. {
  176. vG.push_back(f);
  177. Gflip.push_back(true);
  178. }
  179. break;
  180. case MESH_BOOLEAN_TYPE_INTERSECT:
  181. if((!odd(f) && !flip(f)) || (odd(f) && flip(f)))
  182. {
  183. vG.push_back(f);
  184. Gflip.push_back(type == MESH_BOOLEAN_TYPE_MINUS);
  185. }
  186. break;
  187. default:
  188. assert(false && "Unknown type");
  189. return;
  190. }
  191. }
  192. const Index gm = vG.size();
  193. MatrixX3I G(gm,3);
  194. VectorXi GJ(gm,1);
  195. for(Index g = 0;g<gm;g++)
  196. {
  197. G.row(g) = Gflip[g] ? CF.row(vG[g]).reverse().eval() : CF.row(vG[g]);
  198. GJ(g) = CJ(vG[g]);
  199. }
  200. // remove duplicates: cancel out in all cases? assertion in others?
  201. {
  202. MatrixXi oldG = G;
  203. VectorXi IA,_1;
  204. unique_simplices(oldG,G,IA,_1);
  205. assert(IA.rows() == G.rows());
  206. J.resize(IA.rows(),1);
  207. for(size_t j = 0;j<(size_t)J.size();j++)
  208. {
  209. J(j) = GJ(IA(j));
  210. }
  211. }
  212. // remove unreferenced vertices
  213. VectorXi newIM;
  214. remove_unreferenced(CV,G,VC,FC,newIM);
  215. //cerr<<"warning not removing unref"<<endl;
  216. //VC = CV;
  217. //FC = G;
  218. }
  219. #ifdef IGL_STATIC_LIBRARY
  220. // Explicit template specialization
  221. 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> >&);
  222. 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> >&);
  223. #endif