Skip to content

Commit 4dd9045

Browse files
author
Xing Han Lu
authored
Merge pull request #626 from plotly/add-probing
Add probing to vehicle-geometry app Former-commit-id: ba63e77
2 parents 83c2d1e + 894d972 commit 4dd9045

6 files changed

Lines changed: 107 additions & 15 deletions

File tree

apps/dash-vehicle-geometry/app.py

Lines changed: 106 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]):
2525
reader = vtk.vtkXMLPolyDataReader()
2626
reader.SetFileName(filepath)
2727
reader.Update()
28+
2829
return to_mesh_state(reader.GetOutput(), fieldname, point_arrays, cell_arrays)
2930

3031

32+
def cache_mesh(filepath, di, fieldname=None, point_arrays=[], cell_arrays=[]):
33+
reader = vtk.vtkXMLPolyDataReader()
34+
reader.SetFileName(filepath)
35+
reader.Update()
36+
37+
# Cache mesh for future lookup
38+
part_name = filepath.split("/")[-1].replace(".vtp", "")
39+
di[part_name] = reader.GetOutput()
40+
41+
3142
# -----------------------------------------------------------------------------
3243
# GUI setup
3344
# -----------------------------------------------------------------------------
@@ -48,7 +59,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]):
4859
vehicle_mesh_ids = []
4960
vehicle_meshes = []
5061

51-
for filename in glob.glob(os.path.join(DATA_PATH, "vehicle") + "/*.vtp"):
62+
for filename in glob.glob(os.path.join(DATA_PATH, "vehicle-vect") + "/*.vtp"):
5263
mesh = _load_vtp(filename, point_arrays=["U", "p"])
5364
part_name = filename.split("/")[-1].replace(".vtp", "")
5465
child = dash_vtk.GeometryRepresentation(
@@ -87,13 +98,20 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]):
8798
isomesh_ids.append(f"{surf_name}-mesh")
8899
isosurfs_meshes.append(mesh)
89100

101+
# time to cache meshes
102+
vtk_datasets = {}
103+
for filename in glob.glob(os.path.join(DATA_PATH, "vehicle-vect") + "/*.vtp"):
104+
cache_mesh(filename, vtk_datasets)
105+
106+
for filename in glob.glob(os.path.join(DATA_PATH, "isosurfaces") + "/*.vtp"):
107+
cache_mesh(filename, vtk_datasets)
90108

91109
# -----------------------------------------------------------------------------
92110
# 3D Viz
93111
# -----------------------------------------------------------------------------
94112

95113
# vtk_view = dash_vtk.View(id="vtk-view", children=vehicle_vtk + isosurfs_vtk)
96-
vtk_view = dash_vtk.View(id="vtk-view")
114+
# vtk_view = dash_vtk.View(id="vtk-view", pickingModes=['hover'])
97115

98116
# -----------------------------------------------------------------------------
99117
# Control UI
@@ -173,23 +191,34 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]):
173191
width=8,
174192
children=[
175193
html.Div(
176-
dbc.Spinner(
177-
html.Div(
178-
id="vtk-view-container",
179-
style={
180-
"height": "calc(100vh - 230px)",
181-
"width": "100%",
182-
},
183-
),
184-
color="light",
194+
children=html.Div(
195+
dbc.Spinner(color="light"),
196+
style={
197+
"background-color": "#334c66",
198+
"height": "calc(100vh - 230px)",
199+
"width": "100%",
200+
"text-align": "center",
201+
"padding-top": "calc(50vh - 105px)",
202+
},
185203
),
186-
style={"background-color": "#334c66"},
187-
)
204+
id="vtk-view-container",
205+
style={"height": "calc(100vh - 230px)", "width": "100%",},
206+
),
188207
],
189208
),
190209
],
191210
style={"margin-top": "15px", "height": "calc(100vh - 230px)"},
192211
),
212+
html.Pre(
213+
id="tooltip",
214+
style={
215+
"position": "absolute",
216+
"bottom": "25px",
217+
"left": "25px",
218+
"zIndex": 1,
219+
"color": "white",
220+
},
221+
),
193222
],
194223
)
195224

@@ -206,7 +235,16 @@ def initial_loading(geometry):
206235
if triggered:
207236
return dash.no_update
208237

209-
return dash_vtk.View(id="vtk-view", children=vehicle_vtk + isosurfs_vtk)
238+
cone_pointer = dash_vtk.GeometryRepresentation(
239+
property={"color": [1, 0, 0]},
240+
children=[dash_vtk.Algorithm(id="pointer", vtkClass="vtkConeSource")],
241+
)
242+
243+
return dash_vtk.View(
244+
id="vtk-view",
245+
children=vehicle_vtk + isosurfs_vtk + [cone_pointer],
246+
pickingModes=["hover"],
247+
)
210248

211249

212250
@app.callback(
@@ -267,6 +305,60 @@ def update_scene(geometry, isosurfaces, surfcolor):
267305
return [random.random()] + surf_state + geo_viz + color_ranges + [iso_viz]
268306

269307

308+
SCALE_P = 0.0001
309+
SCALE_U = 0.01
310+
311+
312+
@app.callback(
313+
[Output("tooltip", "children"), Output("pointer", "state"),],
314+
[Input("vtk-view", "hoverInfo"),],
315+
)
316+
def probe_data(info):
317+
cone_state = {"resolution": 12}
318+
if info:
319+
if "representationId" not in info:
320+
return dash.no_update, dash.no_update
321+
ds_name = info["representationId"].replace("-rep", "")
322+
mesh = vtk_datasets[ds_name]
323+
if mesh:
324+
xyx = info["worldPosition"]
325+
idx = mesh.FindPoint(xyx)
326+
if idx > -1:
327+
cone_state["center"] = mesh.GetPoints().GetPoint(idx)
328+
messages = []
329+
pd = mesh.GetPointData()
330+
size = pd.GetNumberOfArrays()
331+
for i in range(size):
332+
array = pd.GetArray(i)
333+
name = array.GetName()
334+
nb_comp = array.GetNumberOfComponents()
335+
value = array.GetValue(idx)
336+
value_str = f"{array.GetValue(idx):.2f}"
337+
norm_str = ""
338+
if nb_comp == 3:
339+
value = array.GetTuple3(idx)
340+
norm = (value[0] ** 2 + value[1] ** 2 + value[2] ** 2) ** 0.5
341+
norm_str = f" norm({norm:.2f})"
342+
value_str = ", ".join([f"{v:.2f}" for v in value])
343+
344+
cone_state["height"] = SCALE_U * norm
345+
cone_state["direction"] = [v / norm for v in value]
346+
347+
if name == "p":
348+
cone_state["radius"] = array.GetValue(idx) * SCALE_P
349+
350+
messages.append(f"{name}: {value_str} {norm_str}")
351+
352+
if "height" in cone_state:
353+
new_center = [v for v in cone_state["center"]]
354+
for i in range(3):
355+
new_center[i] -= 0.5 * cone_state["height"] * cone_state["direction"][i]
356+
cone_state["center"] = new_center
357+
358+
return ["\n".join(messages)], cone_state
359+
return [""], cone_state
360+
361+
270362
# -----------------------------------------------------------------------------
271363

272364
if __name__ == "__main__":
1.68 MB
Binary file not shown.
1.3 MB
Binary file not shown.
269 KB
Binary file not shown.
200 KB
Binary file not shown.

apps/dash-vehicle-geometry/requirements-real.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ appdirs==1.4.4
22
Brotli==1.0.9
33
click==7.1.2
44
dash==1.19.0
5-
dash-bootstrap-components==0.12.0
5+
dash-bootstrap-components==0.12.1
66
dash-core-components==1.15.0
77
dash-html-components==1.1.2
88
dash-renderer==1.9.0

0 commit comments

Comments
 (0)