mesh_boolean.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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((size_t)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<(size_t)V.rows(); i++)
  127. {
  128. for (size_t j=0; j<(size_t)V.cols(); j++)
  129. {
  130. igl::copyleft::cgal::assign_scalar(V(i,j), Vs(i,j));
  131. }
  132. }
  133. Eigen::VectorXi newIM;
  134. igl::remove_unreferenced(Vs,G,VC,FC,newIM);
  135. }
  136. }
  137. template <
  138. typename DerivedVA,
  139. typename DerivedFA,
  140. typename DerivedVB,
  141. typename DerivedFB,
  142. typename ResolveFunc,
  143. typename DerivedVC,
  144. typename DerivedFC,
  145. typename DerivedJ>
  146. IGL_INLINE void igl::copyleft::boolean::mesh_boolean(
  147. const Eigen::PlainObjectBase<DerivedVA > & VA,
  148. const Eigen::PlainObjectBase<DerivedFA > & FA,
  149. const Eigen::PlainObjectBase<DerivedVB > & VB,
  150. const Eigen::PlainObjectBase<DerivedFB > & FB,
  151. const MeshBooleanType & type,
  152. const ResolveFunc& resolve_func,
  153. Eigen::PlainObjectBase<DerivedVC > & VC,
  154. Eigen::PlainObjectBase<DerivedFC > & FC,
  155. Eigen::PlainObjectBase<DerivedJ > & J)
  156. {
  157. typedef CGAL::Epeck Kernel;
  158. typedef Kernel::FT ExactScalar;
  159. typedef Eigen::Matrix<
  160. ExactScalar,
  161. Eigen::Dynamic,
  162. Eigen::Dynamic,
  163. DerivedVC::IsRowMajor> MatrixXES;
  164. switch (type) {
  165. case MESH_BOOLEAN_TYPE_UNION:
  166. igl::copyleft::boolean::mesh_boolean(
  167. VA, FA, VB, FB, igl::copyleft::boolean::BinaryUnion(),
  168. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  169. break;
  170. case MESH_BOOLEAN_TYPE_INTERSECT:
  171. igl::copyleft::boolean::mesh_boolean(
  172. VA, FA, VB, FB, igl::copyleft::boolean::BinaryIntersect(),
  173. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  174. break;
  175. case MESH_BOOLEAN_TYPE_MINUS:
  176. igl::copyleft::boolean::mesh_boolean(
  177. VA, FA, VB, FB, igl::copyleft::boolean::BinaryMinus(),
  178. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  179. break;
  180. case MESH_BOOLEAN_TYPE_XOR:
  181. igl::copyleft::boolean::mesh_boolean(
  182. VA, FA, VB, FB, igl::copyleft::boolean::BinaryXor(),
  183. igl::copyleft::boolean::KeepInside(), resolve_func, VC, FC, J);
  184. break;
  185. case MESH_BOOLEAN_TYPE_RESOLVE:
  186. //op = binary_resolve();
  187. igl::copyleft::boolean::mesh_boolean(
  188. VA, FA, VB, FB, igl::copyleft::boolean::BinaryResolve(),
  189. igl::copyleft::boolean::KeepAll(), resolve_func, VC, FC, J);
  190. break;
  191. default:
  192. throw std::runtime_error("Unsupported boolean type.");
  193. }
  194. }
  195. template <
  196. typename DerivedVA,
  197. typename DerivedFA,
  198. typename DerivedVB,
  199. typename DerivedFB,
  200. typename DerivedVC,
  201. typename DerivedFC,
  202. typename DerivedJ>
  203. IGL_INLINE void igl::copyleft::boolean::mesh_boolean(
  204. const Eigen::PlainObjectBase<DerivedVA > & VA,
  205. const Eigen::PlainObjectBase<DerivedFA > & FA,
  206. const Eigen::PlainObjectBase<DerivedVB > & VB,
  207. const Eigen::PlainObjectBase<DerivedFB > & FB,
  208. const MeshBooleanType & type,
  209. Eigen::PlainObjectBase<DerivedVC > & VC,
  210. Eigen::PlainObjectBase<DerivedFC > & FC,
  211. Eigen::PlainObjectBase<DerivedJ > & J)
  212. {
  213. typedef CGAL::Epeck Kernel;
  214. typedef Kernel::FT ExactScalar;
  215. typedef Eigen::Matrix<
  216. ExactScalar,
  217. Eigen::Dynamic,
  218. Eigen::Dynamic,
  219. DerivedVC::IsRowMajor> MatrixXES;
  220. std::function<void(
  221. const Eigen::PlainObjectBase<DerivedVA>&,
  222. const Eigen::PlainObjectBase<DerivedFA>&,
  223. Eigen::PlainObjectBase<MatrixXES>&,
  224. Eigen::PlainObjectBase<DerivedFC>&,
  225. Eigen::PlainObjectBase<DerivedJ>&)> resolve_func =
  226. [](const Eigen::PlainObjectBase<DerivedVA>& V,
  227. const Eigen::PlainObjectBase<DerivedFA>& F,
  228. Eigen::PlainObjectBase<MatrixXES>& Vo,
  229. Eigen::PlainObjectBase<DerivedFC>& Fo,
  230. Eigen::PlainObjectBase<DerivedJ>& J) {
  231. Eigen::VectorXi I;
  232. igl::copyleft::cgal::RemeshSelfIntersectionsParam params;
  233. MatrixXES Vr;
  234. DerivedFC Fr;
  235. Eigen::MatrixXi IF;
  236. igl::copyleft::cgal::remesh_self_intersections(
  237. V, F, params, Vr, Fr, IF, J, I);
  238. assert(I.size() == Vr.rows());
  239. // Merge coinciding vertices into non-manifold vertices.
  240. std::for_each(Fr.data(), Fr.data()+Fr.size(),
  241. [&I](typename DerivedFC::Scalar& a) { a=I[a]; });
  242. // Remove unreferenced vertices.
  243. Eigen::VectorXi UIM;
  244. igl::remove_unreferenced(Vr, Fr, Vo, Fo, UIM);
  245. };
  246. return mesh_boolean(VA,FA,VB,FB,type,resolve_func,VC,FC,J);
  247. }
  248. template <
  249. typename DerivedVA,
  250. typename DerivedFA,
  251. typename DerivedVB,
  252. typename DerivedFB,
  253. typename DerivedVC,
  254. typename DerivedFC>
  255. IGL_INLINE void igl::copyleft::boolean::mesh_boolean(
  256. const Eigen::PlainObjectBase<DerivedVA > & VA,
  257. const Eigen::PlainObjectBase<DerivedFA > & FA,
  258. const Eigen::PlainObjectBase<DerivedVB > & VB,
  259. const Eigen::PlainObjectBase<DerivedFB > & FB,
  260. const MeshBooleanType & type,
  261. Eigen::PlainObjectBase<DerivedVC > & VC,
  262. Eigen::PlainObjectBase<DerivedFC > & FC) {
  263. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  264. return igl::copyleft::boolean::mesh_boolean(VA,FA,VB,FB,type,VC,FC,J);
  265. }
  266. #ifdef IGL_STATIC_LIBRARY
  267. // Explicit template specialization
  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::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> >&);
  269. 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> >&);
  270. 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> >&);
  271. #undef IGL_STATIC_LIBRARY
  272. #include "../../remove_unreferenced.cpp"
  273. 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> >&);
  274. #include "../../slice.cpp"
  275. 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> >&);
  276. #endif