unproject_in_mesh.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "EmbreeIntersector.h"
  10. #include "../unproject.h"
  11. #include <vector>
  12. template <typename Derivedobj>
  13. IGL_INLINE int igl::embree::unproject_in_mesh(
  14. const Eigen::Vector2f& pos,
  15. const Eigen::Matrix4f& model,
  16. const Eigen::Matrix4f& proj,
  17. const Eigen::Vector4f& viewport,
  18. const EmbreeIntersector & ei,
  19. Eigen::PlainObjectBase<Derivedobj> & obj,
  20. std::vector<igl::embree::Hit > & hits)
  21. {
  22. using namespace igl;
  23. using namespace std;
  24. using namespace Eigen;
  25. // Source and direction on screen
  26. Vector3f win_s(pos(0),pos(1),0);
  27. Vector3f win_d(pos(0),pos(1),1);
  28. // Source, destination and direction in world
  29. Vector3f s,d,dir;
  30. s = igl::unproject(win_s,model,proj,viewport);
  31. d = igl::unproject(win_d,model,proj,viewport);
  32. dir = d-s;
  33. // Shoot ray, collect all hits (could just collect first two)
  34. int num_rays_shot;
  35. hits.clear();
  36. ei.intersectRay(s,dir,hits,num_rays_shot);
  37. switch(hits.size())
  38. {
  39. case 0:
  40. break;
  41. case 1:
  42. {
  43. obj = (s + dir*hits[0].t).cast<typename Derivedobj::Scalar>();
  44. break;
  45. }
  46. case 2:
  47. default:
  48. {
  49. obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[1].t)).cast<typename Derivedobj::Scalar>();
  50. break;
  51. }
  52. }
  53. return hits.size();
  54. }
  55. template <typename Derivedobj>
  56. IGL_INLINE int igl::embree::unproject_in_mesh(
  57. const Eigen::Vector2f& pos,
  58. const Eigen::Matrix4f& model,
  59. const Eigen::Matrix4f& proj,
  60. const Eigen::Vector4f& viewport,
  61. const EmbreeIntersector & ei,
  62. Eigen::PlainObjectBase<Derivedobj> & obj)
  63. {
  64. std::vector<igl::embree::Hit> hits;
  65. return unproject_in_mesh(pos,model,proj,viewport,ei,obj,hits);
  66. }
  67. #ifdef IGL_STATIC_LIBRARY
  68. template int igl::embree::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&, igl::embree::EmbreeIntersector const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, std::vector<igl::embree::Hit, std::allocator<igl::embree::Hit> >&);
  69. template int igl::embree::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&, igl::embree::EmbreeIntersector const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&, std::vector<igl::embree::Hit, std::allocator<igl::embree::Hit> >&);
  70. template int igl::embree::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&, igl::embree::EmbreeIntersector const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
  71. #endif