PDA

View Full Version : Closest point on plane?

 faultymoose10-01-2006, 07:55 AMHi code gurus! I've been working on a script for work that runs a few procedures on a joint chain, and I *thought* I had figured this step out, but alas my success was accidental and reliant upon a limited situation. What I need to do is fairly simple in description: I have 4 locators. I need to snap the fourth locator to the nearest point on the plane described by the first three. I've tried a variety of solutions, but unfortunately my grasp of vector mathematics is somewhat limited, and my hacked solutions didn't work too well either. I hope someone can help me out with this, it'd be hugely appreciated! Thankyou!
NateH
10-01-2006, 08:28 AM
You could try creating a poly surface between the three points and then creating a geometry constraint. havn't tested it though since i have no access to maya atm.

faultymoose
10-01-2006, 08:44 AM
Will this work if the fourth null is outside the area of the first three?

NateH
10-02-2006, 05:54 AM
Should. it will pop the fourth locator to the surface at the closest point on it.

faultymoose
10-02-2006, 08:12 AM
Unfortunately that's not what I want. I want the fourth locator to be popped to the closest point on the plane defined by the first three.

Ie. imagine an infinite plane that runs through the first three locators. The fourth should move to the closest point on that imaginary infinite plane, making all four planar.

Moving it to the closest point on a triangle defined by the first three WOULD make it planar, but it would force it to within the boundary of the first three, which is not what I am after. For example, say the fourth locator is a few meters away from the first three - I still want it to be popped into the nearest planar position, but also staying as close to it's original position as possible.

I guess an ugly hack could be to generate that polygon, center it's pivot, and then scale it to an enormous size before snapping to the nearest point on that poly. This wouldn't be a perfect solution, but it would work so long as the poly was scaled larger than the boundary defined by all four locators, right?

faultymoose
10-02-2006, 11:34 AM
Okay I've tried centering the pivot - even creating a cluster from the vertices of the triangle generated - and the pivot is centered in the bounding box. Any idea how I would calculate the exact center of a triangular polygon (or the center of three coordinates)?

faultymoose
10-02-2006, 11:43 AM
Nevermind, I'm a bit dense tonight:

centerX = (x1 + x2 + x3)/3;
centerY = (y1 + y2 + y3)/3;
centerZ = (z1 + z2 + z3)/3;

faultymoose
10-02-2006, 12:23 PM
It all works now!

/cheer

Thanks for the tip with the polygon. I just scale it up 100x. It's extremely unlikely I'd ever want this script to work outside the boundary this creates.

:)

NateH
10-02-2006, 09:23 PM
Cool, glad it worked for you. :bounce: :applause:

faultymoose
10-02-2006, 11:38 PM
I am having another problem that someone might be able to help with:

My script runs fine, works perfect, until I make it a global procedure (which ideally I'd like to do).

Then I get an error message: Cannot find procedure "nearestPointOnMesh"

The plugin is loaded, and works fine if I execute my procedure as a script.

Ideas?

benject
10-03-2006, 05:56 PM
uhh..i think you should put the script on the correct directory?