mesh_boolean.cpp 13 KB

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