Mean Value Coordinates

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 that vi is a member of
	def A(vr,vs):
		#get an angle
		vec1 = vr-v
		vec2 = vs-v
		return vec1.angle(vec2)
	def N(vr, vs):
		#get the normal
		vecr = vr-v
		vecs = vs-v
		norm = vecr^vecs
		norm.normalize()
		return norm
	
	def ut(i,j,k):
		#floater's equation for ut
		top = A(j,k) + A(i,j)*( N(i, j)*N(j, k) ) + A(k,i)*(N(k, i)*N(j, k))
		bot = 2*( (v-i)*N(j,k) )
		return top/bot

	ri = v.distanceTo(vi)
	#pass all the triangles through floaters equation and sum up the results
	ut_sum = 0
	for t in tris:
		ut_sum += ut(t[0], t[1], t[2])
	return ut_sum/ri

class MVC():
	def __init__(self):
		#this is our main vert, the one that will be deformed
		self.v = u.pnt("v")
		self.bary = self.get_bary()

	def update(self):
		v = self.v
		#refresh the list p with the current location of the locators in maya
		p = [u.pnt("v0"), u.pnt("v1"), u.pnt("v2"), u.pnt("v3")]
		x,y,z = 0,0,0
		for i in range(len(p)):
			x += p[i].x*self.bary[i]
			y += p[i].y*self.bary[i]
			z += p[i].z*self.bary[i]
		#place a new locator at the deformed position
		u.loc(om.MPoint(x, y, z))

	def get_bary(self):
		#get the barycentric weights
		v = self.v
		#get 4 points from maya that represent a polygon
		p = [u.pnt("v0"), u.pnt("v1"), u.pnt("v2"), u.pnt("v3")]
		#create a list of verts and all triangles that that vert 
		#	is a memeber of
		#the triangels are i,j,k, going counter clock wise, and 
		#	i = the associated vert that will be iterated over
		vert_tri=[
		[p[0], [(p[0], p[1], p[2]), (p[0], p[3], p[1]), (p[0], p[2], p[3]),]],
		[p[1], [(p[1], p[2], p[0]), (p[1], p[3], p[2]), (p[1], p[0], p[3]),]],
		[p[2], [(p[2], p[0], p[1]), (p[2], p[1], p[3]), (p[2], p[3], p[0]),]],
		[p[3], [(p[3], p[2], p[1]), (p[3], p[1], p[0]), (p[3], p[0], p[2]),]],
		]

		#verify in maya that all triangles are counter clockwise, 
		#	normal outward, and begin with the same vert, 
		#	the one that will be iterated over
		verify_triangles(vert_tri)

		#get the weight
		weights = []
		for vt in new_vert_tri:
			t= list(vt[1])
			t.reverse()
			weights.append(wi(v, vt[0], t))

		#get the bary weights
		bary_weights=[]
		weights_sum = sum(weights)
		for w in weights:
			bary_weights.append(w/weights_sum)
		return bary_weights

Leave a Reply

Your email address will not be published. Required fields are marked *