123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // This file is part of libigl, a simple c++ geometry processing library.
- //
- // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
- //
- // 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<igl::Hit> &)
- > & shoot_ray,
- Eigen::PlainObjectBase<Derivedobj> & obj,
- std::vector<igl::Hit > & 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<typename Derivedobj::Scalar>();
- break;
- }
- case 2:
- default:
- {
- obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[1].t)).cast<typename Derivedobj::Scalar>();
- 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<DerivedV> & V,
- const Eigen::PlainObjectBase<DerivedF> & F,
- Eigen::PlainObjectBase<Derivedobj> & obj,
- std::vector<igl::Hit > & 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<igl::Hit> & hits)
- {
- // Should be but can't be const
- Vector3d s_d = s.template cast<double>();
- Vector3d dir_d = dir.template cast<double>();
- hits.clear();
- // loop over all triangles
- for(int f = 0;f<F.rows();f++)
- {
- // Should be but can't be const
- RowVector3d v0 = V.row(F(f,0)).template cast<double>();
- RowVector3d v1 = V.row(F(f,1)).template cast<double>();
- RowVector3d v2 = V.row(F(f,2)).template cast<double>();
- // 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<DerivedV> & V,
- const Eigen::PlainObjectBase<DerivedF> & F,
- Eigen::PlainObjectBase<Derivedobj> & obj)
- {
- std::vector<igl::Hit> hits;
- return unproject_in_mesh(pos,model,proj,viewport,V,F,obj,hits);
- }
- #ifdef IGL_STATIC_LIBRARY
- #endif
|