screen_space_selection.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "screen_space_selection.h"
  2. #include "AABB.h"
  3. #include "PlainMatrix.h"
  4. #include "winding_number.h"
  5. #include "project.h"
  6. #include "unproject.h"
  7. #include "Hit.h"
  8. #include "parallel_for.h"
  9. template <
  10. typename DerivedV,
  11. typename DerivedF,
  12. typename DerivedM,
  13. typename DerivedN,
  14. typename DerivedO,
  15. typename Ltype,
  16. typename DerivedW,
  17. typename Deriveda>
  18. IGL_INLINE void igl::screen_space_selection(
  19. const Eigen::MatrixBase<DerivedV> & V,
  20. const Eigen::MatrixBase<DerivedF> & F,
  21. const igl::AABB<DerivedV, 3> & tree,
  22. const Eigen::MatrixBase<DerivedM>& model,
  23. const Eigen::MatrixBase<DerivedN>& proj,
  24. const Eigen::MatrixBase<DerivedO>& viewport,
  25. const std::vector<Eigen::Matrix<Ltype,1,2> > & L,
  26. Eigen::PlainObjectBase<DerivedW> & W,
  27. Eigen::PlainObjectBase<Deriveda> & and_visible)
  28. {
  29. typedef typename DerivedV::Scalar Scalar;
  30. screen_space_selection(V,model,proj,viewport,L,W);
  31. const Eigen::RowVector3d origin =
  32. (model.inverse().col(3)).head(3).template cast<Scalar>();
  33. igl::parallel_for(V.rows(),[&](const int i)
  34. {
  35. // Skip unselected points
  36. if(W(i)<0.5){ return; }
  37. igl::Hit<typename DerivedV::Scalar> hit;
  38. tree.intersect_ray(V,F,origin,V.row(i)-origin,hit);
  39. and_visible(i) = !(hit.t>1e-5 && hit.t<(1-1e-5));
  40. });
  41. }
  42. template <
  43. typename DerivedV,
  44. typename DerivedM,
  45. typename DerivedN,
  46. typename DerivedO,
  47. typename Ltype,
  48. typename DerivedW>
  49. IGL_INLINE void igl::screen_space_selection(
  50. const Eigen::MatrixBase<DerivedV> & V,
  51. const Eigen::MatrixBase<DerivedM>& model,
  52. const Eigen::MatrixBase<DerivedN>& proj,
  53. const Eigen::MatrixBase<DerivedO>& viewport,
  54. const std::vector<Eigen::Matrix<Ltype,1,2> > & L,
  55. Eigen::PlainObjectBase<DerivedW> & W)
  56. {
  57. typedef typename DerivedV::Scalar Scalar;
  58. Eigen::Matrix<Scalar,Eigen::Dynamic,2> P(L.size(),2);
  59. Eigen::Matrix<int,Eigen::Dynamic,2> E(L.size(),2);
  60. for(int i = 0;i<E.rows();i++)
  61. {
  62. P.row(i) = L[i].template cast<Scalar>();
  63. E(i,0) = i;
  64. E(i,1) = (i+1)%E.rows();
  65. }
  66. return screen_space_selection(V,model,proj,viewport,P,E,W);
  67. }
  68. template <
  69. typename DerivedV,
  70. typename DerivedM,
  71. typename DerivedN,
  72. typename DerivedO,
  73. typename DerivedP,
  74. typename DerivedE,
  75. typename DerivedW>
  76. IGL_INLINE void igl::screen_space_selection(
  77. const Eigen::MatrixBase<DerivedV> & V,
  78. const Eigen::MatrixBase<DerivedM>& model,
  79. const Eigen::MatrixBase<DerivedN>& proj,
  80. const Eigen::MatrixBase<DerivedO>& viewport,
  81. const Eigen::MatrixBase<DerivedP> & P,
  82. const Eigen::MatrixBase<DerivedE> & E,
  83. Eigen::PlainObjectBase<DerivedW> & W)
  84. {
  85. // project all mesh vertices to 2D
  86. PlainMatrix<DerivedV,Eigen::Dynamic,Eigen::Dynamic> V2;
  87. igl::project(V,model,proj,viewport,V2);
  88. // In 2D this uses O(N*M) naive algorithm.
  89. igl::winding_number(P,E,V2,W);
  90. W = W.array().abs().eval();
  91. }
  92. #ifdef IGL_STATIC_LIBRARY
  93. // Explicit template instantiation
  94. template void igl::screen_space_selection<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 1, 0, 4, 1>, float, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Array<double, -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&, igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 3> const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 1, 0, 4, 1> > const&, std::vector<Eigen::Matrix<float, 1, 2, 1, 1, 2>, std::allocator<Eigen::Matrix<float, 1, 2, 1, 1, 2> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Array<double, -1, 1, 0, -1, 1> >&);
  95. template void igl::screen_space_selection<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 1, 0, 4, 1>, float, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::MatrixBase<Eigen::Matrix<float, 4, 1, 0, 4, 1> > const&, std::vector<Eigen::Matrix<float, 1, 2, 1, 1, 2>, std::allocator<Eigen::Matrix<float, 1, 2, 1, 1, 2> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  96. #endif