outer_hull.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[email protected]>
  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. #include "outer_hull.h"
  9. #include "extract_cells.h"
  10. #include "remesh_self_intersections.h"
  11. #include "assign.h"
  12. #include "../../remove_unreferenced.h"
  13. #include <CGAL/AABB_tree.h>
  14. #include <CGAL/AABB_traits.h>
  15. #include <CGAL/AABB_triangle_primitive.h>
  16. #include <CGAL/intersections.h>
  17. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  18. template <
  19. typename DerivedV,
  20. typename DerivedF,
  21. typename DerivedHV,
  22. typename DerivedHF,
  23. typename DerivedJ,
  24. typename Derivedflip>
  25. IGL_INLINE void igl::copyleft::cgal::outer_hull(
  26. const Eigen::PlainObjectBase<DerivedV> & V,
  27. const Eigen::PlainObjectBase<DerivedF> & F,
  28. Eigen::PlainObjectBase<DerivedHV> & HV,
  29. Eigen::PlainObjectBase<DerivedHF> & HF,
  30. Eigen::PlainObjectBase<DerivedJ> & J,
  31. Eigen::PlainObjectBase<Derivedflip> & flip)
  32. {
  33. // Exact types
  34. typedef CGAL::Epeck Kernel;
  35. typedef Kernel::FT ExactScalar;
  36. typedef
  37. Eigen::Matrix<
  38. ExactScalar,
  39. Eigen::Dynamic,
  40. Eigen::Dynamic,
  41. DerivedHV::IsRowMajor>
  42. MatrixXES;
  43. // Remesh self-intersections
  44. MatrixXES Vr;
  45. DerivedHF Fr;
  46. DerivedJ Jr;
  47. {
  48. RemeshSelfIntersectionsParam params;
  49. params.stitch_all = true;
  50. Eigen::VectorXi I;
  51. Eigen::MatrixXi IF;
  52. remesh_self_intersections(V, F, params, Vr, Fr, IF, Jr, I);
  53. // Merge coinciding vertices into non-manifold vertices.
  54. std::for_each(Fr.data(), Fr.data()+Fr.size(),
  55. [&I](typename DerivedHF::Scalar& a) { a=I[a]; });
  56. // Remove unreferenced vertices.
  57. Eigen::VectorXi UIM;
  58. remove_unreferenced(MatrixXES(Vr),DerivedHF(Fr), Vr, Fr, UIM);
  59. }
  60. // Extract cells for each face
  61. Eigen::MatrixXi C;
  62. extract_cells(Vr,Fr,C);
  63. // Extract faces on ambient cell
  64. int num_outer = 0;
  65. for(int i = 0;i<C.rows();i++)
  66. {
  67. num_outer += ( C(i,0) == 0 || C(i,1) == 0 ) ? 1 : 0;
  68. }
  69. HF.resize(num_outer,3);
  70. J.resize(num_outer,1);
  71. flip.resize(num_outer,1);
  72. {
  73. int h = 0;
  74. for(int i = 0;i<C.rows();i++)
  75. {
  76. if(C(i,0)==0)
  77. {
  78. HF.row(h) = Fr.row(i);
  79. J(h) = Jr(i);
  80. flip(h) = false;
  81. h++;
  82. }else if(C(i,1) == 0)
  83. {
  84. HF.row(h) = Fr.row(i).reverse();
  85. J(h) = Jr(i);
  86. flip(h) = true;
  87. h++;
  88. }
  89. }
  90. assert(h == num_outer);
  91. }
  92. // Remove unreferenced vertices and re-index faces
  93. {
  94. // Cast to output type
  95. DerivedHV Vr_cast;
  96. assign(Vr,Vr_cast);
  97. Eigen::VectorXi I;
  98. remove_unreferenced(Vr_cast,DerivedHF(HF),HV,HF,I);
  99. }
  100. }
  101. #ifdef IGL_STATIC_LIBRARY
  102. // Explicit template instantiation
  103. template void igl::copyleft::cgal::outer_hull<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::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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -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> >&);
  104. #ifdef WIN32
  105. #endif
  106. #endif