subdivide_segments.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "subdivide_segments.h"
  2. #include "row_to_point.h"
  3. #include <igl/unique.h>
  4. #include <igl/list_to_matrix.h>
  5. #include <igl/copyleft/cgal/assign_scalar.h>
  6. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  7. #include <CGAL/Segment_2.h>
  8. #include <CGAL/Point_2.h>
  9. #include <algorithm>
  10. #include <vector>
  11. template <
  12. typename DerivedV,
  13. typename DerivedE,
  14. typename Kernel,
  15. typename DerivedVI,
  16. typename DerivedEI,
  17. typename DerivedJ,
  18. typename DerivedIM>
  19. IGL_INLINE void igl::copyleft::cgal::subdivide_segments(
  20. const Eigen::PlainObjectBase<DerivedV> & V,
  21. const Eigen::PlainObjectBase<DerivedE> & E,
  22. const std::vector<std::vector<CGAL::Point_2<Kernel> > > & _steiner,
  23. Eigen::PlainObjectBase<DerivedVI> & VI,
  24. Eigen::PlainObjectBase<DerivedEI> & EI,
  25. Eigen::PlainObjectBase<DerivedJ> & J,
  26. Eigen::PlainObjectBase<DerivedIM> & IM)
  27. {
  28. using namespace Eigen;
  29. using namespace igl;
  30. using namespace std;
  31. // Exact scalar type
  32. typedef Kernel K;
  33. typedef typename Kernel::FT EScalar;
  34. typedef CGAL::Segment_2<Kernel> Segment_2;
  35. typedef CGAL::Point_2<Kernel> Point_2;
  36. typedef Matrix<EScalar,Dynamic,Dynamic> MatrixXE;
  37. // non-const copy
  38. std::vector<std::vector<CGAL::Point_2<Kernel> > > steiner = _steiner;
  39. // Convert vertex positions to exact kernel
  40. MatrixXE VE(V.rows(),V.cols());
  41. for(int i = 0;i<V.rows();i++)
  42. {
  43. for(int j = 0;j<V.cols();j++)
  44. {
  45. VE(i,j) = V(i,j);
  46. }
  47. }
  48. // number of original vertices
  49. const int n = V.rows();
  50. // number of original segments
  51. const int m = E.rows();
  52. // now steiner contains lists of points (unsorted) for each edge. Sort them
  53. // and count total number of vertices and edges
  54. int ni = 0;
  55. int mi = 0;
  56. // new steiner points
  57. std::vector<Point_2> S;
  58. std::vector<std::vector<typename DerivedE::Scalar> > vEI;
  59. std::vector<typename DerivedJ::Scalar> vJ;
  60. for(int i = 0;i<m;i++)
  61. {
  62. {
  63. const Point_2 s = row_to_point<K>(VE,E(i,0));
  64. std::sort(
  65. steiner[i].begin(),
  66. steiner[i].end(),
  67. [&s](const Point_2 & A, const Point_2 & B)->bool
  68. {
  69. return (A-s).squared_length() < (B-s).squared_length();
  70. });
  71. }
  72. // remove duplicates
  73. steiner[i].erase(
  74. std::unique(steiner[i].begin(), steiner[i].end()),
  75. steiner[i].end());
  76. {
  77. int s = E(i,0);
  78. // legs to each steiner in order
  79. for(int j = 1;j<steiner[i].size()-1;j++)
  80. {
  81. int d = n+S.size();
  82. S.push_back(steiner[i][j]);
  83. vEI.push_back({s,d});
  84. vJ.push_back(i);
  85. s = d;
  86. }
  87. // don't forget last (which might only) leg
  88. vEI.push_back({s,E(i,1)});
  89. vJ.push_back(i);
  90. }
  91. }
  92. // potentially unnecessary copying ...
  93. VI.resize(n+S.size(),2);
  94. for(int i = 0;i<V.rows();i++)
  95. {
  96. for(int j = 0;j<V.cols();j++)
  97. {
  98. assign_scalar(V(i,j),VI(i,j));
  99. }
  100. }
  101. for(int i = 0;i<S.size();i++)
  102. {
  103. assign_scalar(S[i].x(),VI(n+i,0));
  104. assign_scalar(S[i].y(),VI(n+i,1));
  105. }
  106. list_to_matrix(vEI,EI);
  107. list_to_matrix(vJ,J);
  108. {
  109. // Find unique mapping
  110. std::vector<Point_2> vVES,_1;
  111. for(int i = 0;i<n;i++)
  112. {
  113. vVES.push_back(row_to_point<K>(VE,i));
  114. }
  115. vVES.insert(vVES.end(),S.begin(),S.end());
  116. std::vector<size_t> vA,vIM;
  117. igl::unique(vVES,_1,vA,vIM);
  118. // Push indices back into vVES
  119. for_each(vIM.data(),vIM.data()+vIM.size(),[&vA](size_t & i){i=vA[i];});
  120. list_to_matrix(vIM,IM);
  121. }
  122. }