mesh_boolean.cpp 15 KB

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