Beispiel #1
0
        //! Check if the given ray intersect the box.
        bool intersect(const Ray<Type> & a_ray, Type & tmin, Type & tmax) const
        {
            // Amy Williams, Steve Barrus, R. Keith Morley, and Peter Shirley
            // "An Efficient and Robust Ray-Box Intersection Algorithm"
            //  Journal of graphics tools, 10(1):49-54, 2005
            if( isEmpty() ) return false;

            Vec3<Type> inv_direction(1/a_ray.getDirection()[0], 1/a_ray.getDirection()[1], 1/a_ray.getDirection()[2]);

            Vec3<int> sign( inv_direction[0] < 0, inv_direction[1] < 0, inv_direction[2] < 0);

            tmin  = ((sign[0] ? m_max : m_min).x() - a_ray.getOrigin().x()) * inv_direction.x();
            tmax  = (((1-sign[0]) ? m_max : m_min).x() - a_ray.getOrigin().x()) * inv_direction.x();

            Type tymin = ((sign[1] ? m_max : m_min).y() - a_ray.getOrigin().y()) * inv_direction.y();
            Type tymax = (((1-sign[1]) ? m_max : m_min).y() - a_ray.getOrigin().y()) * inv_direction.y();

            if( (tmin > tymax) || (tymin > tmax) ) return false;

            if(tymin > tmin) tmin = tymin;
            if(tymax < tmax) tmax = tymax;

            Type tzmin = ((sign[2] ? m_max : m_min).z() - a_ray.getOrigin().z()) * inv_direction.z();
            Type tzmax = (((1-sign[2]) ? m_max : m_min).z() - a_ray.getOrigin().z()) * inv_direction.z();

            if( (tmin > tzmax) || (tzmin > tmax) ) return false;

            if (tzmin > tmin) tmin = tzmin;
            if (tzmax < tmax) tmax = tzmax;

            return (tmin >= 0 && tmax >= 1);
        }
Beispiel #2
0
    bool intersectRayBBox(const Vec3r& orig, const Vec3r& dir,   // ray origin and direction
        const Vec3r& boxMin, const Vec3r& boxMax, // the bbox
        real t0, real t1,
        real& tmin, real& tmax,                  // I0=orig+tmin*dir is the first intersection, I1=orig+tmax*dir is the second intersection
        real epsilon){

            float tymin, tymax, tzmin, tzmax;
            Vec3r inv_direction(1.0/dir[0], 1.0/dir[1], 1.0/dir[2]);
            int sign[3];
            sign[0] = (inv_direction.x() < 0);
            sign[1] = (inv_direction.y() < 0);
            sign[2] = (inv_direction.z() < 0);

            Vec3r bounds[2];
            bounds[0] = boxMin;
            bounds[1] = boxMax;

            tmin = (bounds[sign[0]].x() - orig.x()) * inv_direction.x();
            tmax = (bounds[1-sign[0]].x() - orig.x()) * inv_direction.x();
            tymin = (bounds[sign[1]].y() - orig.y()) * inv_direction.y();
            tymax = (bounds[1-sign[1]].y() - orig.y()) * inv_direction.y();
            if ( (tmin > tymax) || (tymin > tmax) )
                return false;
            if (tymin > tmin)
                tmin = tymin;
            if (tymax < tmax)
                tmax = tymax;
            tzmin = (bounds[sign[2]].z() - orig.z()) * inv_direction.z();
            tzmax = (bounds[1-sign[2]].z() - orig.z()) * inv_direction.z();
            if ( (tmin > tzmax) || (tzmin > tmax) )
                return false;
            if (tzmin > tmin)
                tmin = tzmin;
            if (tzmax < tmax)
                tmax = tzmax;
            return ( (tmin < t1) && (tmax > t0) );
        }