unproject_onto_mesh.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@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_onto_mesh.h"
  9. #include "EmbreeIntersector.h"
  10. #include <igl/unproject.h>
  11. #include <igl/embree/unproject_in_mesh.h>
  12. #include <vector>
  13. IGL_INLINE bool igl::embree::unproject_onto_mesh(
  14. const Eigen::Vector2f& pos,
  15. const Eigen::MatrixXi& F,
  16. const Eigen::Matrix4f& model,
  17. const Eigen::Matrix4f& proj,
  18. const Eigen::Vector4f& viewport,
  19. const EmbreeIntersector & ei,
  20. int& fid,
  21. Eigen::Vector3f& bc)
  22. {
  23. using namespace std;
  24. using namespace Eigen;
  25. MatrixXd obj;
  26. vector<igl::Hit> hits;
  27. // This is lazy, it will find more than just the first hit
  28. unproject_in_mesh(pos,model,proj,viewport,ei,obj,hits);
  29. if (hits.size()> 0)
  30. {
  31. bc << 1.0-hits[0].u-hits[0].v, hits[0].u, hits[0].v;
  32. fid = hits[0].id;
  33. return true;
  34. }
  35. return false;
  36. }
  37. IGL_INLINE bool igl::embree::unproject_onto_mesh(
  38. const Eigen::Vector2f& pos,
  39. const Eigen::MatrixXi& F,
  40. const Eigen::Matrix4f& model,
  41. const Eigen::Matrix4f& proj,
  42. const Eigen::Vector4f& viewport,
  43. const EmbreeIntersector & ei,
  44. int& fid,
  45. int& vid)
  46. {
  47. Eigen::Vector3f bc;
  48. bool hit = unproject_onto_mesh(pos,F,model,proj,viewport,ei,fid,bc);
  49. int i;
  50. if (hit)
  51. {
  52. bc.maxCoeff(&i);
  53. vid = F(fid,i);
  54. }
  55. return hit;
  56. }
  57. #ifdef IGL_STATIC_LIBRARY
  58. // Explicit template specialization
  59. #endif