def test_angles_between_vectors_1dim():
    random_state = np.random.RandomState(229)
    A = random_state.randn(100, 3)
    B = random_state.randn(100, 3)
    angles = pbr.angles_between_vectors(A, B)
    angles2 = [pr.angle_between_vectors(a, b) for a, b in zip(A, B)]
    assert_array_almost_equal(angles, angles2)
def test_angles_between_vectors_0dims():
    random_state = np.random.RandomState(228)
    A = random_state.randn(3)
    B = random_state.randn(3)
    angles = pbr.angles_between_vectors(A, B)
    angles2 = pr.angle_between_vectors(A, B)
    assert_array_almost_equal(angles, angles2)
def test_angles_between_vectors_3dims():
    random_state = np.random.RandomState(230)
    A = random_state.randn(2, 4, 3, 4)
    B = random_state.randn(2, 4, 3, 4)
    angles = pbr.angles_between_vectors(A, B).ravel()
    angles2 = [pr.angle_between_vectors(a, b)
               for a, b in zip(A.reshape(-1, 4), B.reshape(-1, 4))]
    assert_array_almost_equal(angles, angles2)
예제 #4
0
def transformCM(ptlist, ftlist, ftlist_trimmed, params):

    nT = params['nT']
    nF = params['nF']
    AC = params['AC']

    ftlist2 = copy.deepcopy(ftlist)
    ptlist2 = copy.deepcopy(ptlist)

    world_origin = np.array([0, 0, 0])
    world_vec = np.identity(3)

    for it in range(nT):
        obj_origin = AC.COM(ftlist_trimmed[it]).transpose()
        w, obj_vec = AC.GetBasisVectors(ftlist_trimmed[it])
        T = (world_origin - obj_origin)

        # Get vector perp to z-axis and nematic director
        vperp = pyro.perpendicular_to_vectors(world_vec[:, -1], obj_vec[:, -1])
        if np.linalg.norm(vperp) > 0.00001:
            # rotation needed
            vperp = vperp / np.linalg.norm(vperp)
            theta = pyro.angle_between_vectors(world_vec[:, -1], obj_vec[:,
                                                                         -1])
            axis_angle = np.concatenate((vperp, -np.array([theta])))
            q = pyro.quaternion_from_axis_angle(axis_angle)

            # transfrom filaments
            for fil in ftlist2[it]:
                fil.pos0 = pyro.q_prod_vector(q, fil.pos0 + T.transpose())
                fil.pos1 = pyro.q_prod_vector(q, fil.pos1 + T.transpose())
            # transfrom proteins
#        for prot in ptlist2[it]:
#            prot.pos0 = pyro.q_prod_vector( q, prot.pos0 +T.transpose() )
#            prot.pos1 = pyro.q_prod_vector( q, prot.pos0 +T.transpose() )

        else:
            # transfrom filaments
            for fil in ftlist2[it]:
                fil.pos0 = fil.pos0 + T.transpose()
                fil.pos1 = fil.pos1 + T.transpose()
            # transfrom proteins
#        for prot in ptlist2[it]:
#            prot.pos0 = prot.pos0 +T.transpose()
#            prot.pos1 = prot.pos0 +T.transpose()

    return ftlist2, ptlist2
예제 #5
0
def transform_coords(coords, origin_new, axis_new):
    # transform coordinates to a new frame of reference with the orivided
    # new origin and the the new basis matrix

    # Translate origin
    coords = coords - origin_new

    # Get vector perp to z-axis and the new z-axis
    vperp = pyro.perpendicular_to_vectors(np.array([0, 0, 1]), axis_new)

    # Rotate if needed
    if np.linalg.norm(vperp) > 0.00001:
        vperp = vperp / np.linalg.norm(vperp)
        theta = pyro.angle_between_vectors(np.array([0, 0, 1]), axis_new)
        axis_angle = np.concatenate((vperp, -np.array([theta])))
        q = pyro.quaternion_from_axis_angle(axis_angle)

        # transfrom coordinates
        coords = q_prod_coords(coords, q)

    return coords
def update_lines(step, start, end, n_frames, rot, profile):
    global velocity
    global last_a

    if step == 0:
        velocity = []
        last_a = start

    if step <= n_frames / 2:
        t = step / float(n_frames / 2 - 1)
        a = pr.axis_angle_slerp(start, end, t)
    else:
        t = (step - n_frames / 2) / float(n_frames / 2 - 1)
        a = interpolate_linear(end, start, t)

    R = pr.matrix_from_axis_angle(a)

    # Draw new frame
    rot[0].set_data(np.array([0, R[0, 0]]), [0, R[1, 0]])
    rot[0].set_3d_properties([0, R[2, 0]])

    rot[1].set_data(np.array([0, R[0, 1]]), [0, R[1, 1]])
    rot[1].set_3d_properties([0, R[2, 1]])

    rot[2].set_data(np.array([0, R[0, 2]]), [0, R[1, 2]])
    rot[2].set_3d_properties([0, R[2, 2]])

    # Update vector in frame
    test = R.dot(np.ones(3) / np.sqrt(3.0))
    rot[3].set_data(np.array([test[0] / 2.0, test[0]]),
                    [test[1] / 2.0, test[1]])
    rot[3].set_3d_properties([test[2] / 2.0, test[2]])

    velocity.append(
        pr.angle_between_vectors(a[:3], last_a[:3]) + a[3] - last_a[3])
    last_a = a
    profile.set_data(np.linspace(0, 1, n_frames)[:len(velocity)], velocity)

    return rot
예제 #7
0
def plot_screw(figure,
               q=np.zeros(3),
               s_axis=np.array([1.0, 0.0, 0.0]),
               h=1.0,
               theta=1.0,
               A2B=None,
               s=1.0):
    """Plot transformation about and along screw axis.

    Parameters
    ----------
    figure : Figure
        Interface to Open3D's visualizer

    q : array-like, shape (3,), optional (default: [0, 0, 0])
        Vector to a point on the screw axis

    s_axis : array-like, shape (3,), optional (default: [1, 0, 0])
        Direction vector of the screw axis

    h : float, optional (default: 1)
        Pitch of the screw. The pitch is the ratio of translation and rotation
        of the screw axis. Infinite pitch indicates pure translation.

    theta : float, optional (default: 1)
        Rotation angle. h * theta is the translation.

    A2B : array-like, shape (4, 4), optional (default: I)
        Origin of the screw

    s : float, optional (default: 1)
        Scaling of the axis and angle that will be drawn
    """
    from pytransform3d.rotations import (vector_projection,
                                         angle_between_vectors,
                                         perpendicular_to_vectors,
                                         slerp_weights)
    from pytransform3d.transformations import (check_screw_parameters,
                                               transform, translate_transform,
                                               vector_to_point,
                                               vector_to_direction,
                                               vectors_to_points)

    if A2B is None:
        A2B = np.eye(4)

    q, s_axis, h = check_screw_parameters(q, s_axis, h)

    origin_projected_on_screw_axis = q + vector_projection(-q, s_axis)

    pure_translation = np.isinf(h)

    if not pure_translation:
        screw_axis_to_old_frame = -origin_projected_on_screw_axis
        screw_axis_to_rotated_frame = perpendicular_to_vectors(
            s_axis, screw_axis_to_old_frame)
        screw_axis_to_translated_frame = h * s_axis

        arc = np.empty((100, 3))
        angle = angle_between_vectors(screw_axis_to_old_frame,
                                      screw_axis_to_rotated_frame)
        for i, t in enumerate(
                zip(np.linspace(0, 2 * theta / np.pi, len(arc)),
                    np.linspace(0.0, 1.0, len(arc)))):
            t1, t2 = t
            w1, w2 = slerp_weights(angle, t1)
            arc[i] = (origin_projected_on_screw_axis +
                      w1 * screw_axis_to_old_frame +
                      w2 * screw_axis_to_rotated_frame +
                      screw_axis_to_translated_frame * t2 * theta)

    q = transform(A2B, vector_to_point(q))[:3]
    s_axis = transform(A2B, vector_to_direction(s_axis))[:3]
    if not pure_translation:
        arc = transform(A2B, vectors_to_points(arc))[:, :3]
        origin_projected_on_screw_axis = transform(
            A2B, vector_to_point(origin_projected_on_screw_axis))[:3]

    # Screw axis
    Q = translate_transform(np.eye(4), q)
    fig.plot_sphere(radius=s * 0.02, A2B=Q, c=[1, 0, 0])
    if pure_translation:
        s_axis *= theta
        Q_plus_S_axis = translate_transform(np.eye(4), q + s_axis)
        fig.plot_sphere(radius=s * 0.02, A2B=Q_plus_S_axis, c=[0, 1, 0])
    P = np.array(
        [[q[0] - s * s_axis[0], q[1] - s * s_axis[1], q[2] - s * s_axis[2]],
         [
             q[0] + (1 + s) * s_axis[0], q[1] + (1 + s) * s_axis[1],
             q[2] + (1 + s) * s_axis[2]
         ]])
    figure.plot(P=P, c=[0, 0, 0])

    if not pure_translation:
        # Transformation
        figure.plot(arc, c=[0, 0, 0])

        for i, c in zip([0, -1], [[1, 0, 0], [0, 1, 0]]):
            arc_bound = np.vstack((origin_projected_on_screw_axis, arc[i]))
            figure.plot(arc_bound, c=c)