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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With