{"id":57,"date":"2012-08-15T14:52:00","date_gmt":"2012-08-15T14:52:00","guid":{"rendered":"http:\/\/www.dannywynne.com\/blog\/?page_id=57"},"modified":"2015-04-18T19:00:21","modified_gmt":"2015-04-18T19:00:21","slug":"57-2","status":"publish","type":"page","link":"http:\/\/www.dannywynne.com\/blog\/?page_id=57","title":{"rendered":"Mean Value Coordinates"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/dannywynne.com\/images\/Capture.JPG\" \/><br \/>\n[python]<br \/>\nimport maya.OpenMaya as om<br \/>\nfrom math import *<br \/>\nimport maya.cmds as cmds<\/p>\n<p>def wi(v,vi,tris):<br \/>\n\t#calculates the weights<br \/>\n\t#v = kernal of the starshaped polyhedron. this is the point that<br \/>\n\t#\twill be deformed<br \/>\n\t#vi = the current vert we are iterating over on the bounding polyhedron<br \/>\n\t#tris = all triangle facets on the polyhedron that vi is a member of<br \/>\n\tdef A(vr,vs):<br \/>\n\t\t#get an angle<br \/>\n\t\tvec1 = vr-v<br \/>\n\t\tvec2 = vs-v<br \/>\n\t\treturn vec1.angle(vec2)<br \/>\n\tdef N(vr, vs):<br \/>\n\t\t#get the normal<br \/>\n\t\tvecr = vr-v<br \/>\n\t\tvecs = vs-v<br \/>\n\t\tnorm = vecr^vecs<br \/>\n\t\tnorm.normalize()<br \/>\n\t\treturn norm<\/p>\n<p>\tdef ut(i,j,k):<br \/>\n\t\t#floater&#8217;s equation for ut<br \/>\n\t\ttop = A(j,k) + A(i,j)*( N(i, j)*N(j, k) ) + A(k,i)*(N(k, i)*N(j, k))<br \/>\n\t\tbot = 2*( (v-i)*N(j,k) )<br \/>\n\t\treturn top\/bot<\/p>\n<p>\tri = v.distanceTo(vi)<br \/>\n\t#pass all the triangles through floaters equation and sum up the results<br \/>\n\tut_sum = 0<br \/>\n\tfor t in tris:<br \/>\n\t\tut_sum += ut(t[0], t[1], t[2])<br \/>\n\treturn ut_sum\/ri<\/p>\n<p>class MVC():<br \/>\n\tdef __init__(self):<br \/>\n\t\t#this is our main vert, the one that will be deformed<br \/>\n\t\tself.v = u.pnt(&#8220;v&#8221;)<br \/>\n\t\tself.bary = self.get_bary()<\/p>\n<p>\tdef update(self):<br \/>\n\t\tv = self.v<br \/>\n\t\t#refresh the list p with the current location of the locators in maya<br \/>\n\t\tp = [u.pnt(&#8220;v0&#8221;), u.pnt(&#8220;v1&#8221;), u.pnt(&#8220;v2&#8221;), u.pnt(&#8220;v3&#8221;)]<br \/>\n\t\tx,y,z = 0,0,0<br \/>\n\t\tfor i in range(len(p)):<br \/>\n\t\t\tx += p[i].x*self.bary[i]<br \/>\n\t\t\ty += p[i].y*self.bary[i]<br \/>\n\t\t\tz += p[i].z*self.bary[i]<br \/>\n\t\t#place a new locator at the deformed position<br \/>\n\t\tu.loc(om.MPoint(x, y, z))<\/p>\n<p>\tdef get_bary(self):<br \/>\n\t\t#get the barycentric weights<br \/>\n\t\tv = self.v<br \/>\n\t\t#get 4 points from maya that represent a polygon<br \/>\n\t\tp = [u.pnt(&#8220;v0&#8221;), u.pnt(&#8220;v1&#8221;), u.pnt(&#8220;v2&#8221;), u.pnt(&#8220;v3&#8221;)]<br \/>\n\t\t#create a list of verts and all triangles that that vert<br \/>\n\t\t#\tis a memeber of<br \/>\n\t\t#the triangels are i,j,k, going counter clock wise, and<br \/>\n\t\t#\ti = the associated vert that will be iterated over<br \/>\n\t\tvert_tri=[<br \/>\n\t\t[p[0], [(p[0], p[1], p[2]), (p[0], p[3], p[1]), (p[0], p[2], p[3]),]],<br \/>\n\t\t[p[1], [(p[1], p[2], p[0]), (p[1], p[3], p[2]), (p[1], p[0], p[3]),]],<br \/>\n\t\t[p[2], [(p[2], p[0], p[1]), (p[2], p[1], p[3]), (p[2], p[3], p[0]),]],<br \/>\n\t\t[p[3], [(p[3], p[2], p[1]), (p[3], p[1], p[0]), (p[3], p[0], p[2]),]],<br \/>\n\t\t]<\/p>\n<p>\t\t#verify in maya that all triangles are counter clockwise,<br \/>\n\t\t#\tnormal outward, and begin with the same vert,<br \/>\n\t\t#\tthe one that will be iterated over<br \/>\n\t\tverify_triangles(vert_tri)<\/p>\n<p>\t\t#get the weight<br \/>\n\t\tweights = []<br \/>\n\t\tfor vt in new_vert_tri:<br \/>\n\t\t\tt= list(vt[1])<br \/>\n\t\t\tt.reverse()<br \/>\n\t\t\tweights.append(wi(v, vt[0], t))<\/p>\n<p>\t\t#get the bary weights<br \/>\n\t\tbary_weights=[]<br \/>\n\t\tweights_sum = sum(weights)<br \/>\n\t\tfor w in weights:<br \/>\n\t\t\tbary_weights.append(w\/weights_sum)<br \/>\n\t\treturn bary_weights<br \/>\n[\/python]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[python] import maya.OpenMaya as om from math import * import maya.cmds as cmds def wi(v,vi,tris): #calculates the weights #v = kernal of the starshaped polyhedron. this is the point that # will be deformed #vi = the current vert we are iterating over on the bounding polyhedron #tris = all triangle facets on the polyhedron [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-57","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.dannywynne.com\/blog\/index.php?rest_route=\/wp\/v2\/pages\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.dannywynne.com\/blog\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.dannywynne.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.dannywynne.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.dannywynne.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":0,"href":"http:\/\/www.dannywynne.com\/blog\/index.php?rest_route=\/wp\/v2\/pages\/57\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.dannywynne.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}