def test_ui_rectangle_2d(): window_size = (700, 700) show_manager = window.ShowManager(size=window_size) rect = ui.Rectangle2D(size=(100, 50)) rect.position = (50, 80) npt.assert_equal(rect.position, (50, 80)) rect.color = (1, 0.5, 0) npt.assert_equal(rect.color, (1, 0.5, 0)) rect.opacity = 0.5 npt.assert_equal(rect.opacity, 0.5) # Check the rectangle is drawn at right place. show_manager.ren.add(rect) # Uncomment this to start the visualisation # show_manager.start() colors = [rect.color] arr = window.snapshot(show_manager.ren, size=window_size, offscreen=True) report = window.analyze_snapshot(arr, colors=colors) assert report.objects == 1 assert report.colors_found # Test visibility off. rect.set_visibility(False) arr = window.snapshot(show_manager.ren, size=window_size, offscreen=True) report = window.analyze_snapshot(arr) assert report.objects == 0
def test_text_block_2d_justification(): window_size = (700, 700) show_manager = window.ShowManager(size=window_size) # To help visualize the text positions. grid_size = (500, 500) bottom, middle, top = 50, 300, 550 left, center, right = 50, 300, 550 line_color = (1, 0, 0) grid_top = (center, top), (grid_size[0], 1) grid_bottom = (center, bottom), (grid_size[0], 1) grid_left = (left, middle), (1, grid_size[1]) grid_right = (right, middle), (1, grid_size[1]) grid_middle = (center, middle), (grid_size[0], 1) grid_center = (center, middle), (1, grid_size[1]) grid_specs = [ grid_top, grid_bottom, grid_left, grid_right, grid_middle, grid_center ] for spec in grid_specs: line = ui.Rectangle2D(size=spec[1], color=line_color) line.center = spec[0] show_manager.ren.add(line) font_size = 60 bg_color = (1, 1, 1) texts = [] texts += [ ui.TextBlock2D("HH", position=(left, top), font_size=font_size, color=(1, 0, 0), bg_color=bg_color, justification="left", vertical_justification="top") ] texts += [ ui.TextBlock2D("HH", position=(center, top), font_size=font_size, color=(0, 1, 0), bg_color=bg_color, justification="center", vertical_justification="top") ] texts += [ ui.TextBlock2D("HH", position=(right, top), font_size=font_size, color=(0, 0, 1), bg_color=bg_color, justification="right", vertical_justification="top") ] texts += [ ui.TextBlock2D("HH", position=(left, middle), font_size=font_size, color=(1, 1, 0), bg_color=bg_color, justification="left", vertical_justification="middle") ] texts += [ ui.TextBlock2D("HH", position=(center, middle), font_size=font_size, color=(0, 1, 1), bg_color=bg_color, justification="center", vertical_justification="middle") ] texts += [ ui.TextBlock2D("HH", position=(right, middle), font_size=font_size, color=(1, 0, 1), bg_color=bg_color, justification="right", vertical_justification="middle") ] texts += [ ui.TextBlock2D("HH", position=(left, bottom), font_size=font_size, color=(0.5, 0, 1), bg_color=bg_color, justification="left", vertical_justification="bottom") ] texts += [ ui.TextBlock2D("HH", position=(center, bottom), font_size=font_size, color=(1, 0.5, 0), bg_color=bg_color, justification="center", vertical_justification="bottom") ] texts += [ ui.TextBlock2D("HH", position=(right, bottom), font_size=font_size, color=(0, 1, 0.5), bg_color=bg_color, justification="right", vertical_justification="bottom") ] show_manager.ren.add(*texts) # Uncomment this to start the visualisation # show_manager.start() arr = window.snapshot(show_manager.ren, size=window_size, offscreen=True) if vtk.vtkVersion.GetVTKVersion() == "6.0.0": expected = np.load(pjoin(DATA_DIR, "test_ui_text_block.npz")) npt.assert_array_almost_equal(arr, expected["arr_0"])
def test_ui_button_panel(recording=False): filename = "test_ui_button_panel" recording_filename = pjoin(DATA_DIR, filename + ".log.gz") expected_events_counts_filename = pjoin(DATA_DIR, filename + ".pkl") # Rectangle rectangle_test = ui.Rectangle2D(size=(10, 10)) another_rectangle_test = ui.Rectangle2D(size=(1, 1)) # Button fetch_viz_icons() icon_files = [] icon_files.append(('stop', read_viz_icons(fname='stop2.png'))) icon_files.append(('play', read_viz_icons(fname='play3.png'))) button_test = ui.Button2D(icon_fnames=icon_files) button_test.center = (20, 20) def make_invisible(i_ren, obj, button): # i_ren: CustomInteractorStyle # obj: vtkActor picked # button: Button2D button.set_visibility(False) i_ren.force_render() i_ren.event.abort() def modify_button_callback(i_ren, obj, button): # i_ren: CustomInteractorStyle # obj: vtkActor picked # button: Button2D button.next_icon() i_ren.force_render() button_test.on_right_mouse_button_pressed = make_invisible button_test.on_left_mouse_button_pressed = modify_button_callback button_test.scale((2, 2)) button_color = button_test.color button_test.color = button_color # TextBlock text_block_test = ui.TextBlock2D() text_block_test.message = 'TextBlock' text_block_test.color = (0, 0, 0) # Panel panel = ui.Panel2D(size=(300, 150), position=(290, 15), color=(1, 1, 1), align="right") panel.add_element(rectangle_test, (290, 135)) panel.add_element(button_test, (0.1, 0.1)) panel.add_element(text_block_test, (0.7, 0.7)) npt.assert_raises(ValueError, panel.add_element, another_rectangle_test, (10., 0.5)) npt.assert_raises(ValueError, panel.add_element, another_rectangle_test, (-0.5, 0.5)) # Assign the counter callback to every possible event. event_counter = EventCounter() event_counter.monitor(button_test) event_counter.monitor(panel.background) current_size = (600, 600) show_manager = window.ShowManager(size=current_size, title="DIPY Button") show_manager.ren.add(panel) if recording: show_manager.record_events_to_file(recording_filename) print(list(event_counter.events_counts.items())) event_counter.save(expected_events_counts_filename) else: show_manager.play_events_from_file(recording_filename) expected = EventCounter.load(expected_events_counts_filename) event_counter.check_counts(expected)
several DIPY UI elements, then use a list box to toggle which element is shown. First, a bunch of imports. """ from dipy.viz import read_viz_icons, fetch_viz_icons from dipy.viz import ui, window """ Shapes ====== Let's start by drawing some simple shapes. First, a rectangle. """ rect = ui.Rectangle2D(size=(200, 200), position=(400, 300), color=(1, 0, 1)) """ Then we can draw a solid circle, or disk. """ disk = ui.Disk2D(outer_radius=50, center=(500, 500), color=(1, 1, 0)) """ Add an inner radius to make a ring. """ ring = ui.Disk2D(outer_radius=50, inner_radius=45, center=(500, 300), color=(0, 1, 1)) """ Image
def test_ui_button_panel(recording=False): filename = "test_ui_button_panel" recording_filename = pjoin(DATA_DIR, filename + ".log.gz") expected_events_counts_filename = pjoin(DATA_DIR, filename + ".pkl") # Rectangle rectangle_test = ui.Rectangle2D(size=(10, 10)) rectangle_test.get_actors() another_rectangle_test = ui.Rectangle2D(size=(1, 1)) # /Rectangle # Button fetch_viz_icons() icon_files = dict() icon_files['stop'] = read_viz_icons(fname='stop2.png') icon_files['play'] = read_viz_icons(fname='play3.png') button_test = ui.Button2D(icon_fnames=icon_files) button_test.set_center((20, 20)) def make_invisible(i_ren, obj, button): # i_ren: CustomInteractorStyle # obj: vtkActor picked # button: Button2D button.set_visibility(False) i_ren.force_render() i_ren.event.abort() def modify_button_callback(i_ren, obj, button): # i_ren: CustomInteractorStyle # obj: vtkActor picked # button: Button2D button.next_icon() i_ren.force_render() button_test.on_right_mouse_button_pressed = make_invisible button_test.on_left_mouse_button_pressed = modify_button_callback button_test.scale((2, 2)) button_color = button_test.color button_test.color = button_color # /Button # Panel panel = ui.Panel2D(center=(440, 90), size=(300, 150), color=(1, 1, 1), align="right") panel.add_element(rectangle_test, 'absolute', (580, 150)) panel.add_element(button_test, 'relative', (0.2, 0.2)) npt.assert_raises(ValueError, panel.add_element, another_rectangle_test, 'error_string', (1, 2)) # /Panel # Assign the counter callback to every possible event. event_counter = EventCounter() event_counter.monitor(button_test) event_counter.monitor(panel) current_size = (600, 600) show_manager = window.ShowManager(size=current_size, title="DIPY Button") show_manager.ren.add(panel) if recording: show_manager.record_events_to_file(recording_filename) print(list(event_counter.events_counts.items())) event_counter.save(expected_events_counts_filename) else: show_manager.play_events_from_file(recording_filename) expected = EventCounter.load(expected_events_counts_filename) event_counter.check_counts(expected)