Beispiel #1
0
def test_euler_matrix():
    rotation = euler_matrix(1, 2, 3, 'syxz')
    npt.assert_equal(np.allclose(np.sum(rotation[0]), -1.34786452), True)

    rotation = euler_matrix(1, 2, 3, (0, 1, 0, 1))
    npt.assert_equal(np.allclose(np.sum(rotation[0]), -0.383436184), True)

    ai, aj, ak = (4.0 * np.pi) * (np.random.random(3) - 0.5)
    for axes in _AXES2TUPLE.keys():
        _ = euler_matrix(ai, aj, ak, axes)
    for axes in _TUPLE2AXES.keys():
        _ = euler_matrix(ai, aj, ak, axes)
Beispiel #2
0
def repeat_primitive(vertices,
                     faces,
                     centers,
                     directions=(1, 0, 0),
                     colors=(255, 0, 0),
                     scale=1,
                     have_tiled_verts=False):
    """Repeat Vertices and triangles of a specific primitive shape.

    It could be seen as a glyph.

    Parameters
    ----------
    vertices: ndarray
        vertices coords to duplicate at the centers positions
    triangles: ndarray
        triangles that composed our shape to duplicate
    centers : ndarray, shape (N, 3)
        Superquadrics positions
    directions : ndarray, shape (N, 3) or tuple (3,), optional
        The orientation vector of the cone.
    colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,)
        RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]
    scale : ndarray, shape (N) or (N,3) or float or int, optional
        The height of the cone.
    have_tiled_verts : bool
        option to control if we need to duplicate vertices of a shape or not

    Returns
    -------
    big_vertices: ndarray
        Expanded vertices at the centers positions
    big_triangles: ndarray
        Expanded triangles that composed our shape to duplicate
    big_colors : ndarray
        Expanded colors applied to all vertices/faces

    """
    # duplicated vertices if needed
    if not have_tiled_verts:
        vertices = np.tile(vertices, (centers.shape[0], 1))

    # Get unit shape
    unit_verts_size = vertices.shape[0] // centers.shape[0]
    unit_triangles_size = faces.shape[0]

    big_centers = np.repeat(centers, unit_verts_size, axis=0)
    # apply centers position
    big_vertices = vertices + big_centers
    # scale them
    if isinstance(scale, (list, tuple, np.ndarray)):
        scale = np.repeat(scale, unit_verts_size, axis=0)
        scale = scale.reshape((big_vertices.shape[0], 1))
    big_vertices *= scale

    # update triangles
    big_triangles = np.array(np.tile(faces, (centers.shape[0], 1)),
                             dtype=np.int32)
    big_triangles += np.repeat(np.arange(0,
                                         centers.shape[0] * unit_verts_size,
                                         step=unit_verts_size),
                               unit_triangles_size,
                               axis=0).reshape((big_triangles.shape[0], 1))

    def normalize_input(arr, arr_name=''):
        if isinstance(arr, (tuple, list, np.ndarray)) and len(arr) == 3 and \
          not all(isinstance(i, (list, tuple, np.ndarray)) for i in arr):
            return np.array([arr] * centers.shape[0])
        elif isinstance(arr, np.ndarray) and len(arr) == 1:
            return np.repeat(arr, centers.shape[0], axis=0)
        elif len(arr) != len(centers):
            msg = "{} size should be 1 or ".format(arr_name)
            msg += "equal to the numbers of centers"
            raise IOError(msg)
        else:
            return np.array(arr)

    # update colors
    colors = normalize_input(colors, 'colors')
    big_colors = np.repeat(colors, unit_verts_size, axis=0)

    # update orientations
    directions = normalize_input(directions, 'directions')
    big_vertices -= big_centers
    for pts, dirs in enumerate(directions):
        ai, aj, ak = transform.Rotation.from_rotvec(np.pi/2 * dirs). \
            as_euler('zyx')
        rotation_matrix = euler_matrix(ai, aj, ak)
        big_vertices[pts * unit_verts_size: (pts+1) * unit_verts_size] = \
            np.dot(rotation_matrix[:3, :3],
                   big_vertices[pts * unit_verts_size:
                                (pts+1) * unit_verts_size].T).T
    big_vertices += big_centers

    return big_vertices, big_triangles, big_colors