mesh_boolean.cpp 14 KB

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