mesh_boolean.cpp 14 KB

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