unproject_onto_mesh.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <alecjacobson@gmail.com>
  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. // Inputs:
  21. // pos screen space coordinates
  22. // model model matrix
  23. // proj projection matrix
  24. // viewport vieweport vector
  25. // V #V by 3 list of mesh vertex positions
  26. // F #F by 3 list of mesh triangle indices into V
  27. // Outputs:
  28. // fid id of the first face hit
  29. // bc barycentric coordinates of hit
  30. // Returns true if there's a hit
  31. template < typename DerivedV, typename DerivedF, typename Derivedbc>
  32. IGL_INLINE bool unproject_onto_mesh(
  33. const Eigen::Vector2f& pos,
  34. const Eigen::Matrix4f& model,
  35. const Eigen::Matrix4f& proj,
  36. const Eigen::Vector4f& viewport,
  37. const Eigen::PlainObjectBase<DerivedV> & V,
  38. const Eigen::PlainObjectBase<DerivedF> & F,
  39. int & fid,
  40. Eigen::PlainObjectBase<Derivedbc> & bc);
  41. //
  42. // Inputs:
  43. // pos screen space coordinates
  44. // model model matrix
  45. // proj projection matrix
  46. // viewport vieweport vector
  47. // shoot_ray function handle that outputs hits of a given ray against a
  48. // mesh (embedded in function handles as captured variable/data)
  49. // Outputs:
  50. // fid id of the first face hit
  51. // bc barycentric coordinates of hit
  52. // Returns true if there's a hit
  53. template <typename Derivedbc>
  54. IGL_INLINE bool unproject_onto_mesh(
  55. const Eigen::Vector2f& pos,
  56. const Eigen::Matrix4f& model,
  57. const Eigen::Matrix4f& proj,
  58. const Eigen::Vector4f& viewport,
  59. const std::function<
  60. bool(
  61. const Eigen::Vector3f&,
  62. const Eigen::Vector3f&,
  63. igl::Hit &)
  64. > & shoot_ray,
  65. int & fid,
  66. Eigen::PlainObjectBase<Derivedbc> & bc);
  67. }
  68. #ifndef IGL_STATIC_LIBRARY
  69. # include "unproject_onto_mesh.cpp"
  70. #endif
  71. #endif