mesh_boolean.cpp 15 KB

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