mesh_boolean_beta.cpp 15 KB

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