import math
import xrt
	
def dot(a, b):
    return (a[0]*b[0]+a[1]*b[1]+a[2]*b[2])
    
def cross(a, b):
	return (a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0])
	
def normalize(vec):
    len = math.sqrt(dot(vec, vec))	
    return (vec[0]/len, vec[1]/len, vec[2]/len)
	
def LookAt(pos, aim, up):
    dir = (aim[0] - pos[0], aim[1] - pos[1], aim[2] - pos[2])
    
    dir = normalize(dir)
    proj = dot(dir, up)
	
    newUp = normalize((up[0] - proj * dir[0], up[1] - proj * dir[1], up[2] - proj * dir[2]))
    right = cross(newUp, dir)
	
    r = xrt.CurrentRenderer
    r.AppendTransform(right[0], newUp[0], dir[0], 0, right[1], newUp[1], dir[1], 0, 
	    right[2], newUp[2], dir[2], 0, 0, 0, 0, 1)
    r.Translate(-pos[0], -pos[1], -pos[2])
