I'm looking for a good code example on calculating Barycentric coordinates on arbitrary polygons in maya using the API. I recently could calculate barycentric coordinates within a triangle like this:
def baryInterp(vecA, vecB, vecC, vecP):
'''
Calculates barycentricInterpolation of a point in a triangle.
:param vecA - OpenMaya.MVector of a vertex point.
:param vecB - OpenMaya.MVector of a vertex point.
:param vecC - OpenMaya.MVector of a vertex point.
:param vecP - OpenMaya.MVector of a point to be interpolated.
Returns list of 3 floats representing weight values per each point.
'''
v0 = vecB - vecA
v1 = vecC - vecA
v2 = vecP - vecA
d00 = v0 * v0
d01 = v0 * v1
d11 = v1 * v1
d20 = v2 * v0
d21 = v2 * v1
denom = d00 * d11 - d01 * d01
v = (d11 * d20 - d01 * d21) / denom
w = (d00 * d21 - d01 * d20) / denom
u = 1.0 - v - w
return [u, v, w]
I'd like to now figure out how to do this with any amount of points that represent a polygon. I'm not sure if i have to just calculate per triangle and some how get coordinates from that.
Any help would be appreciated.
It probably does have to be triangular, for two reasons:
Concavity: If you have a concave n-gon, you will have to test every coordinate to be sure it's inside. A barycentric is basically a unique combination of distances between points, and for a concave figure there will be many combinations that fall outside the bounds of the ngon.
Planarity: A triangle is always planar, so a barycentric coord is always on the triangle. Not true for ngons.
You could however do a barycentric coordinate for convex planar ngons. It's probably better to sumply triangulate the geometry and work with triangles which are simpler and more robust.