unproject_in_mesh.cpp 5.4 KB

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