mesh_boolean_beta.cpp 15 KB

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