3d intersection functions

So i’ve been trying to mesh a random 3d polygon and made these functions.

http://youtu.be/tDLejB-S-N4

function intersectPlane(planePos,planeNorm,a,b)
    local b = b or vec3(0,0,0)
    local ad = (a-planePos):dot(planeNorm)
    local bd = (b-planePos):dot(planeNorm)
    if ad > 0 and bd < 0 or ad < 0 and bd > 0 then
        local intersection = a+((a-b):normalize()*(a:dist(b)/(a-b):dot(-planeNorm)*ad))
        return true,intersection
    end
    return false
end

function intersect3Planes(ap,an,bp,bn,cp,cn)
    local intNormal = an:cross(bn):normalize()
    local abNormal = an:cross(-intNormal):normalize()
    local int1,ab = intersectPlane(bp,bn,ap-(abNormal*100000),ap+(abNormal*100000))
    if int1 then
        local int2,pos = intersectPlane(cp,cn,ab-(intNormal*100000),ab+(intNormal*100000))
        if int2 then
            return true,pos
        end
    end
    return false
end

function intersectVertex(p1,p2,p3,a,b)
    local b = b or vec3(0,0,0)
    local planeNorm = (p1-p3):cross(p2-p1)
    local ad = (a-p1):dot(planeNorm)
    local bd = (b-p1):dot(planeNorm)
    if ad > 0 and bd < 0 or ad < 0 and bd > 0 then
        local intersect = a+((a-b):normalize()*(a:dist(b)/(a-b):dot(-planeNorm)*ad))
        local p1N,p2N,p3N =
        (p2-p1):cross(-planeNorm):normalize(),
        (p1-p3):cross(-planeNorm):normalize(),
        (p3-p2):cross(-planeNorm):normalize()
        local p1D,p2D,p3D = (intersect-p1):dot(p1N),(intersect-p1):dot(p2N),(intersect-p2):dot(p3N)
        if p1D > 0 and p2D > 0 and p3D > 0 then
            return true,intersect
        end
        return false
    end
    return false
end

function intersectVertexNormal(p1,p2,p3,a)
    local planeNormal = (p2-p1):cross(p3-p1):normalize()
    local normDist = (a-p1):dot(planeNormal)
    if normDist > 0 then
        local p1N,p2N,p3N =
        (p2-p1):cross(-planeNormal):normalize(),
        (p1-p3):cross(-planeNormal):normalize(),
        (p3-p2):cross(-planeNormal):normalize()
        local p1D,p2D,p3D = (a-p1):dot(p1N),(a-p1):dot(p2N),(a-p2):dot(p3N)
        if p1D > 0 and p2D > 0 and p3D > 0 then
            return true
        end
    end
    return false
end

And I never did mesh that damn polygon! :stuck_out_tongue: