mesh_boolean.cpp 14 KB

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