mesh_boolean.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. // Qingnan Zhou <qnzhou@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. //
  10. #include "mesh_boolean.h"
  11. #include "BinaryWindingNumberOperations.h"
  12. #include "../cgal/assign_scalar.h"
  13. #include "../cgal/propagate_winding_numbers.h"
  14. #include "../cgal/remesh_self_intersections.h"
  15. #include "../../remove_unreferenced.h"
  16. #include "../../unique_simplices.h"
  17. #include "../../slice.h"
  18. #include "../../resolve_duplicated_faces.h"
  19. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  20. #include <algorithm>
  21. template <
  22. typename DerivedVA,
  23. typename DerivedFA,
  24. typename DerivedVB,
  25. typename DerivedFB,
  26. typename WindingNumberOp,
  27. typename KeepFunc,
  28. typename ResolveFunc,
  29. typename DerivedVC,
  30. typename DerivedFC,
  31. typename DerivedJ>
  32. IGL_INLINE void igl::copyleft::boolean::mesh_boolean(
  33. const Eigen::PlainObjectBase<DerivedVA> & VA,
  34. const Eigen::PlainObjectBase<DerivedFA> & FA,
  35. const Eigen::PlainObjectBase<DerivedVB> & VB,
  36. const Eigen::PlainObjectBase<DerivedFB> & FB,
  37. const WindingNumberOp& wind_num_op,
  38. const KeepFunc& keep,
  39. const ResolveFunc& resolve_fun,
  40. Eigen::PlainObjectBase<DerivedVC > & VC,
  41. Eigen::PlainObjectBase<DerivedFC > & FC,
  42. Eigen::PlainObjectBase<DerivedJ > & J) {
  43. typedef typename DerivedVC::Scalar Scalar;
  44. //typedef typename DerivedFC::Scalar Index;
  45. typedef CGAL::Epeck Kernel;
  46. typedef Kernel::FT ExactScalar;
  47. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
  48. //typedef Eigen::Matrix<Index,Eigen::Dynamic,Eigen::Dynamic> MatrixXI;
  49. typedef Eigen::Matrix<typename DerivedJ::Scalar,Eigen::Dynamic,1> VectorXJ;
  50. // Generate combined mesh.
  51. typedef Eigen::Matrix<
  52. ExactScalar,
  53. Eigen::Dynamic,
  54. Eigen::Dynamic,
  55. DerivedVC::IsRowMajor> MatrixXES;
  56. MatrixXES V;
  57. DerivedFC F;
  58. VectorXJ CJ;
  59. {
  60. DerivedVA VV(VA.rows() + VB.rows(), 3);
  61. DerivedFC FF(FA.rows() + FB.rows(), 3);
  62. VV << VA, VB;
  63. FF << FA, FB.array() + VA.rows();
  64. //// Handle annoying empty cases
  65. //if(VA.size()>0)
  66. //{
  67. // VV<<VA;
  68. //}
  69. //if(VB.size()>0)
  70. //{
  71. // VV<<VB;
  72. //}
  73. //if(FA.size()>0)
  74. //{
  75. // FF<<FA;
  76. //}
  77. //if(FB.size()>0)
  78. //{
  79. // FF<<FB.array()+VA.rows();
  80. //}
  81. resolve_fun(VV, FF, V, F, CJ);
  82. }
  83. // Compute winding numbers on each side of each facet.
  84. const size_t num_faces = F.rows();
  85. Eigen::MatrixXi W;
  86. Eigen::VectorXi labels(num_faces);
  87. std::transform(CJ.data(), CJ.data()+CJ.size(), labels.data(),
  88. [&](int i) { return i<FA.rows() ? 0:1; });
  89. igl::copyleft::cgal::propagate_winding_numbers(V, F, labels, W);
  90. assert((size_t)W.rows() == num_faces);
  91. if (W.cols() == 2) {
  92. assert(FB.rows() == 0);
  93. Eigen::MatrixXi W_tmp(num_faces, 4);
  94. W_tmp << W, Eigen::MatrixXi::Zero(num_faces, 2);
  95. W = W_tmp;
  96. } else {
  97. assert(W.cols() == 4);
  98. }
  99. // Compute resulting winding number.
  100. Eigen::MatrixXi Wr(num_faces, 2);
  101. for (size_t i=0; i<num_faces; i++) {
  102. Eigen::MatrixXi w_out(1,2), w_in(1,2);
  103. w_out << W(i,0), W(i,2);
  104. w_in << W(i,1), W(i,3);
  105. Wr(i,0) = wind_num_op(w_out);
  106. Wr(i,1) = wind_num_op(w_in);
  107. }
  108. // Extract boundary separating inside from outside.
  109. auto index_to_signed_index = [&](size_t i, bool ori) -> int{
  110. return (i+1)*(ori?1:-1);
  111. };
  112. //auto signed_index_to_index = [&](int i) -> size_t {
  113. // return abs(i) - 1;
  114. //};
  115. std::vector<int> selected;
  116. for(size_t i=0; i<num_faces; i++) {
  117. auto should_keep = keep(Wr(i,0), Wr(i,1));
  118. if (should_keep > 0) {
  119. selected.push_back(index_to_signed_index(i, true));
  120. } else if (should_keep < 0) {
  121. selected.push_back(index_to_signed_index(i, false));
  122. }
  123. }
  124. const size_t num_selected = selected.size();
  125. DerivedFC kept_faces(num_selected, 3);
  126. DerivedJ kept_face_indices(num_selected, 1);
  127. for (size_t i=0; i<num_selected; i++) {
  128. size_t idx = abs(selected[i]) - 1;
  129. if (selected[i] > 0) {
  130. kept_faces.row(i) = F.row(idx);
  131. } else {
  132. kept_faces.row(i) = F.row(idx).reverse();
  133. }
  134. kept_face_indices(i, 0) = CJ[idx];
  135. }
  136. // Finally, remove duplicated faces and unreferenced vertices.
  137. {
  138. DerivedFC G;
  139. DerivedJ JJ;
  140. igl::resolve_duplicated_faces(kept_faces, G, JJ);
  141. igl::slice(kept_face_indices, JJ, 1, J);
  142. MatrixX3S Vs(V.rows(), V.cols());
  143. for (size_t i=0; i<(size_t)V.rows(); i++)
  144. {
  145. for (size_t j=0; j<(size_t)V.cols(); j++)
  146. {
  147. igl::copyleft::cgal::assign_scalar(V(i,j), Vs(i,j));
  148. }
  149. }
  150. Eigen::VectorXi newIM;
  151. igl::remove_unreferenced(Vs,G,VC,FC,newIM);
  152. }
  153. }
  154. template <
  155. typename DerivedVA,
  156. typename DerivedFA,
  157. typename DerivedVB,
  158. typename DerivedFB,
  159. typename ResolveFunc,
  160. typename DerivedVC,
  161. typename DerivedFC,
  162. typename DerivedJ>
  163. IGL_INLINE void igl::copyleft::boolean::mesh_boolean(
  164. const Eigen::PlainObjectBase<DerivedVA > & VA,
  165. const Eigen::PlainObjectBase<DerivedFA > & FA,
  166. const Eigen::PlainObjectBase<DerivedVB > & VB,
  167. const Eigen::PlainObjectBase<DerivedFB > & FB,
  168. const MeshBooleanType & type,
  169. const ResolveFunc& resolve_func,
  170. Eigen::PlainObjectBase<DerivedVC > & VC,
  171. Eigen::PlainObjectBase<DerivedFC > & FC,
  172. Eigen::PlainObjectBase<DerivedJ > & J)
  173. {
  174. //typedef CGAL::Epeck Kernel;
  175. //typedef Kernel::FT ExactScalar;
  176. //typedef Eigen::Matrix<
  177. // ExactScalar,
  178. // Eigen::Dynamic,
  179. // Eigen::Dynamic,
  180. // DerivedVC::IsRowMajor> MatrixXES;
  181. switch (type) {
  182. case MESH_BOOLEAN_TYPE_UNION:
  183. igl::copyleft::boolean::mesh_boolean(
  184. VA, FA, VB, FB, igl::copyleft::boolean::BinaryUnion(),
  185. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  186. break;
  187. case MESH_BOOLEAN_TYPE_INTERSECT:
  188. igl::copyleft::boolean::mesh_boolean(
  189. VA, FA, VB, FB, igl::copyleft::boolean::BinaryIntersect(),
  190. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  191. break;
  192. case MESH_BOOLEAN_TYPE_MINUS:
  193. igl::copyleft::boolean::mesh_boolean(
  194. VA, FA, VB, FB, igl::copyleft::boolean::BinaryMinus(),
  195. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  196. break;
  197. case MESH_BOOLEAN_TYPE_XOR:
  198. igl::copyleft::boolean::mesh_boolean(
  199. VA, FA, VB, FB, igl::copyleft::boolean::BinaryXor(),
  200. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  201. break;
  202. case MESH_BOOLEAN_TYPE_RESOLVE:
  203. //op = binary_resolve();
  204. igl::copyleft::boolean::mesh_boolean(
  205. VA, FA, VB, FB, igl::copyleft::boolean::BinaryResolve(),
  206. igl::copyleft::boolean::KeepAll(), resolve_func, VC, FC, J);
  207. break;
  208. default:
  209. throw std::runtime_error("Unsupported boolean type.");
  210. }
  211. }
  212. template <
  213. typename DerivedVA,
  214. typename DerivedFA,
  215. typename DerivedVB,
  216. typename DerivedFB,
  217. typename DerivedVC,
  218. typename DerivedFC,
  219. typename DerivedJ>
  220. IGL_INLINE void igl::copyleft::boolean::mesh_boolean(
  221. const Eigen::PlainObjectBase<DerivedVA > & VA,
  222. const Eigen::PlainObjectBase<DerivedFA > & FA,
  223. const Eigen::PlainObjectBase<DerivedVB > & VB,
  224. const Eigen::PlainObjectBase<DerivedFB > & FB,
  225. const MeshBooleanType & type,
  226. Eigen::PlainObjectBase<DerivedVC > & VC,
  227. Eigen::PlainObjectBase<DerivedFC > & FC,
  228. Eigen::PlainObjectBase<DerivedJ > & J)
  229. {
  230. typedef CGAL::Epeck Kernel;
  231. typedef Kernel::FT ExactScalar;
  232. typedef Eigen::Matrix<
  233. ExactScalar,
  234. Eigen::Dynamic,
  235. Eigen::Dynamic,
  236. DerivedVC::IsRowMajor> MatrixXES;
  237. std::function<void(
  238. const Eigen::PlainObjectBase<DerivedVA>&,
  239. const Eigen::PlainObjectBase<DerivedFA>&,
  240. Eigen::PlainObjectBase<MatrixXES>&,
  241. Eigen::PlainObjectBase<DerivedFC>&,
  242. Eigen::PlainObjectBase<DerivedJ>&)> resolve_func =
  243. [](const Eigen::PlainObjectBase<DerivedVA>& V,
  244. const Eigen::PlainObjectBase<DerivedFA>& F,
  245. Eigen::PlainObjectBase<MatrixXES>& Vo,
  246. Eigen::PlainObjectBase<DerivedFC>& Fo,
  247. Eigen::PlainObjectBase<DerivedJ>& J) {
  248. Eigen::VectorXi I;
  249. igl::copyleft::cgal::RemeshSelfIntersectionsParam params;
  250. MatrixXES Vr;
  251. DerivedFC Fr;
  252. Eigen::MatrixXi IF;
  253. igl::copyleft::cgal::remesh_self_intersections(
  254. V, F, params, Vr, Fr, IF, J, I);
  255. assert(I.size() == Vr.rows());
  256. // Merge coinciding vertices into non-manifold vertices.
  257. std::for_each(Fr.data(), Fr.data()+Fr.size(),
  258. [&I](typename DerivedFC::Scalar& a) { a=I[a]; });
  259. // Remove unreferenced vertices.
  260. Eigen::VectorXi UIM;
  261. igl::remove_unreferenced(Vr, Fr, Vo, Fo, UIM);
  262. };
  263. return mesh_boolean(VA,FA,VB,FB,type,resolve_func,VC,FC,J);
  264. }
  265. template <
  266. typename DerivedVA,
  267. typename DerivedFA,
  268. typename DerivedVB,
  269. typename DerivedFB,
  270. typename DerivedVC,
  271. typename DerivedFC>
  272. IGL_INLINE void igl::copyleft::boolean::mesh_boolean(
  273. const Eigen::PlainObjectBase<DerivedVA > & VA,
  274. const Eigen::PlainObjectBase<DerivedFA > & FA,
  275. const Eigen::PlainObjectBase<DerivedVB > & VB,
  276. const Eigen::PlainObjectBase<DerivedFB > & FB,
  277. const MeshBooleanType & type,
  278. Eigen::PlainObjectBase<DerivedVC > & VC,
  279. Eigen::PlainObjectBase<DerivedFC > & FC) {
  280. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  281. return igl::copyleft::boolean::mesh_boolean(VA,FA,VB,FB,type,VC,FC,J);
  282. }
  283. #ifdef IGL_STATIC_LIBRARY
  284. // Explicit template specialization
  285. template void igl::copyleft::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::copyleft::boolean::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  286. template void igl::copyleft::boolean::mesh_boolean<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<long, -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, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, igl::copyleft::boolean::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  287. template void igl::copyleft::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::copyleft::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> >&);
  288. #undef IGL_STATIC_LIBRARY
  289. #include "../../remove_unreferenced.cpp"
  290. template void igl::remove_unreferenced<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  291. #include "../../slice.cpp"
  292. template void igl::slice<Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -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> > >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> >&);
  293. #endif