find closest point on a curve to a fixed point in space

465 views
Skip to first unread message

s...@weacceptyou.com

unread,
Mar 29, 2016, 3:31:34 PM3/29/16
to Python Programming for Autodesk Maya
Hello there,

i have a point in space and a nearby curve that is running close to the point but not touching. Is there a special python/pymel way of finding the closest point on the curve to this point in space?

any tips welcome,

thanks,
Sam

Sasha C

unread,
Mar 30, 2016, 4:39:58 AM3/30/16
to python_in...@googlegroups.com
Hi Sam,

I had a similar problem, but from point to a line segment which is straight. Look at this code, may be it can be of help:



def closestPointOnLineSegment(lineStartPoint, lineEndPoint, pointInSpace, ignoreNoProjections=True):
    '''
    Find a closest point on the line segment (defined by 2 points) to a given point in space.
    NOTE: There may be more limitations - be aware when using!
    Based on "Using Vector Mathematic, Finding closest point on a line", http://nic-gamedev.blogspot.dk/2011/11/using-vector-mathematics-and-bit-of_08.html
    #---parameters---------------------------------------
    # lineStartPoint       # float list     # A list of 3 elements to represent a point, that defines the start of the line segment.
    # lineEndPoint         # float list     # A list of 3 elements to represent a point, that defines the end of the line segment.
    # pointInSpace         # float list     # A list of 3 elements to represent a point in space.
    # ignoreNoProjections  # boolean        # Points in space that have no perpendicular to the given line segment will be ignored.
    #---return-------------------------------------------
    #                      # float list     # A list of 3 elements to represent a point between line points, that is closest to the given point in space.
    #                                       # If ignoreNoProjections=True and point is outside the segment - will return a zero vector [0.0, 0.0, 0.0]
    #---example------------------------------------------
    # closestPointOnLineSegment(lineStartPoint=[61.87, 4.31, 1.85], lineEndPoint=[65.97, 4.15, 2.37], pointInSpace=[67.89, 3.71, 2.60])
    #----------------------------------------------------
    author      Alexandra "Sasha" Ciolac
    version     1.1
    last update 07.03.2013
    #---Update log---------------------------------------
   '''  
    ac = vecSubtract(vectorA=lineEndPoint, vectorB=lineStartPoint) # Find vector between start and end of the line
    l2 = vecMag(vector=ac) * vecMag(vector=ac) # Find it's square length
    ba = vecSubtract(vectorA=pointInSpace, vectorB=lineStartPoint)  # Find vector from point in space to the start of the line
    dot = vecDot(vectorA=ac, vectorB=ba) # get the dotProduct between ac and ba
    percent = dot / l2 # Find petcentage from start to end point
   
    # if percent value is less than 0.0 or greater than 1.0, then the point in space outside of the line segment - exclude them
    if ignoreNoProjections == True:
        if percent < 0.0 or percent > 1.0:
            return [0.0, 0.0, 0.0] # Return a zero vector
   
    point = vecAdd(vectorA=lineStartPoint, vectorB=vecMultScalar(scalar=percent, vector=ac)) # Now we can find the closest point
    return point


Or use a nearestPointOnCurve node and when done - delete it.

Cheers
Sasha





--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d9b03275-9cf2-4ef4-8628-e6e3cf88377d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

s...@weacceptyou.com

unread,
Mar 30, 2016, 4:41:31 AM3/30/16
to Python Programming for Autodesk Maya
thanks Sasha, very interesting will try this out thanks,

Sam
Reply all
Reply to author
Forward
0 new messages