def __mul__(self, other): """Multiplication with a scalar or dot product. >>> a=vec4(1.0, 0.5, -1.8, 0.2) >>> b=vec4(-0.3, 0.75, 0.5, 0.3) >>> print a*2.0 (2.0000, 1.0000, -3.6000, 0.4000) >>> print 2.0*a (2.0000, 1.0000, -3.6000, 0.4000) >>> print a*b -0.765 """ T = type(other) # vec4*scalar if T in [float, int]: return vec4(self.x * other, self.y * other, self.z * other, self.w * other) # vec4*vec4 if isinstance(other, vec4): return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w # unsupported else: # Try to delegate the operation to the other operand if getattr(other, "__rmul__", None) != None: return other.__rmul__(self) else: raise TypeError("unsupported operand type for *")
def __pos__(self): """ >>> a=vec4(3.0, 2.5, -1.8, 0.2) >>> print +a (3.0000, 2.5000, -1.8000, 0.2000) """ return vec4(+self.x, +self.y, +self.z, +self.w)
def __mul__(self, other): """Multiplication with a scalar or dot product. >>> a=vec4(1.0, 0.5, -1.8, 0.2) >>> b=vec4(-0.3, 0.75, 0.5, 0.3) >>> print a*2.0 (2.0000, 1.0000, -3.6000, 0.4000) >>> print 2.0*a (2.0000, 1.0000, -3.6000, 0.4000) >>> print a*b -0.765 """ T = type(other) # vec4*scalar if T==types.FloatType or T==types.IntType or T==types.LongType: return vec4(self.x*other, self.y*other, self.z*other, self.w*other) # vec4*vec4 if isinstance(other, vec4): return self.x*other.x + self.y*other.y + self.z*other.z + self.w*other.w # unsupported else: # Try to delegate the operation to the other operand if getattr(other,"__rmul__",None)!=None: return other.__rmul__(self) else: raise TypeError, "unsupported operand type for *"
def __neg__(self): """Negation >>> a=vec4(3.0, 2.5, -1.8, 0.2) >>> print -a (-3.0000, -2.5000, 1.8000, -0.2000) """ return vec4(-self.x, -self.y, -self.z, -self.w)
def __mod__(self, other): """Modulo (component wise) >>> a=vec4(3.0, 2.5, -1.8, 0.2) >>> print a%2.0 (1.0000, 0.5000, 0.2000, 0.2000) """ T = type(other) # vec4%scalar if T==types.FloatType or T==types.IntType or T==types.LongType: return vec4(self.x%other, self.y%other, self.z%other, self.w%other) # vec4%vec4 if isinstance(other, vec4): return vec4(self.x%other.x, self.y%other.y, self.z%other.z, self.w%other.w) # unsupported else: raise TypeError, "unsupported operand type for %"
def normalize(self): """Return normalized vector. >>> a=vec4(1.0, 0.5, -1.8, 1.2) >>> print a.normalize() (0.4107, 0.2053, -0.7392, 0.4928) """ nlen = 1.0 / math.sqrt(self * self) return vec4(self.x * nlen, self.y * nlen, self.z * nlen, self.w * nlen)
def normalize(self): """Return normalized vector. >>> a=vec4(1.0, 0.5, -1.8, 1.2) >>> print a.normalize() (0.4107, 0.2053, -0.7392, 0.4928) """ nlen = 1.0/math.sqrt(self*self) return vec4(self.x*nlen, self.y*nlen, self.z*nlen, self.w*nlen)
def get_final_local_transform(self): pm = self.xformparent.get_world_transform() wt = self.get_world_transform() if not self.phys_root: wt = node.gettransformto(None, "inverse_initial_r") m = pm.inverse() * wt t, r, _ = m.decompose() q = quat(r).normalize() ps = pm.decompose()[2] pos = mat4.scaling(ps) * vec4(*t) return pos, q
def isortho(self): l = [vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1)] p = vec3(self.get_world_translation()[0:3]) t = self.get_world_transform() for i in range(3): l[i] = t * l[i] for i in range(3): for j in range(3): if i != j: k = ((i + 1) ^ (j + 1)) - 1 u = vec3(l[i][0:3]) - p v = vec3(l[j][0:3]) - p w = vec3(l[k][0:3]) - p s = u.cross(v) a = abs((s * w) / (s.length() * w.length())) if a <= 0.99: #print(i,j,k,u,v,w,s) #print("cos(a) of %s is %f." % (self.getFullName(), a)) return False return True
def isortho(self): l = [vec4(1,0,0,1), vec4(0,1,0,1), vec4(0,0,1,1)] p = vec3(self.get_world_translation()[0:3]) t = self.get_world_transform() for i in range(3): l[i] = t * l[i] for i in range(3): for j in range(3): if i != j: k = ((i+1)^(j+1))-1 u = vec3(l[i][0:3])-p v = vec3(l[j][0:3])-p w = vec3(l[k][0:3])-p s = u.cross(v) a = abs((s * w) / (s.length()*w.length())) if a <= 0.99: #print(i,j,k,u,v,w,s) #print("cos(a) of %s is %f." % (self.getFullName(), a)) return False return True
def __add__(self, other): """Vector addition. >>> a=vec4(1.0, 0.5, -1.8, 0.2) >>> b=vec4(-0.3, 0.75, 0.5, 0.3) >>> print a+b (0.7000, 1.2500, -1.3000, 0.5000) """ if isinstance(other, vec4): return vec4(self.x+other.x, self.y+other.y, self.z+other.z, self.w+other.w) else: raise TypeError, "unsupported operand type for +"
def __sub__(self, other): """Vector subtraction. >>> a=vec4(1.0, 0.5, -1.8, 0.2) >>> b=vec4(-0.3, 0.75, 0.5, 0.3) >>> print a-b (1.3000, -0.2500, -2.3000, -0.1000) """ if isinstance(other, vec4): return vec4(self.x-other.x, self.y-other.y, self.z-other.z, self.w-other.w) else: raise TypeError, "unsupported operand type for -"
def get_final_local_transform(self): pm = self.xformparent.get_world_transform() wt = self.get_world_transform() if not self.phys_root: wt = node.gettransformto(None, "inverse_initial_r") m = pm.inverse() * wt jointtype = self.get_fixed_attribute("joint", True) if self.is_phys_root and jointtype not in (None, "exclude"): m *= self._pointup_adjust_mat() t, r, _ = m.decompose() q = quat(r).normalize() ps = pm.decompose()[2] pos = mat4.scaling(ps) * vec4(*t) return pos, q
def __div__(self, other): """Division by scalar >>> a=vec4(1.0, 0.5, -1.8, 0.2) >>> print a/2.0 (0.5000, 0.2500, -0.9000, 0.1000) """ T = type(other) # vec4/scalar if T==types.FloatType or T==types.IntType or T==types.LongType: return vec4(self.x/other, self.y/other, self.z/other, self.w/other) # unsupported else: raise TypeError, "unsupported operand type for /"
def __truediv__(self, other): """Division by scalar >>> a=vec4(1.0, 0.5, -1.8, 0.2) >>> print a/2.0 (0.5000, 0.2500, -0.9000, 0.1000) """ T = type(other) # vec4/scalar if T in [float, int]: return vec4(self.x / other, self.y / other, self.z / other, self.w / other) # unsupported else: raise TypeError("unsupported operand type for /")
def scaleverts(group): for node in group: vtx = node.get_fixed_attribute("rgvtx", optional=True) if vtx: for p in node.getparents(): if not p.shape: p.shape = node mnode = node.getParent() meshroot = None m_tr = mnode.gettransformto(meshroot, "original", getparent=lambda n: n.getParent()) if not m_tr: print("Mesh crash!") m_s = m_tr.decompose()[2] transform = mat4.scaling(m_s) vp = vec4(0,0,0,1) for idx in range(0, len(vtx), 3): vp[:3] = vtx[idx:idx+3] vp = transform*vp vtx[idx:idx+3] = vp[:3]
def get_final_mesh_transform(self, physrootpos, bodies, verbose): phys = self.get_mesh_parentphys(bodies) if not phys: return None,None,None,None,None phys.writecount = 1 tm = self.get_world_transform() tp = phys.gettransformto(None, "actual", getparent=lambda n: n.getParent()) tmt, tmr, mscale = tm.decompose() tpt, tpr, _ = tp.decompose() q = quat(tpr.inverse()).normalize() p = tmt-tpt p = q.toMat4() * vec4(p[:]) if verbose: print(self.getName(), "has displacement", p, "compared to", phys.getName(), tmt, tpt) q = quat(tpr.inverse() * tmr).normalize() physidx = bodies.index(phys) if physidx == 0 and physrootpos: p -= physrootpos p = p[0:3] mscale = mscale.length() / (1+1+1)**.5 return phys,physidx,q,p,mscale
def get_world_pivot(self): return vec4(*self.get_world_pivot_transform().decompose()[0])
def __init__(self, *args): """Constructor. There are several possibilities how to initialize a vector: v = vec4() -> v = <0,0,0,0> v = vec4(a) -> v = <a,a,a,a> v = vec4(x,y) -> v = <x,y,0,0> v = vec4(x,y,z) -> v = <x,y,z,0> v = vec4(x,y,z,w) -> v = <x,y,z,w> Note that specifying just one value sets all four components to that value. Additionally you can wrap those values in a list or a tuple or specify them as a string: v = vec4([1,2,3]) -> v = <1,2,3,0> v = vec4("4,5") -> v = <4,5,0,0> """ if len(args)==0: self.x, self.y, self.z, self.w = (0.0, 0.0, 0.0, 0.0) elif len(args)==1: T = type(args[0]) # scalar if T==types.FloatType or T==types.IntType or T==types.LongType: f = float(args[0]) self.x, self.y, self.z, self.w = (f, f, f, f) # vec4 elif isinstance(args[0], vec4): self.x, self.y, self.z, self.w = args[0] # Tuple/List elif T==types.TupleType or T==types.ListType: if len(args[0])==0: self.x = self.y = self.z = self.w = 0.0 elif len(args[0])==1: self.x = self.y = self.z = self.w = float(args[0][0]) elif len(args[0])==2: self.x = float(args[0][0]) self.y = float(args[0][1]) self.z = 0.0 self.w = 0.0 elif len(args[0])==3: x,y,z = args[0] self.x = float(x) self.y = float(y) self.z = float(z) self.w = 0.0 elif len(args[0])==4: x,y,z,w = args[0] self.x = float(x) self.y = float(y) self.z = float(z) self.w = float(w) else: raise TypeError, "vec4() takes at most 4 arguments" # String elif T==types.StringType: s=args[0].replace(","," ").replace(" "," ").strip().split(" ") if s==[""]: s=[] f=map(lambda x: float(x), s) dummy = vec4(f) self.x, self.y, self.z, self.w = dummy # error else: raise TypeError,"vec4() arg can't be converted to vec4" elif len(args)==2: self.x, self.y = (float(args[0]), float(args[1])) self.z, self.w = (0.0, 0.0) elif len(args)==3: x,y,z = args self.x = float(x) self.y = float(y) self.z = float(z) self.w = 0.0 elif len(args)==4: x,y,z,w = args self.x = float(x) self.y = float(y) self.z = float(z) self.w = float(w) else: raise TypeError, "vec4() takes at most 4 arguments"
def __init__(self, *args): """Constructor. There are several possibilities how to initialize a vector: v = vec4() -> v = <0,0,0,0> v = vec4(a) -> v = <a,a,a,a> v = vec4(x,y) -> v = <x,y,0,0> v = vec4(x,y,z) -> v = <x,y,z,0> v = vec4(x,y,z,w) -> v = <x,y,z,w> Note that specifying just one value sets all four components to that value. Additionally you can wrap those values in a list or a tuple or specify them as a string: v = vec4([1,2,3]) -> v = <1,2,3,0> v = vec4("4,5") -> v = <4,5,0,0> """ if len(args) == 0: self.x, self.y, self.z, self.w = (0.0, 0.0, 0.0, 0.0) elif len(args) == 1: T = type(args[0]) # scalar if T == types.FloatType or T == types.IntType or T == types.LongType: self.x, self.y, self.z, self.w = (args[0], args[0], args[0], args[0]) # vec4 elif isinstance(args[0], vec4): self.x, self.y, self.z, self.w = args[0] # Tuple/List elif T == types.TupleType or T == types.ListType: if len(args[0]) == 0: self.x = self.y = self.z = self.w = 0.0 elif len(args[0]) == 1: self.x = self.y = self.z = args[0][0] self.w = 0.0 elif len(args[0]) == 2: self.x, self.y = args[0] self.z = 0.0 self.w = 0.0 elif len(args[0]) == 3: self.x, self.y, self.z = args[0] self.w = 0.0 elif len(args[0]) == 4: self.x, self.y, self.z, self.w = args[0] else: raise TypeError, "vec4() takes at most 4 arguments" # String elif T == types.StringType: s = args[0].replace(",", " ").replace(" ", " ").strip().split(" ") if s == [""]: s = [] f = map(lambda x: float(x), s) dummy = vec4(f) self.x, self.y, self.z, self.w = dummy # error else: raise TypeError, "vec4() arg can't be converted to vec4" elif len(args) == 2: self.x, self.y = args self.z, self.w = (0.0, 0.0) elif len(args) == 3: self.x, self.y, self.z = args self.w = 0.0 elif len(args) == 4: self.x, self.y, self.z, self.w = args else: raise TypeError, "vec4() takes at most 4 arguments"
def get_world_translation(self): return vec4(*self.get_world_transform().decompose()[0])
def __init__(self, *args): """Constructor. There are several possibilities how to initialize a vector: v = vec4() -> v = <0,0,0,0> v = vec4(a) -> v = <a,a,a,a> v = vec4(x,y) -> v = <x,y,0,0> v = vec4(x,y,z) -> v = <x,y,z,0> v = vec4(x,y,z,w) -> v = <x,y,z,w> Note that specifying just one value sets all four components to that value. Additionally you can wrap those values in a list or a tuple or specify them as a string: v = vec4([1,2,3]) -> v = <1,2,3,0> v = vec4("4,5") -> v = <4,5,0,0> """ if len(args) == 0: self.x, self.y, self.z, self.w = (0.0, 0.0, 0.0, 0.0) elif len(args) == 1: T = type(args[0]) # scalar if T in [float, int]: f = float(args[0]) self.x, self.y, self.z, self.w = (f, f, f, f) # vec4 elif isinstance(args[0], vec4): self.x, self.y, self.z, self.w = args[0] # Tuple/List elif T in [tuple, list]: if len(args[0]) == 0: self.x = self.y = self.z = self.w = 0.0 elif len(args[0]) == 1: self.x = self.y = self.z = self.w = float(args[0][0]) elif len(args[0]) == 2: self.x = float(args[0][0]) self.y = float(args[0][1]) self.z = 0.0 self.w = 0.0 elif len(args[0]) == 3: x, y, z = args[0] self.x = float(x) self.y = float(y) self.z = float(z) self.w = 0.0 elif len(args[0]) == 4: x, y, z, w = args[0] self.x = float(x) self.y = float(y) self.z = float(z) self.w = float(w) else: raise TypeError("vec4() takes at most 4 arguments") # String elif T is str: s = args[0].replace(",", " ").replace(" ", " ").strip().split(" ") if s == [""]: s = [] f = list([float(x) for x in s]) dummy = vec4(f) self.x, self.y, self.z, self.w = dummy # error else: raise TypeError("vec4() arg can't be converted to vec4") elif len(args) == 2: self.x, self.y = (float(args[0]), float(args[1])) self.z, self.w = (0.0, 0.0) elif len(args) == 3: x, y, z = args self.x = float(x) self.y = float(y) self.z = float(z) self.w = 0.0 elif len(args) == 4: x, y, z, w = args self.x = float(x) self.y = float(y) self.z = float(z) self.w = float(w) else: raise TypeError("vec4() takes at most 4 arguments")