pgleeson commited on
Commit
005501c
·
1 Parent(s): 76b7f87

Tweaks...

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +33 -108
  3. load.py +157 -79
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /__pycache__
app.py CHANGED
@@ -1,10 +1,13 @@
1
  import streamlit as st
2
  import stpyvista.utils
3
 
 
 
4
  if "IS_XVFB_RUNNING" not in st.session_state:
5
- stpyvista.utils.start_xvfb()
6
- st.session_state.IS_XVFB_RUNNING = True
7
 
 
8
  st.title("Sibernetic simple example")
9
 
10
  import pyvista as pv
@@ -12,125 +15,47 @@ import numpy as np
12
 
13
  import time
14
 
15
- points = []
16
- types = []
17
-
18
- file = "position_buffer.txt"
19
-
20
- colours = {1.1: "lightblue", 2.1: "green", 2.2: "turquoise", 3: "#eeeeee"}
21
- colours = {1.1: "blue", 2.2: "turquoise"}
22
-
23
- line_count = 0
24
- pcount = 0
25
-
26
- all_points = []
27
- all_point_types = []
28
 
29
- time_count = 0
30
 
31
- logStep = None
32
 
33
- include_boundary = False
34
 
35
- for l in open(file):
36
- ws = l.split()
37
- #print(ws)
38
- if line_count == 6:
39
- numOfElasticP = int(ws[0])
40
- if line_count == 7:
41
- numOfLiquidP = int(ws[0])
42
- if line_count == 8:
43
- numOfBoundaryP = int(ws[0])
44
- if line_count == 9:
45
- timeStep = float(ws[0])
46
- if line_count == 10:
47
- logStep = int(ws[0])
48
 
49
- if len(ws) == 4:
50
- type = float(ws[3])
 
 
51
 
52
- if not (type == 3 and not include_boundary):
53
- points.append([float(ws[0]), float(ws[1]), float(ws[2])])
54
- types.append(type)
55
 
56
- if logStep is not None:
57
- pcount+=1
58
-
59
- if pcount==numOfBoundaryP+numOfElasticP+numOfLiquidP:
60
- print('End of one batch of %i added, %i total points at line %i, time: %i'%(len(points),pcount, line_count, time_count))
61
- all_points.append(points)
62
- all_point_types.append(types)
63
 
64
- points = []
65
- types=[]
66
- pcount = 0
67
- numOfBoundaryP=0
68
 
69
- time_count+=1
70
-
71
- line_count += 1
72
 
73
- #all_points_np = np.array(all_points)
74
 
75
- print(f"Loaded positions with %i elastic, %i liquid and %i boundary points (%i total), %i lines"%(numOfElasticP,numOfLiquidP, numOfBoundaryP,numOfElasticP+numOfLiquidP+numOfBoundaryP, line_count))
76
- print("Num of time points found: %i"%len(all_points))
 
77
 
78
- pl = pv.Plotter(window_size=[800,600])
79
- pl.set_background("lightgrey")
80
 
81
- pl.camera.position = (300, 100, -100.0)
82
- pl.camera.focal_point = (50,0,100)
83
- pl.camera.up = (0.0, 1.0, 0.0)
84
- #pl.add_axes_at_origin(labels_off=False)
85
- #pl.add_mesh(pv.Cube(center=(50,0,50)))
86
 
87
- last_mesh = None
88
 
89
- def create_mesh(step):
90
-
91
- step_count = step
92
- value=step_count
93
- global last_mesh
94
- index = int(value)
95
-
96
- print('Changing to time point: %s (%s) '%(index,value))
97
- curr_points = all_points[index]
98
- curr_types = all_point_types[index]
99
- if last_mesh is None:
100
-
101
- print('Adding mesh...')
102
- last_mesh = pv.PolyData(curr_points)
103
- last_mesh["types"] = curr_types
104
- print(last_mesh)
105
- mesh2 = pv.Cube(center=(0,0,0))
106
- pl.add_mesh(mesh2)
107
-
108
- last_actor = pl.add_mesh(
109
- last_mesh,
110
- render_points_as_spheres=True,
111
- cmap=[c for c in colours.values()],
112
- point_size=3,
113
- )
114
- else:
115
-
116
- print('Updating mesh...')
117
- last_mesh.points = curr_points
118
-
119
- #pl.render()
120
-
121
- time.sleep(0.1)
122
-
123
- return
124
-
125
- create_mesh(0)
126
-
127
- max_time = len(all_points)-1
128
- #pl.add_slider_widget(create_mesh, rng=[0,max_time], value=max_time, title='Time point')
129
- #pl.add_timer_event( max_steps=len(all_points), duration=200, callback=create_mesh)
130
-
131
- print('Ready...')
132
-
133
- from stpyvista import stpyvista as spv
134
- ## Send to streamlit
135
- spv(pl, key="pv_cube2")
136
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import stpyvista.utils
3
 
4
+ from stpyvista import stpyvista as spv
5
+
6
  if "IS_XVFB_RUNNING" not in st.session_state:
7
+ stpyvista.utils.start_xvfb()
8
+ st.session_state.IS_XVFB_RUNNING = True
9
 
10
+ st.set_page_config(layout="wide")
11
  st.title("Sibernetic simple example")
12
 
13
  import pyvista as pv
 
15
 
16
  import time
17
 
18
+ from load import load_points
19
+ from load import create_plotter
20
+ from load import create_mesh
 
 
 
 
 
 
 
 
 
 
21
 
 
22
 
23
+ filename = "position_buffer.txt"
24
 
 
25
 
26
+ if "step" not in st.session_state:
27
+ st.session_state.step = 3
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ if "all_points" not in st.session_state:
30
+ st.session_state.all_points, st.session_state.all_point_types = load_points(
31
+ filename
32
+ )
33
 
34
+ if "plotter" not in st.session_state:
35
+ print("Creating a new plotter...")
36
+ st.session_state.plotter = create_plotter()
37
 
38
+ global plotter
 
 
 
 
 
 
39
 
40
+ plotter = st.session_state.plotter
 
 
 
41
 
42
+ last_mesh = None
 
 
43
 
44
+ create_mesh(st.session_state.step, plotter)
45
 
46
+ max_time = len(st.session_state.all_points) - 1
47
+ # pl.add_slider_widget(create_mesh, rng=[0,max_time], value=max_time, title='Time point')
48
+ # pl.add_timer_event( max_steps=len(all_points), duration=200, callback=create_mesh)
49
 
50
+ print("Ready to replay...")
 
51
 
52
+ st.session_state.step += 1
 
 
 
 
53
 
54
+ st.write("Step: %s" % st.session_state.step)
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ if st.button("Step"):
58
+ if st.session_state.step <= len(st.session_state.all_points):
59
+ st.write("Stepped...")
60
+ st.session_state.step += 5
61
+ spv(plotter, "sibernetic")
load.py CHANGED
@@ -2,126 +2,204 @@ import pyvista as pv
2
 
3
  import sys
4
  import numpy as np
5
-
6
  import time
7
 
8
- points = []
9
 
10
- types = []
 
11
 
12
- file = "pressure_buffer.txt"
13
- file = "position_buffer.txt"
14
- if len(sys.argv) == 2:
15
- file = sys.argv[1]
16
 
17
- colours = {1.1: "lightblue", 2.1: "green", 2.2: "turquoise", 3: "#eeeeee"}
18
- colours = {1.1: "blue", 2.2: "turquoise"}
19
 
20
- line_count = 0
21
- pcount = 0
22
 
23
- all_points = []
24
- all_point_types = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- time_count = 0
27
 
28
- logStep = None
29
 
30
- include_boundary = False
31
 
32
- for l in open(file):
33
- ws = l.split()
34
- #print(ws)
35
- if line_count == 6:
36
- numOfElasticP = int(ws[0])
37
- if line_count == 7:
38
- numOfLiquidP = int(ws[0])
39
- if line_count == 8:
40
- numOfBoundaryP = int(ws[0])
41
- if line_count == 9:
42
- timeStep = float(ws[0])
43
- if line_count == 10:
44
- logStep = int(ws[0])
45
 
46
- if len(ws) == 4:
47
- type = float(ws[3])
48
 
49
- if not (type == 3 and not include_boundary):
50
- points.append([float(ws[0]), float(ws[1]), float(ws[2])])
51
- types.append(type)
52
 
53
- if logStep is not None:
54
- pcount+=1
55
-
56
- if pcount==numOfBoundaryP+numOfElasticP+numOfLiquidP:
57
- print('End of one batch of %i added, %i total points at line %i, time: %i'%(len(points),pcount, line_count, time_count))
58
- all_points.append(points)
59
- all_point_types.append(types)
60
 
61
- points = []
62
- types=[]
63
- pcount = 0
64
- numOfBoundaryP=0
65
 
66
- time_count+=1
67
 
68
-
69
- line_count += 1
70
 
71
- #all_points_np = np.array(all_points)
72
 
73
- print(f"Loaded positions with %i elastic, %i liquid and %i boundary points (%i total), %i lines"%(numOfElasticP,numOfLiquidP, numOfBoundaryP,numOfElasticP+numOfLiquidP+numOfBoundaryP, line_count))
74
 
75
- print("Num of time points found: %i"%len(all_points))
 
 
 
 
76
 
77
- pl = pv.Plotter()
78
- pl.set_background("lightgrey")
79
 
80
- pl.camera.position = (300, 100, -100.0)
81
- pl.camera.focal_point = (50,0,100)
82
- pl.camera.up = (0.0, 1.0, 0.0)
83
- #pl.add_axes_at_origin(labels_off=False)
84
- #pl.add_mesh(pv.Cube(center=(50,0,50)))
85
 
86
- last_mesh = None
87
 
 
 
 
 
 
88
 
89
- def create_mesh(step):
90
- step_count = step
91
- value=step_count
92
- global last_mesh
93
  index = int(value)
94
 
95
-
96
- print('Changing to time point: %s (%s) '%(index,value))
97
- curr_points = all_points[index]
98
  curr_types = all_point_types[index]
99
- if last_mesh is None:
100
 
 
101
  last_mesh = pv.PolyData(curr_points)
102
  last_mesh["types"] = curr_types
103
  print(last_mesh)
104
-
105
- last_actor = pl.add_mesh(
106
  last_mesh,
107
- render_points_as_spheres=True,
108
  cmap=[c for c in colours.values()],
109
  point_size=3,
 
110
  )
 
 
 
 
 
 
 
 
 
 
 
 
111
  else:
 
112
  last_mesh.points = curr_points
113
 
114
- pl.render()
 
 
115
 
116
- time.sleep(0.01)
117
-
118
  return
119
-
120
- create_mesh(0)
121
 
122
- max_time = len(all_points)-1
123
- pl.add_slider_widget(create_mesh, rng=[0,max_time], value=max_time, title='Time point')
124
- pl.add_timer_event( max_steps=len(all_points), duration=200, callback=create_mesh)
125
 
126
- pl.show()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
 
 
2
 
3
  import sys
4
  import numpy as np
 
5
  import time
6
 
7
+ filename = "position_buffer.txt"
8
 
9
+ all_worm_points = []
10
+ boundary_points = []
11
 
12
+ BOUNDARY_TYPE = 3
 
 
 
13
 
14
+ all_point_types = []
 
15
 
 
 
16
 
17
+ def load_points(filename):
18
+ global all_worm_points
19
+ global boundary_points
20
+ global all_point_types
21
+
22
+ worm_points = []
23
+ types = []
24
+ line_count = 0
25
+
26
+ pcount = 0
27
+
28
+ time_count = 0
29
+
30
+ logStep = None
31
+
32
+ include_boundary = True
33
+
34
+ for l in open(filename):
35
+ ws = l.split()
36
+ # print(ws)
37
+ if line_count == 6:
38
+ numOfElasticP = int(ws[0])
39
+ if line_count == 7:
40
+ numOfLiquidP = int(ws[0])
41
+ if line_count == 8:
42
+ numOfBoundaryP = int(ws[0])
43
+ if line_count == 9:
44
+ timeStep = float(ws[0])
45
+ if line_count == 10:
46
+ logStep = int(ws[0])
47
+
48
+ if len(ws) == 4:
49
+ type = float(ws[3])
50
+
51
+ if not type == BOUNDARY_TYPE:
52
+ worm_points.append([float(ws[0]), float(ws[1]), float(ws[2])])
53
+ types.append(type)
54
+ else:
55
+ if include_boundary:
56
+ boundary_points.append([float(ws[0]), float(ws[1]), float(ws[2])])
57
+
58
+ if logStep is not None:
59
+ pcount += 1
60
+
61
+ if pcount == numOfBoundaryP + numOfElasticP + numOfLiquidP:
62
+ print(
63
+ "End of one batch: %i worm points added, %i boundary points, %i total points at line %i, time: %i"
64
+ % (
65
+ len(worm_points),
66
+ len(boundary_points),
67
+ pcount,
68
+ line_count,
69
+ time_count,
70
+ )
71
+ )
72
+ all_worm_points.append(worm_points)
73
+
74
+ all_point_types.append(types)
75
+
76
+ worm_points = []
77
+ types = []
78
+ pcount = 0
79
+ numOfBoundaryP = 0
80
+
81
+ time_count += 1
82
+
83
+ line_count += 1
84
+
85
+ # all_points_np = np.array(all_points)
86
+
87
+ print(
88
+ f"Loaded positions with %i elastic, %i liquid and %i boundary points (%i total), %i lines"
89
+ % (
90
+ numOfElasticP,
91
+ numOfLiquidP,
92
+ numOfBoundaryP,
93
+ numOfElasticP + numOfLiquidP + numOfBoundaryP,
94
+ line_count,
95
+ )
96
+ )
97
 
98
+ print("Num of time points found: %i" % len(all_worm_points))
99
 
100
+ return all_worm_points, all_point_types
101
 
 
102
 
103
+ def toggle(flag):
104
+ print("Setting to: %s" % flag)
 
 
 
 
 
 
 
 
 
 
 
105
 
 
 
106
 
107
+ def create_plotter():
108
+ pl = pv.Plotter(window_size=[1200, 800])
109
+ pl.set_background("#bbbbbb")
110
 
111
+ pl.camera.position = (500, 300, 150.0)
112
+ pl.camera.focal_point = (50, 0, 150)
113
+ pl.camera.up = (0.0, 1.0, 0.0)
114
+ pl.add_axes_at_origin(labels_off=True)
115
+ # pl.add_mesh(pv.Cube(center=(50,0,50)))
 
 
116
 
117
+ # pl.add_checkbox_button_widget(toggle, value=True)
 
 
 
118
 
119
+ return pl
120
 
 
 
121
 
122
+ last_mesh = None
123
 
 
124
 
125
+ def create_mesh(step, plotter_=None):
126
+ colours = {1.1: "lightblue", 2.1: "green", 2.2: "turquoise", 3: "#eeeeee"}
127
+ colours = {1.1: "blue", 2.2: "#008080"}
128
+ step_count = step
129
+ value = step_count
130
 
131
+ global last_mesh
 
132
 
133
+ global all_worm_points
134
+ global boundary_points
135
+ global all_point_types
 
 
136
 
137
+ render_here = False
138
 
139
+ if plotter_ == None:
140
+ global plotter
141
+ render_here = True
142
+ else:
143
+ plotter = plotter_
144
 
 
 
 
 
145
  index = int(value)
146
 
147
+ print("Changing to time point: %s (%s) " % (index, value))
148
+
149
+ curr_points = all_worm_points[index]
150
  curr_types = all_point_types[index]
 
151
 
152
+ if last_mesh is None:
153
  last_mesh = pv.PolyData(curr_points)
154
  last_mesh["types"] = curr_types
155
  print(last_mesh)
156
+ print("Initial add of worm mesh")
157
+ last_actor = plotter.add_mesh(
158
  last_mesh,
159
+ render_points_as_spheres=False,
160
  cmap=[c for c in colours.values()],
161
  point_size=3,
162
+ show_scalar_bar=False,
163
  )
164
+
165
+ if len(boundary_points) > 0:
166
+ boundary_mesh = pv.PolyData(boundary_points)
167
+ # boundary_mesh["types"] = curr_t[ypes
168
+ print(boundary_mesh)
169
+ print("Initial add of boundary mesh")
170
+ boundary_actor = plotter.add_mesh(
171
+ boundary_mesh,
172
+ render_points_as_spheres=False,
173
+ point_size=3,
174
+ show_scalar_bar=False,
175
+ )
176
  else:
177
+ print("Updating mesh...")
178
  last_mesh.points = curr_points
179
 
180
+ plotter.render()
181
+
182
+ time.sleep(0.1)
183
 
 
 
184
  return
 
 
185
 
 
 
 
186
 
187
+ if __name__ == "__main__":
188
+ if len(sys.argv) == 2:
189
+ filename = sys.argv[1]
190
+
191
+ load_points(filename)
192
+
193
+ plotter = create_plotter()
194
+ create_mesh(0)
195
+
196
+ max_time = len(all_worm_points) - 1
197
+
198
+ plotter.add_slider_widget(
199
+ create_mesh, rng=[0, max_time], value=max_time, title="Time point"
200
+ )
201
+ plotter.add_timer_event(
202
+ max_steps=len(all_worm_points), duration=200, callback=create_mesh
203
+ )
204
 
205
+ plotter.show()