mesh_boolean.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Qingnan Zhou <qnzhou@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. //
  9. #include "mesh_boolean.h"
  10. #include <igl/cgal/assign_scalar.h>
  11. #include <igl/cgal/propagate_winding_numbers.h>
  12. #include <igl/cgal/remesh_self_intersections.h>
  13. #include <igl/remove_unreferenced.h>
  14. #include <igl/unique_simplices.h>
  15. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  16. #include "../writePLY.h"
  17. #include "../writeDMAT.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. typedef Eigen::Matrix<int, 1, Eigen::Dynamic> WindingNumbers;
  152. typedef std::function<int(const WindingNumbers&)> WindingNumberOperation;
  153. WindingNumberOperation binary_union() {
  154. return [](const WindingNumbers& win_nums) -> int{
  155. return win_nums[0] > 0 || win_nums[1] > 0;
  156. };
  157. }
  158. WindingNumberOperation binary_intersect() {
  159. return [](const WindingNumbers& win_nums) -> int{
  160. return win_nums[0] > 0 && win_nums[1] > 0;
  161. };
  162. }
  163. WindingNumberOperation binary_difference() {
  164. return [](const WindingNumbers& win_nums) -> int{
  165. return win_nums[0] > 0 && win_nums[1] <= 0;
  166. };
  167. }
  168. WindingNumberOperation binary_xor() {
  169. return [](const WindingNumbers& win_nums) -> int{
  170. return (win_nums[0] > 0 && win_nums[1] <= 0) ||
  171. (win_nums[0] <= 0 && win_nums[1] > 0);
  172. };
  173. }
  174. WindingNumberOperation binary_resolve() {
  175. return [](const WindingNumbers& win_nums) -> int{
  176. return true;
  177. };
  178. }
  179. typedef std::function<short(int, int)> ToKeepFunc;
  180. ToKeepFunc keep_inside() {
  181. return [](int out_w, int in_w) -> short {
  182. if (in_w > 0 && out_w <= 0) return 1;
  183. else if (in_w <= 0 && out_w > 0) return -1;
  184. else return 0;
  185. };
  186. }
  187. ToKeepFunc keep_all() {
  188. return [](int out_w, int in_w) -> short {
  189. return true;
  190. };
  191. }
  192. }
  193. }
  194. }
  195. template <
  196. typename DerivedVA,
  197. typename DerivedFA,
  198. typename DerivedVB,
  199. typename DerivedFB,
  200. typename WindingNumberOp,
  201. typename KeepFunc,
  202. typename ResolveFunc,
  203. typename DerivedVC,
  204. typename DerivedFC,
  205. typename DerivedJ>
  206. IGL_INLINE void igl::boolean::per_face_winding_number_binary_operation(
  207. const Eigen::PlainObjectBase<DerivedVA> & VA,
  208. const Eigen::PlainObjectBase<DerivedFA> & FA,
  209. const Eigen::PlainObjectBase<DerivedVB> & VB,
  210. const Eigen::PlainObjectBase<DerivedFB> & FB,
  211. const WindingNumberOp& wind_num_op,
  212. const KeepFunc& keep,
  213. const ResolveFunc& resolve_fun,
  214. Eigen::PlainObjectBase<DerivedVC > & VC,
  215. Eigen::PlainObjectBase<DerivedFC > & FC,
  216. Eigen::PlainObjectBase<DerivedJ > & J) {
  217. using namespace igl::boolean::mesh_boolean_helper;
  218. typedef typename DerivedVC::Scalar Scalar;
  219. typedef typename DerivedFC::Scalar Index;
  220. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
  221. typedef Eigen::Matrix<Index,Eigen::Dynamic,Eigen::Dynamic> MatrixXI;
  222. typedef Eigen::Matrix<typename DerivedJ::Scalar,Eigen::Dynamic,1> VectorXJ;
  223. // Generate combined mesh.
  224. typedef Eigen::Matrix<
  225. ExactScalar,
  226. Eigen::Dynamic,
  227. Eigen::Dynamic,
  228. DerivedVC::IsRowMajor> MatrixXES;
  229. MatrixXES V;
  230. DerivedFC F;
  231. VectorXJ CJ;
  232. resolve_intersections(VA, FA, VB, FB, resolve_fun, V, F, CJ);
  233. // Compute winding numbers on each side of each facet.
  234. const size_t num_faces = F.rows();
  235. Eigen::MatrixXi W;
  236. Eigen::VectorXi labels(num_faces);
  237. std::transform(CJ.data(), CJ.data()+CJ.size(), labels.data(),
  238. [&](int i) { return i<FA.rows() ? 0:1; });
  239. igl::cgal::propagate_winding_numbers_beta(V, F, labels, W);
  240. assert(W.rows() == num_faces);
  241. if (W.cols() == 2) {
  242. assert(FB.rows() == 0);
  243. Eigen::MatrixXi W_tmp(num_faces, 4);
  244. W_tmp << W, Eigen::MatrixXi::Zero(num_faces, 2);
  245. W = W_tmp;
  246. } else {
  247. assert(W.cols() == 4);
  248. }
  249. // Compute resulting winding number.
  250. Eigen::MatrixXi Wr(num_faces, 2);
  251. for (size_t i=0; i<num_faces; i++) {
  252. Eigen::MatrixXi w_out(1,2), w_in(1,2);
  253. w_out << W(i,0), W(i,2);
  254. w_in << W(i,1), W(i,3);
  255. Wr(i,0) = wind_num_op(w_out);
  256. Wr(i,1) = wind_num_op(w_in);
  257. }
  258. // Extract boundary separating inside from outside.
  259. auto index_to_signed_index = [&](size_t i, bool ori) -> int{
  260. return (i+1)*(ori?1:-1);
  261. };
  262. auto signed_index_to_index = [&](int i) -> size_t {
  263. return abs(i) - 1;
  264. };
  265. std::vector<int> selected;
  266. for(size_t i=0; i<num_faces; i++) {
  267. auto should_keep = keep(Wr(i,0), Wr(i,1));
  268. if (should_keep > 0) {
  269. selected.push_back(index_to_signed_index(i, true));
  270. } else if (should_keep < 0) {
  271. selected.push_back(index_to_signed_index(i, false));
  272. }
  273. }
  274. const size_t num_selected = selected.size();
  275. DerivedFC kept_faces(num_selected, 3);
  276. DerivedJ kept_face_indices;
  277. kept_face_indices.resize(num_selected, 1);
  278. for (size_t i=0; i<num_selected; i++) {
  279. size_t idx = abs(selected[i]) - 1;
  280. if (selected[i] > 0) {
  281. kept_faces.row(i) = F.row(idx);
  282. } else {
  283. kept_faces.row(i) = F.row(idx).reverse();
  284. }
  285. kept_face_indices(i, 0) = CJ[idx];
  286. }
  287. // Finally, remove duplicated faces and unreferenced vertices.
  288. {
  289. DerivedFC G;
  290. DerivedJ J;
  291. resolve_duplicated_faces(kept_faces, kept_face_indices, G, J);
  292. MatrixX3S Vs(V.rows(), V.cols());
  293. for (size_t i=0; i<V.rows(); i++) {
  294. for (size_t j=0; j<V.cols(); j++) {
  295. igl::cgal::assign_scalar(V(i,j), Vs(i,j));
  296. }
  297. }
  298. Eigen::VectorXi newIM;
  299. igl::remove_unreferenced(Vs,G,VC,FC,newIM);
  300. }
  301. }
  302. template <
  303. typename DerivedVA,
  304. typename DerivedFA,
  305. typename DerivedVB,
  306. typename DerivedFB,
  307. typename DerivedVC,
  308. typename DerivedFC,
  309. typename DerivedJ>
  310. IGL_INLINE void igl::boolean::mesh_boolean(
  311. const Eigen::PlainObjectBase<DerivedVA > & VA,
  312. const Eigen::PlainObjectBase<DerivedFA > & FA,
  313. const Eigen::PlainObjectBase<DerivedVB > & VB,
  314. const Eigen::PlainObjectBase<DerivedFB > & FB,
  315. const MeshBooleanType & type,
  316. Eigen::PlainObjectBase<DerivedVC > & VC,
  317. Eigen::PlainObjectBase<DerivedFC > & FC,
  318. Eigen::PlainObjectBase<DerivedJ > & J) {
  319. using namespace igl::boolean::mesh_boolean_helper;
  320. WindingNumberOperation op;
  321. ToKeepFunc keep;
  322. switch (type) {
  323. case MESH_BOOLEAN_TYPE_UNION:
  324. op = binary_union();
  325. keep = keep_inside();
  326. break;
  327. case MESH_BOOLEAN_TYPE_INTERSECT:
  328. op = binary_intersect();
  329. keep = keep_inside();
  330. break;
  331. case MESH_BOOLEAN_TYPE_MINUS:
  332. op = binary_difference();
  333. keep = keep_inside();
  334. break;
  335. case MESH_BOOLEAN_TYPE_XOR:
  336. op = binary_xor();
  337. keep = keep_inside();
  338. break;
  339. case MESH_BOOLEAN_TYPE_RESOLVE:
  340. op = binary_resolve();
  341. keep = keep_all();
  342. break;
  343. default:
  344. throw std::runtime_error("Unsupported boolean type.");
  345. }
  346. typedef Eigen::Matrix<
  347. ExactScalar,
  348. Eigen::Dynamic,
  349. Eigen::Dynamic,
  350. DerivedVC::IsRowMajor> MatrixXES;
  351. std::function<void(
  352. const Eigen::PlainObjectBase<DerivedVA>&,
  353. const Eigen::PlainObjectBase<DerivedFA>&,
  354. Eigen::PlainObjectBase<MatrixXES>&,
  355. Eigen::PlainObjectBase<DerivedFC>&,
  356. Eigen::PlainObjectBase<DerivedJ>&)> resolve_func =
  357. igl_resolve<DerivedVA, DerivedFA, MatrixXES, DerivedFC, DerivedJ>;
  358. igl::boolean::per_face_winding_number_binary_operation(
  359. VA, FA, VB, FB, op, keep, resolve_func, VC, FC, J);
  360. }
  361. template <
  362. typename DerivedVA,
  363. typename DerivedFA,
  364. typename DerivedVB,
  365. typename DerivedFB,
  366. typename DerivedVC,
  367. typename DerivedFC>
  368. IGL_INLINE void igl::boolean::mesh_boolean(
  369. const Eigen::PlainObjectBase<DerivedVA > & VA,
  370. const Eigen::PlainObjectBase<DerivedFA > & FA,
  371. const Eigen::PlainObjectBase<DerivedVB > & VB,
  372. const Eigen::PlainObjectBase<DerivedFB > & FB,
  373. const MeshBooleanType & type,
  374. Eigen::PlainObjectBase<DerivedVC > & VC,
  375. Eigen::PlainObjectBase<DerivedFC > & FC) {
  376. Eigen::Matrix<typename DerivedFC::Index, Eigen::Dynamic,1> J;
  377. return igl::boolean::mesh_boolean(VA,FA,VB,FB,type,VC,FC,J);
  378. }
  379. #ifdef IGL_STATIC_LIBRARY
  380. // Explicit template specialization
  381. 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> >&);
  382. #endif