unproject_onto_mesh.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <[email protected]>
  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. #ifndef IGL_UNPROJECT_ONTO_MESH
  9. #define IGL_UNPROJECT_ONTO_MESH
  10. #include "igl_inline.h"
  11. #include "Hit.h"
  12. #include <Eigen/Core>
  13. #include <functional>
  14. namespace igl
  15. {
  16. /// Unproject a screen location (using current opengl viewport, projection, and
  17. /// model view) to a 3D position _onto_ a given mesh, if the ray through the
  18. /// given screen location (x,y) _hits_ the mesh.
  19. ///
  20. /// @param[in] pos screen space coordinates
  21. /// @param[in] model model matrix
  22. /// @param[in] proj projection matrix
  23. /// @param[in] viewport vieweport vector
  24. /// @param[in] V #V by 3 list of mesh vertex positions
  25. /// @param[in] F #F by 3 list of mesh triangle indices into V
  26. /// @param[out] fid id of the first face hit
  27. /// @param[out] bc barycentric coordinates of hit
  28. /// @return true if there's a hit
  29. ///
  30. /// #### Example:
  31. ///
  32. /// \code{cpp}
  33. /// igl::opengl::glfw::Viewer vr;
  34. /// ...
  35. /// igl::unproject_onto_mesh(
  36. /// pos,vr.core().view,vr.core().proj,vr.core().viewport,V,F,fid,bc);
  37. /// \endcode
  38. ///
  39. template < typename DerivedV, typename DerivedF, typename Derivedbc>
  40. IGL_INLINE bool unproject_onto_mesh(
  41. const Eigen::Vector2f& pos,
  42. const Eigen::Matrix4f& model,
  43. const Eigen::Matrix4f& proj,
  44. const Eigen::Vector4f& viewport,
  45. const Eigen::MatrixBase<DerivedV> & V,
  46. const Eigen::MatrixBase<DerivedF> & F,
  47. int & fid,
  48. Eigen::PlainObjectBase<Derivedbc> & bc);
  49. /// \overload
  50. /// @param[in] shoot_ray function handle that outputs hits of a given ray against a
  51. /// mesh (embedded in function handles as captured variable/data)
  52. template <typename Derivedbc>
  53. IGL_INLINE bool unproject_onto_mesh(
  54. const Eigen::Vector2f& pos,
  55. const Eigen::Matrix4f& model,
  56. const Eigen::Matrix4f& proj,
  57. const Eigen::Vector4f& viewport,
  58. const std::function<
  59. bool(
  60. const Eigen::Vector3f&,
  61. const Eigen::Vector3f&,
  62. igl::Hit<float> &)
  63. > & shoot_ray,
  64. int & fid,
  65. Eigen::PlainObjectBase<Derivedbc> & bc);
  66. }
  67. #ifndef IGL_STATIC_LIBRARY
  68. # include "unproject_onto_mesh.cpp"
  69. #endif
  70. #endif