Skip to content

Commit aee7e46

Browse files
Update Core.Rendering package.
1 parent 4d61be2 commit aee7e46

28 files changed

+666
-640
lines changed

examples/Core/rendering/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It is recommended to inspect and run the python scripts following this order:
66

77
* ``visualization.py``: discover all visual object types (how to create and update them).
88
* ``replay.py``: how to replay a simulation from a *Database*.
9-
* ``<backend>_offscreen.py``: an example of offscreen rendering.
9+
* ``offscreen.py``: an example of off-screen rendering.
1010
* ``several_factories.py``: how to manage several *Factories* with a single *Visualizer*.
1111

1212
Run the examples using ``python3 <example_name>.py <backend>`` with backend being either 'vedo' (by default) or 'open3d'.

examples/Core/rendering/offscreen.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,51 @@
22
from numpy.random import random
33
from sys import argv
44

5-
from SSD.Core import UserAPI, Replay
5+
from SSD.Core.Rendering import UserAPI, Replay
66

77
# 1. Create the visualization API
8-
factory = UserAPI(database_name='offscreen',
8+
factory = UserAPI(database_dir='my_databases',
9+
database_name='offscreen',
910
remove_existing=True)
1011

1112
# 2. Create the object to render
12-
armadillo = Mesh('armadillo.obj')
13+
mesh = Mesh('armadillo.obj').compute_normals()
1314

1415
# 3. Add objects to the Visualizer
1516
# 3.1. Add a Mesh (object_id = 0)
16-
factory.add_mesh(positions=armadillo.points(),
17-
cells=armadillo.cells(),
17+
factory.add_mesh(positions=mesh.vertices,
18+
cells=mesh.cells,
1819
at=0,
1920
alpha=0.8,
2021
c='orange6',
2122
wireframe=False,
2223
line_width=0.)
2324
# 3.2. Add a PointCloud (object_id = 1)
24-
factory.add_points(positions=armadillo.points(),
25+
factory.add_points(positions=mesh.vertices,
2526
at=1,
2627
point_size=5,
27-
scalar_field=armadillo.points()[:, 1])
28+
scalar_field=mesh.vertices[:, 1])
2829

2930
# 4. Initialize the visualization
3031
factory.launch_visualizer(offscreen=True)
3132

3233
# 5. Run a few steps
3334
print('Running offscreen...')
3435
for step in range(100):
35-
updated_armadillo = armadillo.clone().points(armadillo.points() + 0.1 * random(armadillo.points().shape))
36+
updated_mesh = mesh.clone()
37+
updated_mesh.vertices = mesh.vertices + 0.1 * random(mesh.vertices.shape)
3638
# 5.1. Update the Mesh
3739
factory.update_mesh(object_id=0,
38-
positions=updated_armadillo.points())
40+
positions=updated_mesh.vertices)
3941
# 5.2. Update the PointCloud
4042
factory.update_points(object_id=1,
41-
positions=updated_armadillo.points())
43+
positions=updated_mesh.vertices)
4244
# 5.3. Call a rendering step
4345
factory.render()
4446

4547
# 6. Close the visualization, replay steps
4648
print('...then replay.')
4749
factory.close()
48-
Replay(database_name='offscreen',
50+
Replay(database_dir='my_databases',
51+
database_name='offscreen',
4952
backend='vedo' if len(argv) == 1 else argv[1]).launch()

examples/Core/rendering/replay.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from os.path import exists
2+
from os import system
23
from sys import argv
34

4-
from SSD.Core import Replay
5+
from SSD.Core.Rendering import Replay
56

67
# Check Database existence
7-
if not exists('visualization.db'):
8-
raise FileNotFoundError("You must create the Database using `python3 visualization.py` before to replay it.")
8+
if not exists('my_databases/visualization.db'):
9+
system('python3 visualization.py')
910

1011
# Launch replay
11-
Replay(database_name='visualization',
12+
Replay(database_dir='my_databases',
13+
database_name='visualization',
1214
backend='vedo' if len(argv) == 1 else argv[1]).launch()

examples/Core/rendering/several_factories.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from threading import Thread
44
from sys import argv
55

6-
from SSD.Core import Database, UserAPI, Visualizer
6+
from SSD.Core.Storage import Database
7+
from SSD.Core.Rendering import UserAPI, Visualizer
78

89

910
class Simulation:
@@ -13,12 +14,12 @@ def __init__(self,
1314
idx_instance: int):
1415

1516
# Create the Mesh object
16-
self.armadillo = Mesh('armadillo.obj')
17+
self.mesh = Mesh('armadillo.obj')
1718
# Create a Factory and add the Mesh object
1819
self.factory = UserAPI(database=database,
1920
idx_instance=idx_instance)
20-
self.factory.add_mesh(positions=self.armadillo.points(),
21-
cells=self.armadillo.cells(),
21+
self.factory.add_mesh(positions=self.mesh.vertices,
22+
cells=self.mesh.cells,
2223
at=idx_instance,
2324
c='orange3' if idx_instance == 0 else 'blue3')
2425

@@ -30,7 +31,7 @@ def connect_to_visualizer(self):
3031
def step(self):
3132

3233
# Update the Mesh positions
33-
updated_positions = self.armadillo.points() + 0.1 * random(self.armadillo.points().shape)
34+
updated_positions = self.mesh.vertices + 0.1 * random(self.mesh.vertices.shape)
3435
self.factory.update_mesh(object_id=0,
3536
positions=updated_positions)
3637
self.factory.render()
@@ -44,7 +45,8 @@ def close(self):
4445
if __name__ == '__main__':
4546

4647
# 1. Create a new Database
47-
db = Database(database_name='several_factories').new(remove_existing=True)
48+
db = Database(database_dir='my_databases',
49+
database_name='several_factories').new(remove_existing=True)
4850

4951
# 2. Create several simulations
5052
nb_simu = 2
@@ -54,6 +56,7 @@ def close(self):
5456
# 3. Connect a single Visualizer to the Factories
5557
# 3.1. Create a new Visualizer
5658
Visualizer.launch(backend='vedo' if len(argv) == 1 else argv[1],
59+
database_dir='my_databases',
5760
database_name='several_factories',
5861
nb_clients=nb_simu)
5962
# 3.2. Connect each Factory to the Visualizer (must be launched in thread)

examples/Core/rendering/several_factories_offscreen.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from numpy.random import random
33
from sys import argv
44

5-
from SSD.Core import Database, UserAPI, Replay
5+
from SSD.Core.Storage import Database
6+
from SSD.Core.Rendering import UserAPI, Replay
67

78

89
class Simulation:
@@ -12,12 +13,12 @@ def __init__(self,
1213
idx_instance: int):
1314

1415
# Create the Mesh object
15-
self.armadillo = Mesh('armadillo.obj')
16+
self.mesh = Mesh('armadillo.obj')
1617
# Create a Factory and add the Mesh object
1718
self.factory = UserAPI(database=database,
1819
idx_instance=idx_instance)
19-
self.factory.add_mesh(positions=self.armadillo.points(),
20-
cells=self.armadillo.cells(),
20+
self.factory.add_mesh(positions=self.mesh.vertices,
21+
cells=self.mesh.cells,
2122
at=idx_instance,
2223
c='orange3' if idx_instance == 0 else 'blue3')
2324

@@ -29,7 +30,7 @@ def connect_to_visualizer(self):
2930
def step(self):
3031

3132
# Update the Mesh positions
32-
updated_positions = self.armadillo.points() + 0.1 * random(self.armadillo.points().shape)
33+
updated_positions = self.mesh.vertices + 0.1 * random(self.mesh.vertices.shape)
3334
self.factory.update_mesh(object_id=0,
3435
positions=updated_positions)
3536
self.factory.render()
@@ -43,7 +44,8 @@ def close(self):
4344
if __name__ == '__main__':
4445

4546
# 1. Create a new Database
46-
db = Database(database_name='several_factories_offscreen').new(remove_existing=True)
47+
db = Database(database_dir='my_databases',
48+
database_name='several_factories_offscreen').new(remove_existing=True)
4749

4850
# 2. Create several simulations
4951
nb_simu = 2
@@ -63,5 +65,6 @@ def close(self):
6365
# 5. Close the Factories & Replay steps
6466
for simu in simulations:
6567
simu.close()
66-
Replay(database_name='several_factories_offscreen',
68+
Replay(database_dir='my_databases',
69+
database_name='several_factories_offscreen',
6770
backend='vedo' if len(argv) == 1 else argv[1]).launch()

examples/Core/rendering/visualization.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,41 @@
33
from numpy.random import random
44
from sys import argv
55

6-
from SSD.Core import UserAPI
6+
from SSD.Core.Rendering import UserAPI
77

88
# 1. Create the visualization API
9-
factory = UserAPI(database_name='visualization',
9+
factory = UserAPI(database_dir='my_databases',
10+
database_name='visualization',
1011
remove_existing=True)
1112

1213
# 2. Create the object to render
13-
armadillo = Mesh('armadillo.obj')
14+
mesh = Mesh('armadillo.obj').compute_normals()
1415

1516
# 3. Add objects to the Visualizer
1617
# 3.1. Add a Mesh (object_id = 0)
17-
factory.add_mesh(positions=armadillo.points(),
18-
cells=armadillo.cells(),
18+
factory.add_mesh(positions=mesh.vertices,
19+
cells=mesh.cells,
1920
at=0,
2021
alpha=0.8,
2122
c='orange6',
2223
wireframe=False,
2324
line_width=0.)
2425
# 3.2. Add a PointCloud (object_id = 1)
25-
factory.add_points(positions=armadillo.points(),
26+
factory.add_points(positions=mesh.vertices,
2627
at=1,
2728
point_size=5,
28-
scalar_field=armadillo.points()[:, 1])
29+
scalar_field=mesh.vertices[:, 1])
2930
# 3.3. Add Arrows (object_id = 2)
30-
factory.add_arrows(positions=armadillo.points()[:100],
31-
vectors=armadillo.normals()[:100],
31+
factory.add_arrows(positions=mesh.vertices[:100],
32+
vectors=mesh.vertex_normals[:100],
3233
at=2,
3334
res=15)
3435
# 3.4. Add Markers (object_id = 3)
3536
factory.add_markers(normal_to=0,
3637
indices=arange(0, 10),
3738
at=3,
3839
size=1,
39-
symbol='0')
40+
symbol='*')
4041
# 3.5. Add Text (object_id = 4)
4142
factory.add_text(content='0',
4243
at=0,
@@ -47,29 +48,30 @@
4748
factory.launch_visualizer(backend='vedo' if len(argv) == 1 else argv[1],
4849
fps=20)
4950

50-
# 5. Run a few steps
51+
# # 5. Run a few steps
5152
for step in range(100):
52-
updated_armadillo = armadillo.clone().points(armadillo.points() + 0.1 * random(armadillo.points().shape))
53+
updated_mesh = mesh.clone()
54+
updated_mesh.vertices = mesh.vertices + 0.1 * random(mesh.vertices.shape)
5355
# 5.1. Update the Mesh
5456
factory.update_mesh(object_id=0,
55-
positions=updated_armadillo.points())
57+
positions=updated_mesh.vertices)
5658
if step == 50:
5759
factory.update_mesh(object_id=0,
5860
wireframe=True,
5961
alpha=1.,
6062
line_width=2.)
6163
# 5.2. Update the PointCloud
6264
factory.update_points(object_id=1,
63-
positions=updated_armadillo.points())
65+
positions=updated_mesh.vertices)
6466
if step == 50:
6567
factory.update_points(object_id=1,
6668
alpha=0.5,
6769
point_size=10,
68-
scalar_field=armadillo.points()[:, 2])
70+
scalar_field=mesh.vertices[:, 2])
6971
# 5.3. Update the Arrows
7072
factory.update_arrows(object_id=2,
71-
positions=armadillo.points()[step:step + 100],
72-
vectors=armadillo.normals()[step:step + 100])
73+
positions=mesh.vertices[step:step + 100],
74+
vectors=mesh.vertex_normals[step:step + 100])
7375
if step == 50:
7476
factory.update_arrows(object_id=2,
7577
c='red')

src/Core/Rendering/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .Replay import Replay
2-
from .Visualizer import Visualizer
3-
from .UserAPI import UserAPI
1+
from .replay import Replay
2+
from .visualizer import Visualizer
3+
from .user_api import UserAPI

0 commit comments

Comments
 (0)