unproject_in_mesh.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <igl/unproject.h>
  11. #include <vector>
  12. template <
  13. typename Derivedobj>
  14. int igl::unproject_in_mesh(
  15. const int x,
  16. const int y,
  17. const igl::EmbreeIntersector & ei,
  18. Eigen::PlainObjectBase<Derivedobj> & obj)
  19. {
  20. std::vector<igl::Hit> hits;
  21. return igl::unproject_in_mesh(x,y,ei,obj,hits);
  22. }
  23. template <
  24. typename Derivedobj>
  25. int igl::unproject_in_mesh(
  26. const int x,
  27. const int y,
  28. const igl::EmbreeIntersector & ei,
  29. Eigen::PlainObjectBase<Derivedobj> & obj,
  30. std::vector<igl::Hit > & hits)
  31. {
  32. using namespace igl;
  33. using namespace std;
  34. using namespace Eigen;
  35. // Source and direction on screen
  36. Vector3f win_s = Vector3f(x,y,0);
  37. Vector3f win_d(x,y,1);
  38. // Source, destination and direction in world
  39. Vector3f s,d,dir;
  40. unproject(win_s,s);
  41. unproject(win_d,d);
  42. dir = d-s;
  43. // Shoot ray, collect all hits (could just collect first two)
  44. int num_rays_shot;
  45. hits.clear();
  46. ei.intersectRay(s,dir,hits,num_rays_shot);
  47. switch(hits.size())
  48. {
  49. case 0:
  50. break;
  51. case 1:
  52. {
  53. obj = (s + dir*hits[0].t).cast<typename Derivedobj::Scalar>();
  54. break;
  55. }
  56. case 2:
  57. default:
  58. {
  59. obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[1].t)).cast<typename Derivedobj::Scalar>();
  60. break;
  61. }
  62. }
  63. return hits.size();
  64. }
  65. #ifndef IGL_HEADER_ONLY
  66. template int igl::unproject_in_mesh<Eigen::Matrix<double, 3, 1, 0, 3, 1> >(int, int, igl::EmbreeIntersector const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&, std::vector<igl::Hit, std::allocator<igl::Hit> >&);
  67. template int igl::unproject_in_mesh<Eigen::Matrix<double, 3, 1, 0, 3, 1> >(int, int, igl::EmbreeIntersector const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
  68. #endif