// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2016 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 "ray_mesh_intersect.h" extern "C" { #include "raytri.c" } namespace igl { template < typename Derivedsource, typename Deriveddir, typename DerivedV, typename DerivedF> IGL_INLINE bool ray_triangle_intersect( const Eigen::MatrixBase & s, const Eigen::MatrixBase & dir, const Eigen::MatrixBase & V, const Eigen::MatrixBase & F, const int f, igl::Hit& hit) { using namespace Eigen; // intersect_triangle1 needs non-const inputs. Vector3d s_d = s.template cast(); Vector3d dir_d = dir.template cast(); RowVector3d v0 = V.row(F(f,0)).template cast(); 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) && t>0) { hit = {static_cast(f),static_cast(-1), static_cast(u),static_cast(v), static_cast(t)}; return true; } return false; } template < typename Derivedsource, typename Deriveddir, typename DerivedV, typename DerivedF> IGL_INLINE bool ray_mesh_intersect( const Eigen::MatrixBase & s, const Eigen::MatrixBase & dir, const Eigen::MatrixBase & V, const Eigen::MatrixBase & F, std::vector & hits) { using namespace Eigen; hits.clear(); hits.reserve(F.rows()); // loop over all triangles for(int f = 0;fbool{ return a.t < b.t;}); return hits.size() > 0; } template < typename Derivedsource, typename Deriveddir, typename DerivedV, typename DerivedF> IGL_INLINE bool ray_mesh_intersect( const Eigen::MatrixBase & s, const Eigen::MatrixBase & dir, const Eigen::MatrixBase & V, const Eigen::MatrixBase & F, igl::Hit & hit) { if (F.rows() == 1) { // Shortcut for AABB based queries. return ray_triangle_intersect(s, dir, V, F, 0, hit); }else { std::vector hits; ray_mesh_intersect(s,dir,V,F,hits); if(hits.size() > 0) { hit = hits.front(); return true; }else { return false; } } } } #ifdef IGL_STATIC_LIBRARY // Explicit template instantiation // generated by autoexplicit.sh template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Block const, 1, -1, false> >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase const, 1, -1, false> > const&, std::vector >&); // generated by autoexplicit.sh template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Block const, 1, -1, false> >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase const, 1, -1, false> > const&, std::vector >&); // generated by autoexplicit.sh template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, std::vector >&); // generated by autoexplicit.sh template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, std::vector >&); template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, std::vector >&); template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, igl::Hit&); template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Block const, 1, -1, false> >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase const, 1, -1, false> > const&, igl::Hit&); #endif