ramer_douglas_peucker.cpp 4.9 KB

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