Friday, December 23, 2011

Point in Plane (finally)

One of the last things I've been stuck on is figuring out if a point is within a plane in. After playing around with nearestPointInMesh a couple days ago, it seemed that the function was designed to determine if a point was inside a 3-dimensional mesh rather than a 2D plan (though I still consider that a mesh), which let to a few days of frustration and misunderstanding.

I ended up coming across Jeren Chen's code on determining if a point is inside a mesh without using plug-ins (which I really prefer since you shouldn't assume your user has all the plug-ins you do). His code, like the Maya plug-in, functioned on a 3D mesh rather than a 2D plane, but I ended up writing a hacky function that takes in a plane, moves it down the y axis a tiny bit and extrudes it upwards twice that amount to form a "buffer" 3d mesh for that plane. And then I called Jeren's code. For those wondering it looks like this:
def pointInPlane(v, p):
    # convert point to MPoint
    m = MPoint()
    coord = cmds.xform(v, q = True, ws = True, t = True)
    m.x = float(coord[0])
    m.y = float(coord[1])
    m.z = float(coord[2])
   
    # copy the given plane (since we'll be extruding it and deleting afterwards)
    plane = cmds.duplicate(p)
   
    # select all faces on the plane
    cmds.select(plane, r=True)
    cmds.ConvertSelectionToFaces()
    f = cmds.ls(sl=True)

    # move plane down a bit and extrude upwards to form a thin "buffer" mesh for the pointInMesh function
    cmds.move(0, -0.05, 0, plane, a=True)
    e = cmds.polyExtrudeFacet(ty=0.1)
   
    # get the resulting polygon
    poly = getPolyFromFace(f)
   
    # call pointInMesh function
    inside = isInside(m,poly)
   
    # delete the copied poly
    cmds.delete(plane)
   
    return inside

Hacky as it is, it works (yay!)

No comments:

Post a Comment