unproject_onto_mesh.cpp 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson
  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 "unproject_onto_mesh.h"
  9. #include "unproject.h"
  10. #include "unproject_ray.h"
  11. #include "ray_mesh_intersect.h"
  12. #include <vector>
  13. template < typename DerivedV, typename DerivedF, typename Derivedbc>
  14. IGL_INLINE bool igl::unproject_onto_mesh(
  15. const Eigen::Vector2f& pos,
  16. const Eigen::Matrix4f& model,
  17. const Eigen::Matrix4f& proj,
  18. const Eigen::Vector4f& viewport,
  19. const Eigen::MatrixBase<DerivedV> & V,
  20. const Eigen::MatrixBase<DerivedF> & F,
  21. int & fid,
  22. Eigen::PlainObjectBase<Derivedbc> & bc)
  23. {
  24. const auto Vf = V.template cast<float>().eval();
  25. const auto & shoot_ray = [&Vf,&F](
  26. const Eigen::Vector3f& s,
  27. const Eigen::Vector3f& dir,
  28. igl::Hit<float> & hit)->bool
  29. {
  30. std::vector<igl::Hit<float>> hits;
  31. if(!ray_mesh_intersect(s,dir,Vf,F,hits))
  32. {
  33. return false;
  34. }
  35. hit = hits[0];
  36. return true;
  37. };
  38. return unproject_onto_mesh(pos,model,proj,viewport,shoot_ray,fid,bc);
  39. }
  40. template <typename Derivedbc>
  41. IGL_INLINE bool igl::unproject_onto_mesh(
  42. const Eigen::Vector2f& pos,
  43. const Eigen::Matrix4f& model,
  44. const Eigen::Matrix4f& proj,
  45. const Eigen::Vector4f& viewport,
  46. const std::function<
  47. bool(
  48. const Eigen::Vector3f&,
  49. const Eigen::Vector3f&,
  50. igl::Hit<float> &)
  51. > & shoot_ray,
  52. int & fid,
  53. Eigen::PlainObjectBase<Derivedbc> & bc)
  54. {
  55. Eigen::Vector3f s,dir;
  56. unproject_ray(pos,model,proj,viewport,s,dir);
  57. Hit<float> hit;
  58. if(!shoot_ray(s,dir,hit))
  59. {
  60. return false;
  61. }
  62. bc.resize(3, 1);
  63. bc << 1.0-hit.u-hit.v, hit.u, hit.v;
  64. fid = hit.id;
  65. return true;
  66. }
  67. #ifdef IGL_STATIC_LIBRARY
  68. // Explicit template instantiation
  69. // generated by autoexplicit.sh
  70. template bool igl::unproject_onto_mesh<Eigen::Matrix<float, 3, 1, 0, 3, 1> >(Eigen::Matrix<float, 2, 1, 0, 2, 1> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&, std::function<bool (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, igl::Hit<float>&)> const&, int&, Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> >&);
  71. // generated by autoexplicit.sh
  72. template bool igl::unproject_onto_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::Matrix<float, 2, 1, 0, 2, 1> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  73. // generated by autoexplicit.sh
  74. template bool igl::unproject_onto_mesh<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<float, 3, 1, 0, 3, 1> >(Eigen::Matrix<float, 2, 1, 0, 2, 1> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, int&, Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> >&);
  75. template bool igl::unproject_onto_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<float, 3, 1, 0, 3, 1> >(Eigen::Matrix<float, 2, 1, 0, 2, 1> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int&, Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> >&);
  76. template bool igl::unproject_onto_mesh<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::Matrix<float, 2, 1, 0, 2, 1> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  77. #endif