subdivide_segments.cpp 3.7 KB

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