unproject_onto_mesh.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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::PlainObjectBase<DerivedV> & V,
  20. const Eigen::PlainObjectBase<DerivedF> & F,
  21. int & fid,
  22. Eigen::PlainObjectBase<Derivedbc> & bc)
  23. {
  24. using namespace std;
  25. using namespace Eigen;
  26. const auto & shoot_ray = [&V,&F](
  27. const Eigen::Vector3f& s,
  28. const Eigen::Vector3f& dir,
  29. igl::Hit & hit)->bool
  30. {
  31. std::vector<igl::Hit> hits;
  32. if(!ray_mesh_intersect(s,dir,V,F,hits))
  33. {
  34. return false;
  35. }
  36. hit = hits[0];
  37. return true;
  38. };
  39. return unproject_onto_mesh(pos,model,proj,viewport,shoot_ray,fid,bc);
  40. }
  41. template <typename Derivedbc>
  42. IGL_INLINE bool igl::unproject_onto_mesh(
  43. const Eigen::Vector2f& pos,
  44. const Eigen::Matrix4f& model,
  45. const Eigen::Matrix4f& proj,
  46. const Eigen::Vector4f& viewport,
  47. const std::function<
  48. bool(
  49. const Eigen::Vector3f&,
  50. const Eigen::Vector3f&,
  51. igl::Hit &)
  52. > & shoot_ray,
  53. int & fid,
  54. Eigen::PlainObjectBase<Derivedbc> & bc)
  55. {
  56. using namespace std;
  57. using namespace Eigen;
  58. Vector3f s,dir;
  59. unproject_ray(pos,model,proj,viewport,s,dir);
  60. Hit hit;
  61. if(!shoot_ray(s,dir,hit))
  62. {
  63. return false;
  64. }
  65. bc.resize(3);
  66. bc << 1.0-hit.u-hit.v, hit.u, hit.v;
  67. fid = hit.id;
  68. return true;
  69. }
  70. #ifdef IGL_STATIC_LIBRARY
  71. // Explicit template specialization
  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<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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int&, Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> >&);
  73. 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  74. #endif