The actual functionality of the intersection calculation is fairly well optimized, though.

If you

*can*make assumptions about the spatial partitioning of the mesh, you can probably write a better function yourself. The basic logic (assuming nothing about the geometry) is as follows:

Let "intersections" be an empty list of triangle indices and distances from the origin of the probe ray

Let "mindist" be a float that is initialized to maximum value of float

Let "mintri" be an index of the nearest found triangle so far

For each triangle in mesh

Check for intersection between ray and current triangle

If (ray intersects triangle)

{

store current triangle index and intersection distance to "intersections" collection

if (mindist is larger than current intersection distance)

{

set mindist = current intersection distance

set mintri = current triangle

}

}

return interections, mindist and mintri; each are potentially empty or unchanged, if intesections were not found