Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tutorial for RadioButton and CheckBox UI #208

Merged
merged 5 commits into from Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
251 changes: 251 additions & 0 deletions docs/tutorials/01_ui/viz_check_boxes.py
@@ -0,0 +1,251 @@
"""
============================================================
Figure and Color Control using Check boxes and Radio Buttons
============================================================

This example shows how to use the UI API. We will demonstrate how to
create a cube and control its color using radio buttons.

First, some imports.
"""

from fury import ui, window
skoudoro marked this conversation as resolved.
Show resolved Hide resolved

########################################################################
# Add a cube to the scene.
# =======================


def cube_maker(color=(1, 1, 1), size=(0.2, 0.2, 0.2), center=(0, 0, 0)):
skoudoro marked this conversation as resolved.
Show resolved Hide resolved
cube = window.vtk.vtkCubeSource()
cube.SetXLength(size[0])
cube.SetYLength(size[1])
cube.SetZLength(size[2])

if center is not None:
cube.SetCenter(*center)

cube_mapper = window.vtk.vtkPolyDataMapper()
cube_mapper.SetInputConnection(cube.GetOutputPort())
cube_actor = window.vtk.vtkActor()
cube_actor.SetMapper(cube_mapper)

if color is not None:
cube_actor.GetProperty().SetColor(color)

return cube_actor


########################################################################
# Add a sphere to the scene.
# =========================


def sphere_maker(color=(1, 1, 1), radius=5.0, center=(0, 0, 0),
skoudoro marked this conversation as resolved.
Show resolved Hide resolved
theta_resolution=360, phi_resolution=360):
sphere = window.vtk.vtkSphereSource()
sphere.SetCenter(*center)
sphere.SetRadius(radius)
sphere.SetThetaResolution(theta_resolution)
sphere.SetPhiResolution(phi_resolution)

sphere_mapper = window.vtk.vtkPolyDataMapper()
if window.vtk.VTK_MAJOR_VERSION <= 5:
sphere_mapper.SetInput(sphere.GetOutput())
else:
sphere_mapper.SetInputConnection(sphere.GetOutputPort())

sphere_actor = window.vtk.vtkActor()
sphere_actor.SetMapper(sphere_mapper)
if color is not None:
sphere_actor.GetProperty().SetColor(color)

return sphere_actor


########################################################################
# Add a cone to the scene.
# =======================


def cone_maker(color=(1, 1, 1), radius=5.0, center=(0, 0, 0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of this function, it is on the fury.actor module. Please, remove it.

height=15, resolution=100, direction=(0, 5, 0)):
cone = window.vtk.vtkConeSource()
cone.SetResolution(resolution)
cone.SetCenter(*center)
cone.SetRadius(radius)
cone.SetHeight(height)

cone.SetDirection(*direction)
cone_mapper = window.vtk.vtkPolyDataMapper()
if window.vtk.VTK_MAJOR_VERSION <= 5:
cone_mapper.SetInput(cone.GetOutput())
else:
cone_mapper.SetInputConnection(cone.GetOutputPort())

cone_actor = window.vtk.vtkActor()
cone_actor.SetMapper(cone_mapper)

if color is not None:
cone_actor.GetProperty().SetColor(color)
return cone_actor


########################################################################
# Add an arrow to the scene.
# =========================


def arrow_maker(color=(1, 1, 1), start_point=(0, 25, 0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of this function, it is on the fury.actor module. Please, remove it.

end_point=(40, 25, 0), shaft_resolution=50,
tip_resolution=50):

# Create an arrow.
arrow = window.vtk.vtkArrowSource()
arrow.SetShaftResolution(shaft_resolution)
arrow.SetTipResolution(tip_resolution)

# Compute a basis
normalizedX = [0 for i in range(3)]
normalizedY = [0 for i in range(3)]
normalizedZ = [0 for i in range(3)]

# The X axis is a vector from start to end
math = window.vtk.vtkMath()
math.Subtract(end_point, start_point, normalizedX)
length = math.Norm(normalizedX)
math.Normalize(normalizedX)

# The Z axis is an arbitrary vector cross X
arbitrary = [60, 10, 0]
math.Cross(normalizedX, arbitrary, normalizedZ)
math.Normalize(normalizedZ)

# The Y axis is Z cross X
math.Cross(normalizedZ, normalizedX, normalizedY)
matrix = window.vtk.vtkMatrix4x4()

# Create the direction cosine matrix
matrix.Identity()
for i in range(3):
matrix.SetElement(i, 0, normalizedX[i])
matrix.SetElement(i, 1, normalizedY[i])
matrix.SetElement(i, 2, normalizedZ[i])

# Apply the transforms
transform = window.vtk.vtkTransform()
transform.Translate(start_point)
transform.Concatenate(matrix)
transform.Scale(length, length, length)

# Transform the polydata
transformPD = window.vtk.vtkTransformPolyDataFilter()
transformPD.SetTransform(transform)
transformPD.SetInputConnection(arrow.GetOutputPort())

# Create a mapper and actor for the arrow
arrow_mapper = window.vtk.vtkPolyDataMapper()
arrow_actor = window.vtk.vtkActor()
arrow_actor.SetUserMatrix(transform.GetMatrix())

if window.vtk.VTK_MAJOR_VERSION <= 5:
arrow_mapper.SetInput(arrow.GetOutput())
else:
arrow_mapper.SetInputConnection(arrow.GetOutputPort())

if color is not None:
arrow_actor.GetProperty().SetColor(color)

arrow_actor.SetMapper(arrow_mapper)

return arrow_actor


# Get difference between two lists.
def sym_diff(l1, l2):
return list(set(l1).symmetric_difference(set(l2)))


# Set Visiblity of the figures
def set_figure_visiblity(checkboxes):
checked = checkboxes.checked_labels
unchecked = sym_diff(list(figure_dict), checked)

for visible in checked:
figure_dict[visible].SetVisibility(True)

for invisible in unchecked:
figure_dict[invisible].SetVisibility(False)


# Toggle colors of the figures
def toggle_color(radio):
color = options[radio.checked_labels[0]]
for _, figure in figure_dict.items():
figure.GetProperty().SetColor(*color)


cube = cube_maker(color=(0, 0, 1), size=(20, 20, 20), center=(15, 0, 0))
sphere = sphere_maker(color=(0, 0, 1), radius=11.0, center=(50, 0, 0),
theta_resolution=360, phi_resolution=360)
cone = cone_maker(color=(0, 0, 1), radius=10.0, center=(-20, -0.5, 0),
height=20)
arrow = arrow_maker(color=(0, 0, 1), start_point=(0, 25, 0),
end_point=(40, 25, 0), shaft_resolution=50,
tip_resolution=50)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove cube_maker, sphere_maker, cone_maker, arrow_maker and replace them by
actor.cone, actor.cube, actor.sphere, actor.arrow.

you need to import actor module on the top

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's cool, I'll have a look at it.
But then why are we not using actor.cube in other tutorials as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because they are old and they need to be updated 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, I was looking at the actor earlier while coding the tutorials and was wondering why are we not incorporating this instead.
In this case, I should update viz_ui.py and viz_ui_slider.py as well, maybe in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skoudoro I'm unable to toggle colors of the figures when using actor.cube.
I've checked the value of colors is being set using cube.GetProperty().GetColor() but it's not reflecting on the window.
what could be the reason? maybe the actors which are returned are the reason?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it is much more complicated to update the colors. We need to create some functions/interfaces to make it easier. So, keep it as it is for the moment and just do the other corrections.
To get the color, you can use cube.GetMapper().GetColorMapColors() but it is not convenient to update it. It needs some thinking.
Thank you @PaulNicolasHunter !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I understand @skoudoro, I'll try to dig more into it and find a workaround as soon as possible.



figure_dict = {'cube': cube, 'sphere': sphere, 'cone': cone, 'arrow': arrow}
check_box = ui.Checkbox(list(figure_dict), list(figure_dict),
padding=1, font_size=18, font_family='Arial',
position=(400, 85))

options = {'Blue': (0, 0, 1), 'Red': (1, 0, 0), 'Green': (0, 1, 0)}
color_toggler = ui.RadioButton(list(options), checked_labels=['Blue'],
padding=1, font_size=16,
font_family='Arial', position=(600, 120))


check_box.on_change = set_figure_visiblity
color_toggler.on_change = toggle_color


###############################################################################
# Show Manager
# ==================================
#
# Now that all the elements have been initialised, we add them to the show
# manager.

current_size = (1000, 1000)
show_manager = window.ShowManager(size=current_size,
title="FURY Checkbox Example")

show_manager.scene.add(cube)
show_manager.scene.add(sphere)
show_manager.scene.add(cone)
show_manager.scene.add(arrow)
show_manager.scene.add(check_box)
show_manager.scene.add(color_toggler)

cube.SetVisibility(True)
sphere.SetVisibility(True)
cone.SetVisibility(True)
arrow.SetVisibility(True)
check_box.set_visibility(True)
color_toggler.set_visibility(True)

###############################################################################
# Set camera for better visualization

show_manager.scene.reset_camera()
show_manager.scene.set_camera(position=(0, 0, 150))
show_manager.scene.reset_clipping_range()
show_manager.scene.azimuth(30)
interactive = False

if interactive:
show_manager.start()

window.record(show_manager.scene,
size=current_size, out_path="viz_slider.png")
88 changes: 88 additions & 0 deletions docs/tutorials/01_ui/viz_radio_buttons.py
@@ -0,0 +1,88 @@
"""
======================================
Cube Color Control using Radio Buttons
======================================

This example shows how to use the UI API. We will demonstrate how to
create a cube and control its color using radio buttons.

First, some imports.
"""

from fury import ui, window

########################################################################
# Cube and Radio Buttons
# ======================
#
# Add a cube to the scene.


def cube_maker(color=(1, 1, 1), size=(0.2, 0.2, 0.2), center=(0, 0, 0)):
cube = window.vtk.vtkCubeSource()
cube.SetXLength(size[0])
cube.SetYLength(size[1])
cube.SetZLength(size[2])

if center is not None:
cube.SetCenter(*center)

cube_mapper = window.vtk.vtkPolyDataMapper()
cube_mapper.SetInputConnection(cube.GetOutputPort())
cube_actor = window.vtk.vtkActor()
cube_actor.SetMapper(cube_mapper)

if color is not None:
cube_actor.GetProperty().SetColor(color)

return cube_actor


# Creating a dict of possible options and mapping it with their values.
options = {'Blue': (0, 0, 1), 'Red': (1, 0, 0), 'Green': (0, 1, 0)}

color_toggler = ui.RadioButton(list(options), checked_labels=['Blue'],
padding=1, font_size=16,
font_family='Arial', position=(200, 200))

cube = cube_maker(color=(0, 0, 1), size=(20, 20, 20), center=(15, 0, 0))


# A callback which will set the values for the box
def toggle_color(radio):
color = options[radio.checked_labels[0]]
cube.GetProperty().SetColor(*color)


color_toggler.on_change = toggle_color


###############################################################################
# Show Manager
# ============
#
# Now that all the elements have been initialised, we add them to the show
# manager.

current_size = (800, 800)
show_manager = window.ShowManager(size=current_size, title="FURY Cube Example")

show_manager.scene.add(cube)
show_manager.scene.add(color_toggler)
color_toggler.set_visibility(True)
cube.SetVisibility(True)

###############################################################################
# Set camera for better visualization

show_manager.scene.reset_camera()
show_manager.scene.set_camera(position=(0, 0, 150))
show_manager.scene.reset_clipping_range()
show_manager.scene.azimuth(30)
interactive = False

if interactive:
show_manager.start()

window.record(show_manager.scene,
size=current_size, out_path="radio_button.png")