-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVispy3DViewer.py
More file actions
235 lines (198 loc) · 9.52 KB
/
Vispy3DViewer.py
File metadata and controls
235 lines (198 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import numpy as np
# import vispy.app
import vispy.scene
from vispy import app, scene
from vispy.scene import visuals
import Mesh_functions as mf
class Visp3dplot():
""" Class that handles all the 3DViewer graphics.
The view is defined into the __init__ method, while
other ad-hoc methods are used to add items to it.
"""
def __init__(self, resolution):
self.resolution = resolution
self.canvas = vispy.scene.SceneCanvas(title='3D Viewer', keys='interactive', show=True, bgcolor='white')
self.view3d = self.canvas.central_widget.add_view()
def print_cloud(self, plotdata, alpha):
""" :param plotdata: 3-columns np array (mct.pcl or mct.netpcl)
"""
self.scatter = visuals.Markers()
if self.resolution == 1:
self.pcl3dplotdata = plotdata
else:
self.npts_sub = int(round(self.resolution * plotdata.shape[0]))
self.randpts = np.random.choice(plotdata.shape[0], size=self.npts_sub, replace=False)
self.pcl3dplotdata = plotdata[self.randpts]
self.scatter.set_data(self.pcl3dplotdata, symbol='disc',
face_color=(255 / 255, 255 / 255, 255 / 255, alpha), size=1.0) ################### default size = 2.7
self.view3d.add(self.scatter)
def print_slices(self, mct):
try:
self.slices = visuals.Markers()
self.sliceplot = mct.slices[mct.zcoords[0]]
for i in mct.zcoords[1:]:
self.sliceplot = np.vstack((self.sliceplot, mct.slices[i]))
self.slices.set_data(self.sliceplot, symbol='disc',
face_color=(0 / 255, 0 / 255, 255 / 255, 1), size=4.3)
self.view3d.add(self.slices)
except TypeError:
print("Error in Visp3dplot.print_slices(): generate the slices first")
def print_centr(self, mct):
self.centroids = visuals.Markers()
self.centrplot = mct.ctrds[mct.zcoords[0]]
for i in mct.zcoords[1:]:
self.centrplot = np.vstack((self.centrplot, mct.ctrds[i]))
self.centroids.set_data(self.centrplot, symbol='disc',
face_color=(255 / 255, 0 / 255, 0 / 255, 1), size=7)
self.view3d.add(self.centroids)
def print_polylines(self, mct):
xyzplines = []
for z in mct.zcoords:
for poly in mct.cleanpolys[z]:
zcolumn = np.zeros((poly.shape[0], 1)) + z
xyzplines += [np.hstack((poly, zcolumn))]
plotitems = []
for i in range(len(xyzplines)):
plotitems += [visuals.Line()]
plotitems[i].set_data(xyzplines[i], color=(0.05, 0.05, 1, 1), width=1)
self.view3d.add(plotitems[i])
vertices = xyzplines[0]
for poly in xyzplines[1:]:
vertices = np.vstack((vertices, poly))
self.vertices = visuals.Markers()
self.vertices.set_data(vertices, symbol='square',
face_color=(220 / 255, 30 / 255, 30 / 255, 1), size=2.7)
self.view3d.add(self.vertices)
# def print_mesh_1(self, mct):
# vertices
# color=(0, 0, 1, 0.5)):
# canvas = scene.SceneCanvas(keys='interactive', bgcolor='white')
# view = canvas.central_widget.add_view()
# view.camera = 'turntable'
# # Create vertices and edges
# vertices = np.array(vertices)
# # Create a PolygonVisual to represent the filled polygon
# polygon = scene.visuals.Polygon(vertices=vertices, color=color, method='gl', parent=view.scene)
# # Show the canvas
# canvas.show()
# # Run the app
# if not hasattr(app, 'event_loop') or not app.event_loop.is_running():
# app.run()
def print_mesh(self, mct): # Metodo provvisiorio
# mct.nodelist, row[i] = [nodeID, x, y, z]
# mct.elconnect, row[i] = [edelmID, nID1, nID2, nID3, nID4, nID5, nID6, nID7, nID8]
faces = []
for i in range(len(mct.elconnect)):
n1 = mct.nodelist[np.where(mct.elconnect[i, 1] == mct.nodelist[:, 0])][0][1:]
n2 = mct.nodelist[np.where(mct.elconnect[i, 2] == mct.nodelist[:, 0])][0][1:].tolist()
n3 = mct.nodelist[np.where(mct.elconnect[i, 3] == mct.nodelist[:, 0])][0][1:].tolist()
n4 = mct.nodelist[np.where(mct.elconnect[i, 4] == mct.nodelist[:, 0])][0][1:].tolist()
n5 = mct.nodelist[np.where(mct.elconnect[i, 5] == mct.nodelist[:, 0])][0][1:].tolist()
n6 = mct.nodelist[np.where(mct.elconnect[i, 6] == mct.nodelist[:, 0])][0][1:].tolist()
n7 = mct.nodelist[np.where(mct.elconnect[i, 7] == mct.nodelist[:, 0])][0][1:].tolist()
n8 = mct.nodelist[np.where(mct.elconnect[i, 8] == mct.nodelist[:, 0])][0][1:].tolist()
faces += [np.array([n1, n2, n3, n4])]
faces += [np.array([n5, n6, n7, n8])]
faces += [np.array([n5, n1, n2, n6])]
faces += [np.array([n6, n2, n3, n7])]
faces += [np.array([n7, n3, n4, n8])]
faces += [np.array([n8, n4, n1, n5])]
# if i == 1:
# print(faces)
# #
# face1 = np.array([[1, 0., 0], [2, 0, 0], [3, 4., 0], [1, 4, 0]])
# face2 = face1 * 3
# face3 = face1 * 6
# faces = [face1, face2, face3]
# print('face inventata shape: ', faces[1].shape)
# # print('faces: ', faces)
# print('len faces: ', len(faces))
# print('face1:', faces[1])
# print('type face 1[0]:', type(faces[1][0][0]))
#
# faceplot = []
# for j in range(len(faces)):
# faceplot += [visuals.Polygon(color=(1, 1, 1, 1))]
#
# for j in range(len(faceplot)):
# faceplot[j].pos = faces[j]
# self.view3d.add(faceplot[j])
#
#
# # GENERAZIONE ARRAY DI NODI
nodes = np.array([0, 0, 0]) # Riga in più da togliere
for face in faces:
for node in face:
nodes = np.vstack((nodes, node))
# LINEE STACCATE
# plotitems = []
# for i in range(int(len(faces))):
# if i < 5200:
# continue
# plotitems += [visuals.Line()]
# plotitems[i].set_data(faces[i], color=(0.05, 0.05, 1, 1), width=1)
# self.view3d.add(plotitems[i])
# LINEE ATTACCATE
self.lines = visuals.Line()
self.lines.set_data(nodes[1:], color=(0.1, 0.3, 0.7, 1), width=1)
self.view3d.add(self.lines)
# PUNTI
self.nodes = visuals.Markers()
self.nodes.set_data(nodes, symbol='square',
face_color=(220 / 255, 30 / 255, 30 / 255, 1), size=3.5)
self.view3d.add(self.nodes)
# SINGOLO POLIGONO FUNZIONA
# faceplot = visuals.Polygon(pos=faces[1], color=(1, 1, 1, 1))
# self.view3d.add(faceplot)
# SINGOLO POLIGONO TEST
# polyg1 = np.array([[1, 0, 0], [2, 0, 0], [3, 4, 0], [1, 4, 0]])
# plotpolyg = visuals.Polygon(pos=polyg1, color=(1, 1, 1, 1))
# view.add(plotpolyg)
def print_mesh2(self, mct): # Metodo provvisiorio
# mct.nodelist, row[i] = [nodeID, x, y, z]
# mct.elconnect, row[i] = [edelmID, nID1, nID2, nID3, nID4, nID5, nID6, nID7, nID8]
vxyz = mct.nodelist
xyz = vxyz[:,1:4]
LCOf = mct.elconnect
csize = LCOf.shape[1]
LCO = LCOf[:,1:csize].astype(int)
boundary_faces = mf.find_external_faces(LCO)
# Create a scatter plot of the points
scatter = scene.visuals.Markers()
scatter.set_data(xyz, edge_color=None, face_color=(1, 1, 1, 1), size=5)
self.view3d.add(scatter)
# Create polygons for the faces
for face_indices in boundary_faces:
face_indices_minus_1 = list([index - 1 for index in face_indices])
try:
vertices1 = xyz[face_indices_minus_1,:]
polygon = scene.visuals.Polygon(vertices=vertices1, faces=face_indices_minus_1, color=(0.2, 0.4, 0.6, 0.7))
self.view3d.add(polygon)
# Create Line visuals for the edges of the rectangle
edges = np.array([[0, 1], [1, 2], [2, 3], [3, 0]], dtype=np.uint32)
rectangle_edges = scene.visuals.Line(vertices=vertices[edges], color='black')
self.view3d.add(rectangle_edges)
print(face_indices, ' plotted!')
except:
try:
vertices = xyz[list(reversed(face_indices_minus_1)),:]
polygon = scene.visuals.Polygon(pos=vertices, color=(0.2, 0.4, 0.6, 0.7))
self.view3d.add(polygon)
rectangle_edges = scene.visuals.Line(vertices=np.vstack((vertices, vertices[0])), color='black')
self.view3d.add(rectangle_edges)
print(face_indices, ' plotted!')
# print(max(face_indices_minus_1))
except:
continue
self.view3d.camera.set_range()
# SINGOLO POLIGONO FUNZIONA
# faceplot = visuals.Polygon(pos=faces[1], color=(1, 1, 1, 1))
# self.view3d.add(faceplot)
# SINGOLO POLIGONO TEST
# polyg1 = np.array([[1, 0, 0], [2, 0, 0], [3, 4, 0], [1, 4, 0]])
# plotpolyg = visuals.Polygon(pos=polyg1, color=(1, 1, 1, 1))
# view.add(plotpolyg)
def final3dsetup(self):
self.view3d.camera = 'turntable' # 'turntable' # or 'arcball'
axis = visuals.XYZAxis(parent=self.view3d.scene)
# vispy.app.run()