mesh_boolean.cpp 14 KB

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