mesh_boolean.cpp 15 KB

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