Exemplo n.º 1
0
Geometry::HitResult<double> Section::
intersectRay(const Geometry::Ray<double,3>& ray) const
{
    /* based on "Fast, Minimum Storage Ray/Triangle Intersection" by Tomas
       Moeller and Ben Trumbore */
    Geometry::Vector<double,3> rayOrig(ray.getOrigin()[0],ray.getOrigin()[1],ray.getOrigin()[2]);
    const Geometry::Vector<double,3>& rayDir = ray.getDirection();

    /* begin calculating determinant - also used to calculate U parameter */
    Geometry::Vector<double,3> pvec = Geometry::cross(rayDir, end);
    /* if determinant is near zero, ray lies in plane of triangle */
    Scalar det = start * pvec;

    if (det>-EPSILON && det<EPSILON)
        return Geometry::HitResult<double>();

    Scalar invDet = Scalar(1) / det;

    /* calculate U parameter and test bounds */
    Scalar u = (rayOrig*pvec) * invDet;
    if (u < Scalar(0))
        return Geometry::HitResult<double>();

    /* prepare to test V parameter */
    Geometry::Vector<double,3> qvec = Geometry::cross(rayOrig, start);
    /* calculate V parameter and test bounds */
    Scalar v = (rayDir*qvec) * invDet;
    if (v < Scalar(0))
        return Geometry::HitResult<double>();

    /* calculate t, ray intersects triangle */
    Scalar t = (end*qvec) * invDet;
    return Geometry::HitResult<double>(t);
}
Exemplo n.º 2
0
Geometry::HitResult<double> Section::
intersectPlane(const Geometry::Ray<double,3>& ray, bool cullBackFace) const
{
    Geometry::Vector<double,3> rayOrig(ray.getOrigin()[0],ray.getOrigin()[1],ray.getOrigin()[2]);
    const Geometry::Vector<double,3>& rayDir = ray.getDirection();

    /* the ray intersects the plane at poing t for which (ray(t)-PlaneOrigin) is
       orthogonal to the normal. See http://softsurfer.com/Archive/algorithm_0104/algorithm_0104B.htm#Line-Plane Intersection
       for nice description */
    double nDotDir = normal * rayDir;

    //exit on back-facing planes if so desired
    if (cullBackFace && nDotDir>0.0)
        return Geometry::HitResult<double>();

    //handle the parallel case
    if (nDotDir>-EPSILON && nDotDir<EPSILON)
    {
        Geometry::Vector<double,3> toOrig    = rayOrig - start;
        double nDotToOrig = normal * toOrig;
        //coincident
        if (nDotToOrig>-EPSILON && nDotToOrig<EPSILON)
            return Geometry::HitResult<double>(0.0);
        //disjoint
        else
            return Geometry::HitResult<double>();
    }

    //compute the intersection parameter
    return Geometry::HitResult<double>((normal * (start - rayOrig)) / nDotDir);
}