mesh_boolean.cpp 14 KB

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