• Create Account

### #ActualNik02

Posted 28 December 2012 - 06:57 AM

The said function is optimized for the general case - it cannot assume anything about the geometry of the intersected mesh. In addition, it needs to interpret the data from a vertex buffer, which causes at least one level of pointer dereferencing (indirect random memory access). Also, being a dll function (as opposed to a native function in the same module as the caller), calling it requires one additional address mapping operation.

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

### #2Nik02

Posted 28 December 2012 - 06:57 AM

The said function is optimized for the general case - it cannot assume anything about the geometry of the intersected mesh. In addition, it needs to interpret the data from a vertex buffer, which causes at least one level of pointer dereferencing (indirect random memory access). Also, being a dll function (as opposed to a native function in the same module as the caller), calling it requires one additional address mapping operation.

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

### #1Nik02

Posted 28 December 2012 - 06:55 AM

The said function is optimized for the general case - it cannot assume anything about the geometry of the intersected mesh. In addition, it needs to interpret the data from a vertex buffer, which causes at least one level of pointer dereferencing (indirect random memory access). Also, being a dll function (as opposed to a native function in the same module as the caller), calling it requires one additional address mapping operation.

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 "collisions" be an empty list of triangle indices and distances from the origin of the collision 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 collision between ray and current triangle

If (ray collides triangle)

{

store current triangle index and collision distance to "collisions" collection

if (mindist is larger than current collision distance)

{

set mindist = current collision distance

set mintri = current triangle

}

}

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

PARTNERS