unproject_in_mesh.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. template < typename Derivedobj>
  11. IGL_INLINE int igl::unproject_in_mesh(
  12. const Eigen::Vector2f& pos,
  13. const Eigen::Matrix4f& model,
  14. const Eigen::Matrix4f& proj,
  15. const Eigen::Vector4f& viewport,
  16. const std::function<
  17. void(
  18. const Eigen::Vector3f&,
  19. const Eigen::Vector3f&,
  20. std::vector<igl::Hit> &)
  21. > & shoot_ray,
  22. Eigen::PlainObjectBase<Derivedobj> & obj,
  23. std::vector<igl::Hit > & hits)
  24. {
  25. using namespace igl;
  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 igl;
  65. using namespace std;
  66. using namespace Eigen;
  67. const auto & shoot_ray = [&V,&F](
  68. const Eigen::Vector3f& s,
  69. const Eigen::Vector3f& dir,
  70. std::vector<igl::Hit> & hits)
  71. {
  72. // Should be but can't be const
  73. Vector3d s_d = s.template cast<double>();
  74. Vector3d dir_d = dir.template cast<double>();
  75. hits.clear();
  76. // loop over all triangles
  77. for(int f = 0;f<F.rows();f++)
  78. {
  79. // Should be but can't be const
  80. RowVector3d v0 = V.row(F(f,0)).template cast<double>();
  81. RowVector3d v1 = V.row(F(f,1)).template cast<double>();
  82. RowVector3d v2 = V.row(F(f,2)).template cast<double>();
  83. // shoot ray, record hit
  84. double t,u,v;
  85. if(intersect_triangle1(
  86. s_d.data(), dir_d.data(), v0.data(), v1.data(), v2.data(), &t, &u, &v))
  87. {
  88. hits.push_back({(int)f,(int)-1,(float)u,(float)v,(float)t});
  89. }
  90. }
  91. // Sort hits based on distance
  92. std::sort(
  93. hits.begin(),
  94. hits.end(),
  95. [](const Hit & a, const Hit & b)->bool{ return a.t < b.t;});
  96. };
  97. return unproject_in_mesh(pos,model,proj,viewport,shoot_ray,obj,hits);
  98. }
  99. template < typename DerivedV, typename DerivedF, typename Derivedobj>
  100. IGL_INLINE int igl::unproject_in_mesh(
  101. const Eigen::Vector2f& pos,
  102. const Eigen::Matrix4f& model,
  103. const Eigen::Matrix4f& proj,
  104. const Eigen::Vector4f& viewport,
  105. const Eigen::PlainObjectBase<DerivedV> & V,
  106. const Eigen::PlainObjectBase<DerivedF> & F,
  107. Eigen::PlainObjectBase<Derivedobj> & obj)
  108. {
  109. std::vector<igl::Hit> hits;
  110. return unproject_in_mesh(pos,model,proj,viewport,V,F,obj,hits);
  111. }
  112. #ifdef IGL_STATIC_LIBRARY
  113. #endif