Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plane-plane intersection in python [closed]

I need to calculate intersection of two planes in form of AX+BY+CZ+D=0 and get a line in form of two (x,y,z) points. I know how to do the math, but I want to avoid inventing a bicycle and use something effective and tested. Is there any library which already implements this? Tried to search opencv and google, but with no success.

like image 872
Stepan Yakovenko Avatar asked Jan 06 '18 11:01

Stepan Yakovenko


1 Answers

my numpy solution:

def plane_intersect(a, b):
    """
    a, b   4-tuples/lists
           Ax + By +Cz + D = 0
           A,B,C,D in order  

    output: 2 points on line of intersection, np.arrays, shape (3,)
    """
    a_vec, b_vec = np.array(a[:3]), np.array(b[:3])

    aXb_vec = np.cross(a_vec, b_vec)

    A = np.array([a_vec, b_vec, aXb_vec])
    d = np.array([-a[3], -b[3], 0.]).reshape(3,1)

# could add np.linalg.det(A) == 0 test to prevent linalg.solve throwing error

    p_inter = np.linalg.solve(A, d).T

    return p_inter[0], (p_inter + aXb_vec)[0]


a, b = (1, -1, 0, 2), (-1, -1, 1, 3)
plane_intersect(a, b)

Out[583]: (array([ 0.,  2., -1.]), array([-1.,  1., -3.]))

a test, subs points back in:

p1, p2 = plane_intersect(a, b)
a_vec, b_vec = np.array(a[:3]), np.array(b[:3])

(np.dot(p1, a_vec), np.dot(p2, a_vec), np.dot(p1, b_vec), np.dot(p2, b_vec))
Out[585]: (-2.0, -2.0, -3.0, -3.0)
like image 164
f5r5e5d Avatar answered Oct 14 '22 06:10

f5r5e5d