mesh_boolean.cpp 18 KB

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