扩展重心坐标

时间:2014-07-01 15:20:15

标签: python api coordinates maya

基本上我发布了这个问题Joint Weight Interpolation Maya。我的这个工作类似于maya的复制皮肤重量。不幸的是,这仅适用于三角形,并且与maya的方法完全相同。如何扩展此重心坐标以使用多边形?有没有一种方法可以让你获得每个三角形的重心坐标,并从那里总结出值?我知道maya对于在多边形内每个点进行线性插值有一些答案,但我不确定如何。

1 个答案:

答案 0 :(得分:1)

我最终为此做的是实施this,它似乎与maya的复制皮肤重量相关。我确实检查了多边形是否是凸的,如果它是,它只是在最近的三角形上使用常规的重心坐标。

此处根据@Paul的请求进行更新。我想到了这一点并希望它有所帮助。我认为这是一个非常早期的测试。

import maya.cmds as cmds
import maya.OpenMaya as OpenMaya

if __name__ == '__main__':

    pnts = []

    p1 = OpenMaya.MVector(*cmds.xform("p0",
                          query=True,
                          worldSpace=True,
                          translation=True))

    p2 = OpenMaya.MVector(*cmds.xform("p1",
                          query=True,
                          worldSpace=True,
                          translation=True))

    p3 = OpenMaya.MVector(*cmds.xform("p2",
                          query=True,
                          worldSpace=True,
                          translation=True))

    p4 = OpenMaya.MVector(*cmds.xform("p3",
                          query=True,
                          worldSpace=True,
                          translation=True))
    p5 = OpenMaya.MVector(*cmds.xform("p4",
                          query=True,
                          worldSpace=True,
                          translation=True))
    pnts.append(p1)
    pnts.append(p2)
    pnts.append(p3)
    pnts.append(p4)
    pnts.append(p5)

    p = OpenMaya.MVector(*cmds.xform("p",
                         query=True,
                         worldSpace=True,
                         translation=True))

    vertCount = len(pnts)

    weights = []

    si = []
    for i, pnt in enumerate(pnts):
        si.append(pnts[i] - p)

    Ri = []
    Ai = []
    Di = []

    for i, pnt in enumerate(pnts):

        iPlus = (i+1) % vertCount

        ri = si[i].length()
        ai = (si[i] ^ si[iPlus]).length()/2.0
        di = si[i] * si[iPlus]

        if round(ri, 7) == 0.0:
            print("%s : 1.0." % i)
            Ri = []
            Ai = []
            Di = []
            break

        elif round(ai, 7) == 0.0 and round(di, 7) < 0.0:
            riPlus = si[iPlus].length()
            print("%s : %s" % (i, ri/(ri+riPlus)))
            print("%s : %s" % (iPlus, riPlus/(ri+riPlus)))
            Ri = []
            Ai = []
            Di = []
            break

        else:
            Ri.append(ri)
            Ai.append(ai)
            Di.append(di)

    if not len(Ri) == 0 or not len(Ai) == 0 or not len(Di) == 0:

        for i, pnt in enumerate(pnts):

            iPlus = (i+1) % vertCount
            iMinus = (i-1) % vertCount

            w = 0.0

            if not Ai[iMinus] == 0.0:
                w += (Ri[iMinus] - Di[iMinus] / Ri[i]) / Ai[iMinus]

            if not Ai[i] == 0.0:
                w += (Ri[iPlus] - Di[i] / Ri[i]) / Ai[i]

            weights.append(w)

        weightSum = sum(weights)
        finalWeights = [weight/weightSum for weight in weights]

        print("Weights: ", finalWeights)