vtkOBBTree::IntersectWithLine sometimes returns wrong intersection values
I'm referring to the code below, from vtkOBBTree.cxx starting at line 929. I don't have time to create a bugfix/pull request, but it's an easy fix.
cell->IntersectWithLine()
overwrites t, x, pCoords, and subId for every intersection check from the cell candiate list.
However, the optimization below if ( foundIntersection != bestIntersection )..
, only sets these return-by-ref values if the last found intersection is not the best (closest) intersection.
So, if the last call of cell->IntersectWithLine
did not actually intersect, the values t, x, pCoords, and subId are all wrong; referring to the last call of cell->IntersectWithLine()
instead of the last intersecting call of cell->IntersectWithLine()
.
I would just remove the if ( foundIntersection != bestIntersection )...
and always set the best values.
if ( cell->IntersectWithLine( a0, a1, tol, t, x,
pcoords, subId ) )
{ // line intersects cell, but is it the best one?
foundIntersection++;
if ( t < tBest )
{ // Yes, it's the best.
bestIntersection = foundIntersection;
tBest = t;
xBest[0] = x[0]; xBest[1] = x[1]; xBest[2] = x[2];
pcoordsBest[0] = pcoords[0]; pcoordsBest[1] = pcoords[1];
pcoordsBest[2] = pcoords[2];
subIdBest = subId;
cellIdBest = thisId;
}
}
}
}
else
{ // push kids onto stack
OBBstack[depth] = node->Kids[0];
OBBstack[depth+1] = node->Kids[1];
depth += 2;
}
}
} // end while
if ( foundIntersection != bestIntersection )
{
t = tBest;
x[0] = xBest[0]; x[1] = xBest[1]; x[2] = xBest[2];
pcoords[0] = pcoordsBest[0]; pcoords[1] = pcoordsBest[1];
pcoords[2] = pcoordsBest[2];
subId= subIdBest ;
}