Consider lots of 3D lines distributed randomly inside a 3D convex body. To find out intersections between all lines, we go to have two loops as follows.
n = number of lines
for i: 1 to n-1 {
for j: i+1 to n {
if line(i) intersects line(j): {
store (i,j)
}
}
}
Considering that n will be large, e.g., 1 million and more, what are optimizations that we could implement to speed up the processing? Lines could have any length, any orientation.
Update 1: The following comments helped to add this update:
- Lines in our problem are "line segments" with finite length limited to the shape of the convex body.
- Intersection or "minimum distance smaller than a tolerance" (very small value, indeed) between two lines is investigated.
Update 2: As suggested in the precious comments below, I was able to benefit from space partitioning, or in simple case, bounding box collision test, to speed up significantly. The optimization framework that I have implemented was as follows.
- Evaluate bounding box intersection for all lines, mark those intersect. (this is so fast and low cost)
- Do intersection test for those were marked.