mesh_boolean.cpp 18 KB

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