Search code examples
pythoncomputational-geometryintersectionplane

Plane-plane intersection in python


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.


Solution

  • Finally I've reused sympy library, by converting Ax+By+Cz+D=0 equation to (n,pt) formulation:

    def planeCoeffToPoint(n,d):
        nabs = [abs(i) for i in n]
        i=nabs.index(max(nabs))
        r=[0,0,0]
        r[i]=-d/n[i]
        return r
    
    import sympy as sp
    n1=(A1,B1,C1)
    D1=...
    n2=(A2,B2,C2)
    D2=...
    pt1=planeCoeffToPoint(n1,D1)
    pt2=planeCoeffToPoint(n2,D2)
    pl1 = sp.Plane(pt1, normal_vector=n1)
    pl2 = sp.Plane(pt2, normal_vector=n2)
    r=pl1.intersection(pl2)
    rp1=r[0].points[0]
    rp2=r[0].points[1]
    [float(rp1[0]), float(rp1[1]), float(rp1[2])] # first point on line
    [float(rp2[0]), float(rp2[1]), float(rp2[2])] # second point on line
    

    You have to call float() explicitly, becuse sympy might return Zero or Rational object, representing float.