unproject_in_mesh.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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_IN_MESH
  9. #define IGL_UNPROJECT_IN_MESH
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <vector>
  13. #include "Hit.h"
  14. namespace igl
  15. {
  16. /// Unproject a screen location (using current opengl viewport, projection, and
  17. /// model view) to a 3D position _inside_ a given mesh. If the ray through the
  18. /// given screen location (x,y) _hits_ the mesh more than twice then the 3D
  19. /// midpoint between the first two hits is return. If it hits once, then that
  20. /// point is return. If it does not hit the mesh then obj is not set.
  21. ///
  22. /// @param[in] pos screen space coordinates
  23. /// @param[in] model model matrix
  24. /// @param[in] proj projection matrix
  25. /// @param[in] viewport vieweport vector
  26. /// @param[in] V #V by 3 list of mesh vertex positions
  27. /// @param[in] F #F by 3 list of mesh triangle indices into V
  28. /// @param[out] obj 3d unprojected mouse point in mesh
  29. /// @param[out] hits vector of hits
  30. /// @return number of hits
  31. ///
  32. template < typename DerivedV, typename DerivedF, typename Derivedobj>
  33. IGL_INLINE int unproject_in_mesh(
  34. const Eigen::Vector2f& pos,
  35. const Eigen::Matrix4f& model,
  36. const Eigen::Matrix4f& proj,
  37. const Eigen::Vector4f& viewport,
  38. const Eigen::MatrixBase<DerivedV> & V,
  39. const Eigen::MatrixBase<DerivedF> & F,
  40. Eigen::PlainObjectBase<Derivedobj> & obj,
  41. std::vector<igl::Hit<float> > & hits);
  42. /// \overload
  43. template < typename DerivedV, typename DerivedF, typename Derivedobj>
  44. IGL_INLINE int unproject_in_mesh(
  45. const Eigen::Vector2f& pos,
  46. const Eigen::Matrix4f& model,
  47. const Eigen::Matrix4f& proj,
  48. const Eigen::Vector4f& viewport,
  49. const Eigen::MatrixBase<DerivedV> & V,
  50. const Eigen::MatrixBase<DerivedF> & F,
  51. Eigen::PlainObjectBase<Derivedobj> & obj);
  52. /// \overload
  53. ///
  54. /// @param[in] shoot_ray function handle that outputs first hit of a given ray
  55. /// against a mesh (embedded in function handles as captured
  56. /// variable/data)
  57. template < typename Derivedobj>
  58. IGL_INLINE int unproject_in_mesh(
  59. const Eigen::Vector2f& pos,
  60. const Eigen::Matrix4f& model,
  61. const Eigen::Matrix4f& proj,
  62. const Eigen::Vector4f& viewport,
  63. const std::function<
  64. void(
  65. const Eigen::Vector3f&,
  66. const Eigen::Vector3f&,
  67. std::vector<igl::Hit<float>> &)
  68. > & shoot_ray,
  69. Eigen::PlainObjectBase<Derivedobj> & obj,
  70. std::vector<igl::Hit<float> > & hits);
  71. }
  72. #ifndef IGL_STATIC_LIBRARY
  73. # include "unproject_in_mesh.cpp"
  74. #endif
  75. #endif