// 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_box_intersect.h" #include #include #include template < typename Derivedsource, typename Deriveddir, typename Scalar> IGL_INLINE bool igl::ray_box_intersect( const Eigen::MatrixBase & origin, const Eigen::MatrixBase & inv_dir, const Eigen::MatrixBase & inv_dir_pad, const Eigen::AlignedBox & box, const Scalar & t0, const Scalar & t1, Scalar & tmin, Scalar & tmax) { using namespace Eigen; typedef Matrix RowVector3S; const std::array sign = { inv_dir(0)<0, inv_dir(1)<0, inv_dir(2)<0}; // http://people.csail.mit.edu/amy/papers/box-jgt.pdf // "An Efficient and Robust Ray–Box Intersection Algorithm" // corrected in "Robust BVH Ray Traversal" by Thiago Ize, section 3: Scalar tymin, tymax, tzmin, tzmax; std::array bounds = {box.min().array(),box.max().array()}; tmin = ( bounds[sign[0]](0) - origin(0)) * inv_dir(0); tmax = ( bounds[1-sign[0]](0) - origin(0)) * inv_dir_pad(0); tymin = (bounds[sign[1]](1) - origin(1)) * inv_dir(1); tymax = (bounds[1-sign[1]](1) - origin(1)) * inv_dir_pad(1); // NaN-safe min and max const auto berger_perrin_min = [&]( const Scalar a, const Scalar b) -> Scalar { return (a < b) ? a : b; }; const auto berger_perrin_max = [&]( const Scalar a, const Scalar b) -> Scalar { return (a > b) ? a : b; }; if ( (tmin > tymax) || (tymin > tmax) ) { return false; } tmin = berger_perrin_max(tmin, tymin); tmax = berger_perrin_min(tmax, tymax); tzmin = (bounds[sign[2]](2) - origin(2)) * inv_dir(2); tzmax = (bounds[1-sign[2]](2) - origin(2)) * inv_dir_pad(2); if ((tmin > tzmax) || (tzmin > tmax)) { return false; } tmin = berger_perrin_max(tmin, tzmin); tmax = berger_perrin_min(tmax, tzmax); return ((tmin < t1) && (tmax > t0)); } template < typename Derivedsource, typename Deriveddir, typename Scalar> IGL_INLINE bool igl::ray_box_intersect( const Eigen::MatrixBase & origin, const Eigen::MatrixBase & dir, const Eigen::AlignedBox & box, const Scalar & t0, const Scalar & t1, Scalar & tmin, Scalar & tmax) { // precompute the inv_dir Eigen::Matrix inv_dir = dir.cwiseInverse(); // see "Robust BVH Ray Traversal" by Thiago Ize, section 3: // for why we need this Eigen::Matrix inv_dir_pad = inv_dir; igl::increment_ulp(inv_dir_pad, 2); return igl::ray_box_intersect(origin, inv_dir, inv_dir_pad, box, t0, t1, tmin, tmax); } #ifdef IGL_STATIC_LIBRARY // Explicit template instantiation // generated by autoexplicit.sh template bool igl::ray_box_intersect, Eigen::Matrix, float>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::AlignedBox const&, float const&, float const&, float&, float&); template bool igl::ray_box_intersect, Eigen::Matrix, double>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::AlignedBox const&, double const&, double const&, double&, double&); template bool igl::ray_box_intersect, Eigen::Matrix, float>(Eigen::MatrixBase> const&, Eigen::MatrixBase> const&, Eigen::AlignedBox const&, float const&, float const&, float&, float&); template bool igl::ray_box_intersect, Eigen::Matrix, float>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::AlignedBox const&, float const&, float const&, float&, float&); template bool igl::ray_box_intersect, Eigen::Matrix, double>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::AlignedBox const&, double const&, double const&, double&, double&); #endif