ramer_douglas_peucker.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "ramer_douglas_peucker.h"
  2. #include "LinSpaced.h"
  3. #include "find.h"
  4. #include "list_to_matrix.h"
  5. #include "cumsum.h"
  6. #include "histc.h"
  7. #include "project_to_line.h"
  8. #include "EPS.h"
  9. template <typename DerivedP, typename DerivedS, typename DerivedJ>
  10. IGL_INLINE void igl::ramer_douglas_peucker(
  11. const Eigen::MatrixBase<DerivedP> & P,
  12. const typename DerivedP::Scalar tol,
  13. Eigen::PlainObjectBase<DerivedS> & S,
  14. Eigen::PlainObjectBase<DerivedJ> & J)
  15. {
  16. typedef typename DerivedP::Scalar Scalar;
  17. // number of vertices
  18. const int n = P.rows();
  19. // Trivial base case
  20. if(n <= 1)
  21. {
  22. J = DerivedJ::Zero(n);
  23. S = P;
  24. return;
  25. }
  26. Eigen::Array<bool,Eigen::Dynamic,1> I =
  27. Eigen::Array<bool,Eigen::Dynamic,1>::Constant(n,1,true);
  28. const auto stol = tol*tol;
  29. std::function<void(const int,const int)> simplify;
  30. simplify = [&I,&P,&stol,&simplify](const int ixs, const int ixe)->void
  31. {
  32. assert(ixe>ixs);
  33. Scalar sdmax = 0;
  34. typename Eigen::Matrix<Scalar,Eigen::Dynamic,1>::Index ixc = -1;
  35. if((ixe-ixs)>1)
  36. {
  37. Scalar sdes = (P.row(ixe)-P.row(ixs)).squaredNorm();
  38. Eigen::Matrix<Scalar,Eigen::Dynamic,1> sD;
  39. const auto & Pblock = P.block(ixs+1,0,((ixe+1)-ixs)-2,P.cols());
  40. if(sdes<=EPS<Scalar>())
  41. {
  42. sD = (Pblock.rowwise()-P.row(ixs)).rowwise().squaredNorm();
  43. }else
  44. {
  45. Eigen::Matrix<Scalar,Eigen::Dynamic,1> T;
  46. project_to_line(Pblock,P.row(ixs).eval(),P.row(ixe).eval(),T,sD);
  47. }
  48. sdmax = sD.maxCoeff(&ixc);
  49. // Index full P
  50. ixc = ixc+(ixs+1);
  51. }
  52. if(sdmax <= stol)
  53. {
  54. if(ixs != ixe-1)
  55. {
  56. I.block(ixs+1,0,((ixe+1)-ixs)-2,1).setConstant(false);
  57. }
  58. }else
  59. {
  60. simplify(ixs,ixc);
  61. simplify(ixc,ixe);
  62. }
  63. };
  64. simplify(0,n-1);
  65. igl::find(I,J);
  66. S = P(J.derived(),Eigen::placeholders::all);
  67. }
  68. template <
  69. typename DerivedP,
  70. typename DerivedS,
  71. typename DerivedJ,
  72. typename DerivedQ>
  73. IGL_INLINE void igl::ramer_douglas_peucker(
  74. const Eigen::MatrixBase<DerivedP> & P,
  75. const typename DerivedP::Scalar tol,
  76. Eigen::PlainObjectBase<DerivedS> & S,
  77. Eigen::PlainObjectBase<DerivedJ> & J,
  78. Eigen::PlainObjectBase<DerivedQ> & Q)
  79. {
  80. typedef typename DerivedP::Scalar Scalar;
  81. ramer_douglas_peucker(P,tol,S,J);
  82. const int n = P.rows();
  83. assert(n>=2 && "Curve should be at least 2 points");
  84. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> VectorXS;
  85. // distance traveled along high-res curve
  86. VectorXS L(n);
  87. L(0) = 0;
  88. L.block(1,0,n-1,1) = (P.bottomRows(n-1)-P.topRows(n-1)).rowwise().norm();
  89. // Give extra on end
  90. VectorXS T;
  91. cumsum(L,1,T);
  92. T.conservativeResize(T.size()+1);
  93. T(T.size()-1) = T(T.size()-2);
  94. // index of coarse point before each fine vertex
  95. Eigen::VectorXi B;
  96. {
  97. Eigen::VectorXi N;
  98. histc(igl::LinSpaced<Eigen::VectorXi >(n,0,n-1),J,N,B);
  99. }
  100. // Add extra point at end
  101. J.conservativeResize(J.size()+1);
  102. J(J.size()-1) = J(J.size()-2);
  103. Eigen::VectorXi s,d;
  104. // Find index in original list of "start" vertices
  105. s = J(B);
  106. // Find index in original list of "destination" vertices
  107. d = J(B.array()+1);
  108. // Parameter between start and destination is linear in arc-length
  109. VectorXS Ts = T(s);
  110. VectorXS Td = T(d);
  111. T = ((T.head(T.size()-1)-Ts).array()/(Td-Ts).array()).eval();
  112. for(int t =0;t<T.size();t++)
  113. {
  114. if(!std::isfinite(T(t)) || T(t)!=T(t))
  115. {
  116. T(t) = 0;
  117. }
  118. }
  119. DerivedS SB = S(B,Eigen::placeholders::all);
  120. Eigen::VectorXi MB = B.array()+1;
  121. for(int b = 0;b<MB.size();b++)
  122. {
  123. if(MB(b) >= S.rows())
  124. {
  125. MB(b) = S.rows()-1;
  126. }
  127. }
  128. DerivedS SMB = S(MB,Eigen::placeholders::all);
  129. Q = SB.array() + ((SMB.array()-SB.array()).colwise()*T.array());
  130. // Remove extra point at end
  131. J.conservativeResize(J.size()-1);
  132. }
  133. #ifdef IGL_STATIC_LIBRARY
  134. // Explicit template instantiation
  135. // generated by autoexplicit.sh
  136. template void igl::ramer_douglas_peucker<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  137. // generated by autoexplicit.sh
  138. template void igl::ramer_douglas_peucker<Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 2, 0, -1, 2> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const&, Eigen::Matrix<double, -1, 2, 0, -1, 2>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&);
  139. #endif