コード例 #1
0
ファイル: DraftVecUtils.py プロジェクト: OLGGL/Model
def rotate(u,angle,axis=Vector(0,0,1)):
    '''rotate(Vector,Float,axis=Vector): rotates the first Vector
    around the given axis, at the given angle.
    If axis is omitted, the rotation is made on the xy plane.'''
    typecheck ([(u,Vector), (angle,(int,float)), (axis,Vector)], "rotate")

    if angle == 0: 
        return u

    l=axis.Length
    x=axis.x/l
    y=axis.y/l
    z=axis.z/l
    c = math.cos(angle)
    s = math.sin(angle)
    t = 1 - c

    xyt = x*y*t
    xzt = x*z*t
    yzt = y*z*t
    xs = x*s
    ys = y*s
    zs = z*s

    m = Matrix(c + x*x*t,   xyt - zs,   xzt + ys,   0,
               xyt + zs,    c + y*y*t,  yzt - xs,   0,
               xzt - ys,    yzt + xs,   c + z*z*t,  0)

    return m.multiply(u)
コード例 #2
0
def rotate(u, angle, axis=Vector(0, 0, 1)):
    '''rotate(Vector,Float,axis=Vector): rotates the first Vector
    around the given axis, at the given angle.
    If axis is omitted, the rotation is made on the xy plane.'''
    typecheck([(u, Vector), (angle, (int, long, float)), (axis, Vector)],
              "rotate")

    if angle == 0:
        return u

    l = axis.Length
    x = axis.x / l
    y = axis.y / l
    z = axis.z / l
    c = math.cos(angle)
    s = math.sin(angle)
    t = 1 - c

    xyt = x * y * t
    xzt = x * z * t
    yzt = y * z * t
    xs = x * s
    ys = y * s
    zs = z * s

    m = Matrix(c + x * x * t, xyt - zs, xzt + ys, 0, xyt + zs, c + y * y * t,
               yzt - xs, 0, xzt - ys, yzt + xs, c + z * z * t, 0)

    return m.multiply(u)
コード例 #3
0
def __valueAtEllipse__(ra, rb, center, axis, u):
	x = VEC(cos(u) * ra, sin(u) * rb, 0)
	theta = axis.getAngle(DIR_Z)
	if (abs(theta) < 1e-06):
		n = DIR_X
	elif (abs(theta - pi) < 1e-06):
		n = -DIR_X
	else:
		n = DIR_Z.cross(axis)
	if (n.Length == 0):
		return x
	n = n.normalize()
	a = cos(theta/2)
	b = n.x*sin(theta/2)
	c = n.y*sin(theta/2)
	d = n.z*sin(theta/2)
	aa, bb, cc, dd = a*a, b*b, c*c, d*d
	bc, ad, ac, ab, bd, cd = b*c, a*d, a*c, a*b, b*d, c*d
	m = MAT(aa+bb-cc-dd, 2*(bc+ad), 2*(bd-ac), 0,
			2*(bc-ad), aa+cc-bb-dd, 2*(cd+ab), 0,
			2*(bd+ac), 2*(cd-ab), aa+dd-bb-cc, 0,
			0, 0, 0, 1)
	return center + m.multiply(x)
コード例 #4
0
def rotate(u, angle, axis=Vector(0, 0, 1)):
    """Rotate the vector by the specified angle, around the given axis.

    If the axis is omitted, the rotation is made around the Z axis
    (on the XY plane).

    It uses a 3x3 rotation matrix.
    ::
        u_rot = R u

                (c + x*x*t    xyt - zs     xzt + ys )
        u_rot = (xyt + zs     c + y*y*t    yzt - xs ) * u
                (xzt - ys     yzt + xs     c + z*z*t)

    Where `x`, `y`, `z` indicate unit components of the axis;
    `c` denotes a cosine of the angle; `t` indicates a complement
    of that cosine; `xs`, `ys`, `zs` indicate products of the unit
    components and the sine of the angle; and `xyt`, `xzt`, `yzt`
    indicate products of two unit components and the complement
    of the cosine.

    Parameters
    ----------
    u : Base::Vector3
        The vector.
    angle : float
        The angle of rotation given in radians.
    axis : Base::Vector3, optional
        The vector specifying the axis of rotation.
        It defaults to `(0, 0, 1)`, the +Z axis.

    Returns
    -------
    Base::Vector3
        The new rotated vector.
        If the `angle` is zero, return the original vector `u`.
    """
    typecheck([(u, Vector), (angle, (int, float)), (axis, Vector)], "rotate")

    if angle == 0:
        return u

    # Unit components, so that x**2 + y**2 + z**2 = 1
    L = axis.Length
    x = axis.x / L
    y = axis.y / L
    z = axis.z / L

    c = math.cos(angle)
    s = math.sin(angle)
    t = 1 - c

    # Various products
    xyt = x * y * t
    xzt = x * z * t
    yzt = y * z * t
    xs = x * s
    ys = y * s
    zs = z * s

    m = Matrix(c + x * x * t, xyt - zs, xzt + ys, 0, xyt + zs, c + y * y * t,
               yzt - xs, 0, xzt - ys, yzt + xs, c + z * z * t, 0)

    return m.multiply(u)