unproject_in_mesh.cpp 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include "unproject_in_mesh.h"
  9. #include "unproject_ray.h"
  10. #include "ray_mesh_intersect.h"
  11. template < typename Derivedobj>
  12. IGL_INLINE int igl::unproject_in_mesh(
  13. const Eigen::Vector2f& pos,
  14. const Eigen::Matrix4f& model,
  15. const Eigen::Matrix4f& proj,
  16. const Eigen::Vector4f& viewport,
  17. const std::function<
  18. void(
  19. const Eigen::Vector3f&,
  20. const Eigen::Vector3f&,
  21. std::vector<igl::Hit> &)
  22. > & shoot_ray,
  23. Eigen::PlainObjectBase<Derivedobj> & obj,
  24. std::vector<igl::Hit > & hits)
  25. {
  26. using namespace igl;
  27. using namespace std;
  28. using namespace Eigen;
  29. Vector3f s,dir;
  30. unproject_ray(pos,model,proj,viewport,s,dir);
  31. shoot_ray(s,dir,hits);
  32. switch(hits.size())
  33. {
  34. case 0:
  35. break;
  36. case 1:
  37. {
  38. obj = (s + dir*hits[0].t).cast<typename Derivedobj::Scalar>();
  39. break;
  40. }
  41. case 2:
  42. default:
  43. {
  44. obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[1].t)).cast<typename Derivedobj::Scalar>();
  45. break;
  46. }
  47. }
  48. return hits.size();
  49. }
  50. extern "C"
  51. {
  52. #include "raytri.c"
  53. }
  54. template < typename DerivedV, typename DerivedF, typename Derivedobj>
  55. IGL_INLINE int igl::unproject_in_mesh(
  56. const Eigen::Vector2f& pos,
  57. const Eigen::Matrix4f& model,
  58. const Eigen::Matrix4f& proj,
  59. const Eigen::Vector4f& viewport,
  60. const Eigen::PlainObjectBase<DerivedV> & V,
  61. const Eigen::PlainObjectBase<DerivedF> & F,
  62. Eigen::PlainObjectBase<Derivedobj> & obj,
  63. std::vector<igl::Hit > & hits)
  64. {
  65. using namespace igl;
  66. using namespace std;
  67. using namespace Eigen;
  68. const auto & shoot_ray = [&V,&F](
  69. const Eigen::Vector3f& s,
  70. const Eigen::Vector3f& dir,
  71. std::vector<igl::Hit> & hits)
  72. {
  73. ray_mesh_intersect(s,dir,V,F,hits);
  74. };
  75. return unproject_in_mesh(pos,model,proj,viewport,shoot_ray,obj,hits);
  76. }
  77. template < typename DerivedV, typename DerivedF, typename Derivedobj>
  78. IGL_INLINE int igl::unproject_in_mesh(
  79. const Eigen::Vector2f& pos,
  80. const Eigen::Matrix4f& model,
  81. const Eigen::Matrix4f& proj,
  82. const Eigen::Vector4f& viewport,
  83. const Eigen::PlainObjectBase<DerivedV> & V,
  84. const Eigen::PlainObjectBase<DerivedF> & F,
  85. Eigen::PlainObjectBase<Derivedobj> & obj)
  86. {
  87. std::vector<igl::Hit> hits;
  88. return unproject_in_mesh(pos,model,proj,viewport,V,F,obj,hits);
  89. }
  90. #ifdef IGL_STATIC_LIBRARY
  91. template int igl::unproject_in_mesh<Eigen::Matrix<double, 1, 3, 1, 1, 3> >(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&, std::function<void (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, std::vector<igl::Hit, std::allocator<igl::Hit> >&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&, std::vector<igl::Hit, std::allocator<igl::Hit> >&);
  92. template int igl::unproject_in_mesh<Eigen::Matrix<double, 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&, std::function<void (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, std::vector<igl::Hit, std::allocator<igl::Hit> >&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&, std::vector<igl::Hit, std::allocator<igl::Hit> >&);
  93. template int igl::unproject_in_mesh<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&, std::function<void (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, std::vector<igl::Hit, std::allocator<igl::Hit> >&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, std::vector<igl::Hit, std::allocator<igl::Hit> >&);
  94. #endif