How to calculate 3D line-line interesction?

I got 2 infinite lines and each line was defined by two points.
I’m trying to compute the intersection of the two lines with code below:

img1Line1P1 = [35.522341272816135, 244.0, -81.8808026069687]
img1Line1P2 = [36.1800944701046, 231.0, -81.31657002936069]
img2Line1P1 = [72.34037273903428, 244.0, -81.8925739257786]
img2Line1P2 = [70.43558942657907, 231.0, -81.32453925781688]

u = vtk.mutable(0)
v = vtk.mutable(0)

intersectType = vtk.vtkLine().Intersection(
img1Line1P1, img1Line1P2, img2Line1P1, img2Line1P2, u, v)
target11 = [img1Line1P1[i]+u*(img1Line1P2[i]-img1Line1P1[i]) for i in range(3)]
target12 = [img2Line1P1[i]+v*(img2Line1P2[i]-img2Line1P1[i]) for i in range(3)]
target1 = [(target11[i]+target12[i])/2 for i in range(3)]

print(intersectType)
print(u,v)
print(target11)
print(target12)
print(target1)

img1Line1P1 = [35.5223, 244, -81.8808]
img1Line1P2 = [36.4701, 231, -81.3736]
img2Line1P1 = [72.3401, 244, -81.8927]
img2Line1P2 = [70.4352, 231, -81.3244]

u = vtk.mutable(0)
v = vtk.mutable(0)

intersectType = vtk.vtkLine().Intersection(
img1Line1P1, img1Line1P2, img2Line1P1, img2Line1P2, u, v)
target11 = [img1Line1P1[i]+u*(img1Line1P2[i]-img1Line1P1[i]) for i in range(3)]
target12 = [img2Line1P1[i]+v*(img2Line1P2[i]-img2Line1P1[i]) for i in range(3)]
target1 = [(target11[i]+target12[i])/2 for i in range(3)]

print(intersectType)
print(u,v)
print(target11)
print(target12)
print(target1)

Output of code above goes like:

0
14.367889142863488 14.367746083471086
[44.972866294820804, 57.217441142774646, -73.7739714811047]
[44.97292976164488, 57.21930091487587, -73.73119604989571]
[44.97289802823284, 57.21837102882526, -73.7525837655002]
0
12.902095599460948 12.899676454379204
[47.7509062091691, 76.27275720700769, -75.33685711195344]
[47.76750632205291, 76.30420609307035, -74.56181387097621]
[47.759206265611006, 76.28848165003902, -74.94933549146482]

As you can see, even with a tiny difference of the points coordinates, the intersection calculated was totally different, especially with the Y-axis.

I was wondering if my code is the correct way to calculate the intersection of two 3D lines? If not, is there a better way to calculate 3D line-line intersection?

I used this function for intersection of finite line segments and it worked perfectly:

http://paulbourke.net/geometry/pointlineplane/L3D.py

The question was also asked on the VTK forum:

@nnzzll please avoid cross-posting to prevent multiple people spending time with answering your question at the same time. Instead, post to the most relevant forum and give some time for people to respond.

If you post to multiple forums, e.g., because you are in some exceptional circumstance, like a very close deadline, then you must disclose every other location where you posted the question elsewhere so that people can check if the question was answered already, before taking the time to answer. Discourse warns me when I’m starting to answer a question and somebody else is already working on an answer within the same forum, so unnecessary duplicate efforts are minimized; but manual checking is needed if the question is asked at the same time on different forums.

1 Like

Can you please advise on how to load this into slicer and what to modify if I would use the 4 control points of the two intersecting line in the codes? Thank you in advance.

    def lines2P(p0, p01, p1, p11):
        '''lines2P 2线交点

        '''
        drt00 = np.array([p01[0]])
        drt01 = np.array([p01[1]])
        drt02 = np.array([p01[2]])
        drt10 = np.array([p11[0]])
        drt11 = np.array([p11[1]])
        p0x = np.array([p0[0]])
        p0y = np.array([p0[1]])
        p0z = np.array([p0[2]])
        p1x = np.array([p1[0]])
        p1y = np.array([p1[1]])
        t = (drt10 * (p1y - p0y) + drt11 * (p0x - p1x)) / ((drt10 * drt01) - (drt00 * drt11))
        x = p0x + drt00 * t
        y = p0y + drt01 * t
        z = p0z + drt02 * t
        return np.array([x[0], y[0], z[0]])

:point_up_2:t2:我写了一个函数, 可以试一试啊