Skip to content

Commit 913a79f

Browse files
Merge pull request #1 from RobinEnjalbert/vedo_text
Add Text in available Vedo objects.
2 parents 7ae8399 + 9ec9f5d commit 913a79f

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

examples/Core/rendering/text.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from time import sleep
2+
3+
from SSD.Core.Rendering.VedoFactory import VedoFactory
4+
from SSD.Core.Rendering.VedoVisualizer import VedoVisualizer
5+
6+
7+
# 1. Create the rendering and the Factory, bind them to a new Database
8+
visualizer = VedoVisualizer(database_name='text',
9+
remove_existing=True)
10+
factory = VedoFactory(database=visualizer.get_database())
11+
12+
13+
# 2. Add objects to the Factory then init the rendering
14+
factory.add_text(content='Static Bold Text',
15+
at=0, corner='TM', c='grey', font='Times', size=3., bold=True)
16+
factory.add_text(content='Static Italic Text',
17+
at=0, corner='BM', c='green7', font='Courier', size=2., italic=True)
18+
factory.add_text(content='0', at=0)
19+
visualizer.init_visualizer()
20+
21+
22+
# 3. Run a few step
23+
for step in range(50):
24+
factory.update_text(object_id=2, content=f'{step}')
25+
visualizer.render()
26+
sleep(0.05)

src/Core/Rendering/VedoActor.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Optional, Dict
22
from numpy import array, tile
3-
from vedo import Mesh, Points, Arrows, Marker, Glyph
3+
from vedo import Mesh, Points, Arrows, Marker, Glyph, Text2D
44

55

66
class VedoActor:
@@ -23,12 +23,14 @@ def __init__(self,
2323
'Points': self.__create_points,
2424
'Arrows': self.__create_arrows,
2525
'Markers': self.__create_markers,
26-
'Symbols': self.__create_symbols}
26+
'Symbols': self.__create_symbols,
27+
'Text': self.__create_text}
2728
update = {'Mesh': self.__update_mesh,
2829
'Points': self.__update_points,
2930
'Arrows': self.__update_arrows,
3031
'Markers': self.__update_markers,
31-
'Symbols': self.__update_symbols}
32+
'Symbols': self.__update_symbols,
33+
'Text': self.__update_text}
3234
self.create = create[self.actor_type]
3335
self.update = update[self.actor_type]
3436

@@ -233,3 +235,40 @@ def __update_symbols(self,
233235
c=self.actor_data['c'],
234236
alpha=self.actor_data['alpha'])
235237
return self
238+
239+
########
240+
# TEXT #
241+
########
242+
243+
def __create_text(self,
244+
data: Dict[str, Any]):
245+
246+
# Register Actor data
247+
self.actor_data = data
248+
249+
# Get Text position
250+
coord = {'B': 'bottom', 'L': 'left', 'M': 'middle', 'R': 'right', 'T': 'top'}
251+
corner = data['corner']
252+
pos = f'{coord[corner[0].upper()]}-{coord[corner[1].upper()]}'
253+
254+
# Create instance
255+
self.instance = Text2D(txt=data['content'],
256+
pos=pos,
257+
s=data['size'],
258+
font=data['font'],
259+
bold=data['bold'],
260+
italic=data['italic'],
261+
c=data['c'])
262+
return self
263+
264+
def __update_text(self,
265+
data: Dict[str, Any]):
266+
267+
# Register Actor data
268+
for key, value in data.items():
269+
if value is not None:
270+
self.actor_data[key] = value
271+
272+
# Update instance
273+
self.instance.text(self.actor_data['content']).c(self.actor_data['c'])
274+
return self

src/Core/Rendering/VedoFactory.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,47 @@ def update_symbols(self,
391391

392392
object_id = self.__check_id(object_id, 'Symbols')
393393
return self.__update_object(object_id, locals())
394+
395+
########
396+
# TEXT #
397+
########
398+
399+
def add_text(self,
400+
content: str,
401+
at: int = 0,
402+
corner: str = 'BR',
403+
c: str = 'black',
404+
font: str = 'Arial',
405+
size: float = 1.,
406+
bold: bool = False,
407+
italic: bool = False):
408+
"""
409+
Add new 2D Text to the Factory.
410+
411+
:param content: Content of the Text.
412+
:param at: Index of the window in which the Text will be rendered.
413+
:param corner: Horizontal and vertical positions of the Text between T (top), M (middle) and B (bottom) - for
414+
instance, 'BR' stands for 'bottom-right'.
415+
:param c: Text color.
416+
:param font: Font of the Text.
417+
:param size: Size of the font.
418+
:param bold: Apply bold style to the Text.
419+
:param italic: Apply italic style to the Text.
420+
"""
421+
422+
return self.__add_object('Text', locals())
423+
424+
def update_text(self,
425+
object_id: int,
426+
content: Optional[str] = None,
427+
c: Optional[str] = None):
428+
"""
429+
Update existing Text in the Factory.
430+
431+
:param object_id: Index of the object (follows the global order of creation).
432+
:param content: Content of the Text.
433+
:param c: Text color.
434+
"""
435+
436+
object_id = self.__check_id(object_id, 'Text')
437+
return self.__update_object(object_id, locals())

src/Core/Rendering/VedoTable.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ def __init__(self,
2121
'Points': self.__create_points_columns,
2222
'Arrows': self.__create_arrows_columns,
2323
'Markers': self.__create_markers_columns,
24-
'Symbols': self.__create_symbols_columns}
24+
'Symbols': self.__create_symbols_columns,
25+
'Text': self.__create_text_columns}
2526
format_data = {'Mesh': self.__format_mesh_data,
2627
'Points': self.__format_points_data,
2728
'Arrows': self.__format_arrows_data,
2829
'Markers': self.__format_markers_data,
29-
'Symbols': self.__format_symbols_data}
30+
'Symbols': self.__format_symbols_data,
31+
'Text': self.__format_text_data}
3032
self.create_columns = create_columns[self.table_type]
3133
self.format_data = format_data[self.table_type]
3234

@@ -126,6 +128,19 @@ def __create_symbols_columns(self):
126128
])
127129
return self
128130

131+
def __create_text_columns(self):
132+
133+
self.database.create_table(table_name=self.table_name,
134+
fields=[('content', str),
135+
('corner', str),
136+
('c', str),
137+
('font', str),
138+
('size', float),
139+
('bold', bool),
140+
('italic', bool),
141+
('at', int, 0)])
142+
return self
143+
129144
#######################
130145
# FORMAT DATA METHODS #
131146
#######################
@@ -196,6 +211,16 @@ def __format_symbols_data(cls,
196211
data_dict[field] = cls.parse_vector(value, coords=field in ['positions', 'orientations'])
197212
return data_dict
198213

214+
@classmethod
215+
def __format_text_data(cls,
216+
data_dict: Dict[str, Any]):
217+
218+
data_dict_copy = data_dict.copy()
219+
for field, value in data_dict_copy.items():
220+
if value is None:
221+
data_dict.pop(field)
222+
return data_dict
223+
199224
@classmethod
200225
def parse_vector(cls,
201226
vec,

0 commit comments

Comments
 (0)