-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_grid.py
182 lines (147 loc) · 5.37 KB
/
slice_grid.py
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
import dash
from dash import html
import dash_vtk
from dash_vtk.utils import to_mesh_state
from vtkmodules.vtkFiltersGeometry import vtkExplicitStructuredGridSurfaceFilter
from vtkmodules.vtkCommonDataModel import vtkPolyPlane
from vtkmodules.vtkCommonDataModel import vtkCellLocator
from vtkmodules.vtkFiltersCore import vtkCutter
from vtkmodules.vtkFiltersCore import vtkImplicitPolyDataDistance
from vtkmodules.vtkFiltersCore import vtkImplicitPolyDataDistance
from vtkmodules.vtkCommonCore import vtkIdList
# Not yet available!
#from vtkmodules.vtkFiltersCore import vtkExtractCellsAlongPolyLine
import numpy as np
import pyvista as pv
import grid_factory
grid = grid_factory.create_explicit_structured_grid(5, 4, 3, 20.0, 10.0, 5.0)
test_cell_blanking = True
if test_cell_blanking:
cellLocator = vtkCellLocator()
cellLocator.SetDataSet(grid)
cellLocator.BuildLocator()
cellIds = vtkIdList()
cellLocator.FindCellsAlongLine((6.0, 6.0, 12.0), (67.0, 12.0, 12.0), 0.001, cellIds)
for i in range(cellIds.GetNumberOfIds()):
id = cellIds.GetId(i)
grid.BlankCell(id)
extractSkinFilter = vtkExplicitStructuredGridSurfaceFilter()
extractSkinFilter.SetInputData(grid)
extractSkinFilter.Update()
polydata = extractSkinFilter.GetOutput()
# grid = grid.cast_to_unstructured_grid()
# polydata = grid.extract_geometry()
skin_mesh_state = to_mesh_state(polydata)
z = 14.0
line_verts = np.array([(-5.0, -1.0, z), (5.0, 5.0, z), (6.0, 5.0, z), (7.0, 4.0, z), (8.0, 9.0, z), (15.0, 12.0, z), (25.0, 23.0, z), (70.0, 31.0, z), (65.0, 15.0, z), (66.0, -2.0, z)])
#line_verts = np.array([(-5.0, -1.0, z), (5.0, 5.0, z), (6.0, 5.0, z), (7.0, 4.0, z), (8.0, 9.0, z), (15.0, 12.0, z), (25.0, 23.0, z), (70.0, 31.0, z), (65.0, 15.0, z)])
#line_verts = np.array([(5.0, 5.0, z), (6.0, 5.0, z), (7.0, 4.0, z), (8.0, 9.0, z), (15.0, 12.0, z), (25.0, 23.0, z), (70.0, 31.0, z), (65.0, 15.0, z)])
#line_verts = np.array([(5.0, 5.0, z), (15.0, 12.0, z), (25.0, 23.0, z), (70.0, 31.0, z), (65.0, 15.0, z)])
#line_verts = np.array([(-5.0, -1.0, z), (15.0, 12.0, z), (19.0, 23.0, z), (105.0, 38.0, z), (85.0, 25.0, z)])
#line_verts = np.array([(-5.0, -1.0, z), (105.0, 38.0, z)])
vertex_count = len(line_verts)
line_conn = np.arange(-1, vertex_count)
line_conn[0] = vertex_count
my_polyline = pv.PolyData(line_verts, lines=line_conn);
polyline_mesh_state = to_mesh_state(my_polyline)
cut_func = vtkPolyPlane()
cut_func.SetPolyLine(my_polyline.GetCell(0))
# poly_verts = line_verts.repeat(2, axis=0)
# poly_verts = poly_verts.reshape(-1, 3)
# num_poly_verts = 2*len(line_verts)
# for i in range(1, num_poly_verts, 2):
# poly_verts[i][2] = 10.0
# num_polys = int(len(poly_verts)/2 - 1)
# poly_conn = np.zeros(5*num_polys, dtype=int)
# for i in range(0, num_polys):
# poly_conn[5*i] = 4
# poly_conn[5*i + 1] = 2*i
# poly_conn[5*i + 2] = 2*i + 1
# poly_conn[5*i + 3] = 2*i + 3
# poly_conn[5*i + 4] = 2*i + 2
# print(poly_verts)
# print(poly_conn)
# cut_poly_data = pv.PolyData(poly_verts, faces=poly_conn);
# cut_func = vtkImplicitPolyDataDistance()
# cut_func.SetInput(cut_poly_data)
alg = vtkCutter()
alg.SetInputDataObject(grid)
alg.SetCutFunction(cut_func)
alg.Update()
intersection = alg.GetOutput()
#intersection = grid.slice_along_line(my_polyline)
#intersection = grid.slice(normal=(-1.0, 2.3, 0.0), origin=(0.0, 0.0, 0.0), generate_triangles=True)
# Does not yet exist in the VTK version
# alg = vtkExtractCellsAlongPolyLine()
# alg.SetInput(0, grid)
# alg.SetInput(1, my_polyline)
# alg.Update()
# intersection = alg.GetOutput()
isect_mesh_state = to_mesh_state(intersection)
content = dash_vtk.View([
dash_vtk.GeometryRepresentation(
property={
"lighting": True,
"edgeVisibility": True,
"opacity": 0.5,
"color": [0.5, 0.5, 0.1],
"lineWidth": 5,
"pointSize": 5,
"representation": 2, # 0=points, 1=wireframe, 2=surf
},
children=[
dash_vtk.Mesh(state=skin_mesh_state)
]
),
dash_vtk.GeometryRepresentation(
property={
"lighting": False,
"color": [1.0, 1.0, 0.1],
"pointSize": 5,
"representation": 0, # 0=points, 1=wireframe, 2=surf
},
children=[
dash_vtk.Mesh(state=polyline_mesh_state),
]
),
dash_vtk.GeometryRepresentation(
property={
"lighting": False,
"color": [1.0, 0.5, 0.1],
"lineWidth": 5,
},
children=[
dash_vtk.Mesh(state=polyline_mesh_state),
]
),
# dash_vtk.GeometryRepresentation(
# property={
# "color": [0.8, 0.2, 0.1],
# "edgeVisibility": True,
# "representation": 2, # 0=points, 1=wireframe, 2=surf
# },
# children=[
# dash_vtk.Mesh(state=to_mesh_state(cut_poly_data)),
# ]
# ),
dash_vtk.GeometryRepresentation(
property={
"lighting": False,
"edgeVisibility": True,
"color": [0.3, 0.8, 0.1],
"lineWidth": 5,
},
children=[
dash_vtk.Mesh(state=isect_mesh_state),
]
),
])
# Dash setup
app = dash.Dash(__name__)
server = app.server
app.layout = html.Div(
style={"width": "100%", "height": "1000px"},
children=[content],
)
if __name__ == "__main__":
app.run_server(debug=True)