Ray intersects with AABB cartridge detection algorithm (Slabs method)

A. Ray and plane intersection

Ray as the starting point set P 0, the ray direction vector is , Equations ray any one point can be expressed as

    

Plane normal vector is set , the plane coordinates of any point P satisfies

    

Means, coordinates of a point and the origin vector office plane in the projection length in a constant d.

It is seen from the above equation rays and planes, meet at the intersection coordinates

    

 

Solutions have to

    

 

 

II. Ray opposite near and far plane surface surrounded by the cartridge

AABB cartridge a total of six surfaces, three surfaces which may be divided into rays near the surface, the other surface of the three faces far considered. Near side and the far side is not defined by the distance from the ray origin or from a point-rays , but by the normal vector direction of the surface 6 of the cartridge , the ray direction vector of the same direction as considered far surface, the ray direction vector with a direction opposite to the approach surface considered, namely: the ray direction vector and near surface point normal vector by <0, normal vectors point away surface by> 0, if a surface normal vector point by ray cassette with = 0, the ray does not intersect with the cartridge. Of course, there will not need to seek rays and the normal vector of each product which is then determined which face near the far surface, but the direct calculation of ray intersection of two surfaces coaxially AABB cartridge, and then by the following point 3, to determine which is the point which is far nearly personally personally point.

If the ray intersects with the AABB box, it certainly will meet the following points:

  1. If the ray intersection with AABB box there, then we will certainly pass through the AABB ray box
  2. Ray must intersect with the first three in a near surface
  3. The linear equations and 2, in which a certain t value is much large than the value of t personally point near the point of personally

Not self-evident to the above three points, 1, 2, for the third point, imagine that each side will be extended AABB box, that ray will intersect the three were near and far side faces AABB box, some intersection in the box some intersect at the extension side of the case, a total of six points of intersection (if the cassette intersects a vertex near the three surfaces (or far plane) coincides with the intersection), nearly three personally points, 3 points away personally, nearly 3 personally points, intersect only on a cassette, the other two intersecting surfaces is the same in extended, the distal surface of the cartridge. Further, the three points near personally, the value of t on the cartridge than at the point of intersection of the extension of the surface intersects the maximum value of t (t considering the sign, rather than absolute values), at the same time, than in the far t arbitrary point intersecting surfaces smaller.

If not this, then all the intersections were extended surface.

 

// sp, sq starting coordinate rays, and amax Amin is the minimum and the maximum point AABB cassette point coordinates that take an array of point coordinates, said corresponding index x, y, z-axis 0,1,2 
// t values tmin and tmax are personally ray and the far point and the near point personally
bool isectSegAABB(const float* sp, const float* sq, const float* amin, const float* amax, float& tmin, float& tmax)
{
    static const float EPS = 1e-6f;
    float d[3];
    d[0] = sq[0] - sp[0];
    d[1] = sq[1] - sp[1];
    d[2] = sq[2] - SP [ 2 ]; 
    Tmin = 0.0 ; 
    a tmax = 1.0f ; 

    // respectively two partial surfaces of the three axes to find the intersections 
    for ( int I = 0 ; I < . 3 ; I ++ ) 
    { 
        // If an axis between the two components had not changed, then the perpendicular to this axis, only needs to determine whether the cartridge inside the AABB, it is impossible to intersect 
        iF (fabsf (D [I]) < the EPS) 
        { 
            iF (SP [I] < Amin [I] || SP [I]> AMAX [I])
                 return  to false ; 
        } 
        the else {
             // find the value of t by the above formula,Obtaining two surfaces of the cassette AABB each axis ray intersections with the t-value, a large value is obtained personally far point t value, small value near t personally point value, then the normal vector of each plane n value is taken as the shaft 1, the other two axes score of 0
            const float ood = 1.0f / d[i];
            float t1 = (amin[i] - sp[i]) * ood;
            float t2 = (amax[i] - sp[i]) * ood;
            if (t1 > t2) { float tmp = t1; t1 = t2; t2 = tmp; }
            if (t1 > tmin) tmin = t1;
            if (t2 < tmax) tmax = t2;
            if (tmin > tmax) return false;
        }
    }
    return true;
}

Guess you like

Origin www.cnblogs.com/zengyg/p/12040940.html