// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2015 Alec Jacobson // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include "unproject_in_mesh.h" #include "unproject_ray.h" template < typename Derivedobj> IGL_INLINE int igl::unproject_in_mesh( const Eigen::Vector2f& pos, const Eigen::Matrix4f& model, const Eigen::Matrix4f& proj, const Eigen::Vector4f& viewport, const std::function< void( const Eigen::Vector3f&, const Eigen::Vector3f&, std::vector &) > & shoot_ray, Eigen::PlainObjectBase & obj, std::vector & hits) { using namespace igl; using namespace std; using namespace Eigen; Vector3f s,dir; unproject_ray(pos,model,proj,viewport,s,dir); shoot_ray(s,dir,hits); switch(hits.size()) { case 0: break; case 1: { obj = (s + dir*hits[0].t).cast(); break; } case 2: default: { obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[1].t)).cast(); break; } } return hits.size(); } extern "C" { #include "raytri.c" } template < typename DerivedV, typename DerivedF, typename Derivedobj> IGL_INLINE int igl::unproject_in_mesh( const Eigen::Vector2f& pos, const Eigen::Matrix4f& model, const Eigen::Matrix4f& proj, const Eigen::Vector4f& viewport, const Eigen::PlainObjectBase & V, const Eigen::PlainObjectBase & F, Eigen::PlainObjectBase & obj, std::vector & hits) { using namespace igl; using namespace std; using namespace Eigen; const auto & shoot_ray = [&V,&F]( const Eigen::Vector3f& s, const Eigen::Vector3f& dir, std::vector & hits) { // Should be but can't be const Vector3d s_d = s.template cast(); Vector3d dir_d = dir.template cast(); hits.clear(); // loop over all triangles for(int f = 0;f(); RowVector3d v1 = V.row(F(f,1)).template cast(); RowVector3d v2 = V.row(F(f,2)).template cast(); // shoot ray, record hit double t,u,v; if(intersect_triangle1( s_d.data(), dir_d.data(), v0.data(), v1.data(), v2.data(), &t, &u, &v)) { hits.push_back({(int)f,(int)-1,(float)u,(float)v,(float)t}); } } // Sort hits based on distance std::sort( hits.begin(), hits.end(), [](const Hit & a, const Hit & b)->bool{ return a.t < b.t;}); }; return unproject_in_mesh(pos,model,proj,viewport,shoot_ray,obj,hits); } template < typename DerivedV, typename DerivedF, typename Derivedobj> IGL_INLINE int igl::unproject_in_mesh( const Eigen::Vector2f& pos, const Eigen::Matrix4f& model, const Eigen::Matrix4f& proj, const Eigen::Vector4f& viewport, const Eigen::PlainObjectBase & V, const Eigen::PlainObjectBase & F, Eigen::PlainObjectBase & obj) { std::vector hits; return unproject_in_mesh(pos,model,proj,viewport,V,F,obj,hits); } #ifdef IGL_STATIC_LIBRARY #endif