mesh_boolean.cpp 15 KB

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