// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2013 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 "EmbreeIntersector.h" #include "../unproject.h" #include template IGL_INLINE int igl::embree::unproject_in_mesh( const Eigen::Vector2f& pos, const Eigen::Matrix4f& model, const Eigen::Matrix4f& proj, const Eigen::Vector4f& viewport, const EmbreeIntersector & ei, Eigen::PlainObjectBase & obj, std::vector & hits) { using namespace igl; using namespace std; using namespace Eigen; // Source and direction on screen Vector3f win_s(pos(0),pos(1),0); Vector3f win_d(pos(0),pos(1),1); // Source, destination and direction in world Vector3f s,d,dir; s = igl::unproject(win_s,model,proj,viewport); d = igl::unproject(win_d,model,proj,viewport); dir = d-s; // Shoot ray, collect all hits (could just collect first two) int num_rays_shot; hits.clear(); ei.intersectRay(s,dir,hits,num_rays_shot); 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(); } template IGL_INLINE int igl::embree::unproject_in_mesh( const Eigen::Vector2f& pos, const Eigen::Matrix4f& model, const Eigen::Matrix4f& proj, const Eigen::Vector4f& viewport, const EmbreeIntersector & ei, Eigen::PlainObjectBase & obj) { std::vector hits; return unproject_in_mesh(pos,model,proj,viewport,ei,obj,hits); } #ifdef IGL_STATIC_LIBRARY template int igl::embree::unproject_in_mesh >(Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, igl::embree::EmbreeIntersector const&, Eigen::PlainObjectBase >&, std::vector >&); template int igl::embree::unproject_in_mesh >(Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, igl::embree::EmbreeIntersector const&, Eigen::PlainObjectBase >&, std::vector >&); template int igl::embree::unproject_in_mesh >(Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, Eigen::Matrix const&, igl::embree::EmbreeIntersector const&, Eigen::PlainObjectBase >&); #endif