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
| import os import random
import dash import dash_bootstrap_components as dbc import dash_html_components as html import dash_core_components as dcc from dash.dependencies import Input, Output, State
import dash_vtk from dash_vtk.utils import to_volume_state
import vtk
random.seed(42)
head_vti = "head.vti"
reader = vtk.vtkXMLImageDataReader() reader.SetFileName(head_vti) reader.Update()
volume_state = to_volume_state(reader.GetOutput())
sliders = { "Slice i": dcc.Slider(id="slider-i", min=0, max=256, value=128), "Slice j": dcc.Slider(id="slider-j", min=0, max=256, value=128), "Slice k": dcc.Slider(id="slider-k", min=0, max=95, value=47), "Color Level": dcc.Slider(id="slider-lvl", min=0, max=4095, value=1000), "Color Window": dcc.Slider(id="slider-window", min=0, max=4095, value=4095), }
controls = dbc.Card( body=True, children=dbc.Row( [ dbc.Col([dbc.Label(label), component], style={"width": "150px"}) for label, component in sliders.items() ] ), )
slice_property = {"colorWindow": 4095, "colorLevel": 1000}
slice_view = dash_vtk.View( id="slice-view", cameraPosition=[1, 0, 0], cameraViewUp=[0, 0, -1], cameraParallelProjection=False, background=[0.9, 0.9, 1], children=[ dash_vtk.ShareDataSet(dash_vtk.Volume(state=volume_state)), dash_vtk.SliceRepresentation( id="slice-repr-i", iSlice=128, property=slice_property, children=dash_vtk.ShareDataSet(), ), dash_vtk.SliceRepresentation( id="slice-repr-j", jSlice=128, property=slice_property, children=dash_vtk.ShareDataSet(), ), dash_vtk.SliceRepresentation( id="slice-repr-k", kSlice=47, property=slice_property, children=dash_vtk.ShareDataSet(), ), ], )
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) server = app.server
app.layout = dbc.Container( fluid=True, style={"height": "calc(100vh - 30px)"}, children=[ html.Div( style={"height": "20%", "display": "flex", "alignItems": "center"}, children=[ html.Br(), controls, html.Br(), ], ), html.Div(slice_view, style={"height": "80%"}), ], )
@app.callback( [ Output("slice-view", "triggerRender"), Output("slice-repr-i", "property"), Output("slice-repr-i", "iSlice"), Output("slice-repr-j", "property"), Output("slice-repr-j", "jSlice"), Output("slice-repr-k", "property"), Output("slice-repr-k", "kSlice"), ], [ Input("slider-i", "value"), Input("slider-j", "value"), Input("slider-k", "value"), Input("slider-lvl", "value"), Input("slider-window", "value"), ], ) def update_slice_property(i, j, k, level, window): render_call = random.random() slice_prop = {"colorLevel": level, "colorWindow": window} return render_call, slice_prop, i, slice_prop, j, slice_prop, k
if __name__ == "__main__": app.run_server(debug=True)
|