unproject_in_mesh.cpp 4.7 KB

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