# Copyright (c) 2004 Nanorex, Inc. All rights reserved. """ VQT.py Vectors, Quaternions, and Trackballs Vectors are a simplified interface to the Numeric arrays. A relatively full implementation of Quaternions. Trackball produces incremental quaternions using a mapping of the screen onto a sphere, tracking the cursor on the sphere. $Id$ """ import math, types from math import * from Numeric import * from LinearAlgebra import * intType = type(2) floType = type(2.0) numTypes = [intType, floType] def V(*v): return array(v, Float) def A(a): return array(a, Float) def cross(v1, v2): return V(v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]) def vlen(v1): return sqrt(dot(v1, v1)) def norm(v1): lng = vlen(v1) if lng: return v1 / lng # bruce 041012 optimized this by using lng instead of # recomputing vlen(v1) -- code was v1 / vlen(v1) else: return v1+0 # p1 and p2 are points, v1 is a direction vector from p1. # return (dist, wid) where dist is the distance from p1 to p2 # measured in the direction of v1, and wid is the orthogonal # distance from p2 to the p1-v1 line. # v1 should be a unit vector. def orthodist(p1, v1, p2): dist = dot(v1, p2-p1) wid = vlen(p1+dist*v1-p2) return (dist, wid) class Q: """Q(W, x, y, z) is the quaternion with axis vector x,y,z and sin(theta/2) = W (e.g. Q(1,0,0,0) is no rotation) Q(x, y, z) where x, y, and z are three orthonormal vectors is the quaternion that rotates the standard axes into that reference frame. (the frame has to be right handed, or there's no quaternion that can do it!) Q(V(x,y,z), theta) is what you probably want. Q(vector, vector) gives the quat that rotates between them """ def __init__(self, x, y=None, z=None, w=None): # 4 numbers if w != None: self.vec=V(x,y,z,w) elif z: # three axis vectors # Just use first two a100 = V(1,0,0) c1 = cross(a100,x) if vlen(c1)<0.000001: self.vec = Q(y,z).vec return ax1 = norm((a100+x)/2.0) x2 = cross(ax1,c1) a010 = V(0,1,0) c2 = cross(a010,y) if vlen(c2)<0.000001: self.vec = Q(x,z).vec return ay1 = norm((a010+y)/2.0) y2 = cross(ay1,c2) axis = cross(x2, y2) nw = sqrt(1.0 + x[0] + y[1] + z[2])/2.0 axis = norm(axis)*sqrt(1.0-nw**2) self.vec = V(nw, axis[0], axis[1], axis[2]) elif type(y) in numTypes: # axis vector and angle v = (x / vlen(x)) * sin(y*0.5) self.vec = V(cos(y*0.5), v[0], v[1], v[2]) elif y: # rotation between 2 vectors x = norm(x) y = norm(y) v = cross(x, y) theta = acos(min(1.0,max(-1.0,dot(x, y)))) if dot(y, cross(x, v)) > 0.0: theta = 2.0 * pi - theta w=cos(theta*0.5) vl = vlen(v) # null rotation if w==1.0: self.vec=V(1, 0, 0, 0) # opposite pole elif vl<0.000001: ax1 = cross(x,V(1,0,0)) ax2 = cross(x,V(0,1,0)) if vlen(ax1)>vlen(ax2): self.vec = norm(V(0, ax1[0],ax1[1],ax1[2])) else: self.vec = norm(V(0, ax2[0],ax2[1],ax2[2])) else: s=sqrt(1-w**2)/vl self.vec=V(w, v[0]*s, v[1]*s, v[2]*s) elif type(x) in numTypes: # just one number self.vec=V(1, 0, 0, 0) else: self.vec=V(x[0], x[1], x[2], x[3]) self.counter = 50 def __getattr__(self, name): if name == 'w': return self.vec[0] elif name in ('x', 'i'): return self.vec[1] elif name in ('y', 'j'): return self.vec[2] elif name in ('z', 'k'): return self.vec[3] elif name == 'angle': if -1.00.0001: return V(s*x/d, s*y/d, cos(theta)) else: return V(0.0, 0.0, 1.0) class Trackball: '''A trackball object. The current transformation matrix can be retrieved using the "matrix" attribute.''' def __init__(self, wide, high): '''Create a Trackball object. "size" is the radius of the inner trackball sphere. ''' self.w2=wide/2.0 self.h2=high/2.0 self.scale = 1.1 / min(wide/2.0, high/2.0) self.quat = Q(1,0,0,0) self.oldmouse = None def rescale(self, wide, high): self.w2=wide/2.0 self.h2=high/2.0 self.scale = 1.1 / min(wide/2.0, high/2.0) def start(self, px, py): self.oldmouse=proj2sphere((px-self.w2)*self.scale, (self.h2-py)*self.scale) def update(self, px, py, uq=None): newmouse = proj2sphere((px-self.w2)*self.scale, (self.h2-py)*self.scale) if self.oldmouse and not uq: quat = Q(self.oldmouse, newmouse) elif self.oldmouse and uq: quat = uq + Q(self.oldmouse, newmouse) - uq else: quat = Q(1,0,0,0) self.oldmouse = newmouse return quat def ptonline(xpt, lpt, ldr): """return the point on a line (point lpt, direction ldr) nearest to point xpt """ ldr = norm(ldr) return dot(xpt-lpt,ldr)*ldr + lpt def planeXline(ppt, pv, lpt, lv): """find the intersection of a line (point lpt, vector lv) with a plane (point ppt, normal pv) return None if (almost) parallel (warning to callers: retvals other than None might still be false, e.g. V(0,0,0) -- untested, but likely; so don't use retval as boolean) """ d=dot(lv,pv) if abs(d)<0.000001: return None return lpt+lv*(dot(ppt-lpt,pv)/d) def cat(a,b): """concatenate two arrays (the NumPy version is a mess) """ if not a: return b if not b: return a r1 = shape(a) r2 = shape(b) if len(r1) == len(r2): return concatenate((a,b)) if len(r1)