本文整理汇总了Python中vec3._vec3函数的典型用法代码示例。如果您正苦于以下问题:Python _vec3函数的具体用法?Python _vec3怎么用?Python _vec3使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_vec3函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lookAt
def lookAt(pos, target, up=_vec3(0,0,1)):
"""Look from pos to target.
The resulting transformation moves the origin to pos and
rotates so that the z-axis points to target. The y-axis is
as close as possible to the up vector.
"""
pos = _vec3(pos)
target = _vec3(target)
up = _vec3(up)
dir = (target - pos).normalize()
up = up.normalize()
up -= (up * dir) * dir
try:
up = up.normalize()
except:
# We're looking along the up direction, so choose
# an arbitrary direction that is perpendicular to dir
# as new up.
up = dir.ortho()
right = up.cross(dir).normalize()
return mat4(right.x, up.x, dir.x, pos.x,
right.y, up.y, dir.y, pos.y,
right.z, up.z, dir.z, pos.z,
0.0, 0.0, 0.0, 1.0)
示例2: rotateVec
def rotateVec(self, v):
"""Return the rotated vector v.
The quaternion must be a unit quaternion.
This operation is equivalent to turning v into a quat, computing
self*v*self.conjugate() and turning the result back into a vec3.
"""
u = _vec3(v[:3])
ww = self.w*self.w
xx = self.x*self.x
yy = self.y*self.y
zz = self.z*self.z
wx = self.w*self.x
wy = self.w*self.y
wz = self.w*self.z
xy = self.x*self.y
xz = self.x*self.z
yz = self.y*self.z
u = (ww*u.x + xx*u.x - yy*u.x - zz*u.x + 2*((xy-wz)*u.y + (xz+wy)*u.z),
ww*u.y - xx*u.y + yy*u.y - zz*u.y + 2*((xy+wz)*u.x + (yz-wx)*u.z),
ww*u.z - xx*u.z - yy*u.z + zz*u.z + 2*((xz-wy)*u.x + (yz+wx)*u.y))
if isinstance(v, _vec4):
return _vec4(u)
return _vec3(u)
示例3: decompose
def decompose(self):
"""Decomposes the matrix into a translation, rotation and scaling part.
Returns a tuple (translation, rotation, scaling). The
translation and scaling parts are given as vec3's, the rotation
is still given as a mat4.
"""
dummy = self.ortho()
dummy.setRow(3,_vec4(0.0, 0.0, 0.0, 1.0))
dummy.setColumn(3,_vec4(0.0, 0.0, 0.0, 1.0))
x = dummy.getColumn(0)
y = dummy.getColumn(1)
z = dummy.getColumn(2)
xl = x.length()
yl = y.length()
zl = z.length()
scale = _vec3(xl,yl,zl)
x/=xl
y/=yl
z/=zl
dummy.setColumn(0,x)
dummy.setColumn(1,y)
dummy.setColumn(2,z)
if dummy.determinant()<0.0:
dummy.setColumn(0,-x)
scale.x=-scale.x
return (_vec3(self.mlist[3], self.mlist[7], self.mlist[11]),
dummy,
scale)
示例4: getColumn
def getColumn(self, idx):
"""Return a column (as vec3)."""
if idx==0: return _vec3(self.mlist[0], self.mlist[3], self.mlist[6])
elif idx==1: return _vec3(self.mlist[1], self.mlist[4], self.mlist[7])
elif idx==2: return _vec3(self.mlist[2], self.mlist[5], self.mlist[8])
else:
raise IndexError,"index out of range"
示例5: decompose
def decompose(self):
"""Decomposes the matrix into a rotation and scaling part.
Returns a tuple (rotation, scaling). The scaling part is given
as a vec3, the rotation is still a mat3.
"""
try:
dummy = self.ortho()
except ZeroDivisionError:
return (mat3(1.0), _vec3(0))
x = dummy.getColumn(0)
y = dummy.getColumn(1)
z = dummy.getColumn(2)
xl = x.length()
yl = y.length()
zl = z.length()
scale = _vec3(xl,yl,zl)
x/=xl
y/=yl
z/=zl
dummy.setColumn(0,x)
dummy.setColumn(1,y)
dummy.setColumn(2,z)
if dummy.determinant()<0.0:
dummy.setColumn(0,-x)
scale.x=-scale.x
return (dummy, scale)
示例6: ortho
def ortho(self):
"""Return a matrix with orthogonal base vectors.
Makes the x-, y- and z-axis orthogonal.
The fourth column and row remain untouched.
"""
m11,m12,m13,m14,m21,m22,m23,m24,m31,m32,m33,m34,m41,m42,m43,m44 = self.mlist
x = _vec3(m11, m21, m31)
y = _vec3(m12, m22, m32)
z = _vec3(m13, m23, m33)
xl = x.length()
xl*=xl
y = y - ((x*y)/xl)*x
z = z - ((x*z)/xl)*x
yl = y.length()
yl*=yl
z = z - ((y*z)/yl)*y
return mat4( x.x, y.x, z.x, m14,
x.y, y.y, z.y, m24,
x.z, y.z, z.z, m34,
m41, m42, m43, m44)
示例7: __getitem__
def __getitem__(self, key):
"""Return a column or an individual element."""
if key==0: return _vec3(self.mlist[0],self.mlist[3],self.mlist[6])
elif key==1: return _vec3(self.mlist[1],self.mlist[4],self.mlist[7])
elif key==2: return _vec3(self.mlist[2],self.mlist[5],self.mlist[8])
elif type(key)==types.TupleType:
i,j=key
if i<0 or i>2 or j<0 or j>2:
raise IndexError, "index out of range"
return self.mlist[i*3+j]
else:
raise IndexError,"index out of range"
示例8: getColumn
def getColumn(self, index):
"""Return a column (as vec3)."""
if type(index)==int:
if index==0:
return _vec3(self.mlist[0], self.mlist[3], self.mlist[6])
elif index==1:
return _vec3(self.mlist[1], self.mlist[4], self.mlist[7])
elif index==2:
return _vec3(self.mlist[2], self.mlist[5], self.mlist[8])
else:
raise IndexError("index out of range")
else:
raise TypeError("index must be an integer")
示例9: getRow
def getRow(self, index):
"""Return a row (as vec3)."""
if type(index)==int:
if index==0:
return _vec3(self.mlist[0], self.mlist[1], self.mlist[2])
elif index==1:
return _vec3(self.mlist[3], self.mlist[4], self.mlist[5])
elif index==2:
return _vec3(self.mlist[6], self.mlist[7], self.mlist[8])
else:
raise IndexError,"index out of range"
else:
raise TypeError,"index must be an integer"
示例10: rotation
def rotation(angle, axis):
"""Return rotation matrix.
angle must be given in radians. axis should be of type vec3.
"""
axis = _vec3(axis)
sqr_a = axis.x*axis.x
sqr_b = axis.y*axis.y
sqr_c = axis.z*axis.z
len2 = sqr_a+sqr_b+sqr_c
k2 = math.cos(angle)
k1 = (1.0-k2)/len2
k3 = math.sin(angle)/math.sqrt(len2)
k1ab = k1*axis.x*axis.y
k1ac = k1*axis.x*axis.z
k1bc = k1*axis.y*axis.z
k3a = k3*axis.x
k3b = k3*axis.y
k3c = k3*axis.z
return mat4( k1*sqr_a+k2, k1ab-k3c, k1ac+k3b, 0.0,
k1ab+k3c, k1*sqr_b+k2, k1bc-k3a, 0.0,
k1ac-k3b, k1bc+k3a, k1*sqr_c+k2, 0.0,
0.0, 0.0, 0.0, 1.0)
示例11: __rmul__
def __rmul__(self, other):
T = type(other)
# scalar*mat4
if T==types.FloatType or T==types.IntType or T==types.LongType:
return mat4(map(lambda x,other=other: other*x, self.mlist))
# vec4*mat4
if isinstance(other, _vec4):
m11,m12,m13,m14,m21,m22,m23,m24,m31,m32,m33,m34,m41,m42,m43,m44 = self.mlist
return _vec4(other.x*m11 + other.y*m21 + other.z*m31 + other.w*m41,
other.x*m12 + other.y*m22 + other.z*m32 + other.w*m42,
other.x*m13 + other.y*m23 + other.z*m33 + other.w*m43,
other.x*m14 + other.y*m24 + other.z*m34 + other.w*m44)
# vec3*mat4
if isinstance(other, _vec3):
m11,m12,m13,m14,m21,m22,m23,m24,m31,m32,m33,m34,m41,m42,m43,m44 = self.mlist
w = float(other.x*m14 + other.y*m24 + other.z*m34 + m44)
return _vec3(other.x*m11 + other.y*m21 + other.z*m31 + m41,
other.x*m12 + other.y*m22 + other.z*m32 + m42,
other.x*m13 + other.y*m23 + other.z*m33 + m43)/w
# mat4*mat4
if isinstance(other, mat4):
return self.__mul__(other)
# unsupported
else:
raise TypeError("unsupported operand type for *")
示例12: __mul__
def __mul__(self, other):
T = type(other)
# mat3*scalar
if T==types.FloatType or T==types.IntType or T==types.LongType:
return mat3(map(lambda x,other=other: x*other, self.mlist))
# mat3*vec3
if isinstance(other, _vec3):
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
return _vec3(m11*other.x + m12*other.y + m13*other.z,
m21*other.x + m22*other.y + m23*other.z,
m31*other.x + m32*other.y + m33*other.z)
# mat3*mat3
if isinstance(other, mat3):
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
n11,n12,n13,n21,n22,n23,n31,n32,n33 = other.mlist
return mat3( m11*n11+m12*n21+m13*n31,
m11*n12+m12*n22+m13*n32,
m11*n13+m12*n23+m13*n33,
m21*n11+m22*n21+m23*n31,
m21*n12+m22*n22+m23*n32,
m21*n13+m22*n23+m23*n33,
m31*n11+m32*n21+m33*n31,
m31*n12+m32*n22+m33*n32,
m31*n13+m32*n23+m33*n33)
# unsupported
else:
raise TypeError, "unsupported operand type for *"
示例13: ortho
def ortho(self):
"""Return a matrix with orthogonal base vectors.
"""
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
x = _vec3(m11, m21, m31)
y = _vec3(m12, m22, m32)
z = _vec3(m13, m23, m33)
xl = x.length()
xl*=xl
y = y - ((x*y)/xl)*x
z = z - ((x*z)/xl)*x
yl = y.length()
yl*=yl
z = z - ((y*z)/yl)*y
return mat3( x.x, y.x, z.x,
x.y, y.y, z.y,
x.z, y.z, z.z)
示例14: toAngleAxis
def toAngleAxis(self):
"""Return angle (in radians) and rotation axis.
>>> q=quat(0.9, 0.5, 0.2, 0.3)
>>> angle, axis = q.toAngleAxis()
>>> print round(angle,4)
1.2011
>>> print axis
(0.8111, 0.3244, 0.4867)
"""
nself = self.normalize()
# Clamp nself.w (since the quat has to be normalized it should
# be between -1 and 1 anyway, but it might be slightly off due
# to numerical inaccuracies)
w = max(min(nself.w,1.0),-1.0)
w = math.acos(w)
s = math.sin(w)
if s<1E-12:
return (0.0, _vec3(0.0,0.0,0.0))
return (2.0*w, _vec3(nself.x/s, nself.y/s, nself.z/s))
示例15: __rmul__
def __rmul__(self, other):
T = type(other)
# scalar*mat3
if T==types.FloatType or T==types.IntType or T==types.LongType:
return mat3(map(lambda x,other=other: other*x, self.mlist))
# vec3*mat3
if isinstance(other, _vec3):
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
return _vec3(other.x*m11 + other.y*m21 + other.z*m31,
other.x*m12 + other.y*m22 + other.z*m32,
other.x*m13 + other.y*m23 + other.z*m33)
# mat3*mat3
if isinstance(other, mat3):
return self.__mul__(other)
# unsupported
else:
raise TypeError, "unsupported operand type for *"