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

Implemented vtkBillboardTextActor #525

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 83 additions & 4 deletions fury/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Texture, FloatArray, VTK_TEXT_LEFT, VTK_TEXT_RIGHT,
VTK_TEXT_BOTTOM, VTK_TEXT_TOP, VTK_TEXT_CENTERED,
TexturedActor2D, TextureMapToPlane, TextActor3D,
Follower, VectorText)
Follower, VectorText, OpenGLBillboardTextActor3D)
import fury.primitive as fp
from fury.utils import (lines_to_vtk_polydata, set_input, apply_affine,
set_polydata_vertices, set_polydata_triangles,
Expand Down Expand Up @@ -954,6 +954,7 @@ def _roll_evals(evals, axis=-1):

return evals


def _fa(evals, axis=-1):
r"""Return Fractional anisotropy (FA) of a diffusion tensor.

Expand Down Expand Up @@ -1026,9 +1027,6 @@ def _color_fa(fa, evecs):
return np.abs(evecs[..., 0]) * np.clip(fa, 0, 1)[..., None]





def tensor_slicer(evals, evecs, affine=None, mask=None, sphere=None, scale=2.2,
norm=True, opacity=1., scalar_colors=None):
"""Slice many tensors as ellipsoids in native or world coordinates.
Expand Down Expand Up @@ -2417,6 +2415,87 @@ def _update_user_matrix(self):
return text_actor


def opengl_billboard_text3D(text, position=(0, 0, 0), color=(1, 1, 1),
font_size=12, font_family='Arial',
justification='center',
vertical_justification='bottom'):
""" Fixed sized, pixel aligned text, faces the camera

parameters
----------
text : str
position : tuple
color : tuple
font_size : int
font_family : str
justification : str
Left, center or right (default left)
vertical_justification : str
Bottom, middle or top (default bottom)

Returns
-------
vtkOpenGLBillboardTextActor3D
"""

class OpenGlBillboardText3D(OpenGLBillboardTextActor3D):
def set_message(self, text):
self.SetInput(text)

def get_message(self):
return self.GetInput()

def set_position(self, position):
self.SetPosition(position)

def get_position(self):
return self.GetPosition()

def font_size(self, size):
self.GetTextProperty().SetFontSize(size)

def font_family(self, _family='Arial'):
self.GetTextProperty().SetFontFamilyToArial()

def color(self, color):
self.GetTextProperty().SetColor(*color)

def justification(self, justification):
tprop = self.GetTextProperty()
if justification == 'left':
tprop.SetJustificationToLeft()
elif justification == 'center':
tprop.SetJustificationToCentered()
elif justification == 'right':
tprop.SetJustificationToRight()
else:
raise ValueError("Unknown justification: '{}'"
.format(justification))

def vertical_justification(self, justification):
tprop = self.GetTextProperty()
if justification == 'top':
tprop.SetVerticalJustificationToTop()
elif justification == 'middle':
tprop.SetVerticalJustificationToCentered()
elif justification == 'bottom':
tprop.SetVerticalJustificationToBottom()
else:
raise ValueError("Unknown vertical justification: '{}'"
.format(justification))

billboard_text_actor = OpenGlBillboardText3D()
billboard_text_actor.set_message(text)
billboard_text_actor.set_position(position)
billboard_text_actor.font_size(font_size)
billboard_text_actor.font_family(font_family)
billboard_text_actor.color(color)
billboard_text_actor.justification(justification)
billboard_text_actor.vertical_justification(vertical_justification)

return billboard_text_actor


class Container(object):
""" Provides functionalities for grouping multiple actors using a given
layout.
Expand Down
2 changes: 2 additions & 0 deletions fury/lib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from vtkmodules.vtkRenderingOpenGL2 import vtkOpenGLBillboardTextActor3D
import vtkmodules.vtkCommonCore as ccvtk
import vtkmodules.vtkCommonDataModel as cdmvtk
import vtkmodules.vtkCommonExecutionModel as cemvtk
Expand Down Expand Up @@ -103,6 +104,7 @@
##############################################################
# vtkRenderingOpenGL2 Module
Shader = roglvtk.vtkShader
OpenGLBillboardTextActor3D = roglvtk.vtkOpenGLBillboardTextActor3D

##############################################################
# vtkInteractionStyle Module
Expand Down
41 changes: 41 additions & 0 deletions fury/tests/test_actors.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,47 @@ def test_text_3d():
assert_greater_equal(arr.mean(), arr_bottom.mean())


def test_billboard_text_3d():
msg = 'I\nLove\nFury'

bill_txt_actor = actor.opengl_billboard_text3D(msg, color=(1, 0, 0))
npt.assert_equal(bill_txt_actor.get_message().lower(), msg.lower())
npt.assert_raises(ValueError, bill_txt_actor.justification, 'middle')
npt.assert_raises(
ValueError, bill_txt_actor.vertical_justification, 'center')

scene = window.Scene()

scene.add(bill_txt_actor)
bill_txt_actor.vertical_justification('middle')
bill_txt_actor.justification('right')

arr_right = window.snapshot(scene, size=(1920, 1080), offscreen=True)

scene.clear()
scene.add(bill_txt_actor)
bill_txt_actor.vertical_justification('middle')
bill_txt_actor.justification('left')
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

# TODO:
# assert_greater(center_of_mass(arr_left)[0], center_of_mass(arr_right)[0])

snap_small_text = window.snapshot(scene, size=(500, 500), offscreen=True)
scene.clear()

bill_txt_actor.font_size(13)
scene.add(bill_txt_actor)
snap_large_text = window.snapshot(scene, size=(500, 500), offscreen=True)

# Sum of Red values in larger text should be greater
assert_greater(
np.sum(snap_large_text[:, :, 0]), np.sum(snap_small_text[:, :, 0]))


def test_container():
container = actor.Container()

Expand Down