mesh_boolean.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 "../cgal/relabel_small_immersed_cells.h"
  16. #include "../cgal/extract_cells.h"
  17. #include "../../extract_manifold_patches.h"
  18. #include "../../remove_unreferenced.h"
  19. #include "../../unique_simplices.h"
  20. #include "../../unique_edge_map.h"
  21. #include "../../slice.h"
  22. #include "../../resolve_duplicated_faces.h"
  23. #include "../../get_seconds.h"
  24. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  25. #include <algorithm>
  26. //#define MESH_BOOLEAN_TIMING
  27. //#define DOUBLE_CHECK_EXACT_OUTPUT
  28. //#define SMALL_CELL_REMOVAL
  29. template <
  30. typename DerivedVA,
  31. typename DerivedFA,
  32. typename DerivedVB,
  33. typename DerivedFB,
  34. typename WindingNumberOp,
  35. typename KeepFunc,
  36. typename ResolveFunc,
  37. typename DerivedVC,
  38. typename DerivedFC,
  39. typename DerivedJ>
  40. IGL_INLINE bool igl::copyleft::boolean::mesh_boolean(
  41. const Eigen::PlainObjectBase<DerivedVA> & VA,
  42. const Eigen::PlainObjectBase<DerivedFA> & FA,
  43. const Eigen::PlainObjectBase<DerivedVB> & VB,
  44. const Eigen::PlainObjectBase<DerivedFB> & FB,
  45. const WindingNumberOp& wind_num_op,
  46. const KeepFunc& keep,
  47. const ResolveFunc& resolve_fun,
  48. Eigen::PlainObjectBase<DerivedVC > & VC,
  49. Eigen::PlainObjectBase<DerivedFC > & FC,
  50. Eigen::PlainObjectBase<DerivedJ > & J)
  51. {
  52. #ifdef MESH_BOOLEAN_TIMING
  53. const auto & tictoc = []() -> double
  54. {
  55. static double t_start = igl::get_seconds();
  56. double diff = igl::get_seconds()-t_start;
  57. t_start += diff;
  58. return diff;
  59. };
  60. const auto log_time = [&](const std::string& label) -> void {
  61. std::cout << "mesh_boolean." << label << ": "
  62. << tictoc() << std::endl;
  63. };
  64. tictoc();
  65. #endif
  66. typedef typename DerivedVC::Scalar Scalar;
  67. //typedef typename DerivedFC::Scalar Index;
  68. typedef CGAL::Epeck Kernel;
  69. typedef Kernel::FT ExactScalar;
  70. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
  71. //typedef Eigen::Matrix<Index,Eigen::Dynamic,Eigen::Dynamic> MatrixXI;
  72. typedef Eigen::Matrix<typename DerivedJ::Scalar,Eigen::Dynamic,1> VectorXJ;
  73. // Generate combined mesh.
  74. typedef Eigen::Matrix<
  75. ExactScalar,
  76. Eigen::Dynamic,
  77. Eigen::Dynamic,
  78. DerivedVC::IsRowMajor> MatrixXES;
  79. MatrixXES V;
  80. DerivedFC F;
  81. VectorXJ CJ;
  82. {
  83. DerivedVA VV(VA.rows() + VB.rows(), 3);
  84. DerivedFC FF(FA.rows() + FB.rows(), 3);
  85. VV << VA, VB;
  86. FF << FA, FB.array() + VA.rows();
  87. resolve_fun(VV, FF, V, F, CJ);
  88. }
  89. #ifdef MESH_BOOLEAN_TIMING
  90. log_time("resolve_self_intersection");
  91. #endif
  92. // Compute edges.
  93. Eigen::MatrixXi E, uE;
  94. Eigen::VectorXi EMAP;
  95. std::vector<std::vector<size_t> > uE2E;
  96. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  97. // Compute patches
  98. Eigen::VectorXi P;
  99. const size_t num_patches = igl::extract_manifold_patches(F, EMAP, uE2E, P);
  100. #ifdef MESH_BOOLEAN_TIMING
  101. log_time("patch_extraction");
  102. #endif
  103. // Compute cells.
  104. Eigen::MatrixXi per_patch_cells;
  105. const size_t num_cells =
  106. igl::copyleft::cgal::extract_cells(
  107. V, F, P, E, uE, uE2E, EMAP, per_patch_cells);
  108. #ifdef MESH_BOOLEAN_TIMING
  109. log_time("cell_extraction");
  110. #endif
  111. // Compute winding numbers on each side of each facet.
  112. const size_t num_faces = F.rows();
  113. Eigen::MatrixXi W;
  114. Eigen::VectorXi labels(num_faces);
  115. std::transform(CJ.data(), CJ.data()+CJ.size(), labels.data(),
  116. [&](int i) { return i<FA.rows() ? 0:1; });
  117. bool valid = true;
  118. if (num_faces > 0)
  119. {
  120. valid = valid &
  121. igl::copyleft::cgal::propagate_winding_numbers(
  122. V, F, uE, uE2E, num_patches, P, num_cells, per_patch_cells, labels, W);
  123. } else
  124. {
  125. W.resize(0, 4);
  126. }
  127. assert((size_t)W.rows() == num_faces);
  128. if (W.cols() == 2)
  129. {
  130. assert(FB.rows() == 0);
  131. Eigen::MatrixXi W_tmp(num_faces, 4);
  132. W_tmp << W, Eigen::MatrixXi::Zero(num_faces, 2);
  133. W = W_tmp;
  134. } else {
  135. assert(W.cols() == 4);
  136. }
  137. #ifdef MESH_BOOLEAN_TIMING
  138. log_time("propagate_input_winding_number");
  139. #endif
  140. // Compute resulting winding number.
  141. Eigen::MatrixXi Wr(num_faces, 2);
  142. for (size_t i=0; i<num_faces; i++)
  143. {
  144. Eigen::MatrixXi w_out(1,2), w_in(1,2);
  145. w_out << W(i,0), W(i,2);
  146. w_in << W(i,1), W(i,3);
  147. Wr(i,0) = wind_num_op(w_out);
  148. Wr(i,1) = wind_num_op(w_in);
  149. }
  150. #ifdef MESH_BOOLEAN_TIMING
  151. log_time("compute_output_winding_number");
  152. #endif
  153. #ifdef SMALL_CELL_REMOVAL
  154. igl::copyleft::cgal::relabel_small_immersed_cells(V, F,
  155. num_patches, P, num_cells, per_patch_cells, 1e-3, Wr);
  156. #endif
  157. // Extract boundary separating inside from outside.
  158. auto index_to_signed_index = [&](size_t i, bool ori) -> int
  159. {
  160. return (i+1)*(ori?1:-1);
  161. };
  162. //auto signed_index_to_index = [&](int i) -> size_t {
  163. // return abs(i) - 1;
  164. //};
  165. std::vector<int> selected;
  166. for(size_t i=0; i<num_faces; i++)
  167. {
  168. auto should_keep = keep(Wr(i,0), Wr(i,1));
  169. if (should_keep > 0)
  170. {
  171. selected.push_back(index_to_signed_index(i, true));
  172. } else if (should_keep < 0)
  173. {
  174. selected.push_back(index_to_signed_index(i, false));
  175. }
  176. }
  177. const size_t num_selected = selected.size();
  178. DerivedFC kept_faces(num_selected, 3);
  179. DerivedJ kept_face_indices(num_selected, 1);
  180. for (size_t i=0; i<num_selected; i++)
  181. {
  182. size_t idx = abs(selected[i]) - 1;
  183. if (selected[i] > 0)
  184. {
  185. kept_faces.row(i) = F.row(idx);
  186. } else
  187. {
  188. kept_faces.row(i) = F.row(idx).reverse();
  189. }
  190. kept_face_indices(i, 0) = CJ[idx];
  191. }
  192. #ifdef MESH_BOOLEAN_TIMING
  193. log_time("extract_output");
  194. #endif
  195. // Finally, remove duplicated faces and unreferenced vertices.
  196. {
  197. DerivedFC G;
  198. DerivedJ JJ;
  199. igl::resolve_duplicated_faces(kept_faces, G, JJ);
  200. igl::slice(kept_face_indices, JJ, 1, J);
  201. #ifdef DOUBLE_CHECK_EXACT_OUTPUT
  202. {
  203. // Sanity check on exact output.
  204. igl::copyleft::cgal::RemeshSelfIntersectionsParam params;
  205. params.detect_only = true;
  206. params.first_only = true;
  207. MatrixXES dummy_VV;
  208. DerivedFC dummy_FF, dummy_IF;
  209. Eigen::VectorXi dummy_J, dummy_IM;
  210. igl::copyleft::cgal::SelfIntersectMesh<
  211. Kernel,
  212. MatrixXES, DerivedFC,
  213. MatrixXES, DerivedFC,
  214. DerivedFC,
  215. Eigen::VectorXi,
  216. Eigen::VectorXi
  217. > checker(V, G, params,
  218. dummy_VV, dummy_FF, dummy_IF, dummy_J, dummy_IM);
  219. if (checker.count != 0)
  220. {
  221. throw "Self-intersection not fully resolved.";
  222. }
  223. }
  224. #endif
  225. MatrixX3S Vs(V.rows(), V.cols());
  226. for (size_t i=0; i<(size_t)V.rows(); i++)
  227. {
  228. for (size_t j=0; j<(size_t)V.cols(); j++)
  229. {
  230. igl::copyleft::cgal::assign_scalar(V(i,j), Vs(i,j));
  231. }
  232. }
  233. Eigen::VectorXi newIM;
  234. igl::remove_unreferenced(Vs,G,VC,FC,newIM);
  235. }
  236. #ifdef MESH_BOOLEAN_TIMING
  237. log_time("clean_up");
  238. #endif
  239. return valid;
  240. }
  241. template <
  242. typename DerivedVA,
  243. typename DerivedFA,
  244. typename DerivedVB,
  245. typename DerivedFB,
  246. typename ResolveFunc,
  247. typename DerivedVC,
  248. typename DerivedFC,
  249. typename DerivedJ>
  250. IGL_INLINE bool igl::copyleft::boolean::mesh_boolean(
  251. const Eigen::PlainObjectBase<DerivedVA > & VA,
  252. const Eigen::PlainObjectBase<DerivedFA > & FA,
  253. const Eigen::PlainObjectBase<DerivedVB > & VB,
  254. const Eigen::PlainObjectBase<DerivedFB > & FB,
  255. const MeshBooleanType & type,
  256. const ResolveFunc& resolve_func,
  257. Eigen::PlainObjectBase<DerivedVC > & VC,
  258. Eigen::PlainObjectBase<DerivedFC > & FC,
  259. Eigen::PlainObjectBase<DerivedJ > & J)
  260. {
  261. switch (type)
  262. {
  263. case MESH_BOOLEAN_TYPE_UNION:
  264. return igl::copyleft::boolean::mesh_boolean(
  265. VA, FA, VB, FB, igl::copyleft::boolean::BinaryUnion(),
  266. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  267. case MESH_BOOLEAN_TYPE_INTERSECT:
  268. return igl::copyleft::boolean::mesh_boolean(
  269. VA, FA, VB, FB, igl::copyleft::boolean::BinaryIntersect(),
  270. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  271. case MESH_BOOLEAN_TYPE_MINUS:
  272. return igl::copyleft::boolean::mesh_boolean(
  273. VA, FA, VB, FB, igl::copyleft::boolean::BinaryMinus(),
  274. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  275. case MESH_BOOLEAN_TYPE_XOR:
  276. return igl::copyleft::boolean::mesh_boolean(
  277. VA, FA, VB, FB, igl::copyleft::boolean::BinaryXor(),
  278. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  279. case MESH_BOOLEAN_TYPE_RESOLVE:
  280. //op = binary_resolve();
  281. return igl::copyleft::boolean::mesh_boolean(
  282. VA, FA, VB, FB, igl::copyleft::boolean::BinaryResolve(),
  283. igl::copyleft::boolean::KeepAll(), resolve_func, VC, FC, J);
  284. default:
  285. assert(false && "Unsupported boolean type.");
  286. return false;
  287. }
  288. }
  289. template <
  290. typename DerivedVA,
  291. typename DerivedFA,
  292. typename DerivedVB,
  293. typename DerivedFB,
  294. typename DerivedVC,
  295. typename DerivedFC,
  296. typename DerivedJ>
  297. IGL_INLINE bool igl::copyleft::boolean::mesh_boolean(
  298. const Eigen::PlainObjectBase<DerivedVA > & VA,
  299. const Eigen::PlainObjectBase<DerivedFA > & FA,
  300. const Eigen::PlainObjectBase<DerivedVB > & VB,
  301. const Eigen::PlainObjectBase<DerivedFB > & FB,
  302. const MeshBooleanType & type,
  303. Eigen::PlainObjectBase<DerivedVC > & VC,
  304. Eigen::PlainObjectBase<DerivedFC > & FC,
  305. Eigen::PlainObjectBase<DerivedJ > & J)
  306. {
  307. typedef CGAL::Epeck Kernel;
  308. typedef Kernel::FT ExactScalar;
  309. typedef Eigen::Matrix<
  310. ExactScalar,
  311. Eigen::Dynamic,
  312. Eigen::Dynamic,
  313. DerivedVC::IsRowMajor> MatrixXES;
  314. std::function<void(
  315. const Eigen::PlainObjectBase<DerivedVA>&,
  316. const Eigen::PlainObjectBase<DerivedFA>&,
  317. Eigen::PlainObjectBase<MatrixXES>&,
  318. Eigen::PlainObjectBase<DerivedFC>&,
  319. Eigen::PlainObjectBase<DerivedJ>&)> resolve_func =
  320. [](const Eigen::PlainObjectBase<DerivedVA>& V,
  321. const Eigen::PlainObjectBase<DerivedFA>& F,
  322. Eigen::PlainObjectBase<MatrixXES>& Vo,
  323. Eigen::PlainObjectBase<DerivedFC>& Fo,
  324. Eigen::PlainObjectBase<DerivedJ>& J) {
  325. Eigen::VectorXi I;
  326. igl::copyleft::cgal::RemeshSelfIntersectionsParam params;
  327. params.stitch_all = true;
  328. MatrixXES Vr;
  329. DerivedFC Fr;
  330. Eigen::MatrixXi IF;
  331. igl::copyleft::cgal::remesh_self_intersections(
  332. V, F, params, Vr, Fr, IF, J, I);
  333. assert(I.size() == Vr.rows());
  334. // Merge coinciding vertices into non-manifold vertices.
  335. std::for_each(Fr.data(), Fr.data()+Fr.size(),
  336. [&I](typename DerivedFC::Scalar& a) { a=I[a]; });
  337. // Remove unreferenced vertices.
  338. Eigen::VectorXi UIM;
  339. igl::remove_unreferenced(Vr, Fr, Vo, Fo, UIM);
  340. };
  341. return mesh_boolean(VA,FA,VB,FB,type,resolve_func,VC,FC,J);
  342. }
  343. template <
  344. typename DerivedVA,
  345. typename DerivedFA,
  346. typename DerivedVB,
  347. typename DerivedFB,
  348. typename DerivedVC,
  349. typename DerivedFC>
  350. IGL_INLINE bool igl::copyleft::boolean::mesh_boolean(
  351. const Eigen::PlainObjectBase<DerivedVA > & VA,
  352. const Eigen::PlainObjectBase<DerivedFA > & FA,
  353. const Eigen::PlainObjectBase<DerivedVB > & VB,
  354. const Eigen::PlainObjectBase<DerivedFB > & FB,
  355. const MeshBooleanType & type,
  356. Eigen::PlainObjectBase<DerivedVC > & VC,
  357. Eigen::PlainObjectBase<DerivedFC > & FC)
  358. {
  359. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  360. return igl::copyleft::boolean::mesh_boolean(VA,FA,VB,FB,type,VC,FC,J);
  361. }
  362. #ifdef IGL_STATIC_LIBRARY
  363. // Explicit template specialization
  364. template bool 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> >&);
  365. template bool 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> >&);
  366. template bool 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> >&);
  367. #undef IGL_STATIC_LIBRARY
  368. #include "../../remove_unreferenced.cpp"
  369. 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> >&);
  370. #include "../../slice.cpp"
  371. 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> >&);
  372. #endif