Пример #1
0
def test_polydata_polygon(interactive=False):
    # Create a cube
    my_triangles = np.array([[0, 6, 4],
                             [0, 2, 6],
                             [0, 3, 2],
                             [0, 1, 3],
                             [2, 7, 6],
                             [2, 3, 7],
                             [4, 6, 7],
                             [4, 7, 5],
                             [0, 4, 5],
                             [0, 5, 1],
                             [1, 5, 7],
                             [1, 7, 3]], dtype='i8')
    my_vertices = np.array([[0.0, 0.0, 0.0],
                            [0.0, 0.0, 1.0],
                            [0.0, 1.0, 0.0],
                            [0.0, 1.0, 1.0],
                            [1.0, 0.0, 0.0],
                            [1.0, 0.0, 1.0],
                            [1.0, 1.0, 0.0],
                            [1.0, 1.0, 1.0]])
    colors = my_vertices * 255
    my_polydata = vtk.vtkPolyData()

    utils.set_polydata_vertices(my_polydata, my_vertices)
    utils.set_polydata_triangles(my_polydata, my_triangles)

    npt.assert_equal(len(my_vertices), my_polydata.GetNumberOfPoints())
    npt.assert_equal(len(my_triangles), my_polydata.GetNumberOfCells())
    npt.assert_equal(utils.get_polydata_normals(my_polydata), None)

    res_triangles = utils.get_polydata_triangles(my_polydata)
    res_vertices = utils.get_polydata_vertices(my_polydata)

    npt.assert_array_equal(my_vertices, res_vertices)
    npt.assert_array_equal(my_triangles, res_triangles)

    utils.set_polydata_colors(my_polydata, colors)
    npt.assert_equal(utils.get_polydata_colors(my_polydata), colors)

    utils.update_polydata_normals(my_polydata)
    normals = utils.get_polydata_normals(my_polydata)
    npt.assert_equal(len(normals), len(my_vertices))

    mapper = utils.get_polymapper_from_polydata(my_polydata)
    actor1 = utils.get_actor_from_polymapper(mapper)
    actor2 = utils.get_actor_from_polydata(my_polydata)

    scene = window.Scene()
    for actor in [actor1, actor2]:
        scene.add(actor)
        if interactive:
            window.show(scene)
        arr = window.snapshot(scene)

        report = window.analyze_snapshot(arr)
        npt.assert_equal(report.objects, 1)
Пример #2
0
def custom_glyph(centers, directions=None, colors=(1, 0, 0),
                 normals=(1, 0, 0), sq_params=None,
                 geom='square', scale=1, **kwargs):
    """Return a custom glyph actor
    """
    if geom.lower() == 'square':
        unit_verts, unit_triangles = square()
        origin_z = 0
    elif geom.lower() == 'box':
        unit_verts, unit_triangles = box()
        origin_z = 0.5
    elif geom.lower() == 'octahedron':
        unit_verts, unit_triangles = octahedron()
        origin_z = 0.5
    elif geom.lower() == 'icosahedron':
        unit_verts, unit_triangles = icosahedron()
        origin_z = 0.5
    elif geom.lower() == 'superquadric':
        unit_verts, unit_triangles = superquadric(sq_params)
        origin_z = 0.5
    else:
        unit_verts, unit_triangles = None
        origin_z = 0

    # update vertices
    big_vertices = np.tile(unit_verts, (centers.shape[0], 1))
    big_centers = np.repeat(centers, unit_verts.shape[0], axis=0)
    # center it
    big_vertices -= np.array([0.5, 0.5, origin_z])
    # apply centers position
    big_vertices += big_centers
    # scale them
    if isinstance(scale, (list, tuple, np.ndarray)):
        scale = np.repeat(scale, unit_verts.shape[0], axis=0)
        scale = scale.reshape((big_vertices.shape[0], 1))
    big_vertices *= scale

    # update triangles
    big_triangles = np.tile(unit_triangles, (centers.shape[0], 1))
    z = np.repeat(np.arange(0, centers.shape[0] *
                            unit_verts.shape[0], step=unit_verts.shape[0]),
                            unit_triangles.shape[0],
                            axis=0).reshape((big_triangles.shape[0], 1))
    big_triangles = np.add(z, big_triangles, casting="unsafe")

    # update colors
    if isinstance(colors, (tuple, list)):
        colors = np.array([colors] * centers.shape[0])
    big_colors = np.repeat(colors*255, unit_verts.shape[0], axis=0)

    # update normals
    if isinstance(normals, (tuple, list)):
        normals = np.array([normals] * centers.shape[0])
    big_normals = np.repeat(normals, unit_verts.shape[0], axis=0)

    # if isinstance(normals, (tuple, list)):
    #     directions = np.array([directions] * centers.shape[0])
    # big_dirs = np.repeat(normals, unit_verts.shape[0], axis=0)
    r, p, t = cart2sphere(0, 0, 1)
    m = euler_matrix(r, p, t, 'rxzy')
    print(big_vertices)
    big_vertices -= big_centers
    big_vertices = np.dot(m[:3, :3], big_vertices.T).T + big_centers

    # Create a Polydata
    pd = vtk.vtkPolyData()
    set_polydata_vertices(pd, big_vertices)
    set_polydata_triangles(pd, big_triangles)
    set_polydata_colors(pd, big_colors)
    set_polydata_normals(pd, big_normals)
    update_polydata_normals(pd)

    current_actor = get_actor_from_polydata(pd)
    if geom.lower() == 'square':
        current_actor.GetProperty().BackfaceCullingOff()
    return current_actor