mesh_boolean.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 winding numbers on each side of each facet.
  88. const size_t num_faces = F.rows();
  89. Eigen::MatrixXi W;
  90. Eigen::VectorXi labels(num_faces);
  91. std::transform(CJ.data(), CJ.data()+CJ.size(), labels.data(),
  92. [&](int i) { return i<FA.rows() ? 0:1; });
  93. bool valid = true;
  94. if (num_faces > 0)
  95. {
  96. valid = valid &
  97. igl::copyleft::cgal::propagate_winding_numbers(V, F, labels, W);
  98. } else
  99. {
  100. W.resize(0, 4);
  101. }
  102. assert((size_t)W.rows() == num_faces);
  103. if (W.cols() == 2)
  104. {
  105. assert(FB.rows() == 0);
  106. Eigen::MatrixXi W_tmp(num_faces, 4);
  107. W_tmp << W, Eigen::MatrixXi::Zero(num_faces, 2);
  108. W = W_tmp;
  109. } else {
  110. assert(W.cols() == 4);
  111. }
  112. #ifdef MESH_BOOLEAN_TIMING
  113. log_time("propagate_input_winding_number");
  114. #endif
  115. // Compute resulting winding number.
  116. Eigen::MatrixXi Wr(num_faces, 2);
  117. for (size_t i=0; i<num_faces; i++)
  118. {
  119. Eigen::MatrixXi w_out(1,2), w_in(1,2);
  120. w_out << W(i,0), W(i,2);
  121. w_in << W(i,1), W(i,3);
  122. Wr(i,0) = wind_num_op(w_out);
  123. Wr(i,1) = wind_num_op(w_in);
  124. }
  125. #ifdef MESH_BOOLEAN_TIMING
  126. log_time("compute_output_winding_number");
  127. #endif
  128. // Extract boundary separating inside from outside.
  129. auto index_to_signed_index = [&](size_t i, bool ori) -> int
  130. {
  131. return (i+1)*(ori?1:-1);
  132. };
  133. //auto signed_index_to_index = [&](int i) -> size_t {
  134. // return abs(i) - 1;
  135. //};
  136. std::vector<int> selected;
  137. for(size_t i=0; i<num_faces; i++)
  138. {
  139. auto should_keep = keep(Wr(i,0), Wr(i,1));
  140. if (should_keep > 0)
  141. {
  142. selected.push_back(index_to_signed_index(i, true));
  143. } else if (should_keep < 0)
  144. {
  145. selected.push_back(index_to_signed_index(i, false));
  146. }
  147. }
  148. const size_t num_selected = selected.size();
  149. DerivedFC kept_faces(num_selected, 3);
  150. DerivedJ kept_face_indices(num_selected, 1);
  151. for (size_t i=0; i<num_selected; i++)
  152. {
  153. size_t idx = abs(selected[i]) - 1;
  154. if (selected[i] > 0)
  155. {
  156. kept_faces.row(i) = F.row(idx);
  157. } else
  158. {
  159. kept_faces.row(i) = F.row(idx).reverse();
  160. }
  161. kept_face_indices(i, 0) = CJ[idx];
  162. }
  163. #ifdef MESH_BOOLEAN_TIMING
  164. log_time("extract_output");
  165. #endif
  166. // Finally, remove duplicated faces and unreferenced vertices.
  167. {
  168. DerivedFC G;
  169. DerivedJ JJ;
  170. igl::resolve_duplicated_faces(kept_faces, G, JJ);
  171. igl::slice(kept_face_indices, JJ, 1, J);
  172. #ifdef DOUBLE_CHECK_EXACT_OUTPUT
  173. {
  174. // Sanity check on exact output.
  175. igl::copyleft::cgal::RemeshSelfIntersectionsParam params;
  176. params.detect_only = true;
  177. params.first_only = true;
  178. MatrixXES dummy_VV;
  179. DerivedFC dummy_FF, dummy_IF;
  180. Eigen::VectorXi dummy_J, dummy_IM;
  181. igl::copyleft::cgal::SelfIntersectMesh<
  182. Kernel,
  183. MatrixXES, DerivedFC,
  184. MatrixXES, DerivedFC,
  185. DerivedFC,
  186. Eigen::VectorXi,
  187. Eigen::VectorXi
  188. > checker(V, G, params,
  189. dummy_VV, dummy_FF, dummy_IF, dummy_J, dummy_IM);
  190. if (checker.count != 0)
  191. {
  192. throw "Self-intersection not fully resolved.";
  193. }
  194. }
  195. #endif
  196. MatrixX3S Vs(V.rows(), V.cols());
  197. for (size_t i=0; i<(size_t)V.rows(); i++)
  198. {
  199. for (size_t j=0; j<(size_t)V.cols(); j++)
  200. {
  201. igl::copyleft::cgal::assign_scalar(V(i,j), Vs(i,j));
  202. }
  203. }
  204. Eigen::VectorXi newIM;
  205. igl::remove_unreferenced(Vs,G,VC,FC,newIM);
  206. }
  207. #ifdef MESH_BOOLEAN_TIMING
  208. log_time("clean_up");
  209. #endif
  210. return valid;
  211. }
  212. template <
  213. typename DerivedVA,
  214. typename DerivedFA,
  215. typename DerivedVB,
  216. typename DerivedFB,
  217. typename ResolveFunc,
  218. typename DerivedVC,
  219. typename DerivedFC,
  220. typename DerivedJ>
  221. IGL_INLINE bool igl::copyleft::boolean::mesh_boolean(
  222. const Eigen::PlainObjectBase<DerivedVA > & VA,
  223. const Eigen::PlainObjectBase<DerivedFA > & FA,
  224. const Eigen::PlainObjectBase<DerivedVB > & VB,
  225. const Eigen::PlainObjectBase<DerivedFB > & FB,
  226. const MeshBooleanType & type,
  227. const ResolveFunc& resolve_func,
  228. Eigen::PlainObjectBase<DerivedVC > & VC,
  229. Eigen::PlainObjectBase<DerivedFC > & FC,
  230. Eigen::PlainObjectBase<DerivedJ > & J)
  231. {
  232. switch (type)
  233. {
  234. case MESH_BOOLEAN_TYPE_UNION:
  235. return igl::copyleft::boolean::mesh_boolean(
  236. VA, FA, VB, FB, igl::copyleft::boolean::BinaryUnion(),
  237. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  238. case MESH_BOOLEAN_TYPE_INTERSECT:
  239. return igl::copyleft::boolean::mesh_boolean(
  240. VA, FA, VB, FB, igl::copyleft::boolean::BinaryIntersect(),
  241. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  242. case MESH_BOOLEAN_TYPE_MINUS:
  243. return igl::copyleft::boolean::mesh_boolean(
  244. VA, FA, VB, FB, igl::copyleft::boolean::BinaryMinus(),
  245. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  246. case MESH_BOOLEAN_TYPE_XOR:
  247. return igl::copyleft::boolean::mesh_boolean(
  248. VA, FA, VB, FB, igl::copyleft::boolean::BinaryXor(),
  249. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  250. case MESH_BOOLEAN_TYPE_RESOLVE:
  251. //op = binary_resolve();
  252. return igl::copyleft::boolean::mesh_boolean(
  253. VA, FA, VB, FB, igl::copyleft::boolean::BinaryResolve(),
  254. igl::copyleft::boolean::KeepAll(), resolve_func, VC, FC, J);
  255. default:
  256. assert(false && "Unsupported boolean type.");
  257. return false;
  258. }
  259. }
  260. template <
  261. typename DerivedVA,
  262. typename DerivedFA,
  263. typename DerivedVB,
  264. typename DerivedFB,
  265. typename DerivedVC,
  266. typename DerivedFC,
  267. typename DerivedJ>
  268. IGL_INLINE bool igl::copyleft::boolean::mesh_boolean(
  269. const Eigen::PlainObjectBase<DerivedVA > & VA,
  270. const Eigen::PlainObjectBase<DerivedFA > & FA,
  271. const Eigen::PlainObjectBase<DerivedVB > & VB,
  272. const Eigen::PlainObjectBase<DerivedFB > & FB,
  273. const MeshBooleanType & type,
  274. Eigen::PlainObjectBase<DerivedVC > & VC,
  275. Eigen::PlainObjectBase<DerivedFC > & FC,
  276. Eigen::PlainObjectBase<DerivedJ > & J)
  277. {
  278. typedef CGAL::Epeck Kernel;
  279. typedef Kernel::FT ExactScalar;
  280. typedef Eigen::Matrix<
  281. ExactScalar,
  282. Eigen::Dynamic,
  283. Eigen::Dynamic,
  284. DerivedVC::IsRowMajor> MatrixXES;
  285. std::function<void(
  286. const Eigen::PlainObjectBase<DerivedVA>&,
  287. const Eigen::PlainObjectBase<DerivedFA>&,
  288. Eigen::PlainObjectBase<MatrixXES>&,
  289. Eigen::PlainObjectBase<DerivedFC>&,
  290. Eigen::PlainObjectBase<DerivedJ>&)> resolve_func =
  291. [](const Eigen::PlainObjectBase<DerivedVA>& V,
  292. const Eigen::PlainObjectBase<DerivedFA>& F,
  293. Eigen::PlainObjectBase<MatrixXES>& Vo,
  294. Eigen::PlainObjectBase<DerivedFC>& Fo,
  295. Eigen::PlainObjectBase<DerivedJ>& J) {
  296. Eigen::VectorXi I;
  297. igl::copyleft::cgal::RemeshSelfIntersectionsParam params;
  298. MatrixXES Vr;
  299. DerivedFC Fr;
  300. Eigen::MatrixXi IF;
  301. igl::copyleft::cgal::remesh_self_intersections(
  302. V, F, params, Vr, Fr, IF, J, I);
  303. assert(I.size() == Vr.rows());
  304. // Merge coinciding vertices into non-manifold vertices.
  305. std::for_each(Fr.data(), Fr.data()+Fr.size(),
  306. [&I](typename DerivedFC::Scalar& a) { a=I[a]; });
  307. // Remove unreferenced vertices.
  308. Eigen::VectorXi UIM;
  309. igl::remove_unreferenced(Vr, Fr, Vo, Fo, UIM);
  310. };
  311. return mesh_boolean(VA,FA,VB,FB,type,resolve_func,VC,FC,J);
  312. }
  313. template <
  314. typename DerivedVA,
  315. typename DerivedFA,
  316. typename DerivedVB,
  317. typename DerivedFB,
  318. typename DerivedVC,
  319. typename DerivedFC>
  320. IGL_INLINE bool igl::copyleft::boolean::mesh_boolean(
  321. const Eigen::PlainObjectBase<DerivedVA > & VA,
  322. const Eigen::PlainObjectBase<DerivedFA > & FA,
  323. const Eigen::PlainObjectBase<DerivedVB > & VB,
  324. const Eigen::PlainObjectBase<DerivedFB > & FB,
  325. const MeshBooleanType & type,
  326. Eigen::PlainObjectBase<DerivedVC > & VC,
  327. Eigen::PlainObjectBase<DerivedFC > & FC)
  328. {
  329. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  330. return igl::copyleft::boolean::mesh_boolean(VA,FA,VB,FB,type,VC,FC,J);
  331. }
  332. #ifdef IGL_STATIC_LIBRARY
  333. // Explicit template specialization
  334. 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> >&);
  335. 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> >&);
  336. 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> >&);
  337. #undef IGL_STATIC_LIBRARY
  338. #include "../../remove_unreferenced.cpp"
  339. 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> >&);
  340. #include "../../slice.cpp"
  341. 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> >&);
  342. #endif