当前位置: 首页>>代码示例>>Python>>正文


Python Vector.mults方法代码示例

本文整理汇总了Python中Vector.Vector.mults方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.mults方法的具体用法?Python Vector.mults怎么用?Python Vector.mults使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Vector.Vector的用法示例。


在下文中一共展示了Vector.mults方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testRotationalQuaternion

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import mults [as 别名]
    def testRotationalQuaternion(self):
        'Test the quaternion representation of a rotation.'
        axis = Vector(1, 1, 1).normalize()
        angle = 2.0 # radians!
        q1 = Quaternion.forRotation(axis, angle)

        vv = math.sin(1.0) / (math.sqrt(3.0))
        cc = math.cos(1.0)
        q2 = Quaternion(cc, vv, vv, vv)
        assert q1.__str__() == q2.__str__(), '%s %s' % (q1, q2)

        hitError = False
        axis = axis.mults(1.2)
        try:
            q1 = Quaternion.forRotation(axis, angle)
        except ValueError, e:
            assert e.message == 'rotation axis must be a unit vector!'
            hitError = True
开发者ID:pmbrennan,项目名称:math3d,代码行数:20,代码来源:Quaternion.py

示例2: __init__

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import mults [as 别名]
class Quaternion:
    """Representation of a quaternion, defined as:

    s + ai + bj + ck
    or
    [s,v]

    where s,a,b,c are scalars, v is a vector, 

    and i, j, k are defined such that:
    i^2 = j^2 = k^2 = ijk = -1
    ij = k, jk = i, ki = j
    ji = -k, kj = -i, ik = -j
    """

    def __init__(self, s, a, b, c):
        self.mPrintSpec = '%f'
        self.mScalar = s
        self.mVector = Vector(a, b, c)

    @staticmethod
    def fromScalarVector(scalar, vector):
        """Define a quaternion from a scalar and a vector."""
        # TODO: Refactor for performance.
        return Quaternion(scalar, vector[0], vector[1], vector[2])

    def clone(self):
        v = self.mVector[:]
        return Quaternion(self.mScalar, v[0], v[1], v[2])

    def __str__(self):
        return '[ %s, %s ]' % (self.mPrintSpec % self.mScalar, self.mVector)

    def str2(self):
        """Alternate way to represent a Quaternion as a string."""
        signs = [ ('+' if f >= 0 else '-') for f in self.mVector ]
        vals = [ abs(f) for f in self.mVector ]

        return '%s %s %si %s %sj %s %sk' % (self.mScalar, 
                                            signs[0],
                                            vals[0],
                                            signs[1],
                                            vals[1],
                                            signs[2],
                                            vals[2])

    def __eq__(self, q):
        'Equality operator.'
        return self.mScalar == q.mScalar and self.mVector == q.mVector

    def __ne__(self, q):
        'Not equals'
        return not self.__eq__(q)

    def compare(self, seq):
        """Compare the quaternion to a sequence assumed to be in
        the form [ s, a, b, c ]."""
        return (len(seq) == 4 and 
                self.mScalar == seq[0] and self.mVector[0] == seq[1] and
                self.mVector[1] == seq[2] and self.mVector[2] == seq[3])

    def __add__(self, q):
        'Return self + q'
        return Quaternion(self.mScalar + q.mScalar, 
                          self.mVector[0] + q.mVector[0],
                          self.mVector[1] + q.mVector[1], 
                          self.mVector[2] + q.mVector[2])

    def __sub__(self, q):
        'Return self - q'
        return Quaternion(self.mScalar - q.mScalar, 
                          self.mVector[0] - q.mVector[0],
                          self.mVector[1] - q.mVector[1], 
                          self.mVector[2] - q.mVector[2])

    def scale(self, s):
        'Scale this quaternion by scalar s in-place.'
        self.mScalar = self.mScalar * float(s)
        self.mVector.scale(s)

    def mults(self, s):
        'Return self * scalar as a new Quaternion.'
        r = Quaternion.fromScalarVector(self.mScalar, self.mVector)
        r.scale(s)
        return r

    def mul1(self, q):
        """Multiplication Algorithm 1:
        This is a very nice definition of the quaternion multiplication
        operator, but it is terribly inefficient."""
        s = self.mScalar * q.mScalar - self.mVector.dot(q.mVector)
        v = q.mVector.mults(self.mScalar) + \
            self.mVector.mults(q.mScalar) + \
            self.mVector.cross(q.mVector)
        return Quaternion.fromScalarVector(s, v)

    def mul2(self, q):
        """Multiplication Algorithm 2: This is a much more efficient
        implementation of quaternion multiplication. It isover 3x faster than
        mul1."""
#.........这里部分代码省略.........
开发者ID:pmbrennan,项目名称:math3d,代码行数:103,代码来源:Quaternion.py


注:本文中的Vector.Vector.mults方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。