subdivide_segments.cpp 5.5 KB

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