Exemplo n.º 1
0
def test_captured_output():
    def foo():
        print('hello world!')

    with ft.captured_output() as (out, _):
        foo()

    npt.assert_equal(out.getvalue().strip(), "hello world!")
Exemplo n.º 2
0
def test_captured_output():
    def foo():
        print('hello world!')

    with ft.captured_output() as (out, err):
        foo()
        # logging.error("Wrong error")

    npt.assert_equal(out.getvalue().strip(), "hello world!")
Exemplo n.º 3
0
def test_scene():

    scene = window.Scene()

    npt.assert_equal(scene.size(), (0, 0))

    # background color for scene (1, 0.5, 0)
    # 0.001 added here to remove numerical errors when moving from float
    # to int values
    bg_float = (1, 0.501, 0)

    # that will come in the image in the 0-255 uint scale
    bg_color = tuple((np.round(255 * np.array(bg_float))).astype('uint8'))

    scene.background(bg_float)
    # window.show(scene)
    arr = window.snapshot(scene)

    report = window.analyze_snapshot(arr,
                                     bg_color=bg_color,
                                     colors=[bg_color, (0, 127, 0)])
    npt.assert_equal(report.objects, 0)
    npt.assert_equal(report.colors_found, [True, False])

    axes = actor.axes()
    scene.add(axes)
    # window.show(scene)

    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 1)

    scene.rm(axes)
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 0)

    scene.add(axes)
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 1)

    scene.rm_all()
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 0)

    ren2 = window.Scene(bg_float)
    ren2.background((0, 0, 0.))

    report = window.analyze_scene(ren2)
    npt.assert_equal(report.bg_color, (0, 0, 0))

    ren2.add(axes)

    report = window.analyze_scene(ren2)
    npt.assert_equal(report.actors, 3)

    ren2.rm(axes)
    report = window.analyze_scene(ren2)
    npt.assert_equal(report.actors, 0)

    with captured_output() as (out, err):
        scene.camera_info()
    npt.assert_equal(
        out.getvalue().strip(), '# Active Camera\n   '
        'Position (0.00, 0.00, 1.00)\n   '
        'Focal Point (0.00, 0.00, 0.00)\n   '
        'View Up (0.00, 1.00, 0.00)')
    npt.assert_equal(err.getvalue().strip(), '')
Exemplo n.º 4
0
def test_record():
    xyzr = np.array([[0, 0, 0, 10], [100, 0, 0, 25], [200, 0, 0, 50]])
    colors = np.array([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1., 1]])
    sphere_actor = actor.sphere(centers=xyzr[:, :3],
                                colors=colors[:],
                                radii=xyzr[:, 3])
    scene = window.Scene()
    scene.add(sphere_actor)

    def test_content(filename='fury.png', colors_found=(True, True)):
        npt.assert_equal(os.path.isfile(filename), True)
        arr = io.load_image(filename)
        report = window.analyze_snapshot(arr,
                                         colors=[(0, 255, 0), (255, 0, 0)])
        npt.assert_equal(report.objects, 3)
        npt.assert_equal(report.colors_found, colors_found)
        return arr

    # Basic test
    with InTemporaryDirectory():
        window.record(scene)
        test_content()

    # test out_path and path_numbering, n_frame
    with InTemporaryDirectory():
        filename = "tmp_snapshot.png"
        window.record(scene, out_path=filename)
        test_content(filename)
        window.record(scene, out_path=filename, path_numbering=True)
        test_content(filename + "000000.png")
        window.record(scene,
                      out_path=filename,
                      path_numbering=True,
                      n_frames=3)
        test_content(filename + "000000.png")
        test_content(filename + "000001.png")
        test_content(filename + "000002.png")
        npt.assert_equal(os.path.isfile(filename + "000003.png"), False)

    # test verbose
    with captured_output() as (out, _):
        window.record(scene, verbose=True)

    npt.assert_equal(
        out.getvalue().strip(), "Camera Position (315.14, 0.00, 536.43)\n"
        "Camera Focal Point (119.89, 0.00, 0.00)\n"
        "Camera View Up (0.00, 1.00, 0.00)")
    # test camera option
    with InTemporaryDirectory():
        window.record(scene,
                      cam_pos=(310, 0, 530),
                      cam_focal=(120, 0, 0),
                      cam_view=(0, 0, 1))
        test_content()

    # test size and clipping
    # Skip it on Mac mainly due to offscreen case on Travis. It works well
    # with a display. Need to check if screen_clip works. Need to check if
    # ReadFrontBufferOff(), ShouldRerenderOn() could improved this OSX case.
    if not skip_osx:
        with InTemporaryDirectory():
            window.record(scene,
                          out_path='fury_1.png',
                          size=(1000, 1000),
                          magnification=5)
            npt.assert_equal(os.path.isfile('fury_1.png'), True)
            arr = io.load_image('fury_1.png')

            npt.assert_equal(arr.shape, (5000, 5000, 3))

            window.record(scene,
                          out_path='fury_2.png',
                          size=(5000, 5000),
                          screen_clip=True)
            npt.assert_equal(os.path.isfile('fury_2.png'), True)
            arr = io.load_image('fury_2.png')

            assert_less_equal(arr.shape[0], 5000)
            assert_less_equal(arr.shape[1], 5000)
Exemplo n.º 5
0
def test_scene():
    scene = window.Scene()
    # Scene size test
    npt.assert_equal(scene.size(), (0, 0))
    # Color background test
    # Background color for scene (1, 0.5, 0). 0.001 added here to remove
    # numerical errors when moving from float to int values
    bg_float = (1, 0.501, 0)
    # That will come in the image in the 0-255 uint scale
    bg_color = tuple((np.round(255 * np.array(bg_float))).astype('uint8'))
    scene.background(bg_float)
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color=bg_color,
                                     colors=[bg_color, (0, 127, 0)])
    npt.assert_equal(report.objects, 0)
    npt.assert_equal(report.colors_found, [True, False])
    # Add actor to scene to test the remove actor function by analyzing a
    # snapshot
    axes = actor.axes()
    scene.add(axes)
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 1)
    # Test remove actor function by analyzing a snapshot
    scene.rm(axes)
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 0)
    # Add actor to scene to test the remove all actors function by analyzing a
    # snapshot
    scene.add(axes)
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 1)
    # Test remove all actors function by analyzing a snapshot
    scene.rm_all()
    arr = window.snapshot(scene)
    report = window.analyze_snapshot(arr, bg_color)
    npt.assert_equal(report.objects, 0)
    # Test change background color from scene by analyzing the scene
    ren2 = window.Scene(bg_float)
    ren2.background((0, 0, 0.))
    report = window.analyze_scene(ren2)
    npt.assert_equal(report.bg_color, (0, 0, 0))
    # Add actor to scene to test the remove actor function by analyzing the
    # scene
    ren2.add(axes)
    report = window.analyze_scene(ren2)
    npt.assert_equal(report.actors, 1)
    # Test remove actor function by analyzing the scene
    ren2.rm(axes)
    report = window.analyze_scene(ren2)
    npt.assert_equal(report.actors, 0)
    # Test camera information retrieving
    with captured_output() as (out, err):
        scene.camera_info()
    npt.assert_equal(out.getvalue().strip(),
                     '# Active Camera\n   '
                     'Position (0.00, 0.00, 1.00)\n   '
                     'Focal Point (0.00, 0.00, 0.00)\n   '
                     'View Up (0.00, 1.00, 0.00)')
    npt.assert_equal(err.getvalue().strip(), '')
    # Test skybox
    scene = window.Scene()
    npt.assert_equal(scene.GetUseImageBasedLighting(), False)
    npt.assert_equal(scene.GetAutomaticLightCreation(), 1)
    npt.assert_equal(scene.GetSphericalHarmonics(), None)
    npt.assert_equal(scene.GetEnvironmentTexture(), None)
    test_tex = Texture()
    scene = window.Scene(skybox=test_tex)
    npt.assert_equal(scene.GetUseImageBasedLighting(), True)
    npt.assert_equal(scene.GetAutomaticLightCreation(), 0)
    npt.assert_equal(scene.GetSphericalHarmonics(), None)
    npt.assert_equal(scene.GetEnvironmentTexture(), test_tex)
    # Test automatically shown skybox
    test_tex = Texture()
    test_tex.CubeMapOn()
    checker_arr = np.array([[1, 0], [0, 1]], dtype=np.uint8) * 255
    for i in range(6):
        vtk_img = ImageData()
        vtk_img.SetDimensions(2, 2, 1)
        img_arr = np.zeros((2, 2, 3), dtype=np.uint8)
        img_arr[:, :, i // 2] = checker_arr
        vtk_arr = numpy_support.numpy_to_vtk(img_arr.reshape((-1, 3),
                                                             order='F'))
        vtk_arr.SetName('Image')
        img_point_data = vtk_img.GetPointData()
        img_point_data.AddArray(vtk_arr)
        img_point_data.SetActiveScalars('Image')
        test_tex.SetInputDataObject(i, vtk_img)
    scene = window.Scene(skybox=test_tex)
    report = window.analyze_scene(scene)
    npt.assert_equal(report.actors, 1)
    ss = window.snapshot(scene)
    npt.assert_array_equal(ss[75, 75, :], [0, 0, 255])
    npt.assert_array_equal(ss[75, 225, :], [0, 0, 0])
    scene.yaw(90)
    ss = window.snapshot(scene)
    npt.assert_array_equal(ss[75, 75, :], [255, 0, 0])
    npt.assert_array_equal(ss[75, 225, :], [0, 0, 0])
    scene.pitch(90)
    ss = window.snapshot(scene)
    npt.assert_array_equal(ss[75, 75, :], [0, 0, 0])
    npt.assert_array_equal(ss[75, 225, :], [0, 255, 0])