unproject_in_mesh.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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_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. // Inputs:
  23. // pos screen space coordinates
  24. // model model matrix
  25. // proj projection matrix
  26. // viewport vieweport vector
  27. // V #V by 3 list of mesh vertex positions
  28. // F #F by 3 list of mesh triangle indices into V
  29. // Outputs:
  30. // obj 3d unprojected mouse point in mesh
  31. // hits vector of hits
  32. // Returns number of hits
  33. //
  34. template < typename DerivedV, typename DerivedF, typename Derivedobj>
  35. IGL_INLINE int unproject_in_mesh(
  36. const Eigen::Vector2f& pos,
  37. const Eigen::Matrix4f& model,
  38. const Eigen::Matrix4f& proj,
  39. const Eigen::Vector4f& viewport,
  40. const Eigen::PlainObjectBase<DerivedV> & V,
  41. const Eigen::PlainObjectBase<DerivedF> & F,
  42. Eigen::PlainObjectBase<Derivedobj> & obj,
  43. std::vector<igl::Hit > & hits);
  44. //
  45. // Inputs:
  46. // pos screen space coordinates
  47. // model model matrix
  48. // proj projection matrix
  49. // viewport vieweport vector
  50. // shoot_ray function handle that outputs hits of a given ray against a
  51. // mesh (embedded in function handles as captured variable/data)
  52. // Outputs:
  53. // obj 3d unprojected mouse point in mesh
  54. // hits vector of hits
  55. // Returns number of hits
  56. //
  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> &)
  68. > & shoot_ray,
  69. Eigen::PlainObjectBase<Derivedobj> & obj,
  70. std::vector<igl::Hit > & hits);
  71. template < typename DerivedV, typename DerivedF, typename Derivedobj>
  72. IGL_INLINE int unproject_in_mesh(
  73. const Eigen::Vector2f& pos,
  74. const Eigen::Matrix4f& model,
  75. const Eigen::Matrix4f& proj,
  76. const Eigen::Vector4f& viewport,
  77. const Eigen::PlainObjectBase<DerivedV> & V,
  78. const Eigen::PlainObjectBase<DerivedF> & F,
  79. Eigen::PlainObjectBase<Derivedobj> & obj);
  80. }
  81. #ifndef IGL_STATIC_LIBRARY
  82. # include "unproject_in_mesh.cpp"
  83. #endif
  84. #endif