def test_save_and_load_polydata(): l_ext = ["vtk", "fib", "ply", "xml"] fname = "temp-io" for ext in l_ext: with InTemporaryDirectory() as odir: data = np.random.randint(0, 255, size=(50, 3)) pd = vtk.vtkPolyData() pd.SetPoints(numpy_to_vtk_points(data)) fname_path = pjoin(odir, "{0}.{1}".format(fname, ext)) save_polydata(pd, fname_path) npt.assert_equal(os.path.isfile(fname_path), True) assert_greater(os.stat(fname_path).st_size, 0) out_pd = load_polydata(fname_path) out_data = numpy_support.vtk_to_numpy(out_pd.GetPoints().GetData()) npt.assert_array_equal(data, out_data) npt.assert_raises(IOError, save_polydata, vtk.vtkPolyData(), "test.vti") npt.assert_raises(IOError, save_polydata, vtk.vtkPolyData(), "test.obj") npt.assert_raises(IOError, load_polydata, "test.vti")
def test_container(): container = actor.Container() axes = actor.axes() container.add(axes) npt.assert_equal(len(container), 1) npt.assert_equal(container.GetBounds(), axes.GetBounds()) npt.assert_equal(container.GetCenter(), axes.GetCenter()) npt.assert_equal(container.GetLength(), axes.GetLength()) container.clear() npt.assert_equal(len(container), 0) container.add(axes) container_shallow_copy = shallow_copy(container) container_shallow_copy.add(actor.axes()) assert_greater(len(container_shallow_copy), len(container)) npt.assert_equal(container_shallow_copy.GetPosition(), container.GetPosition()) npt.assert_equal(container_shallow_copy.GetVisibility(), container.GetVisibility()) # Check is the shallow_copy do not modify original container container_shallow_copy.SetVisibility(False) npt.assert_equal(container.GetVisibility(), True) container_shallow_copy.SetPosition((1, 1, 1)) npt.assert_equal(container.GetPosition(), (0, 0, 0))
def test_save_load_image(): l_ext = ["png", "jpeg", "jpg", "bmp", "tiff"] fname = "temp-io" for ext in l_ext: with InTemporaryDirectory() as odir: data = np.random.randint(0, 255, size=(50, 3), dtype=np.uint8) fname_path = pjoin(odir, "{0}.{1}".format(fname, ext)) save_image(data, fname_path, compression_quality=100) npt.assert_equal(os.path.isfile(fname_path), True) assert_greater(os.stat(fname_path).st_size, 0) out_image = load_image(fname_path) if ext not in ["jpeg", "jpg", "tiff"]: npt.assert_array_equal(data[..., 0], out_image[..., 0]) else: npt.assert_array_almost_equal(data[..., 0], out_image[..., 0], decimal=0) npt.assert_raises(IOError, load_image, "test.vtk") npt.assert_raises(IOError, load_image, "test.vtk", use_pillow=False) npt.assert_raises(IOError, save_image, np.random.randint(0, 255, size=(50, 3)), "test.vtk") npt.assert_raises(IOError, save_image, np.random.randint(0, 255, size=(50, 3)), "test.vtk", use_pillow=False) npt.assert_raises(IOError, save_image, np.random.randint(0, 255, size=(50, 3, 1, 1)), "test.png") compression_type = [None, "lzw"] for ct in compression_type: with InTemporaryDirectory() as odir: try: data = np.random.randint(0, 255, size=(50, 3), dtype=np.uint8) fname_path = pjoin(odir, "{0}.tif".format(fname)) save_image(data, fname_path, compression_type=ct, use_pillow=False) npt.assert_equal(os.path.isfile(fname_path), True) assert_greater(os.stat(fname_path).st_size, 0) except OSError: continue
def test_order_transparent(): scene = window.Scene() red_cube = actor.cube(centers=np.array([[0., 0., 2]]), directions=np.array([[0, 1., 0]]), colors=np.array([[1, 0., 0]])) green_cube = actor.cube(centers=np.array([[0., 0., -2]]), directions=np.array([[0, 1., 0]]), colors=np.array([[0, 1., 0]])) red_cube.GetProperty().SetOpacity(0.2) green_cube.GetProperty().SetOpacity(0.2) scene.add(red_cube) scene.add(green_cube) scene.reset_camera() scene.reset_clipping_range() # without order_transparency the green will look stronger # when looked from behind the red cube arr = window.snapshot(scene, fname=None, offscreen=True, order_transparent=False) # check if flags are set as expected (here no order transparency) npt.assert_equal(scene.GetLastRenderingUsedDepthPeeling(), 0) green_stronger = arr[150, 150, 1] arr = window.snapshot(scene, fname=None, offscreen=True, order_transparent=True) # # check if flags are set as expected (here with order transparency) npt.assert_equal(scene.GetLastRenderingUsedDepthPeeling(), 1) # when order transparency is True green should be weaker green_weaker = arr[150, 150, 1] assert_greater(green_stronger, green_weaker)
def test_save_and_load_options(): l_ext = ["ply", "vtk"] l_options = [{ 'color_array_name': 'horizon', }, { 'binary': True, }] fname = "temp-io" for ext, option in zip(l_ext, l_options): with InTemporaryDirectory() as odir: data = np.random.randint(0, 255, size=(50, 3)) pd = vtk.vtkPolyData() pd.SetPoints(numpy_to_vtk_points(data)) fname_path = pjoin(odir, "{0}.{1}".format(fname, ext)) save_polydata(pd, fname_path, **option) npt.assert_equal(os.path.isfile(fname_path), True) assert_greater(os.stat(fname_path).st_size, 0) out_pd = load_polydata(fname_path) out_data = numpy_support.vtk_to_numpy(out_pd.GetPoints().GetData()) npt.assert_array_equal(data, out_data) l_ext = ["stl", "obj"] l_options = [{}, { 'is_mni_obj': True, }] for ext, option in zip(l_ext, l_options): with InTemporaryDirectory() as odir: data = np.random.randint(0, 255, size=(50, 3)) pd = vtk.vtkPolyData() pd.SetPoints(numpy_to_vtk_points(data)) fname_path = pjoin(odir, "{0}.{1}".format(fname, ext)) save_polydata(pd, fname_path, **option) npt.assert_equal(os.path.isfile(fname_path), True) assert_greater(os.stat(fname_path).st_size, 0)
def test_text_3d(): msg = 'I \nlove\n FURY' txt_actor = actor.text_3d(msg) npt.assert_equal(txt_actor.get_message().lower(), msg.lower()) npt.assert_raises(ValueError, txt_actor.justification, 'middle') npt.assert_raises(ValueError, txt_actor.vertical_justification, 'center') scene = window.Scene() scene.add(txt_actor) txt_actor.vertical_justification('middle') txt_actor.justification('right') arr_right = window.snapshot(scene, size=(1920, 1080), offscreen=True) scene.clear() txt_actor.vertical_justification('middle') txt_actor.justification('left') scene.add(txt_actor) arr_left = window.snapshot(scene, size=(1920, 1080), offscreen=True) # X axis of right alignment should have a lower center of mass position # than left assert_greater(center_of_mass(arr_left)[0], center_of_mass(arr_right)[0]) scene.clear() txt_actor.justification('center') txt_actor.vertical_justification('top') scene.add(txt_actor) arr_top = window.snapshot(scene, size=(1920, 1080), offscreen=True) scene.clear() txt_actor.justification('center') txt_actor.vertical_justification('bottom') scene.add(txt_actor) arr_bottom = window.snapshot(scene, size=(1920, 1080), offscreen=True) assert_greater_equal( center_of_mass(arr_bottom)[0], center_of_mass(arr_top)[0]) scene.clear() txt_actor.font_style(bold=True, italic=True, shadow=True) scene.add(txt_actor) arr = window.snapshot(scene, size=(1920, 1080), offscreen=True) assert_greater_equal(arr.mean(), arr_bottom.mean())
def test_frame_rate_and_anti_aliasing(): """Testing frame rate with/out anti-aliasing""" length_ = 200 multi_samples = 32 max_peels = 8 st_x = np.arange(length_) st_y = np.sin(np.arange(length_)) st_z = np.zeros(st_x.shape) st = np.zeros((length_, 3)) st[:, 0] = st_x st[:, 1] = st_y st[:, 2] = st_z all_st = [] all_st.append(st) for i in range(1000): all_st.append(st + i * np.array([0., .5, 0])) # st_actor = actor.line(all_st, linewidth=1) # TODO: textblock disappears when lod=True st_actor = actor.streamtube(all_st, linewidth=0.1, lod=False) scene = window.Scene() scene.background((1, 1., 1)) # quick game style antialiasing scene.fxaa_on() scene.fxaa_off() # the good staff is later with multi-sampling tb = ui.TextBlock2D(font_size=40, color=(1, 0.5, 0)) panel = ui.Panel2D(position=(400, 400), size=(400, 400)) panel.add_element(tb, (0.2, 0.5)) counter = itertools.count() showm = window.ShowManager(scene, size=(1980, 1080), reset_camera=False, order_transparent=True, multi_samples=multi_samples, max_peels=max_peels, occlusion_ratio=0.0) showm.initialize() scene.add(panel) scene.add(st_actor) scene.reset_camera_tight() scene.zoom(5) class FrameRateHolder(object): fpss = [] frh = FrameRateHolder() def timer_callback(_obj, _event): cnt = next(counter) if cnt % 1 == 0: fps = np.round(scene.frame_rate, 0) frh.fpss.append(fps) msg = "FPS " + str(fps) + ' ' + str(cnt) tb.message = msg showm.render() if cnt > 10: showm.exit() # Run every 200 milliseconds showm.add_timer_callback(True, 200, timer_callback) showm.start() arr = window.snapshot(scene, size=(1980, 1080), offscreen=True, order_transparent=True, multi_samples=multi_samples, max_peels=max_peels, occlusion_ratio=0.0) assert_greater(np.sum(arr), 0) # TODO: check why in osx we have issues in Azure if not skip_osx: assert_greater(np.median(frh.fpss), 0) frh.fpss = [] counter = itertools.count() multi_samples = 0 showm = window.ShowManager(scene, size=(1980, 1080), reset_camera=False, order_transparent=True, multi_samples=multi_samples, max_peels=max_peels, occlusion_ratio=0.0) showm.initialize() showm.add_timer_callback(True, 200, timer_callback) showm.start() arr2 = window.snapshot(scene, size=(1980, 1080), offscreen=True, order_transparent=True, multi_samples=multi_samples, max_peels=max_peels, occlusion_ratio=0.0) assert_greater(np.sum(arr2), 0) if not skip_osx: assert_greater(np.median(frh.fpss), 0)
def test_picking_manager(): xyz = 10 * np.random.rand(100, 3) colors = np.random.rand(100, 4) radii = np.random.rand(100) + 0.5 scene = window.Scene() sphere_actor = actor.sphere(centers=xyz, colors=colors, radii=radii) scene.add(sphere_actor) showm = window.ShowManager(scene, size=(900, 768), reset_camera=False, order_transparent=True) showm.initialize() tb = ui.TextBlock2D(bold=True) # use itertools to avoid global variables counter = itertools.count() pickm = pick.PickingManager() record_indices = { 'vertex_indices': [], 'face_indices': [], 'xyz': [], 'actor': [] } def timer_callback(_obj, _event): cnt = next(counter) tb.message = "Let's count up to 15 and exit :" + str(cnt) showm.scene.azimuth(0.05 * cnt) # sphere_actor.GetProperty().SetOpacity(cnt/100.) if cnt % 10 == 0: # pick at position info = pickm.pick((900 / 2, 768 / 2), scene) record_indices['vertex_indices'].append(info['vertex']) record_indices['face_indices'].append(info['face']) record_indices['xyz'].append(info['xyz']) record_indices['actor'].append(info['actor']) showm.render() if cnt == 15: showm.exit() scene.add(tb) # Run every 200 milliseconds showm.add_timer_callback(True, 200, timer_callback) showm.start() assert_greater(np.sum(np.array(record_indices['vertex_indices'])), 1) assert_greater(np.sum(np.array(record_indices['face_indices'])), 1) for ac in record_indices['actor']: if ac is not None: npt.assert_equal(ac is sphere_actor, True) assert_greater( np.sum(np.abs(np.diff(np.array(record_indices['xyz']), axis=0))), 0)
def test_save_load_image(): l_ext = ["png", "jpeg", "jpg", "bmp", "tiff"] fury_logo_link = 'https://raw.githubusercontent.com/fury-gl/'\ 'fury-communication-assets/main/fury-logo.png' invalid_link = 'https://picsum.photos/200' fname = "temp-io" for ext in l_ext: with InTemporaryDirectory() as odir: data = np.random.randint(0, 255, size=(50, 3), dtype=np.uint8) url_image = load_image(fury_logo_link) url_fname_path = pjoin(odir, f'fury_logo.{ext}') fname_path = pjoin(odir, "{0}.{1}".format(fname, ext)) save_image(data, fname_path, compression_quality=100) save_image(url_image, url_fname_path, compression_quality=100) npt.assert_equal(os.path.isfile(fname_path), True) npt.assert_equal(os.path.isfile(url_fname_path), True) assert_greater(os.stat(fname_path).st_size, 0) assert_greater(os.stat(url_fname_path).st_size, 0) out_image = load_image(fname_path) if ext not in ["jpeg", "jpg", "tiff"]: npt.assert_array_equal(data[..., 0], out_image[..., 0]) else: npt.assert_array_almost_equal(data[..., 0], out_image[..., 0], decimal=0) npt.assert_raises(IOError, load_image, invalid_link) npt.assert_raises(IOError, load_image, "test.vtk") npt.assert_raises(IOError, load_image, "test.vtk", use_pillow=False) npt.assert_raises(IOError, save_image, np.random.randint(0, 255, size=(50, 3)), "test.vtk") npt.assert_raises(IOError, save_image, np.random.randint(0, 255, size=(50, 3)), "test.vtk", use_pillow=False) npt.assert_raises(IOError, save_image, np.random.randint(0, 255, size=(50, 3, 1, 1)), "test.png") compression_type = [None, "lzw"] for ct in compression_type: with InTemporaryDirectory() as odir: try: data = np.random.randint(0, 255, size=(50, 3), dtype=np.uint8) fname_path = pjoin(odir, "{0}.tif".format(fname)) save_image(data, fname_path, compression_type=ct, use_pillow=False) npt.assert_equal(os.path.isfile(fname_path), True) assert_greater(os.stat(fname_path).st_size, 0) except OSError: continue